perf(parser): short-circuit _parse_pivots when not at PIVOT/UNPIVOT#7557
Merged
georgesittas merged 1 commit intomainfrom Apr 24, 2026
Merged
perf(parser): short-circuit _parse_pivots when not at PIVOT/UNPIVOT#7557georgesittas merged 1 commit intomainfrom
georgesittas merged 1 commit intomainfrom
Conversation
…/UNPIVOT _parse_pivots runs once per Join and most table refs, but always builds an iter+list even when the next token can never start a PIVOT clause. Guard on the token type up front so the common no-pivot path is a single check. Small but consistent win under mypyc (~1-3% on join-heavy queries like many_joins). No behavior change.
Contributor
SQLGlot Integration Test ResultsComparing:
By Dialect
Overallmain: 113234 total, 112044 passed (pass rate: 98.9%), sqlglot version: sqlglot:parse-pivots-guard: 110925 total, 110027 passed (pass rate: 99.2%), sqlglot version: Transitions: Dialect pair changes: 0 previous results not found, 1 current results not found ✅ 34 test(s) passed |
georgesittas
approved these changes
Apr 24, 2026
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.
Summary
`_parse_pivots` is called once per Join and after most table refs, but always builds a callable-iterator + `list()` even when the next token can never start a `PIVOT`/`UNPIVOT` clause. Guard on the token type up front so the common no-pivot path is a single membership check.
Perf
Measured on the 200-JOIN `many_joins` benchmark with `sqlglot[c]`:
~3% faster on join-heavy parses. Perf report showed `_parse_pivots` at 1.89% of parse before; the change reduces it to near zero in the common case. No measurable effect on pure Python (interpreter overhead dominates).
Test plan