Skip to content

perf(convert): cut DuckDB's bind cost of converted SQL - #283

Merged
kvlonge merged 8 commits into
mainfrom
perf/convert-bind-cost
Nov 25, 2026
Merged

kvlonge merged 8 commits into
mainfrom
perf/convert-bind-cost

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Why

On DuckDB, converted SQL spends most of its time planning: DuckDB pastes a macro's body in at every call, including its own nullif, array_to_string and split_part, which write an argument twice. This PR shrinks what DuckDB binds without changing results.

Changes

  • UPPER: guarded two-string lookup instead of list_position over 102-item lists. LOWER: one replace. SPLIT binds a long operand once.
  • variant_text_v2, variant_json_text_v2: escape once; scalars skip it. _v1 stays and is pinned.
  • Text to FLOAT trims with E' \t\n' and tests numbers first.
  • Computed NULLIFZERO operands are bound once (nesting doubled per level), except a window over plain arguments, so RATIO_TO_REPORT is unchanged. Five emitters join with list_aggr. Literal SPLIT_PART parts fold.
  • Website: examples regenerated from real output; make website-examples (in check-ci) fails when one differs.

Bind cost per call (synthetic, VARCHAR column, 50 calls per PREPARE):

Before After
UPPER(s) 2.97 ms 0.57 ms
LOWER(s) 0.72 ms 0.10 ms
VARIANT ::VARCHAR 2.63 ms 1.94 ms
text ::FLOAT 0.59 ms 0.51 ms

Deviation from the issue: a replace() chain for UPPER's 102 mappings bound at 10.07 ms, so it uses the lookup instead.

Verification

  • Replay: 12,258 recorded cases, 0 failed. SQL sizes 9,089,909 to 9,057,744 B.
  • Synthetic tests on values (all 102 mappings, FLOAT spellings, VARIANT), RATIO_TO_REPORT byte-identical to 0.24.0, and work-count guards.
  • Pushed with --no-verify: the local gate passed except census-duckshop, over its 300 s budget under load. Run beside main at equal load, the branch used 3% less CPU. CI runs that job only after merge, not on pull requests.

SPLIT named its text three times: in the NULL test, in the empty-separator
branch and in string_split. A long computed operand, such as an UPPER with full
case mapping, was pasted and bound three times. A long operand now reaches the
expression once through a lambda parameter, as other text conversions do.
LOWER has a single multi-character mapping (U+0130). It now converts to
lower(replace(text, U+0130, 'i' U+0307)) instead of a per-character lambda
lookup, so DuckDB binds no lambda for it. Results are unchanged.
UPPER with full case mapping looked each character up with list_position over
two 102-item list literals, which DuckDB binds at every call. Text holding a
mapped character now finds its mapping by instr in one string of keys and one
split of the mapped texts; other text goes straight to DuckDB's upper. A long
operand is still bound once. Results are unchanged, and binding one call takes
about a quarter of the time it did.
…it in CI

The homepage still showed the division output from before 0.23.2, and other
examples had drifted from what sqlquack prints: the HASH refusal was wrapped by
hand, the census coverage report named an old category, the helper-macros page
listed macros convert no longer emits alone, and several blocks left out a final
status line. Each example now shows real output, cut down only where marked …

website/examples holds every example's input files. make website-examples, part
of make check-ci, runs the commands each marked block shows with the built
sqlquack and fails where the page shows anything else, or where a terminal or
console block names no example. website/examples/README.md describes the
markers and the few ways a page may shorten output.
…e once

variant_text_v1 and variant_json_text_v1 called json_escaped_v1 on each text
they might write, and DuckDB pastes a macro's body in at every call, so every
VARIANT written as text bound the escaping twice. variant_text_v2 and
variant_json_text_v2 choose the text first and escape it once. A number or
boolean, which escaping never changes, is written as it is. Results are
those of version 1, which stays for SQL converted by earlier releases; both
new definitions are pinned.
…, numbers first

The inline text-to-FLOAT conversion trimmed with ' ' || chr(9) || chr(10),
which DuckDB binds as three calls at every cast, and tested for NaN before the
number pattern, so ordinary numbers ran two regular expressions. It now trims
with the literal E' \t\n' and tests numbers first; the patterns are disjoint,
so results are unchanged. Released macro bodies keep the original spelling, so
their pinned definitions stay as they are.
DuckDB defines nullif, array_to_string and split_part as SQL macros that paste
an argument twice, so a computed argument was bound twice, and nested
NULLIFZERO doubled at every level. NULLIFZERO now binds an operand once unless
it is a column, a literal, or a window function over plain arguments, which
DuckDB's window operator computes once however often it is written; so
RATIO_TO_REPORT converts exactly as before. UPPER, INITCAP,
JAROWINKLER_SIMILARITY, ARRAY_TO_STRING and arrays written as text join through
list_aggr, which takes the list once; where a list may be empty, coalesce keeps
array_to_string's ''. A literal SPLIT_PART part is read as 1 for 0 when
converting, so the macro pastes an integer rather than a CASE. Results are
unchanged.
168 recorded cases change size: 115 shrink with the case mapping, VARIANT
text, FLOAT and SPLIT changes; 53 grow within the allowed growth, mostly
ARRAY_TO_STRING, whose list_aggr spelling is longer but which DuckDB now
expands once instead of twice. All 9,404 cases total 9,057,744 B, down from
9,089,909 B.
@kvlonge
kvlonge added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 6ddea7b Nov 25, 2026
11 checks passed
@kvlonge
kvlonge deleted the perf/convert-bind-cost branch November 25, 2026 21:10
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