Skip to content

Commit cc86832

Browse files
clean up _convert_decimal, introduce scale and precision as kwargs
Signed-off-by: varun-edachali-dbx <varun.edachali@databricks.com>
1 parent cc5203d commit cc86832

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

src/databricks/sql/backend/sea/utils/conversion.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,40 @@
1414
logger = logging.getLogger(__name__)
1515

1616

17+
def _convert_decimal(
18+
value: str, precision: Optional[int] = None, scale: Optional[int] = None
19+
) -> decimal.Decimal:
20+
"""
21+
Convert a string value to a decimal with optional precision and scale.
22+
23+
Args:
24+
value: The string value to convert
25+
precision: Optional precision (total number of significant digits) for the decimal
26+
scale: Optional scale (number of decimal places) for the decimal
27+
28+
Returns:
29+
A decimal.Decimal object with appropriate precision and scale
30+
"""
31+
32+
# First create the decimal from the string value
33+
result = decimal.Decimal(value)
34+
35+
# Apply scale (quantize to specific number of decimal places) if specified
36+
quantizer = None
37+
if scale is not None:
38+
quantizer = decimal.Decimal(f'0.{"0" * scale}')
39+
40+
# Apply precision (total number of significant digits) if specified
41+
context = None
42+
if precision is not None:
43+
context = decimal.Context(prec=precision)
44+
45+
if quantizer is not None:
46+
result = result.quantize(quantizer, context=context)
47+
48+
return result
49+
50+
1751
class SqlType:
1852
"""
1953
SQL type constants
@@ -72,13 +106,7 @@ class SqlTypeConverter:
72106
SqlType.LONG: lambda v: int(v),
73107
SqlType.FLOAT: lambda v: float(v),
74108
SqlType.DOUBLE: lambda v: float(v),
75-
SqlType.DECIMAL: lambda v, p=None, s=None: (
76-
decimal.Decimal(v).quantize(
77-
decimal.Decimal(f'0.{"0" * s}'), context=decimal.Context(prec=p)
78-
)
79-
if p is not None and s is not None
80-
else decimal.Decimal(v)
81-
),
109+
SqlType.DECIMAL: _convert_decimal,
82110
# Boolean type
83111
SqlType.BOOLEAN: lambda v: v.lower() in ("true", "t", "1", "yes", "y"),
84112
# Date/Time types
@@ -98,19 +126,17 @@ class SqlTypeConverter:
98126

99127
@staticmethod
100128
def convert_value(
101-
value: Any,
129+
value: str,
102130
sql_type: str,
103-
precision: Optional[int] = None,
104-
scale: Optional[int] = None,
131+
**kwargs,
105132
) -> Any:
106133
"""
107134
Convert a string value to the appropriate Python type based on SQL type.
108135
109136
Args:
110137
value: The string value to convert
111138
sql_type: The SQL type (e.g., 'int', 'decimal')
112-
precision: Optional precision for decimal types
113-
scale: Optional scale for decimal types
139+
**kwargs: Additional keyword arguments for the conversion function
114140
115141
Returns:
116142
The converted value in the appropriate Python type
@@ -127,6 +153,8 @@ def convert_value(
127153
converter_func = SqlTypeConverter.TYPE_MAPPING[sql_type]
128154
try:
129155
if sql_type == SqlType.DECIMAL:
156+
precision = kwargs.get("precision", None)
157+
scale = kwargs.get("scale", None)
130158
return converter_func(value, precision, scale)
131159
else:
132160
return converter_func(value)

0 commit comments

Comments
 (0)