Skip to content

Commit

Permalink
Vertica: prevent overwriting row data when duplicated column names exist
Browse files Browse the repository at this point in the history
  • Loading branch information
rui-sfds committed Sep 30, 2019
1 parent a8af968 commit 7175832
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions redash/query_runner/vertica.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,11 @@ def run_query(self, query, user):

# TODO - very similar to pg.py
if cursor.description is not None:
columns_data = [(i[0], i[1]) for i in cursor.description]
columns_data = [(i[0], types_map.get(i[1], None)) for i in cursor.description]

rows = [dict(zip((c[0] for c in columns_data), row)) for row in cursor.fetchall()]
columns = [{'name': col[0],
'friendly_name': col[0],
'type': types_map.get(col[1], None)} for col in columns_data]
columns = self.fetch_columns(column_data)

This comment has been minimized.

Copy link
@bennywij

bennywij Oct 1, 2019

Contributor

@rz269 -- can we simplify this to a single change, just adding this line and only this line --columns = self.fetch_columns(column_data) -- since fetch_columns will fix the naming problem for us. And making sure as you already did that rows is defined strictly after this.

def fetch_columns(self, columns):
column_names = []
duplicates_counter = 1
new_columns = []
for col in columns:
column_name = col[0]
if column_name in column_names:
column_name = "{}{}".format(column_name, duplicates_counter)
duplicates_counter += 1
column_names.append(column_name)
new_columns.append({'name': column_name,
'friendly_name': column_name,
'type': col[1]})
return new_columns

rows = [dict(zip(([c['name'] for c in columns]), r))
for i, r in enumerate(cursor.fetchall())]

data = {'columns': columns, 'rows': rows}
json_data = json_dumps(data)
Expand Down

0 comments on commit 7175832

Please sign in to comment.