/
group-by.sql.out
666 lines (525 loc) · 14.4 KB
/
group-by.sql.out
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 64
-- !query
CREATE OR REPLACE TEMPORARY VIEW testData AS SELECT * FROM VALUES
(1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2), (null, 1), (3, null), (null, null)
AS testData(a, b)
-- !query schema
struct<>
-- !query output
-- !query
SELECT a, COUNT(b) FROM testData
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
grouping expressions sequence is empty, and 'testdata.a' is not an aggregate function. Wrap '(count(testdata.b) AS `count(b)`)' in windowing function(s) or wrap 'testdata.a' in first() (or first_value) if you don't care which value you get.
-- !query
SELECT COUNT(a), COUNT(b) FROM testData
-- !query schema
struct<count(a):bigint,count(b):bigint>
-- !query output
7 7
-- !query
SELECT a, COUNT(b) FROM testData GROUP BY a
-- !query schema
struct<a:int,count(b):bigint>
-- !query output
1 2
2 2
3 2
NULL 1
-- !query
SELECT a, COUNT(b) FROM testData GROUP BY b
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
expression 'testdata.a' is neither present in the group by, nor is it an aggregate function. Add to group by or wrap in first() (or first_value) if you don't care which value you get.
-- !query
SELECT COUNT(a), COUNT(b) FROM testData GROUP BY a
-- !query schema
struct<count(a):bigint,count(b):bigint>
-- !query output
0 1
2 2
2 2
3 2
-- !query
SELECT 'foo', COUNT(a) FROM testData GROUP BY 1
-- !query schema
struct<foo:string,count(a):bigint>
-- !query output
foo 7
-- !query
SELECT 'foo' FROM testData WHERE a = 0 GROUP BY 1
-- !query schema
struct<foo:string>
-- !query output
-- !query
SELECT 'foo', APPROX_COUNT_DISTINCT(a) FROM testData WHERE a = 0 GROUP BY 1
-- !query schema
struct<foo:string,approx_count_distinct(a):bigint>
-- !query output
-- !query
SELECT 'foo', MAX(STRUCT(a)) FROM testData WHERE a = 0 GROUP BY 1
-- !query schema
struct<foo:string,max(struct(a)):struct<a:int>>
-- !query output
-- !query
SELECT a + b, COUNT(b) FROM testData GROUP BY a + b
-- !query schema
struct<(a + b):int,count(b):bigint>
-- !query output
2 1
3 2
4 2
5 1
NULL 1
-- !query
SELECT a + 2, COUNT(b) FROM testData GROUP BY a + 1
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
expression 'testdata.a' is neither present in the group by, nor is it an aggregate function. Add to group by or wrap in first() (or first_value) if you don't care which value you get.
-- !query
SELECT a + 1 + 1, COUNT(b) FROM testData GROUP BY a + 1
-- !query schema
struct<((a + 1) + 1):int,count(b):bigint>
-- !query output
3 2
4 2
5 2
NULL 1
-- !query
SELECT SKEWNESS(a), KURTOSIS(a), MIN(a), MAX(a), AVG(a), VARIANCE(a), STDDEV(a), SUM(a), COUNT(a)
FROM testData
-- !query schema
struct<skewness(a):double,kurtosis(a):double,min(a):int,max(a):int,avg(a):double,variance(a):double,stddev(a):double,sum(a):bigint,count(a):bigint>
-- !query output
-0.2723801058145729 -1.5069204152249134 1 3 2.142857142857143 0.8095238095238094 0.8997354108424372 15 7
-- !query
SELECT COUNT(DISTINCT b), COUNT(DISTINCT b, c) FROM (SELECT 1 AS a, 2 AS b, 3 AS c) GROUP BY a
-- !query schema
struct<count(DISTINCT b):bigint,count(DISTINCT b, c):bigint>
-- !query output
1 1
-- !query
SELECT a AS k, COUNT(b) FROM testData GROUP BY k
-- !query schema
struct<k:int,count(b):bigint>
-- !query output
1 2
2 2
3 2
NULL 1
-- !query
SELECT a AS k, COUNT(b) FROM testData GROUP BY k HAVING k > 1
-- !query schema
struct<k:int,count(b):bigint>
-- !query output
2 2
3 2
-- !query
SELECT COUNT(b) AS k FROM testData GROUP BY k
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
aggregate functions are not allowed in GROUP BY, but found count(testdata.b)
-- !query
CREATE OR REPLACE TEMPORARY VIEW testDataHasSameNameWithAlias AS SELECT * FROM VALUES
(1, 1, 3), (1, 2, 1) AS testDataHasSameNameWithAlias(k, a, v)
-- !query schema
struct<>
-- !query output
-- !query
SELECT k AS a, COUNT(v) FROM testDataHasSameNameWithAlias GROUP BY a
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
expression 'testdatahassamenamewithalias.k' is neither present in the group by, nor is it an aggregate function. Add to group by or wrap in first() (or first_value) if you don't care which value you get.
-- !query
set spark.sql.groupByAliases=false
-- !query schema
struct<key:string,value:string>
-- !query output
spark.sql.groupByAliases false
-- !query
SELECT a AS k, COUNT(b) FROM testData GROUP BY k
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'k' given input columns: [testdata.a, testdata.b]; line 1 pos 47
-- !query
SELECT a, COUNT(1) FROM testData WHERE false GROUP BY a
-- !query schema
struct<a:int,count(1):bigint>
-- !query output
-- !query
SELECT COUNT(1) FROM testData WHERE false
-- !query schema
struct<count(1):bigint>
-- !query output
0
-- !query
SELECT 1 FROM (SELECT COUNT(1) FROM testData WHERE false) t
-- !query schema
struct<1:int>
-- !query output
1
-- !query
SELECT 1 from (
SELECT 1 AS z,
MIN(a.x)
FROM (select 1 as x) a
WHERE false
) b
where b.z != b.z
-- !query schema
struct<1:int>
-- !query output
-- !query
SELECT corr(DISTINCT x, y), corr(DISTINCT y, x), count(*)
FROM (VALUES (1, 1), (2, 2), (2, 2)) t(x, y)
-- !query schema
struct<corr(DISTINCT x, y):double,corr(DISTINCT y, x):double,count(1):bigint>
-- !query output
1.0 1.0 3
-- !query
SELECT 1 FROM range(10) HAVING true
-- !query schema
struct<1:int>
-- !query output
1
-- !query
SELECT 1 FROM range(10) HAVING MAX(id) > 0
-- !query schema
struct<1:int>
-- !query output
1
-- !query
SELECT id FROM range(10) HAVING id > 0
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
grouping expressions sequence is empty, and 'id' is not an aggregate function. Wrap '()' in windowing function(s) or wrap 'id' in first() (or first_value) if you don't care which value you get.
-- !query
SET spark.sql.legacy.parser.havingWithoutGroupByAsWhere=true
-- !query schema
struct<key:string,value:string>
-- !query output
spark.sql.legacy.parser.havingWithoutGroupByAsWhere true
-- !query
SELECT 1 FROM range(10) HAVING true
-- !query schema
struct<1:int>
-- !query output
1
1
1
1
1
1
1
1
1
1
-- !query
SELECT 1 FROM range(10) HAVING MAX(id) > 0
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Aggregate/Window/Generate expressions are not valid in where clause of the query.
Expression in where clause: [(max(id) > CAST(0 AS BIGINT))]
Invalid expressions: [max(id)]
-- !query
SELECT id FROM range(10) HAVING id > 0
-- !query schema
struct<id:bigint>
-- !query output
1
2
3
4
5
6
7
8
9
-- !query
SET spark.sql.legacy.parser.havingWithoutGroupByAsWhere=false
-- !query schema
struct<key:string,value:string>
-- !query output
spark.sql.legacy.parser.havingWithoutGroupByAsWhere false
-- !query
CREATE OR REPLACE TEMPORARY VIEW test_agg AS SELECT * FROM VALUES
(1, true), (1, false),
(2, true),
(3, false), (3, null),
(4, null), (4, null),
(5, null), (5, true), (5, false) AS test_agg(k, v)
-- !query schema
struct<>
-- !query output
-- !query
SELECT every(v), some(v), any(v), bool_and(v), bool_or(v) FROM test_agg WHERE 1 = 0
-- !query schema
struct<every(v):boolean,some(v):boolean,any(v):boolean,bool_and(v):boolean,bool_or(v):boolean>
-- !query output
NULL NULL NULL NULL NULL
-- !query
SELECT every(v), some(v), any(v), bool_and(v), bool_or(v) FROM test_agg WHERE k = 4
-- !query schema
struct<every(v):boolean,some(v):boolean,any(v):boolean,bool_and(v):boolean,bool_or(v):boolean>
-- !query output
NULL NULL NULL NULL NULL
-- !query
SELECT every(v), some(v), any(v), bool_and(v), bool_or(v) FROM test_agg WHERE k = 5
-- !query schema
struct<every(v):boolean,some(v):boolean,any(v):boolean,bool_and(v):boolean,bool_or(v):boolean>
-- !query output
false true true false true
-- !query
SELECT k, every(v), some(v), any(v), bool_and(v), bool_or(v) FROM test_agg GROUP BY k
-- !query schema
struct<k:int,every(v):boolean,some(v):boolean,any(v):boolean,bool_and(v):boolean,bool_or(v):boolean>
-- !query output
1 false true true false true
2 true true true true true
3 false false false false false
4 NULL NULL NULL NULL NULL
5 false true true false true
-- !query
SELECT k, every(v) FROM test_agg GROUP BY k HAVING every(v) = false
-- !query schema
struct<k:int,every(v):boolean>
-- !query output
1 false
3 false
5 false
-- !query
SELECT k, every(v) FROM test_agg GROUP BY k HAVING every(v) IS NULL
-- !query schema
struct<k:int,every(v):boolean>
-- !query output
4 NULL
-- !query
SELECT k,
Every(v) AS every
FROM test_agg
WHERE k = 2
AND v IN (SELECT Any(v)
FROM test_agg
WHERE k = 1)
GROUP BY k
-- !query schema
struct<k:int,every:boolean>
-- !query output
2 true
-- !query
SELECT k,
Every(v) AS every
FROM test_agg
WHERE k = 2
AND v IN (SELECT Every(v)
FROM test_agg
WHERE k = 1)
GROUP BY k
-- !query schema
struct<k:int,every:boolean>
-- !query output
-- !query
SELECT every(1)
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'every(1)' due to data type mismatch: Input to function 'every' should have been boolean, but it's [int].; line 1 pos 7
-- !query
SELECT some(1S)
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'some(1S)' due to data type mismatch: Input to function 'some' should have been boolean, but it's [smallint].; line 1 pos 7
-- !query
SELECT any(1L)
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'any(1L)' due to data type mismatch: Input to function 'any' should have been boolean, but it's [bigint].; line 1 pos 7
-- !query
SELECT every("true")
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'every('true')' due to data type mismatch: Input to function 'every' should have been boolean, but it's [string].; line 1 pos 7
-- !query
SELECT bool_and(1.0)
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'bool_and(1.0BD)' due to data type mismatch: Input to function 'bool_and' should have been boolean, but it's [decimal(2,1)].; line 1 pos 7
-- !query
SELECT bool_or(1.0D)
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
cannot resolve 'bool_or(1.0D)' due to data type mismatch: Input to function 'bool_or' should have been boolean, but it's [double].; line 1 pos 7
-- !query
SELECT k, v, every(v) OVER (PARTITION BY k ORDER BY v) FROM test_agg
-- !query schema
struct<k:int,v:boolean,every(v) OVER (PARTITION BY k ORDER BY v ASC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW):boolean>
-- !query output
1 false false
1 true false
2 true true
3 NULL NULL
3 false false
4 NULL NULL
4 NULL NULL
5 NULL NULL
5 false false
5 true false
-- !query
SELECT k, v, some(v) OVER (PARTITION BY k ORDER BY v) FROM test_agg
-- !query schema
struct<k:int,v:boolean,some(v) OVER (PARTITION BY k ORDER BY v ASC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW):boolean>
-- !query output
1 false false
1 true true
2 true true
3 NULL NULL
3 false false
4 NULL NULL
4 NULL NULL
5 NULL NULL
5 false false
5 true true
-- !query
SELECT k, v, any(v) OVER (PARTITION BY k ORDER BY v) FROM test_agg
-- !query schema
struct<k:int,v:boolean,any(v) OVER (PARTITION BY k ORDER BY v ASC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW):boolean>
-- !query output
1 false false
1 true true
2 true true
3 NULL NULL
3 false false
4 NULL NULL
4 NULL NULL
5 NULL NULL
5 false false
5 true true
-- !query
SELECT k, v, bool_and(v) OVER (PARTITION BY k ORDER BY v) FROM test_agg
-- !query schema
struct<k:int,v:boolean,bool_and(v) OVER (PARTITION BY k ORDER BY v ASC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW):boolean>
-- !query output
1 false false
1 true false
2 true true
3 NULL NULL
3 false false
4 NULL NULL
4 NULL NULL
5 NULL NULL
5 false false
5 true false
-- !query
SELECT k, v, bool_or(v) OVER (PARTITION BY k ORDER BY v) FROM test_agg
-- !query schema
struct<k:int,v:boolean,bool_or(v) OVER (PARTITION BY k ORDER BY v ASC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW):boolean>
-- !query output
1 false false
1 true true
2 true true
3 NULL NULL
3 false false
4 NULL NULL
4 NULL NULL
5 NULL NULL
5 false false
5 true true
-- !query
SELECT count(*) FROM test_agg HAVING count(*) > 1L
-- !query schema
struct<count(1):bigint>
-- !query output
10
-- !query
SELECT k, max(v) FROM test_agg GROUP BY k HAVING max(v) = true
-- !query schema
struct<k:int,max(v):boolean>
-- !query output
1 true
2 true
5 true
-- !query
SELECT * FROM (SELECT COUNT(*) AS cnt FROM test_agg) WHERE cnt > 1L
-- !query schema
struct<cnt:bigint>
-- !query output
10
-- !query
SELECT count(*) FROM test_agg WHERE count(*) > 1L
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Aggregate/Window/Generate expressions are not valid in where clause of the query.
Expression in where clause: [(count(1) > 1L)]
Invalid expressions: [count(1)]
-- !query
SELECT count(*) FROM test_agg WHERE count(*) + 1L > 1L
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Aggregate/Window/Generate expressions are not valid in where clause of the query.
Expression in where clause: [((count(1) + 1L) > 1L)]
Invalid expressions: [count(1)]
-- !query
SELECT count(*) FROM test_agg WHERE k = 1 or k = 2 or count(*) + 1L > 1L or max(k) > 1
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Aggregate/Window/Generate expressions are not valid in where clause of the query.
Expression in where clause: [(((test_agg.k = 1) OR (test_agg.k = 2)) OR (((count(1) + 1L) > 1L) OR (max(test_agg.k) > 1)))]
Invalid expressions: [count(1), max(test_agg.k)]
-- !query
SELECT AVG(DISTINCT decimal_col), SUM(DISTINCT decimal_col) FROM VALUES (CAST(1 AS DECIMAL(9, 0))) t(decimal_col)
-- !query schema
struct<avg(DISTINCT decimal_col):decimal(13,4),sum(DISTINCT decimal_col):decimal(19,0)>
-- !query output
1.0000 1
-- !query
SELECT not(a IS NULL), count(*) AS c
FROM testData
GROUP BY a IS NULL
-- !query schema
struct<(NOT (a IS NULL)):boolean,c:bigint>
-- !query output
false 2
true 7
-- !query
SELECT if(not(a IS NULL), rand(0), 1), count(*) AS c
FROM testData
GROUP BY a IS NULL
-- !query schema
struct<(IF((NOT (a IS NULL)), rand(0), 1)):double,c:bigint>
-- !query output
0.7604953758285915 7
1.0 2