Skip to content

Commit 32f2835

Browse files
mdesmethashhar
authored andcommitted
Fix Decimal with scientific notation conversion
1 parent e10bc53 commit 32f2835

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

tests/integration/test_dbapi_integration.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,25 @@ def test_decimal_query_param(trino_connection):
261261
assert_cursor_description(cur, trino_type="decimal(10, 6)", precision=10, scale=6)
262262

263263

264+
def test_decimal_scientific_notation_query_param(trino_connection):
265+
cur = trino_connection.cursor()
266+
267+
cur.execute("SELECT ?", params=(Decimal('0E-10'),))
268+
rows = cur.fetchall()
269+
270+
assert rows[0][0] == Decimal('0E-10')
271+
assert_cursor_description(cur, trino_type="decimal(10, 10)", precision=10, scale=10)
272+
273+
# Ensure we don't convert to floats
274+
assert Decimal('0.1') == Decimal('1E-1') != 0.1
275+
276+
cur.execute("SELECT ?", params=(Decimal('1E-1'),))
277+
rows = cur.fetchall()
278+
279+
assert rows[0][0] == Decimal('1E-1')
280+
assert_cursor_description(cur, trino_type="decimal(1, 1)", precision=1, scale=1)
281+
282+
264283
def test_null_decimal(trino_connection):
265284
cur = trino_connection.cursor()
266285

trino/dbapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def _format_prepared_param(self, param):
455455
return "UUID '%s'" % param
456456

457457
if isinstance(param, Decimal):
458-
return "DECIMAL '%s'" % param
458+
return "DECIMAL '%s'" % format(param, "f")
459459

460460
if isinstance(param, (bytes, bytearray)):
461461
return "X'%s'" % binascii.hexlify(param).decode("utf-8")

0 commit comments

Comments
 (0)