Skip to content

Commit 4f4d73f

Browse files
authored
[branch-55] fix: restrict reverse cast constraint propagation to safe conversions (#25531) (#26870)
- Part of #26868 This PR: - Backports #25531 to the `branch-55` line Adjusted two `Int32 ↔ Date32` test expectations to match the existing safe-cast allowlist on `branch-55`.
1 parent 81176c5 commit 4f4d73f

2 files changed

Lines changed: 178 additions & 4 deletions

File tree

  • datafusion

‎datafusion/physical-expr/src/expressions/cast.rs‎

Lines changed: 130 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,13 @@ impl PhysicalExpr for CastExpr {
363363
interval: &Interval,
364364
children: &[&Interval],
365365
) -> Result<Option<Vec<Interval>>> {
366-
let child_interval = children[0];
367-
// Get child's datatype:
368-
let cast_type = child_interval.data_type();
366+
let source_type = children[0].data_type();
367+
let target_type = self.cast_type();
368+
if !can_propagate_cast_constraints(&source_type, target_type) {
369+
return Ok(Some(vec![]));
370+
}
369371
Ok(Some(vec![
370-
interval.cast_to(&cast_type, &DEFAULT_SAFE_CAST_OPTIONS)?,
372+
interval.cast_to(&source_type, &DEFAULT_SAFE_CAST_OPTIONS)?,
371373
]))
372374
}
373375

@@ -404,6 +406,19 @@ impl PhysicalExpr for CastExpr {
404406
}
405407
}
406408

409+
/// Whether output bounds can be cast back to `source` without excluding valid inputs.
410+
///
411+
/// Used for reverse constraint propagation through a cast from `source` to `target`.
412+
/// Many-to-one casts, such as Float64 to Int32, cannot generally be inverted this way:
413+
/// an output of 0 does not imply an input of 0.0.
414+
/// Returns false for unrecognized conversions so the input range remains unchanged.
415+
fn can_propagate_cast_constraints(source: &DataType, target: &DataType) -> bool {
416+
CastExpr::check_bigger_cast(target, source)
417+
|| (source.is_integer() && target.is_integer())
418+
// NaN bounds are unbounded; finite Float32 values widen exactly.
419+
|| (*source == Float32 && *target == Float64)
420+
}
421+
407422
#[cfg(feature = "proto")]
408423
impl CastExpr {
409424
/// Reconstruct a [`CastExpr`] from its protobuf representation.
@@ -572,10 +587,121 @@ mod tests {
572587
as_boolean_array, as_int64_array, as_string_array, as_struct_array,
573588
as_uint8_array,
574589
};
590+
use datafusion_common::rounding::{next_down, next_up};
575591
use datafusion_physical_expr_common::physical_expr::fmt_sql;
576592
use insta::assert_snapshot;
577593
use std::collections::HashMap;
578594

595+
#[test]
596+
fn test_cast_constraint_propagation() -> Result<()> {
597+
for (source, target, propagates) in [
598+
(Utf8, Int32, false),
599+
(Utf8View, Int32, false),
600+
(Timestamp(TimeUnit::Nanosecond, None), Date32, false),
601+
// branch-55 does not recognize integer/date casts in check_bigger_cast.
602+
(Int32, Date32, false),
603+
(Date32, Int32, false),
604+
(Utf8, LargeUtf8, true),
605+
(Utf8, Utf8, true),
606+
(Float64, Int32, false),
607+
(Int64, Float32, false),
608+
(Float64, Float32, false),
609+
(Decimal128(4, 1), Decimal128(4, 0), false),
610+
(Decimal128(4, 1), Int32, false),
611+
(Float64, Decimal128(4, 1), false),
612+
(Int8, Int64, true),
613+
(Int64, Int8, true),
614+
(Int32, UInt32, true),
615+
(UInt32, Int32, true),
616+
(Int32, Float64, true),
617+
(Float32, Float64, true),
618+
(Decimal128(4, 1), Decimal128(4, 1), true),
619+
] {
620+
let schema = Schema::new(vec![Field::new("x", source.clone(), true)]);
621+
let expr = CastExpr::new(col("x", &schema)?, target.clone(), None);
622+
let input = Interval::make_unbounded(&source)?;
623+
let value = ScalarValue::Int32(Some(0)).cast_to(&target)?;
624+
let output = Interval::from(&value);
625+
let expected = if propagates {
626+
vec![Interval::from(&value.cast_to(&source)?)]
627+
} else {
628+
vec![]
629+
};
630+
assert_eq!(
631+
expr.propagate_constraints(&output, &[&input])?,
632+
Some(expected),
633+
"{source} -> {target}"
634+
);
635+
}
636+
Ok(())
637+
}
638+
639+
#[test]
640+
fn test_float_widening_constraint_boundaries() -> Result<()> {
641+
let mut values = vec![
642+
f32::NEG_INFINITY,
643+
-f32::MAX,
644+
-1.0,
645+
-f32::MIN_POSITIVE,
646+
-f32::from_bits(1),
647+
-0.0,
648+
0.0,
649+
f32::from_bits(1),
650+
f32::MIN_POSITIVE,
651+
1.0,
652+
f32::MAX,
653+
f32::INFINITY,
654+
];
655+
// Include both signs of signaling and quiet NaNs with distinct payloads.
656+
values.extend(
657+
[0x7f800001, 0x7f800002, 0x7fc00001, 0xff800001, 0xffc00001]
658+
.map(f32::from_bits),
659+
);
660+
values.extend([next_down(1.0f32), next_up(1.0f32)]);
661+
let schema = Arc::new(Schema::new(vec![Field::new("x", Float32, false)]));
662+
let batch = RecordBatch::try_new(
663+
Arc::clone(&schema),
664+
vec![Arc::new(Float32Array::from(values.clone()))],
665+
)?;
666+
let expr = CastExpr::new(col("x", &schema)?, Float64, None);
667+
let array = expr.evaluate(&batch)?.into_array(values.len())?;
668+
let widened = array.as_any().downcast_ref::<Float64Array>().unwrap();
669+
let mut bounds = vec![-f64::MAX, f64::MAX];
670+
for value in widened.values() {
671+
bounds.extend([next_down(*value), *value, next_up(*value)]);
672+
}
673+
// Midpoints exercise rounding back to Float32 in both directions.
674+
bounds.extend([
675+
f64::from(f32::from_bits(1)) / 2.0,
676+
-f64::from(f32::from_bits(1)) / 2.0,
677+
f64::midpoint(1.0, f64::from(next_up(1.0f32))),
678+
]);
679+
bounds.sort_by(f64::total_cmp);
680+
bounds.dedup_by(|a, b| a.to_bits() == b.to_bits());
681+
let input = Interval::make_unbounded(&Float32)?;
682+
for (i, lower) in bounds.iter().enumerate() {
683+
for upper in &bounds[i..] {
684+
let output = Interval::make(Some(*lower), Some(*upper))?;
685+
let propagated = expr.propagate_constraints(&output, &[&input])?.unwrap();
686+
assert_eq!(propagated.len(), 1);
687+
for (index, value) in values.iter().enumerate() {
688+
if output.contains_value(ScalarValue::Float64(Some(
689+
widened.value(index),
690+
)))? {
691+
assert!(
692+
propagated[0]
693+
.contains_value(ScalarValue::Float32(Some(*value)))?,
694+
"input bits={:08x}, output={output}, propagated={:?}",
695+
value.to_bits(),
696+
propagated[0]
697+
);
698+
}
699+
}
700+
}
701+
}
702+
Ok(())
703+
}
704+
579705
fn make_struct_array(fields: Fields, arrays: Vec<ArrayRef>) -> StructArray {
580706
StructArray::new(fields, arrays, None)
581707
}

‎datafusion/sqllogictest/test_files/cast.slt‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,54 @@ statement ok
9191
drop table t0;
9292

9393

94+
# Regression for https://github.com/apache/datafusion/issues/25523.
95+
statement ok
96+
SET datafusion.execution.target_partitions = 1;
97+
98+
statement ok
99+
CREATE TABLE lossy_cast (id INT, x DOUBLE, n BIGINT)
100+
AS VALUES (1, -0.5, 16777216), (2, 0.5, 16777217), (3, NULL, NULL);
101+
102+
# A singleton cast result does not imply a singleton input.
103+
query I
104+
SELECT id FROM lossy_cast WHERE CAST(x AS INT) = 0 ORDER BY x DESC;
105+
----
106+
2
107+
1
108+
109+
query I
110+
SELECT id FROM lossy_cast WHERE CAST(n AS REAL) = 16777216::REAL ORDER BY n DESC;
111+
----
112+
2
113+
1
114+
115+
statement ok
116+
DROP TABLE lossy_cast;
117+
118+
statement ok
119+
CREATE TABLE lossy_non_numeric (id INT, t TIMESTAMP, s VARCHAR)
120+
AS VALUES (1, TIMESTAMP '2026-01-01 01:00:00', '01'),
121+
(2, TIMESTAMP '2026-01-01 02:00:00', '1');
122+
123+
query I
124+
SELECT id FROM lossy_non_numeric
125+
WHERE CAST(t AS DATE) = DATE '2026-01-01' ORDER BY t DESC;
126+
----
127+
2
128+
1
129+
130+
query I
131+
SELECT id FROM lossy_non_numeric WHERE CAST(s AS INT) = 1 ORDER BY s DESC;
132+
----
133+
2
134+
1
135+
136+
statement ok
137+
DROP TABLE lossy_non_numeric;
138+
139+
statement ok
140+
SET datafusion.execution.target_partitions = 4;
141+
94142
# ensure that automatically casting with "datafusion.optimizer.expand_views_at_output" does not
95143
# change the column name
96144

0 commit comments

Comments
 (0)