Repository: tobymao/sqlglot

Issue:
IS NOT NULL incorrect transformation
Wrong `IS NOT NULL` transormation

```python
from sqlglot import Dialects, parse_one

sql = "SELECT * FROM ABC WHERE toUInt8OrNull(num) IS NOT NULL"

print(parse_one(sql, read=Dialects.CLICKHOUSE).sql(dialect=Dialects.CLICKHOUSE))
```

Output with the version SQLGlot v25.27.0

```sql
SELECT * FROM ABC WHERE NOT toUInt8OrNull(num) IS NULL
```

Simmilar to https://github.com/tobymao/sqlglot/issues/4285


Discussion:

Patch:
diff --git a/sqlglot/dialects/clickhouse.py b/sqlglot/dialects/clickhouse.py
index fa86e3ac..25cc399d 100644
--- a/sqlglot/dialects/clickhouse.py
+++ b/sqlglot/dialects/clickhouse.py
@@ -1223,8 +1223,8 @@ class ClickHouse(Dialect):
         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)
+            if isinstance(expression.parent, exp.Not):
+                # value IS NOT NULL -> NOT (value IS NULL)
                 is_sql = self.wrap(is_sql)
 
             return is_sql


Test patch:
diff --git a/tests/dialects/test_clickhouse.py b/tests/dialects/test_clickhouse.py
index b3a2ddcf..a0efb54e 100644
--- a/tests/dialects/test_clickhouse.py
+++ b/tests/dialects/test_clickhouse.py
@@ -153,6 +153,10 @@ class TestClickhouse(Validator):
         self.validate_identity(
             "CREATE TABLE t (foo String CODEC(LZ4HC(9), ZSTD, DELTA), size String ALIAS formatReadableSize(size_bytes), INDEX idx1 a TYPE bloom_filter(0.001) GRANULARITY 1, INDEX idx2 a TYPE set(100) GRANULARITY 2, INDEX idx3 a TYPE minmax GRANULARITY 3)"
         )
+        self.validate_identity(
+            "SELECT (toUInt8('1') + toUInt8('2')) IS NOT NULL",
+            "SELECT NOT ((toUInt8('1') + toUInt8('2')) IS NULL)",
+        )
         self.validate_identity(
             "SELECT $1$foo$1$",
             "SELECT 'foo'",
diff --git a/tests/dialects/test_dialect.py b/tests/dialects/test_dialect.py
index 43644a0e..85402e2c 100644
--- a/tests/dialects/test_dialect.py
+++ b/tests/dialects/test_dialect.py
@@ -526,7 +526,7 @@ class TestDialect(Validator):
             write={
                 "": "SELECT NVL2(a, b, c)",
                 "bigquery": "SELECT CASE WHEN NOT a IS NULL THEN b ELSE c END",
-                "clickhouse": "SELECT CASE WHEN NOT a IS NULL THEN b ELSE c END",
+                "clickhouse": "SELECT CASE WHEN NOT (a IS NULL) THEN b ELSE c END",
                 "databricks": "SELECT NVL2(a, b, c)",
                 "doris": "SELECT CASE WHEN NOT a IS NULL THEN b ELSE c END",
                 "drill": "SELECT CASE WHEN NOT a IS NULL THEN b ELSE c END",
@@ -552,7 +552,7 @@ class TestDialect(Validator):
             write={
                 "": "SELECT NVL2(a, b)",
                 "bigquery": "SELECT CASE WHEN NOT a IS NULL THEN b END",
-                "clickhouse": "SELECT CASE WHEN NOT a IS NULL THEN b END",
+                "clickhouse": "SELECT CASE WHEN NOT (a IS NULL) THEN b END",
                 "databricks": "SELECT NVL2(a, b)",
                 "doris": "SELECT CASE WHEN NOT a IS NULL THEN b END",
                 "drill": "SELECT CASE WHEN NOT a IS NULL THEN b END",