Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Results: querying a column with a dictionary or array fails #3887

Merged
merged 3 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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