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

   ### Describe the bug
   
   
   `format_string` with `%e` / `%E` produces an invalid mantissa of `10.xxx` 
when the value rounds up to the next power of ten. The exponent is not 
incremented. Spark (Java `String.format`) carries into the exponent.
   
   The scientific branch computes the exponent from the unrounded value 
(`abs.log10().floor()`), divides to get the mantissa, and then lets 
`format!("{mantissa:.prec$}")` round it. When the mantissa rounds up to `10`, 
the exponent is already fixed, so the output is `10.000000e+00` instead of 
`1.000000e+01`. `%g` is not affected because it rounds before computing the 
exponent.
   
   ### To Reproduce
   
   ```sql
   SELECT format_string('%e', CAST(9.9999999 AS DOUBLE));
   ```
   
   datafusion-spark returns `10.000000e+00`.
   
   ### To Reproduce
   
   ```sql
   **1. Minimal query**
   
   
   SELECT format_string('%e', CAST(9.9999999 AS DOUBLE));
   
   
   datafusion-spark returns `10.000000e+00`. Java returns `1.000000e+01`.
   
   **2. As a failing sqllogictest** (from the repo root)
   
   Add this record to `crates/sqllogictest/test_files/string/format_string.slt`:
   
   
   query T
   SELECT format_string('%e', CAST(9.9999999 AS DOUBLE));
   ----
   1.000000e+01
   
   
   Then run:
   
   
   cargo test -p datafusion-spark-sqllogictest -- format_string
   
   
   The record fails in both modes with:
   
   
   -   1.000000e+01
   +   10.000000e+00
   
   
   **3. Compare against Java** (what Spark's `format_string` delegates to)
   
   
   public class Repro {
       public static void main(String[] args) {
           System.out.println(String.format("%e", 9.9999999));    // 
1.000000e+01
           System.out.println(String.format("%.0e", 9.9999999));  // 1e+01
           System.out.println(String.format("%.2e", 9.999));      // 1.00e+01
       }
   }
   
   
   Run it with `javac Repro.java && java Repro`.
   ```
   
   ### Expected behavior
   
   As in Spark (Java `String.format`):
   
   | Format | Value | datafusion-spark | Java |
   |---|---|---|---|
   | `%e` | `9.9999999` | `10.000000e+00` | `1.000000e+01` |
   | `%E` | `9.9999999` | `10.000000E+00` | `1.000000E+01` |
   | `%.2e` | `9.999` | `10.00e+00` | `1.00e+01` |
   | `%.0e` | `9.9999999` | `10e+00` | `1e+01` |
   | `%.0e` | `-9.9999999` | `-10e+00` | `-1e+01` |
   | `%.0e` | `99999.0` | `10e+04` | `1e+05` |
   | `%.1e` | `0.99999` | `10.0e-01` | `1.0e+00` |
   | `%#.0e` | `9.9999999` | `10.e+00` | `1.e+01` |
   
   Not affected: `%g` / `%G` (e.g. `%g` of `9.9999999` gives `10.0000`, and 
`%G` of `999999.5` gives `1.00000E+06`, both matching Java) and values that do 
not round up (`%.0e` of `1e-5` gives `1e-05`).
   
   
   ### Spark versions
   
   The expected values come from running `java.util.Formatter` directly 
(`String.format`), which is what Spark's `format_string` delegates to. I have 
not run them through PySpark.
   
   ### datafusion-spark version
   
   main at ab9d308
   
   ### Additional context
   
   Found while reviewing #416 and #427. It reproduces on `main` and with both 
PRs applied, so it is separate from both. Fixing it will likely mean rounding 
to the requested precision before computing the exponent, as the `%g` path 
already does.


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