Skip to content

Commit 2441986

Browse files
authored
Handle decimal types in query results (#6837)
Since #6687, we don't serialize query results as JSON before returning them. This is fine, except for the query results data source which needs to pass the data directly to sqlite3, and doesn't know how to do that with the decimal types that are occasionally returned by (at least) the PostgreSQL query runner: https://www.psycopg.org/docs/faq.html#problems-with-type-conversions
1 parent c4d3d9c commit 2441986

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

redash/query_runner/query_results.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import decimal
12
import hashlib
23
import logging
34
import re
@@ -105,6 +106,8 @@ def fix_column_name(name):
105106
def flatten(value):
106107
if isinstance(value, (list, dict)):
107108
return json_dumps(value)
109+
elif isinstance(value, decimal.Decimal):
110+
return float(value)
108111
else:
109112
return value
110113

tests/query_runner/test_query_results.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import decimal
12
import sqlite3
23
from unittest import TestCase
34

@@ -107,6 +108,16 @@ def test_creates_table_with_non_ascii_in_column_name(self):
107108
create_table(connection, table_name, results)
108109
connection.execute("SELECT 1 FROM query_123")
109110

111+
def test_creates_table_with_decimal_in_column_value(self):
112+
connection = sqlite3.connect(":memory:")
113+
results = {
114+
"columns": [{"name": "test1"}, {"name": "test2"}],
115+
"rows": [{"test1": 1, "test2": decimal.Decimal(2)}],
116+
}
117+
table_name = "query_123"
118+
create_table(connection, table_name, results)
119+
connection.execute("SELECT 1 FROM query_123")
120+
110121
def test_shows_meaningful_error_on_failure_to_create_table(self):
111122
connection = sqlite3.connect(":memory:")
112123
results = {"columns": [], "rows": []}

0 commit comments

Comments
 (0)