Skip to content

Commit

Permalink
Fix Decimal with scientific notation conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesmet authored and hashhar committed Apr 10, 2023
1 parent e10bc53 commit 32f2835
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions tests/integration/test_dbapi_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,25 @@ def test_decimal_query_param(trino_connection):
assert_cursor_description(cur, trino_type="decimal(10, 6)", precision=10, scale=6)


def test_decimal_scientific_notation_query_param(trino_connection):
cur = trino_connection.cursor()

cur.execute("SELECT ?", params=(Decimal('0E-10'),))
rows = cur.fetchall()

assert rows[0][0] == Decimal('0E-10')
assert_cursor_description(cur, trino_type="decimal(10, 10)", precision=10, scale=10)

# Ensure we don't convert to floats
assert Decimal('0.1') == Decimal('1E-1') != 0.1

cur.execute("SELECT ?", params=(Decimal('1E-1'),))
rows = cur.fetchall()

assert rows[0][0] == Decimal('1E-1')
assert_cursor_description(cur, trino_type="decimal(1, 1)", precision=1, scale=1)


def test_null_decimal(trino_connection):
cur = trino_connection.cursor()

Expand Down
2 changes: 1 addition & 1 deletion trino/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def _format_prepared_param(self, param):
return "UUID '%s'" % param

if isinstance(param, Decimal):
return "DECIMAL '%s'" % param
return "DECIMAL '%s'" % format(param, "f")

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

0 comments on commit 32f2835

Please sign in to comment.