Skip to content

Commit be3902f

Browse files
committed
Implement ntile window function.
The current implementation does not allow for a dynamic expression within the sum function's parameter.
1 parent a5d3008 commit be3902f

File tree

8 files changed

+588
-1
lines changed

8 files changed

+588
-1
lines changed

mysql-test/r/win_ntile.result

Lines changed: 360 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,360 @@
1+
create table t1 (
2+
pk int primary key,
3+
a int,
4+
b int
5+
);
6+
insert into t1 values
7+
(11 , 0, 10),
8+
(12 , 0, 10),
9+
(13 , 1, 10),
10+
(14 , 1, 10),
11+
(18 , 2, 10),
12+
(15 , 2, 20),
13+
(16 , 2, 20),
14+
(17 , 2, 20),
15+
(19 , 4, 20),
16+
(20 , 4, 20);
17+
select pk, a, b, ntile(-1) over (order by a)
18+
from t1;
19+
ERROR HY000: Argument of NTILE must be greater than 0
20+
select pk, a, b,
21+
ntile(0) over (order by a)
22+
from t1;
23+
ERROR HY000: Argument of NTILE must be greater than 0
24+
select pk, a, b,
25+
ntile(1) over (order by pk)
26+
from t1;
27+
pk a b ntile(1) over (order by pk)
28+
11 0 10 1
29+
12 0 10 1
30+
13 1 10 1
31+
14 1 10 1
32+
15 2 20 1
33+
16 2 20 1
34+
17 2 20 1
35+
18 2 10 1
36+
19 4 20 1
37+
20 4 20 1
38+
select pk, a, b,
39+
ntile(2) over (order by pk)
40+
from t1;
41+
pk a b ntile(2) over (order by pk)
42+
11 0 10 1
43+
12 0 10 1
44+
13 1 10 1
45+
14 1 10 1
46+
15 2 20 1
47+
16 2 20 2
48+
17 2 20 2
49+
18 2 10 2
50+
19 4 20 2
51+
20 4 20 2
52+
select pk, a, b,
53+
ntile(3) over (order by pk)
54+
from t1;
55+
pk a b ntile(3) over (order by pk)
56+
11 0 10 1
57+
12 0 10 1
58+
13 1 10 1
59+
14 1 10 1
60+
15 2 20 2
61+
16 2 20 2
62+
17 2 20 2
63+
18 2 10 3
64+
19 4 20 3
65+
20 4 20 3
66+
select pk, a, b,
67+
ntile(4) over (order by pk)
68+
from t1;
69+
pk a b ntile(4) over (order by pk)
70+
11 0 10 1
71+
12 0 10 1
72+
13 1 10 1
73+
14 1 10 2
74+
15 2 20 2
75+
16 2 20 2
76+
17 2 20 3
77+
18 2 10 3
78+
19 4 20 4
79+
20 4 20 4
80+
select pk, a, b,
81+
ntile(5) over (order by pk)
82+
from t1;
83+
pk a b ntile(5) over (order by pk)
84+
11 0 10 1
85+
12 0 10 1
86+
13 1 10 2
87+
14 1 10 2
88+
15 2 20 3
89+
16 2 20 3
90+
17 2 20 4
91+
18 2 10 4
92+
19 4 20 5
93+
20 4 20 5
94+
select pk, a, b,
95+
ntile(6) over (order by pk)
96+
from t1;
97+
pk a b ntile(6) over (order by pk)
98+
11 0 10 1
99+
12 0 10 1
100+
13 1 10 2
101+
14 1 10 2
102+
15 2 20 3
103+
16 2 20 3
104+
17 2 20 4
105+
18 2 10 4
106+
19 4 20 5
107+
20 4 20 6
108+
select pk, a, b,
109+
ntile(7) over (order by pk)
110+
from t1;
111+
pk a b ntile(7) over (order by pk)
112+
11 0 10 1
113+
12 0 10 1
114+
13 1 10 2
115+
14 1 10 2
116+
15 2 20 3
117+
16 2 20 3
118+
17 2 20 4
119+
18 2 10 5
120+
19 4 20 6
121+
20 4 20 7
122+
select pk, a, b,
123+
ntile(8) over (order by pk)
124+
from t1;
125+
pk a b ntile(8) over (order by pk)
126+
11 0 10 1
127+
12 0 10 1
128+
13 1 10 2
129+
14 1 10 2
130+
15 2 20 3
131+
16 2 20 4
132+
17 2 20 5
133+
18 2 10 6
134+
19 4 20 7
135+
20 4 20 8
136+
select pk, a, b,
137+
ntile(9) over (order by pk)
138+
from t1;
139+
pk a b ntile(9) over (order by pk)
140+
11 0 10 1
141+
12 0 10 1
142+
13 1 10 2
143+
14 1 10 3
144+
15 2 20 4
145+
16 2 20 5
146+
17 2 20 6
147+
18 2 10 7
148+
19 4 20 8
149+
20 4 20 9
150+
select pk, a, b,
151+
ntile(10) over (order by pk)
152+
from t1;
153+
pk a b ntile(10) over (order by pk)
154+
11 0 10 1
155+
12 0 10 2
156+
13 1 10 3
157+
14 1 10 4
158+
15 2 20 5
159+
16 2 20 6
160+
17 2 20 7
161+
18 2 10 8
162+
19 4 20 9
163+
20 4 20 10
164+
select pk, a, b,
165+
ntile(11) over (order by pk)
166+
from t1;
167+
pk a b ntile(11) over (order by pk)
168+
11 0 10 1
169+
12 0 10 2
170+
13 1 10 3
171+
14 1 10 4
172+
15 2 20 5
173+
16 2 20 6
174+
17 2 20 7
175+
18 2 10 8
176+
19 4 20 9
177+
20 4 20 10
178+
select pk, a, b,
179+
ntile(20) over (order by pk)
180+
from t1;
181+
pk a b ntile(20) over (order by pk)
182+
11 0 10 1
183+
12 0 10 2
184+
13 1 10 3
185+
14 1 10 4
186+
15 2 20 5
187+
16 2 20 6
188+
17 2 20 7
189+
18 2 10 8
190+
19 4 20 9
191+
20 4 20 10
192+
select pk, a, b,
193+
ntile(1) over (partition by b order by pk)
194+
from t1;
195+
pk a b ntile(1) over (partition by b order by pk)
196+
11 0 10 1
197+
12 0 10 1
198+
13 1 10 1
199+
14 1 10 1
200+
18 2 10 1
201+
15 2 20 1
202+
16 2 20 1
203+
17 2 20 1
204+
19 4 20 1
205+
20 4 20 1
206+
select pk, a, b,
207+
ntile(2) over (partition by b order by pk)
208+
from t1;
209+
pk a b ntile(2) over (partition by b order by pk)
210+
11 0 10 1
211+
12 0 10 1
212+
13 1 10 1
213+
14 1 10 2
214+
18 2 10 2
215+
15 2 20 1
216+
16 2 20 1
217+
17 2 20 1
218+
19 4 20 2
219+
20 4 20 2
220+
select pk, a, b,
221+
ntile(3) over (partition by b order by pk)
222+
from t1;
223+
pk a b ntile(3) over (partition by b order by pk)
224+
11 0 10 1
225+
12 0 10 1
226+
13 1 10 2
227+
14 1 10 2
228+
18 2 10 3
229+
15 2 20 1
230+
16 2 20 1
231+
17 2 20 2
232+
19 4 20 2
233+
20 4 20 3
234+
select pk, a, b,
235+
ntile(4) over (partition by b order by pk)
236+
from t1;
237+
pk a b ntile(4) over (partition by b order by pk)
238+
11 0 10 1
239+
12 0 10 1
240+
13 1 10 2
241+
14 1 10 3
242+
18 2 10 4
243+
15 2 20 1
244+
16 2 20 1
245+
17 2 20 2
246+
19 4 20 3
247+
20 4 20 4
248+
select pk, a, b,
249+
ntile(5) over (partition by b order by pk)
250+
from t1;
251+
pk a b ntile(5) over (partition by b order by pk)
252+
11 0 10 1
253+
12 0 10 2
254+
13 1 10 3
255+
14 1 10 4
256+
18 2 10 5
257+
15 2 20 1
258+
16 2 20 2
259+
17 2 20 3
260+
19 4 20 4
261+
20 4 20 5
262+
select pk, a, b,
263+
ntile(6) over (partition by b order by pk)
264+
from t1;
265+
pk a b ntile(6) over (partition by b order by pk)
266+
11 0 10 1
267+
12 0 10 2
268+
13 1 10 3
269+
14 1 10 4
270+
18 2 10 5
271+
15 2 20 1
272+
16 2 20 2
273+
17 2 20 3
274+
19 4 20 4
275+
20 4 20 5
276+
select pk, a, b,
277+
ntile(7) over (partition by b order by pk)
278+
from t1;
279+
pk a b ntile(7) over (partition by b order by pk)
280+
11 0 10 1
281+
12 0 10 2
282+
13 1 10 3
283+
14 1 10 4
284+
18 2 10 5
285+
15 2 20 1
286+
16 2 20 2
287+
17 2 20 3
288+
19 4 20 4
289+
20 4 20 5
290+
select pk, a, b,
291+
ntile(8) over (partition by b order by pk)
292+
from t1;
293+
pk a b ntile(8) over (partition by b order by pk)
294+
11 0 10 1
295+
12 0 10 2
296+
13 1 10 3
297+
14 1 10 4
298+
18 2 10 5
299+
15 2 20 1
300+
16 2 20 2
301+
17 2 20 3
302+
19 4 20 4
303+
20 4 20 5
304+
select pk, a, b,
305+
ntile(9) over (partition by b order by pk)
306+
from t1;
307+
pk a b ntile(9) over (partition by b order by pk)
308+
11 0 10 1
309+
12 0 10 2
310+
13 1 10 3
311+
14 1 10 4
312+
18 2 10 5
313+
15 2 20 1
314+
16 2 20 2
315+
17 2 20 3
316+
19 4 20 4
317+
20 4 20 5
318+
select pk, a, b,
319+
ntile(10) over (partition by b order by pk)
320+
from t1;
321+
pk a b ntile(10) over (partition by b order by pk)
322+
11 0 10 1
323+
12 0 10 2
324+
13 1 10 3
325+
14 1 10 4
326+
18 2 10 5
327+
15 2 20 1
328+
16 2 20 2
329+
17 2 20 3
330+
19 4 20 4
331+
20 4 20 5
332+
select pk, a, b,
333+
ntile(11) over (partition by b order by pk)
334+
from t1;
335+
pk a b ntile(11) over (partition by b order by pk)
336+
11 0 10 1
337+
12 0 10 2
338+
13 1 10 3
339+
14 1 10 4
340+
18 2 10 5
341+
15 2 20 1
342+
16 2 20 2
343+
17 2 20 3
344+
19 4 20 4
345+
20 4 20 5
346+
select pk, a, b,
347+
ntile(20) over (partition by b order by pk)
348+
from t1;
349+
pk a b ntile(20) over (partition by b order by pk)
350+
11 0 10 1
351+
12 0 10 2
352+
13 1 10 3
353+
14 1 10 4
354+
18 2 10 5
355+
15 2 20 1
356+
16 2 20 2
357+
17 2 20 3
358+
19 4 20 4
359+
20 4 20 5
360+
drop table t1;

0 commit comments

Comments
 (0)