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

Add utf8 column name support for query result data source #2145

Closed
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
17 changes: 14 additions & 3 deletions redash/query_runner/query_results.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

import json
import logging
import numbers
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions redash/tasks/queries.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

import json
import logging
import signal
Expand Down
13 changes: 13 additions & 0 deletions tests/query_runner/test_query_results.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

import sqlite3
from unittest import TestCase

Expand Down Expand Up @@ -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'}]
Expand Down