Skip to content

perf(convert): write set operations, constant JSON and repeated operands once - #279

Merged
kvlonge merged 10 commits into
mainfrom
perf/union-and-json-folding
Nov 25, 2026
Merged

kvlonge merged 10 commits into
mainfrom
perf/union-and-json-folding

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Why

Converted SQL still said things several times. Every UNION ALL branch restated its names, parentheses and integer casts, a constant PARSE_JSON literal was checked at run time, and some blocks repeated an operand or regex match. Each change keeps the same rows, names, types, NULLs and raised error tags.

Changes

  • Set operations: only the first branch writes column names, plain branches lose their parentheses, and a relation aliased to its own name drops the alias. A UNION ALL chain whose every branch holds an integer literal in a column writes it bare and casts the column once around the chain, where that is shorter.
  • A PARSE_JSON of a string literal that json_checked_v1's fast check would read as it is becomes json('<literal>'). A stricter Rust reader decides; any other literal keeps the call.
  • VARIANCE and STDDEV of a long FLOAT operand list the values once for the count and both exact sums.
  • TO_DATE and TO_TIMESTAMP_NTZ in a literal format match its groups once.
  • An ordered window aggregate without a frame no longer writes DuckDB's default frame.
  • A cast of a long VARIANT-held value to ARRAY or OBJECT binds the value once.
  • The SQL size baseline is regenerated; released macros are unchanged.

Verification

  • New tests: set operations over the shop rows (names, DuckDB types and rows, including BY NAME, EXCEPT and INTERSECT around a UNION ALL, a star before a literal, and literals of 18 and 19 digits); constant JSON literals that fold or keep the macro (duplicate keys, 64-bit overflow, -0, NaN, malformed text) with the same DuckDB result either way; VARIANCE of text read as FLOAT.
  • Grouped and per-group regexp_extract agree on DuckDB for the corpus's 42 format patterns.
  • make verify-recorded (strict errors): 0 failed, 8,237 passed and 1,054 diverged as recorded; error checks 397 agree, 113 divergence raises, 0 findings; 0 syntax-coverage changes.
  • SQL sizes: 11,337,947 B to 9,089,909 B (−19.8%); no case grew. make check-ci passes.

…e fast check would

A PARSE_JSON of a string literal that json_checked_v1's fast check reads
as it is (plain strings, short numbers, true, false and null, valid, no
key repeated) is now json('<literal>') instead of the macro call. A Rust
reader decides it and is never looser than the macro: it also refuses
escaped keys and escaped backslashes, and any other literal keeps the
call.
…integer casts

- Only a set operation's first branch writes its select-list names;
  DuckDB takes the result's names from it. BY NAME and ordered set
  operations keep every name they read.
- A plain SELECT branch, and a left branch that is the same set
  operation, are no longer parenthesised; DuckDB groups them the same.
- A relation aliased to its own name is written without the alias.
- A UNION ALL chain whose every branch holds an integer literal of at
  most 18 digits in a column writes it bare and casts the column once
  around the chain, where that saves more bytes than the wrapper costs.
…and sums

VARIANCE, VAR_POP, STDDEV and the like over a long operand, such as text
read as FLOAT, wrote the operand into its count, its exact sum and its
exact sum of squares, seven times in all. They now list the group's
values once and take the count, the sum and the sum of squares from that
list, with the same exactness checks and raise.
…ame, bind a long held cast

- TO_DATE and TO_TIMESTAMP_NTZ in a literal format extract all the
  format's groups with one regexp_extract into a struct, where that is
  shorter than one regexp_extract per group.
- An ordered window aggregate without a frame no longer writes RANGE
  BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which is DuckDB's frame
  when none is written.
- A cast of a long VARIANT-held value to ARRAY or OBJECT binds the value
  once instead of writing it in both the kind check and the result.
A later branch's star expands to several columns, so an item's position
was not its column's: a literal after the star marked the wrong column
bare, skipping that column's own reading of text. The chain now casts
once only when every branch has one item per column.
…nd meet the lint rules

A star can expand to no columns, so a branch could match the first's
item count while its columns still shifted. The chain now casts once
only when no branch item is a star. The set operation helpers take
grouped parameters, the new constants move to the constants files, and
the test helpers move to helpers.rs.
@kvlonge
kvlonge enabled auto-merge November 25, 2026 03:42
The playground test and the converted SQL on the home page and in the
docs still showed FROM "ORDERS" AS "ORDERS", which the converter no
longer writes.
@kvlonge
kvlonge added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit fa86bb0 Nov 25, 2026
10 checks passed
@kvlonge
kvlonge deleted the perf/union-and-json-folding branch November 25, 2026 05:29
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