diff --git a/redash/query_runner/query_results.py b/redash/query_runner/query_results.py index b22bac556f..5bc0eed29a 100644 --- a/redash/query_runner/query_results.py +++ b/redash/query_runner/query_results.py @@ -1,3 +1,4 @@ +import datetime import decimal import hashlib import logging @@ -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 diff --git a/tests/query_runner/test_query_results.py b/tests/query_runner/test_query_results.py index 756797492c..1c7ff70cc8 100644 --- a/tests/query_runner/test_query_results.py +++ b/tests/query_runner/test_query_results.py @@ -1,3 +1,4 @@ +import datetime import decimal import sqlite3 from unittest import TestCase @@ -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)