|
| 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 | +//! String expression builders |
| 19 | +
|
| 20 | +use std::cmp::max; |
| 21 | +use std::sync::Arc; |
| 22 | + |
| 23 | +use arrow::datatypes::SchemaRef; |
| 24 | +use datafusion::common::ScalarValue; |
| 25 | +use datafusion::physical_expr::expressions::{LikeExpr, Literal}; |
| 26 | +use datafusion::physical_expr::PhysicalExpr; |
| 27 | +use datafusion_comet_proto::spark_expression::Expr; |
| 28 | +use datafusion_comet_spark_expr::{FromJson, RLike, SubstringExpr}; |
| 29 | + |
| 30 | +use crate::execution::{ |
| 31 | + expressions::extract_expr, |
| 32 | + operators::ExecutionError, |
| 33 | + planner::{expression_registry::ExpressionBuilder, PhysicalPlanner}, |
| 34 | + serde::to_arrow_datatype, |
| 35 | +}; |
| 36 | + |
| 37 | +/// Builder for Substring expressions |
| 38 | +pub struct SubstringBuilder; |
| 39 | + |
| 40 | +impl ExpressionBuilder for SubstringBuilder { |
| 41 | + fn build( |
| 42 | + &self, |
| 43 | + spark_expr: &Expr, |
| 44 | + input_schema: SchemaRef, |
| 45 | + planner: &PhysicalPlanner, |
| 46 | + ) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> { |
| 47 | + let expr = extract_expr!(spark_expr, Substring); |
| 48 | + let child = planner.create_expr(expr.child.as_ref().unwrap(), input_schema)?; |
| 49 | + // Spark Substring's start is 1-based when start > 0 |
| 50 | + let start = expr.start - i32::from(expr.start > 0); |
| 51 | + // substring negative len is treated as 0 in Spark |
| 52 | + let len = max(expr.len, 0); |
| 53 | + |
| 54 | + Ok(Arc::new(SubstringExpr::new( |
| 55 | + child, |
| 56 | + start as i64, |
| 57 | + len as u64, |
| 58 | + ))) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Builder for Like expressions |
| 63 | +pub struct LikeBuilder; |
| 64 | + |
| 65 | +impl ExpressionBuilder for LikeBuilder { |
| 66 | + fn build( |
| 67 | + &self, |
| 68 | + spark_expr: &Expr, |
| 69 | + input_schema: SchemaRef, |
| 70 | + planner: &PhysicalPlanner, |
| 71 | + ) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> { |
| 72 | + let expr = extract_expr!(spark_expr, Like); |
| 73 | + let left = planner.create_expr(expr.left.as_ref().unwrap(), Arc::clone(&input_schema))?; |
| 74 | + let right = planner.create_expr(expr.right.as_ref().unwrap(), input_schema)?; |
| 75 | + |
| 76 | + Ok(Arc::new(LikeExpr::new(false, false, left, right))) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Builder for Rlike (regex like) expressions |
| 81 | +pub struct RlikeBuilder; |
| 82 | + |
| 83 | +impl ExpressionBuilder for RlikeBuilder { |
| 84 | + fn build( |
| 85 | + &self, |
| 86 | + spark_expr: &Expr, |
| 87 | + input_schema: SchemaRef, |
| 88 | + planner: &PhysicalPlanner, |
| 89 | + ) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> { |
| 90 | + let expr = extract_expr!(spark_expr, Rlike); |
| 91 | + let left = planner.create_expr(expr.left.as_ref().unwrap(), Arc::clone(&input_schema))?; |
| 92 | + let right = planner.create_expr(expr.right.as_ref().unwrap(), input_schema)?; |
| 93 | + |
| 94 | + match right.as_any().downcast_ref::<Literal>().unwrap().value() { |
| 95 | + ScalarValue::Utf8(Some(pattern)) => Ok(Arc::new(RLike::try_new(left, pattern)?)), |
| 96 | + _ => Err(ExecutionError::GeneralError( |
| 97 | + "RLike only supports scalar patterns".to_string(), |
| 98 | + )), |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +pub struct FromJsonBuilder; |
| 104 | + |
| 105 | +impl ExpressionBuilder for FromJsonBuilder { |
| 106 | + fn build( |
| 107 | + &self, |
| 108 | + spark_expr: &Expr, |
| 109 | + input_schema: SchemaRef, |
| 110 | + planner: &PhysicalPlanner, |
| 111 | + ) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> { |
| 112 | + let expr = extract_expr!(spark_expr, FromJson); |
| 113 | + let child = planner.create_expr( |
| 114 | + expr.child.as_ref().ok_or_else(|| { |
| 115 | + ExecutionError::GeneralError("FromJson missing child".to_string()) |
| 116 | + })?, |
| 117 | + input_schema, |
| 118 | + )?; |
| 119 | + let schema = |
| 120 | + to_arrow_datatype(expr.schema.as_ref().ok_or_else(|| { |
| 121 | + ExecutionError::GeneralError("FromJson missing schema".to_string()) |
| 122 | + })?); |
| 123 | + Ok(Arc::new(FromJson::new(child, schema, &expr.timezone))) |
| 124 | + } |
| 125 | +} |
0 commit comments