comphead commented on code in PR #7599:
URL: https://github.com/apache/datafusion-comet/pull/7599#discussion_r4565706073
##########
native/spark-expr/src/math_funcs/negative.rs:
##########
@@ -447,4 +453,91 @@ mod tests {
"expected the ArrowError to pass through unchanged, got: {err:?}"
);
}
+
+ /// Negate `input` as a scalar and as a one element array, and return both
results.
+ fn negate_as_scalar_and_array(
+ input: &ScalarValue,
+ fail_on_error: bool,
+ ) -> (ScalarValue, ScalarValue) {
+ let ColumnarValue::Scalar(from_scalar) = eval_scalar(input.clone(),
fail_on_error).unwrap()
+ else {
+ panic!("expected scalar result")
+ };
+ let ColumnarValue::Array(from_array) =
+ eval_array(input.to_array().unwrap(), fail_on_error).unwrap()
+ else {
+ panic!("expected array result")
+ };
+ (
+ from_scalar,
+ ScalarValue::try_from_array(&from_array, 0).unwrap(),
+ )
+ }
+
+ /// In legacy mode the minimum of a signed integer type negates to itself,
as a scalar just
+ /// as in an array. ANSI mode still raises the overflow for the same
scalar.
+ fn assert_legacy_scalar_wraps_min(min: ScalarValue) {
+ let (from_scalar, from_array) = negate_as_scalar_and_array(&min,
false);
+ assert_eq!(from_array, min);
+ assert_eq!(from_scalar, min);
+ assert!(eval_scalar(min, true).is_err());
+ }
Review Comment:
Most of the new tests repeat existing coverage. The array half of
`assert_legacy_scalar_wraps_min` is `test_legacy_mode_wraps_min_values`, its
ANSI assertion is the loop at the end of `test_scalar_negation`, and the
ordinary and null cases are in `test_mixed_ordinary_values` and
`test_scalar_negation`. The new behavior fits in one loop next to the ANSI one
in `test_scalar_negation`:
```rust
for min in [
ScalarValue::Int8(Some(i8::MIN)),
ScalarValue::Int16(Some(i16::MIN)),
ScalarValue::Int32(Some(i32::MIN)),
ScalarValue::Int64(Some(i64::MIN)),
] {
let ColumnarValue::Scalar(res) = eval_scalar(min.clone(),
false).unwrap() else {
panic!("expected scalar result")
};
assert_eq!(res, min);
}
```
That would let the two helpers and five tests go. For coverage against Spark
itself, `arithmetic.sql` runs with ANSI off and `ConstantFolding` excluded, so
a case next to `-- unary negative with literal` should reach this scalar branch:
```sql
query
SELECT -(-128Y), -(-32768S), -(-2147483648), -(-9223372036854775808L)
```
`arithmetic_ansi.sql` already has the ANSI counterpart. I have not run this
query.
--
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]