From 24419863ecbe432728ba71a0324d5d298448aa93 Mon Sep 17 00:00:00 2001 From: Will Lachance Date: Fri, 29 Mar 2024 03:51:14 -0400 Subject: [PATCH] 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 --- redash/query_runner/query_results.py | 3 +++ tests/query_runner/test_query_results.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/redash/query_runner/query_results.py b/redash/query_runner/query_results.py index ea83e5089b..b22bac556f 100644 --- a/redash/query_runner/query_results.py +++ b/redash/query_runner/query_results.py @@ -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 diff --git a/tests/query_runner/test_query_results.py b/tests/query_runner/test_query_results.py index d9eb45ae42..756797492c 100644 --- a/tests/query_runner/test_query_results.py +++ b/tests/query_runner/test_query_results.py @@ -1,3 +1,4 @@ +import decimal import sqlite3 from unittest import TestCase @@ -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": []}