Skip to content

Commit

Permalink
Handle decimal types in query results
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wlach committed Mar 28, 2024
1 parent 1672cd9 commit 593a828
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions redash/query_runner/query_results.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import decimal
import hashlib
import logging
import re
Expand Down Expand Up @@ -105,6 +106,8 @@ def fix_column_name(name):
def flatten(value):
if isinstance(value, (list, dict)):
return json_dumps(value)
elif isinstance(value, decimal.Decimal):
return float(value)
else:
return value

Expand Down
11 changes: 11 additions & 0 deletions tests/query_runner/test_query_results.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import decimal
import sqlite3
from unittest import TestCase

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

def test_creates_table_with_decimal_in_column_value(self):
connection = sqlite3.connect(":memory:")
results = {
"columns": [{"name": "test1"}, {"name": "test2"}],
"rows": [{"test1": 1, "test2": decimal.Decimal(2)}],
}
table_name = "query_123"
create_table(connection, table_name, results)
connection.execute("SELECT 1 FROM query_123")

def test_shows_meaningful_error_on_failure_to_create_table(self):
connection = sqlite3.connect(":memory:")
results = {"columns": [], "rows": []}
Expand Down

0 comments on commit 593a828

Please sign in to comment.