From 4e69b73b0fdf365dfbf165c5d923e0c4138c5696 Mon Sep 17 00:00:00 2001 From: Levko Kravets Date: Thu, 21 Mar 2019 19:39:22 +0200 Subject: [PATCH] [Bug fix] Query Results fails to use query which has double quotes in column names (#3618) --- redash/query_runner/query_results.py | 2 +- tests/query_runner/test_query_results.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/redash/query_runner/query_results.py b/redash/query_runner/query_results.py index 8a80b5b072..2e8b95432f 100644 --- a/redash/query_runner/query_results.py +++ b/redash/query_runner/query_results.py @@ -73,7 +73,7 @@ def create_tables_from_query_ids(user, connection, query_ids, cached_query_ids=[ def fix_column_name(name): - return u'"{}"'.format(name.replace(':', '_').replace('.', '_').replace(' ', '_')) + return u'"{}"'.format(re.sub('[:."\s]', '_', name, flags=re.UNICODE)) def create_table(connection, table_name, query_results): diff --git a/tests/query_runner/test_query_results.py b/tests/query_runner/test_query_results.py index 80649c5b9e..6e453cbe8a 100644 --- a/tests/query_runner/test_query_results.py +++ b/tests/query_runner/test_query_results.py @@ -34,6 +34,14 @@ def test_creates_table_with_colons_in_column_name(self): create_table(connection, table_name, results) connection.execute('SELECT 1 FROM query_123') + def test_creates_table_with_double_quotes_in_column_name(self): + connection = sqlite3.connect(':memory:') + results = {'columns': [{'name': 'ga:newUsers'}, { + 'name': '"test2"'}], 'rows': [{'ga:newUsers': 123, '"test2"': 2}]} + table_name = 'query_123' + create_table(connection, table_name, results) + connection.execute('SELECT 1 FROM query_123') + def test_creates_table(self): connection = sqlite3.connect(':memory:') results = {'columns': [{'name': 'test1'},