vaibhawvipul commented on code in PR #471:
URL: https://github.com/apache/datafusion-comet/pull/471#discussion_r1615266173


##########
core/src/execution/datafusion/expressions/negative.rs:
##########
@@ -0,0 +1,397 @@
+// 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::hash::{Hash, Hasher};
+use std::sync::Arc;
+use arrow::compute::kernels::numeric::neg_wrapping;
+use arrow_array::RecordBatch;
+use arrow_schema::{DataType, Schema};
+use datafusion_common::{Result, ScalarValue};
+use datafusion::logical_expr::interval_arithmetic::Interval;
+use datafusion::logical_expr::ColumnarValue;
+use datafusion::physical_expr::PhysicalExpr;
+use datafusion_physical_expr::aggregate::utils::down_cast_any_ref;
+use datafusion_physical_expr::sort_properties::SortProperties;
+use crate::errors::CometError;
+use crate::execution::datafusion::expressions::cast::EvalMode;
+
+pub fn create_negate_expr(expr: Arc<dyn PhysicalExpr>, eval_mode: EvalMode) -> 
Result<Arc<dyn PhysicalExpr>, CometError> {
+    Ok(Arc::new(NegativeExpr::new(expr, eval_mode)))
+}
+
+/// Negative expression
+#[derive(Debug, Hash)]
+pub struct NegativeExpr {
+    /// Input expression
+    arg: Arc<dyn PhysicalExpr>,
+    eval_mode: EvalMode,
+}
+
+impl NegativeExpr {
+    /// Create new not expression
+    pub fn new(arg: Arc<dyn PhysicalExpr>, eval_mode: EvalMode) -> Self {
+        Self { arg, eval_mode }
+    }
+
+    /// Get the input expression
+    pub fn arg(&self) -> &Arc<dyn PhysicalExpr> {
+        &self.arg
+    }
+}
+
+impl std::fmt::Display for NegativeExpr {
+    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+        write!(f, "(- {})", self.arg)
+    }
+}
+
+impl PhysicalExpr for NegativeExpr {
+    /// Return a reference to Any that can be used for downcasting
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn data_type(&self, input_schema: &Schema) -> Result<DataType> {
+        self.arg.data_type(input_schema)
+    }
+
+    fn nullable(&self, input_schema: &Schema) -> Result<bool> {
+        self.arg.nullable(input_schema)
+    }
+
+    fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
+        let arg = self.arg.evaluate(batch)?;        
+        match arg {
+            ColumnarValue::Array(array) => {
+                if self.eval_mode == EvalMode::Ansi {
+                    match array.data_type() {
+                        DataType::Int8 => {
+                            let int_array = 
array.as_any().downcast_ref::<arrow::array::Int8Array>().unwrap();
+                            for i in 0..int_array.len() {
+                                if int_array.value(i) <= i8::MIN || 
int_array.value(i) >= i8::MAX {
+                                    return Err(CometError::ArithmeticOverflow{
+                                        from_type: "integer".to_string(),
+                                    }.into());
+                                }
+                            }
+                        }
+                        DataType::Int16 => {
+                            let int_array = 
array.as_any().downcast_ref::<arrow::array::Int16Array>().unwrap();
+                            for i in 0..int_array.len() {
+                                if int_array.value(i) <= i16::MIN || 
int_array.value(i) >= i16::MAX {
+                                    return Err(CometError::ArithmeticOverflow{
+                                        from_type: "integer".to_string(),
+                                    }.into());
+                                }
+                            }
+                        }
+                        DataType::Int32 => {
+                            let int_array = 
array.as_any().downcast_ref::<arrow::array::Int32Array>().unwrap();
+                            for i in 0..int_array.len() {
+                                if int_array.value(i) <= i32::MIN || 
int_array.value(i) >= i32::MAX {
+                                    return Err(CometError::ArithmeticOverflow{
+                                        from_type: "integer".to_string(),
+                                    }.into());
+                                }
+                            }
+                        }
+                        DataType::Int64 => {
+                            let int_array = 
array.as_any().downcast_ref::<arrow::array::Int64Array>().unwrap();
+                            for i in 0..int_array.len() {
+                                if int_array.value(i) <= i64::MIN || 
int_array.value(i) >= i64::MAX {
+                                    return Err(CometError::ArithmeticOverflow{
+                                        from_type: "integer".to_string(),
+                                    }.into());
+                                }
+                            }
+                        }
+                        DataType::UInt8 => {

Review Comment:
   Hmm... you are right. I added these checks in order to handle all the types 
present in `ScalarValue`. But it seems like we might not need unsigned types.



-- 
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