andygrove commented on code in PR #616:
URL: https://github.com/apache/datafusion-comet/pull/616#discussion_r1672731981


##########
core/src/execution/datafusion/expressions/binary.rs:
##########
@@ -0,0 +1,211 @@
+// 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.
+
+use std::any::Any;
+use std::fmt::{Display, Formatter};
+use std::hash::Hasher;
+use std::sync::Arc;
+
+use arrow_array::{BooleanArray, RecordBatch};
+use arrow_schema::{DataType, Schema};
+use datafusion_common::{DataFusionError, Result, ScalarValue};
+use datafusion_expr::interval_arithmetic::Interval;
+use datafusion_expr::sort_properties::ExprProperties;
+use datafusion_expr::{ColumnarValue, Operator};
+use datafusion_physical_expr::expressions::{BinaryExpr, Literal};
+use datafusion_physical_expr_common::physical_expr::{down_cast_any_ref, 
PhysicalExpr};
+
+use crate::execution::datafusion::expressions::EvalMode;
+
+use super::arithmetic_overflow_error;
+
+#[derive(Debug, Hash, Clone)]
+pub struct CometBinaryExpr {
+    left: Arc<dyn PhysicalExpr>,
+    op: Operator,
+    right: Arc<dyn PhysicalExpr>,
+    eval_mode: EvalMode,
+    inner: Arc<BinaryExpr>,
+}
+
+impl CometBinaryExpr {
+    pub fn new(
+        left: Arc<dyn PhysicalExpr>,
+        op: Operator,
+        right: Arc<dyn PhysicalExpr>,
+        eval_mode: EvalMode,
+    ) -> Self {
+        Self {
+            left: Arc::clone(&left),
+            op,
+            right: Arc::clone(&right),
+            eval_mode,
+            inner: Arc::new(BinaryExpr::new(left, op, right)),
+        }
+    }
+
+    fn fail_on_overflow(&self, batch: &RecordBatch, result: &ColumnarValue) -> 
Result<()> {
+        if self.eval_mode == EvalMode::Ansi && self.op == Operator::Plus {
+            match result.data_type() {
+                DataType::Int8 | DataType::Int16 | DataType::Int32 | 
DataType::Int64 => {
+                    self.check_int_overflow(batch, result)?
+                }
+                _ => {}
+            }
+        }
+        Ok(())
+    }
+
+    fn check_int_overflow(&self, batch: &RecordBatch, result: &ColumnarValue) 
-> Result<()> {
+        let check_overflow_expr = Arc::new(BinaryExpr::new(
+            Arc::new(BinaryExpr::new(
+                Arc::new(BinaryExpr::new(
+                    self.left.clone(),
+                    Operator::BitwiseXor,
+                    self.inner.clone(),
+                )),
+                Operator::BitwiseAnd,
+                Arc::new(BinaryExpr::new(
+                    self.right.clone(),
+                    Operator::BitwiseXor,
+                    self.inner.clone(),
+                )),
+            )),
+            Operator::Lt,
+            Self::zero_literal(&result.data_type())?,
+        ));
+        match check_overflow_expr.evaluate(batch)? {

Review Comment:
   This is a very expensive way of implementing this. We don't need to use 
DataFusion to perform simple math operations when we can just implement this in 
Rust directly as we process the arrays. I think we can delegate to arrow-rs 
though and avoid all of this. As stated in another comment, I will provide an 
example later today.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to