Skip to content

feat(convert): let callers skip DuckDB's bind check, and bind a simple CASE operand once - #282

Merged
kvlonge merged 3 commits into
mainfrom
feat/convert-bind-opt-out
Nov 25, 2026
Merged

kvlonge merged 3 commits into
mainfrom
feat/convert-bind-opt-out

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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 simple CASE with a computed operand was bound once per WHEN.

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-check skips 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 simple CASE <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 per WHEN (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, and make python-api pass; hygiene scan clean.
  • Corpus (release builds, compact output): main 46.8 s wall / 182 s CPU, p90 557 ms; this branch with the bind check off 7.5 s wall / 28 s CPU, p90 78 ms, max 419 ms.

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.
@kvlonge
kvlonge added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit b9fd94c Nov 25, 2026
15 checks passed
@kvlonge
kvlonge deleted the feat/convert-bind-opt-out branch November 25, 2026 09:19
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.

1 participant