Skip to main content

datafusion_physical_expr/expressions/
literal.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Literal expressions for physical operations
19
20use std::any::Any;
21use std::hash::Hash;
22use std::sync::Arc;
23
24use crate::physical_expr::PhysicalExpr;
25
26use arrow::datatypes::{Field, FieldRef};
27use arrow::{
28    datatypes::{DataType, Schema},
29    record_batch::RecordBatch,
30};
31use datafusion_common::metadata::FieldMetadata;
32use datafusion_common::{Result, ScalarValue};
33use datafusion_expr::Expr;
34use datafusion_expr_common::columnar_value::ColumnarValue;
35use datafusion_expr_common::interval_arithmetic::Interval;
36use datafusion_expr_common::placement::ExpressionPlacement;
37use datafusion_expr_common::sort_properties::{ExprProperties, SortProperties};
38
39/// Represents a literal value
40#[derive(Debug, PartialEq, Eq, Clone)]
41pub struct Literal {
42    value: ScalarValue,
43    field: FieldRef,
44}
45
46impl Hash for Literal {
47    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
48        self.value.hash(state);
49        let metadata = self.field.metadata();
50        let mut keys = metadata.keys().collect::<Vec<_>>();
51        keys.sort();
52        for key in keys {
53            key.hash(state);
54            metadata.get(key).unwrap().hash(state);
55        }
56    }
57}
58
59impl Literal {
60    /// Create a literal value expression
61    pub fn new(value: ScalarValue) -> Self {
62        Self::new_with_metadata(value, None)
63    }
64
65    /// Create a literal value expression
66    pub fn new_with_metadata(
67        value: ScalarValue,
68        metadata: Option<FieldMetadata>,
69    ) -> Self {
70        let mut field = Field::new("lit".to_string(), value.data_type(), value.is_null());
71
72        if let Some(metadata) = metadata {
73            field = metadata.add_to_field(field);
74        }
75
76        Self {
77            value,
78            field: field.into(),
79        }
80    }
81
82    /// Get the scalar value
83    pub fn value(&self) -> &ScalarValue {
84        &self.value
85    }
86}
87
88impl std::fmt::Display for Literal {
89    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
90        write!(f, "{}", self.value)
91    }
92}
93
94impl PhysicalExpr for Literal {
95    /// Return a reference to Any that can be used for downcasting
96    fn as_any(&self) -> &dyn Any {
97        self
98    }
99
100    fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
101        Ok(self.value.data_type())
102    }
103
104    fn nullable(&self, _input_schema: &Schema) -> Result<bool> {
105        Ok(self.value.is_null())
106    }
107
108    fn return_field(&self, _input_schema: &Schema) -> Result<FieldRef> {
109        Ok(Arc::clone(&self.field))
110    }
111
112    fn evaluate(&self, _batch: &RecordBatch) -> Result<ColumnarValue> {
113        Ok(ColumnarValue::Scalar(self.value.clone()))
114    }
115
116    fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
117        vec![]
118    }
119
120    fn with_new_children(
121        self: Arc<Self>,
122        _children: Vec<Arc<dyn PhysicalExpr>>,
123    ) -> Result<Arc<dyn PhysicalExpr>> {
124        Ok(self)
125    }
126
127    fn get_properties(&self, _children: &[ExprProperties]) -> Result<ExprProperties> {
128        Ok(ExprProperties {
129            sort_properties: SortProperties::Singleton,
130            range: Interval::try_new(self.value().clone(), self.value().clone())?,
131            preserves_lex_ordering: true,
132        })
133    }
134
135    fn fmt_sql(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136        std::fmt::Display::fmt(self, f)
137    }
138
139    fn placement(&self) -> ExpressionPlacement {
140        ExpressionPlacement::Literal
141    }
142}
143
144/// Create a literal expression
145#[expect(clippy::needless_pass_by_value)]
146pub fn lit<T: datafusion_expr::Literal>(value: T) -> Arc<dyn PhysicalExpr> {
147    match value.lit() {
148        Expr::Literal(v, _) => Arc::new(Literal::new(v)),
149        _ => unreachable!(),
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156
157    use arrow::array::Int32Array;
158    use arrow::datatypes::Field;
159    use datafusion_common::cast::as_int32_array;
160    use datafusion_physical_expr_common::physical_expr::fmt_sql;
161
162    #[test]
163    fn literal_i32() -> Result<()> {
164        // create an arbitrary record batch
165        let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
166        let a = Int32Array::from(vec![Some(1), None, Some(3), Some(4), Some(5)]);
167        let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)])?;
168
169        // create and evaluate a literal expression
170        let literal_expr = lit(42i32);
171        assert_eq!("42", format!("{literal_expr}"));
172
173        let literal_array = literal_expr
174            .evaluate(&batch)?
175            .into_array(batch.num_rows())
176            .expect("Failed to convert to array");
177        let literal_array = as_int32_array(&literal_array)?;
178
179        // note that the contents of the literal array are unrelated to the batch contents except for the length of the array
180        assert_eq!(literal_array.len(), 5); // 5 rows in the batch
181        for i in 0..literal_array.len() {
182            assert_eq!(literal_array.value(i), 42);
183        }
184
185        Ok(())
186    }
187
188    #[test]
189    fn test_fmt_sql() -> Result<()> {
190        // create and evaluate a literal expression
191        let expr = lit(42i32);
192        let display_string = expr.to_string();
193        assert_eq!(display_string, "42");
194        let sql_string = fmt_sql(expr.as_ref()).to_string();
195        assert_eq!(sql_string, "42");
196
197        Ok(())
198    }
199}