Skip to main content

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