[fix](constant folding) avoid folding non-comparable ordered literals - #66830
Open
Baymine wants to merge 1 commit into
Open
[fix](constant folding) avoid folding non-comparable ordered literals#66830Baymine wants to merge 1 commit into
Baymine wants to merge 1 commit into
Conversation
### What problem does this PR solve? Issue Number: close apache#66829 Problem Summary: The FE FoldConstantRuleOnFE visitors for the ordered comparisons `>`, `>=`, `<`, `<=` unconditionally cast both operands to `ComparableLiteral` before folding. When a child folds to a literal that extends `Literal` but does not implement `ComparableLiteral` (for example `curtime()` folding to `TimeV2Literal`), the cast throws a `java.lang.ClassCastException` during planning, crashing queries such as `curtime() >= '20:00:00'`. Root cause: `visitGreaterThan`, `visitGreaterThanEqual`, `visitLessThan`, and `visitLessThanEqual` perform the cast without first checking that both children are `ComparableLiteral`, unlike the already-correct `visitEqualTo` and `visitNullSafeEqual` visitors which guard with `instanceof ComparableLiteral`. Fix: guard each of the four ordered-comparison visitors with the same `instanceof ComparableLiteral` check. When both operands are comparable the fold proceeds as before; otherwise the original comparison expression is returned unchanged so the predicate is preserved for later evaluation instead of crashing at plan time. ### Release note Fix a ClassCastException in the Nereids FE constant-folding rule when an ordered comparison (>, >=, <, <=) has an operand that folds to a non-comparable literal such as a TIME value; the comparison is now kept as a predicate instead of crashing planning. ### Check List (For Author) - Test: Unit Test - Added FoldConstantTest#testComparisonFoldWithNonComparableLiteral covering all four ordered operators (>, >=, <, <=) with TimeV2Literal, asserting the comparison stays un-folded and both children remain TimeV2Literal; verified red before the fix (ClassCastException) and green after. Full FoldConstantTest class passes (25 tests). FE checkstyle and FE build pass. - Behavior changed: Yes. Previously an ordered comparison over a non-comparable folded literal threw a ClassCastException during planning; it is now preserved as a predicate and evaluated normally. - Does this need documentation: No
Baymine
requested review from
924060929,
englefly,
morrySnow and
starocean999
as code owners
August 17, 2026 04:34
Contributor
|
Thank you for your contribution to Apache Doris. Please clearly describe your PR:
|
Contributor
Author
|
run buildall |
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.
What problem does this PR solve?
Issue Number: close #66829
Problem Summary:
The FE FoldConstantRuleOnFE visitors for the ordered comparisons
>,>=,<,<=unconditionally cast both operands toComparableLiteralbefore folding. When a child folds to a literal thatextends
Literalbut does not implementComparableLiteral(forexample
curtime()folding toTimeV2Literal), the cast throws ajava.lang.ClassCastExceptionduring planning, crashing queries such ascurtime() >= '20:00:00'.Root cause:
visitGreaterThan,visitGreaterThanEqual,visitLessThan,and
visitLessThanEqualperform the cast without first checking thatboth children are
ComparableLiteral, unlike the already-correctvisitEqualToandvisitNullSafeEqualvisitors which guard withinstanceof ComparableLiteral.Fix: guard each of the four ordered-comparison visitors with the same
instanceof ComparableLiteralcheck. When both operands are comparablethe fold proceeds as before; otherwise the original comparison
expression is returned unchanged so the predicate is preserved for later
evaluation instead of crashing at plan time.
Release note
Fix a ClassCastException in the Nereids FE constant-folding rule when an
ordered comparison (>, >=, <, <=) has an operand that folds to a
non-comparable literal such as a TIME value; the comparison is now kept
as a predicate instead of crashing planning.
Check List (For Author)
covering all four ordered operators (>, >=, <, <=) with
TimeV2Literal, asserting the comparison stays un-folded and both
children remain TimeV2Literal; verified red before the fix
(ClassCastException) and green after. Full FoldConstantTest class
passes (25 tests). FE checkstyle and FE build pass.
non-comparable folded literal threw a ClassCastException during
planning; it is now preserved as a predicate and evaluated normally.