Repository navigation
fix(v18): throw SqlError from fingerprint/normalize/scan on every libpg_query error - #183
Open
pyramation wants to merge 4 commits into
Open
pyramation wants to merge 4 commits into
pyramation wants to merge 4 commits into
Conversation
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
Review complete. No blocking issues — approved ✅; 2 nitpicks below. 🧹 Nitpicks (2) — 🟢 2 low
This PR replaces error-string plumbing with a structured
Reviewed commit: 229f315 |
…tial or empty tokens
This was referenced Oct 25, 2026
This was referenced Oct 25, 2026
|
Folded into #190 |
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
wasm_fingerprint,wasm_normalize_queryandwasm_scanreturned either the result or the error message as one bare string. The JS side guessed which it was from the prefix (syntax error/deparse error/ERROR). Any other libpg_query error was therefore returned as if it were the result:Running the PostgreSQL regression corpus (libpg_query
test/sql/**, 50,928 statements) showed 76 statements wherefingerprint/normalizereturned an error message. All of them are grammarereports that aren't syntax errors, e.g.column number must be in range from 1 to 32767andunrecognized partitioning strategy "magic". 18.2.0 and 18.2.1 behave the same.Change:
wasm_fingerprint,wasm_normalize_queryandwasm_scannow return the existingWasmDetailedResult(has_error,message,funcname,filename,lineno,cursorpos,context,data), freed withwasm_free_detailed_result.readDetailedResult()intemplates/full/index.tsthrows the sameSqlErrorthatparseSyncthrows, includingcursorPosition(0-based). The prefix check is gone.build_scan_jsonnow escapes the other control characters (< 0x20) as\u00XX, with buffers sized at 6 bytes per input byte. Before,scanSync("SELECT E'a\u0002b'")produced invalid JSON (corpus case from libpg_query'sparse_tests.c).build_scan_json, the scan unpack or the result copy fails to allocate, the function returnsNULL, and JS throwslibpg_query returned no result: out of WASM memory …. Before, the whole token list came back empty, and any single token whose copy failed was skipped. The buffer-size sum is also checked for 32-bitsize_toverflow.v13–17 are unaffected: they only export
parse/parseSync, which already read the structuredPgQueryParseResult.Tests (v18)
New cases in
test/fingerprint.test.js,test/normalize.test.jsandtest/scan.test.js:SqlErrorwith the exact message, including lexer errors (SELECT 'abc,SELECT "x,SELECT $$x);SqlErrorInvalid input…;scanSync/scanmust throwout of WASM memory, and the next call must still work;scanSync('SELECT 1;\n'.repeat(2000000))(6M tokens, repro from fix(scan): throw when the scan result does not fit in memory #184) must throw instead of returningtokens: [], while 1M repeats still returns all 3M tokens;Results:
wasm/(current main):tests 52, pass 40, fail 12(actual: 'SyntaxError', expected: 'SqlError'etc.). The control-character test also fails before the escape fix:Bad control character in string literal in JSON.Missing expected exception: published 18.2.1 returnstokens: []for both). The other new tests in that commit (lexer errors in fingerprint/normalize, empty input) already passed at ed28cca and pin that behaviour.pnpm build, emsdk 6.0.3):pnpm testpasses 165/165.SqlError.Known, not fixed here
From about 50 MB of short statements (≈18.6M tokens),
scanSyncaborts withRuntimeError: Aborted(Assertion failed: <binary>)ortable index is out of bounds. Sync calls in the same tick then keep failing until the replacement instance loads; async calls recover.The cause is in the libpg_query fork's
pg_query_scan(src/pg_query_scan.c): it never checks itsmallocs foroutput_tokens, each token, andresult.pbuf.data. Once the 1 GiB heap is exhausted,malloc(result.pbuf.len)returnsNULL, andpg_query__scan_result__packwrites the protobuf to address 0. In wasm that does not trap; it overwrites static data. The "assertion text" is literally packedScanTokenbytes (\b,\u0010,,(field tags).Because the instance's memory is corrupt, the wrapper's stale-instance sync path cannot be used safely; this is not a wrapper recovery bug. Repro:
scanSync('SELECT a, b FROM t WHERE c = 1;\n'.repeat(1690000)). 39.7 MB still throws the clean out-of-memory error. The fix belongs in the fork: return an "out of memory"PgQueryErrorwhen thosemallocs fail.