diff --git a/redash/query_runner/query_results.py b/redash/query_runner/query_results.py index 4e4ed6137e..ee0a64185c 100644 --- a/redash/query_runner/query_results.py +++ b/redash/query_runner/query_results.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import json import logging import numbers @@ -76,14 +78,23 @@ def create_tables_from_query_ids(user, connection, query_ids): create_table(connection, table_name, results) -def fix_column_name(name): - return name.replace(':', '_').replace('.', '_').replace(' ', '_') +def is_ascii(string): + for c in string: + if ord(c) >= 128: + return False + return True + + +def fix_column_name(i, name): + if is_ascii(name): + return name.replace(':', '_').replace('.', '_').replace(' ', '_') + return 'column_{}'.format(i+1) def create_table(connection, table_name, query_results): columns = [column['name'] for column in query_results['columns']] - safe_columns = [fix_column_name(column) for column in columns] + safe_columns = [fix_column_name(i, column) for (i, column) in enumerate(columns)] column_list = ", ".join(safe_columns) create_table = "CREATE TABLE {table_name} ({column_list})".format( diff --git a/redash/tasks/queries.py b/redash/tasks/queries.py index c2c4322fc3..6036d8b517 100644 --- a/redash/tasks/queries.py +++ b/redash/tasks/queries.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import json import logging import signal diff --git a/tests/query_runner/test_query_results.py b/tests/query_runner/test_query_results.py index bde09e2280..156f82f3a7 100644 --- a/tests/query_runner/test_query_results.py +++ b/tests/query_runner/test_query_results.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import sqlite3 from unittest import TestCase @@ -57,6 +59,17 @@ def test_creates_table_with_spaces_in_column_name(self): create_table(connection, table_name, results) connection.execute('SELECT 1 FROM query_123') + def test_creates_table_with_non_ascii_in_column_name(self): + connection = sqlite3.connect(':memory:') + results = {'columns': [{'name': '日の일'}, {'name': 'normal'}], 'rows': [ + {'日の일': 1, 'normal': 2}]} + table_name = 'query_123' + expected_column_names = ['column_1', 'normal'] + create_table(connection, table_name, results) + table_description = connection.execute('SELECT * FROM query_123').description + actual_column_names = [x[0] for x in table_description] + self.assertEquals(actual_column_names, expected_column_names) + def test_loads_results(self): connection = sqlite3.connect(':memory:') rows = [{'test1': 1, 'test2': 'test'}, {'test1': 2, 'test2': 'test2'}]