Conversation
…lues
DecimalParameter.calculate_decimal_cast_string inferred DECIMAL(precision,
scale) by string-splitting str(value) on ".", which miscounts two forms
that str(Decimal) legitimately produces:
- exponent notation (Decimal("1500").normalize() -> "1.5E+3"): the "E+3"
was counted as fractional digits, giving DECIMAL(5,4) for 1500, which
overflows (an error in ANSI mode, NULL in legacy mode).
- a leading minus sign: counted as an extra integer digit, so a negative
38-digit value produced DECIMAL(39,0), exceeding Databricks' max DECIMAL
precision of 38 and failing the cast.
Derive precision/scale from Decimal.as_tuple() (digits + exponent), ignoring
sign and display format. Existing cast-string tests are unchanged; added
regression cases for exponent notation and negative values.
Signed-off-by: Madan Kumar <winklemad@outlook.com>
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.
Description
DecimalParameter.calculate_decimal_cast_string(parameters/native.py) infers theDECIMAL(precision, scale)to cast a boundDecimalto by string-splittingstr(value)on".". That miscounts two formsstr(Decimal)legitimately produces, so the generated type can't hold the bound value.Exponent notation —
str()emitsEfor many magnitudes (e.g.Decimal("1500").normalize()isDecimal("1.5E+3");.scaleb(), arithmetic, and scientific input do the same):Negative values — the leading
-is counted as an integer digit, over-widening every negative by one precision digit, which becomes a hard failure at the boundary:Both the cast type and the bound literal come from the same value, so the server can't reconcile them — a perfectly valid
Decimalis rejected or silently nulled.Fix
Derive precision/scale from
Decimal.as_tuple()(sign, digits, exponent) — the exact numeric value — instead of the display string, so sign and exponent formatting no longer inflate the counts. Handles exponent notation, negatives, and sub-1 values uniformly.Tests
The six existing
test_calculate_decimal_cast_stringcases are unchanged and still pass. Added regression cases for exponent notation (Decimal("1500").normalize()→DECIMAL(4,0),Decimal("1e5")→DECIMAL(6,0)) and negatives (-9…9(38 digits) →DECIMAL(38,0),Decimal("-12.34")→DECIMAL(4,2)).tests/unit/test_parameters.pypasses (60).