Commit Graph

1785 Commits

Author SHA1 Message Date
Jussi Saurio
ad80285437 Rename is_scope to deferred and invert respective boolean logic
Much clearer name for what it is/does
2025-10-22 23:40:44 +03:00
Jussi Saurio
6557a41503 Refactor emit_fk_violation() to always issue a FkCounter instruction 2025-10-22 23:40:44 +03:00
Pekka Enberg
d2d995a9c0 Merge 'Make sure explicit column aliases have binding precedence in orderby' from Pavan Nambi
closes https://github.com/tursodatabase/turso/issues/3684

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

Closes #3709
2025-10-21 19:04:42 +03:00
PThorpe92
ddd674c340 Move duplicate table identifier checking to parse_join to allow for natural joins 2025-10-16 18:32:48 -04:00
PThorpe92
10c69b910e Prevent ambiguous self-join table reference 2025-10-16 16:39:10 -04:00
PThorpe92
edaa1b675e Prevent column definitions on CREATE TABLE or opening DB with ON CONFLICT on column def 2025-10-16 15:45:20 -04:00
PThorpe92
04c9eee4f1 Throw parse error on GENERATED constraint when creating new table 2025-10-16 14:27:22 -04:00
Preston Thorpe
b31908fe99 Merge 'translate/select: Fix rewriting Rowid expression when no btree table exists in joined table refs ' from Preston Thorpe
closes https://github.com/tursodatabase/turso/issues/3667

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

Closes #3754
2025-10-16 14:22:51 -04:00
PThorpe92
e417188cb2 Fix panic when selecting explicit rowid from FROM clause subquery 2025-10-16 13:40:01 -04:00
PThorpe92
bd33b3fa83 Throw parse error on CHECK constraint in create table 2025-10-16 13:07:12 -04:00
Jussi Saurio
e8e583ace6 Default ON CONFLICT behavior should be ROLLBACK 2025-10-16 14:28:18 +03:00
Jussi Saurio
95f375791b refactor: move condition outside init_autoincrement 2025-10-16 09:34:13 +03:00
Jussi Saurio
25339a5200 rename: CheckConstraints -> ConstraintsToCheck
CHECK constraints is a separate SQL concept, so let's remove some
potential confusion from the naming.
2025-10-16 09:30:41 +03:00
PThorpe92
41d2a0af77 Add INSERT OR IGNORE handling and refactor INSERT further 2025-10-15 22:51:10 -04:00
Jussi Saurio
d7a719418e Fix: outer CTEs should be available in subqueries 2025-10-15 15:15:55 +03:00
Jussi Saurio
4e6f373e3d Merge 'Fix: Evaluating expression in LIMIT and OFFSET clauses.' from
Closes #3687 .
Previously, the `try_fold_expr_to_i64` function casted `NULL` as `0`
when evaluating expressions in `LIMIT` or `OFFSET` clauses. I removed
this function since evaluating the expression directly and relying on
the MustBeInt operation for casting seems to handle everything.

Closes #3695
2025-10-15 10:36:36 +03:00
Jussi Saurio
bae33cb52c Avoid unwrapping failed f64 parsing attempts 2025-10-15 09:47:47 +03:00
Jussi Saurio
b1cb897216 Merge 'Fix another "should have been rewritten" translation panic' from Jussi Saurio
Closes #2158

Closes #3702
2025-10-15 09:25:01 +03:00
Preston Thorpe
74bbb0d5a3 Merge 'Allow using indexes to iterate rows in UPDATE statements' from Jussi Saurio
Closes #2600
## Problem
Every btree has a key it is sorted by - this is the integer `rowid` for
tables and an arbitrary-sized, potentially multi-column key for indexes.
Executing an UPDATE in a loop is not safe if the update modifies any
part of the key of the btree that is used for iterating the rows in said
loop. For example:
- Using the table itself to iterate rows is not safe if the UPDATE
modifies the rowid (or rowid alias) of a row, because since it modifies
the iteration order itself, it may cause rows to be skipped:
```sql
CREATE TABLE t(x INTEGER PRIMARY KEY, y);
INSERT <something>
UPDATE t SET y = RANDOM() where x > 100; // safe to iterate 't', 'y' is not being modified
UPDATE t SET x = RANDOM() where x > 100; // not safe to iterate 't', 'x' is being modified
```
- Using an index to iterate rows is not safe if the UPDATE modifies any
of the columns in the index key
```sql
CREATE TABLE t(x, y, z);
CREATE INDEX txy ON t (x,y);
INSERT <something>
UPDATE t SET z = RANDOM() where x = 100 and y > 0; // safe to iterate txy, neither x or y is being modified
UPDATE t SET x = RANDOM() where x = 100 and y > 0; // not safe to iterate txy, 'x' is being modified
UPDATE t SET y = RANDOM() where x = 100 and y > 0; // not safe to iterate txy, 'y' is being modified
```
## Current solution in tursodb
Our current `main` code recognizes this issue and adopts this pseudocode
algorithm from SQLite:
- open a table or index for reading the rows of the source table,
- for each row that matches the condition in the UPDATE statement, write
the row into a temporary table
- then use that temporary table for iteration in the UPDATE loop.
This guarantees that the iteration order will not be affected by the
UPDATEs because the ephemeral table is not under modification.
## Problem with current solution
Our `main` code specialcases the ephemeral table solution to rowids /
rowid aliases only. Using indexes for UPDATE iteration was disabled in
an earlier PR (#2599) due to the safety issue mentioned above, which
means that many UPDATE statements become full table scans:
```sql
turso> create table t(x PRIMARY KEY);
turso> insert into t select value from generate_series(1,10000);
turso> explain update t set x = x + 100000 where x > 50 and x < 60;
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     28    0                    0   Start at 28
1     OpenWrite          0     2     0                    0   root=2; iDb=0
2     OpenWrite          1     3     0                    0   root=3; iDb=0
-- scan entire 't' despite very narrow update range!
3     Rewind             0     27    0                    0   Rewind table t
...
```
## Solution
We move the ephemeral table logic to _after_ the optimizer has selected
the best access path for the table, and then, if the UPDATE modifies the
key of the chosen access path (table or index; whichever was selected by
the optimizer), we change the plan to include the ephemeral table
prepopulation. Hence, the same query from above becomes:
```sql
turso> explain update t set x = x + 100000 where x > 50 and x < 60;
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     35    0                    0   Start at 35
1     OpenEphemeral      0     1     0                    0   cursor=0 is_table=true
2     OpenRead           1     3     0                    0   index=sqlite_autoindex_t_1, root=3, iDb=0
3     Integer            50    2     0                    0   r[2]=50
-- index seek on PRIMARY KEY index
4     SeekGT             1     10    2                    0   key=[2..2]
5       Integer          60    2     0                    0   r[2]=60
6       IdxGE            1     10    2                    0   key=[2..2]
7       IdxRowId         1     1     0                    0   r[1]=cursor 1 for index sqlite_autoindex_t_1.rowid
8       Insert           0     3     1     ephemeral_scratch  2   intkey=r[1] data=r[3]
9     Next               1     6     0                    0   
10    OpenWrite          2     2     0                    0   root=2; iDb=0
11    OpenWrite          3     3     0                    0   root=3; iDb=0
-- only scan rows that were inserted to ephemeral index
12    Rewind             0     34    0                    0   Rewind table ephemeral_scratch
13      RowId            0     5     0                    0   r[5]=ephemeral_scratch.rowid
```
Note that an ephemeral index does not have to be used if the index is
not affected:
```sql
turso> create table t(x PRIMARY KEY, data);
turso> explain update t set data = 'some_data' where x > 50 and x < 60;
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     15    0                    0   Start at 15
1     OpenWrite          0     2     0                    0   root=2; iDb=0
2     OpenWrite          1     3     0                    0   root=3; iDb=0
3     Integer            50    1     0                    0   r[1]=50
-- direct index seek
4     SeekGT             1     14    1                    0   key=[1..1]
```

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

Closes #3728
2025-10-14 16:11:25 -04:00
PThorpe92
792877d421 add doc comments to InsertEmitCtx 2025-10-14 13:22:32 -04:00
PThorpe92
20bdb1133d fix clippy warnings 2025-10-14 13:00:31 -04:00
PThorpe92
22e98964cc Refactor INSERT translation to a modular setup with emitter context 2025-10-14 12:48:34 -04:00
Pavan-Nambi
796ff4b2ac resolve explicit aliases for cannonical col binding 2025-10-14 20:46:15 +05:30
Jussi Saurio
b3be21f472 Do not count ephemeral table INSERTs as changes 2025-10-14 16:15:20 +03:00
Jussi Saurio
87434b8a72 Do not count DELETEs occuring in an UPDATE stmt as separate changes 2025-10-14 16:11:43 +03:00
Jussi Saurio
0173d31c04 clippy: collapse nested if 2025-10-14 15:51:31 +03:00
Jussi Saurio
4b80678898 Allow case where cursor for btree is already opened
When populating an ephemeral table for UPDATE, it may open a cursor
on the (permanent) table - in this case we don't need to open it
again in the UPDATE loop
2025-10-14 15:32:48 +03:00
Jussi Saurio
f5ee4807da Properly differentiate between source and target in UPDATE
- Encode information about ephemeral source table in OperationMode::UPDATE
  if present
- Use OperationMode information to correctly resolve cursors in UPDATE
2025-10-14 14:17:28 +03:00
Jussi Saurio
691dce6b8a Make decision about UpdatePlan::ephemeral_plan _after_ optimizer
An ephemeral table is required if the b-tree key of the table (rowid)
or the index (index key) is affected by the UPDATE.
2025-10-14 14:17:28 +03:00
Jussi Saurio
c2fe13ad4f Update documentation of UpdatePlan::ephemeral_plan
It now better reflects when it is used.
2025-10-14 12:18:53 +03:00
Jussi Saurio
bc80ac1754 require &mut ProgramBuilder argument in optimize_plan()
this will be used for ephemeral plan construction for UPDATE in
a later commit.
2025-10-14 12:18:13 +03:00
Jussi Saurio
29770382f9 temporarily remove ephemeral plan construction from prepare_update_plan
the decision to use an ephemeral table in UPDATE will be made after
the optimizer has made the decision about which index to use. this will
be implemented in a later commit.
2025-10-14 12:14:15 +03:00
Jussi Saurio
4e34c6be51 Merge 'names shall not be shared between tables,indexs,vtabs,views' from Pavan Nambi
closes #3675

Closes #3681
2025-10-14 07:30:37 +03:00
Jussi Saurio
bd15fee1f8 Merge 'Get aliases to where shall they be used' from Pavan Nambi
closes #3678

Closes #3680
2025-10-14 07:28:09 +03:00
Pekka Enberg
1c9eaa3d95 Merge 'Restrict joins to max 63 tables and allow arbitrary number of table columns' from Jussi Saurio
Closes #2076
Closes #2075

Closes #3705
2025-10-13 18:23:11 +03:00
Pavan-Nambi
6b3fcfd3d4 explicit column aliase must have preference 2025-10-13 19:11:55 +05:30
Pavan-Nambi
57a06835bf add test and fmt and clippy
i was stupid

remove comment
2025-10-13 18:07:51 +05:30
Jussi Saurio
c54e150a52 Merge 'Fix: Table entry is not removed from sqlite_schema when a table is dropped' from
Fixes #3682 .
Ignore case of table name when dropping table.

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

Closes #3683
2025-10-13 15:02:29 +03:00
Jussi Saurio
3669437482 Add vibecoded tests for ColumnUsedMask 2025-10-13 14:03:34 +03:00
Jussi Saurio
e055ed9a8d Allow arbitrarily many columns in a table
Use roaring bitmaps because ColumnUsedMask is likely to be
sparsely populated.
2025-10-13 13:30:26 +03:00
Jussi Saurio
59a1c2ae2e Disallow joining more than 63 tables
Returns an error instead of panicing
2025-10-13 13:30:03 +03:00
Jussi Saurio
523b155df1 Fix another "should have been rewritten" translation panic
Closes #2158
2025-10-13 11:02:42 +03:00
rajajisai
f703cc7fa7 Remove function 2025-10-12 22:46:41 -04:00
rajajisai
cd763ce373 Fix evalauting expression for limit and offset. 2025-10-12 22:46:25 -04:00
Pavan-Nambi
e1f23aeb2c fmt and add tests 2025-10-12 22:23:04 +05:30
Pavan-Nambi
3491e1f42e add if alais and allow iff to have more arguments 2025-10-12 22:17:35 +05:30
Pavan-Nambi
36bf88119f add tests
clippy

expect err to make clippy happy

cleanup
2025-10-12 16:38:12 +05:30
rajajisai
6c082660ca convert table name to lower case 2025-10-11 21:39:41 -04:00
Pavan-Nambi
ae00542755 get em aliases shall they be used 2025-10-12 05:25:22 +05:30
ultraman
5153e2aa32 Fix disallow reserved prefixes in ALTER TABLE RENAME TO 2025-10-11 16:30:25 +08:00