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:
Will Lachance
2024-03-29 03:51:14 -04:00
committed by GitHub
parent c4d3d9c683
commit 24419863ec
2 changed files with 14 additions and 0 deletions

View File

@@ -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

View File

@@ -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": []}