Skip to content

[wrangler] Recognise compound statement markers without surrounding whitespace - #15226

Closed
MatheusMartinho wants to merge 12 commits into
cloudflare:mainfrom
MatheusMartinho:fix/d1-splitter-compound-delimiters
Closed

MatheusMartinho wants to merge 12 commits into
cloudflare:mainfrom
MatheusMartinho:fix/d1-splitter-compound-delimiters

Conversation

@MatheusMartinho

@MatheusMartinho MatheusMartinho commented Aug 16, 2026 •

Copy link
Copy Markdown

Fixes #15228. Related to #15093 (see note below).

splitSqlQuery() splits a SQL file into statements for wrangler d1 execute --file and wrangler d1 migrations apply. It tracks compound statements so that semicolons inside a trigger body do not split it, but it only recognised the BEGIN/CASE/END markers when they were surrounded by whitespace:

/\s(BEGIN|CASE)\s$/i
/\sEND[;\s]$/i

SQLite also accepts those markers delimited by punctuation, and then the split goes wrong in two ways:

  • INSERT INTO audit VALUES (new.id);END; — the END is preceded by ;, so the compound statement never terminates and every following statement is swallowed into the trigger and sent to D1 as one statement. The visible symptom is silently missing result sets, not an error.
  • CREATE TRIGGER ... WHEN (1=1)BEGIN — the BEGIN is preceded by ), so the compound statement never starts and the trigger body is split at its first internal semicolon.

Both markers are now matched when delimited by any non-identifier character, using a negative lookbehind so identifiers that merely end in a keyword — a weekend table, a value of 'weekend' — still do not match.

Review follow-up: bracket-quoted and non-ASCII identifiers

The Devin review correctly caught that widening the delimiter class introduced two false positives, which are now fixed:

  • Bracket-quoted identifiers: [ and ] are outside the identifier class, so a trigger body containing a name like [end] popped the compound statement early and the trigger was chopped apart. splitSqlIntoStatements() now consumes [ … ] like the other quote styles. This also fixes [wrangler] D1 SQL splitter does not treat bracket-quoted identifiers as quoted #15228, which is pre-existing: a ; inside a bracket-quoted identifier (CREATE TABLE metrics ([value;unit] TEXT);) splits the statement even on main — normalizeSqlLineEndings() in the same file already treats [ … ] as a quote pair, so the two scanners disagreed about the same syntax. (My earlier review reply claiming the [end] case reproduced identically on main was wrong — on main the whitespace-only markers coincidentally never fire inside [end]; I've corrected that in the thread.)
  • Identifiers with diacritics: the ASCII-only class treated a letter such as é as a delimiter, so a néend column matched the END marker. Both regexes now use Unicode identifier classes (\p{L}\p{N}_$ with the u flag), consistent with how isDollarQuoteIdentifier() in the same file treats letters with diacritical marks.

On #15093

That issue reports the same class of bug for a lowercase end;. Case handling is already fixed on main (both regexes carry /i), and I confirmed lowercase input now splits identically to uppercase, so the originally reported reproduction passes as-is. The variants above still reproduced, which is what this PR fixes.

Testing

Seven regression tests in splitter.test.ts, red/green verified:

  • ;END;, (1=1)BEGIN and SUM(CASE ... END) fail against main and pass with this change; the false-positive guard (weekend) passes both before and after.
  • [value;unit] fails on main and on this branch before the scanner change (it is the [wrangler] D1 SQL splitter does not treat bracket-quoted identifiers as quoted #15228 bug), and passes now.
  • The trigger-body [end] and néend cases pass on main, failed on this branch before the follow-up commits (the regressions Devin found), and pass again now.

Full file passing; check:type and check:format are clean. I also verified end-to-end with wrangler d1 execute --local --file that the [value;unit] schema fails on released wrangler 4.123.0 and that the trigger examples execute correctly with this branch.


  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: this fixes splitting of SQL that the documented behaviour already implies is supported; no user-facing interface changes.

Note

This contribution was written with an AI agent: Claude Code (Claude Fable 5), directed and reviewed by @MatheusMartinho, who takes responsibility for the change.


Devin Review

…hitespace

The D1 SQL splitter only treated BEGIN, CASE and END as compound
statement markers when they were surrounded by whitespace. SQLite also
accepts them delimited by punctuation, so a trigger body ending in
`INSERT ...;END;` never terminated and every following statement was
swallowed into it and sent as one statement, and a trigger declared with
`WHEN (1=1)BEGIN` was split in the middle of its body.

Match the markers when they are delimited by any non-identifier
character, keeping identifiers that merely end in a keyword, such as a
`weekend` table, from matching.
@changeset-bot

changeset-bot Bot commented Aug 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 02492ea

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
wrangler Patch
@cloudflare/vite-plugin Patch
@cloudflare/vitest-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/d1
  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/olive-donuts-battle.md: [@cloudflare/wrangler]
  • packages/wrangler/src/tests/d1/splitter.test.ts: [@cloudflare/d1 @cloudflare/wrangler]
  • packages/wrangler/src/d1/splitter.ts: [@cloudflare/d1 @cloudflare/wrangler]

devin-ai-integration[bot]

This comment was marked as resolved.

@pkg-pr-new

pkg-pr-new Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/@cloudflare/autoconfig@15226

@cloudflare/build-output-utils

npm i https://pkg.pr.new/@cloudflare/build-output-utils@15226

@cloudflare/codemods

npm i https://pkg.pr.new/@cloudflare/codemods@15226

@cloudflare/config

npm i https://pkg.pr.new/@cloudflare/config@15226

@cloudflare/containers-shared

npm i https://pkg.pr.new/@cloudflare/containers-shared@15226

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@15226

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@15226

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@15226

miniflare

npm i https://pkg.pr.new/miniflare@15226

@cloudflare/pages-functions

npm i https://pkg.pr.new/@cloudflare/pages-functions@15226

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@15226

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@15226

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@15226

@cloudflare/vitest-plugin

npm i https://pkg.pr.new/@cloudflare/vitest-plugin@15226

@cloudflare/workers-auth

npm i https://pkg.pr.new/@cloudflare/workers-auth@15226

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@15226

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@15226

wrangler

npm i https://pkg.pr.new/wrangler@15226

commit: 02492ea

…racter

Relaxing only the character before the marker made a parenthesised CASE
expression, such as SUM(CASE WHEN a THEN 1 ELSE 0 END), open a compound
statement that END) never closed, so the rest of the file was sent as a
single statement. Accept any non-identifier character after END too, and
cover the case in the tests.
devin-ai-integration[bot]

This comment was marked as resolved.

@MatheusMartinho

MatheusMartinho commented Aug 16, 2026 •

Copy link
Copy Markdown
Author

The three red Tests jobs are unrelated to this change — they are container, dev-registry and browser-rendering fixtures:

Job Failing task Symptom
Tests (Linux, fixtures) @fixture/interactive-dev container image never comes up: runc run failed: container process is already dead, then Container is not listening to port 8080
Tests (macOS, fixtures) @fixture/dev-registry supports fetch over service binding times out after 58s (already retried)
Tests (Windows, packages-and-tools) miniflare 18 of 21 plugins/browser tests fail, plus one dev-registry timing test

This diff only changes two regexes in the D1 SQL splitter, and the tests that cover it pass on all three platforms — on the Windows run, d1/execute.test.ts (16) and d1/export.test.ts (12) are green, and splitter.test.ts passes at 21/21 locally.

Worth noting when comparing against other PRs that appear fully green: they do not run the same set. Because this PR touches wrangler, Turbo invalidates nearly every fixture (0 cached, 94 total here), whereas e.g. #15227 never executed @fixture/interactive-dev at all. The dev-registry flakiness also looks known — there are several active branches on the repo (exp/dev-registry-crash-bisect, fix/dev-registry-teardown-order, fix/defer-dev-registry-registration) investigating it.

Happy to rebase or push an empty commit for a re-run if that helps.

MatheusMartinho and others added 3 commits August 17, 2026 11:46
The statement scanner handled ', " and backtick quoting but not SQLite's
bracket-quoted identifiers, while normalizeSqlLineEndings() in the same
file already did. A ; inside such an identifier, as in
CREATE TABLE metrics ([value;unit] TEXT), was treated as a statement
boundary and broken fragments were sent to D1 (pre-existing, cloudflare#15228).
With the punctuation-delimited markers from this branch the gap also
made a [end] column inside a trigger body pop the compound statement
early and chop the trigger apart.

Consume [ ... ] like the other quote styles, and cover both cases in
the tests.
The ASCII-only class treated a letter with a diacritical mark as a
delimiter, so a column such as néend matched the END marker and closed
a trigger body early. Match identifier characters with \p{L}\p{N}
instead, consistent with how isDollarQuoteIdentifier() in the same file
treats letters with diacritics.
@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/d1
  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/brave-brackets-split.md: [@cloudflare/wrangler]
  • .changeset/olive-donuts-battle.md: [@cloudflare/wrangler]
  • packages/wrangler/src/tests/d1/splitter.test.ts: [@cloudflare/d1 @cloudflare/wrangler]
  • packages/wrangler/src/d1/splitter.ts: [@cloudflare/d1 @cloudflare/wrangler]

@MatheusMartinho

Copy link
Copy Markdown
Author

Now rebased on main (0fde4b0), and updating my earlier note on the red checks, because the failing set has changed since I wrote it.

All three fixtures jobs are green now. The two red ones are both in packages-and-tools, and neither touches D1:

Job Failing test Symptom
Tests (Linux, packages-and-tools) @cloudflare/vitest-plugin : test/new-config.test.ts > evaluates config functions with the Vite mode Error: Test timed out in 15000ms
Tests (Windows, packages-and-tools) miniflare : test/dev-registry.spec.ts > DevRegistry > registers after a conflicting entry becomes stale AssertionError: expected 'previous-instance' to be 'f6c6c4ac-54dc-428c-abde-df4488f6d098'

This diff only changes the D1 SQL splitter in packages/wrangler/src/d1/splitter.ts, and the tests that cover it are green on all three platforms: splitter.test.ts passes 21/21, and on the Windows run d1/execute.test.ts and d1/export.test.ts are both green.

The dev-registry failure looks like the flake already being tracked on exp/dev-registry-crash-bisect, fix/dev-registry-teardown-order and fix/defer-dev-registry-registration.

Both Devin findings are fixed and I have resolved those two threads: bracket-quoted identifiers are now consumed by splitSqlIntoStatements() (de80155) and the marker regexes use Unicode identifier classes (d543f3a), with regression tests for the [end], [value;unit] and néend cases.

Ready for a codeowner from @cloudflare/d1 or @cloudflare/wrangler whenever there is a moment. Happy to push an empty commit for a CI re-run if that helps.

@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/d1
  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/brave-brackets-split.md: [@cloudflare/wrangler]
  • .changeset/olive-donuts-battle.md: [@cloudflare/wrangler]
  • packages/wrangler/src/tests/d1/splitter.test.ts: [@cloudflare/d1 @cloudflare/wrangler]
  • packages/wrangler/src/d1/splitter.ts: [@cloudflare/d1 @cloudflare/wrangler]

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

This report is out of date. Scroll down for Devin Review's latest report on this PR.

Devin Review found 2 potential issues.

Devin Review

Comment thread packages/wrangler/src/d1/splitter.ts Outdated
* include letters with diacritical marks, as in `isDollarQuoteIdentifier()`,
* so a `néend` column does not match either.
*/
const COMPOUND_STATEMENT_START = /(?<![\p{L}\p{N}_$])(BEGIN|CASE)\s$/iu;

@devin-ai-integration devin-ai-integration Bot Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Parenthesized CASE breaks trigger splitting

When a trigger contains CASE(, COMPOUND_STATEMENT_START misses the case expression. Its END closes the trigger early, splitting remaining statements apart.

Learn more

Update the compound-statement start matching in packages/wrangler/src/d1/splitter.ts so BEGIN and CASE are recognized when followed by any valid non-identifier delimiter, not only whitespace. Preserve the guard against keyword suffixes in identifiers. Add a splitter regression test with a trigger containing CASE(expression) and at least one subsequent statement inside the trigger, followed by another top-level statement, so premature closure is observable.

Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread packages/wrangler/src/d1/splitter.ts Resolved
@MatheusMartinho

Copy link
Copy Markdown
Author

Thanks for the review. Both cases are real, and both sit outside what this PR set out to fix, so I'd rather be explicit about scope than silently widen it.

This PR fixes one thing: a lowercase end; closing a compound statement was not recognised, so every statement after it got merged into the previous one, silently and with no error. That is the bug I hit and reproduced.

On the two cases you raise:

  1. CASE( — agreed, the marker match does not account for a following parenthesis. I'm happy to add it here if you'd rather keep it to one PR, or leave it as a follow-up so this one stays a minimal fix with its own regression test.

  2. Decomposed Unicode before end — also agreed, and I'd prefer to handle it separately, since normalising identifiers touches the tokenizer more broadly than the splitter does.

Let me know which you prefer and I'll push.

The two failing checks (vitest-plugin timeout and miniflare dev-registry) are unrelated to the splitter. The D1 splitter tests pass 21/21.

@workers-devprod

workers-devprod commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/d1
  • ✅ @cloudflare/wrangler
Show detailed file reviewers
  • packages/wrangler/src/tests/d1/splitter.test.ts: [@cloudflare/d1]
  • packages/wrangler/src/d1/splitter.ts: [@cloudflare/d1]

@cjol cjol self-assigned this Sep 15, 2026
Comment thread packages/wrangler/src/d1/splitter.ts Resolved
devin-ai-integration[bot]

This comment was marked as resolved.

@MatheusMartinho

Copy link
Copy Markdown
Author

Thanks for picking this up, @cjol, and for pushing the fixes directly.

Adding \p{M} is the right call. I had flagged the decomposed-accent case in the Devin thread and pushed back on handling it here, because I assumed it meant normalising identifiers somewhere in the tokenizer, which felt out of scope for this PR. Extending the identifier class to include combining marks does the same job in one character and keeps the change local. Good catch.

I have just updated the branch against main. Happy to take anything else you want on this.

@MatheusMartinho

Copy link
Copy Markdown
Author

Thanks for the review @cjol!

The only red check is Tests (macOS, packages-and-tools), and the failure looks unrelated to this PR:

FAIL  test/index.spec.ts > Miniflare: python modules
Error: Test timed out in 30000ms
 ❯ test/index.spec.ts:2112:1

That's a 30s timeout in miniflare's Python modules test — 1 failed out of 1319. This PR only touches packages/wrangler/src/d1/splitter.ts and its test file, so it shouldn't be able to affect miniflare or Python support.

I don't have write access to re-run the job. Could someone kick it off? Happy to rebase onto main instead if that's easier.

@cjol

cjol commented Sep 21, 2026

Copy link
Copy Markdown
Member

@MatheusMartinho gonna hold of on merging this in favour of #15712 which seems a more robust fix.

@MatheusMartinho

Copy link
Copy Markdown
Author

Makes sense — porting SQLite's complete.c is the right fix, and better than patching the splitter one edge case at a time.

I checked #15712's tests against this PR's: they already cover every case added here (END right after ;, BEGIN right after (, CASE closed by ) or ,, identifiers ending in END including accented ones, qualified keyword identifiers, named parameters, bracket-quoted identifiers), so nothing needs to carry over. Happy for this to be closed once #15712 lands.

Thanks for the review, @cjol.

@cjol cjol closed this Sep 23, 2026
@github-project-automation github-project-automation Bot moved this from Untriaged to Done in workers-sdk Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[wrangler] D1 SQL splitter does not treat bracket-quoted identifiers as quoted

3 participants