Skip to content

Conversation

@hsiang-c
Copy link
Contributor

@hsiang-c hsiang-c commented Oct 21, 2025

Which issue does this PR close?

Rationale for this change

  • Apache Spark's abs() behaves differently than DataFusion.
  • Apache Spark's ANSI-compliant dialect can be toggled by SparkConf spark.sql.ansi.enabled. When it is off, arithmetic overflow doesn't throw exception like DataFusion does.
  • Apache Spark's abs also supports ANSI interval types: YearMonthIntervalType and DayTimeIntervalType
  • DataFusion Comet can leverage it at fix: re-enable Comet abs datafusion-comet#2595

What changes are included in this PR?

  • Mimics Apache Spark v4.0.1 abs expression
  • DataFusion Spark's abs() API takes an additional flag fail_on_error if spark.sql.ansi.enabled=true at caller's side.

Are these changes tested?

  • unit tests
  • sqllogictest: test_files/spark/math/abs.slt

Are there any user-facing changes?

Yes, the abs function can be specified in the SQL.

  • Arithmetic overflow will be thrown when spark.sql.ansi.enabled=true
  • Support ANSI interval types: YearMonthIntervalType and DayTimeIntervalType

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) spark labels Oct 21, 2025
@hsiang-c
Copy link
Contributor Author

cc @comphead for code review, thank you.


# abs: signed int minimal values
query IIII
select abs(c1), abs(c2), abs(c3), abs(c4) from test_nullable_integer where dataset = 'mins'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering would be that easier to test like

query II
select abs(1), abs(-1)
----
1 1

?

instead of creating/dropping tables

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing abs(-128), abs(-32768) and abs(-2147483648) doesn't work b/c type widening.

Doing abs(-128::SMALLINT), abs(-32768::SMALLINT), abs(-2147483648::INT), abs(-9223372036854775808::BIGINT) throws casting error. For example, DataFusion error: Arrow error: Cast error: Can't cast value 128 to type Int8

Copy link
Contributor

@Jefffrey Jefffrey Oct 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bug in SQL parsing:

> select -128::tinyint;
Arrow error: Cast error: Can't cast value 128 to type Int8
> select (-128)::tinyint;
+-------------+
| Int64(-128) |
+-------------+
| -128        |
+-------------+
1 row(s) fetched.
Elapsed 0.003 seconds.
  • It casts the 128 value without accounting for the negative; might need to raise an issue for this? Not sure if this is intended behaviour or not

So can wrap it in parentheses to ensure the correct precedence, or alternatively use arrow_cast:

> select arrow_cast(-128, 'Int8');
+--------------------------------------+
| arrow_cast(Int64(-128),Utf8("Int8")) |
+--------------------------------------+
| -128                                 |
+--------------------------------------+
1 row(s) fetched.
Elapsed 0.007 seconds.

0 0
1 1
1 1
NULL NULL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its better to use inline query, in this example the answers and input data are out of order and it might be more difficult to read

## PySpark 3.5.5 Result: {"abs(INTERVAL '-1-1' YEAR TO MONTH)": 13, "typeof(abs(INTERVAL '-1-1' YEAR TO MONTH))": 'interval year to month', "typeof(INTERVAL '-1-1' YEAR TO MONTH)": 'interval year to month'}
#query
#SELECT abs(INTERVAL '-1-1' YEAR TO MONTH::interval year to month);
query error DataFusion error: This feature is not implemented: Unsupported SQL type INTERVAL YEAR TO MONTH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets create a github ticket to fix this and refer to it in the comments in addition to the error.

Looks like abs works with intervals for Spark only

Copy link
Contributor

@Jefffrey Jefffrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've raised a question on the epic on how we plan to support ansi mode:

#15914 (comment)

From what I see in this PR, this is done via an extra argument to abs (though I'm not sure it's actually being passed through coerce_types correctly 🤔 )

Comment on lines +172 to +176
let fail_on_error = if args.len() == 2 {
match &args[1] {
ColumnarValue::Scalar(ScalarValue::Boolean(Some(fail_on_error))) => {
*fail_on_error
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this branch actually being tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if branch is tested by flipping the fail_or_error flag in the with_fail_on_error helper function. However, you're right, I didn't test the else branch. I'll cover it when I refactor the unit tests.

Comment on lines +492 to +517
#[test]
fn test_abs_u8_scalar() {
with_fail_on_error(|fail_on_error| {
let args = ColumnarValue::Scalar(ScalarValue::UInt8(Some(u8::MAX)));
let fail_on_error_arg =
ColumnarValue::Scalar(ScalarValue::Boolean(Some(fail_on_error)));
match spark_abs(&[args, fail_on_error_arg]) {
Ok(ColumnarValue::Scalar(ScalarValue::UInt8(Some(result)))) => {
assert_eq!(result, u8::MAX);
Ok(())
}
Err(e) => {
if fail_on_error {
assert!(
e.to_string().contains("ARITHMETIC_OVERFLOW"),
"Error message did not match. Actual message: {e}"
);
Ok(())
} else {
panic!("Didn't expect error, but got: {e:?}")
}
}
_ => unreachable!(),
}
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test design is very confusing; we can't tell if a test case is meant to return Ok or Err as it automatically does the "correct" verification for each case. This automatic way of passing the test on Err should be switched so if we have a test case that is meant to return Err, that is the only thing we check for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jefffrey You're right, thanks for the feedback.

Comment on lines +158 to +165
fn arithmetic_overflow_error(from_type: &str) -> DataFusionError {
ArrowError(
Box::from(arrow::error::ArrowError::ComputeError(format!(
"arithmetic overflow from {from_type}",
))),
None,
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we should return a DataFusionError::Execution here instead of creating an arrow error and wrapping it in datafusion error, given the error occurs in our datafusion code

Comment on lines +119 to +121
let n = $ARRAY.as_any().downcast_ref::<$TYPE>();
match n {
Some(array) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if we unwrap n directly instead of matching on it, as we are guaranteed it would be of the correct array type; same goes for ansi_compute_op below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

spark sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants