Skip to content

Commit d578b17

Browse files
author
peco-engineer-bot[bot]
committed
ai: apply changes for #1006 (1 review thread)
Addresses: - #4418299211 at src/databricks/sql/parameters/native.py:641 Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
1 parent 5e09830 commit d578b17

3 files changed

Lines changed: 23 additions & 13 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). Passing `scale > precision` now raises `ValueError`, and a declared `scale` smaller than the value's own scale is widened so fractional digits are not lost.
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 `DeprecationWarning`; 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 so fractional digits are not lost.
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: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import datetime
44
import decimal
5+
import warnings
56
from enum import Enum, auto
67
from typing import Optional, Sequence, Any, TYPE_CHECKING
78

@@ -640,15 +641,21 @@ def __init__(
640641

641642
if self.scale is not None and self.precision is not None and self.scale > self.precision:
642643
# Earlier releases documented these arguments the other way round
643-
# (scale = total digits, precision = fractional digits). Fail loudly
644-
# instead of silently reinterpreting a call written that way.
645-
raise ValueError(
646-
"DecimalParameter scale ({}) must not exceed precision ({}). `precision` is "
647-
"the total number of digits and `scale` is the number of digits after the "
648-
"decimal point, as in SQL DECIMAL(precision, scale).".format(
649-
self.scale, self.precision
650-
)
644+
# (scale = total digits, precision = fractional digits). A valid
645+
# DECIMAL can never have scale > precision, so this can only be a call
646+
# written for the old convention: keep it working for now (swap the
647+
# arguments) but warn so callers migrate before this becomes an error.
648+
warnings.warn(
649+
"DecimalParameter received scale ({}) > precision ({}); interpreting the "
650+
"arguments using the legacy (swapped) convention as DECIMAL({},{}). "
651+
"`precision` is the total number of digits and `scale` is the number of "
652+
"digits after the decimal point, as in SQL DECIMAL(precision, scale). "
653+
"Swap your arguments; this will raise ValueError in a future "
654+
"release.".format(self.scale, self.precision, self.scale, self.precision),
655+
DeprecationWarning,
656+
stacklevel=2,
651657
)
658+
self.scale, self.precision = self.precision, self.scale
652659

653660
def valid_scale_and_precision(self):
654661
if (self.scale is None and self.precision is None) or (

‎tests/unit/test_parameters.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,13 @@ def test_decimal_explicit_precision_scale_cast_string(value, scale, precision, e
135135
assert p._cast_expr() == expected
136136

137137

138-
def test_decimal_scale_greater_than_precision_rejected():
139-
"""Callers using the old (swapped) argument convention must fail loudly."""
140-
with pytest.raises(ValueError, match="must not exceed precision"):
141-
DecimalParameter(Decimal("12.3456"), scale=4, precision=2)
138+
def test_decimal_scale_greater_than_precision_legacy_swapped():
139+
"""Callers using the old (swapped) convention keep working, with a deprecation warning."""
140+
with pytest.warns(DeprecationWarning, match="legacy"):
141+
p = DecimalParameter(Decimal("123.45"), scale=10, precision=2)
142+
assert p.precision == 10
143+
assert p.scale == 2
144+
assert p._cast_expr() == "DECIMAL(10,2)"
142145

143146

144147
class Primitive(Enum):

0 commit comments

Comments
 (0)