Skip to content

Commit 02ef4c5

Browse files
committed
Use parameter for start and end of WINDOW FRAME
1 parent b98c950 commit 02ef4c5

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Use parameter for start and end of WINDOW FRAME
12
* Use parameter for limit and offset
23

34
Version 1.5.0 - 2024-05-13

sql/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,7 @@ def has_alias(self):
20092009
return AliasManager.contains(self)
20102010

20112011
def __str__(self):
2012+
param = Flavor.get().param
20122013
partition = ''
20132014
if self.partition:
20142015
partition = 'PARTITION BY ' + ', '.join(map(str, self.partition))
@@ -2022,9 +2023,9 @@ def format(frame, direction):
20222023
elif not frame:
20232024
return 'CURRENT ROW'
20242025
elif frame < 0:
2025-
return '%s PRECEDING' % -frame
2026+
return '%s PRECEDING' % param
20262027
elif frame > 0:
2027-
return '%s FOLLOWING' % frame
2028+
return '%s FOLLOWING' % param
20282029

20292030
frame = ''
20302031
if self.frame:
@@ -2045,6 +2046,11 @@ def params(self):
20452046
if self.order_by:
20462047
for expression in self.order_by:
20472048
p.extend(expression.params)
2049+
if self.frame:
2050+
if self.start:
2051+
p.append(abs(self.start))
2052+
if self.end:
2053+
p.append(abs(self.end))
20482054
return tuple(p)
20492055

20502056

sql/tests/test_window.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ def test_window_range(self):
3333
window.start = -1
3434
self.assertEqual(str(window),
3535
'PARTITION BY "c" RANGE '
36-
'BETWEEN 1 PRECEDING AND CURRENT ROW')
37-
self.assertEqual(window.params, ())
36+
'BETWEEN %s PRECEDING AND CURRENT ROW')
37+
self.assertEqual(window.params, (1,))
3838

3939
window.start = 0
4040
window.end = 1
4141
self.assertEqual(str(window),
4242
'PARTITION BY "c" RANGE '
43-
'BETWEEN CURRENT ROW AND 1 FOLLOWING')
44-
self.assertEqual(window.params, ())
43+
'BETWEEN CURRENT ROW AND %s FOLLOWING')
44+
self.assertEqual(window.params, (1,))
4545

4646
window.start = 1
4747
window.end = None
4848
self.assertEqual(str(window),
4949
'PARTITION BY "c" RANGE '
50-
'BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING')
51-
self.assertEqual(window.params, ())
50+
'BETWEEN %s FOLLOWING AND UNBOUNDED FOLLOWING')
51+
self.assertEqual(window.params, (1,))
5252

5353
def test_window_exclude(self):
5454
t = Table('t')

0 commit comments

Comments
 (0)