Skip to content

fix(sql): keep a SQLite trigger or PostgreSQL BEGIN ATOMIC body in one statement - #553

Open
goranai wants to merge 1 commit into
bytebase:mainfrom
goranai:fix/compound-body-split
Open

goranai wants to merge 1 commit into
bytebase:mainfrom
goranai:fix/compound-body-split

Conversation

@goranai

@goranai goranai commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #552

splitSQLStatements ended a statement at every ; outside a quote or comment. A SQLite trigger body and a PostgreSQL 14+ BEGIN ATOMIC function or procedure body hold ; of their own, so the SQLite and PostgreSQL connectors, which run the pieces one by one, got incomplete statements. On SQLite every CREATE TRIGGER failed with incomplete input.

The change is in src/utils/sql-parser.ts. While scanning, splitWith now reads the words of a statement (only outside comments, strings and quoted names, because it reads only the scanner's plain tokens) and keeps the body whole:

  • SQLite: a statement that starts CREATE [TEMP|TEMPORARY] TRIGGER opens a body at its BEGIN.
  • PostgreSQL: a statement that starts CREATE opens a body at BEGIN ATOMIC.
  • The body ends at the first END that starts a statement. For SQLite this is the rule sqlite3_complete() uses: an END that comes right after a ;. A CASE ... END, and a column that is named end (SQLite accepts UPDATE t SET end = 1), do not start a statement, so they do not close the body. The next ; then ends the statement as before. PostgreSQL also allows an empty body, BEGIN ATOMIC END, so there an END right after ATOMIC closes the body too. The same check runs for SQLite, so a trigger written as BEGIN END is closed there as well. sqlite3_complete() does not close on that one, but SQLite rejects a trigger with no statement in it either way.
  • A transaction BEGIN, a BEGIN in any other statement, BEGIN ATOMIC inside a $$ body, and a column or alias named begin do not open a body.
  • MySQL, MariaDB, SQL Server, Oracle, ClickHouse and the no-dialect splitter are unchanged. A body with no END runs to the end of the input.

The read-only check is not weakened. A body only opens in a statement that starts with CREATE, which read-only mode refuses anyway. Classes change only from unknown to ddl for these statements, and no verdict changes.

Behavior change: a body that is still open at the end of the input (a trigger with no END) is now one piece instead of several. SQLite and PostgreSQL reject it either way.

Tests:

  • sql-parser.test.ts: 14 new cases (9 fail on main).
  • sqlite.integration.test.ts (no Docker): a trigger with two statements and a CASE, then an insert that fires it. Fails on main with incomplete input.
  • Unit suite: 1774 passed, 4 skipped, 1 failed. The one failure is manager.test.ts "should resolve an alias from ~/.ssh/config", which also fails on main on Windows (the test expects .ssh/config and gets a backslash path). tsc --noEmit is clean.

I also checked it against real parsers and real files:

  • 60000 random SQLite scripts with triggers (CASE in the body and in WHEN, END in strings, comments and quoted names, a column named end, TEMP, IF NOT EXISTS, BEGIN/COMMIT/END statements, CRLF, non-ASCII names), split and compared with the sqlite3_complete() rule (the SQLite engine accepts 27541 of them): 0 differ with the change, 48302 differ on main. Every trigger in these scripts has at least one statement in its body.
  • 20000 random SQLite scripts with triggers, split and prepared piece by piece against the SQLite engine running the whole script (the engine accepts 12239 of them): 0 mismatches with the change, 8750 on main.
  • 2 x 100000 random PostgreSQL scripts against the statement ranges of libpg-query, 89869 and 89788 that it parses: 0 differ with the change, 54406 and 54485 differ on main.
  • The schema of a real sqlite-utils 4.2.1 database (FTS5 and counts triggers) runs through SQLiteConnector.executeSQL and the execute_sql tool, and the triggers fire. On main it fails with incomplete input.
  • The 10 BEGIN ATOMIC blocks of PostgreSQL's create_function_sql.sql: 14 statements, all split correctly. On main all 10 blocks were split wrongly.
  • Old and new splitter on 1.6 million combinations of 8 dialects: 22313 differ, and every one has a CREATE ... TRIGGER ... BEGIN or CREATE ... BEGIN ATOMIC head. No difference for MySQL, MariaDB, SQL Server, Oracle, ClickHouse or no dialect.
  • Read-only: 20021 inputs, with and without a hidden write after a trigger or function body, give the same verdicts on main and with the change.

Known limits: I had no PostgreSQL server, so the PostgreSQL half is checked against libpg-query and not against a running server. CREATE RULE ... DO ALSO (stmt; stmt) is still split at its inner ;, as on main.

Two more limits. EXPLAIN CREATE TRIGGER ... on SQLite is still cut at the inner semicolons, as on main. One case is new with this change: in PostgreSQL, two adjacent column names begin and atomic (SELECT 1 AS begin, atomic FROM t; SELECT 2) are read as a body start, so the next statement is joined to that piece. Read-only mode still judges the joined piece by its first statement.

Validated with Claude Opus 5.5.

@goranai
goranai requested a review from tianzhou as a code owner November 29, 2026 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

execute_sql cannot create a SQLite trigger or a PostgreSQL BEGIN ATOMIC function: the statement is cut at the semicolons inside its body

1 participant