apache / apache/doris

[Bug](nereids) Ordered constant folding throws ClassCastException for TIME literals

Open
#66,829 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
15.9k
Forks
3.9k
Avg merge
2d 23h
Merged PRs (30d)
520

Description

### Search before asking

- [x] I searched the issues and found no similar issue.

### Version

master

### What's Wrong?

Nereids FE constant folding unconditionally casts operands of ordered comparisons (`>`, `>=`, `<`, and `<=`) to `ComparableLiteral` after their children are folded.

Functions such as `curtime()` fold to `TimeV2Literal`, which extends `Literal` but does not implement `ComparableLiteral`. As a result, expressions such as `curtime() >= '20:00:00'` can fail during planning with `ClassCastException`.

### What You Expected?

Ordered comparisons whose folded operands do not implement `ComparableLiteral` should remain as predicates for later evaluation instead of crashing during FE constant folding.

### How to Reproduce?

Run a query containing an ordered comparison over `curtime()`, for example:

```sql
SELECT curtime() >= '20:00:00';
```

### Proposed Fix

Guard ordered comparison folding by checking that both operands implement `ComparableLiteral`. Fold when both are comparable; otherwise preserve the rewritten comparison expression.

Contributor guide

Open the contributing guide

Research direction

Start with the Nereids FE constant-folding logic for ordered comparisons and reproduce the failure with SELECT curtime() >= '20:00:00'. Verify that comparisons fold when both operands implement ComparableLiteral and remain predicates when a folded operand is a TimeV2Literal, without the ClassCastException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.