fix: calcite optimization adds LITERAL_AGG(true) #963
Open
mbwhite wants to merge 2 commits into
Open
Conversation
Signed-off-by: matthew brian white <whitemat@uk.ibm.com>
|
ACTION NEEDED Substrait follows the Conventional Commits specification for release automation. The PR title and description are used as the merge commit message. Please update your PR title and description to match the specification. |
Signed-off-by: matthew brian white <whitemat@uk.ibm.com>
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.
fix: calcite optimization adds literal_agg
I was testing running various Calcite optimisations; TPC/H 16 specifically was optimised in such a way that it wasn't possible for the Isthmus
SubstraitRelVisitorto handle it.The issue was that calcite had added
LITERAL_AGG(true).In Apache Calcite,
LITERAL_AGG(true)is an internal aggregate function injected during the decorrelation process (specifically when handlingANY,SOME, orINsubqueries).Here is why it is there and what it does:
1. The Problem: Handling Empty Subqueries
In your "before" plan, there is a
<> SOME(...)correlated subquery. By SQL standards, if a subquery inside a quantified comparison (SOME/ANY) returns zero rows, the entire condition evaluates toFALSE(orUNKNOWN), notNULL.2. The Solution: Flagging Matches
When Calcite transforms the correlated subquery into a join (
LogicalCorrelate/LogicalAggregate), it needs a foolproof way to know if the subquery actually produced any rows for a given correlated key ($cor0.L_PARTKEY).LITERAL_AGG(true)evaluates the literal valuetruefor every row entering the aggregate.LITERAL_AGG(true)will returnNULL.true.3. How Calcite Uses It
Look closely at the massive
LogicalFilter(condition=[OR(...)]right above the correlate in your "after" plan.Calcite uses the output of
LITERAL_AGG(true)(which maps to column$19or$20in that flattened row) to evaluate the exact short-circuiting logic of theSOMEoperator:If it's NULL: The subquery was empty$\rightarrow$ handle as false/null.$\rightarrow$ proceed to check the actual value comparisons (like
If it's TRUE: The subquery returned data
COUNTandMAX).It essentially acts as a highly optimized, null-aware boolean flag for row existence during complex subquery unnesting.
🤖 built with assistance from AI