Commit Graph

934 Commits

Author SHA1 Message Date
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
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
PThorpe92
8dd1cf11eb Add some more specific tests for upsert 2025-09-17 06:44:10 -04:00
TcMits
6606bf12d3 is_nonnull returns true on 1 / 0 2025-09-17 14:50:15 +07:00
Jussi Saurio
cae234818b Merge 'Inital support for window functions' from Piotr Rżysko
This adds basic support for window functions. For now:
* Only existing aggregate functions can be used as window functions.
* Specialized window-specific functions (`rank`, `row_number`, etc.) are
not yet supported.
* Only the default frame definition is implemented:
`RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE NO OTHERS`.

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

Closes #3079
2025-09-17 08:29:16 +03:00
Pekka Enberg
74331898a3 Merge 'Add quoted identifier test cases for ALTER TABLE' from Levy A.
Resolves #2093
There is a small incompatibility on how we quote the added column on the
final schema, but doesn't change any behavior.

Closes #2943
2025-09-16 11:46:12 +03:00
Pavan-Nambi
0effb981e6 autoincrement functionality works as good as sqlite now, handled all edge cases that we are aware of
- The code now prevents dropping or indexing `sqlite_sequence`
- make sure that AUTOINCREMENT only works on a single `INTEGER PRIMARY KEY`
-  handles `i64::MAX` gracefully by returning `SQLITE_FULL`
- also AUTOINCREMENT now works in both column and table constraints.

fmt
2025-09-13 16:35:36 +05:30
Piotr Rzysko
f5efcbe745 Add support for window functions
Adds initial support for window functions. For now, only existing
aggregate functions can be used as window functions—no specialized
window-specific functions are supported yet.

Currently, only the default frame definition is implemented:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE NO OTHERS.
2025-09-13 11:12:44 +02:00
Piotr Rzysko
6d84cbedc2 Fix delimiter handling in group_concat and string_agg
Non-literal delimiters must be translated by AggArgumentSource.
2025-09-13 10:49:14 +02:00
Piotr Rzysko
110ffba2a1 Fix accumulator reset when arguments outnumber aggregates
Previously, while resetting accumulator registers, we would also
reset subsequent registers. This happened because the number of registers
to reset was computed as the sum of arguments rather than the number of
aggregate functions.
2025-09-13 10:49:14 +02:00
Pekka Enberg
c620a15a55 Merge 'core: Support ceiling modifier in datetime' from Ceferino Patino
Resolves #2677
- Implements the modifier for Floor on the datetime object.
- Implements the modifier for Ceiling on the datetime object.
- Includes additional testing changes in testing/scalar-functions-
datetime.test to test the sql functionality.
Consolidation PR of #2678 and #2679 since the functions ended up being
much more complicated than I initially thought and had to be done in a
single PR.

Closes #2757
2025-09-10 14:46:07 +03:00