Repository navigation
fix: composite keys, computed/atomic UPDATE, batch UPDATE-as-DELETE, CHECK and JOIN/subquery ORDER BY - #518
Open
CastleSoft wants to merge 1 commit into
Open
CastleSoft wants to merge 1 commit into
CastleSoft wants to merge 1 commit into
Conversation
…TE, CHECK and JOIN/subquery ORDER BY - Composite PRIMARY KEY and multi-column UNIQUE (directory storage): table-level PRIMARY KEY (a, b) was read as a phantom column PRIMARY/CONSTRAINT that became an always-NULL key; PRIMARY KEY on several columns (EF Core EnsureCreated) kept only the last one; UNIQUE (a, b) was never checked. A composite key is now a UNIQUE constraint over its columns with the row id as storage key, enforced on INSERT, multi-row INSERT and every UPDATE path. - ExecuteBatchSQL ran an UPDATE its fast path declined (e.g. a quoted SET column) as a DELETE of the same WHERE. - UPDATE SET values that read a column or call a function are computed per row from the pre-statement row (directory, single-file, batch); every matched row is validated before the first write (statement atomicity). - UPDATE without WHERE is accepted; a SET literal containing "where" is no longer cut. - CHECK: table-level constraints no longer reject every row; NULL passes; <> and functions work. - ORDER BY on the JOIN and AST/subquery paths reads every key and expression keys; JOIN honours LIMIT/OFFSET.
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
|
This was referenced Nov 16, 2026
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Works through the four open items from #516/#517. The probes found four more defects on the way, including a data-loss bug in
ExecuteBatchSQL. All of them are fixed here. Each fix has a regression test that was run red on the unfixed source and green after.Items asked for
UNIQUE (a, b)PRIMARY KEY (a, b)was read as a column calledPRIMARY(withCONSTRAINT pk …, a column calledCONSTRAINT). That column became the key and was always NULL, so the table could hold one row. InlinePRIMARY KEYon several columns (EF CoreEnsureCreated) kept only the last column.PRIMARY KEY (id)on one column makes that column the key. Named table constraints (CONSTRAINT x …) are parsed, including FOREIGN KEY.CHECK (n > id)andSET n = 2.LENGTH(p.name) DESCsorted by nothing, andc.score * -1sorted ascending by score. It also ignored OFFSET, and LIMIT 0 returned every row. The AST path read each key as a bare name (UPPER(name)→ "Unexpected trailing content '(name)'"). Its subquery executor used the first key only.SqlOrderByand handles all three LIMIT forms. The AST parser keeps the expression text of each item, and both AST executors sort on every key.Found on the way
ExecuteBatchSQLran some UPDATEs as DELETEs. When the UPDATE fast path declined a statement, the DELETE classifier tried next and accepted the canonical UPDATE shape. Example: a quoted SET column,SET "n" = 5, as linq2db and EF Core write it. The batch then deleted the rows it was asked to update, on columnar and page-based tables, and they stayed deleted after reopen.UPDATE … SET n = n + 5failed ("Invalid value 'n + 5'").SET label = nameon a TEXT column stored the textname.SET a = b, b = aswaps. This works on directory and single-file tables and throughExecuteBatchSQL. Constant arithmetic keeps its old literal parse and is evaluated only when that parse fails (SET n = 2 * 3).UPDATE t SET …without WHERE threw "Invalid UPDATE syntax". A SET literal containingwherewas cut at that word.CHECK (…)rejected every row: it was stored with its keyword and could not be evaluated. A NULL value failed a CHECK, and<>was not an operator.<>and functions work. Syntax the new evaluator doesn't cover falls back to the previous evaluator.Deliberately unchanged
SET name = 2 * 3still stores the text2 * 3. Constant expressions keep their old literal reading so existing unquoted values (dates, for example) don't change meaning. SQLite would store6.Tests
New test classes:
CompositeKeyTests,UpdateSetExpressionTests,BatchUpdateClassificationTests,TableCheckConstraintTests,JoinAndSubqueryOrderByTests. They hold 57 test cases; 51 fail on the unfixed source. The six that already passed there are expected:ReleasesTheKeycases check against over-enforcement, and the old code enforced nothing.CheckFails…/NotNullFails…cases couldn't detect a partial update on the old code: it rejected the expression before writing any row. Atomicity itself is proven byUpdate_LiteralFailingAColumnCheckOnALaterRow_…, which fails on the old code with row 1 left at2.AGENTS.mdlist)SharpCoreDB.CI.slnfbuilds with 0 errors.Performance
Write-path gate (
--gate, Release): GATE PASSED, with every metricok.The limit is 1.50×; ratio = baseline ÷ current, so above 1 is slower. On the hot UPDATE path the new SET-value check is an allocation-free literal test. Validating rows before writing replaces the per-row validation that was already there.