Repository navigation
fix(spark): format_string %s of a DECIMAL and %g like Java's Formatter - #26346
Draft
ozenyunus35 wants to merge 1 commit into
Draft
ozenyunus35 wants to merge 1 commit into
ozenyunus35 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
Spark's
format_stringuses Java'sFormatter, anddatafusion-sparkdiffered from it in two ways:%sof a DECIMAL printed the unscaled integer (123456for123.456), and%g/%Gdropped trailing zeros like C (1234.5), while Java always prints the requested number of significant digits (1234.50).What changes are included in this PR?
%s/%Sof aDecimal128orDecimal256prints the value with its scale in plain notation (123.456,0.50), through a smalldecimal_to_plain_stringhelper.%g/%Gno longer strip trailing zeros, for float and decimal arguments. Thestrip_trailing_0sflag andtrim_trailing_0swere only used for this, so they are removed.%gof0.0panicked with "attempt to subtract with overflow", because the exponent came fromlog10(0). Adecimal_exponenthelper returns 0 for zero, so%ggives0.00000and%egives0.000000e+00, as in Java.I checked every new expected value by running
String.formatwith the same pattern on JDK 21 (BigDecimalfor the decimal%scases).One thing I left as is: Java's
BigDecimal.toStringswitches to E notation for very small values (1E-7for0.0000001); this PR prints those in plain notation. I couldn't confirm what Spark's ownDecimal.toStringgives there without a Spark install, so I didn't guess.What is the testing strategy for this PR?
format_string.slt: the expectations the issue points at are updated (%s/%SofDecimal128andDecimal256,%g/%GofFloat64,Float32,Decimal128andDecimal256), and new cases cover%gon small, large, zero, negative and rounded values,%.3g,%.0g,%10.2g,%eof zero, and%s/%S/%10sof decimals. Onmainthe file fails (and the%gof0.0case panics).cargo test -p datafusion-spark --lib format_stringandcargo clippy -p datafusion-spark --all-targets -- -D warningspass.Are there any user-facing changes?
Yes:
format_string('%s', <decimal>)prints the decimal with its scale,%g/%Gkeep trailing zeros, and%gof zero no longer panics.I used an AI assistant to help draft parts of the code and tests; I reviewed the changes and ran the tests myself.