mirror of
https://github.com/getredash/redash.git
synced 2025-12-19 17:37:19 -05:00
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
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import decimal
|
||||
import hashlib
|
||||
import logging
|
||||
import re
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import decimal
|
||||
import sqlite3
|
||||
from unittest import TestCase
|
||||
|
||||
@@ -107,6 +108,16 @@ class TestCreateTable(TestCase):
|
||||
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": []}
|
||||
|
||||
Reference in New Issue
Block a user