File: test_mode_window.test

package info (click to toggle)
duckdb 1.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 299,196 kB
  • sloc: cpp: 865,414; ansic: 57,292; python: 18,871; sql: 12,663; lisp: 11,751; yacc: 7,412; lex: 1,682; sh: 747; makefile: 564
file content (177 lines) | stat: -rw-r--r-- 2,788 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# name: test/sql/window/test_mode_window.test
# description: Test MODE aggregate as a window function
# group: [window]

statement ok
SET default_null_order='nulls_first';

statement ok
PRAGMA enable_verification

statement ok
PRAGMA verify_external

statement ok
create table modes as select range r from range(10) union all values (NULL), (NULL), (NULL);

query IIII
SELECT r % 2, r, r//3, mode(r//3) over (partition by r % 2 order by r) FROM modes ORDER BY 1, 2
----
NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL
0	0	0	0
0	2	0	0
0	4	1	0
0	6	2	0
0	8	2	0
1	1	0	0
1	3	1	0
1	5	1	1
1	7	2	1
1	9	3	1

query III
SELECT r, r//3, mode(r//3) over (order by r rows between 1 preceding and 1 following) 
FROM modes 
ORDER BY ALL
----
NULL	NULL	NULL
NULL	NULL	NULL
NULL	NULL	0
0	0	0
1	0	0
2	0	0
3	1	1
4	1	1
5	1	1
6	2	2
7	2	2
8	2	2
9	3	2

query III
SELECT r, r//3, mode(r//3) over (order by r rows between 1 preceding and 3 following) FROM modes ORDER BY 1, 2
----
NULL	NULL	0
NULL	NULL	0
NULL	NULL	0
0	0	0
1	0	0
2	0	1
3	1	1
4	1	1
5	1	2
6	2	2
7	2	2
8	2	2
9	3	2

# Scattered NULLs
query IIII
SELECT r, r // 3, n, mode(n) over (partition by r % 3 order by r)
FROM (SELECT r, CASE r % 2 WHEN 0 THEN r ELSE NULL END AS n FROM modes) nulls
ORDER BY 1
----
NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL
0	0	0	0
1	0	NULL	NULL
2	0	2	2
3	1	NULL	0
4	1	4	4
5	1	NULL	2
6	2	6	0
7	2	NULL	4
8	2	8	2
9	3	NULL	0

query III
SELECT r, n, mode(n) over (order by r rows between 1 preceding and 1 following)
FROM (SELECT r, CASE r % 2 WHEN 0 THEN r ELSE NULL END AS n FROM modes) nulls
ORDER BY ALL
----
NULL	NULL	NULL
NULL	NULL	NULL
NULL	NULL	0
0	0	0
1	NULL	0
2	2	2
3	NULL	2
4	4	4
5	NULL	4
6	6	6
7	NULL	6
8	8	8
9	NULL	8

query III
SELECT r, n, mode(n) over (order by r rows between 1 preceding and 3 following)
FROM (SELECT r, CASE r % 2 WHEN 0 THEN r ELSE NULL END AS n FROM modes) nulls
ORDER BY 1
----
NULL	NULL	0
NULL	NULL	0
NULL	NULL	0
0	0	0
1	NULL	0
2	2	2
3	NULL	2
4	4	4
5	NULL	4
6	6	6
7	NULL	6
8	8	8
9	NULL	8

query III
SELECT r, n, mode(n) over (order by r rows between unbounded preceding and unbounded following)
FROM (SELECT r, CASE r % 2 WHEN 0 THEN r ELSE NULL END AS n FROM modes) nulls
ORDER BY 1
----
NULL	NULL	0
NULL	NULL	0
NULL	NULL	0
0	0	0
1	NULL	0
2	2	0
3	NULL	0
4	4	0
5	NULL	0
6	6	0
7	NULL	0
8	8	0
9	NULL	0

#
# Compare implementations
#

foreach windowmode "window" "combine" "separate"

statement ok
PRAGMA debug_window_mode=${windowmode}

query III
WITH t(r) AS (VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (NULL), (NULL), (NULL))
SELECT r, r//3, mode(r//3) over (order by r rows between 1 preceding and 1 following) 
FROM t 
ORDER BY ALL
----
NULL	NULL	NULL
NULL	NULL	NULL
NULL	NULL	0
0	0	0
1	0	0
2	0	0
3	1	1
4	1	1
5	1	1
6	2	2
7	2	2
8	2	2
9	3	2

endloop