Skip to content

Commit ff0a726

Browse files
Jesseharveyrendell
authored andcommitted
Snowflake: add option to lowercase column names (getredash#5657)
Ported from app.redash.io codebase. * Add option to lowercase column names * Black the file
1 parent 06d40b9 commit ff0a726

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

redash/query_runner/snowflake.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
try:
22
import snowflake.connector
3+
34
enabled = True
45
except ImportError:
56
enabled = False
@@ -39,14 +40,27 @@ def configuration_schema(cls):
3940
"type": "object",
4041
"properties": {
4142
"account": {"type": "string"},
42-
"region": {"type": "string", "default": "us-west"},
4343
"user": {"type": "string"},
4444
"password": {"type": "string"},
4545
"warehouse": {"type": "string"},
4646
"database": {"type": "string"},
47+
"region": {"type": "string", "default": "us-west"},
48+
"lower_case_columns": {
49+
"type": "boolean",
50+
"title": "Lower Case Column Names in Results",
51+
"default": False,
52+
},
4753
"host": {"type": "string"},
4854
},
49-
"order": ["account", "region", "user", "password", "warehouse", "database", "host"],
55+
"order": [
56+
"account",
57+
"user",
58+
"password",
59+
"warehouse",
60+
"database",
61+
"region",
62+
"host",
63+
],
5064
"required": ["user", "password", "account", "database", "warehouse"],
5165
"secret": ["password"],
5266
"extra_options": [
@@ -81,20 +95,28 @@ def _get_connection(self):
8195
else:
8296
host = "{}.snowflakecomputing.com".format(account)
8397

84-
8598
connection = snowflake.connector.connect(
86-
user = self.configuration["user"],
87-
password = self.configuration["password"],
88-
account = account,
89-
region = region,
90-
host = host
99+
user=self.configuration["user"],
100+
password=self.configuration["password"],
101+
account=account,
102+
region=region,
103+
host=host,
91104
)
92105

93106
return connection
94107

108+
def _column_name(self, column_name):
109+
if self.configuration.get("lower_case_columns", False):
110+
return column_name.lower()
111+
112+
return column_name
113+
95114
def _parse_results(self, cursor):
96115
columns = self.fetch_columns(
97-
[(i[0], self.determine_type(i[1], i[5])) for i in cursor.description]
116+
[
117+
(self._column_name(i[0]), self.determine_type(i[1], i[5]))
118+
for i in cursor.description
119+
]
98120
)
99121
rows = [
100122
dict(zip((column["name"] for column in columns), row)) for row in cursor
@@ -137,10 +159,10 @@ def _run_query_without_warehouse(self, query):
137159
cursor.close()
138160
connection.close()
139161

140-
return data, error
141-
162+
return data, error
163+
142164
def _database_name_includes_schema(self):
143-
return '.' in self.configuration.get('database')
165+
return "." in self.configuration.get("database")
144166

145167
def get_schema(self, get_stats=False):
146168
if self._database_name_includes_schema():

0 commit comments

Comments
 (0)