MaxGekk opened a new pull request, #60769:
URL: https://github.com/apache/spark/pull/60769
### What changes were proposed in this pull request?
This PR adds two SQL functions, `try_abs(expr)` and `try_negative(expr)`,
with the semantics of `abs` and `negative`, except that they return NULL
instead of raising an overflow error, as `try_add`, `try_subtract` and
`try_multiply` do. The result is NULL for:
- the minimum value of `TINYINT`, `SMALLINT`, `INT` and `BIGINT`;
- the minimum value of a `YEAR TO MONTH` or `DAY TO SECOND` interval;
- for `try_negative`, a legacy `CALENDAR INTERVAL` with a field at its
minimum value (`abs` does not accept that type).
Like the other `try_` arithmetic functions, they return NULL there whatever
`spark.sql.ansi.enabled` is. For every other input they are `abs` and
`negative`: floating-point and decimal values cannot overflow (`Decimal.abs`
and negation keep the precision), and a string or NULL argument is coerced to
`DOUBLE` as for `abs`.
Implementation:
- `TryAbs` and `TryNegative` in `TryEval.scala` are `RuntimeReplaceable`
with `InheritAnalysisRules`, like `TryAdd`. Over a type that can overflow, the
replacement is `AbsOrNull` / `NegateOrNull`; otherwise it is `Abs` /
`UnaryMinus`.
- `AbsOrNull` and `NegateOrNull` (in `arithmetic.scala`, next to `Abs`)
extend `UnaryArithmeticOrNull`. They compare the value with the minimum of its
type, and return NULL there, in both the interpreted path and generated code.
They are `nullable`, check their input types, and are `NoThrow`, since they
cannot raise themselves.
- They do not use `TryEval`, so an error raised by the argument itself still
surfaces: under ANSI mode, `try_abs(2147483647 + 1)` raises
`ARITHMETIC_OVERFLOW` from the addition. `try_add` over intervals wraps the
expression in `TryEval`, which also hides such errors. A consequence: under
ANSI mode, `try_negative('abc')` raises `CAST_INVALID_INPUT` from the implicit
cast to `DOUBLE`, where `try_add(1, 'abc')` returns NULL.
- `functions.try_abs` / `functions.try_negative` in Scala, and the PySpark
(classic and Connect) functions, with docs. The SQL function reference and
`sql-ref-ansi-compliance.md` list both.
### Why are the changes needed?
Spark has `try_` counterparts for the arithmetic operators that raise an
overflow error under ANSI mode (`try_add`, `try_subtract`, `try_multiply`,
`try_divide`, `try_mod`, `try_sum`, `try_avg`), but not for the unary ones.
Under ANSI mode, `abs(x)` and `-x` raise `ARITHMETIC_OVERFLOW` for the minimum
value of an integral type, for example `abs(-2147483648)`, and the only ways to
get NULL instead were to turn ANSI mode off or to write `CASE WHEN x =
<minimum> THEN NULL ELSE abs(x) END` per type.
### Does this PR introduce _any_ user-facing change?
Yes, two new SQL functions and the matching Scala and Python APIs:
```sql
SELECT try_abs(-1), try_abs(-2147483648),
try_negative(-9223372036854775808L);
-- 1 NULL NULL
SELECT try_negative(INTERVAL '-178956970-8' YEAR TO MONTH);
-- NULL
```
### How was this patch tested?
- `TryEvalSuite`: `AbsOrNull` and `NegateOrNull` over the minimum, minimum +
1, maximum, -1, 0, 1 and NULL of each integral type and of both ANSI interval
types, in the interpreted, mutable-projection and unsafe-projection modes. Also
`try_negative` over calendar intervals with each field at its minimum, the
types that cannot overflow, the replacement with ANSI mode on and off, the
input type checks, and an argument whose own error is not caught.
- `ExprUtilsSuite`: both classes are listed as `NoThrow`, and they do not
raise over the extreme values of each integral type.
- `try_arithmetic.sql` (ANSI and non-ANSI golden files): literals and
column-read inputs of every integral type, intervals, decimals at `DECIMAL(38,
0)` extremes, doubles, strings, NULL, an argument that raises, and invalid
input types.
- `streaming/try-abs-negative.sql`: both functions in a streaming flow.
- `MathFunctionsSuite`: the SQL and DataFrame API forms with ANSI mode on
and off.
- `PlanGenerationTestSuite` / `ProtoToParsedPlanTestSuite`: new Connect
golden files.
- `ExpressionsSchemaSuite` (regenerated `sql-expression-schema.md`),
`ExpressionInfoSuite` (the examples), and the function parity test in
`DataFrameFunctionsSuite`.
- The PySpark doctests of both functions, in classic mode with ANSI mode on
and off.
```
build/sbt 'catalyst/testOnly *TryEvalSuite *ExprUtilsSuite' \
'sql/testOnly *MathFunctionsSuite *DataFrameFunctionsSuite
*ExpressionInfoSuite *ExpressionsSchemaSuite' \
'sql/testOnly *SQLQueryTestSuite -- -z try_arithmetic.sql' \
'sql/testOnly *StreamingSQLQueryTestSuite -- -z try-abs-negative' \
'connect-client-jvm/testOnly *PlanGenerationTestSuite -- -z try_abs -z
try_negative' \
'connect/testOnly *ProtoToParsedPlanTestSuite -- -z try_abs -z
try_negative'
```
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]