Skip to content

fix: use cast preimages for cast predicate rewrites#10

Closed
discord9 wants to merge 9 commits into
GreptimeTeam:mainfrom
discord9:discord9/cast-preimage-forkfix
Closed

fix: use cast preimages for cast predicate rewrites#10
discord9 wants to merge 9 commits into
GreptimeTeam:mainfrom
discord9:discord9/cast-preimage-forkfix

Conversation

@discord9

@discord9 discord9 commented Jul 2, 2026

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

GreptimeDB can produce incorrect timestamp predicate results when a timestamp column is compared through a lower-precision cast, for example matching 2026-06-02 03:50:00.195 for a <= '2026-06-02 03:50:00' predicate.

The existing cast predicate rewrite can unwrap casts that are not one-to-one, which changes predicate semantics for timestamp precision narrowing and other lossy casts. This ports the upstream Apache DataFusion cast-preimage fix so predicate rewrites are only applied when the preimage is known to preserve semantics.

What changes are included in this PR?

  • Introduce shared cast predicate preimage logic in datafusion-expr-common.
  • Use exact preimages for safe cast predicate rewrites.
  • Use range preimages for timestamp precision narrowing instead of unsafe exact cast unwrapping.
  • Reject unsafe exact rewrites for numeric narrowing, lossy decimal casts, timestamp narrowing, and similar many-to-one casts.
  • Share the safer logic between logical optimizer and physical expression simplifier.
  • Add regression coverage for timestamp, decimal, distinctness, and edge-case cast predicate behavior.

Are these changes tested?

This is a direct port of the upstream Apache DataFusion fix from apache#22906, whose CI passed upstream.

Local tests were intentionally skipped for this urgent fork PR so GreptimeTeam CI can validate the branch as soon as possible.

Are there any user-facing changes?

Yes. Query results for predicates involving lossy casts become more correct and conservative: unsafe cast unwrapping is avoided, and timestamp precision narrowing is rewritten using ranges that preserve predicate semantics.

discord9 added 8 commits July 2, 2026 16:49
Signed-off-by: discord9 <discord9@163.com>
Signed-off-by: discord9 <discord9@163.com>
Signed-off-by: discord9 <discord9@163.com>
Signed-off-by: discord9 <discord9@163.com>
Signed-off-by: discord9 <discord9@163.com>
@discord9

discord9 commented Jul 2, 2026

Copy link
Copy Markdown
Author

Closing this PR because it targeted the wrong base branch (). GreptimeDB currently depends on the branch/rev, so the fix has been reopened against the correct base in #11.

@discord9 discord9 closed this Jul 2, 2026
@discord9

discord9 commented Jul 2, 2026

Copy link
Copy Markdown
Author

Closing because this PR targeted the wrong base branch: main. GreptimeDB currently depends on GreptimeTeam/datafusion branch greptimedb-53.1.0-function-signature-exec-error at rev e8a127c, so the fix has been reopened against the correct base in #11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant