Skip to content

Commit 6177ab4

Browse files
author
peco-engineer-bot[bot]
committed
ai: apply changes for #1006 (3 review threads)
Addresses: - #4418514374 at src/databricks/sql/parameters/native.py:648 - #4418514381 at src/databricks/sql/parameters/native.py:737 - #4418514389 at CHANGELOG.md:4 Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
1 parent 2518fc6 commit 6177ab4

3 files changed

Lines changed: 57 additions & 55 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Release History
22

33
# Unreleased
4-
- **Behaviour change:** `DecimalParameter` now binds `DECIMAL(precision, scale)`, matching SQL: `precision` is the total number of digits and `scale` the number of fractional digits (earlier docs described them the other way round). Calls written for the old convention (`scale > precision`, e.g. `scale=10, precision=2`) still bind `DECIMAL(10,2)` but emit a `FutureWarning`; swap your arguments, as this will raise `ValueError` in a future release. A declared `scale` equal to or less than `precision` is now interpreted with the SQL meaning, and the bound type is widened to fit the value: a declared `scale` smaller than the value's own scale is widened so fractional digits are not lost, and a declared `precision` whose integer room (`precision - scale`) is smaller than the value's integer digits is also widened, so such values are no longer rejected by the server as out of range (e.g. `DecimalParameter(Decimal("123456789012.5"), precision=10, scale=2)` now binds `DECIMAL(14,2)` instead of failing). If you relied on `precision` as a server-side range check, validate the value client-side. Widening is capped at the 38-digit DECIMAL maximum; a value needing more digits than that still has its excess fractional digits rounded.
4+
- **Behaviour change:** `DecimalParameter` now binds `DECIMAL(precision, scale)`, matching SQL: `precision` is the total number of digits and `scale` the number of fractional digits (earlier docs described them the other way round). Calls written for the old convention (`scale > precision`, e.g. `scale=10, precision=2`) still bind `DECIMAL(10,2)` but emit a `FutureWarning`; swap your arguments, as this will raise `ValueError` in a future release. A declared `scale` equal to or less than `precision` is now interpreted with the SQL meaning, and a declared `scale` smaller than the value's own scale is widened (growing the precision by the same amount) so fractional digits are not lost (e.g. `DecimalParameter(Decimal("123.4567"), precision=10, scale=2)` binds `DECIMAL(12,4)`). The declared integer room (`precision - scale`) is not widened, so a value with more integer digits is still rejected by the server as out of range. Widening is capped at the 38-digit DECIMAL maximum; a value needing more digits than that still has its excess fractional digits rounded. The constructor now also raises `ValueError` for inputs it previously accepted: a `precision` outside 1..38 (e.g. `scale=0, precision=0`, which used to fall back to an inferred type), a negative `scale`, and `bool` values for `scale` or `precision`.
55
- Preserve the originating session when re-fetching or concurrently polling async statements on the kernel backend.
66

77
# 4.8.0 (2026-10-24)

‎src/databricks/sql/parameters/native.py‎

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,13 @@ def __init__(
604604
If set, `scale` and `precision` must both be set. If neither is set, the value
605605
will be casted to the smallest possible DECIMAL type that can contain it.
606606
607-
When set, `precision` and `scale` declare a *minimum* DECIMAL(precision, scale)
608-
type: if the value needs more fractional or integer digits than declared, the
609-
bound type is widened to fit it (capped at precision 38) so no digits are lost.
610-
The value is never rounded or truncated to a narrower declared type; round the
611-
`Decimal` yourself (e.g. with `Decimal.quantize`) before binding if you need that.
607+
When set, `precision` and `scale` declare a DECIMAL(precision, scale) type. If
608+
the value has more fractional digits than `scale`, the bound scale (and so the
609+
precision) is widened to fit them (capped at precision 38) so they are not lost;
610+
the declared integer room (`precision - scale`) is kept, so a value with more
611+
integer digits than that is still rejected by the server as out of range.
612+
Round the `Decimal` yourself (e.g. with `Decimal.quantize`) before binding if
613+
you need it narrowed to the declared scale.
612614
613615
:value:
614616
The value to bind for this parameter. This will be casted to a DECIMAL.
@@ -630,9 +632,9 @@ def __init__(
630632
(unless the value needs more than 38 digits in total; the excess fractional
631633
digits are then rounded by the server).
632634
:precision:
633-
The minimum precision (total number of digits) of the number, between 1 and 38.
634-
If the value has more integer digits than `precision - scale` allows, or its
635-
scale is widened, the precision grows to fit it (up to 38).
635+
The precision (total number of digits) of the number, between 1 and 38.
636+
`precision - scale` integer digits are allowed; if the scale is widened to
637+
fit the value, the precision grows by the same amount (up to 38).
636638
"""
637639
self.value: decimal.Decimal = value
638640
self.name = name
@@ -645,7 +647,11 @@ def __init__(
645647
"as integers"
646648
)
647649

648-
if self.scale is not None and self.precision is not None and self.scale > self.precision:
650+
if (
651+
self.scale is not None
652+
and self.precision is not None
653+
and self.scale > self.precision
654+
):
649655
# Earlier releases documented these arguments the other way round
650656
# (scale = total digits, precision = fractional digits). A valid
651657
# DECIMAL can never have scale > precision, so this can only be a call
@@ -657,7 +663,9 @@ def __init__(
657663
"`precision` is the total number of digits and `scale` is the number of "
658664
"digits after the decimal point, as in SQL DECIMAL(precision, scale). "
659665
"Swap your arguments; this will raise ValueError in a future "
660-
"release.".format(self.scale, self.precision, self.scale, self.precision),
666+
"release.".format(
667+
self.scale, self.precision, self.scale, self.precision
668+
),
661669
# FutureWarning (not DeprecationWarning) so end users see it by
662670
# default, not only when the call originates from __main__.
663671
FutureWarning,
@@ -692,24 +700,13 @@ def _is_int(x):
692700

693701
def _cast_expr(self):
694702
if self.scale is not None and self.precision is not None:
695-
precision, scale = self._effective_precision_and_scale(self.precision, self.scale)
696-
if precision - scale > self.precision - self.scale:
697-
# The value has more integer digits than the declared type allows.
698-
# Earlier releases let the server reject this as out of range, so
699-
# surface it above DEBUG for callers relying on the declared type.
700-
logger.warning(
701-
"DecimalParameter value has more integer digits than declared "
702-
"DECIMAL(%d,%d) allows; it is bound as DECIMAL(%d,%d) instead of "
703-
"being rejected as out of range",
704-
self.precision,
705-
self.scale,
706-
precision,
707-
scale,
708-
)
709-
elif (precision, scale) != (self.precision, self.scale):
703+
precision, scale = self._effective_precision_and_scale(
704+
self.precision, self.scale
705+
)
706+
if (precision, scale) != (self.precision, self.scale):
710707
logger.debug(
711708
"DecimalParameter declared DECIMAL(%d,%d) is bound as DECIMAL(%d,%d) "
712-
"to fit the value's digits (precision capped at 38)",
709+
"to fit the value's fractional digits (precision capped at 38)",
713710
self.precision,
714711
self.scale,
715712
precision,
@@ -723,15 +720,15 @@ def _cast_expr(self):
723720
MAX_PRECISION = 38
724721

725722
def _effective_precision_and_scale(self, precision: int, scale: int):
726-
"""Widen a declared DECIMAL(precision, scale) so the bound value fits: the
727-
value's own scale is authoritative over an under-declared target scale, and
728-
the integer-digit room is widened to the value's integer digits when the
729-
declared ``precision - scale`` is too small (so the declared precision is not
730-
enforced as a range check). Extra integer room from the declared type is kept.
723+
"""Widen a declared DECIMAL(precision, scale) so the value's fractional digits
724+
fit: the value's own scale is authoritative over an under-declared target
725+
scale. The declared integer room (``precision - scale``) is never widened, so
726+
a value with more integer digits is still rejected by the server as out of
727+
range, as in earlier releases.
731728
732729
Exception: the total is capped at MAX_PRECISION (38) digits. If the value
733-
itself needs more than 38 digits (e.g. a scale over 38), the cast is clamped
734-
and the server rounds away the fractional digits that do not fit.
730+
needs more than that (e.g. a scale over 38), the cast is clamped and the
731+
server rounds away the fractional digits that do not fit.
735732
"""
736733
value = self.value
737734
if not isinstance(value, decimal.Decimal):
@@ -747,20 +744,22 @@ def _effective_precision_and_scale(self, precision: int, scale: int):
747744
return precision, scale
748745

749746
value_scale = max(-exponent, 0)
750-
value_int_digits = max(len(digits) + exponent, 0)
747+
declared_int_digits = precision - scale
748+
# Integer room is never widened past the declared one: an overflowing value
749+
# is left for the server to reject as out of range.
750+
value_int_digits = min(max(len(digits) + exponent, 0), declared_int_digits)
751751

752752
desired_scale = max(scale, value_scale)
753-
desired_int_digits = max(precision - scale, value_int_digits)
754753

755754
# Allocate the (at most MAX_PRECISION) digits by priority so that, when the
756755
# cap is hit, the value's own digits win over extra declared room
757756
# (similar to Spark's decimal precision adjustment):
758757
# 1. the value's integer digits, 2. the value's fractional digits,
759758
# 3. extra declared integer room, 4. extra declared fractional room.
760-
int_digits = min(value_int_digits, self.MAX_PRECISION)
759+
int_digits = value_int_digits
761760
effective_scale = min(value_scale, self.MAX_PRECISION - int_digits)
762761
remaining = self.MAX_PRECISION - int_digits - effective_scale
763-
extra_int = min(desired_int_digits - int_digits, remaining)
762+
extra_int = min(declared_int_digits - int_digits, remaining)
764763
int_digits += extra_int
765764
remaining -= extra_int
766765
effective_scale += min(desired_scale - effective_scale, remaining)

‎tests/unit/test_parameters.py‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,31 @@ def test_calculate_decimal_cast_string(value, expected):
114114
(Decimal("123"), 0, 10, "DECIMAL(10,0)"),
115115
# Value scale exceeds declared scale: scale widens, declared int room (8) is kept
116116
(Decimal("123.4567"), 2, 10, "DECIMAL(12,4)"),
117-
# Value has more integer digits than declared precision - scale allows
118-
(Decimal("123456789012.5"), 2, 10, "DECIMAL(14,2)"),
119-
# Positive exponent: 1E+3 needs 4 integer digits, no fractional digits
120-
(Decimal("1E+3"), 0, 2, "DECIMAL(4,0)"),
117+
# Value has more integer digits than declared precision - scale allows:
118+
# integer room is not widened, so the server rejects it as out of range
119+
(Decimal("123456789012.5"), 2, 10, "DECIMAL(10,2)"),
120+
# Positive exponent: 1E+3 needs 4 integer digits; declared room (2) is kept
121+
(Decimal("1E+3"), 0, 2, "DECIMAL(2,0)"),
122+
(Decimal("1E+3"), 0, 5, "DECIMAL(5,0)"),
121123
# Widened precision is capped at 38
122124
(Decimal("0.123456789012345678901234567890123456"), 2, 10, "DECIMAL(38,36)"),
123125
# Widened scale is capped at the (capped) precision
124126
(Decimal("0.1234567890123456789012345678901234567890"), 2, 10, "DECIMAL(38,38)"),
125-
# When the cap is hit, the value's integer digits win and extra declared
126-
# scale shrinks (31 int + 1 frac digits fit losslessly in DECIMAL(38,7))
127-
(Decimal("1234567890123456789012345678901.5"), 8, 10, "DECIMAL(38,7)"),
128-
# Value fills all 38 digits: declared extra scale is dropped entirely
129-
(Decimal("1234567890123456789012345678901234567.8"), 8, 10, "DECIMAL(38,1)"),
127+
# When the cap is hit, the value's fractional digits win over extra declared
128+
# integer room (1 int + 5 frac digits; 32 of the 36 declared int digits kept)
129+
(Decimal("1.12345"), 2, 38, "DECIMAL(38,5)"),
130+
# The value's integer digits win over its fractional digits at the cap
131+
# (36 int digits fill the declared room; the server rounds the excess scale)
132+
(Decimal("123456789012345678901234567890123456.12345"), 2, 38, "DECIMAL(38,2)"),
133+
# Integer overflow of the declared room is never widened, even near the cap
134+
(Decimal("1234567890123456789012345678901.5"), 8, 10, "DECIMAL(10,8)"),
130135
# NaN / Infinity leave the declared type untouched
131136
(Decimal("NaN"), 2, 10, "DECIMAL(10,2)"),
132137
(Decimal("Infinity"), 2, 10, "DECIMAL(10,2)"),
133138
# Non-Decimal values are inspected via str(value), which is what is sent
134139
(1.5, 2, 10, "DECIMAL(10,2)"),
135140
(12.3456, 2, 10, "DECIMAL(12,4)"),
136-
(123456789012, 0, 10, "DECIMAL(12,0)"),
141+
(123456789012, 0, 10, "DECIMAL(10,0)"),
137142
("123.4567", 2, 10, "DECIMAL(12,4)"),
138143
# Unparseable values fall back to the declared type
139144
("not-a-number", 2, 10, "DECIMAL(10,2)"),
@@ -153,14 +158,12 @@ def test_decimal_widened_type_is_logged(caplog):
153158
assert "DECIMAL(10,2) is bound as DECIMAL(12,4)" in caplog.text
154159

155160

156-
def test_decimal_widened_integer_room_is_warned(caplog):
157-
"""Widening the integer room (no longer an out-of-range error) is logged at WARNING."""
161+
def test_decimal_integer_room_is_not_widened(caplog):
162+
"""Too many integer digits keep the declared type so the server range check applies."""
158163
p = DecimalParameter(Decimal("123456789012.5"), scale=2, precision=10)
159-
with caplog.at_level("WARNING", logger="databricks.sql.parameters.native"):
160-
assert p._cast_expr() == "DECIMAL(14,2)"
161-
warnings = [r for r in caplog.records if r.levelname == "WARNING"]
162-
assert len(warnings) == 1
163-
assert "DECIMAL(10,2) allows; it is bound as DECIMAL(14,2)" in warnings[0].getMessage()
164+
with caplog.at_level("DEBUG", logger="databricks.sql.parameters.native"):
165+
assert p._cast_expr() == "DECIMAL(10,2)"
166+
assert "is bound as" not in caplog.text
164167

165168

166169
def test_decimal_widened_scale_only_is_not_warned(caplog):

0 commit comments

Comments
 (0)