Repository navigation
fix: atomic ExecuteBatchSQL, upsert / INSERT OR conflict clauses, subquery ORDER BY collation, quoted batch SET columns - #521
Open
CastleSoft wants to merge 2 commits into
Conversation
…query ORDER BY collation, quoted batch SET columns - ExecuteBatchSQL: SELECT-bearing and mixed batches ran statement by statement without a transaction; they now run as one transaction with the PageBased undo scope, and every grouped batch transaction uses the undo scope too, so a failing batch leaves every table unchanged. - INSERT OR IGNORE|REPLACE|ABORT|FAIL|ROLLBACK, REPLACE INTO and ON CONFLICT ... DO NOTHING | DO UPDATE SET ... [WHERE ...] run on the SQL INSERT path (VALUES and SELECT, single and composite keys); DO UPDATE assignments are expressions with excluded.col. Composite conflict lookups compare with the column collation. - AST ORDER BY (derived tables, IN-subqueries) honours COLLATE and NULLS FIRST/LAST and compares text as BINARY by default; COLLATE NOCASE DESC no longer loses its direction. - Batch UPDATEs with a quoted SET column take the structured fast path again. - Composite UNIQUE check registers the column's auto index before falling back to a scan.
…DER BY splitters (SonarCloud duplication) SqlLiteralScanner.FindTopLevel / IsKeywordAt replace three copies of the same quote- and parenthesis-aware loop (SplitUpdateSetAndWhere, the ON CONFLICT finder, FindOrderItemEnd), which SonarCloud's quality gate failed on as duplicated new code (3.6% > 3%). Also clears the flagged smells: the conflict-clause reader and resolver split into smaller methods (ReadInsertOrPolicy, ReadDoUpdateAssignments, DeleteConflictingRows), ResolveInsertConflict made static without its unused table-name parameter, and nested ternaries in SqlOrderBy / ParseOrderItemSuffix rewritten. No behaviour change.
|
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
This PR covers items 4–9 from the follow-up list. Every fix has a regression test that fails on #520's code and passes after.
4.
ExecuteBatchSQLis atomicA failing statement in a batch kept some or all of the statements before it:
Both paths now run as a single transaction with the PageBased undo scope from #520. If a caller already has a transaction open, that transaction decides the outcome.
BatchAtomicityTestscovers UPDATE, mixed and SELECT-bearing batches, a successful mixed batch, and a batch inside an outer transaction.5. Upsert and
INSERT OR …: they didn't run at allINSERT OR IGNORE,OR REPLACE,OR ABORT,OR FAIL,OR ROLLBACKandREPLACE INTOfailed with "Unsupported SQL statement".ON CONFLICT … DO NOTHING | DO UPDATE SET … [WHERE …]clause was silently ignored, so an upsert onto an existing key failed as a duplicate.SqlParser.DML.InsertConflictexisted, but nothing called them.All of these now run on the SQL INSERT path:
VALUESandSELECTinserts;ExecuteBatchSQLtoo.DO UPDATEassignments and theirWHEREare evaluated as expressions over the existing row, withexcluded.colholding the incoming value, son = n + excluded.nworks.UpsertTestscovers it.6. Subquery and derived-table ORDER BY
COLLATEandNULLS FIRST/LASTwere ignored.COLLATE NOCASE DESClost its DESC as well.ORDER BY nameover a derived table put'a'before'C'.SubqueryOrderByCollationTestscovers these.7. Upsert lookups ignored collation
The composite conflict lookup now compares each column with its declared collation, as INSERT and UPDATE do: NOCASE conflicts on
'ABC'/'abc', BINARY doesn't. Its "same row" primary-key check was always case-insensitive and now uses the key's own collation.8. Composite UNIQUE check without its index
There's no deadlock risk: the table lock allows recursion. The full-scan fallback now registers the column's auto index first and scans only as a last resort. This is hardening: the constraint's own index can't be dropped, as in SQLite.
9. Quoted batch SET columns take the fast path again
In
ExecuteBatchSQL,SET "n" = 5andSET [n] = 5were looked up with their quotes, missed, and fell off the structured fast path. The name is now unquoted first; an unquoted name allocates nothing.BatchUpdateClassificationTests.BatchUpdate_QuotedSetColumn_TakesTheStructuredFastPathcovers it.Tests
The new and extended tests are in
BatchAtomicityTests,UpsertTestsandSubqueryOrderByCollationTests, plus one new fact inBatchUpdateClassificationTests. Of the 33 cases run red on #520's code, 24 fail. The 9 that pass there are expected:INSERT OR ABORTalready threw, though for a different reason.COLLATE NOCASEderived-table case passed only because the default comparison was case-insensitive. That defect is now pinned byDerivedTable_OrderByTextWithoutCollate_SortsAsBinary.SharpCoreDB.CI.slnfbuilds with 0 errors, and the documentation consistency tests pass.Performance
Write-path gate: GATE PASSED. Nothing is slower than baseline × 1.50.
The READ ratios are higher than the last two runs: 1.30×/1.27× on #520 and 1.33× on #519. This PR doesn't change the SELECT path; it changes the batch dispatchers, the INSERT path and AST ORDER BY. The machine wasn't quieted: a permission rule blocks the quiet script's cleanup. I recommend a gate rerun on a quieted machine before merging the stack, to tell noise from a real READ drift.