Copilot commented on code in PR #2394: URL: https://github.com/apache/auron/pull/2394#discussion_r3618345733
########## native-engine/datafusion-ext-exprs/src/spark_negative.rs: ########## @@ -0,0 +1,258 @@ +// 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, + fmt::{Debug, Display, Formatter}, + hash::{Hash, Hasher}, + sync::Arc, +}; + +use arrow::{ + array::{ArrayRef, Int8Array, Int16Array, Int32Array, Int64Array}, + datatypes::{DataType, Schema}, + record_batch::RecordBatch, +}; +use datafusion::{ + common::{Result, ScalarValue}, + logical_expr::ColumnarValue, + physical_expr::{PhysicalExpr, PhysicalExprRef}, + physical_plan::expressions::NegativeExpr, +}; +use datafusion_ext_commons::{df_execution_err, downcast_any}; + +pub struct SparkNegativeExpr { + expr: PhysicalExprRef, + ansi_enabled: bool, +} + +impl SparkNegativeExpr { + pub fn new(expr: PhysicalExprRef, ansi_enabled: bool) -> Self { + Self { expr, ansi_enabled } + } +} + +impl Display for SparkNegativeExpr { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "negative({})", self.expr) + } +} + +impl Debug for SparkNegativeExpr { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "negative({})", self.expr) + } +} + +impl PartialEq for SparkNegativeExpr { + fn eq(&self, other: &Self) -> bool { + self.expr.eq(&other.expr) && self.ansi_enabled == other.ansi_enabled + } +} + +impl Eq for SparkNegativeExpr {} + +impl Hash for SparkNegativeExpr { + fn hash<H: Hasher>(&self, state: &mut H) { + self.expr.hash(state); + self.ansi_enabled.hash(state); + } +} + +impl PhysicalExpr for SparkNegativeExpr { + fn as_any(&self) -> &dyn Any { + self + } + + fn data_type(&self, input_schema: &Schema) -> Result<DataType> { + self.expr.data_type(input_schema) + } + + fn nullable(&self, input_schema: &Schema) -> Result<bool> { + self.expr.nullable(input_schema) + } + + fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> { + if self.ansi_enabled + && matches!( + self.expr.data_type(batch.schema().as_ref())?, + DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 + ) + { + return checked_negate(self.expr.evaluate(batch)?); + } + + NegativeExpr::new(self.expr.clone()).evaluate(batch) + } + + fn children(&self) -> Vec<&PhysicalExprRef> { + vec![&self.expr] + } + + fn with_new_children( + self: Arc<Self>, + children: Vec<PhysicalExprRef>, + ) -> Result<PhysicalExprRef> { + Ok(Arc::new(Self::new(children[0].clone(), self.ansi_enabled))) + } + + fn fmt_sql(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "fmt_sql not used") + } +} + +fn checked_negate(value: ColumnarValue) -> Result<ColumnarValue> { + Ok(match value { + ColumnarValue::Scalar(scalar) => ColumnarValue::Scalar(checked_negate_scalar(scalar)?), + ColumnarValue::Array(array) => ColumnarValue::Array(checked_negate_array(array.as_ref())?), + }) +} + +fn checked_negate_scalar(scalar: ScalarValue) -> Result<ScalarValue> { + Ok(match scalar { + ScalarValue::Int8(Some(v)) => ScalarValue::Int8(Some(checked_negate_value(v)?)), + ScalarValue::Int16(Some(v)) => ScalarValue::Int16(Some(checked_negate_value(v)?)), + ScalarValue::Int32(Some(v)) => ScalarValue::Int32(Some(checked_negate_value(v)?)), + ScalarValue::Int64(Some(v)) => ScalarValue::Int64(Some(checked_negate_value(v)?)), + ScalarValue::Int8(None) => ScalarValue::Int8(None), + ScalarValue::Int16(None) => ScalarValue::Int16(None), + ScalarValue::Int32(None) => ScalarValue::Int32(None), + ScalarValue::Int64(None) => ScalarValue::Int64(None), + other => return df_execution_err!("unsupported ANSI negative data type: {other}"), + }) +} + +macro_rules! checked_negate_primitive_array { + ($array:expr, $array_ty:ty) => {{ + let array = downcast_any!($array, $array_ty)?; + let mut values = Vec::with_capacity(array.len()); + for value in array.iter() { + values.push(match value { + Some(v) => Some(checked_negate_value(v)?), + None => None, + }); + } + Ok(Arc::new(<$array_ty>::from(values)) as ArrayRef) + }}; +} + +fn checked_negate_array(array: &dyn arrow::array::Array) -> Result<ArrayRef> { + match array.data_type() { + DataType::Int8 => checked_negate_primitive_array!(array, Int8Array), + DataType::Int16 => checked_negate_primitive_array!(array, Int16Array), + DataType::Int32 => checked_negate_primitive_array!(array, Int32Array), + DataType::Int64 => checked_negate_primitive_array!(array, Int64Array), + other => df_execution_err!("unsupported ANSI negative data type: {other}"), + } +} + +fn checked_negate_value<T>(value: T) -> Result<T> +where + T: CheckedNeg, +{ + value.checked_neg().ok_or_else(|| { + datafusion::common::DataFusionError::Execution( + "[ARITHMETIC_OVERFLOW] arithmetic overflow in unary minus".to_string(), + ) + }) +} + +trait CheckedNeg { + fn checked_neg(self) -> Option<Self> + where + Self: Sized; +} + +macro_rules! impl_checked_neg { + ($($ty:ty),+) => { + $( + impl CheckedNeg for $ty { + fn checked_neg(self) -> Option<Self> { + <$ty>::checked_neg(self) + } + } + )+ + }; +} + +impl_checked_neg!(i8, i16, i32, i64); + +#[cfg(test)] +mod test { + use std::{error::Error, sync::Arc}; + + use arrow::{ + array::{ArrayRef, Float64Array, Int32Array, Int64Array}, + datatypes::{DataType, Field, Schema}, + record_batch::RecordBatch, + }; + use datafusion::physical_expr::{PhysicalExpr, expressions::Column}; + + use super::SparkNegativeExpr; + + #[test] + fn test_ansi_checked_negation() -> Result<(), Box<dyn Error>> { + let batch = batch( + DataType::Int64, + Arc::new(Int64Array::from(vec![Some(i64::MIN)])), + )?; + let expr = expression(true); + let err = expr.evaluate(&batch).expect_err("expected overflow"); + assert!(err.to_string().contains("[ARITHMETIC_OVERFLOW]")); + Ok(()) + } Review Comment: The implementation applies ANSI checked negation to Int8/Int16/Int32/Int64, but the unit test `test_ansi_checked_negation` only covers the Int64 overflow case. Adding Int8/Int16 (and Int32) overflow cases would better lock in the intended Spark-compatibility behavior for all types handled by the ANSI branch. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
