Skip to content

Commit

Permalink
fix unique inner join claus (#2774)
Browse files Browse the repository at this point in the history
where there is a duplicate inner joins.
  • Loading branch information
Teccolat authored Sep 19, 2023
1 parent 767a828 commit d606f88
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions dataworkspace/dataworkspace/apps/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,17 @@ def get_sql_snippet_for_reference_dataset(schema, dataset, limit):
base_table = dataset.table_name
fields = f"{base_table}.*"
joins = ""
unique_tables = []
for col_def in col_defs:
fields = fields + ", " + f'{col_def["table"]}.{col_def["ext_field"]}'
joins = (
joins
+ f' INNER JOIN "{schema}"."{col_def["table"]}" ON {base_table}.{col_def["field"]}_id = {col_def["table"]}.id'
)
if col_def["table"] not in unique_tables:
unique_tables.append(col_def["table"])
joins = (
joins
+ f""" INNER JOIN "{schema}"."{col_def["table"]}" \
ON {base_table}.{col_def["field"]}_id = {col_def["table"]}.id"""
)

sql = f'SELECT {fields} FROM "{schema}"."{base_table}"{joins} LIMIT {limit}'
formated_sql = sqlparse.format(sql, reindent=True, keyword_case="upper")
return formated_sql
Expand Down

0 comments on commit d606f88

Please sign in to comment.