diff --git a/redash/query_runner/query_results.py b/redash/query_runner/query_results.py index 4da44b6fe2..910df7c9c1 100644 --- a/redash/query_runner/query_results.py +++ b/redash/query_runner/query_results.py @@ -72,6 +72,13 @@ def fix_column_name(name): return u'"{}"'.format(re.sub('[:."\s]', '_', name, flags=re.UNICODE)) +def flatten(value): + if isinstance(value, (list, dict)): + return json_dumps(value) + else: + return value + + def create_table(connection, table_name, query_results): try: columns = [column['name'] @@ -92,7 +99,7 @@ def create_table(connection, table_name, query_results): place_holders=','.join(['?'] * len(columns))) for row in query_results['rows']: - values = [row.get(column) for column in columns] + values = [flatten(row.get(column)) for column in columns] connection.execute(insert_template, values) diff --git a/tests/query_runner/test_query_results.py b/tests/query_runner/test_query_results.py index 6e453cbe8a..db047e587f 100644 --- a/tests/query_runner/test_query_results.py +++ b/tests/query_runner/test_query_results.py @@ -102,6 +102,16 @@ def test_loads_results(self): self.assertEquals( len(list(connection.execute('SELECT * FROM query_123'))), 2) + def test_loads_list_and_dict_results(self): + connection = sqlite3.connect(':memory:') + rows = [{'test1': [1,2,3]}, {'test2': {'a': 'b'}}] + results = {'columns': [{'name': 'test1'}, + {'name': 'test2'}], 'rows': rows} + table_name = 'query_123' + create_table(connection, table_name, results) + self.assertEquals( + len(list(connection.execute('SELECT * FROM query_123'))), 2) + class TestGetQuery(BaseTestCase): # test query from different account