Commit Graph

1010 Commits

Author SHA1 Message Date
PThorpe92
a232e3cc7a Implement proper handling of deferred foreign keys 2025-10-07 16:45:23 -04:00
PThorpe92
f56f37fae5 Add more tests for self-referencing FKs and remove unneeded FkIfZero checks/labels in emitter 2025-10-07 16:45:23 -04:00
PThorpe92
99ae96c5f6 Fix self-referential FK relationships and validation of FKs 2025-10-07 16:45:22 -04:00
PThorpe92
ae975afe49 Remove unnecessary FK resolution on schema parsing 2025-10-07 16:45:16 -04:00
PThorpe92
16d19fd39e Add tcl tests for foreign keys 2025-10-07 16:28:04 -04:00
Pekka Enberg
dacb8e3350 Merge 'Fix attach I/O error with in-memory databases' from Preston Thorpe
closes #3540

Closes #3602
2025-10-07 09:00:02 +03:00
PThorpe92
addb9ef65b Add regression test for #3540 attach issue 2025-10-06 21:33:42 -04:00
Glauber Costa
beb44e8e8c fix mviews with re-insertion of data with the same key
There is currently a bug found in our materialized view implementation
that happens when we delete a row, and then re-insert another row with
the same primary key.

Our insert code needs to detect updates and generate a DELETE +
INSERT. But in this case, after the initial DELETE, the fresh insert
generates another delete.

We ended up with the wrong response for aggregations (and I am pretty
sure even filter-only views would manifest the bug as well), where
groups that should still be present just disappeared because of the
extra delete.

A new test case is added that fails without the fix.
2025-10-06 20:12:49 -05:00
Pekka Enberg
b063d0d41a Merge 'Don't panic if doing INSERT INTO ... SELECT rowid' from Jussi Saurio
Backport: 0.2
Closes #3567

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

Closes #3572
2025-10-04 10:11:09 +03:00
Pekka Enberg
50607607fa Merge 'Actually enforce uniqueness in create unique index' from Jussi Saurio
we just weren't doing it 🤡
Backport: 0.2
Closes #3568

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

Closes #3571
2025-10-04 10:07:44 +03:00
Jussi Saurio
81b437c690 Don't panic if doing INSERT INTO ... SELECT rowid
Closes #3567
2025-10-03 23:12:24 +03:00
Jussi Saurio
8dac1ba21a Fix: actually enforce uniqueness in CREATE UNIQUE INDEX
...we just didn't do it
2025-10-03 22:58:42 +03:00
Pekka Enberg
1b42f77300 Merge 'Add short writes to unreliable-libc' from FamHaggs
Add short writes in the faulty_libc
As @PThorpe92 stated in #3209, this should be implemented here instead
of the memory io in the simulator. Running this in the stress test I
caught a logic bug in the try_pwritev_raw I will create a pr for that
small fix. I will close #3209 in favor of this pr.

Closes #3569
2025-10-03 21:52:47 +03:00
FHaggs
dd6e092a5c Add short writes to pwritev in faulty_libc. 2025-10-03 18:35:03 +02:00
Jussi Saurio
d2f5e67b25 Merge 'Fix COLLATE' from Jussi Saurio
Fixes the following problems with COLLATE:
- Fix: incorrectly used e.g. `x COLLATE NOCASE = 'fOo'` as index
constraint on an index whose column was not case-insensitively collated
- Fix: various ephemeral indexes (in GROUP BY, ORDER BY, DISTINCT) and
subqueries did not retain proper collation information of columns
- Fix: collation of a given expression was not determined properly
according to SQLite's rules
Adds TCL tests and fuzz test
Closes #3476
Closes #1524
Closes #3305

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

Closes #3538
2025-10-03 09:34:24 +03:00
Jussi Saurio
ce7fe54841 Collate: add more TCL tests 2025-10-02 21:49:33 +03:00
PThorpe92
361bd70a26 Add regression test for rowid affinity 2025-10-02 14:31:22 -04:00
Pekka Enberg
dc1463c70d Merge 'Improve error handling for cyclic views' from Duy Dang
The cycle is detected by marking a seen view, if a seen view is process
again, that's a cycle and we throw an error.
Close #3404

Closes #3467
2025-10-02 19:33:12 +03:00
FHaggs
095e72eac9 Add short write to pwrite in faulty_libc. 2025-10-02 16:11:15 +02:00
Jussi Saurio
30e6524c4e Fix: JOIN USING should pick columns from left table, not right
Closes #3468
Closes #3479
2025-10-02 06:56:52 +03:00
Jussi Saurio
c0da38e24a Merge 'Clear WhereTerm 'from_outer_join' state when LEFT JOIN is optimized to INNER JOIN' from Jussi Saurio
Closes #3470
## Background
In a query like `SELECT * FROM t LEFT JOIN s ON t.a=s.a WHERE s.a =
'foo'` we can remove the LEFT JOIN and replace it with an `INNER JOIN`
because NULL values will never be equal to 'foo'. Rewriting as `INNER
JOIN` allows the optimizer to also reorder the table join order to come
up with a more efficient query plan. In fact, we have this optimization
already.
## Problem
However, there is a dumb bug where `WhereTerm`s involving this join
still retain their `from_outer_join` state, resulting in forcing the
evaluation of those terms at the original join index, which results in
completely wrong bytecode if the join optimizer decides to reorder the
join as `s JOIN t` instead. Effectively it will evaluate `t.a=s.a` after
table `s` is open but table `t` is not open yet.
## Fix
This PR fixes that issue by clearing `from_outer_join` properly from the
relevant `WhereTerm`s.

Closes #3475
2025-10-02 06:56:07 +03:00
Jussi Saurio
78cccdd87a Merge 'Substr fix UTF-8' from Pedro Muniz
Fixes:
- `start_value` and `length_value` should be casted to integers
- proper handling of utf-8 characters
- do not need to cast blob to string, as substr in blobs refers to byte
indexes and not char-indexes

Closes #3465
2025-10-02 06:55:38 +03:00
Preston Thorpe
b310411997 Merge 'printf should truncates floats' from Pavan Nambi
closes https://github.com/tursodatabase/turso/issues/3308

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

Closes #3415
2025-10-01 19:31:39 -04:00
Jussi Saurio
3ff6b44de2 Merge 'Fix index bookkeeping in DROP COLUMN' from Jussi Saurio
Closes #3448. Nasty bug - see issue for details

Closes #3449
2025-10-01 08:57:08 +03:00
Jussi Saurio
27b1c1a1db Merge 'Fix self-insert with nested subquery' from Mikaël Francoeur
There were 2 problems:
1. The SELECT wasn't propagating which register it used for its results,
so sometimes the INSERT read bad data.
2. `TableReferences::contains_table` was only checking the top-level
tables, not the nested tables in FROM queries. This condition is used to
emit "template 4", the bytecode template for self-inserts.
Closes https://github.com/tursodatabase/turso/issues/3312

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

Closes #3436
2025-10-01 08:56:16 +03:00
Jussi Saurio
63f9913dbb Clear WhereTerm 'from_outer_join' state when LEFT JOIN is optimized to INNER JOIN
Closes #2470

In a query like `SELECT * FROM t LEFT JOIN s ON t.a=s.a WHERE s.a = 'foo'` we can
remove the LEFT JOIN because NULL values will be equal to 'foo'. In fact, we have
this optimization already.

However, there was a dumb bug where `WhereTerm`s involving this join still retained
their `from_outer_join` state, resulting in forcing the evaluation of those terms
at the original join index, which results in completely wrong bytecode if the join
optimizer decides to reorder the join as `s JOIN t` instead. Effectively it will
evaluate `t.a=s.a` after table `s` is open but table `t` is not open yet.

This PR fixes that issue by clearing `from_outer_join` properly from the relevant
`WhereTerm`s.
2025-10-01 00:33:22 +03:00
Duy Dang
5ceab1b3f4 Circle detection for views 2025-10-01 02:12:21 +07:00
Nikita Sivukhin
9ef05adc5e fix upsert conflict handling 2025-09-30 22:39:55 +04:00
pedrocarlo
d5e365def4 add test 2025-09-30 15:26:13 -03:00
Nikita Sivukhin
3590f9882d support multiple conflict clauses in upsert 2025-09-30 20:47:39 +04:00
Nikita Sivukhin
e5aa836ad5 add simple test 2025-09-30 17:57:25 +04:00
Jussi Saurio
594bdce999 Merge 'sum should identify if there is num in strings/prefix of strings' from Pavan Nambi
closes #3285
maybe adding seperate func for it is stupid but i kept running into
issues with not closing some random `}` and it got annoying real quick
so i just moved tht into its own func. - and as i am using same logic in
3 places i think it's ok.

Closes #3412
2025-09-30 10:37:17 +03:00
Jussi Saurio
e6a2e2a9cf Merge 'Remove double-quoted identifier assert' from Diego Reis
Closes #3301
Not every identifier should be double-quoted

Closes #3440
2025-09-30 10:34:49 +03:00
Jussi Saurio
568b0eeff3 Merge 'substr scalar should also work with non-text values' from Diego Reis
Closes #3306
Not only blobs, but numbers are also cast to text

Closes #3439
2025-09-30 10:33:40 +03:00
Jussi Saurio
67be0478e4 Add TCL regression test for DROP COLUMN issue #3448 2025-09-30 10:04:31 +03:00
Pekka Enberg
2b59863447 Merge 'core/printf: Compatibility tests and fixes for printf()' from Luiz Gustavo
Addition of compatibilty tests for `printf()`.
While doing this I found some differences in the current implementation,
so this fixes those too.

Closes #3438
2025-09-30 07:44:35 +03:00
luizgfc
f1811794b9 core/testing: Basic sqlite compatibility tests for printf() 2025-09-30 01:02:12 -03:00
Diego Reis
8e60d64518 rename limbo -> turso 2025-09-29 22:33:39 -03:00
Diego Reis
90f4d69774 fix(3301): Remove identifier assert assumption
Not every identifier should be double-quoted
2025-09-29 22:33:21 -03:00
Diego Reis
c9421e034d fix(3306): substr scalar should also work with non-text values 2025-09-29 21:42:21 -03:00
Mikaël Francoeur
dc231abb2e fix self-insert bug 2025-09-29 17:18:19 -04:00
Glauber Costa
2fde976605 Fix materialized views with complex expressions
SQLite supports complex expressions in group by columns - because of
course it does...

So we need to make sure that a column is created for this expression if
it doesn't exist already, and compute it, the same way we compute
pre-projections in the filter operator.

Fixes #3363
Fixes #3366
Fixes #3365
2025-09-29 11:56:21 -05:00
Preston Thorpe
cdab174350 Merge 'Fix column fetch in joins' from Glauber Costa
In comparisons for joins, we were assuming that the left column belonged
to the left join (and vice-versa). That is incorrect, because you can
write the comparison condition in any order.
Fixes #3368

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

Closes #3400
2025-09-29 12:34:45 -04:00
Preston Thorpe
bbbcbf894e Merge 'translate/upsert: fix explicit conflict target of non-rowid primary key in UPSERT' from Preston Thorpe
closes #3384

Closes #3386
2025-09-29 11:09:29 -04:00
Nikita Sivukhin
a142c59de4 use explicit null if it set instead of column default value 2025-09-29 16:28:09 +04:00
Pavan-Nambi
2335578c94 printf truncates floats 2025-09-28 21:16:33 +05:30
Pavan-Nambi
074a363c30 sum should identify if there is num in strings/prefix of strings 2025-09-28 17:23:55 +05:30
PThorpe92
ace2ac632a Remove semicolon from test 2025-09-27 13:59:51 -04:00
PThorpe92
6e3c30623c Fix regression test to not use dot command 2025-09-27 13:17:50 -04:00
PThorpe92
bd17c5d5df Add regression test for alter table with notnull constraint 2025-09-27 13:15:48 -04:00