Skip to content

Apache DataFusion SQL Query Engine

fix: validate cast target type in interval check_support (#25562)

Jeremy Schoemaker37c954c639da
diff --git a/datafusion/physical-expr/src/intervals/utils.rs b/datafusion/physical-expr/src/intervals/utils.rs
index 1090660a6b..e85e7a5d26 100644
--- a/datafusion/physical-expr/src/intervals/utils.rs
+++ b/datafusion/physical-expr/src/intervals/utils.rs
@@ -53,7 +53,11 @@ pub fn check_support(expr: &Arc<dyn PhysicalExpr>, schema: &SchemaRef) -> bool {
false
}
} else if let Some(cast) = expr.downcast_ref::<CastExpr>() {
- check_support(cast.expr(), schema)
+ // The cast target type must itself be one we can reason about. Casting
+ // interval endpoints to a type with a different ordering (e.g. integers
+ // to strings) does not preserve the interval, so such expressions are
+ // left unanalyzed rather than producing bogus bounds.
+ is_datatype_supported(cast.cast_type()) && check_support(cast.expr(), schema)
} else if let Some(negative) = expr.downcast_ref::<NegativeExpr>() {
check_support(negative.arg(), schema)
} else {
diff --git a/datafusion/sqllogictest/test_files/cast.slt b/datafusion/sqllogictest/test_files/cast.slt
index 6d62461235..414ee2f636 100644
--- a/datafusion/sqllogictest/test_files/cast.slt
+++ b/datafusion/sqllogictest/test_files/cast.slt
@@ -332,3 +332,39 @@ NULL NULL
statement ok
DROP TABLE narrow_column;
+
+# Casting integer ids to strings in a view and back to integers in the filter
+# must not use string-ordered interval bounds derived from numeric statistics.
+
+statement ok
+COPY (SELECT column1 AS id FROM (VALUES (2), (10)))
+TO 'test_files/scratch/cast/cast_interval_ids.parquet'
+STORED AS PARQUET;
+
+statement ok
+CREATE EXTERNAL TABLE cast_interval_ids
+STORED AS PARQUET
+LOCATION 'test_files/scratch/cast/cast_interval_ids.parquet';
+
+statement ok
+CREATE VIEW cast_interval_text_ids AS
+SELECT CAST(id AS VARCHAR) AS id FROM cast_interval_ids;
+
+query T
+SELECT id FROM cast_interval_text_ids WHERE CAST(id AS INT) > 1 ORDER BY id;
+----
+10
+2
+
+# Selective filtering through the cast must apply the numeric predicate after
+# casting, not string-ordered interval bounds derived from the statistics.
+query T
+SELECT id FROM cast_interval_text_ids WHERE CAST(id AS INT) > 5;
+----
+10
+
+statement ok
+DROP VIEW cast_interval_text_ids;
+
+statement ok
+DROP TABLE cast_interval_ids;