Skip to content

Commit

Permalink
fix(clickhouse): Wrap subquery if it's LHS of IS NOT NULL (#4287)
Browse files Browse the repository at this point in the history
  • Loading branch information
VaggelisD authored Oct 24, 2024
1 parent c3c1997 commit 559e7bc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,3 +1212,12 @@ def replacepartition_sql(self, expression: exp.ReplacePartition) -> str:

def projectiondef_sql(self, expression: exp.ProjectionDef) -> str:
return f"PROJECTION {self.sql(expression.this)} {self.wrap(expression.expression)}"

def is_sql(self, expression: exp.Is) -> str:
is_sql = super().is_sql(expression)

if isinstance(expression.parent, exp.Not) and isinstance(expression.this, exp.Subquery):
# WHERE (SELECT ...) IS NOT NULL -> NOT ((SELECT ...) IS NULL)
is_sql = self.wrap(is_sql)

return is_sql
4 changes: 4 additions & 0 deletions tests/dialects/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@ def test_clickhouse(self):
"SELECT * FROM ABC WHERE hasAny(COLUMNS('.*field') APPLY(toUInt64) APPLY(to), (SELECT groupUniqArray(toUInt64(field))))"
)
self.validate_identity("SELECT col apply", "SELECT col AS apply")
self.validate_identity(
"SELECT name FROM data WHERE (SELECT DISTINCT name FROM data) IS NOT NULL",
"SELECT name FROM data WHERE NOT ((SELECT DISTINCT name FROM data) IS NULL)",
)

def test_clickhouse_values(self):
values = exp.select("*").from_(
Expand Down

0 comments on commit 559e7bc

Please sign in to comment.