Skip to content

Commit 986a712

Browse files
authored
refactor: Port CaseExpr proto serialization hooks (apache#22838)
## Which issue does this PR close? - Closes apache#22421. ## Rationale for this change CaseExpr should own its protobuf serialization and deserialization through the PhysicalExpr try_to_proto / try_from_proto hooks, matching the migration pattern used by other physical expressions and avoiding the central downcast chain. ## What changes are included in this PR? - Adds CaseExpr::try_to_proto in the PhysicalExpr impl. - Adds CaseExpr::try_from_proto as an inherent proto constructor. - Routes physical expression decoding for CaseExpr through the new hook. - Removes the old CaseExpr serialization arm and its helper from datafusion-proto. - Adds direct hook tests covering encode, decode, wrong node variant, missing when/then children, and child encode/decode error propagation. ## Are these changes tested? Yes: - cargo fmt --all - cargo test -p datafusion-physical-expr --features proto case::tests::try_ - cargo test -p datafusion-proto --test proto_integration - cargo clippy -p datafusion-physical-expr --features proto --tests -- -D warnings - cargo clippy -p datafusion-proto --test proto_integration -- -D warnings ## Are there any user-facing changes? No. This is an internal proto serialization refactor with the same wire format. --------- Co-authored-by: chakkk309 <[email protected]>
1 parent 84dcc0b commit 986a712

3 files changed

Lines changed: 257 additions & 92 deletions

File tree

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

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,86 @@ impl PhysicalExpr for CaseExpr {
14101410
}
14111411
write!(f, "END")
14121412
}
1413+
1414+
#[cfg(feature = "proto")]
1415+
fn try_to_proto(
1416+
&self,
1417+
ctx: &datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx<'_>,
1418+
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalExprNode>> {
1419+
use datafusion_proto_models::protobuf;
1420+
1421+
Ok(Some(protobuf::PhysicalExprNode {
1422+
expr_id: None,
1423+
expr_type: Some(protobuf::physical_expr_node::ExprType::Case(Box::new(
1424+
protobuf::PhysicalCaseNode {
1425+
expr: self
1426+
.expr()
1427+
.map(|expr| ctx.encode_child(expr).map(Box::new))
1428+
.transpose()?,
1429+
when_then_expr: self
1430+
.when_then_expr()
1431+
.iter()
1432+
.map(|(when_expr, then_expr)| {
1433+
Ok(protobuf::PhysicalWhenThen {
1434+
when_expr: Some(ctx.encode_child(when_expr)?),
1435+
then_expr: Some(ctx.encode_child(then_expr)?),
1436+
})
1437+
})
1438+
.collect::<Result<Vec<_>>>()?,
1439+
else_expr: self
1440+
.else_expr()
1441+
.map(|expr| ctx.encode_child(expr).map(Box::new))
1442+
.transpose()?,
1443+
},
1444+
))),
1445+
}))
1446+
}
1447+
}
1448+
1449+
#[cfg(feature = "proto")]
1450+
impl CaseExpr {
1451+
/// Reconstruct a [`CaseExpr`] from its protobuf representation.
1452+
pub fn try_from_proto(
1453+
node: &datafusion_proto_models::protobuf::PhysicalExprNode,
1454+
ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
1455+
) -> Result<Arc<dyn PhysicalExpr>> {
1456+
use datafusion_physical_expr_common::expect_expr_variant;
1457+
use datafusion_proto_models::protobuf;
1458+
1459+
let case = expect_expr_variant!(
1460+
node,
1461+
protobuf::physical_expr_node::ExprType::Case,
1462+
"CaseExpr",
1463+
);
1464+
1465+
Ok(Arc::new(CaseExpr::try_new(
1466+
case.expr
1467+
.as_deref()
1468+
.map(|expr| ctx.decode(expr))
1469+
.transpose()?,
1470+
case.when_then_expr
1471+
.iter()
1472+
.map(|when_then| {
1473+
Ok((
1474+
ctx.decode_required_expression(
1475+
when_then.when_expr.as_ref(),
1476+
"CaseExpr",
1477+
"when_expr",
1478+
)?,
1479+
ctx.decode_required_expression(
1480+
when_then.then_expr.as_ref(),
1481+
"CaseExpr",
1482+
"then_expr",
1483+
)?,
1484+
))
1485+
})
1486+
.collect::<Result<Vec<_>>>()?,
1487+
case.else_expr
1488+
.as_deref()
1489+
.map(|expr| ctx.decode(expr))
1490+
.transpose()?,
1491+
)?))
1492+
}
14131493
}
14141494

14151495
/// Attempts to const evaluate the given `predicate`.
@@ -3193,3 +3273,177 @@ mod tests {
31933273
Ok(())
31943274
}
31953275
}
3276+
3277+
#[cfg(all(test, feature = "proto"))]
3278+
mod proto_tests {
3279+
use super::*;
3280+
use crate::expressions::col;
3281+
use crate::proto_test_util::{
3282+
StubDecoder, StubEncoder, UnreachableDecoder, column_node,
3283+
};
3284+
use arrow::datatypes::Field;
3285+
use datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx;
3286+
use datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx;
3287+
use datafusion_proto_models::protobuf;
3288+
use datafusion_proto_models::protobuf::{PhysicalExprNode, PhysicalWhenThen};
3289+
3290+
fn proto_case_fixture() -> CaseExpr {
3291+
let schema = Schema::new(vec![Field::new("a", DataType::Boolean, true)]);
3292+
CaseExpr::try_new(
3293+
Some(col("a", &schema).unwrap()),
3294+
vec![(lit(true), lit(1_i32))],
3295+
Some(lit(0_i32)),
3296+
)
3297+
.unwrap()
3298+
}
3299+
3300+
fn proto_when_then(
3301+
when_expr: Option<PhysicalExprNode>,
3302+
then_expr: Option<PhysicalExprNode>,
3303+
) -> PhysicalWhenThen {
3304+
PhysicalWhenThen {
3305+
when_expr,
3306+
then_expr,
3307+
}
3308+
}
3309+
3310+
fn proto_case_node(
3311+
expr: Option<Box<PhysicalExprNode>>,
3312+
when_then_expr: Vec<PhysicalWhenThen>,
3313+
else_expr: Option<Box<PhysicalExprNode>>,
3314+
) -> PhysicalExprNode {
3315+
PhysicalExprNode {
3316+
expr_id: None,
3317+
expr_type: Some(protobuf::physical_expr_node::ExprType::Case(Box::new(
3318+
protobuf::PhysicalCaseNode {
3319+
expr,
3320+
when_then_expr,
3321+
else_expr,
3322+
},
3323+
))),
3324+
}
3325+
}
3326+
3327+
#[test]
3328+
fn try_to_proto_encodes_case_expr() {
3329+
let case = proto_case_fixture();
3330+
let encoder = StubEncoder::ok();
3331+
let ctx = PhysicalExprEncodeCtx::new(&encoder);
3332+
3333+
let node = case
3334+
.try_to_proto(&ctx)
3335+
.unwrap()
3336+
.expect("CaseExpr should encode to Some(node)");
3337+
3338+
assert!(node.expr_id.is_none());
3339+
let case_node = match node.expr_type {
3340+
Some(protobuf::physical_expr_node::ExprType::Case(boxed)) => *boxed,
3341+
other => panic!("expected a CaseExpr node, got {other:?}"),
3342+
};
3343+
assert!(case_node.expr.is_some());
3344+
assert_eq!(case_node.when_then_expr.len(), 1);
3345+
assert!(case_node.when_then_expr[0].when_expr.is_some());
3346+
assert!(case_node.when_then_expr[0].then_expr.is_some());
3347+
assert!(case_node.else_expr.is_some());
3348+
}
3349+
3350+
#[test]
3351+
fn try_to_proto_propagates_child_encode_error() {
3352+
let case = proto_case_fixture();
3353+
// Call 1 is the optional CASE expr, call 2 is the WHEN expr.
3354+
let encoder = StubEncoder::failing_on(2);
3355+
let ctx = PhysicalExprEncodeCtx::new(&encoder);
3356+
3357+
let err = case.try_to_proto(&ctx).unwrap_err();
3358+
assert!(matches!(err, DataFusionError::Internal(msg) if msg.contains("call 2")));
3359+
}
3360+
3361+
#[test]
3362+
fn try_from_proto_decodes_case_expr() {
3363+
let node = proto_case_node(
3364+
Some(Box::new(column_node("case"))),
3365+
vec![proto_when_then(
3366+
Some(column_node("when")),
3367+
Some(column_node("then")),
3368+
)],
3369+
Some(Box::new(column_node("else"))),
3370+
);
3371+
let schema = Schema::empty();
3372+
let decoder = StubDecoder::ok();
3373+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
3374+
3375+
let decoded = CaseExpr::try_from_proto(&node, &ctx).unwrap();
3376+
let case = decoded
3377+
.downcast_ref::<CaseExpr>()
3378+
.expect("decoded expr should be a CaseExpr");
3379+
3380+
assert!(case.expr().is_some());
3381+
assert_eq!(case.when_then_expr().len(), 1);
3382+
assert!(case.else_expr().is_some());
3383+
}
3384+
3385+
#[test]
3386+
fn try_from_proto_rejects_non_case_node() {
3387+
let node = column_node("a");
3388+
let schema = Schema::empty();
3389+
let decoder = UnreachableDecoder;
3390+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
3391+
3392+
let err = CaseExpr::try_from_proto(&node, &ctx).unwrap_err();
3393+
assert!(
3394+
matches!(err, DataFusionError::Internal(msg) if msg.contains("PhysicalExprNode is not a CaseExpr"))
3395+
);
3396+
}
3397+
3398+
#[test]
3399+
fn try_from_proto_rejects_missing_when_expr() {
3400+
let node = proto_case_node(
3401+
None,
3402+
vec![proto_when_then(None, Some(column_node("then")))],
3403+
None,
3404+
);
3405+
let schema = Schema::empty();
3406+
let decoder = UnreachableDecoder;
3407+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
3408+
3409+
let err = CaseExpr::try_from_proto(&node, &ctx).unwrap_err();
3410+
assert!(
3411+
matches!(err, DataFusionError::Internal(msg) if msg.contains("CaseExpr is missing required field 'when_expr'"))
3412+
);
3413+
}
3414+
3415+
#[test]
3416+
fn try_from_proto_rejects_missing_then_expr() {
3417+
let node = proto_case_node(
3418+
None,
3419+
vec![proto_when_then(Some(column_node("when")), None)],
3420+
None,
3421+
);
3422+
let schema = Schema::empty();
3423+
let decoder = StubDecoder::ok();
3424+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
3425+
3426+
let err = CaseExpr::try_from_proto(&node, &ctx).unwrap_err();
3427+
assert!(
3428+
matches!(err, DataFusionError::Internal(msg) if msg.contains("CaseExpr is missing required field 'then_expr'"))
3429+
);
3430+
}
3431+
3432+
#[test]
3433+
fn try_from_proto_propagates_child_decode_error() {
3434+
let node = proto_case_node(
3435+
Some(Box::new(column_node("case"))),
3436+
vec![proto_when_then(
3437+
Some(column_node("when")),
3438+
Some(column_node("then")),
3439+
)],
3440+
Some(Box::new(column_node("else"))),
3441+
);
3442+
let schema = Schema::empty();
3443+
let decoder = StubDecoder::failing_on(2);
3444+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
3445+
3446+
let err = CaseExpr::try_from_proto(&node, &ctx).unwrap_err();
3447+
assert!(matches!(err, DataFusionError::Internal(msg) if msg.contains("call 2")));
3448+
}
3449+
}

‎datafusion/proto/src/physical_plan/from_proto.rs‎

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -303,41 +303,7 @@ pub fn parse_physical_expr_with_converter(
303303
ExprType::NotExpr(_) => NotExpr::try_from_proto(proto, &decode_ctx)?,
304304
ExprType::Negative(_) => NegativeExpr::try_from_proto(proto, &decode_ctx)?,
305305
ExprType::InList(_) => InListExpr::try_from_proto(proto, &decode_ctx)?,
306-
ExprType::Case(e) => Arc::new(CaseExpr::try_new(
307-
e.expr
308-
.as_ref()
309-
.map(|e| {
310-
proto_converter.proto_to_physical_expr(e.as_ref(), input_schema, ctx)
311-
})
312-
.transpose()?,
313-
e.when_then_expr
314-
.iter()
315-
.map(|e| {
316-
Ok((
317-
parse_required_physical_expr(
318-
e.when_expr.as_ref(),
319-
ctx,
320-
"when_expr",
321-
input_schema,
322-
proto_converter,
323-
)?,
324-
parse_required_physical_expr(
325-
e.then_expr.as_ref(),
326-
ctx,
327-
"then_expr",
328-
input_schema,
329-
proto_converter,
330-
)?,
331-
))
332-
})
333-
.collect::<Result<Vec<_>>>()?,
334-
e.else_expr
335-
.as_ref()
336-
.map(|e| {
337-
proto_converter.proto_to_physical_expr(e.as_ref(), input_schema, ctx)
338-
})
339-
.transpose()?,
340-
)?),
306+
ExprType::Case(_) => CaseExpr::try_from_proto(proto, &decode_ctx)?,
341307
ExprType::Cast(_) => CastExpr::try_from_proto(proto, &decode_ctx)?,
342308
ExprType::TryCast(_) => TryCastExpr::try_from_proto(proto, &decode_ctx)?,
343309
ExprType::ScalarUdf(e) => {

‎datafusion/proto/src/physical_plan/to_proto.rs‎

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use datafusion_physical_expr::ScalarFunctionExpr;
3535
use datafusion_physical_expr::scalar_subquery::ScalarSubqueryExpr;
3636
use datafusion_physical_expr::window::{SlidingAggregateWindowExpr, StandardWindowExpr};
3737
use datafusion_physical_expr_common::sort_expr::PhysicalSortExpr;
38-
use datafusion_physical_plan::expressions::{CaseExpr, DynamicFilterPhysicalExpr};
38+
use datafusion_physical_plan::expressions::DynamicFilterPhysicalExpr;
3939
use datafusion_physical_plan::udaf::AggregateFunctionExpr;
4040
use datafusion_physical_plan::windows::{PlainAggregateWindowExpr, WindowUDFExpr};
4141
use datafusion_physical_plan::{
@@ -300,50 +300,7 @@ pub fn serialize_physical_expr_with_converter(
300300
return Ok(node);
301301
}
302302

303-
if let Some(expr) = expr.downcast_ref::<CaseExpr>() {
304-
Ok(protobuf::PhysicalExprNode {
305-
expr_id,
306-
expr_type: Some(
307-
protobuf::physical_expr_node::ExprType::Case(
308-
Box::new(
309-
protobuf::PhysicalCaseNode {
310-
expr: expr
311-
.expr()
312-
.map(|exp| {
313-
proto_converter
314-
.physical_expr_to_proto(exp, codec)
315-
.map(Box::new)
316-
})
317-
.transpose()?,
318-
when_then_expr: expr
319-
.when_then_expr()
320-
.iter()
321-
.map(|(when_expr, then_expr)| {
322-
serialize_when_then_expr(
323-
when_expr,
324-
then_expr,
325-
codec,
326-
proto_converter,
327-
)
328-
})
329-
.collect::<Result<
330-
Vec<protobuf::PhysicalWhenThen>,
331-
DataFusionError,
332-
>>()?,
333-
else_expr: expr
334-
.else_expr()
335-
.map(|a| {
336-
proto_converter
337-
.physical_expr_to_proto(a, codec)
338-
.map(Box::new)
339-
})
340-
.transpose()?,
341-
},
342-
),
343-
),
344-
),
345-
})
346-
} else if let Some(expr) = expr.downcast_ref::<ScalarFunctionExpr>() {
303+
if let Some(expr) = expr.downcast_ref::<ScalarFunctionExpr>() {
347304
let mut buf = Vec::new();
348305
codec.try_encode_udf(expr.fun(), &mut buf)?;
349306
Ok(protobuf::PhysicalExprNode {
@@ -500,18 +457,6 @@ fn serialize_range_split_point(
500457
})
501458
}
502459

503-
fn serialize_when_then_expr(
504-
when_expr: &Arc<dyn PhysicalExpr>,
505-
then_expr: &Arc<dyn PhysicalExpr>,
506-
codec: &dyn PhysicalExtensionCodec,
507-
proto_converter: &dyn PhysicalProtoConverterExtension,
508-
) -> Result<protobuf::PhysicalWhenThen> {
509-
Ok(protobuf::PhysicalWhenThen {
510-
when_expr: Some(proto_converter.physical_expr_to_proto(when_expr, codec)?),
511-
then_expr: Some(proto_converter.physical_expr_to_proto(then_expr, codec)?),
512-
})
513-
}
514-
515460
impl TryFromProto<&PartitionedFile> for protobuf::PartitionedFile {
516461
type Error = DataFusionError;
517462

0 commit comments

Comments
 (0)