Skip to content

Commit 702a550

Browse files
authored
Handle timedelta in query results (#6846)
1 parent 38a06c7 commit 702a550

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

redash/query_runner/query_results.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import decimal
23
import hashlib
34
import logging
@@ -108,6 +109,8 @@ def flatten(value):
108109
return json_dumps(value)
109110
elif isinstance(value, decimal.Decimal):
110111
return float(value)
112+
elif isinstance(value, datetime.timedelta):
113+
return str(value)
111114
else:
112115
return value
113116

tests/query_runner/test_query_results.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import decimal
23
import sqlite3
34
from unittest import TestCase
@@ -108,11 +109,11 @@ def test_creates_table_with_non_ascii_in_column_name(self):
108109
create_table(connection, table_name, results)
109110
connection.execute("SELECT 1 FROM query_123")
110111

111-
def test_creates_table_with_decimal_in_column_value(self):
112+
def test_creates_table_with_decimal_and_timedelta_in_column_value(self):
112113
connection = sqlite3.connect(":memory:")
113114
results = {
114-
"columns": [{"name": "test1"}, {"name": "test2"}],
115-
"rows": [{"test1": 1, "test2": decimal.Decimal(2)}],
115+
"columns": [{"name": "test1"}, {"name": "test2"}, {"name": "test3"}],
116+
"rows": [{"test1": 1, "test2": decimal.Decimal(2), "test3": datetime.timedelta(seconds=3)}],
116117
}
117118
table_name = "query_123"
118119
create_table(connection, table_name, results)

0 commit comments

Comments
 (0)