datafusion_comet_spark_expr/string_funcs/
string_space.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#![allow(deprecated)]
19
20use crate::kernels::strings::string_space;
21use arrow::record_batch::RecordBatch;
22use arrow_schema::{DataType, Schema};
23use datafusion::logical_expr::ColumnarValue;
24use datafusion_common::DataFusionError;
25use datafusion_physical_expr::PhysicalExpr;
26use std::{
27    any::Any,
28    fmt::{Display, Formatter},
29    hash::Hash,
30    sync::Arc,
31};
32
33#[derive(Debug, Eq)]
34pub struct StringSpaceExpr {
35    pub child: Arc<dyn PhysicalExpr>,
36}
37
38impl Hash for StringSpaceExpr {
39    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
40        self.child.hash(state);
41    }
42}
43
44impl PartialEq for StringSpaceExpr {
45    fn eq(&self, other: &Self) -> bool {
46        self.child.eq(&other.child)
47    }
48}
49
50impl StringSpaceExpr {
51    pub fn new(child: Arc<dyn PhysicalExpr>) -> Self {
52        Self { child }
53    }
54}
55
56impl Display for StringSpaceExpr {
57    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
58        write!(f, "StringSpace [child: {}] ", self.child)
59    }
60}
61
62impl PhysicalExpr for StringSpaceExpr {
63    fn as_any(&self) -> &dyn Any {
64        self
65    }
66
67    fn data_type(&self, input_schema: &Schema) -> datafusion_common::Result<DataType> {
68        match self.child.data_type(input_schema)? {
69            DataType::Dictionary(key_type, _) => {
70                Ok(DataType::Dictionary(key_type, Box::new(DataType::Utf8)))
71            }
72            _ => Ok(DataType::Utf8),
73        }
74    }
75
76    fn nullable(&self, _: &Schema) -> datafusion_common::Result<bool> {
77        Ok(true)
78    }
79
80    fn evaluate(&self, batch: &RecordBatch) -> datafusion_common::Result<ColumnarValue> {
81        let arg = self.child.evaluate(batch)?;
82        match arg {
83            ColumnarValue::Array(array) => {
84                let result = string_space(&array)?;
85
86                Ok(ColumnarValue::Array(result))
87            }
88            _ => Err(DataFusionError::Execution(
89                "StringSpace(scalar) should be fold in Spark JVM side.".to_string(),
90            )),
91        }
92    }
93
94    fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
95        vec![&self.child]
96    }
97
98    fn with_new_children(
99        self: Arc<Self>,
100        children: Vec<Arc<dyn PhysicalExpr>>,
101    ) -> datafusion_common::Result<Arc<dyn PhysicalExpr>> {
102        Ok(Arc::new(StringSpaceExpr::new(Arc::clone(&children[0]))))
103    }
104}