forked from apache/datafusion-comet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic.rs
More file actions
273 lines (241 loc) · 9.14 KB
/
arithmetic.rs
File metadata and controls
273 lines (241 loc) · 9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Arithmetic expression builders
use std::any::Any;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use arrow::datatypes::{DataType, Schema};
use arrow::record_batch::RecordBatch;
use datafusion::common::DataFusionError;
use datafusion::logical_expr::ColumnarValue;
use datafusion::physical_expr::PhysicalExpr;
use datafusion_comet_spark_expr::{QueryContext, SparkError, SparkErrorWithContext};
/// Wrapper expression that catches and wraps SparkError with QueryContext
/// for binary arithmetic operations.
#[derive(Debug)]
pub struct CheckedBinaryExpr {
/// The underlying physical expression (typically a ScalarFunctionExpr)
child: Arc<dyn PhysicalExpr>,
/// Optional query context to attach to errors
query_context: Option<Arc<QueryContext>>,
}
impl CheckedBinaryExpr {
pub fn new(child: Arc<dyn PhysicalExpr>, query_context: Option<Arc<QueryContext>>) -> Self {
Self {
child,
query_context,
}
}
}
impl Display for CheckedBinaryExpr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "CheckedBinaryExpr({})", self.child)
}
}
impl PartialEq for CheckedBinaryExpr {
fn eq(&self, other: &Self) -> bool {
self.child.eq(&other.child)
}
}
impl Eq for CheckedBinaryExpr {}
impl PartialEq<dyn Any> for CheckedBinaryExpr {
fn eq(&self, other: &dyn Any) -> bool {
other
.downcast_ref::<Self>()
.map(|x| self.eq(x))
.unwrap_or(false)
}
}
impl Hash for CheckedBinaryExpr {
fn hash<H: Hasher>(&self, state: &mut H) {
self.child.hash(state);
}
}
impl PhysicalExpr for CheckedBinaryExpr {
fn as_any(&self) -> &dyn Any {
self
}
fn fmt_sql(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.child.fmt_sql(f)
}
fn data_type(&self, input_schema: &Schema) -> datafusion::common::Result<DataType> {
self.child.data_type(input_schema)
}
fn nullable(&self, input_schema: &Schema) -> datafusion::common::Result<bool> {
self.child.nullable(input_schema)
}
fn evaluate(&self, batch: &RecordBatch) -> datafusion::common::Result<ColumnarValue> {
let result = self.child.evaluate(batch);
// If there's an error and we have query_context, wrap it
match result {
Err(DataFusionError::External(e)) if self.query_context.is_some() => {
if let Some(spark_err) = e.downcast_ref::<SparkError>() {
let wrapped = SparkErrorWithContext::with_context(
spark_err.clone(),
Arc::clone(self.query_context.as_ref().unwrap()),
);
Err(DataFusionError::External(Box::new(wrapped)))
} else {
Err(DataFusionError::External(e))
}
}
other => other,
}
}
fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
vec![&self.child]
}
fn with_new_children(
self: Arc<Self>,
children: Vec<Arc<dyn PhysicalExpr>>,
) -> datafusion::common::Result<Arc<dyn PhysicalExpr>> {
match children.len() {
1 => Ok(Arc::new(CheckedBinaryExpr::new(
Arc::clone(&children[0]),
self.query_context.clone(),
))),
_ => Err(DataFusionError::Internal(
"CheckedBinaryExpr should have exactly one child".to_string(),
)),
}
}
}
/// Macro to generate arithmetic expression builders that need eval_mode handling
#[macro_export]
macro_rules! arithmetic_expr_builder {
($builder_name:ident, $expr_type:ident, $operator:expr) => {
pub struct $builder_name;
impl $crate::execution::planner::expression_registry::ExpressionBuilder for $builder_name {
fn build(
&self,
spark_expr: &datafusion_comet_proto::spark_expression::Expr,
input_schema: arrow::datatypes::SchemaRef,
planner: &$crate::execution::planner::PhysicalPlanner,
) -> Result<
std::sync::Arc<dyn datafusion::physical_expr::PhysicalExpr>,
$crate::execution::operators::ExecutionError,
> {
let expr = $crate::extract_expr!(spark_expr, $expr_type);
let eval_mode =
$crate::execution::planner::from_protobuf_eval_mode(expr.eval_mode)?;
planner.create_binary_expr(
spark_expr, // Pass the full spark_expr for query_context lookup
expr.left.as_ref().unwrap(),
expr.right.as_ref().unwrap(),
expr.return_type.as_ref(),
$operator,
input_schema,
eval_mode,
)
}
}
};
}
use std::sync::Arc;
use arrow::datatypes::SchemaRef;
use datafusion::logical_expr::Operator as DataFusionOperator;
use datafusion_comet_proto::spark_expression::Expr;
use datafusion_comet_spark_expr::{create_modulo_expr, create_negate_expr, EvalMode};
use crate::execution::{
expressions::extract_expr,
operators::ExecutionError,
planner::{
expression_registry::ExpressionBuilder, from_protobuf_eval_mode, BinaryExprOptions,
PhysicalPlanner,
},
};
/// Macro to define basic arithmetic builders that use eval_mode
macro_rules! define_basic_arithmetic_builders {
($(($builder:ident, $expr_type:ident, $op:expr)),* $(,)?) => {
$(
arithmetic_expr_builder!($builder, $expr_type, $op);
)*
};
}
define_basic_arithmetic_builders![
(AddBuilder, Add, DataFusionOperator::Plus),
(SubtractBuilder, Subtract, DataFusionOperator::Minus),
(MultiplyBuilder, Multiply, DataFusionOperator::Multiply),
(DivideBuilder, Divide, DataFusionOperator::Divide),
];
/// Builder for IntegralDivide expressions (requires special options)
pub struct IntegralDivideBuilder;
impl ExpressionBuilder for IntegralDivideBuilder {
fn build(
&self,
spark_expr: &Expr,
input_schema: SchemaRef,
planner: &PhysicalPlanner,
) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> {
let expr = extract_expr!(spark_expr, IntegralDivide);
let eval_mode = from_protobuf_eval_mode(expr.eval_mode)?;
planner.create_binary_expr_with_options(
spark_expr, // Pass the full spark_expr for query_context lookup
expr.left.as_ref().unwrap(),
expr.right.as_ref().unwrap(),
expr.return_type.as_ref(),
DataFusionOperator::Divide,
input_schema,
BinaryExprOptions {
is_integral_div: true,
},
eval_mode,
)
}
}
/// Builder for Remainder expressions (uses special modulo function)
pub struct RemainderBuilder;
impl ExpressionBuilder for RemainderBuilder {
fn build(
&self,
spark_expr: &Expr,
input_schema: SchemaRef,
planner: &PhysicalPlanner,
) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> {
let expr = extract_expr!(spark_expr, Remainder);
let eval_mode = from_protobuf_eval_mode(expr.eval_mode)?;
let left = planner.create_expr(expr.left.as_ref().unwrap(), Arc::clone(&input_schema))?;
let right = planner.create_expr(expr.right.as_ref().unwrap(), Arc::clone(&input_schema))?;
let result = create_modulo_expr(
left,
right,
expr.return_type
.as_ref()
.map(crate::execution::serde::to_arrow_datatype)
.unwrap(),
input_schema,
eval_mode == EvalMode::Ansi,
&planner.session_ctx().state(),
);
result.map_err(|e| ExecutionError::GeneralError(e.to_string()))
}
}
/// Builder for UnaryMinus expressions (uses special negate function)
pub struct UnaryMinusBuilder;
impl ExpressionBuilder for UnaryMinusBuilder {
fn build(
&self,
spark_expr: &Expr,
input_schema: SchemaRef,
planner: &PhysicalPlanner,
) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> {
let expr = extract_expr!(spark_expr, UnaryMinus);
let child = planner.create_expr(expr.child.as_ref().unwrap(), input_schema)?;
let result = create_negate_expr(child, expr.fail_on_error);
result.map_err(|e| ExecutionError::GeneralError(e.to_string()))
}
}