Skip to content

Commit 2a93fe8

Browse files
Kanishk Sachankoopatroopa787
authored andcommitted
feat(physical-expr): port Literal to try_to_proto / try_from_proto hooks
Move protobuf serialization/deserialization for Literal out of the central downcast chains in to_proto.rs and from_proto.rs into dedicated try_to_proto / try_from_proto hooks on Literal itself, following the pattern established for NotExpr, NegativeExpr, IsNullExpr, and IsNotNullExpr. Closes apache#22427
1 parent 533ef35 commit 2a93fe8

3 files changed

Lines changed: 141 additions & 4 deletions

File tree

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

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,41 @@ impl PhysicalExpr for Literal {
133133
fn placement(&self) -> ExpressionPlacement {
134134
ExpressionPlacement::Literal
135135
}
136+
137+
#[cfg(feature = "proto")]
138+
fn try_to_proto(
139+
&self,
140+
_ctx: &datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx<'_>,
141+
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalExprNode>> {
142+
use datafusion_proto_models::protobuf;
143+
144+
Ok(Some(protobuf::PhysicalExprNode {
145+
expr_id: None,
146+
expr_type: Some(protobuf::physical_expr_node::ExprType::Literal(
147+
(&self.value).try_into()?,
148+
)),
149+
}))
150+
}
151+
}
152+
153+
#[cfg(feature = "proto")]
154+
impl Literal {
155+
/// Reconstruct a [`Literal`] from its protobuf representation.
156+
pub fn try_from_proto(
157+
node: &datafusion_proto_models::protobuf::PhysicalExprNode,
158+
_ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
159+
) -> Result<Arc<dyn PhysicalExpr>> {
160+
use datafusion_physical_expr_common::expect_expr_variant;
161+
use datafusion_proto_models::protobuf;
162+
163+
let scalar_proto = expect_expr_variant!(
164+
node,
165+
protobuf::physical_expr_node::ExprType::Literal,
166+
"Literal",
167+
);
168+
let value = ScalarValue::try_from(scalar_proto)?;
169+
Ok(Arc::new(Literal::new(value)))
170+
}
136171
}
137172

138173
/// Create a literal expression
@@ -190,3 +225,93 @@ mod tests {
190225
Ok(())
191226
}
192227
}
228+
229+
/// Tests for the `try_to_proto` / `try_from_proto` hooks.
230+
#[cfg(all(test, feature = "proto"))]
231+
mod proto_tests {
232+
use super::*;
233+
use crate::proto_test_util::{StubEncoder, UnreachableDecoder, column_node};
234+
use datafusion_common::DataFusionError;
235+
use datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx;
236+
use datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx;
237+
use datafusion_proto_models::protobuf::physical_expr_node;
238+
239+
fn i32_literal() -> Literal {
240+
Literal::new(ScalarValue::Int32(Some(42)))
241+
}
242+
243+
// ── try_to_proto ─────────────────────────────────────────────────────────
244+
245+
#[test]
246+
fn try_to_proto_encodes_literal() {
247+
let literal = i32_literal();
248+
let encoder = StubEncoder::ok();
249+
let ctx = PhysicalExprEncodeCtx::new(&encoder);
250+
251+
let node = literal
252+
.try_to_proto(&ctx)
253+
.unwrap()
254+
.expect("Literal should encode to Some(node)");
255+
256+
// Literal nodes never set expr_id.
257+
assert!(node.expr_id.is_none());
258+
// Variant must be Literal, not any other expr type.
259+
assert!(matches!(
260+
node.expr_type,
261+
Some(physical_expr_node::ExprType::Literal(_))
262+
));
263+
}
264+
265+
#[test]
266+
fn try_to_proto_null_literal() {
267+
let literal = Literal::new(ScalarValue::Int32(None));
268+
let encoder = StubEncoder::ok();
269+
let ctx = PhysicalExprEncodeCtx::new(&encoder);
270+
271+
let node = literal
272+
.try_to_proto(&ctx)
273+
.unwrap()
274+
.expect("null Literal should encode to Some(node)");
275+
276+
assert!(matches!(
277+
node.expr_type,
278+
Some(physical_expr_node::ExprType::Literal(_))
279+
));
280+
}
281+
282+
// ── try_from_proto ───────────────────────────────────────────────────────
283+
284+
#[test]
285+
fn try_from_proto_roundtrip() {
286+
let original = i32_literal();
287+
let encoder = StubEncoder::ok();
288+
let enc_ctx = PhysicalExprEncodeCtx::new(&encoder);
289+
290+
let node = original
291+
.try_to_proto(&enc_ctx)
292+
.unwrap()
293+
.expect("should encode");
294+
295+
let schema = Schema::empty();
296+
let decoder = UnreachableDecoder;
297+
let dec_ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
298+
299+
let decoded = Literal::try_from_proto(&node, &dec_ctx).unwrap();
300+
let lit = decoded
301+
.downcast_ref::<Literal>()
302+
.expect("decoded expr should be a Literal");
303+
assert_eq!(lit.value(), &ScalarValue::Int32(Some(42)));
304+
}
305+
306+
#[test]
307+
fn try_from_proto_rejects_non_literal_node() {
308+
let node = column_node("a");
309+
let schema = Schema::empty();
310+
let decoder = UnreachableDecoder;
311+
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
312+
let err = Literal::try_from_proto(&node, &ctx).unwrap_err();
313+
assert!(
314+
matches!(err, DataFusionError::Internal(ref msg) if msg.contains("PhysicalExprNode is not a Literal"))
315+
);
316+
}
317+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub fn parse_physical_expr_with_converter(
283283
// to the right constructor.
284284
ExprType::Column(_) => Column::try_from_proto(proto, &decode_ctx)?,
285285
ExprType::UnknownColumn(_) => UnKnownColumn::try_from_proto(proto, &decode_ctx)?,
286-
ExprType::Literal(scalar) => Arc::new(Literal::new(scalar.try_into()?)),
286+
ExprType::Literal(_) => Literal::try_from_proto(proto, &decode_ctx)?,
287287
ExprType::BinaryExpr(_) => BinaryExpr::try_from_proto(proto, &decode_ctx)?,
288288
ExprType::AggregateExpr(_) => {
289289
return not_impl_err!(

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +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::{
39-
CaseExpr, DynamicFilterPhysicalExpr, Literal,
40-
};
38+
use datafusion_physical_plan::expressions::{CaseExpr, DynamicFilterPhysicalExpr};
4139
use datafusion_physical_plan::udaf::AggregateFunctionExpr;
4240
use datafusion_physical_plan::windows::{PlainAggregateWindowExpr, WindowUDFExpr};
4341
use datafusion_physical_plan::{
@@ -345,12 +343,26 @@ pub fn serialize_physical_expr_with_converter(
345343
),
346344
),
347345
})
346+
<<<<<<< HEAD
348347
} else if let Some(lit) = expr.downcast_ref::<Literal>() {
349348
Ok(protobuf::PhysicalExprNode {
350349
expr_id,
351350
expr_type: Some(protobuf::physical_expr_node::ExprType::Literal(
352351
lit.value().try_into()?,
353352
)),
353+
=======
354+
} else if let Some(cast) = expr.downcast_ref::<TryCastExpr>() {
355+
Ok(protobuf::PhysicalExprNode {
356+
expr_id,
357+
expr_type: Some(protobuf::physical_expr_node::ExprType::TryCast(Box::new(
358+
protobuf::PhysicalTryCastNode {
359+
expr: Some(Box::new(
360+
proto_converter.physical_expr_to_proto(cast.expr(), codec)?,
361+
)),
362+
arrow_type: Some(cast.cast_type().try_into()?),
363+
},
364+
))),
365+
>>>>>>> 69352ffb1 (feat(physical-expr): port Literal to try_to_proto / try_from_proto hooks)
354366
})
355367
} else if let Some(expr) = expr.downcast_ref::<ScalarFunctionExpr>() {
356368
let mut buf = Vec::new();

0 commit comments

Comments
 (0)