Skip to content

Commit d249ce3

Browse files
committed
fix: prevent const-folding of empty struct cast literals
Prevents dimension mismatch when simplifier evaluates 0-row struct literals against its 1-row input batch, fixing test failures.
1 parent 528baeb commit d249ce3

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! Expression simplification API
1919
2020
use arrow::{
21-
array::{AsArray, new_null_array},
21+
array::{Array, AsArray, new_null_array},
2222
datatypes::{DataType, Field, Schema},
2323
record_batch::RecordBatch,
2424
};
@@ -652,6 +652,17 @@ impl ConstEvaluator {
652652
if source_fields.len() != target_fields.len() {
653653
return false;
654654
}
655+
656+
// Don't const-fold struct casts with empty (0-row) literals
657+
// The simplifier uses a 1-row input batch, which causes dimension mismatches
658+
// when evaluating 0-row struct literals
659+
if let Expr::Literal(ScalarValue::Struct(struct_array), _) =
660+
expr.as_ref()
661+
{
662+
if struct_array.len() == 0 {
663+
return false;
664+
}
665+
}
655666
}
656667
true
657668
}
@@ -5069,7 +5080,7 @@ mod tests {
50695080
]);
50705081

50715082
// Create an empty struct with the source fields
5072-
let arrays: Vec<Arc<dyn arrow::array::Array>> = vec![
5083+
let arrays: Vec<Arc<dyn Array>> = vec![
50735084
Arc::new(arrow::array::Int32Array::new(vec![].into(), None)),
50745085
Arc::new(arrow::array::Int32Array::new(vec![].into(), None)),
50755086
];
@@ -5109,7 +5120,7 @@ mod tests {
51095120
]);
51105121

51115122
// Create an empty struct with the source fields
5112-
let arrays: Vec<Arc<dyn arrow::array::Array>> = vec![
5123+
let arrays: Vec<Arc<dyn Array>> = vec![
51135124
Arc::new(arrow::array::Int32Array::new(vec![].into(), None)),
51145125
Arc::new(arrow::array::Int32Array::new(vec![].into(), None)),
51155126
];
@@ -5148,7 +5159,7 @@ mod tests {
51485159
]);
51495160

51505161
// Create an empty struct with the source fields
5151-
let arrays: Vec<Arc<dyn arrow::array::Array>> = vec![
5162+
let arrays: Vec<Arc<dyn Array>> = vec![
51525163
Arc::new(arrow::array::Int32Array::new(vec![].into(), None)),
51535164
Arc::new(arrow::array::Int32Array::new(vec![].into(), None)),
51545165
];

0 commit comments

Comments
 (0)