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::collections::HashMap;
22use std::hash::Hash;
23use std::sync::Arc;
24
25use crate::physical_expr::PhysicalExpr;
26
27use arrow::datatypes::{Field, FieldRef};
28use arrow::{
29    datatypes::{DataType, Schema},
30    record_batch::RecordBatch,
31};
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::sort_properties::{ExprProperties, SortProperties};
37
38/// Represents a literal value
39#[derive(Debug, PartialEq, Eq)]
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    /// Create a literal value expression
60    pub fn new(value: ScalarValue) -> Self {
61        Self::new_with_metadata(value, None)
62    }
63
64    /// Create a literal value expression
65    pub fn new_with_metadata(
66        value: ScalarValue,
67        metadata: impl Into<Option<HashMap<String, String>>>,
68    ) -> Self {
69        let metadata = metadata.into();
70        let mut field =
71            Field::new(format!("{value}"), value.data_type(), value.is_null());
72
73        if let Some(metadata) = metadata {
74            field = field.with_metadata(metadata);
75        }
76
77        Self {
78            value,
79            field: field.into(),
80        }
81    }
82
83    /// Get the scalar value
84    pub fn value(&self) -> &ScalarValue {
85        &self.value
86    }
87}
88
89impl std::fmt::Display for Literal {
90    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
91        write!(f, "{}", self.value)
92    }
93}
94
95impl PhysicalExpr for Literal {
96    /// Return a reference to Any that can be used for downcasting
97    fn as_any(&self) -> &dyn Any {
98        self
99    }
100
101    fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
102        Ok(self.value.data_type())
103    }
104
105    fn nullable(&self, _input_schema: &Schema) -> Result<bool> {
106        Ok(self.value.is_null())
107    }
108
109    fn return_field(&self, _input_schema: &Schema) -> Result<FieldRef> {
110        Ok(Arc::clone(&self.field))
111    }
112
113    fn evaluate(&self, _batch: &RecordBatch) -> Result<ColumnarValue> {
114        Ok(ColumnarValue::Scalar(self.value.clone()))
115    }
116
117    fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
118        vec![]
119    }
120
121    fn with_new_children(
122        self: Arc<Self>,
123        _children: Vec<Arc<dyn PhysicalExpr>>,
124    ) -> Result<Arc<dyn PhysicalExpr>> {
125        Ok(self)
126    }
127
128    fn get_properties(&self, _children: &[ExprProperties]) -> Result<ExprProperties> {
129        Ok(ExprProperties {
130            sort_properties: SortProperties::Singleton,
131            range: Interval::try_new(self.value().clone(), self.value().clone())?,
132            preserves_lex_ordering: true,
133        })
134    }
135
136    fn fmt_sql(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137        std::fmt::Display::fmt(self, f)
138    }
139}
140
141/// Create a literal expression
142pub fn lit<T: datafusion_expr::Literal>(value: T) -> Arc<dyn PhysicalExpr> {
143    match value.lit() {
144        Expr::Literal(v, _) => Arc::new(Literal::new(v)),
145        _ => unreachable!(),
146    }
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    use arrow::array::Int32Array;
154    use arrow::datatypes::Field;
155    use datafusion_common::cast::as_int32_array;
156    use datafusion_physical_expr_common::physical_expr::fmt_sql;
157
158    #[test]
159    fn literal_i32() -> Result<()> {
160        // create an arbitrary record batch
161        let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
162        let a = Int32Array::from(vec![Some(1), None, Some(3), Some(4), Some(5)]);
163        let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)])?;
164
165        // create and evaluate a literal expression
166        let literal_expr = lit(42i32);
167        assert_eq!("42", format!("{literal_expr}"));
168
169        let literal_array = literal_expr
170            .evaluate(&batch)?
171            .into_array(batch.num_rows())
172            .expect("Failed to convert to array");
173        let literal_array = as_int32_array(&literal_array)?;
174
175        // note that the contents of the literal array are unrelated to the batch contents except for the length of the array
176        assert_eq!(literal_array.len(), 5); // 5 rows in the batch
177        for i in 0..literal_array.len() {
178            assert_eq!(literal_array.value(i), 42);
179        }
180
181        Ok(())
182    }
183
184    #[test]
185    fn test_fmt_sql() -> Result<()> {
186        // create and evaluate a literal expression
187        let expr = lit(42i32);
188        let display_string = expr.to_string();
189        assert_eq!(display_string, "42");
190        let sql_string = fmt_sql(expr.as_ref()).to_string();
191        assert_eq!(sql_string, "42");
192
193        Ok(())
194    }
195}