Skip to content

perf(convert): write shorter DuckDB SQL for casts, division, zones and VARIANTs - #277

Merged
kvlonge merged 6 commits into
mainfrom
perf/shorter-duckdb-sql
Nov 25, 2026
Merged

kvlonge merged 6 commits into
mainfrom
perf/shorter-duckdb-sql

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Why

Converted SQL often spelled a value out at greater length than DuckDB needs. Text was trimmed three times, literals the compiler could fold were parsed at run time, and rounding used a doubling step when the result cast already rounds. Some checks repeated ones the surrounding SQL already makes. Each change shortens the SQL and keeps the same rows, types, NULLs and raised error tags.

Changes

  • Fold TIME text literals, and text literals cast to FLOAT, into plain casts.
  • Bind trimmed text once when reading it as FLOAT or NUMBER, with fewer arms.
  • Compute a FLOAT's units once when rounding it to a NUMBER, and square a FLOAT sum once in VARIANCE.
  • Round short decimal quotients by computing one spare digit and letting the result cast round it. AVG drops the division's own NULL and zero checks, which its zero-count case already covers.
  • Convert a nanosecond timestamp in a literal zone directly, drop the zone check for a literal zone, and read the last nanosecond digits with nanosecond().
  • Drop the outer cast on a raise where another arm already gives its type.
  • Write a FLOAT at a VARIANT's root as %.15e directly.
  • Skip the repeated-key check in OBJECT_CONSTRUCT when the keys are distinct literals.
  • Put whole NUMBERs of 18 digits or fewer into a VARIANT without json_checked_v1.
  • Move the VARIANT writer into a new helper macro, variant_written_v1.
  • Released macros keep their pinned bodies. The SQL size baseline is regenerated.

Verification

  • On DuckDB, each rewrite was compared with today's SQL over generated inputs: NULLs, boundaries, exact halves, special words, signed zero, malformed text and every recorded zone. There were 0 differences.
  • make verify-recorded (strict errors) on the final head: 0 failed, the same 397 agreeing error checks, 0 translation result changes.
  • SQL sizes: 13,940,273 B to 11,337,947 B (−18.7%); no case grew.
  • make check-ci passes. The blocks and convert crate tests pass.

Work in progress. Items 1-15 of the batch written; the FLOAT-in-VARIANT
writer macro is not. Not run: cargo tests, make check-ci, the full
verify. A first full replay of the earlier items is running.
…macro

Work in progress. All batch items written. Crate tests passed before
the zone change; not run since: make check-ci, the full verify.
…aseline

The recorded corpus replays with 0 failures and the SQL size baseline
falls from 13,940,273 to 11,337,947 bytes. Not run on this head yet:
make check-ci, the full verify.
A spare place there would make the scaling literal 0.<37 zeros>1, which
DuckDB types as DOUBLE. Also pins variant_written_v1 by the hash of its
definition statement.
@kvlonge
kvlonge enabled auto-merge November 24, 2026 22:51
…does not fold

The short argument was a text literal, which a FLOAT cast now folds to a
plain cast, so the length difference counted the whole block as copies.
@kvlonge
kvlonge added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit f203425 Nov 25, 2026
10 checks passed
@kvlonge
kvlonge deleted the perf/shorter-duckdb-sql branch November 25, 2026 00:34
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