Repository navigation
[CALCITE-7887] SubQueryRemoveRule fails for a sub-query inside a window function - #5359
Merged
Merged
Conversation
|
mihaibudiu
approved these changes
Oct 25, 2026
|
|
||
| # [CALCITE-7887] SubQueryRemoveRule fails for a sub-query inside a window function | ||
| # IN sub-query inside the argument of a windowed aggregate. | ||
| # The result of this query was validated using Postgres. |
Contributor
There was a problem hiding this comment.
only this one or all of them?
Contributor
Author
There was a problem hiding this comment.
all of them
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.



CALCITE-7887
Root cause
SubQueryRemoveRule.matchProject locates the sub-query to rewrite with RexUtil.SubQueryFinder, which is a deep RexVisitorImpl and therefore descends into a RexOver's operands, partition keys and order keys. It then calls LogicVisitor.find to compute the Logic under which that sub-query is evaluated. However, LogicVisitor.visitOver only called end(over, arg) and never visited the operands or the window of the RexOver. As a result, a sub-query nested anywhere inside a window function was found by SubQueryFinder but was invisible to LogicVisitor, so the collected logic set was empty and LogicVisitor.find threw IllegalArgumentException: not found.
Fix
LogicVisitor.visitOver now visits the aggregate operands, the partition keys, the order keys and the bounds of the window. They are visited with Logic.TRUE_FALSE_UNKNOWN, because a window function consumes the value of these expressions, not just whether they are true. For example, COUNT(x) ignores NULLs, so UNKNOWN must be distinguished from FALSE; PARTITION BY / ORDER BY treat NULL and FALSE as different keys. Using a weaker logic such as UNKNOWN_AS_FALSE could therefore produce wrong results.
Only Project is affected, since window functions can only appear in the SELECT list (QUALIFY is converted to a Filter on top of a Project).