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

snowflake: add option to lowercase column names #5657

Merged
merged 2 commits into from
Feb 1, 2022
Merged
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
46 changes: 34 additions & 12 deletions redash/query_runner/snowflake.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
try:
import snowflake.connector

enabled = True
except ImportError:
enabled = False
Expand Down Expand Up @@ -39,14 +40,27 @@ def configuration_schema(cls):
"type": "object",
"properties": {
"account": {"type": "string"},
"region": {"type": "string", "default": "us-west"},
"user": {"type": "string"},
"password": {"type": "string"},
"warehouse": {"type": "string"},
"database": {"type": "string"},
"region": {"type": "string", "default": "us-west"},
"lower_case_columns": {
"type": "boolean",
"title": "Lower Case Column Names in Results",
"default": False,
},
"host": {"type": "string"},
},
"order": ["account", "region", "user", "password", "warehouse", "database", "host"],
"order": [
"account",
"user",
"password",
"warehouse",
"database",
"region",
"host",
],
"required": ["user", "password", "account", "database", "warehouse"],
"secret": ["password"],
"extra_options": [
Expand Down Expand Up @@ -81,20 +95,28 @@ def _get_connection(self):
else:
host = "{}.snowflakecomputing.com".format(account)


connection = snowflake.connector.connect(
user = self.configuration["user"],
password = self.configuration["password"],
account = account,
region = region,
host = host
user=self.configuration["user"],
password=self.configuration["password"],
account=account,
region=region,
host=host,
)

return connection

def _column_name(self, column_name):
if self.configuration.get("lower_case_columns", False):
return column_name.lower()

return column_name

def _parse_results(self, cursor):
columns = self.fetch_columns(
[(i[0], self.determine_type(i[1], i[5])) for i in cursor.description]
[
(self._column_name(i[0]), self.determine_type(i[1], i[5]))
for i in cursor.description
]
)
rows = [
dict(zip((column["name"] for column in columns), row)) for row in cursor
Expand Down Expand Up @@ -137,10 +159,10 @@ def _run_query_without_warehouse(self, query):
cursor.close()
connection.close()

return data, error
return data, error

def _database_name_includes_schema(self):
return '.' in self.configuration.get('database')
return "." in self.configuration.get("database")

def get_schema(self, get_stats=False):
if self._database_name_includes_schema():
Expand Down