Repository navigation
perf(convert): cut DuckDB's bind cost of converted SQL - #283
Merged
Merged
Conversation
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.
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
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_stringandsplit_part, which write an argument twice. This PR shrinks what DuckDB binds without changing results.Changes
UPPER: guarded two-string lookup instead oflist_positionover 102-item lists.LOWER: onereplace.SPLITbinds a long operand once.variant_text_v2,variant_json_text_v2: escape once; scalars skip it._v1stays and is pinned.E' \t\n'and tests numbers first.NULLIFZEROoperands are bound once (nesting doubled per level), except a window over plain arguments, soRATIO_TO_REPORTis unchanged. Five emitters join withlist_aggr. LiteralSPLIT_PARTparts fold.make website-examples(incheck-ci) fails when one differs.Bind cost per call (synthetic, VARCHAR column, 50 calls per
PREPARE):UPPER(s)LOWER(s)::VARCHAR::FLOATDeviation from the issue: a
replace()chain forUPPER's 102 mappings bound at 10.07 ms, so it uses the lookup instead.Verification
RATIO_TO_REPORTbyte-identical to 0.24.0, and work-count guards.--no-verify: the local gate passed exceptcensus-duckshop, over its 300 s budget under load. Run besidemainat equal load, the branch used 3% less CPU. CI runs that job only after merge, not on pull requests.