Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle decimal types in query results #6837

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that this fails without the above fix applied.


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