Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(clickhouse): parse ternary operator #1603

Merged
merged 5 commits into from
May 12, 2023
Merged

Conversation

georgesittas
Copy link
Collaborator

Fixes #1602

@@ -2985,8 +2999,18 @@ def _parse_column(self) -> t.Optional[exp.Expression]:
field = self._parse_types()
if not field:
self.raise_error("Expected type")
elif op:
elif op and self._curr:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without self._curr here we might raise an index error in the self._advance call below (happened earlier).

Comment on lines 3008 to 3009
self._prev.token_type != TokenType.STRING
or self._match_set(self.TERNARY_OPERATOR_TOKENS)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the prev token is a STRING we can't immediately disambiguate between a JSONBContains expression (x ? 'foo' in postgres) and a ternary operator.

@georgesittas
Copy link
Collaborator Author

georgesittas commented May 12, 2023

With these additions our parser is actually more lenient than Clickhouse's. For example, we can handle this:

>>> import sqlglot
>>> sqlglot.parse_one("x ? y ? 1 : 2 : 3")
(IF this:
  (COLUMN this:
    (IDENTIFIER this: x, quoted: False)), true:
  (IF this:
    (COLUMN this:
      (IDENTIFIER this: y, quoted: False)), true:
    (LITERAL this: 1, is_string: False), false:
    (LITERAL this: 2, is_string: False)), false:
  (LITERAL this: 3, is_string: False))

Whereas Clickhouse fails to parse it:

Screenshot 2023-05-12 at 6 03 24 PM

@tobymao
Copy link
Owner

tobymao commented May 12, 2023

maybe this should be clickhouse only so you don't have to worry about conflicts.

also, maybe you should treat it like the exponent use case for postgres. it can have another precedence

@tobymao tobymao merged commit f585eef into main May 12, 2023
@tobymao tobymao deleted the jo/clickhouse_ternary branch May 12, 2023 16:26
adrianisk pushed a commit to adrianisk/sqlglot that referenced this pull request Jun 21, 2023
* Feat(clickhouse): parse ternary operator

* Formatting

* Refactor

* Fixup

* Fixup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clickhouse: Ternary if parsing
2 participants