Repository: tobymao/sqlglot

Issue:
Clickhouse: Ternary if parsing (2)
Minimal code to reproduce:
```python
import sqlglot

exp = "if(bar(col), sign > 0 ? foo() : 0, 1)"
sqlglot.parse(exp, read='clickhouse')
```
While trying to parse expression with clickhouse ternary if, get the following error:
```
sqlglot.errors.ParseError: Expecting ). Line 1, Col: 24.
  if(bar(col), sign > 0 ? foo() : 0, 1)
```

Observed on using ternary if inside if functiion.

Documentation:
https://clickhouse.com/docs/en/sql-reference/functions/conditional-functions


Discussion:

Resolving patch:
diff --git a/sqlglot/dialects/clickhouse.py b/sqlglot/dialects/clickhouse.py
--- a/sqlglot/dialects/clickhouse.py
+++ b/sqlglot/dialects/clickhouse.py
@@ -112,11 +112,8 @@ class Parser(parser.Parser):
             "format": lambda self: self._parse_id_var() if self._match(TokenType.FORMAT) else None,
         }
 
-        def _parse_expression(self, explicit_alias: bool = False) -> t.Optional[exp.Expression]:
-            return self._parse_alias(self._parse_ternary(), explicit=explicit_alias)
-
-        def _parse_ternary(self) -> t.Optional[exp.Expression]:
-            this = self._parse_conjunction()
+        def _parse_conjunction(self) -> t.Optional[exp.Expression]:
+            this = super()._parse_conjunction()
 
             if self._match(TokenType.PLACEHOLDER):
                 return self.expression(
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -2705,8 +2705,8 @@ def _parse_set_operations(self, this: t.Optional[exp.Expression]) -> t.Optional[
             expression=self._parse_set_operations(self._parse_select(nested=True)),
         )
 
-    def _parse_expression(self, explicit_alias: bool = False) -> t.Optional[exp.Expression]:
-        return self._parse_alias(self._parse_conjunction(), explicit=explicit_alias)
+    def _parse_expression(self) -> t.Optional[exp.Expression]:
+        return self._parse_alias(self._parse_conjunction())
 
     def _parse_conjunction(self) -> t.Optional[exp.Expression]:
         return self._parse_tokens(self._parse_equality, self.CONJUNCTION)
@@ -3073,7 +3073,7 @@ def _parse_primary(self) -> t.Optional[exp.Expression]:
             if query:
                 expressions = [query]
             else:
-                expressions = self._parse_csv(lambda: self._parse_expression(explicit_alias=True))
+                expressions = self._parse_csv(self._parse_expression)
 
             this = self._parse_query_modifiers(seq_get(expressions, 0))
 


Test patch:
diff --git a/tests/dialects/test_clickhouse.py b/tests/dialects/test_clickhouse.py
--- a/tests/dialects/test_clickhouse.py
+++ b/tests/dialects/test_clickhouse.py
@@ -120,6 +120,12 @@ def test_cte(self):
 
     def test_ternary(self):
         self.validate_all("x ? 1 : 2", write={"clickhouse": "CASE WHEN x THEN 1 ELSE 2 END"})
+        self.validate_all(
+            "IF(BAR(col), sign > 0 ? FOO() : 0, 1)",
+            write={
+                "clickhouse": "CASE WHEN BAR(col) THEN CASE WHEN sign > 0 THEN FOO() ELSE 0 END ELSE 1 END"
+            },
+        )
         self.validate_all(
             "x AND FOO() > 3 + 2 ? 1 : 2",
             write={"clickhouse": "CASE WHEN x AND FOO() > 3 + 2 THEN 1 ELSE 2 END"},
@@ -148,6 +154,8 @@ def test_ternary(self):
         self.assertIsInstance(nested_ternary.args["true"], exp.Literal)
         self.assertIsInstance(nested_ternary.args["false"], exp.Literal)
 
+        parse_one("a and b ? 1 : 2", read="clickhouse").assert_is(exp.If).this.assert_is(exp.And)
+
     def test_parameterization(self):
         self.validate_all(
             "SELECT {abc: UInt32}, {b: String}, {c: DateTime},{d: Map(String, Array(UInt8))}, {e: Tuple(UInt8, String)}",