Skip to content

Commit

Permalink
Fix: global join clickhouse closes #1606
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed May 12, 2023
1 parent 85d3d81 commit b5f0abc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
18 changes: 17 additions & 1 deletion sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sqlglot.errors import ParseError
from sqlglot.helper import ensure_list, seq_get
from sqlglot.parser import parse_var_map
from sqlglot.tokens import TokenType
from sqlglot.tokens import Token, TokenType


def _lower_func(sql: str) -> str:
Expand Down Expand Up @@ -153,6 +153,22 @@ def _parse_cte(self) -> exp.Expression:

return self.expression(exp.CTE, this=statement, alias=statement and statement.this)

def _parse_join_side_and_kind(
self,
) -> t.Tuple[t.Optional[Token], t.Optional[Token], t.Optional[Token]]:
return (
self._match(TokenType.GLOBAL) and self._prev,
self._match_set(self.JOIN_SIDES) and self._prev,
self._match_set(self.JOIN_KINDS) and self._prev,
)

def _parse_join(self, skip_join_token: bool = False) -> t.Optional[exp.Expression]:
join = super()._parse_join(skip_join_token)

if join:
join.set("global", join.args.pop("natural", None))
return join

class Generator(generator.Generator):
STRUCT_DELIMITER = ("(", ")")

Expand Down
1 change: 1 addition & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,7 @@ class Join(Expression):
"kind": False,
"using": False,
"natural": False,
"global": False,
"hint": False,
}

Expand Down
1 change: 1 addition & 0 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,7 @@ def join_sql(self, expression: exp.Join) -> str:
op
for op in (
"NATURAL" if expression.args.get("natural") else None,
"GLOBAL" if expression.args.get("global") else None,
expression.side,
expression.kind,
expression.hint if self.JOIN_HINTS else None,
Expand Down
2 changes: 2 additions & 0 deletions tests/dialects/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def test_clickhouse(self):
self.validate_identity("SELECT * FROM foo LEFT ASOF JOIN bla")
self.validate_identity("SELECT * FROM foo ASOF JOIN bla")
self.validate_identity("SELECT * FROM foo ANY JOIN bla")
self.validate_identity("SELECT * FROM foo GLOBAL ANY JOIN bla")
self.validate_identity("SELECT * FROM foo GLOBAL LEFT ANY JOIN bla")
self.validate_identity("SELECT quantile(0.5)(a)")
self.validate_identity("SELECT quantiles(0.5)(a) AS x FROM t")
self.validate_identity("SELECT quantiles(0.1, 0.2, 0.3)(a)")
Expand Down

0 comments on commit b5f0abc

Please sign in to comment.