TechWizard9999 opened a new issue, #404:
URL: https://github.com/apache/datafusion-spark/issues/404

   ### Describe the bug
   
   `width_bucket` panics when `num_buckets` is `9223372036854775807` 
(`Long.MaxValue`). Spark returns NULL for that value.
   
   `(buckets + 1) as i64` overflows 
([width_bucket.rs#L252](https://github.com/apache/datafusion-spark/blob/d6d281904bc793d1f96eaa96cbbf5f2823146143/crates/spark/src/function/math/width_bucket.rs#L252)).
 The only NULL check before it is `buckets <= 0` 
([#L248](https://github.com/apache/datafusion-spark/blob/d6d281904bc793d1f96eaa96cbbf5f2823146143/crates/spark/src/function/math/width_bucket.rs#L248)).
 Spark also returns NULL for `Long.MaxValue`.
   
   ### To Reproduce
   
   ```sql
   SELECT width_bucket(5.0::double, 0.0::double, 10.0::double, 
9223372036854775807::bigint);
   ```
   
   datafusion-spark returns:
   
   ```
   panicked at crates\spark\src\function\math\width_bucket.rs:311:1:
   attempt to add with overflow
   ```
   
   The panic is from a debug build. A default release build has no overflow 
check, so the addition wraps instead of panicking. From reading the code (not 
run), it then returns `-9223372036854775808`.
   
   ### Expected behavior
   
   As in Spark:
   
   ```
   NULL
   ```
   
   `9223372036854775806` already works: `width_bucket(5.35, 0.024, 10.06, 
9223372036854775806)` returns `4894746858139549697`, as in Spark.
   
   ### Spark versions
   
   Spark 4.0.0. `WidthBucket.isNull` is true for `numBucket == Long.MaxValue`, 
so the result is NULL 
([mathExpressions.scala#L1762](https://github.com/apache/spark/blob/v4.0.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala#L1762)).
 `operators.sql.out` expects NULL for `width_bucket(5.35, 0.024, 10.06, 
9223372036854775807L)` 
([#L687](https://github.com/apache/spark/blob/v4.0.0/sql/core/src/test/resources/sql-tests/results/operators.sql.out#L687))
 and `4894746858139549697` for `9223372036854775807L - 1` 
([#L695](https://github.com/apache/spark/blob/v4.0.0/sql/core/src/test/resources/sql-tests/results/operators.sql.out#L695)).
 Not run on a live Spark.
   
   ### datafusion-spark version
   
   main at d6d2819
   
   ### Additional context
   
   The interval path has the same `num_buckets + 1` 
([#L417](https://github.com/apache/datafusion-spark/blob/d6d281904bc793d1f96eaa96cbbf5f2823146143/crates/spark/src/function/math/width_bucket.rs#L417)),
 so it is likely affected too. Found with the sqllogictest runner, in both 
simplify and evaluate modes.
   
   cc @andygrove
   


-- 
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]

Reply via email to