Skip to content

Commit

Permalink
fix: Do not generate DISTINCT keyword in FILTER
Browse files Browse the repository at this point in the history
  • Loading branch information
VaggelisD committed Sep 16, 2024
1 parent 4506b3b commit f621179
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4323,6 +4323,13 @@ def arrayagg_sql(self, expression: exp.ArrayAgg) -> str:
parent_cond = parent.expression.this
parent_cond.replace(parent_cond.and_(expression.this.is_(exp.null()).not_()))
else:
array_agg = f"{array_agg} FILTER(WHERE {self.sql(expression, 'this')} IS NOT NULL)"
# DISTINCT is already present in the agg function, do not propagate it to FILTER as well
this = expression.this
this_sql = (
self.expressions(this)
if isinstance(this, exp.Distinct)
else self.sql(expression, "this")
)
array_agg = f"{array_agg} FILTER(WHERE {this_sql} IS NOT NULL)"

return array_agg
2 changes: 2 additions & 0 deletions tests/dialects/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,8 @@ def test_snowflake(self):
write={
"spark": "SELECT COLLECT_LIST(DISTINCT a)",
"snowflake": "SELECT ARRAY_AGG(DISTINCT a)",
"duckdb": "SELECT ARRAY_AGG(DISTINCT a) FILTER(WHERE a IS NOT NULL)",
"presto": "SELECT ARRAY_AGG(DISTINCT a) FILTER(WHERE a IS NOT NULL)",
},
)
self.validate_all(
Expand Down

0 comments on commit f621179

Please sign in to comment.