|
| 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 | +//! Arithmetic expression builders |
| 19 | +
|
| 20 | +/// Macro to generate arithmetic expression builders that need eval_mode handling |
| 21 | +#[macro_export] |
| 22 | +macro_rules! arithmetic_expr_builder { |
| 23 | + ($builder_name:ident, $expr_type:ident, $operator:expr) => { |
| 24 | + pub struct $builder_name; |
| 25 | + |
| 26 | + impl $crate::execution::planner::traits::ExpressionBuilder for $builder_name { |
| 27 | + fn build( |
| 28 | + &self, |
| 29 | + spark_expr: &datafusion_comet_proto::spark_expression::Expr, |
| 30 | + input_schema: arrow::datatypes::SchemaRef, |
| 31 | + planner: &$crate::execution::planner::PhysicalPlanner, |
| 32 | + ) -> Result< |
| 33 | + std::sync::Arc<dyn datafusion::physical_expr::PhysicalExpr>, |
| 34 | + $crate::execution::operators::ExecutionError, |
| 35 | + > { |
| 36 | + let expr = $crate::extract_expr!(spark_expr, $expr_type); |
| 37 | + let eval_mode = |
| 38 | + $crate::execution::planner::from_protobuf_eval_mode(expr.eval_mode)?; |
| 39 | + planner.create_binary_expr( |
| 40 | + expr.left.as_ref().unwrap(), |
| 41 | + expr.right.as_ref().unwrap(), |
| 42 | + expr.return_type.as_ref(), |
| 43 | + $operator, |
| 44 | + input_schema, |
| 45 | + eval_mode, |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +use std::sync::Arc; |
| 53 | + |
| 54 | +use arrow::datatypes::SchemaRef; |
| 55 | +use datafusion::logical_expr::Operator as DataFusionOperator; |
| 56 | +use datafusion::physical_expr::PhysicalExpr; |
| 57 | +use datafusion_comet_proto::spark_expression::Expr; |
| 58 | +use datafusion_comet_spark_expr::{create_modulo_expr, create_negate_expr, EvalMode}; |
| 59 | + |
| 60 | +use crate::execution::{ |
| 61 | + expressions::extract_expr, |
| 62 | + operators::ExecutionError, |
| 63 | + planner::{ |
| 64 | + from_protobuf_eval_mode, traits::ExpressionBuilder, BinaryExprOptions, PhysicalPlanner, |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +/// Macro to define basic arithmetic builders that use eval_mode |
| 69 | +macro_rules! define_basic_arithmetic_builders { |
| 70 | + ($(($builder:ident, $expr_type:ident, $op:expr)),* $(,)?) => { |
| 71 | + $( |
| 72 | + arithmetic_expr_builder!($builder, $expr_type, $op); |
| 73 | + )* |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +define_basic_arithmetic_builders![ |
| 78 | + (AddBuilder, Add, DataFusionOperator::Plus), |
| 79 | + (SubtractBuilder, Subtract, DataFusionOperator::Minus), |
| 80 | + (MultiplyBuilder, Multiply, DataFusionOperator::Multiply), |
| 81 | + (DivideBuilder, Divide, DataFusionOperator::Divide), |
| 82 | +]; |
| 83 | + |
| 84 | +/// Builder for IntegralDivide expressions (requires special options) |
| 85 | +pub struct IntegralDivideBuilder; |
| 86 | + |
| 87 | +impl ExpressionBuilder for IntegralDivideBuilder { |
| 88 | + fn build( |
| 89 | + &self, |
| 90 | + spark_expr: &Expr, |
| 91 | + input_schema: SchemaRef, |
| 92 | + planner: &PhysicalPlanner, |
| 93 | + ) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> { |
| 94 | + let expr = extract_expr!(spark_expr, IntegralDivide); |
| 95 | + let eval_mode = from_protobuf_eval_mode(expr.eval_mode)?; |
| 96 | + planner.create_binary_expr_with_options( |
| 97 | + expr.left.as_ref().unwrap(), |
| 98 | + expr.right.as_ref().unwrap(), |
| 99 | + expr.return_type.as_ref(), |
| 100 | + DataFusionOperator::Divide, |
| 101 | + input_schema, |
| 102 | + BinaryExprOptions { |
| 103 | + is_integral_div: true, |
| 104 | + }, |
| 105 | + eval_mode, |
| 106 | + ) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/// Builder for Remainder expressions (uses special modulo function) |
| 111 | +pub struct RemainderBuilder; |
| 112 | + |
| 113 | +impl ExpressionBuilder for RemainderBuilder { |
| 114 | + fn build( |
| 115 | + &self, |
| 116 | + spark_expr: &Expr, |
| 117 | + input_schema: SchemaRef, |
| 118 | + planner: &PhysicalPlanner, |
| 119 | + ) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> { |
| 120 | + let expr = extract_expr!(spark_expr, Remainder); |
| 121 | + let eval_mode = from_protobuf_eval_mode(expr.eval_mode)?; |
| 122 | + let left = planner.create_expr(expr.left.as_ref().unwrap(), Arc::clone(&input_schema))?; |
| 123 | + let right = planner.create_expr(expr.right.as_ref().unwrap(), Arc::clone(&input_schema))?; |
| 124 | + |
| 125 | + let result = create_modulo_expr( |
| 126 | + left, |
| 127 | + right, |
| 128 | + expr.return_type |
| 129 | + .as_ref() |
| 130 | + .map(crate::execution::serde::to_arrow_datatype) |
| 131 | + .unwrap(), |
| 132 | + input_schema, |
| 133 | + eval_mode == EvalMode::Ansi, |
| 134 | + &planner.session_ctx().state(), |
| 135 | + ); |
| 136 | + result.map_err(|e| ExecutionError::GeneralError(e.to_string())) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/// Builder for UnaryMinus expressions (uses special negate function) |
| 141 | +pub struct UnaryMinusBuilder; |
| 142 | + |
| 143 | +impl ExpressionBuilder for UnaryMinusBuilder { |
| 144 | + fn build( |
| 145 | + &self, |
| 146 | + spark_expr: &Expr, |
| 147 | + input_schema: SchemaRef, |
| 148 | + planner: &PhysicalPlanner, |
| 149 | + ) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> { |
| 150 | + let expr = extract_expr!(spark_expr, UnaryMinus); |
| 151 | + let child = planner.create_expr(expr.child.as_ref().unwrap(), input_schema)?; |
| 152 | + let result = create_negate_expr(child, expr.fail_on_error); |
| 153 | + result.map_err(|e| ExecutionError::GeneralError(e.to_string())) |
| 154 | + } |
| 155 | +} |
0 commit comments