Skip to content

Commit 864acfa

Browse files
ai: apply changes for #1006 (2 review threads)
Addresses: - #4418398193 at CHANGELOG.md:4 - #4418398199 at src/databricks/sql/parameters/native.py:673 Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
1 parent 72cdd60 commit 864acfa

3 files changed

Lines changed: 29 additions & 2 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 `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 (up to 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 so fractional digits are not lost (up to the 38-digit DECIMAL maximum; a value needing more digits than that still has its excess fractional digits rounded).
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

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

910
from databricks.sql.exc import NotSupportedError
1011

12+
logger = logging.getLogger(__name__)
13+
1114
if TYPE_CHECKING:
1215
# Type-annotation-only imports. ``from __future__ import annotations`` keeps
1316
# these out of the runtime import graph; the ``TSparkParameter*`` objects
@@ -672,6 +675,15 @@ def valid_scale_and_precision(self):
672675
def _cast_expr(self):
673676
if self.scale is not None and self.precision is not None:
674677
precision, scale = self._effective_precision_and_scale(self.precision, self.scale)
678+
if (precision, scale) != (self.precision, self.scale):
679+
logger.debug(
680+
"DecimalParameter declared DECIMAL(%d,%d) is bound as DECIMAL(%d,%d) "
681+
"to fit the value's digits (precision capped at 38)",
682+
self.precision,
683+
self.scale,
684+
precision,
685+
scale,
686+
)
675687
# SQL DECIMAL type syntax is DECIMAL(precision, scale)
676688
return self.CAST_EXPR.format(precision, scale)
677689
else:

‎tests/unit/test_parameters.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,23 @@ def test_decimal_explicit_precision_scale_cast_string(value, scale, precision, e
140140
assert p._cast_expr() == expected
141141

142142

143+
def test_decimal_widened_type_is_logged(caplog):
144+
"""A bound type that differs from the declared one is reported at debug level."""
145+
p = DecimalParameter(Decimal("123.4567"), scale=2, precision=10)
146+
with caplog.at_level("DEBUG", logger="databricks.sql.parameters.native"):
147+
assert p._cast_expr() == "DECIMAL(12,4)"
148+
assert "DECIMAL(10,2) is bound as DECIMAL(12,4)" in caplog.text
149+
150+
151+
def test_decimal_unchanged_type_is_not_logged(caplog):
152+
p = DecimalParameter(Decimal("123.45"), scale=2, precision=10)
153+
with caplog.at_level("DEBUG", logger="databricks.sql.parameters.native"):
154+
assert p._cast_expr() == "DECIMAL(10,2)"
155+
assert "is bound as" not in caplog.text
156+
157+
143158
def test_decimal_scale_greater_than_precision_legacy_swapped():
144-
"""Callers using the old (swapped) convention keep working, with a deprecation warning."""
159+
"""Callers using the old (swapped) convention keep working, with a FutureWarning."""
145160
with pytest.warns(FutureWarning, match="legacy"):
146161
p = DecimalParameter(Decimal("123.45"), scale=10, precision=2)
147162
assert p.precision == 10

0 commit comments

Comments
 (0)