datafusion_physical_expr/expressions/
unknown_column.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//! UnKnownColumn expression
19
20use std::any::Any;
21use std::hash::{Hash, Hasher};
22use std::sync::Arc;
23
24use crate::PhysicalExpr;
25
26use arrow::{
27    datatypes::{DataType, Schema},
28    record_batch::RecordBatch,
29};
30use datafusion_common::{internal_err, Result};
31use datafusion_expr::ColumnarValue;
32
33#[derive(Debug, Clone, Eq)]
34pub struct UnKnownColumn {
35    name: String,
36}
37
38impl UnKnownColumn {
39    /// Create a new unknown column expression
40    pub fn new(name: &str) -> Self {
41        Self {
42            name: name.to_owned(),
43        }
44    }
45
46    /// Get the column name
47    pub fn name(&self) -> &str {
48        &self.name
49    }
50}
51
52impl std::fmt::Display for UnKnownColumn {
53    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
54        write!(f, "{}", self.name)
55    }
56}
57
58impl PhysicalExpr for UnKnownColumn {
59    /// Return a reference to Any that can be used for downcasting
60    fn as_any(&self) -> &dyn Any {
61        self
62    }
63
64    /// Get the data type of this expression, given the schema of the input
65    fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
66        Ok(DataType::Null)
67    }
68
69    /// Decide whether this expression is nullable, given the schema of the input
70    fn nullable(&self, _input_schema: &Schema) -> Result<bool> {
71        Ok(true)
72    }
73
74    /// Evaluate the expression
75    fn evaluate(&self, _batch: &RecordBatch) -> Result<ColumnarValue> {
76        internal_err!("UnKnownColumn::evaluate() should not be called")
77    }
78
79    fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
80        vec![]
81    }
82
83    fn with_new_children(
84        self: Arc<Self>,
85        _children: Vec<Arc<dyn PhysicalExpr>>,
86    ) -> Result<Arc<dyn PhysicalExpr>> {
87        Ok(self)
88    }
89}
90
91impl Hash for UnKnownColumn {
92    fn hash<H: Hasher>(&self, state: &mut H) {
93        self.name.hash(state);
94    }
95}
96
97impl PartialEq for UnKnownColumn {
98    fn eq(&self, _other: &Self) -> bool {
99        // UnknownColumn is not a valid expression, so it should not be equal to any other expression.
100        // See https://github.com/apache/datafusion/pull/11536
101        false
102    }
103}