Skip to content

Commit

Permalink
Feat: add distribute by alias for hive window functions closes #2043
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Aug 13, 2023
1 parent c817e19 commit 689956b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions sqlglot/dialects/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class Tokenizer(tokens.Tokenizer):
class Parser(parser.Parser):
LOG_DEFAULTS_TO_LN = True
STRICT_CAST = False
PARTITION_BY_TOKENS = {*parser.Parser.PARTITION_BY_TOKENS, TokenType.DISTRIBUTE_BY}

FUNCTIONS = {
**parser.Parser.FUNCTIONS,
Expand Down
3 changes: 2 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ class Parser(metaclass=_Parser):
WINDOW_ALIAS_TOKENS = ID_VAR_TOKENS - {TokenType.ROWS}
WINDOW_BEFORE_PAREN_TOKENS = {TokenType.OVER}
WINDOW_SIDES = {"FOLLOWING", "PRECEDING"}
PARTITION_BY_TOKENS = {TokenType.PARTITION_BY}

ADD_CONSTRAINT_TOKENS = {TokenType.CONSTRAINT, TokenType.PRIMARY_KEY, TokenType.FOREIGN_KEY}

Expand Down Expand Up @@ -1623,7 +1624,7 @@ def _parse_locking(self) -> exp.LockingProperty:
)

def _parse_partition_by(self) -> t.List[t.Optional[exp.Expression]]:
if self._match(TokenType.PARTITION_BY):
if self._match_set(self.PARTITION_BY_TOKENS):
return self._parse_csv(self._parse_conjunction)
return []

Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ def test_order_by(self):
)

def test_hive(self):
self.validate_identity(
"SELECT ROW() OVER (DISTRIBUTE BY x)", "SELECT ROW() OVER (PARTITION BY x)"
)
self.validate_identity("SELECT transform")
self.validate_identity("SELECT * FROM test DISTRIBUTE BY y SORT BY x DESC ORDER BY l")
self.validate_identity(
Expand Down

0 comments on commit 689956b

Please sign in to comment.