Skip to content

Commit a754587

Browse files
authored
Port CastExpr to proto hooks (apache#22569)
## Which issue does this PR close? - Closes apache#22428. ## Rationale for this change `CastExpr` protobuf serialization is currently handled by the central physical expression downcast chain. This PR migrates it to the per-expression `try_to_proto` / `try_from_proto` hooks, following the existing `Column` / `LikeExpr` pattern. ## What changes are included in this PR? - Adds `CastExpr::try_to_proto` in the `impl PhysicalExpr for CastExpr` block. - Adds inherent `CastExpr::try_from_proto` for decoding `PhysicalCastNode`. - Routes `ExprType::Cast` decoding through the new hook. - Removes the old central `CastExpr` serialization arm from `to_proto.rs`. - Adds direct hook tests for successful encode/decode and bad-input cases. ## Are these changes tested? Yes ## Are there any user-facing changes? No. The protobuf wire format for `CastExpr` remains unchanged.
1 parent 2888bce commit a754587

3 files changed

Lines changed: 219 additions & 25 deletions

File tree

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

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,61 @@ impl PhysicalExpr for CastExpr {
298298

299299
write!(f, ")")
300300
}
301+
302+
#[cfg(feature = "proto")]
303+
fn try_to_proto(
304+
&self,
305+
ctx: &datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx<'_>,
306+
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalExprNode>> {
307+
use datafusion_proto_models::protobuf;
308+
309+
Ok(Some(protobuf::PhysicalExprNode {
310+
expr_id: None,
311+
expr_type: Some(protobuf::physical_expr_node::ExprType::Cast(Box::new(
312+
protobuf::PhysicalCastNode {
313+
expr: Some(Box::new(ctx.encode_child(self.expr())?)),
314+
arrow_type: Some(self.cast_type().try_into()?),
315+
},
316+
))),
317+
}))
318+
}
319+
}
320+
321+
#[cfg(feature = "proto")]
322+
impl CastExpr {
323+
/// Reconstruct a [`CastExpr`] from its protobuf representation.
324+
///
325+
/// Takes the whole [`PhysicalExprNode`] so the decode signature matches
326+
/// other migrated expressions and can inspect outer-node metadata if
327+
/// needed in the future.
328+
///
329+
/// [`PhysicalExprNode`]: datafusion_proto_models::protobuf::PhysicalExprNode
330+
pub fn try_from_proto(
331+
node: &datafusion_proto_models::protobuf::PhysicalExprNode,
332+
ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
333+
) -> Result<Arc<dyn PhysicalExpr>> {
334+
use datafusion_common::internal_datafusion_err;
335+
use datafusion_common::internal_err;
336+
use datafusion_proto_models::protobuf;
337+
338+
let cast_expr = match &node.expr_type {
339+
Some(protobuf::physical_expr_node::ExprType::Cast(cast_expr)) => {
340+
cast_expr.as_ref()
341+
}
342+
_ => return internal_err!("PhysicalExprNode is not a CastExpr"),
343+
};
344+
345+
let expr = ctx.decode_required_expression(
346+
cast_expr.expr.as_deref(),
347+
"CastExpr",
348+
"expr",
349+
)?;
350+
let arrow_type = cast_expr.arrow_type.as_ref().ok_or_else(|| {
351+
internal_datafusion_err!("CastExpr is missing required field 'arrow_type'")
352+
})?;
353+
354+
Ok(Arc::new(CastExpr::new(expr, arrow_type.try_into()?, None)))
355+
}
301356
}
302357

303358
/// Return a PhysicalExpression representing `expr` casted to
@@ -1154,3 +1209,164 @@ mod tests {
11541209
Ok(())
11551210
}
11561211
}
1212+
1213+
/// Tests for the `try_to_proto` / `try_from_proto` hooks.
1214+
#[cfg(all(test, feature = "proto"))]
1215+
mod proto_tests {
1216+
use super::*;
1217+
use crate::expressions::{Column, col};
1218+
use crate::proto_test_util::{
1219+
StubDecoder, StubEncoder, UnreachableDecoder, column_node,
1220+
};
1221+
use arrow::datatypes::Field;
1222+
use datafusion_common::DataFusionError;
1223+
use datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx;
1224+
use datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx;
1225+
use datafusion_proto_models::datafusion_common::ArrowType;
1226+
use datafusion_proto_models::protobuf::{
1227+
PhysicalCastNode, PhysicalExprNode, physical_expr_node,
1228+
};
1229+
1230+
/// A `CastExpr` over an `Int32` column, casting to `Int64`.
1231+
fn proto_cast_fixture() -> CastExpr {
1232+
let schema = Schema::new(vec![Field::new("a", Int32, false)]);
1233+
CastExpr::new(col("a", &schema).unwrap(), Int64, None)
1234+
}
1235+
1236+
fn proto_int64_arrow_type() -> ArrowType {
1237+
(&Int64).try_into().unwrap()
1238+
}
1239+
1240+
/// Build a `CastExpr` proto node with the given child and target type.
1241+
fn proto_cast_node(
1242+
expr: Option<Box<PhysicalExprNode>>,
1243+
arrow_type: Option<ArrowType>,
1244+
) -> PhysicalExprNode {
1245+
PhysicalExprNode {
1246+
expr_id: None,
1247+
expr_type: Some(physical_expr_node::ExprType::Cast(Box::new(
1248+
PhysicalCastNode { expr, arrow_type },
1249+
))),
1250+
}
1251+
}
1252+
1253+
#[test]
1254+
fn try_to_proto_encodes_cast_expr() {
1255+
let cast = proto_cast_fixture();
1256+
let encoder = StubEncoder::ok();
1257+
let ctx = PhysicalExprEncodeCtx::new(&encoder);
1258+
1259+
let node = cast
1260+
.try_to_proto(&ctx)
1261+
.unwrap()
1262+
.expect("CastExpr should encode to Some(node)");
1263+
1264+
assert!(node.expr_id.is_none());
1265+
let cast_node = match node.expr_type {
1266+
Some(physical_expr_node::ExprType::Cast(cast_node)) => *cast_node,
1267+
other => panic!("expected a Cast node, got {other:?}"),
1268+
};
1269+
assert!(cast_node.expr.is_some());
1270+
1271+
let arrow_type = cast_node
1272+
.arrow_type
1273+
.as_ref()
1274+
.expect("cast type should be encoded");
1275+
let data_type: DataType = arrow_type.try_into().unwrap();
1276+
assert_eq!(data_type, Int64);
1277+
}
1278+
1279+
#[test]
1280+
fn try_to_proto_propagates_child_encode_error() {
1281+
let cast = proto_cast_fixture();
1282+
let encoder = StubEncoder::failing_on(1);
1283+
let ctx = PhysicalExprEncodeCtx::new(&encoder);
1284+
1285+
let err = cast.try_to_proto(&ctx).unwrap_err();
1286+
assert!(matches!(
1287+
err,
1288+
DataFusionError::Internal(msg) if msg.contains("call 1")
1289+
));
1290+
}
1291+
1292+
#[test]
1293+
fn try_from_proto_decodes_cast_expr() {
1294+
let node = proto_cast_node(
1295+
Some(Box::new(column_node("a"))),
1296+
Some(proto_int64_arrow_type()),
1297+
);
1298+
let schema = Schema::empty();
1299+
let decoder = StubDecoder::ok();
1300+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
1301+
1302+
let decoded = CastExpr::try_from_proto(&node, &ctx).unwrap();
1303+
let cast = decoded
1304+
.downcast_ref::<CastExpr>()
1305+
.expect("decoded expr should be a CastExpr");
1306+
1307+
assert_eq!(cast.cast_type(), &Int64);
1308+
assert!(cast.expr().downcast_ref::<Column>().is_some());
1309+
}
1310+
1311+
#[test]
1312+
fn try_from_proto_rejects_non_cast_node() {
1313+
let node = column_node("a");
1314+
let schema = Schema::empty();
1315+
let decoder = UnreachableDecoder;
1316+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
1317+
1318+
let err = CastExpr::try_from_proto(&node, &ctx).unwrap_err();
1319+
assert!(matches!(
1320+
err,
1321+
DataFusionError::Internal(msg)
1322+
if msg.contains("PhysicalExprNode is not a CastExpr")
1323+
));
1324+
}
1325+
1326+
#[test]
1327+
fn try_from_proto_rejects_missing_expr() {
1328+
let node = proto_cast_node(None, Some(proto_int64_arrow_type()));
1329+
let schema = Schema::empty();
1330+
let decoder = UnreachableDecoder;
1331+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
1332+
1333+
let err = CastExpr::try_from_proto(&node, &ctx).unwrap_err();
1334+
assert!(matches!(
1335+
err,
1336+
DataFusionError::Internal(msg)
1337+
if msg.contains("CastExpr is missing required field 'expr'")
1338+
));
1339+
}
1340+
1341+
#[test]
1342+
fn try_from_proto_rejects_missing_arrow_type() {
1343+
let node = proto_cast_node(Some(Box::new(column_node("a"))), None);
1344+
let schema = Schema::empty();
1345+
let decoder = StubDecoder::ok();
1346+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
1347+
1348+
let err = CastExpr::try_from_proto(&node, &ctx).unwrap_err();
1349+
assert!(matches!(
1350+
err,
1351+
DataFusionError::Internal(msg)
1352+
if msg.contains("CastExpr is missing required field 'arrow_type'")
1353+
));
1354+
}
1355+
1356+
#[test]
1357+
fn try_from_proto_propagates_child_decode_error() {
1358+
let node = proto_cast_node(
1359+
Some(Box::new(column_node("a"))),
1360+
Some(proto_int64_arrow_type()),
1361+
);
1362+
let schema = Schema::empty();
1363+
let decoder = StubDecoder::failing_on(1);
1364+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
1365+
1366+
let err = CastExpr::try_from_proto(&node, &ctx).unwrap_err();
1367+
assert!(matches!(
1368+
err,
1369+
DataFusionError::Internal(msg) if msg.contains("call 1")
1370+
));
1371+
}
1372+
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,17 +360,7 @@ pub fn parse_physical_expr_with_converter(
360360
})
361361
.transpose()?,
362362
)?),
363-
ExprType::Cast(e) => Arc::new(CastExpr::new(
364-
parse_required_physical_expr(
365-
e.expr.as_deref(),
366-
ctx,
367-
"expr",
368-
input_schema,
369-
proto_converter,
370-
)?,
371-
convert_required!(e.arrow_type)?,
372-
None,
373-
)),
363+
ExprType::Cast(_) => CastExpr::try_from_proto(proto, &decode_ctx)?,
374364
ExprType::TryCast(e) => Arc::new(TryCastExpr::new(
375365
parse_required_physical_expr(
376366
e.expr.as_deref(),

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ use datafusion_physical_expr::scalar_subquery::ScalarSubqueryExpr;
3636
use datafusion_physical_expr::window::{SlidingAggregateWindowExpr, StandardWindowExpr};
3737
use datafusion_physical_expr_common::sort_expr::PhysicalSortExpr;
3838
use datafusion_physical_plan::expressions::{
39-
CaseExpr, CastExpr, DynamicFilterPhysicalExpr, IsNotNullExpr, IsNullExpr, Literal,
40-
NotExpr, TryCastExpr, UnKnownColumn,
39+
CaseExpr, DynamicFilterPhysicalExpr, IsNotNullExpr, IsNullExpr, Literal, NotExpr,
40+
TryCastExpr, UnKnownColumn,
4141
};
4242
use datafusion_physical_plan::udaf::AggregateFunctionExpr;
4343
use datafusion_physical_plan::windows::{PlainAggregateWindowExpr, WindowUDFExpr};
@@ -395,18 +395,6 @@ pub fn serialize_physical_expr_with_converter(
395395
lit.value().try_into()?,
396396
)),
397397
})
398-
} else if let Some(cast) = expr.downcast_ref::<CastExpr>() {
399-
Ok(protobuf::PhysicalExprNode {
400-
expr_id,
401-
expr_type: Some(protobuf::physical_expr_node::ExprType::Cast(Box::new(
402-
protobuf::PhysicalCastNode {
403-
expr: Some(Box::new(
404-
proto_converter.physical_expr_to_proto(cast.expr(), codec)?,
405-
)),
406-
arrow_type: Some(cast.cast_type().try_into()?),
407-
},
408-
))),
409-
})
410398
} else if let Some(cast) = expr.downcast_ref::<TryCastExpr>() {
411399
Ok(protobuf::PhysicalExprNode {
412400
expr_id,

0 commit comments

Comments
 (0)