Skip to content

Commit 1cb5c2c

Browse files
committed
Use correct frame bounds when window frame was not specified
Also added win_bit.result (result values checked)
1 parent 53784d9 commit 1cb5c2c

File tree

2 files changed

+137
-5
lines changed

2 files changed

+137
-5
lines changed

mysql-test/r/win_bit.result

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
create table t1 (
2+
pk int primary key,
3+
a int,
4+
b int
5+
);
6+
create table t2 (
7+
pk int primary key,
8+
a int,
9+
b int
10+
);
11+
insert into t1 values
12+
( 1 , 0, 1),
13+
( 2 , 0, 2),
14+
( 3 , 1, 4),
15+
( 4 , 1, 8),
16+
( 5 , 2, 32),
17+
( 6 , 2, 64),
18+
( 7 , 2, 128),
19+
( 8 , 2, 16);
20+
insert into t2 values
21+
( 1 , 0, 2),
22+
( 2 , 0, 2),
23+
( 3 , 1, 4),
24+
( 4 , 1, 4),
25+
( 5 , 2, 16),
26+
( 6 , 2, 64),
27+
( 7 , 2, 128),
28+
( 8 , 2, 16);
29+
# Test bit functions on only one partition.
30+
select pk, a, b,
31+
bit_or(b) over (order by pk) as bit_or,
32+
bit_and(b) over (order by pk) as bit_and,
33+
bit_xor(b) over (order by pk) as bit_xor
34+
from t1;
35+
pk a b bit_or bit_and bit_xor
36+
1 0 1 1 1 1
37+
2 0 2 3 0 3
38+
3 1 4 7 0 7
39+
4 1 8 15 0 15
40+
5 2 32 47 0 47
41+
6 2 64 111 0 111
42+
7 2 128 239 0 239
43+
8 2 16 255 0 255
44+
select pk, a, b,
45+
bit_or(b) over (order by pk) as bit_or,
46+
bit_and(b) over (order by pk) as bit_and,
47+
bit_xor(b) over (order by pk) as bit_xor
48+
from t2;
49+
pk a b bit_or bit_and bit_xor
50+
1 0 2 2 2 2
51+
2 0 2 2 2 0
52+
3 1 4 6 0 4
53+
4 1 4 6 0 0
54+
5 2 16 22 0 16
55+
6 2 64 86 0 80
56+
7 2 128 214 0 208
57+
8 2 16 214 0 192
58+
# Test multiple partitions with bit functions.
59+
select pk, a, b,
60+
bit_or(b) over (partition by a order by pk) as bit_or,
61+
bit_and(b) over (partition by a order by pk) as bit_and,
62+
bit_xor(b) over (partition by a order by pk) as bit_xor
63+
from t1;
64+
pk a b bit_or bit_and bit_xor
65+
1 0 1 1 1 1
66+
2 0 2 3 0 3
67+
3 1 4 4 4 4
68+
4 1 8 12 0 12
69+
5 2 32 32 32 32
70+
6 2 64 96 0 96
71+
7 2 128 224 0 224
72+
8 2 16 240 0 240
73+
select pk, a, b,
74+
bit_or(b) over (partition by a order by pk) as bit_or,
75+
bit_and(b) over (partition by a order by pk) as bit_and,
76+
bit_xor(b) over (partition by a order by pk) as bit_xor
77+
from t2;
78+
pk a b bit_or bit_and bit_xor
79+
1 0 2 2 2 2
80+
2 0 2 2 2 0
81+
3 1 4 4 4 4
82+
4 1 4 4 4 0
83+
5 2 16 16 16 16
84+
6 2 64 80 0 80
85+
7 2 128 208 0 208
86+
8 2 16 208 0 192
87+
# Test remove function for bit functions using a sliding window.
88+
select pk, a, b,
89+
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as bit_or,
90+
bit_and(b) over (partition by a order by pk) as bit_and,
91+
bit_xor(b) over (partition by a order by pk) as bit_xor
92+
from t1;
93+
pk a b bit_or bit_and bit_xor
94+
1 0 1 3 1 1
95+
2 0 2 3 0 3
96+
3 1 4 12 4 4
97+
4 1 8 12 0 12
98+
5 2 32 96 32 32
99+
6 2 64 224 0 96
100+
7 2 128 208 0 224
101+
8 2 16 144 0 240
102+
select pk, a, b,
103+
bit_or(b) over (partition by a order by pk) as bit_or,
104+
bit_and(b) over (partition by a order by pk) as bit_and,
105+
bit_xor(b) over (partition by a order by pk) as bit_xor
106+
from t2;
107+
pk a b bit_or bit_and bit_xor
108+
1 0 2 2 2 2
109+
2 0 2 2 2 0
110+
3 1 4 4 4 4
111+
4 1 4 4 4 0
112+
5 2 16 16 16 16
113+
6 2 64 80 0 80
114+
7 2 128 208 0 208
115+
8 2 16 208 0 192
116+
drop table t1;
117+
drop table t2;
118+

sql/sql_window.cc

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -820,16 +820,30 @@ class Frame_n_rows_following : public Frame_cursor
820820

821821
Frame_cursor *get_frame_cursor(Window_frame *frame, bool is_top_bound)
822822
{
823-
// TODO-cvicentiu When a frame is not specified, which is the frame type
824-
// that we will use?
825-
// Postgres uses RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.
826-
// For now we use UNBOUNDED FOLLOWING and UNBOUNDED PRECEDING.
827823
if (!frame)
828824
{
825+
/*
826+
The docs say this about the lack of frame clause:
827+
828+
Let WD be a window structure descriptor.
829+
...
830+
If WD has no window framing clause, then
831+
Case:
832+
i) If the window ordering clause of WD is not present, then WF is the
833+
window partition of R.
834+
ii) Otherwise, WF consists of all rows of the partition of R that
835+
precede R or are peers of R in the window ordering of the window
836+
partition defined by the window ordering clause.
837+
838+
For case #ii, the frame bounds essentially are "RANGE BETWEEN UNBOUNDED
839+
PRECEDING AND CURRENT ROW".
840+
For the case #i, without ordering clause all rows are considered peers,
841+
so again the same frame bounds can be used.
842+
*/
829843
if (is_top_bound)
830844
return new Frame_unbounded_preceding;
831845
else
832-
return new Frame_unbounded_following;
846+
return new Frame_range_current_row_bottom;
833847
}
834848

835849
Window_frame_bound *bound= is_top_bound? frame->top_bound :

0 commit comments

Comments
 (0)