Repository navigation
feat(convert): let callers skip DuckDB's bind check, and bind a simple CASE operand once - #282
Merged
Merged
Conversation
The Snowflake parser sliced its source by character offsets by walking the whole text from the start on every call once the text held any non-ASCII character, so parsing such a statement cost its length times the number of slices. It now builds the character-to-byte offsets once per parse, which also takes 2.6% off the strict-parsing instruction count; the baseline records the gain. Synthetic tests guard set-operator chains: their converted SQL nests no deeper and grows by the same bytes for each further branch, and chains over a multi-byte character return Snowflake's rows.
`sqlquack convert --no-bind-check` and `ConvertOptions(bind_check=False)` skip DuckDB's parse and bind of the emitted statement and of SQL function bodies, for callers that run the converted SQL and handle DuckDB's errors. The bind only catches converted SQL DuckDB cannot bind, a SQLQuack bug, and is often the larger part of the conversion time. The lexical checks (one statement, no lambda parameter read with a dot) still run, every other refusal is unchanged, and the default keeps the check.
A simple CASE over a computed operand binds the operand once in a lambda that returns the first equal WHEN's position, then chose the result with a simple CASE over that position. DuckDB repeats a simple CASE's operand in every WHEN, so the whole lambda, and everything in the operand, was bound once per WHEN: binding grew with the WHENs times the operand's size. When every result and the ELSE are a column or a literal, they are now packed beside the operand and chosen inside the lambda, so DuckDB parses the operand once. Each result is still cast only where it is chosen. Other CASEs keep the position form.
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.
Why
Converting a corpus of 787 real SQL unit-test statements took ~70 s wall (~270 s CPU) on main. Most of the remaining time was DuckDB binding the converted SQL inside
convert, which callers that execute the SQL themselves don't need, plus one pathological shape where a simpleCASEwith a computed operand was bound once perWHEN.Changes
fix(convert): the Snowflake parser builds character-to-byte offsets once per parse instead of re-walking non-ASCII source on every slice (largest probe 1,447 -> 1,250 ms; strict-parsing instructions -2.6%, baseline re-recorded). Synthetic set-chain tests guard nesting depth and per-branch output growth.feat(convert):ConvertOptions(bind_check=False)/--no-bind-checkskips DuckDB's bind check of the converted SQL for callers that run it and handle DuckDB errors. The default is unchanged; the cheap lexical checks still run. On the corpus, no outcome, SQL or refusal changes with it off. Docs say bind-only failures then surface when the SQL runs.fix(convert): a simpleCASE <computed operand> WHEN ...whose results and ELSE are columns or literals now carries them in the lambda, so DuckDB binds the operand once instead of once perWHEN(synthetic bind time flat at ~95 ms from 1 to 20 branches, previously 100 ms -> 1,668 ms). Work-count guard: copies of the operand in DuckDB's parse (json_serialize_sql) are 1 instead of 2/8/32. 384 synthetic combinations return identical rows to main.Verification
make check-ci,rust-lint, crate tests for sqlquack-blocks, sqlquack-convert and sqlquack, andmake python-apipass; hygiene scan clean.