Repository navigation
Fold logical AND, OR and NOT through the Scheme optimizer - #1072
Merged
Merged
Conversation
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.
Logical
AND,ORandNOTexpressions currently retain provably constant branches until physical code optimization, carrying unnecessary boolean trees through decorrelation and later planning passes. Fold complete constant expressions while combining bound logical rewrites, using the existing Scheme optimizer rather than a separate SQL truth-value implementation.Add
tryfold(code): it accepts a raw Scheme expression and returns its constant value, or raises when folding fails. It uses the optimizer's existing function declaration metadata and lazy evaluation rules. It clones the input AST and decodes only constant values/data encodings; it never executes residual code. SCM/Go has no knowledge of frontend expression/type/collation records. Each frontend inlib/owns its metadata; this boundary is now explicit inINVARIANTS.md.The logical planner tries this interface for boolean operators with literal operands and retains the original AST on failure. For example,
0 AND expressioncan become false, whilethrowing_expression AND 0must retain its earlier operand and its error. The same distinction applies toOR. SQL name binding still validates the complete expression first, and collected stages/sources remain available to the existing dependency-demand pass. Mixed expressions that are not completely constant stay as logical ASTs.This also fixes the failing normal/JIT SQL tests: the earlier PR implementation incorrectly suppressed invalid-REGEXP errors preceding a later false/true operand.
Manual A/B measurements
The workload contains ordered SELECTs with OR-connected
LIKEpredicates,IN (SELECT ... UNION SELECT ...)membership, and nested correlatedEXISTS/scalar projections. The persisted fixture contains 80,000 outer rows and 40,000 membership rows. The unbounded SELECT case complements the existing LIMIT cases; two additional actor/session variants verify permission results.Compared unchanged development baseline
beb30f0c298e82b340c2c6de242b8b364bf6d4bcwith the final implementation usingrun_sql_tests.py --perf-ab. Each worktree uses its own build, the same candidate test specification, and independent copies of the same baseline-prepared fixture. Both roles usewarmup: 0,timing_samples: 3,timing_aggregation: total: one initial query plus two further queries, in identical case order. Repeated the complete comparison with a second fresh fixture.These measurements include initial compilation and statistics-driven recompilation. The final optimized physical Scheme program is unchanged after normalizing generated structural hashes: the gain is in query compilation.
Validation
ordered-fulltext-membership.yamlpass, including restricted and unprivileged actors./scm.make jitgen; Go formatting, the changed Scheme file formatter check andgit diff --checkpass.