Commit Graph

943 Commits

Author SHA1 Message Date
Jussi Saurio
cfa449a0c0 Merge 'Disallow multiple primary keys in table definition' from Jussi Saurio
Closes #3309

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #3340
2025-09-26 09:16:14 +03:00
Jussi Saurio
064cc04b69 Merge 'Fix CREATE INDEX with quoted identifiers' from Iaroslav Zeigerman
Discovered this one while working on #3322
It was a bit more elusive because the original error was essentially a
red herring:
```
turso> CREATE INDEX idx ON "t t" (`a a`);
  × unexpected token at SourceSpan { offset: SourceOffset(22), length: 1 }
   ╭────
 1 │ CREATE INDEX idx ON "t t" (`a a`);
   ·                       ┬
   ·                       ╰── here
   ╰────
  help: expected [TK_LP] but found TK_ID
```

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3345
2025-09-26 09:13:30 +03:00
Jussi Saurio
c83ee9fc3e Merge 'Fix ungrouped aggregate with offset clause' from Preston Thorpe
closes #3300

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3347
2025-09-26 09:13:16 +03:00
Jussi Saurio
9549ca5d51 Merge 'translate/update: allow for updating nonalias'd rowid column explicitly ' from Preston Thorpe
closes #3276

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3349
2025-09-26 09:13:03 +03:00
Jussi Saurio
6d6fc91da3 Disallow multiple primary keys in table definition 2025-09-26 08:59:36 +03:00
PThorpe92
04de70fd66 Update test to reflect lack of err message when .schema nonexistant table is ran 2025-09-25 19:44:42 -04:00
PThorpe92
9d4dee01fb Add regression test for explicit rowid update 2025-09-25 19:16:20 -04:00
PThorpe92
05cab4bb91 Add test for ungrouped aggregate with offset 2025-09-25 16:47:26 -04:00
Preston Thorpe
d44a1f8bca Merge 'Implement json_tree' from Mikaël Francoeur
This PR implements the `json_tree` table-valued function.
It's not 100% compatible with SQLite, because SQLite does some iffy
things with the `key` and `path` columns. I started a
[thread](https://www.sqlite.org/forum/forumpost/48f5763d8c) on their
forum and I linked it to the disabled tests in `json.test`.

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3256
2025-09-25 16:45:44 -04:00
Iaroslav Zeigerman
b34e53d6c6 Fix CREATE INDEX with quoted identifiers 2025-09-25 12:55:06 -07:00
Pekka Enberg
c255e456d6 Merge 'Fix incorrect "column is ambiguous" error with USING clause' from Jussi Saurio
Closes #3299

Closes #3343
2025-09-25 21:02:36 +03:00
Pekka Enberg
b3a0008950 Merge 'parser: fix incorrect LIMIT/OFFSET parsing of form LIMIT x,y' from Jussi Saurio
In this case x is the OFFSET and y is the LIMIT, not the other way
around.
Closes #3303

Closes #3339
2025-09-25 21:01:40 +03:00
Pere Diaz Bou
91cff65e44 Merge 'Autoincrement' from Pavan Nambi
fixes #1976
and #1605
```zsh
turso> DROP TABLE IF EXISTS t;
CREATE TABLE t (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT
);
turso> INSERT INTO t (name) VALUES ('A'); SELECT * FROM sqlite_sequence;
┌──────┬─────┐
│ name │ seq │
├──────┼─────┤
│ t    │   1 │
└──────┴─────┘
turso> DROP TABLE IF EXISTS t;
CREATE TABLE t (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT
);
turso> INSERT INTO t (name) VALUES ('A'); SELECT * FROM sqlite_sequence;
┌──────┬─────┐
│ name │ seq │
├──────┼─────┤
│ t    │   1 │
└──────┴─────┘
turso> INSERT INTO t (name) VALUES ('A'); SELECT * FROM sqlite_sequence;
┌──────┬─────┐
│ name │ seq │
├──────┼─────┤
│ t    │   2 │
└──────┴─────┘
turso>
```

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #2983
2025-09-25 18:57:24 +02:00
Pekka Enberg
f8130d051f Merge 'Fix JavaScript bindings' from Nikita Sivukhin
This PR makes all JS db packages to have uniform interface: `new
Database(...)` constructor with explicit `connect()` and `close()`
methods.
Also, this PR adds docstrings in the code and properly support few
better-sqlite options (readonly, fileMustExist, timeout)

Closes #3334
2025-09-25 17:24:03 +03:00
Jussi Saurio
00466aa84e Fix incorrect "column is ambiguous" error with USING clause 2025-09-25 16:06:40 +03:00
Jussi Saurio
bb082c25f5 Fix incorrect LIMIT/OFFSET parsing of form LIMIT x,y
In this case x is the OFFSET and y is the LIMIT, not the
other way around.
2025-09-25 15:10:14 +03:00
Jussi Saurio
8f46721848 Merge 'Normalize target table name identifier on table or column rename' from Iaroslav Zeigerman
Closes #3320
Closes #3286
In addition to the problem reported in the ticket, 2 more issues were
identified:
1. Renaming a column for a table with a special character in its name
failed with
```
turso> CREATE TABLE `t t`(a);
turso> ALTER TABLE `t t` RENAME COLUMN a TO `a a`;

thread 'main' panicked at core/vdbe/execute.rs:7870:14:
table being renamed should be in schema
```
2. The renamed table in the `sql` column of `sqlite_schema` was not
reflected correctly after renaming:
```
turso>  select * from sqlite_schema;
┌───────┬──────┬──────────┬──────────┬──────────────────────┐
│ type  │ name │ tbl_name │ rootpage │ sql                  │
├───────┼──────┼──────────┼──────────┼──────────────────────┤
│ table │ t t  │ t t      │        2 │ CREATE TABLE t t (a) │
└───────┴──────┴──────────┴──────────┴──────────────────────┘
```
3. `sql` for indexes was not reflected correctly after renaming a column
that contains special characters:
```
turso> ALTER TABLE `t t` RENAME COLUMN `a a` TO `b b`;
turso> SELECT sql FROM sqlite_schema;

┌───────────────────────────────────┐
│ sql                               │
├───────────────────────────────────┤
│ CREATE TABLE `t t` (`b b`)        │
├───────────────────────────────────┤
│ CREATE INDEX idx ON `t t` (`a a`) │
├───────────────────────────────────┤
```

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3322
2025-09-25 11:41:45 +03:00
Nikita Sivukhin
6015dee36e fix tests in testing/javascript 2025-09-25 12:07:57 +04:00
Pekka Enberg
b81cc1da1d Merge 'Fix: NULL values in group_concat()' from
Closes #3304.
group_concat() now ignores null values.

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3321
2025-09-25 10:19:41 +03:00
Iaroslav Zeigerman
4799f23261 Fix: Normalize the target table name identifier when renaming a table / column 2025-09-24 22:23:04 -07:00
Jussi Saurio
c18c44b032 fix: result columns have varying binding precedence
In e.g. `SELECT x AS y, y AS x FROM t ORDER BY x;`, the `x` in the
`ORDER BY` should reference t.y, which has been aliased as `x` for this
query. The same goes for GROUP BY, JOIN ON etc. but NOT for WHERE.

Previously we had wrong precedence in `bind_and_rewrite_expr`.
2025-09-25 08:07:37 +03:00
rajajisai
b9ef9d6c63 Ignore null values in group concat 2025-09-24 18:11:02 -04:00
Pavan-Nambi
49d5141f2d Merge remote-tracking branch 'origin/main' into cdc_fail_autoincrement 2025-09-24 18:06:02 +05:30
Pekka Enberg
d0e15f9ac0 Merge 'Fix INSERT INTO t DEFAULT VALUES' from Jussi Saurio
Closes #3279

Closes #3291
2025-09-24 11:09:27 +03:00
Jussi Saurio
5c82b72e5f fix INSERT INTO t DEFAULT VALUES 2025-09-24 09:54:43 +03:00
Jussi Saurio
726bc24e78 Support referring to rowid as _rowid_ or oid 2025-09-24 09:17:28 +03:00
Jussi Saurio
73571d9c55 Merge 'Don't allow duplicate columns and get column type as passed ' from Pavan Nambi
fixes #3231
```zsh

❯ sqlite3
SQLite version 3.50.4 2025-07-30 19:33:53
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite>   CREATE TABLE t1 (a);
    ALTER TABLE t1 ADD COLUMN a;
Parse error: duplicate column name: a
sqlite>   ALTER TABLE t1 ADD COLUMN name varchar(255);
    SELECT sql FROM sqlite_schema WHERE name = 't1';
CREATE TABLE t1 (a, name varchar(255))
sqlite>
```
```zsh
turso>
turso>  CREATE TABLE t1 (a);
    ALTER TABLE t1 ADD COLUMN a;
  x Parse error: duplicate column name: a

turso>  ALTER TABLE t1 ADD COLUMN name varchar(255);
    SELECT sql FROM sqlite_schema WHERE name = 't1';
┌─────────────────────────────────────────┐
│ sql                                     │
├─────────────────────────────────────────┤
│ CREATE TABLE t1 (a, name varchar (255)) │
└─────────────────────────────────────────┘
turso>
```

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #3249
2025-09-24 09:04:08 +03:00
Jussi Saurio
d5de088abe Merge 'translate: implement Sequence opcode and fix sort order' from Preston Thorpe
This PR implements the `Sequence` and `SequenceTest` opcodes, although
does not yet add plumbing to emit the latter.
SQLite has two distinct mechanisms that determine the final row order
with aggregates:
Traversal order of GROUP BY, and ORDER BY tiebreaking. When ORDER BY
contains only aggregate expressions and/or constants, SQLite has no
extra tiebreak key, but when ORDER BY mixes aggregate and non-aggregate
terms, SQLite adds an implicit, stable row `sequence` so “ties” respect
the input order.
This PR also fixes an issue with a query like the following:
```sql
SELECT u.first_name, COUNT(*) AS c
FROM users u
JOIN orders o ON o.user_id = u.id
GROUP BY u.first_name
ORDER BY c DESC;
```
Because ORDER BY has only an aggregate (COUNT(*) DESC) and no non-
aggregate terms, SQLite traverses the group key (u.first_name) in DESC
order in this case, so ties on c naturally appear with group keys in
descending order.
Previously tursodb would return the group key sorted in ASC order,
because it was used in all cases as the default

Closes #3287
2025-09-24 08:38:08 +03:00
PThorpe92
4cc02dbed1 Add regression test for sort order with aggregate term in orderby with groupby 2025-09-23 23:08:31 -04:00
Mikaël Francoeur
3e915d9868 implement json_tree 2025-09-23 14:22:02 -04:00
Pavan-Nambi
59660f0c25 remove tests for column type spaces 2025-09-23 12:48:35 +05:30
Pavan-Nambi
8e02855c98 update seq table onconflict nothing too and refactor logic into seperate function
add testcase for failed fuzz

more

remove autoincrement if col aint integer

fmt
2025-09-23 11:41:52 +05:30
PThorpe92
efe7891482 Add test to ensure we return proper changes() 2025-09-22 19:18:18 -04:00
Pavan Nambi
f1ac855441 Merge branch 'main' into cdc_fail_autoincrement 2025-09-22 21:11:26 +05:30
Pavan-Nambi
cd1f1ce46e add tests for duplicate columns and column type
fmt

correct expected values spaces
2025-09-22 19:19:06 +05:30
Preston Thorpe
44dc4c9636 Merge 'translate/emitter: Implement partial indexes' from Preston Thorpe
This PR adds support for partial indexes, e.g. `CREATE INDEX` with a
provided predicate
```sql
CREATE UNIQUE INDEX idx_expensive ON products(sku) where price > 100;
```
The PR does not yet implement support for using the partial indexes in
the optimizer.

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3228
2025-09-22 09:09:54 -04:00
Glauber Costa
2627ad44de support union statements in the DBSP circuit compiler 2025-09-21 21:00:27 -03:00
Pavan Nambi
47194d7658 Merge branch 'tursodatabase:main' into cdc_fail_autoincrement 2025-09-21 16:03:38 +05:30
PThorpe92
33538a1ebf Add some tests for UPSERT with partial indexes 2025-09-20 18:32:50 -04:00
PThorpe92
0f771ecb5d Fix tests to assert for UNIQUE constraint failed: table.col_name instead of idx name 2025-09-20 15:02:38 -04:00
PThorpe92
67cb59d9a7 Add UPDATE tests for partial index behavior 2025-09-20 14:38:49 -04:00
PThorpe92
2d952feae3 Add DELETE behavior tests for partial indexes 2025-09-20 14:38:49 -04:00
PThorpe92
4c6917f849 Add testing for partial indexes 2025-09-20 14:38:48 -04:00
Glauber Costa
e5a106d8d6 enable joins in IncrementalView 2025-09-19 03:59:28 -05:00
Pavan-Nambi
020921f803 Merge remote-tracking branch 'upstream/main' into cdc_fail_autoincrement 2025-09-18 19:27:19 +05:30
Jussi Saurio
1d2b461a2a Merge 'Compat: Translate the 2nd argument of group_concat / string_agg' from Iaroslav Zeigerman
Fixes #3140

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3155
2025-09-18 09:23:05 +03:00
PThorpe92
faa3113c77 Add regression test for table creation pk autoincrement constraint with backticks 2025-09-17 15:53:35 -04:00
Preston Thorpe
8c53d7f024 Merge 'translation: rewrite expressions and properly handle quoted identifiers in UPSERT' from Preston Thorpe
This PR fixes bugs found in the [turso-
go](https://github.com/tursodatabase/turso-go) driver with UPSERT clause
earlier, where `Gorm` will (obviously) use Expr::Variable's as well as
use quotes for `Expr::Qualified` in the tail end of an UPSERT statement.
Example:
```sql
INSERT INTO users (a,b,c) VALUES (?,?,?) ON CONFLICT (`users`.`a`) DO UPDATE SET b = `excluded`.`b`, a = ?;
```
and previously we were not properly calling `rewrite_expr`, which was
not properly setting the anonymous `Expr::Variable` to `__param_N` named
parameter, so it would ignore it completely, then return the wrong # of
parameters.
Also, we didn't handle quoted "`excluded`.`x`", so it would panic in the
optimizer that Qualified should have been rewritten earlier.

Closes #3157
2025-09-17 11:25:13 -04:00
Iaroslav Zeigerman
29e0cabf2a Compat: Translate the 2nd argument of group_concat / string_agg 2025-09-17 07:42:07 -07:00
Preston Thorpe
bcafb288ad Merge 'Fix is_nonnull returns true on 1 / 0' from Lâm Hoàng Phúc
turso:
```sh
turso> CREATE TABLE t (x PRIMARY KEY, y, z);
turso> INSERT INTO t VALUES (37, -70, -196792117);
turso> SELECT * FROM t WHERE  (1 / 0) >= -3289742039 < t.x;
┌────┬─────┬────────────┐
│ x  │ y   │ z          │
├────┼─────┼────────────┤
│ 37 │ -70 │ -196792117 │
└────┴─────┴────────────┘
turso>
```
sqlite:
```sh
sqlite> CREATE TABLE t (x PRIMARY KEY, y, z);
sqlite> INSERT INTO t VALUES (37, -70, -196792117);
sqlite> SELECT * FROM t WHERE  (1 / 0) >= -3289742039 < t.x;
sqlite>
```
related: https://github.com/tursodatabase/turso/actions/runs/17765571409
/job/50488042583?pr=3147#step:8:855

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>
Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3167
2025-09-17 06:55:55 -04:00