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

Window #2

Merged
merged 2 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ class Select(Expression):

class Window(Expression):
token_type = TokenType.OVER
arg_types = {'this': True, 'partition': False, 'order': False}
arg_types = {'this': True, 'partition': False, 'order': False, 'spec': False}


class WindowSpec(Expression):
token_type = TokenType.SPEC
arg_types = {'kind': False, 'start': False, 'start_side': False, 'end': False, 'end_side': False}


# Binary Expressions
# (PLUS a b)
Expand Down
10 changes: 9 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,15 @@ def window_sql(self, expression):
order = expression.args.get('order')
order_sql = self.order_sql(order, flat=True) if order else ''
partition_sql = partition + ' ' if partition and order else partition
return f"{this_sql} OVER({partition_sql}{order_sql})"
spec = expression.args.get('spec')
spec_sql = ' ' + self.window_spec_sql(spec) if spec else ''
return f"{this_sql} OVER({partition_sql}{order_sql}{spec_sql})"

def window_spec_sql(self, expression):
kind = self.sql(expression, 'kind')
start = csv(self.sql(expression, 'start'), self.sql(expression, 'start_side'), sep=' ')
end = csv(self.sql(expression, 'end'), self.sql(expression, 'end_side'), sep=' ')
return f"{kind} BETWEEN {start} AND {end}"

def between_sql(self, expression):
this = self.sql(expression, 'this')
Expand Down
4 changes: 2 additions & 2 deletions sqlglot/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ def list_get(arr, index):
return arr[index] if index < len(arr) else None


def csv(*args):
return ', '.join(arg for arg in args if arg)
def csv(*args, sep=', '):
return sep.join(arg for arg in args if arg)
33 changes: 32 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,41 @@ def _parse_window(self, this):

order = self._parse_order(None)

spec = None

if self._match(TokenType.ROWS, TokenType.RANGE):
kind = self._prev
self._match(TokenType.BETWEEN)
start = self._parse_window_spec()
self._match(TokenType.AND)
end = self._parse_window_spec()

spec = exp.WindowSpec(
kind=kind,
start=start['value'],
start_side=start['side'],
end=end['value'],
end_side=end['side'],
)

if not self._match(TokenType.R_PAREN):
self.raise_error('Expecting )')

return exp.Window(this=this, partition=partition, order=order)
return exp.Window(this=this, partition=partition, order=order, spec=spec)

def _parse_window_spec(self):
self._match(TokenType.BETWEEN)

spec = {'value': None, 'side': None}

if self._match(TokenType.UNBOUNDED, TokenType.CURRENT_ROW):
spec['value'] = self._prev
else:
spec['value'] = self._parse_primary()

if self._match(TokenType.PRECEDING, TokenType.FOLLOWING):
spec['side'] = self._prev
return spec

def _parse_alias(self, this):
self._match(TokenType.ALIAS)
Expand Down
15 changes: 15 additions & 0 deletions sqlglot/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ class TokenType(AutoName):
COMMENT_START = auto()
CREATE = auto()
CROSS = auto()
CURRENT_ROW = auto()
DESC = auto()
DISTINCT = auto()
DROP = auto()
ELSE = auto()
END = auto()
EXISTS = auto()
FOLLOWING = auto()
FORMAT = auto()
FULL = auto()
FUNC = auto()
Expand All @@ -94,6 +96,7 @@ class TokenType(AutoName):
IF = auto()
IN = auto()
INNER = auto()
INTERVAL = auto()
IS = auto()
JOIN = auto()
LATERAL = auto()
Expand All @@ -109,11 +112,16 @@ class TokenType(AutoName):
OUTER = auto()
OVER = auto()
PARTITION = auto()
PRECEDING = auto()
SELECT = auto()
STORED = auto()
RANGE = auto()
RIGHT = auto()
RLIKE = auto()
ROWS = auto()
SPEC = auto()
THEN = auto()
UNBOUNDED = auto()
UNION = auto()
UNNEST = auto()
VIEW = auto()
Expand Down Expand Up @@ -200,6 +208,7 @@ class Tokenizer:
'COUNT': TokenType.COUNT,
'CREATE': TokenType.CREATE,
'CROSS': TokenType.CROSS,
'CURRENT ROW': TokenType.CURRENT_ROW,
'DESC': TokenType.DESC,
'DISTINCT': TokenType.DISTINCT,
'DROP': TokenType.DROP,
Expand All @@ -208,12 +217,14 @@ class Tokenizer:
'EXISTS': TokenType.EXISTS,
'FORMAT': TokenType.FORMAT,
'FULL': TokenType.FULL,
'FOLLOWING': TokenType.FOLLOWING,
'FROM': TokenType.FROM,
'GROUP BY': TokenType.GROUP,
'HAVING': TokenType.HAVING,
'IF': TokenType.IF,
'IN': TokenType.IN,
'INNER': TokenType.INNER,
'INTERVAL': TokenType.INTERVAL,
'IS': TokenType.IS,
'JOIN': TokenType.JOIN,
'LATERAL': TokenType.LATERAL,
Expand All @@ -228,12 +239,16 @@ class Tokenizer:
'OUTER': TokenType.OUTER,
'OVER': TokenType.OVER,
'PARTITION BY': TokenType.PARTITION,
'PRECEDING': TokenType.PRECEDING,
'RANGE': TokenType.RANGE,
'RIGHT': TokenType.RIGHT,
'RLIKE': TokenType.RLIKE,
'ROWS': TokenType.ROWS,
'SELECT': TokenType.SELECT,
'STORED': TokenType.STORED,
'TABLE': TokenType.TABLE,
'THEN': TokenType.THEN,
'UNBOUNDED': TokenType.UNBOUNDED,
'UNION': TokenType.UNION,
'UNNEST': TokenType.UNNEST,
'VIEW': TokenType.VIEW,
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ SELECT RANK() OVER(ORDER BY a) FROM x
SELECT RANK() OVER(ORDER BY a, b) FROM x
SELECT RANK() OVER(PARTITION BY a ORDER BY a) FROM x
SELECT RANK() OVER(PARTITION BY a, b ORDER BY a, b DESC) FROM x
SELECT SUM(x) OVER(PARTITION BY a) AS y FROM x
SELECT SUM(x) OVER(PARTITION BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
SELECT SUM(x) OVER(PARTITION BY a ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
SELECT SUM(x) OVER(PARTITION BY a ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
SELECT SUM(x) OVER(PARTITION BY a ROWS BETWEEN UNBOUNDED PRECEDING AND PRECEDING)
SELECT SUM(x) OVER(PARTITION BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
SELECT SUM(x) OVER(PARTITION BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
SELECT SUM(x) OVER(PARTITION BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
SELECT SUM(x) OVER(PARTITION BY a RANGE BETWEEN 1 AND 3)
SELECT SUM(x) OVER(PARTITION BY a RANGE BETWEEN 1 FOLLOWING AND 3)
SELECT SUM(x) OVER(PARTITION BY a RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
SELECT COUNT(DISTINCT a) OVER(PARTITION BY c ORDER BY d ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
SELECT a['1'], b[0], x.c[0], "x".d['1'] FROM x
SELECT ARRAY(1, 2, 3) FROM x
SELECT ARRAY(ARRAY(1), ARRAY(2)) FROM x
Expand Down