matanper opened a new issue, #70635:
URL: https://github.com/apache/doris/issues/70635

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   ### Version
   
   4.1.4. master (`13cff7ff484`) has the same code.
   
   ### What's Wrong?
   
   Comparing two constant TIME values with `<`, `<=`, `>` or `>=` fails 
planning with an internal error:
   
   ```
   errCode = 2, detailMessage = class 
org.apache.doris.nereids.trees.expressions.literal.TimeV2Literal
   cannot be cast to class 
org.apache.doris.nereids.trees.expressions.literal.ComparableLiteral
   ```
   
   This also hits comparisons that only become constant during planning, such 
as `CAST('10:15:30' AS TIME) < CURRENT_TIME()`, because `CURRENT_TIME()` is 
folded to a literal first. Equality between the same values works.
   
   ### Why
   
   `FoldConstantRuleOnFE` 
(`fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java`):
   
   - `visitEqualTo` (`:298-303`) folds through `ComparableLiteral` only when 
both sides implement it, and otherwise falls back to `equals()`.
   - `visitGreaterThan`, `visitGreaterThanEqual`, `visitLessThan` and 
`visitLessThanEqual` (`:313-340`) cast both operands to `ComparableLiteral` 
unconditionally:
   
   ```java
   return BooleanLiteral.of(((ComparableLiteral) lessThan.left())
           .compareTo((ComparableLiteral) lessThan.right()) < 0);
   ```
   
   `TimeV2Literal` extends `Literal` and does not implement 
`ComparableLiteral`, so a range comparison between two TIME literals throws 
`ClassCastException` instead of folding.
   
   ### What You Expected?
   
   `CAST('10:15:30' AS TIME) < CAST('11:00:00' AS TIME)` folds to `true`, and 
`CAST('10:15:30' AS TIME) < CURRENT_TIME()` returns a boolean, like the same 
comparison on column values does.
   
   ### How to Reproduce?
   
   ```sql
   SELECT CAST('10:15:30' AS TIME) < CAST('11:00:00' AS TIME);
   -- ERROR: TimeV2Literal cannot be cast to class ... ComparableLiteral
   
   SELECT CAST('10:15:30' AS TIME) < CURRENT_TIME();
   -- same error
   
   SELECT CAST('10:15:30' AS TIME) = CAST('10:15:30' AS TIME);
   -- returns 1 (equality takes the guarded path)
   ```
   
   Comparing a TIME column with a TIME literal (for example `CAST(ts AS TIME) < 
CAST('10:00:30' AS TIME)` on a table) is not affected, because it is not folded 
on the FE.
   
   ### Anything Else?
   
   Proposed fix: make `TimeV2Literal` implement `ComparableLiteral`, comparing 
by its value (microseconds of the day, the value the BE compares). 
Alternatively, give the four range visitors the same guard as `visitEqualTo`: 
leave the comparison unfolded, for BE evaluation, when either operand is not a 
`ComparableLiteral`. The guard is worth adding either way, so any other literal 
type that does not implement `ComparableLiteral` cannot fail planning the same 
way.
   
   We can open a PR.
   
   ### Are you willing to submit PR?
   
   - [x] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to