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

   ### Describe the bug
   
   `format_string` panics for `%g` or `%G` of `0.0`, and prints 
`NaNe-2147483648` for `%e` of `0.0`. Spark prints `0.00000` and `0.000000e+00`.
   
   The exponent is `abs.log10().floor() as i32` 
([format_string.rs#L1743](https://github.com/apache/datafusion-spark/blob/d6d281904bc793d1f96eaa96cbbf5f2823146143/crates/spark/src/function/string/format_string.rs#L1743)).
 For `0.0` that is `-inf`, which casts to `i32::MIN`. `precision - 1 - 
exponent` then overflows 
([#L1766](https://github.com/apache/datafusion-spark/blob/d6d281904bc793d1f96eaa96cbbf5f2823146143/crates/spark/src/function/string/format_string.rs#L1766)),
 and `%e` computes `0.0 / 10^i32::MIN`, which is `0.0 / 0.0` = NaN 
([#L1788](https://github.com/apache/datafusion-spark/blob/d6d281904bc793d1f96eaa96cbbf5f2823146143/crates/spark/src/function/string/format_string.rs#L1788)).
   
   ### To Reproduce
   
   ```sql
   SELECT format_string('%g', 0.0::double);
   SELECT format_string('%e', 0.0::double);
   ```
   
   datafusion-spark returns:
   
   ```
   panicked at crates\spark\src\function\string\format_string.rs:1766:39:
   attempt to subtract with overflow
   
   NaNe-2147483648
   ```
   
   The panic is from a debug build. A default release build has no overflow 
check, so the subtraction wraps instead of panicking.
   
   ### Expected behavior
   
   As in Spark (Java `String.format`):
   
   ```
   0.00000
   0.000000e+00
   ```
   
   ### Spark versions
   
   Spark 3.5 and 4.x. Spark hands the arguments to `java.util.Formatter`, so 
both versions give the same result. Not run on a live Spark.
   
   ### datafusion-spark version
   
   main at d6d2819
   
   ### Additional context
   
   #39 is another `%g` difference in the same code. 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