datafusion_physical_expr/expressions/
literal.rs1use std::hash::Hash;
21use std::sync::Arc;
22
23use crate::physical_expr::PhysicalExpr;
24
25use arrow::datatypes::{Field, FieldRef};
26use arrow::{
27 datatypes::{DataType, Schema},
28 record_batch::RecordBatch,
29};
30use datafusion_common::metadata::FieldMetadata;
31use datafusion_common::{Result, ScalarValue};
32use datafusion_expr::Expr;
33use datafusion_expr_common::columnar_value::ColumnarValue;
34use datafusion_expr_common::interval_arithmetic::Interval;
35use datafusion_expr_common::placement::ExpressionPlacement;
36use datafusion_expr_common::sort_properties::{ExprProperties, SortProperties};
37
38#[derive(Debug, PartialEq, Eq, Clone)]
40pub struct Literal {
41 value: ScalarValue,
42 field: FieldRef,
43}
44
45impl Hash for Literal {
46 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
47 self.value.hash(state);
48 let metadata = self.field.metadata();
49 let mut keys = metadata.keys().collect::<Vec<_>>();
50 keys.sort();
51 for key in keys {
52 key.hash(state);
53 metadata.get(key).unwrap().hash(state);
54 }
55 }
56}
57
58impl Literal {
59 pub fn new(value: ScalarValue) -> Self {
61 Self::new_with_metadata(value, None)
62 }
63
64 pub fn new_with_metadata(
66 value: ScalarValue,
67 metadata: Option<FieldMetadata>,
68 ) -> Self {
69 let mut field = Field::new("lit".to_string(), value.data_type(), value.is_null());
70
71 if let Some(metadata) = metadata {
72 field = metadata.add_to_field(field);
73 }
74
75 Self {
76 value,
77 field: field.into(),
78 }
79 }
80
81 pub fn value(&self) -> &ScalarValue {
83 &self.value
84 }
85}
86
87impl std::fmt::Display for Literal {
88 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
89 write!(f, "{}", self.value)
90 }
91}
92
93impl PhysicalExpr for Literal {
94 fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
95 Ok(self.value.data_type())
96 }
97
98 fn nullable(&self, _input_schema: &Schema) -> Result<bool> {
99 Ok(self.value.is_null())
100 }
101
102 fn return_field(&self, _input_schema: &Schema) -> Result<FieldRef> {
103 Ok(Arc::clone(&self.field))
104 }
105
106 fn evaluate(&self, _batch: &RecordBatch) -> Result<ColumnarValue> {
107 Ok(ColumnarValue::Scalar(self.value.clone()))
108 }
109
110 fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
111 vec![]
112 }
113
114 fn with_new_children(
115 self: Arc<Self>,
116 _children: Vec<Arc<dyn PhysicalExpr>>,
117 ) -> Result<Arc<dyn PhysicalExpr>> {
118 Ok(self)
119 }
120
121 fn get_properties(&self, _children: &[ExprProperties]) -> Result<ExprProperties> {
122 Ok(ExprProperties {
123 sort_properties: SortProperties::Singleton,
124 range: Interval::try_new(self.value().clone(), self.value().clone())?,
125 preserves_lex_ordering: true,
126 strictly_order_preserving: true,
128 })
129 }
130
131 fn fmt_sql(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132 std::fmt::Display::fmt(self, f)
133 }
134
135 fn placement(&self) -> ExpressionPlacement {
136 ExpressionPlacement::Literal
137 }
138
139 #[cfg(feature = "proto")]
140 fn try_to_proto(
141 &self,
142 _ctx: &datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx<'_>,
143 ) -> Result<Option<datafusion_proto_models::protobuf::PhysicalExprNode>> {
144 use datafusion_proto_models::protobuf;
145
146 Ok(Some(protobuf::PhysicalExprNode {
147 expr_id: None,
148 expr_type: Some(protobuf::physical_expr_node::ExprType::Literal(
149 (&self.value).try_into()?,
150 )),
151 }))
152 }
153}
154
155#[cfg(feature = "proto")]
156impl Literal {
157 pub fn try_from_proto(
159 node: &datafusion_proto_models::protobuf::PhysicalExprNode,
160 _ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
161 ) -> Result<Arc<dyn PhysicalExpr>> {
162 use datafusion_physical_expr_common::expect_expr_variant;
163 use datafusion_proto_models::protobuf;
164
165 let scalar_proto = expect_expr_variant!(
166 node,
167 protobuf::physical_expr_node::ExprType::Literal,
168 "Literal",
169 );
170 let value = ScalarValue::try_from(scalar_proto)?;
171 Ok(Arc::new(Literal::new(value)))
172 }
173}
174
175#[expect(clippy::needless_pass_by_value)]
177pub fn lit<T: datafusion_expr::Literal>(value: T) -> Arc<dyn PhysicalExpr> {
178 match value.lit() {
179 Expr::Literal(v, _) => Arc::new(Literal::new(v)),
180 _ => unreachable!(),
181 }
182}
183
184#[cfg(test)]
185mod tests {
186 use super::*;
187
188 use arrow::array::Int32Array;
189 use datafusion_common::cast::as_int32_array;
190 use datafusion_physical_expr_common::physical_expr::fmt_sql;
191
192 #[test]
193 fn literal_i32() -> Result<()> {
194 let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
196 let a = Int32Array::from(vec![Some(1), None, Some(3), Some(4), Some(5)]);
197 let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)])?;
198
199 let literal_expr = lit(42i32);
201 assert_eq!("42", format!("{literal_expr}"));
202
203 let literal_array = literal_expr
204 .evaluate(&batch)?
205 .into_array(batch.num_rows())
206 .expect("Failed to convert to array");
207 let literal_array = as_int32_array(&literal_array)?;
208
209 assert_eq!(literal_array.len(), 5); for i in 0..literal_array.len() {
212 assert_eq!(literal_array.value(i), 42);
213 }
214
215 Ok(())
216 }
217
218 #[test]
219 fn test_fmt_sql() -> Result<()> {
220 let expr = lit(42i32);
222 let display_string = expr.to_string();
223 assert_eq!(display_string, "42");
224 let sql_string = fmt_sql(expr.as_ref()).to_string();
225 assert_eq!(sql_string, "42");
226
227 Ok(())
228 }
229}
230
231#[cfg(all(test, feature = "proto"))]
233mod proto_tests {
234 use super::*;
235 use crate::proto_test_util::{StubEncoder, UnreachableDecoder, column_node};
236 use datafusion_common::DataFusionError;
237 use datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx;
238 use datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx;
239 use datafusion_proto_models::protobuf::physical_expr_node;
240
241 fn i32_literal() -> Literal {
242 Literal::new(ScalarValue::Int32(Some(42)))
243 }
244
245 #[test]
248 fn try_to_proto_encodes_literal() {
249 let literal = i32_literal();
250 let encoder = StubEncoder::ok();
251 let ctx = PhysicalExprEncodeCtx::new(&encoder);
252
253 let node = literal
254 .try_to_proto(&ctx)
255 .unwrap()
256 .expect("Literal should encode to Some(node)");
257
258 assert!(node.expr_id.is_none());
260 assert!(matches!(
262 node.expr_type,
263 Some(physical_expr_node::ExprType::Literal(_))
264 ));
265 }
266
267 #[test]
268 fn try_to_proto_null_literal() {
269 let literal = Literal::new(ScalarValue::Int32(None));
270 let encoder = StubEncoder::ok();
271 let ctx = PhysicalExprEncodeCtx::new(&encoder);
272
273 let node = literal
274 .try_to_proto(&ctx)
275 .unwrap()
276 .expect("null Literal should encode to Some(node)");
277
278 assert!(matches!(
279 node.expr_type,
280 Some(physical_expr_node::ExprType::Literal(_))
281 ));
282
283 let schema = Schema::empty();
285 let decoder = UnreachableDecoder;
286 let dec_ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
287 let decoded = Literal::try_from_proto(&node, &dec_ctx).unwrap();
288 let lit = decoded
289 .downcast_ref::<Literal>()
290 .expect("decoded expr should be a Literal");
291 assert_eq!(lit.value(), &ScalarValue::Int32(None));
292 }
293
294 #[test]
297 fn try_from_proto_roundtrip() {
298 let original = i32_literal();
299 let encoder = StubEncoder::ok();
300 let enc_ctx = PhysicalExprEncodeCtx::new(&encoder);
301
302 let node = original
303 .try_to_proto(&enc_ctx)
304 .unwrap()
305 .expect("should encode");
306
307 let schema = Schema::empty();
308 let decoder = UnreachableDecoder;
309 let dec_ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
310
311 let decoded = Literal::try_from_proto(&node, &dec_ctx).unwrap();
312 let lit = decoded
313 .downcast_ref::<Literal>()
314 .expect("decoded expr should be a Literal");
315 assert_eq!(lit.value(), &ScalarValue::Int32(Some(42)));
316 }
317
318 #[test]
319 fn try_from_proto_rejects_non_literal_node() {
320 let node = column_node("a");
321 let schema = Schema::empty();
322 let decoder = UnreachableDecoder;
323 let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
324 let err = Literal::try_from_proto(&node, &ctx).unwrap_err();
325 assert!(
326 matches!(err, DataFusionError::Internal(ref msg) if msg.contains("PhysicalExprNode is not a Literal"))
327 );
328 }
329}