@@ -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+ }
0 commit comments