Skip to content

fix: block timestamp precision narrowing unwrap#22837

Open
discord9 wants to merge 2 commits into
apache:mainfrom
discord9:fix/timestamp-precision-unwrap-cast-blocklist
Open

fix: block timestamp precision narrowing unwrap#22837
discord9 wants to merge 2 commits into
apache:mainfrom
discord9:fix/timestamp-precision-unwrap-cast-blocklist

Conversation

@discord9

@discord9 discord9 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Part of GreptimeTeam/greptimedb#8214.
Follow-up / smaller alternative to #21908.

Rationale for this change

unwrap_cast can currently rewrite predicates like:

CAST(ts_ns AS timestamp(3)) = timestamp(3) '2024-01-01 00:00:00.001'

into an equality against the original nanosecond column. That is not equivalent: the original predicate matches every nanosecond timestamp within the same millisecond, while the rewritten predicate only matches the exact millisecond boundary.

What changes are included in this PR?

This is intentionally a small blocklist-only fix:

  • add a shared is_timestamp_precision_narrowing_cast helper
  • block comparison cast unwrap when a timestamp cast narrows precision
  • apply the same guard to logical and physical unwrap-cast simplifiers
  • keep timestamp precision widening unwraps enabled

Are these changes tested?

Added targeted logical, physical, and helper tests.

Ran:

cargo test -p datafusion-optimizer unwrap_cast
cargo test -p datafusion-physical-expr unwrap_cast
cargo test -p datafusion-expr-common test_timestamp_precision_narrowing_cast

Are there any user-facing changes?

Plans will keep timestamp precision-narrowing casts in comparison predicates instead of unwrapping them incorrectly.

Signed-off-by: discord9 <discord9@163.com>
@github-actions github-actions Bot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates optimizer Optimizer rules labels Jun 9, 2026
Signed-off-by: discord9 <discord9@163.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions optimizer Optimizer rules physical-expr Changes to the physical-expr crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant