mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
Merge 'Insert or fix' from Pedro Muniz
Fixes https://github.com/tursodatabase/turso/issues/5286 ### Root cause In `emit_notnulls()` (`core/translate/insert.rs`), the `pending_resume_label` mechanism chained labels across columns: the `resume_label` from column N was reused as the `default_label` for column N+1. This meant that when column N's value was NOT null, the "skip past default" jump landed directly on column N+1's default evaluation code, unconditionally overwriting the user's value. ``` addr 18: Goto L2 -- val is not null, resume at L2 ... addr 24: [L2] String8 'unnamed', r[5] -- but L2 is name's default! ``` ### Fix Each column now gets its own independent `default_label` and `resume_label`, resolved immediately after its own default block. No cross-column label sharing. The resulting bytecode matches SQLite's pattern where each NOT NULL DEFAULT column is a self-contained `IsNotNull`/default block. Also removed the previous `IsNull` - default and goto resume block in favor of `IsNotNull` - default block like SQLITE does it. SQLITE: ```sql explain INSERT OR REPLACE INTO t_defaults VALUES (1, 100, 'hello'); addr opcode p1 p2 p3 p4 p5 comment ---- ------------- ---- ---- ---- ------------- -- ------------- 0 Init 0 21 0 0 Start at 21 1 OpenWrite 0 2 0 3 0 root=2 iDb=0; t_defaults 2 SoftNull 2 0 0 0 r[2]=NULL 3 Integer 100 3 0 0 r[3]=100 4 String8 0 4 0 hello 0 r[4]='hello' 5 Integer 1 1 0 0 r[1]=1 6 NotNull 1 8 0 0 if r[1]!=NULL goto 8 7 NewRowid 0 1 0 0 r[1]=rowid 8 MustBeInt 1 0 0 0 9 NotNull 3 11 0 0 if r[3]!=NULL goto 11 10 Integer 42 3 0 0 r[3]=42 11 NotNull 4 13 0 0 if r[4]!=NULL goto 13 12 String8 0 4 0 unnamed 0 r[4]='unnamed' 13 HaltIfNull 1299 2 3 t_defaults.val 1 if r[3]=null halt 14 HaltIfNull 1299 2 4 t_defaults.name 1 if r[4]=null halt 15 Noop 0 0 0 0 uniqueness check for ROWID 16 NotExists 0 18 1 0 intkey=r[1] 17 Delete 0 64 0 t_defaults 0 18 MakeRecord 2 3 5 DDB 0 r[5]=mkrec(r[2..4]) 19 Insert 0 5 1 t_defaults 49 intkey=r[1] data=r[5] 20 Halt 0 0 0 0 21 Transaction 0 1 1 0 1 usesStmtJournal=0 22 Goto 0 1 0 0 ``` TURSO: ```sql explain INSERT OR REPLACE INTO t_defaults VALUES (1, 100, 'hello'); 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 Integer 1 2 0 0 r[2]=1 3 SoftNull 3 0 0 0 4 Integer 100 4 0 0 r[4]=100 5 String8 0 5 0 hello 0 r[5]='hello' 6 NotNull 2 8 0 0 r[2]!=NULL -> goto 8 7 Goto 0 10 0 0 8 MustBeInt 2 0 0 0 9 Goto 0 11 0 0 10 NewRowid 0 2 0 0 r[2]=rowid 11 Affinity 3 3 0 0 r[3..6] = D, D, B 12 NotExists 0 17 2 0 13 Copy 2 1 0 0 r[1]=r[2] 14 SeekRowid 0 1 27 0 if (r[1]!=cursor 0 for table t_defaults.rowid) goto 27 15 Delete 0 0 0 t_defaults 0 16 Goto 0 17 0 0 17 NotNull 4 19 0 0 r[4]!=NULL -> goto 19 18 Integer 42 4 0 0 r[4]=42 19 HaltIfNull 1299 0 4 t_defaults.val 0 20 NotNull 5 22 0 0 r[5]!=NULL -> goto 22 21 String8 0 5 0 unnamed 0 r[5]='unnamed' 22 HaltIfNull 1299 0 5 t_defaults.name 0 23 MakeRecord 3 3 6 0 r[6]=mkrec(r[3..5]) 24 Insert 0 6 2 t_defaults 2 intkey=r[2] data=r[6] 25 Goto 0 26 0 0 26 Goto 0 27 0 0 27 Halt 0 0 0 0 28 Transaction 0 2 1 0 iDb=0 tx_mode=Write 29 Goto 0 1 0 0 ``` ### Tests Added 5 sqltests in `on_conflict.sqltest` covering: - Basic multi-column NOT NULL DEFAULT preservation - 3+ NOT NULL DEFAULT columns - CHECK constraint not bypassed by default substitution - UNIQUE index-table data consistency - `INSERT OR REPLACE ... SELECT` variant Added 2 snapshot tests in `modifications.sqltest` to capture the corrected bytecode. Generate with Claude Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com> Closes #5350
This commit is contained in:
@@ -572,7 +572,7 @@ pub fn translate_insert(
|
||||
&mut preflight_ctx,
|
||||
)?;
|
||||
|
||||
let notnull_resume_label = emit_notnulls(&mut program, &ctx, &insertion, resolver)?;
|
||||
emit_notnulls(&mut program, &ctx, &insertion, resolver)?;
|
||||
|
||||
// Create and insert the record
|
||||
let affinity_str = insertion
|
||||
@@ -581,9 +581,6 @@ pub fn translate_insert(
|
||||
.map(|col_mapping| col_mapping.column.affinity().aff_mask())
|
||||
.collect::<String>();
|
||||
|
||||
if let Some(lbl) = notnull_resume_label {
|
||||
program.preassign_label_to_next_insn(lbl);
|
||||
}
|
||||
program.emit_insn(Insn::MakeRecord {
|
||||
start_reg: to_u16(insertion.first_col_register()),
|
||||
count: to_u16(insertion.col_mappings.len()),
|
||||
@@ -1168,10 +1165,9 @@ fn emit_notnulls(
|
||||
ctx: &InsertEmitCtx,
|
||||
insertion: &Insertion,
|
||||
resolver: &Resolver,
|
||||
) -> Result<Option<BranchOffset>> {
|
||||
) -> Result<()> {
|
||||
let on_replace = matches!(ctx.on_conflict, ResolveType::Replace);
|
||||
let on_ignore = matches!(ctx.on_conflict, ResolveType::Ignore);
|
||||
let mut pending_resume_label = None;
|
||||
for column_mapping in insertion
|
||||
.col_mappings
|
||||
.iter()
|
||||
@@ -1186,26 +1182,13 @@ fn emit_notnulls(
|
||||
// or if the column has no default value, then the ABORT algorithm is used
|
||||
if on_replace {
|
||||
if let Some(default_expr) = column_mapping.column.default.as_ref() {
|
||||
let default_label = {
|
||||
if let Some(lbl) = pending_resume_label {
|
||||
lbl
|
||||
} else {
|
||||
program.allocate_label()
|
||||
}
|
||||
};
|
||||
let resume_label = program.allocate_label();
|
||||
let skip_label = program.allocate_label();
|
||||
|
||||
program.emit_insn(Insn::IsNull {
|
||||
program.emit_insn(Insn::NotNull {
|
||||
reg: column_mapping.register,
|
||||
target_pc: default_label,
|
||||
target_pc: skip_label,
|
||||
});
|
||||
|
||||
program.emit_insn(Insn::Goto {
|
||||
target_pc: resume_label,
|
||||
});
|
||||
|
||||
program.resolve_label(default_label, program.offset());
|
||||
|
||||
// Evaluate default expression into the column register.
|
||||
translate_expr_no_constant_opt(
|
||||
program,
|
||||
@@ -1216,10 +1199,7 @@ fn emit_notnulls(
|
||||
NoConstantOptReason::RegisterReuse,
|
||||
)?;
|
||||
|
||||
program.emit_insn(Insn::Goto {
|
||||
target_pc: resume_label,
|
||||
});
|
||||
pending_resume_label = Some(resume_label);
|
||||
program.preassign_label_to_next_insn(skip_label);
|
||||
}
|
||||
// OR REPLACE but no DEFAULT, fall through to ABORT behavior
|
||||
}
|
||||
@@ -1259,7 +1239,7 @@ fn emit_notnulls(
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(pending_resume_label)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct BoundInsertResult {
|
||||
|
||||
@@ -169,6 +169,81 @@ expect {
|
||||
2|two
|
||||
}
|
||||
|
||||
# INSERT OR REPLACE should preserve user values for multiple NOT NULL DEFAULT columns
|
||||
|
||||
test insert-or-replace-multi-notnull-default {
|
||||
CREATE TABLE t1(
|
||||
id INTEGER PRIMARY KEY,
|
||||
val INTEGER NOT NULL DEFAULT 42,
|
||||
name TEXT NOT NULL DEFAULT 'unnamed'
|
||||
);
|
||||
INSERT OR REPLACE INTO t1 VALUES (1, 100, 'hello');
|
||||
SELECT * FROM t1;
|
||||
}
|
||||
expect {
|
||||
1|100|hello
|
||||
}
|
||||
|
||||
test insert-or-replace-multi-notnull-default-many-cols {
|
||||
CREATE TABLE t1(id INTEGER PRIMARY KEY,
|
||||
a TEXT NOT NULL DEFAULT 'da',
|
||||
b TEXT NOT NULL DEFAULT 'db',
|
||||
c TEXT NOT NULL DEFAULT 'dc');
|
||||
INSERT OR REPLACE INTO t1 VALUES (1, 'one', 'two', 'three');
|
||||
SELECT * FROM t1;
|
||||
}
|
||||
expect {
|
||||
1|one|two|three
|
||||
}
|
||||
|
||||
# INSERT OR REPLACE must not bypass CHECK constraints via default substitution
|
||||
|
||||
test insert-or-replace-notnull-default-respects-check {
|
||||
CREATE TABLE t1(
|
||||
id INTEGER PRIMARY KEY,
|
||||
a INTEGER NOT NULL DEFAULT 999 CHECK(a < 100),
|
||||
b INTEGER NOT NULL DEFAULT 999 CHECK(b < 100)
|
||||
);
|
||||
INSERT OR REPLACE INTO t1 VALUES (1, 10, 20);
|
||||
SELECT * FROM t1;
|
||||
}
|
||||
expect {
|
||||
1|10|20
|
||||
}
|
||||
|
||||
# INSERT OR REPLACE must keep index and table data consistent for NOT NULL DEFAULT UNIQUE columns
|
||||
|
||||
test insert-or-replace-notnull-default-unique-index-consistent {
|
||||
CREATE TABLE t1(
|
||||
id INTEGER PRIMARY KEY,
|
||||
a INTEGER NOT NULL DEFAULT 42,
|
||||
b INTEGER NOT NULL DEFAULT 42 UNIQUE
|
||||
);
|
||||
INSERT INTO t1 VALUES (1, 10, 42);
|
||||
INSERT OR REPLACE INTO t1 VALUES (2, 20, 99);
|
||||
SELECT * FROM t1 WHERE b = 99;
|
||||
}
|
||||
expect {
|
||||
2|20|99
|
||||
}
|
||||
|
||||
# INSERT OR REPLACE ... SELECT should also preserve user values for NOT NULL DEFAULT columns
|
||||
|
||||
test insert-or-replace-select-multi-notnull-default {
|
||||
CREATE TABLE src(id INTEGER PRIMARY KEY, val INTEGER, name TEXT);
|
||||
INSERT INTO src VALUES (1, 100, 'hello');
|
||||
CREATE TABLE dst(
|
||||
id INTEGER PRIMARY KEY,
|
||||
val INTEGER NOT NULL DEFAULT 42,
|
||||
name TEXT NOT NULL DEFAULT 'unnamed'
|
||||
);
|
||||
INSERT OR REPLACE INTO dst SELECT * FROM src;
|
||||
SELECT * FROM dst;
|
||||
}
|
||||
expect {
|
||||
1|100|hello
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# INSERT OR ROLLBACK tests
|
||||
# =============================================================================
|
||||
|
||||
@@ -179,3 +179,25 @@ snapshot insert-or-replace-multi-unique {
|
||||
INSERT OR REPLACE INTO items (id, sku, barcode, name)
|
||||
VALUES (1, 'SKU-001', 'BC-001', 'Widget');
|
||||
}
|
||||
|
||||
# INSERT OR REPLACE with multiple NOT NULL DEFAULT columns
|
||||
# Verify each column gets its own independent IsNull/default block
|
||||
# so that non-null user values are never overwritten by defaults.
|
||||
|
||||
setup notnull_defaults {
|
||||
CREATE TABLE t_defaults (
|
||||
id INTEGER PRIMARY KEY,
|
||||
val INTEGER NOT NULL DEFAULT 42,
|
||||
name TEXT NOT NULL DEFAULT 'unnamed'
|
||||
);
|
||||
}
|
||||
|
||||
@setup notnull_defaults
|
||||
snapshot insert-or-replace-multi-notnull-default {
|
||||
INSERT OR REPLACE INTO t_defaults VALUES (1, 100, 'hello');
|
||||
}
|
||||
|
||||
@setup notnull_defaults
|
||||
snapshot insert-or-replace-multi-notnull-default-partial {
|
||||
INSERT OR REPLACE INTO t_defaults(id, val) VALUES (1, 100);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
source: modifications.sqltest
|
||||
expression: INSERT OR REPLACE INTO t_defaults(id, val) VALUES (1, 100);
|
||||
info:
|
||||
statement_type: INSERT
|
||||
setup_blocks:
|
||||
- notnull_defaults
|
||||
database: ':memory:'
|
||||
---
|
||||
QUERY PLAN
|
||||
|
||||
|
||||
BYTECODE
|
||||
addr opcode p1 p2 p3 p4 p5 comment
|
||||
0 Init 0 27 0 0 Start at 27
|
||||
1 OpenWrite 0 2 0 0 root=2; iDb=0
|
||||
2 Integer 1 2 0 0 r[2]=1
|
||||
3 SoftNull 3 0 0 0
|
||||
4 Integer 100 4 0 0 r[4]=100
|
||||
5 NotNull 2 7 0 0 r[2]!=NULL -> goto 7
|
||||
6 Goto 0 9 0 0
|
||||
7 MustBeInt 2 0 0 0
|
||||
8 Goto 0 10 0 0
|
||||
9 NewRowid 0 2 0 0 r[2]=rowid
|
||||
10 Affinity 3 3 0 0 r[3..6] = D, D, B
|
||||
11 NotExists 0 16 2 0
|
||||
12 Copy 2 1 0 0 r[1]=r[2]
|
||||
13 SeekRowid 0 1 26 0 if (r[1]!=cursor 0 for table t_defaults.rowid) goto 26
|
||||
14 Delete 0 0 0 t_defaults 0
|
||||
15 Goto 0 16 0 0
|
||||
16 NotNull 4 18 0 0 r[4]!=NULL -> goto 18
|
||||
17 Integer 42 4 0 0 r[4]=42
|
||||
18 HaltIfNull 1299 0 4 t_defaults.val 0
|
||||
19 NotNull 5 21 0 0 r[5]!=NULL -> goto 21
|
||||
20 String8 0 5 0 unnamed 0 r[5]='unnamed'
|
||||
21 HaltIfNull 1299 0 5 t_defaults.name 0
|
||||
22 MakeRecord 3 3 6 0 r[6]=mkrec(r[3..5])
|
||||
23 Insert 0 6 2 t_defaults 2 intkey=r[2] data=r[6]
|
||||
24 Goto 0 25 0 0
|
||||
25 Goto 0 26 0 0
|
||||
26 Halt 0 0 0 0
|
||||
27 Transaction 0 2 1 0 iDb=0 tx_mode=Write
|
||||
28 String8 0 5 0 unnamed 0 r[5]='unnamed'
|
||||
29 Goto 0 1 0 0
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
source: modifications.sqltest
|
||||
expression: INSERT OR REPLACE INTO t_defaults VALUES (1, 100, 'hello');
|
||||
info:
|
||||
statement_type: INSERT
|
||||
setup_blocks:
|
||||
- notnull_defaults
|
||||
database: ':memory:'
|
||||
---
|
||||
QUERY PLAN
|
||||
|
||||
|
||||
BYTECODE
|
||||
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 Integer 1 2 0 0 r[2]=1
|
||||
3 SoftNull 3 0 0 0
|
||||
4 Integer 100 4 0 0 r[4]=100
|
||||
5 String8 0 5 0 hello 0 r[5]='hello'
|
||||
6 NotNull 2 8 0 0 r[2]!=NULL -> goto 8
|
||||
7 Goto 0 10 0 0
|
||||
8 MustBeInt 2 0 0 0
|
||||
9 Goto 0 11 0 0
|
||||
10 NewRowid 0 2 0 0 r[2]=rowid
|
||||
11 Affinity 3 3 0 0 r[3..6] = D, D, B
|
||||
12 NotExists 0 17 2 0
|
||||
13 Copy 2 1 0 0 r[1]=r[2]
|
||||
14 SeekRowid 0 1 27 0 if (r[1]!=cursor 0 for table t_defaults.rowid) goto 27
|
||||
15 Delete 0 0 0 t_defaults 0
|
||||
16 Goto 0 17 0 0
|
||||
17 NotNull 4 19 0 0 r[4]!=NULL -> goto 19
|
||||
18 Integer 42 4 0 0 r[4]=42
|
||||
19 HaltIfNull 1299 0 4 t_defaults.val 0
|
||||
20 NotNull 5 22 0 0 r[5]!=NULL -> goto 22
|
||||
21 String8 0 5 0 unnamed 0 r[5]='unnamed'
|
||||
22 HaltIfNull 1299 0 5 t_defaults.name 0
|
||||
23 MakeRecord 3 3 6 0 r[6]=mkrec(r[3..5])
|
||||
24 Insert 0 6 2 t_defaults 2 intkey=r[2] data=r[6]
|
||||
25 Goto 0 26 0 0
|
||||
26 Goto 0 27 0 0
|
||||
27 Halt 0 0 0 0
|
||||
28 Transaction 0 2 1 0 iDb=0 tx_mode=Write
|
||||
29 Goto 0 1 0 0
|
||||
Reference in New Issue
Block a user