-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(spark): Adds negative spark function #20006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SubhamSinghal
wants to merge
28
commits into
apache:main
Choose a base branch
from
SubhamSinghal:negative-spark-function
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+555
−2
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
ced42b5
Adds negative spark function
744ad95
Lint fix
897fbef
Update datafusion/spark/src/function/math/negative.rs
SubhamSinghal 34819a0
Update datafusion/spark/src/function/math/negative.rs
SubhamSinghal 76aa1d3
Apply suggestion
SubhamSinghal 2901ca5
Adds slt file
e227109
Use as_primitive fn
0dfa277
Use inline macro
e4232d4
Remove scalar macro
78f0159
Adds decimal32 and 64 types
e86473a
Fix lint
f53316f
Fix UT
30f000b
Update datafusion/spark/src/function/math/negative.rs
SubhamSinghal 89b3059
Update datafusion/spark/src/function/math/negative.rs
SubhamSinghal 77f63af
Update datafusion/spark/src/function/math/negative.rs
SubhamSinghal ddd30d0
Adds interval type
9191b3a
Lint fix
da620bb
Update datafusion/spark/src/function/math/negative.rs
SubhamSinghal 1b971c8
Update datafusion/spark/src/function/math/negative.rs
SubhamSinghal 99cba0a
Adds arg
cb79d2c
Add unsigned values for scalar
aaed6d2
Adds UT for interval and unsigned type
6af34c4
Update datafusion/spark/src/function/math/negative.rs
SubhamSinghal b743145
Fix UT
67c4b40
Remove any signature type
431ff5a
Fix interval type test cases
11296c4
Throw error for unsigned int
2e9b72e
Fix UT
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,293 @@ | ||
| // 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 arrow::array::types::*; | ||
| use arrow::array::*; | ||
| use arrow::datatypes::{DataType, IntervalDayTime, IntervalMonthDayNano, IntervalUnit}; | ||
| use bigdecimal::num_traits::WrappingNeg; | ||
| use datafusion_common::utils::take_function_args; | ||
| use datafusion_common::{Result, ScalarValue, not_impl_err}; | ||
| use datafusion_expr::{ | ||
| ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, | ||
| Volatility, | ||
| }; | ||
| use std::any::Any; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Spark-compatible `negative` expression | ||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#negative> | ||
| /// | ||
| /// Returns the negation of input (equivalent to unary minus) | ||
| /// Returns NULL if input is NULL, returns NaN if input is NaN. | ||
| /// | ||
| /// ANSI mode support see (<https://github.com/apache/datafusion/issues/20034>): | ||
| /// - Spark's ANSI-compliant dialect, when off (i.e. `spark.sql.ansi.enabled=false`), | ||
| /// negating the minimal value of a signed integer wraps around. | ||
| /// For example: negative(i32::MIN) returns i32::MIN (wraps instead of error). | ||
| /// This is the current implementation (legacy mode only). | ||
| /// - Spark's ANSI mode (when `spark.sql.ansi.enabled=true`) should throw an | ||
| /// ARITHMETIC_OVERFLOW error on integer overflow instead of wrapping. | ||
| /// This is not yet implemented - all operations currently use wrapping behavior. | ||
| /// | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct SparkNegative { | ||
| signature: Signature, | ||
| } | ||
|
|
||
| impl Default for SparkNegative { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl SparkNegative { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature { | ||
| type_signature: TypeSignature::OneOf(vec![ | ||
| // Numeric types: signed integers, float, decimals | ||
| TypeSignature::Numeric(1), | ||
| // Interval types: YearMonth, DayTime, MonthDayNano | ||
| TypeSignature::Uniform( | ||
| 1, | ||
| vec![ | ||
| DataType::Interval(IntervalUnit::YearMonth), | ||
| DataType::Interval(IntervalUnit::DayTime), | ||
| DataType::Interval(IntervalUnit::MonthDayNano), | ||
| ], | ||
| ), | ||
| ]), | ||
| volatility: Volatility::Immutable, | ||
| parameter_names: None, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for SparkNegative { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "negative" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
| Ok(arg_types[0].clone()) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| spark_negative(&args.args) | ||
| } | ||
| } | ||
|
|
||
| /// Core implementation of Spark's negative function | ||
| fn spark_negative(args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
| let [arg] = take_function_args("negative", args)?; | ||
|
|
||
| match arg { | ||
| ColumnarValue::Array(array) => match array.data_type() { | ||
| DataType::Null => Ok(arg.clone()), | ||
|
|
||
| // Signed integers - use wrapping negation (Spark legacy mode behavior) | ||
| DataType::Int8 => { | ||
| let array = array.as_primitive::<Int8Type>(); | ||
| let result: PrimitiveArray<Int8Type> = array.unary(|x| x.wrapping_neg()); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| DataType::Int16 => { | ||
| let array = array.as_primitive::<Int16Type>(); | ||
| let result: PrimitiveArray<Int16Type> = array.unary(|x| x.wrapping_neg()); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| DataType::Int32 => { | ||
| let array = array.as_primitive::<Int32Type>(); | ||
| let result: PrimitiveArray<Int32Type> = array.unary(|x| x.wrapping_neg()); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| DataType::Int64 => { | ||
| let array = array.as_primitive::<Int64Type>(); | ||
| let result: PrimitiveArray<Int64Type> = array.unary(|x| x.wrapping_neg()); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
|
|
||
| // Floating point - simple negation (no overflow possible) | ||
| DataType::Float16 => { | ||
| let array = array.as_primitive::<Float16Type>(); | ||
| let result: PrimitiveArray<Float16Type> = array.unary(|x| -x); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| DataType::Float32 => { | ||
| let array = array.as_primitive::<Float32Type>(); | ||
| let result: PrimitiveArray<Float32Type> = array.unary(|x| -x); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| DataType::Float64 => { | ||
| let array = array.as_primitive::<Float64Type>(); | ||
| let result: PrimitiveArray<Float64Type> = array.unary(|x| -x); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
|
|
||
| // Decimal types - wrapping negation | ||
| DataType::Decimal32(_, _) => { | ||
| let array = array.as_primitive::<Decimal32Type>(); | ||
| let result: PrimitiveArray<Decimal32Type> = | ||
| array.unary(|x| x.wrapping_neg()); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| DataType::Decimal64(_, _) => { | ||
| let array = array.as_primitive::<Decimal64Type>(); | ||
| let result: PrimitiveArray<Decimal64Type> = | ||
| array.unary(|x| x.wrapping_neg()); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| DataType::Decimal128(_, _) => { | ||
| let array = array.as_primitive::<Decimal128Type>(); | ||
| let result: PrimitiveArray<Decimal128Type> = | ||
| array.unary(|x| x.wrapping_neg()); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| DataType::Decimal256(_, _) => { | ||
| let array = array.as_primitive::<Decimal256Type>(); | ||
| let result: PrimitiveArray<Decimal256Type> = | ||
| array.unary(|x| x.wrapping_neg()); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
|
|
||
| // interval type | ||
| DataType::Interval(IntervalUnit::YearMonth) => { | ||
| let array = array.as_primitive::<IntervalYearMonthType>(); | ||
| let result: PrimitiveArray<IntervalYearMonthType> = | ||
| array.unary(|x| x.wrapping_neg()); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| DataType::Interval(IntervalUnit::DayTime) => { | ||
| let array = array.as_primitive::<IntervalDayTimeType>(); | ||
| let result: PrimitiveArray<IntervalDayTimeType> = | ||
| array.unary(|x| IntervalDayTime { | ||
| days: x.days.wrapping_neg(), | ||
| milliseconds: x.milliseconds.wrapping_neg(), | ||
| }); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| DataType::Interval(IntervalUnit::MonthDayNano) => { | ||
| let array = array.as_primitive::<IntervalMonthDayNanoType>(); | ||
| let result: PrimitiveArray<IntervalMonthDayNanoType> = | ||
| array.unary(|x| IntervalMonthDayNano { | ||
| months: x.months.wrapping_neg(), | ||
| days: x.days.wrapping_neg(), | ||
| nanoseconds: x.nanoseconds.wrapping_neg(), | ||
| }); | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
|
|
||
| dt => not_impl_err!("Not supported datatype for Spark negative(): {dt}"), | ||
| }, | ||
| ColumnarValue::Scalar(sv) => match sv { | ||
| ScalarValue::Null => Ok(arg.clone()), | ||
| _ if sv.is_null() => Ok(arg.clone()), | ||
|
|
||
| // Signed integers - wrapping negation | ||
| ScalarValue::Int8(Some(v)) => { | ||
| let result = v.wrapping_neg(); | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Int8(Some(result)))) | ||
| } | ||
| ScalarValue::Int16(Some(v)) => { | ||
| let result = v.wrapping_neg(); | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Int16(Some(result)))) | ||
| } | ||
| ScalarValue::Int32(Some(v)) => { | ||
| let result = v.wrapping_neg(); | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Int32(Some(result)))) | ||
| } | ||
| ScalarValue::Int64(Some(v)) => { | ||
| let result = v.wrapping_neg(); | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Int64(Some(result)))) | ||
| } | ||
|
|
||
| // Floating point - simple negation | ||
| ScalarValue::Float16(Some(v)) => { | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Float16(Some(-v)))) | ||
| } | ||
| ScalarValue::Float32(Some(v)) => { | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Float32(Some(-v)))) | ||
| } | ||
| ScalarValue::Float64(Some(v)) => { | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Float64(Some(-v)))) | ||
| } | ||
|
|
||
| // Decimal types - wrapping negation | ||
| ScalarValue::Decimal32(Some(v), precision, scale) => { | ||
| let result = v.wrapping_neg(); | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Decimal32( | ||
| Some(result), | ||
| *precision, | ||
| *scale, | ||
| ))) | ||
| } | ||
| ScalarValue::Decimal64(Some(v), precision, scale) => { | ||
| let result = v.wrapping_neg(); | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Decimal64( | ||
| Some(result), | ||
| *precision, | ||
| *scale, | ||
| ))) | ||
| } | ||
| ScalarValue::Decimal128(Some(v), precision, scale) => { | ||
| let result = v.wrapping_neg(); | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Decimal128( | ||
| Some(result), | ||
| *precision, | ||
| *scale, | ||
| ))) | ||
| } | ||
| ScalarValue::Decimal256(Some(v), precision, scale) => { | ||
| let result = v.wrapping_neg(); | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Decimal256( | ||
| Some(result), | ||
| *precision, | ||
| *scale, | ||
| ))) | ||
| } | ||
|
|
||
| //interval type | ||
| ScalarValue::IntervalYearMonth(Some(v)) => Ok(ColumnarValue::Scalar( | ||
| ScalarValue::IntervalYearMonth(Some(v.wrapping_neg())), | ||
| )), | ||
| ScalarValue::IntervalDayTime(Some(v)) => Ok(ColumnarValue::Scalar( | ||
| ScalarValue::IntervalDayTime(Some(IntervalDayTime { | ||
| days: v.days.wrapping_neg(), | ||
| milliseconds: v.milliseconds.wrapping_neg(), | ||
| })), | ||
| )), | ||
| ScalarValue::IntervalMonthDayNano(Some(v)) => Ok(ColumnarValue::Scalar( | ||
| ScalarValue::IntervalMonthDayNano(Some(IntervalMonthDayNano { | ||
| months: v.months.wrapping_neg(), | ||
| days: v.days.wrapping_neg(), | ||
| nanoseconds: v.nanoseconds.wrapping_neg(), | ||
| })), | ||
| )), | ||
|
|
||
| dt => not_impl_err!("Not supported datatype for Spark negative(): {dt}"), | ||
| }, | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.