Skip to content

Commit 90922d9

Browse files
committed
Refactor cast_with_target_field_and_options
Simplify is_valid_cast logic to compute a single decision. Return a single not_impl_err! for invalid cast cases. Construct CastExpr::new_with_target_field(...) once on the successful path, enhancing code clarity and reducing duplication.
1 parent d67b94b commit 90922d9

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

  • datafusion/physical-expr/src/expressions

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -338,34 +338,26 @@ pub(crate) fn cast_with_target_field_and_options(
338338
) -> Result<Arc<dyn PhysicalExpr>> {
339339
let expr_type = expr.data_type(input_schema)?;
340340
let cast_type = target_field.data_type().clone();
341-
if expr_type == cast_type {
342-
Ok(Arc::new(CastExpr::new_with_target_field(
343-
expr,
344-
target_field,
345-
cast_options,
346-
)))
341+
let is_valid_cast = if expr_type == cast_type {
342+
true
347343
} else if matches!((&expr_type, &cast_type), (Struct(_), Struct(_))) {
348-
if can_cast_struct_types(&expr_type, &cast_type) {
349-
// Allow struct-to-struct casts that pass name-based compatibility validation.
350-
// This validation is applied at planning time (now) to fail fast, rather than
351-
// deferring errors to execution time. The name-based casting logic will be
352-
// executed at runtime via ColumnarValue::cast_to.
353-
Ok(Arc::new(CastExpr::new_with_target_field(
354-
expr,
355-
target_field,
356-
cast_options,
357-
)))
358-
} else {
359-
not_impl_err!("Unsupported CAST from {expr_type} to {cast_type}")
360-
}
361-
} else if can_cast_types(&expr_type, &cast_type) {
344+
// Allow struct-to-struct casts that pass name-based compatibility validation.
345+
// This validation is applied at planning time (now) to fail fast, rather than
346+
// deferring errors to execution time. The name-based casting logic will be
347+
// executed at runtime via ColumnarValue::cast_to.
348+
can_cast_struct_types(&expr_type, &cast_type)
349+
} else {
350+
can_cast_types(&expr_type, &cast_type)
351+
};
352+
353+
if !is_valid_cast {
354+
not_impl_err!("Unsupported CAST from {expr_type} to {cast_type}")
355+
} else {
362356
Ok(Arc::new(CastExpr::new_with_target_field(
363357
expr,
364358
target_field,
365359
cast_options,
366360
)))
367-
} else {
368-
not_impl_err!("Unsupported CAST from {expr_type} to {cast_type}")
369361
}
370362
}
371363

0 commit comments

Comments
 (0)