Skip to content

Commit

Permalink
Handle timedelta in query results (getredash#6846)
Browse files Browse the repository at this point in the history
  • Loading branch information
wlach authored Apr 3, 2024
1 parent 38a06c7 commit 702a550
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 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 datetime
import decimal
import hashlib
import logging
Expand Down Expand Up @@ -108,6 +109,8 @@ def flatten(value):
return json_dumps(value)
elif isinstance(value, decimal.Decimal):
return float(value)
elif isinstance(value, datetime.timedelta):
return str(value)
else:
return value

Expand Down
7 changes: 4 additions & 3 deletions tests/query_runner/test_query_results.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import decimal
import sqlite3
from unittest import TestCase
Expand Down Expand Up @@ -108,11 +109,11 @@ 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):
def test_creates_table_with_decimal_and_timedelta_in_column_value(self):
connection = sqlite3.connect(":memory:")
results = {
"columns": [{"name": "test1"}, {"name": "test2"}],
"rows": [{"test1": 1, "test2": decimal.Decimal(2)}],
"columns": [{"name": "test1"}, {"name": "test2"}, {"name": "test3"}],
"rows": [{"test1": 1, "test2": decimal.Decimal(2), "test3": datetime.timedelta(seconds=3)}],
}
table_name = "query_123"
create_table(connection, table_name, results)
Expand Down

0 comments on commit 702a550

Please sign in to comment.