Skip to content

Commit 074e873

Browse files
committed
fix(parameters): honor explicit decimal precision and scale
Render declared decimal types in SQL precision,scale order and preserve an explicit zero scale. Correct the parameter descriptions and document the compatibility change for callers that followed the reversed descriptions. Add regression coverage through actual parameter binding, preserving automatic sizing and the string payload. This addresses part of issue #901 only. Validated in Linux aarch64 Docker: 1250 ordinary unit tests and 351 subtests, a separate real-kernel test, formatting, type checks and package builds pass. Four existing tests remain skipped; no live Databricks/e2e tests were run. Signed-off-by: Lenin Mookiah <lenin.world@gmail.com>
1 parent b1978d4 commit 074e873

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎CHANGELOG.md‎

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

33
# Unreleased
4+
- Correct explicit `DecimalParameter` precision and scale ordering and honor a declared zero scale. Callers that followed the previously reversed parameter descriptions must use `precision` for total digits and `scale` for fractional digits. Explicit zero precision is now sent as declared instead of silently using automatic sizing.
45
- Add the `escape_catalog_name` connection parameter (default `False`) to opt in to escaping unescaped `_` in metadata catalog names on Thrift and kernel connections. Existing escapes and `%` wildcards are preserved.
56

67
# 4.6.0 (2026-09-24)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,9 @@ def __init__(
615615
616616
The `name` argument to this function would be `my_param`.
617617
:scale:
618-
The maximum precision (total number of digits) of the number between 1 and 38.
618+
The number of digits to the right of the decimal point, from 0 to precision.
619619
:precision:
620-
The number of digits to the right of the decimal point.
620+
The maximum precision (total number of digits) of the number between 1 and 38.
621621
"""
622622
self.value: decimal.Decimal = value
623623
self.name = name
@@ -638,8 +638,8 @@ def valid_scale_and_precision(self):
638638
return False
639639

640640
def _cast_expr(self):
641-
if self.scale and self.precision:
642-
return self.CAST_EXPR.format(self.scale, self.precision)
641+
if self.scale is not None and self.precision is not None:
642+
return self.CAST_EXPR.format(self.precision, self.scale)
643643
else:
644644
return self.calculate_decimal_cast_string(self.value)
645645

‎tests/unit/test_parameters.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,21 @@ def test_calculate_decimal_cast_string(value, expected):
107107
assert p._cast_expr() == expected
108108

109109

110+
@pytest.mark.parametrize(
111+
"value,precision,scale,expected",
112+
(
113+
(Decimal("123.45"), 10, 2, "DECIMAL(10,2)"),
114+
(Decimal("123"), 10, 0, "DECIMAL(10,0)"),
115+
(Decimal("123.45"), None, None, "DECIMAL(5,2)"),
116+
),
117+
)
118+
def test_decimal_parameter_explicit_type(value, precision, scale, expected):
119+
parameter = DecimalParameter(value, precision=precision, scale=scale)
120+
bound = parameter.as_tspark_param(named=False)
121+
assert bound.type == expected
122+
assert bound.value.stringValue == str(value)
123+
124+
110125
class Primitive(Enum):
111126
"""These are the inferrable types. This Enum is used for parametrized tests."""
112127

0 commit comments

Comments
 (0)