From cd2aaa4bafd3179f796b2fdfd906f737254b35a6 Mon Sep 17 00:00:00 2001 From: Hao Jiang Date: Sat, 9 Dec 2017 23:01:15 +0900 Subject: [PATCH 1/2] Add: utf8 column name support for query result DS --- redash/query_runner/query_results.py | 16 +++++++++++++--- redash/tasks/queries.py | 2 ++ tests/query_runner/test_query_results.py | 13 +++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/redash/query_runner/query_results.py b/redash/query_runner/query_results.py index 4e4ed6137e..c6a5d6a1c3 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,22 @@ 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..a880c8e632 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..670e197f87 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'}] From c788cd5f960edf43edbe90a4a0d9d2206003c4e4 Mon Sep 17 00:00:00 2001 From: Hao Jiang Date: Sun, 10 Dec 2017 10:17:04 +0900 Subject: [PATCH 2/2] Fix: pep8 lint --- redash/query_runner/query_results.py | 3 ++- redash/tasks/queries.py | 2 +- tests/query_runner/test_query_results.py | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/redash/query_runner/query_results.py b/redash/query_runner/query_results.py index c6a5d6a1c3..ee0a64185c 100644 --- a/redash/query_runner/query_results.py +++ b/redash/query_runner/query_results.py @@ -1,4 +1,4 @@ -#-*- coding: utf-8 -*- +# -*- coding: utf-8 -*- import json import logging @@ -84,6 +84,7 @@ def is_ascii(string): return False return True + def fix_column_name(i, name): if is_ascii(name): return name.replace(':', '_').replace('.', '_').replace(' ', '_') diff --git a/redash/tasks/queries.py b/redash/tasks/queries.py index a880c8e632..6036d8b517 100644 --- a/redash/tasks/queries.py +++ b/redash/tasks/queries.py @@ -1,4 +1,4 @@ -#-*- coding: utf-8 -*- +# -*- coding: utf-8 -*- import json import logging diff --git a/tests/query_runner/test_query_results.py b/tests/query_runner/test_query_results.py index 670e197f87..156f82f3a7 100644 --- a/tests/query_runner/test_query_results.py +++ b/tests/query_runner/test_query_results.py @@ -1,4 +1,4 @@ -#-*- coding: utf-8 -*- +# -*- coding: utf-8 -*- import sqlite3 from unittest import TestCase