From 702a5506590f4af2776f5bafe5b25cd5d63a5430 Mon Sep 17 00:00:00 2001 From: Will Lachance Date: Wed, 3 Apr 2024 11:44:08 -0400 Subject: [PATCH] Handle timedelta in query results (#6846) --- redash/query_runner/query_results.py | 3 +++ tests/query_runner/test_query_results.py | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) 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)