Pastoray opened a new pull request, #435:
URL: https://github.com/apache/datafusion-spark/pull/435
<!--
SPDX-License-Identifier: Apache-2.0
SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under
one or more contributor license agreements
-->
## Which issue does this PR close?
- Closes #433.
## Rationale for this change
`format_string('%e', 9.9999999)` returns `10.000000e+00`, where Spark (Java
`String.format`) returns `1.000000e+01`.
The scientific branch takes the exponent from the unrounded value, then lets
the mantissa round to `precision` digits. When the mantissa rounds up to 10,
the exponent is already fixed, so the output has a two-digit mantissa. This
also happens with `%E`, negative values and other precisions (`%.0e` of
`99999.0` gives `10e+04`, Java gives `1e+05`). `%g` and `%G` are not affected,
because they round before picking the exponent.
## What changes are included in this PR?
In the scientific-notation branch of `format_float`, if the formatted
mantissa starts with `10`, the mantissa becomes `1.0` and the exponent goes up
by one. The mantissa has a single digit before rounding, so `10` can only
appear when it rounded up. No other path changes.
`format_decimal_scientific` (DECIMAL arguments) has the same shape, but
Spark rejects `%e` on DECIMAL (#97), so there is no Spark behavior to match and
I left it alone.
## Are these changes tested?
Yes, with new records in `string/format_string.slt`:
- `%e`, `%E`, `%.2e`, `%.0e` and `%.1e` on values that round up to the next
power of ten, including a negative value and one where the carry crosses zero
(`0.99999`).
- Values that must not change (`9.4`, `9.994`, `9.5`).
- An array input.
- `%g` and `%G`, to show they were never affected.
The expected values were checked against Spark 4.2.0 with the PySpark runner
in `crates/sqllogictest/scripts` (`pyspark_slt_runner.py` reports no
differences for `string/format_string.slt`), and against `java.util.Formatter`
directly. The new records fail on `main` and pass with this change.
`cargo test -p datafusion-spark-sqllogictest` passes (518 of 518), and
`cargo fmt` and `cargo clippy --all-targets --all-features --workspace -- -D
warnings` are clean.
`%#.0e` of `9.9999999` also needs the `#` flag fix in #427 to print
`1.e+01`. I did not add that record here so this PR does not depend on another
open PR.
## Are there any user-facing changes?
`%e` and `%E` now return Spark's result for values that round up to the next
power of ten. There are no API changes.
--
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]