dtenedor opened a new pull request, #60734: URL: https://github.com/apache/spark/pull/60734
### What changes were proposed in this pull request? `to_char` (and its alias `to_varchar`) loses the digit when it formats a zero value with a format string that has no digits after the decimal point: ```sql SELECT to_char(0, '999'); -- ' ' (three spaces) SELECT to_char(0, '9'); -- ' ' ``` `ToNumberParser.formatSplitInputBeforeAndAfterDecimalPoint` strips all leading zeros from the integer part of the input. For a zero value that leaves no digits at all, and with no fractional digit positions in the format, nothing else renders the value. Formats with fractional digits are not affected: `to_char(0, '999.9')` already returns `' 0.0'`. This PR keeps a single zero digit for the integer part in that case, so `to_char(0, '999')` returns `' 0'` and `to_char(0, '9')` returns `'0'`. The result keeps the same width as before, and signs, `$`, `PR` and a trailing `.` keep their existing positions, for example `to_char(0, 'S999')` returns `' +0'`. It also adds the internal config `spark.sql.legacy.toChar.formatZeroAsBlank` (default `false`, binding policy `SESSION`) to restore the previous behavior. ### Why are the changes needed? It is a correctness bug. The `to_char` documentation says a sequence of `0` or `9` in the format string matches a sequence of digits in the input value and pads it with spaces to the length of the format sequence. Zero has one digit, so it should be rendered. PostgreSQL and Oracle also print the zero digit. Silently printing a blank makes a zero count or balance in a report indistinguishable from a missing value. ### Does this PR introduce _any_ user-facing change? Yes. `to_char(0, '999')` now returns `' 0'` instead of `' '`. Values other than zero, and formats with digits after the decimal point, are unchanged. The previous behavior can be restored by setting `spark.sql.legacy.toChar.formatZeroAsBlank` to `true`. The SQL migration guide is updated. ### How was this patch tested? Added a test to `StringExpressionsSuite` covering zero values with `9`, `G`, `$`, `S`, `MI`, `PR`, leading `0`, fractional and trailing-`.` formats, with and without the legacy config. `StringExpressionsSuite` and `ToNumberParserSuite` pass. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Isaac (Claude Opus 5.5) This pull request and its description were written by Isaac. -- 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]
