Skip to content

Commit

Permalink
Query Results: querying a column with a dictionary or array fails (ge…
Browse files Browse the repository at this point in the history
…tredash#3887)

* flatten lists and dicts to json to be used with SQLite's json_extract functions

* add test that verifies that lists and dicts are saved

* add test that verifies that lists and dicts are saved
  • Loading branch information
Omer Lachish authored and harveyrendell committed Nov 14, 2019
1 parent c4dbce7 commit 73e825f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion redash/query_runner/query_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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)


Expand Down
10 changes: 10 additions & 0 deletions tests/query_runner/test_query_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 73e825f

Please sign in to comment.