Skip to content

Commit 95a7e7e

Browse files
committed
Refactor planner tests and document cast behavior
Use as_planner_cast(...) helper in planner tests to eliminate repeated downcasting. Update cast_with_target_field documentation to clarify that default synthesized fields are elided while explicit field semantics are preserved.
1 parent 7b0c2d1 commit 95a7e7e

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ pub fn cast_with_options(
327327
/// Return a PhysicalExpression representing `expr` casted to `target_field`,
328328
/// preserving any explicit field semantics such as name, nullability, and
329329
/// metadata.
330+
///
331+
/// If the input expression already has the same data type, this helper still
332+
/// preserves an explicit `target_field` by constructing a field-aware
333+
/// [`CastExpr`]. Only the default synthesized field created by the legacy
334+
/// type-only API is elided back to the original child expression.
330335
pub fn cast_with_target_field(
331336
expr: Arc<dyn PhysicalExpr>,
332337
input_schema: &Schema,

datafusion/physical-expr/src/planner.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,13 @@ mod tests {
448448
create_physical_expr(expr, &df_schema, &ExecutionProps::new())
449449
}
450450

451+
fn as_planner_cast(physical: &Arc<dyn PhysicalExpr>) -> &expressions::CastExpr {
452+
physical
453+
.as_any()
454+
.downcast_ref::<expressions::CastExpr>()
455+
.expect("planner should lower logical CAST to CastExpr")
456+
}
457+
451458
#[test]
452459
fn test_create_physical_expr_scalar_input_output() -> Result<()> {
453460
let expr = col("letter").eq(lit("A"));
@@ -487,10 +494,7 @@ mod tests {
487494
));
488495

489496
let physical = lower_cast_expr(&cast_expr, &schema)?;
490-
let cast = physical
491-
.as_any()
492-
.downcast_ref::<expressions::CastExpr>()
493-
.expect("planner should lower logical CAST to CastExpr");
497+
let cast = as_planner_cast(&physical);
494498

495499
assert_eq!(cast.target_field(), &target_field);
496500
assert_eq!(physical.return_field(&schema)?, target_field);
@@ -505,10 +509,7 @@ mod tests {
505509
let cast_expr = Expr::Cast(Cast::new(Box::new(col("a")), DataType::Int64));
506510

507511
let physical = lower_cast_expr(&cast_expr, &schema)?;
508-
let cast = physical
509-
.as_any()
510-
.downcast_ref::<expressions::CastExpr>()
511-
.expect("planner should lower ordinary CAST to CastExpr");
512+
let cast = as_planner_cast(&physical);
512513
let returned_field = physical.return_field(&schema)?;
513514

514515
assert_eq!(cast.cast_type(), &DataType::Int64);
@@ -533,10 +534,7 @@ mod tests {
533534
));
534535

535536
let physical = lower_cast_expr(&cast_expr, &schema)?;
536-
let cast = physical
537-
.as_any()
538-
.downcast_ref::<expressions::CastExpr>()
539-
.expect("same-type casts should not be elided when the target field carries semantics");
537+
let cast = as_planner_cast(&physical);
540538

541539
assert_eq!(cast.target_field(), &target_field);
542540
assert_eq!(physical.return_field(&schema)?, target_field);

0 commit comments

Comments
 (0)