datafusion_comet_spark_expr/unbound.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
18use arrow_array::RecordBatch;
19use arrow_schema::{DataType, Schema};
20use datafusion::physical_plan::ColumnarValue;
21use datafusion_common::{internal_err, Result};
22use datafusion_physical_expr::PhysicalExpr;
23use std::{hash::Hash, sync::Arc};
24
25/// This is similar to `UnKnownColumn` in DataFusion, but it has data type.
26/// This is only used when the column is not bound to a schema, for example, the
27/// inputs to aggregation functions in final aggregation. In the case, we cannot
28/// bind the aggregation functions to the input schema which is grouping columns
29/// and aggregate buffer attributes in Spark (DataFusion has different design).
30/// But when creating certain aggregation functions, we need to know its input
31/// data types. As `UnKnownColumn` doesn't have data type, we implement this
32/// `UnboundColumn` to carry the data type.
33#[derive(Debug, Hash, PartialEq, Eq, Clone)]
34pub struct UnboundColumn {
35 name: String,
36 datatype: DataType,
37}
38
39impl UnboundColumn {
40 /// Create a new unbound column expression
41 pub fn new(name: &str, datatype: DataType) -> Self {
42 Self {
43 name: name.to_owned(),
44 datatype,
45 }
46 }
47
48 /// Get the column name
49 pub fn name(&self) -> &str {
50 &self.name
51 }
52}
53
54impl std::fmt::Display for UnboundColumn {
55 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
56 write!(f, "{}, datatype: {}", self.name, self.datatype)
57 }
58}
59
60impl PhysicalExpr for UnboundColumn {
61 /// Return a reference to Any that can be used for downcasting
62 fn as_any(&self) -> &dyn std::any::Any {
63 self
64 }
65
66 /// Get the data type of this expression, given the schema of the input
67 fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
68 Ok(self.datatype.clone())
69 }
70
71 /// Decide whether this expression is nullable, given the schema of the input
72 fn nullable(&self, _input_schema: &Schema) -> Result<bool> {
73 Ok(true)
74 }
75
76 /// Evaluate the expression
77 fn evaluate(&self, _batch: &RecordBatch) -> Result<ColumnarValue> {
78 internal_err!("UnboundColumn::evaluate() should not be called")
79 }
80
81 fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
82 vec![]
83 }
84
85 fn with_new_children(
86 self: Arc<Self>,
87 _children: Vec<Arc<dyn PhysicalExpr>>,
88 ) -> Result<Arc<dyn PhysicalExpr>> {
89 Ok(self)
90 }
91}