Skip to content

Commit 97c95d9

Browse files
Fix issue apache#25095: Reject unwrap_cast for timestamp literals when timezone matching is lossy
This prevents the optimizer from dropping timezone shifts when casting between a timezone-aware and timezone-naive timestamp in comparisons.
1 parent 696eaf5 commit 97c95d9

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎datafusion/expr-common/src/casts.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ fn is_lossy_temporal_cast(from_type: &DataType, to_type: &DataType) -> bool {
120120
if is_date_type(from_type) && is_date_type(to_type) {
121121
return false;
122122
}
123+
if let (DataType::Timestamp(_, from_tz), DataType::Timestamp(_, to_tz)) = (from_type, to_type) {
124+
if from_tz.is_some() != to_tz.is_some() {
125+
return true;
126+
}
127+
}
123128
(is_date_type(from_type) && to_type.is_temporal())
124129
|| (is_date_type(to_type) && from_type.is_temporal())
125130
}
@@ -998,6 +1003,23 @@ mod tests {
9981003
assert!(is_lossy_temporal_cast(&ts, &DataType::Date32));
9991004
}
10001005

1006+
#[test]
1007+
fn test_is_lossy_temporal_cast_timestamp_tz() {
1008+
let ts_naive = DataType::Timestamp(TimeUnit::Millisecond, None);
1009+
let ts_utc = DataType::Timestamp(TimeUnit::Millisecond, Some("UTC".into()));
1010+
let ts_sgt = DataType::Timestamp(TimeUnit::Millisecond, Some("Asia/Singapore".into()));
1011+
1012+
// Naive <-> Tz-aware is lossy because it ignores session timezone
1013+
assert!(is_lossy_temporal_cast(&ts_naive, &ts_utc));
1014+
assert!(is_lossy_temporal_cast(&ts_utc, &ts_naive));
1015+
assert!(is_lossy_temporal_cast(&ts_naive, &ts_sgt));
1016+
assert!(is_lossy_temporal_cast(&ts_sgt, &ts_naive));
1017+
1018+
// Tz-aware <-> Tz-aware is not lossy (both are UTC under the hood)
1019+
assert!(!is_lossy_temporal_cast(&ts_utc, &ts_sgt));
1020+
assert!(!is_lossy_temporal_cast(&ts_sgt, &ts_utc));
1021+
}
1022+
10011023
#[test]
10021024
fn test_timestamp_precision_narrowing_cast() {
10031025
let ts_ns = DataType::Timestamp(TimeUnit::Nanosecond, None);

0 commit comments

Comments
 (0)