Repository navigation
fix: avoid pruning timestamp-to-time casts #26452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1349,6 +1349,16 @@ fn verify_support_type_for_prune(from_type: &DataType, to_type: &DataType) -> Re | |||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| _ => to_type, | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| // Casting a timestamp to a time-of-day wraps at midnight and does not | ||||||||||||||||||||||||||||||||||
| // preserve ordering across day boundaries. Casting timestamp min/max | ||||||||||||||||||||||||||||||||||
| // statistics could therefore produce invalid bounds and prune matches. | ||||||||||||||||||||||||||||||||||
| if matches!(from_type, DataType::Timestamp(_, _)) | ||||||||||||||||||||||||||||||||||
| && matches!(to_type, DataType::Time32(_) | DataType::Time64(_)) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| return plan_err!( | ||||||||||||||||||||||||||||||||||
| "Try Cast/Cast with from type {from_type} to type {to_type} is not supported" | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| // If both types are strings or both are not strings (number, timestamp, etc) | ||||||||||||||||||||||||||||||||||
| // then we can compare them. | ||||||||||||||||||||||||||||||||||
| // PruningPredicate does not support casting of strings to numbers and such. | ||||||||||||||||||||||||||||||||||
|
|
@@ -4971,6 +4981,56 @@ mod tests { | |||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||
| fn row_group_predicate_cast_timestamp_to_time() { | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I had claude update the test to handle timestamp with tz as I wanted to be sure that this fix covered that case as well.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks — added this in 1fc4b25. The helper now runs the same |
||||||||||||||||||||||||||||||||||
| // A timezone shifts where the day boundary falls but does not remove | ||||||||||||||||||||||||||||||||||
| // it, so a timezone-aware timestamp is just as unsafe to prune on. | ||||||||||||||||||||||||||||||||||
| for timestamp_type in [ | ||||||||||||||||||||||||||||||||||
| DataType::Timestamp(TimeUnit::Second, None), | ||||||||||||||||||||||||||||||||||
| DataType::Timestamp(TimeUnit::Second, Some("+05:00".into())), | ||||||||||||||||||||||||||||||||||
| DataType::Timestamp(TimeUnit::Nanosecond, Some("UTC".into())), | ||||||||||||||||||||||||||||||||||
| ] { | ||||||||||||||||||||||||||||||||||
| assert_timestamp_to_time_cast_is_not_prunable(timestamp_type); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /// Asserts that no comparison of `CAST`/`TRY_CAST` from `timestamp_type` to | ||||||||||||||||||||||||||||||||||
| /// a time-of-day produces a pruning expression. | ||||||||||||||||||||||||||||||||||
| fn assert_timestamp_to_time_cast_is_not_prunable(timestamp_type: DataType) { | ||||||||||||||||||||||||||||||||||
| let schema = Schema::new(vec![Field::new("ts", timestamp_type, false)]); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| for (time_type, literal) in [ | ||||||||||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||||||||||
| DataType::Time32(TimeUnit::Second), | ||||||||||||||||||||||||||||||||||
| ScalarValue::Time32Second(Some(0)), | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||||||||||
| DataType::Time32(TimeUnit::Millisecond), | ||||||||||||||||||||||||||||||||||
| ScalarValue::Time32Millisecond(Some(0)), | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||||||||||
| DataType::Time64(TimeUnit::Microsecond), | ||||||||||||||||||||||||||||||||||
| ScalarValue::Time64Microsecond(Some(0)), | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||||||||||
| DataType::Time64(TimeUnit::Nanosecond), | ||||||||||||||||||||||||||||||||||
| ScalarValue::Time64Nanosecond(Some(0)), | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| ] { | ||||||||||||||||||||||||||||||||||
| for expr in [ | ||||||||||||||||||||||||||||||||||
| cast(col("ts"), time_type.clone()).eq(lit(literal.clone())), | ||||||||||||||||||||||||||||||||||
| try_cast(col("ts"), time_type).eq(lit(literal)), | ||||||||||||||||||||||||||||||||||
| ] { | ||||||||||||||||||||||||||||||||||
| let predicate_expr = test_build_predicate_expression( | ||||||||||||||||||||||||||||||||||
| &expr, | ||||||||||||||||||||||||||||||||||
| &schema, | ||||||||||||||||||||||||||||||||||
| &mut RequiredColumns::new(), | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| assert_eq!(predicate_expr.to_string(), "true"); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||
| fn row_group_predicate_cast_string_string() -> Result<()> { | ||||||||||||||||||||||||||||||||||
| let schema = Schema::new(vec![Field::new("c1", DataType::Utf8View, false)]); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rgbuilds thanks for the fix! The root cause is that pruning has its own order check that allows any cast unless exactly one side is a string, instead of asking
is_order_preserving_castinphysical-expr/src/expressions/cast.rs, which already rejects this cast. So similar casts still drop rows here, e.g.CAST(i AS BOOLEAN)andCAST(i AS BYTEA)(#26981). #26982 moves pruning onto that shared rule and will replace this arm, so this is fine to merge as the fix for #26451.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jayzhan211 for identifying the broader root cause. I’ll keep this PR scoped to #26451