Skip to content

Commit 9be50d5

Browse files
committed
Port 'pivot.sql' to UDF test base
1 parent f830005 commit 9be50d5

2 files changed

Lines changed: 800 additions & 0 deletions

File tree

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
-- This test file was converted from pivot.sql.
2+
3+
-- Note that currently registered UDF returns a string. So there are some differences, for instance
4+
-- in string cast within UDF in Scala and Python.
5+
6+
create temporary view courseSales as select * from values
7+
("dotNET", 2012, 10000),
8+
("Java", 2012, 20000),
9+
("dotNET", 2012, 5000),
10+
("dotNET", 2013, 48000),
11+
("Java", 2013, 30000)
12+
as courseSales(course, year, earnings);
13+
14+
create temporary view years as select * from values
15+
(2012, 1),
16+
(2013, 2)
17+
as years(y, s);
18+
19+
create temporary view yearsWithComplexTypes as select * from values
20+
(2012, array(1, 1), map('1', 1), struct(1, 'a')),
21+
(2013, array(2, 2), map('2', 2), struct(2, 'b'))
22+
as yearsWithComplexTypes(y, a, m, s);
23+
24+
-- pivot courses
25+
SELECT * FROM (
26+
SELECT udf(year), course, earnings FROM courseSales
27+
)
28+
PIVOT (
29+
udf(sum(earnings))
30+
FOR course IN ('dotNET', 'Java')
31+
);
32+
33+
-- pivot years with no subquery
34+
SELECT * FROM courseSales
35+
PIVOT (
36+
udf(sum(earnings))
37+
FOR year IN (2012, 2013)
38+
);
39+
40+
-- pivot courses with multiple aggregations
41+
SELECT * FROM (
42+
SELECT year, course, earnings FROM courseSales
43+
)
44+
PIVOT (
45+
udf(sum(earnings)), udf(avg(earnings))
46+
FOR course IN ('dotNET', 'Java')
47+
);
48+
49+
-- pivot with no group by column
50+
SELECT * FROM (
51+
SELECT udf(course) as course, earnings FROM courseSales
52+
)
53+
PIVOT (
54+
udf(sum(earnings))
55+
FOR course IN ('dotNET', 'Java')
56+
);
57+
58+
-- pivot with no group by column and with multiple aggregations on different columns
59+
SELECT * FROM (
60+
SELECT year, course, earnings FROM courseSales
61+
)
62+
PIVOT (
63+
udf(sum(earnings)), udf(min(year))
64+
FOR course IN ('dotNET', 'Java')
65+
);
66+
67+
-- pivot on join query with multiple group by columns
68+
SELECT * FROM (
69+
SELECT course, year, earnings, udf(s) as s
70+
FROM courseSales
71+
JOIN years ON year = y
72+
)
73+
PIVOT (
74+
udf(sum(earnings))
75+
FOR s IN (1, 2)
76+
);
77+
78+
-- pivot on join query with multiple aggregations on different columns
79+
SELECT * FROM (
80+
SELECT course, year, earnings, s
81+
FROM courseSales
82+
JOIN years ON year = y
83+
)
84+
PIVOT (
85+
udf(sum(earnings)), udf(min(s))
86+
FOR course IN ('dotNET', 'Java')
87+
);
88+
89+
-- pivot on join query with multiple columns in one aggregation
90+
SELECT * FROM (
91+
SELECT course, year, earnings, s
92+
FROM courseSales
93+
JOIN years ON year = y
94+
)
95+
PIVOT (
96+
udf(sum(earnings * s))
97+
FOR course IN ('dotNET', 'Java')
98+
);
99+
100+
-- pivot with aliases and projection
101+
SELECT 2012_s, 2013_s, 2012_a, 2013_a, c FROM (
102+
SELECT year y, course c, earnings e FROM courseSales
103+
)
104+
PIVOT (
105+
udf(sum(e)) s, udf(avg(e)) a
106+
FOR y IN (2012, 2013)
107+
);
108+
109+
-- pivot with projection and value aliases
110+
SELECT firstYear_s, secondYear_s, firstYear_a, secondYear_a, c FROM (
111+
SELECT year y, course c, earnings e FROM courseSales
112+
)
113+
PIVOT (
114+
udf(sum(e)) s, udf(avg(e)) a
115+
FOR y IN (2012 as firstYear, 2013 secondYear)
116+
);
117+
118+
-- pivot years with non-aggregate function
119+
SELECT * FROM courseSales
120+
PIVOT (
121+
udf(abs(earnings))
122+
FOR year IN (2012, 2013)
123+
);
124+
125+
-- pivot with one of the expressions as non-aggregate function
126+
SELECT * FROM (
127+
SELECT year, course, earnings FROM courseSales
128+
)
129+
PIVOT (
130+
udf(sum(earnings)), year
131+
FOR course IN ('dotNET', 'Java')
132+
);
133+
134+
-- pivot with unresolvable columns
135+
SELECT * FROM (
136+
SELECT course, earnings FROM courseSales
137+
)
138+
PIVOT (
139+
udf(sum(earnings))
140+
FOR year IN (2012, 2013)
141+
);
142+
143+
-- pivot with complex aggregate expressions
144+
SELECT * FROM (
145+
SELECT year, course, earnings FROM courseSales
146+
)
147+
PIVOT (
148+
udf(ceil(udf(sum(earnings)))), avg(earnings) + 1 as a1
149+
FOR course IN ('dotNET', 'Java')
150+
);
151+
152+
-- pivot with invalid arguments in aggregate expressions
153+
SELECT * FROM (
154+
SELECT year, course, earnings FROM courseSales
155+
)
156+
PIVOT (
157+
sum(udf(avg(earnings)))
158+
FOR course IN ('dotNET', 'Java')
159+
);
160+
161+
-- pivot on multiple pivot columns
162+
SELECT * FROM (
163+
SELECT course, year, earnings, s
164+
FROM courseSales
165+
JOIN years ON year = y
166+
)
167+
PIVOT (
168+
udf(sum(earnings))
169+
FOR (course, year) IN (('dotNET', 2012), ('Java', 2013))
170+
);
171+
172+
-- pivot on multiple pivot columns with aliased values
173+
SELECT * FROM (
174+
SELECT course, year, earnings, s
175+
FROM courseSales
176+
JOIN years ON year = y
177+
)
178+
PIVOT (
179+
udf(sum(earnings))
180+
FOR (course, s) IN (('dotNET', 2) as c1, ('Java', 1) as c2)
181+
);
182+
183+
-- pivot on multiple pivot columns with values of wrong data types
184+
SELECT * FROM (
185+
SELECT course, year, earnings, s
186+
FROM courseSales
187+
JOIN years ON year = y
188+
)
189+
PIVOT (
190+
udf(sum(earnings))
191+
FOR (course, year) IN ('dotNET', 'Java')
192+
);
193+
194+
-- pivot with unresolvable values
195+
SELECT * FROM courseSales
196+
PIVOT (
197+
udf(sum(earnings))
198+
FOR year IN (s, 2013)
199+
);
200+
201+
-- pivot with non-literal values
202+
SELECT * FROM courseSales
203+
PIVOT (
204+
udf(sum(earnings))
205+
FOR year IN (course, 2013)
206+
);
207+
208+
-- pivot on join query with columns of complex data types
209+
SELECT * FROM (
210+
SELECT course, year, a
211+
FROM courseSales
212+
JOIN yearsWithComplexTypes ON year = y
213+
)
214+
PIVOT (
215+
udf(min(a))
216+
FOR course IN ('dotNET', 'Java')
217+
);
218+
219+
-- pivot on multiple pivot columns with agg columns of complex data types
220+
SELECT * FROM (
221+
SELECT course, year, y, a
222+
FROM courseSales
223+
JOIN yearsWithComplexTypes ON year = y
224+
)
225+
PIVOT (
226+
udf(max(a))
227+
FOR (y, course) IN ((2012, 'dotNET'), (2013, 'Java'))
228+
);
229+
230+
-- pivot on pivot column of array type
231+
SELECT * FROM (
232+
SELECT earnings, year, a
233+
FROM courseSales
234+
JOIN yearsWithComplexTypes ON year = y
235+
)
236+
PIVOT (
237+
udf(sum(earnings))
238+
FOR a IN (array(1, 1), array(2, 2))
239+
);
240+
241+
-- pivot on multiple pivot columns containing array type
242+
SELECT * FROM (
243+
SELECT course, earnings, year, a
244+
FROM courseSales
245+
JOIN yearsWithComplexTypes ON year = y
246+
)
247+
PIVOT (
248+
udf(sum(earnings))
249+
FOR (course, a) IN (('dotNET', array(1, 1)), ('Java', array(2, 2)))
250+
);
251+
252+
-- pivot on pivot column of struct type
253+
SELECT * FROM (
254+
SELECT earnings, year, s
255+
FROM courseSales
256+
JOIN yearsWithComplexTypes ON year = y
257+
)
258+
PIVOT (
259+
udf(sum(earnings))
260+
FOR s IN ((1, 'a'), (2, 'b'))
261+
);
262+
263+
-- pivot on multiple pivot columns containing struct type
264+
SELECT * FROM (
265+
SELECT course, earnings, year, s
266+
FROM courseSales
267+
JOIN yearsWithComplexTypes ON year = y
268+
)
269+
PIVOT (
270+
udf(sum(earnings))
271+
FOR (course, s) IN (('dotNET', (1, 'a')), ('Java', (2, 'b')))
272+
);
273+
274+
-- pivot on pivot column of map type
275+
SELECT * FROM (
276+
SELECT earnings, year, m
277+
FROM courseSales
278+
JOIN yearsWithComplexTypes ON year = y
279+
)
280+
PIVOT (
281+
udf(sum(earnings))
282+
FOR m IN (map('1', 1), map('2', 2))
283+
);
284+
285+
-- pivot on multiple pivot columns containing map type
286+
SELECT * FROM (
287+
SELECT course, earnings, year, m
288+
FROM courseSales
289+
JOIN yearsWithComplexTypes ON year = y
290+
)
291+
PIVOT (
292+
udf(sum(earnings))
293+
FOR (course, m) IN (('dotNET', map('1', 1)), ('Java', map('2', 2)))
294+
);
295+
296+
-- grouping columns output in the same order as input
297+
-- correctly handle pivot columns with different cases
298+
SELECT * FROM (
299+
SELECT course, earnings, udf("a") as a, udf("z") as z, udf("b") as b, udf("y") as y,
300+
udf("c") as c, udf("x") as x, udf("d") as d, udf("w") as w
301+
FROM courseSales
302+
)
303+
PIVOT (
304+
udf(sum(Earnings))
305+
FOR Course IN ('dotNET', 'Java')
306+
);

0 commit comments

Comments
 (0)