Back to home page

OSCL-LXR

 
 

    


0001 -- Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
0002 --
0003 -- GROUPING SETS
0004 -- https://github.com/postgres/postgres/blob/REL_12_STABLE/src/test/regress/sql/groupingsets.sql
0005 
0006 -- test data sources
0007 
0008 create temp view gstest1(a,b,v)
0009   as values (1,1,10),(1,1,11),(1,2,12),(1,2,13),(1,3,14),
0010             (2,3,15),
0011             (3,3,16),(3,4,17),
0012             (4,1,18),(4,1,19);
0013 
0014 -- Since Spark doesn't support CREATE TEMPORARY TABLE, we used CREATE TABLE instead
0015 -- create temp table gstest2 (a integer, b integer, c integer, d integer,
0016 --                            e integer, f integer, g integer, h integer);
0017 create table gstest2 (a integer, b integer, c integer, d integer,
0018                       e integer, f integer, g integer, h integer) using parquet;
0019 -- [SPARK-29386] Copy data between a file and a table
0020 -- copy gstest2 from stdin;
0021 -- 1    1       1       1       1       1       1       1
0022 -- 1    1       1       1       1       1       1       2
0023 -- 1    1       1       1       1       1       2       2
0024 -- 1    1       1       1       1       2       2       2
0025 -- 1    1       1       1       2       2       2       2
0026 -- 1    1       1       2       2       2       2       2
0027 -- 1    1       2       2       2       2       2       2
0028 -- 1    2       2       2       2       2       2       2
0029 -- 2    2       2       2       2       2       2       2
0030 -- \.
0031 insert into gstest2 values
0032   (1, 1, 1, 1, 1, 1, 1, 1),
0033   (1, 1, 1, 1, 1, 1, 1, 2),
0034   (1, 1, 1, 1, 1, 1, 2, 2),
0035   (1, 1, 1, 1, 1, 2, 2, 2),
0036   (1, 1, 1, 1, 2, 2, 2, 2),
0037   (1, 1, 1, 2, 2, 2, 2, 2),
0038   (1, 1, 2, 2, 2, 2, 2, 2),
0039   (1, 2, 2, 2, 2, 2, 2, 2),
0040   (2, 2, 2, 2, 2, 2, 2, 2);
0041 
0042 -- Since Spark doesn't support CREATE TEMPORARY TABLE, we used CREATE TABLE instead
0043 -- create temp table gstest3 (a integer, b integer, c integer, d integer);
0044 create table gstest3 (a integer, b integer, c integer, d integer) using parquet;
0045 -- [SPARK-29386] Copy data between a file and a table
0046 -- copy gstest3 from stdin;
0047 -- 1    1       1       1
0048 -- 2    2       2       2
0049 -- \.
0050 insert into gstest3 values
0051   (1, 1, 1, 1),
0052   (2, 2, 2, 2);
0053 -- [SPARK-19842] Informational Referential Integrity Constraints Support in Spark
0054 -- alter table gstest3 add primary key (a);
0055 
0056 -- Since Spark doesn't support CREATE TEMPORARY TABLE, we used CREATE TABLE instead
0057 -- create temp table gstest4(id integer, v integer,
0058 --                           unhashable_col bit(4), unsortable_col xid);
0059 -- [SPARK-29697] Support bit string types/literals
0060 create table gstest4(id integer, v integer,
0061                      unhashable_col /* bit(4) */ byte, unsortable_col /* xid */ integer) using parquet;
0062 insert into gstest4
0063 -- values (1,1,b'0000','1'), (2,2,b'0001','1'),
0064 --        (3,4,b'0010','2'), (4,8,b'0011','2'),
0065 --        (5,16,b'0000','2'), (6,32,b'0001','2'),
0066 --        (7,64,b'0010','1'), (8,128,b'0011','1');
0067 values (1,1,tinyint('0'),1), (2,2,tinyint('1'),1),
0068        (3,4,tinyint('2'),2), (4,8,tinyint('3'),2),
0069        (5,16,tinyint('0'),2), (6,32,tinyint('1'),2),
0070        (7,64,tinyint('2'),1), (8,128,tinyint('3'),1);
0071 
0072 -- Since Spark doesn't support CREATE TEMPORARY TABLE, we used CREATE TABLE instead
0073 -- create temp table gstest_empty (a integer, b integer, v integer);
0074 create table gstest_empty (a integer, b integer, v integer) using parquet;
0075 
0076 -- Spark doesn't handle UDFs in SQL
0077 -- create function gstest_data(v integer, out a integer, out b integer)
0078 --   returns setof record
0079 --   as $f$
0080 --     begin
0081 --       return query select v, i from generate_series(1,3) i;
0082 --     end;
0083 --   $f$ language plpgsql;
0084 
0085 -- basic functionality
0086 
0087 -- Ignore a PostgreSQL-specific option
0088 -- set enable_hashagg = false;  -- test hashing explicitly later
0089 
0090 -- simple rollup with multiple plain aggregates, with and without ordering
0091 -- (and with ordering differing from grouping)
0092 
0093 -- [SPARK-29698] Support grouping function with multiple arguments
0094 -- select a, b, grouping(a,b), sum(v), count(*), max(v)
0095 select a, b, grouping(a), grouping(b), sum(v), count(*), max(v)
0096   from gstest1 group by rollup (a,b);
0097 -- select a, b, grouping(a,b), sum(v), count(*), max(v)
0098 select a, b, grouping(a), grouping(b), sum(v), count(*), max(v)
0099   from gstest1 group by rollup (a,b) order by a,b;
0100 -- select a, b, grouping(a,b), sum(v), count(*), max(v)
0101 select a, b, grouping(a), grouping(b), sum(v), count(*), max(v)
0102   from gstest1 group by rollup (a,b) order by b desc, a;
0103 -- select a, b, grouping(a,b), sum(v), count(*), max(v)
0104 select a, b, grouping(a), grouping(b), sum(v), count(*), max(v)
0105   from gstest1 group by rollup (a,b) order by coalesce(a,0)+coalesce(b,0);
0106 
0107 -- [SPARK-28664] ORDER BY in aggregate function
0108 -- various types of ordered aggs
0109 -- select a, b, grouping(a,b),
0110 --        array_agg(v order by v),
0111 --        string_agg(string(v:text, ':' order by v desc),
0112 --        percentile_disc(0.5) within group (order by v),
0113 --        rank(1,2,12) within group (order by a,b,v)
0114 --   from gstest1 group by rollup (a,b) order by a,b;
0115 
0116 -- [SPARK-28664] ORDER BY in aggregate function
0117 -- test usage of grouped columns in direct args of aggs
0118 -- select grouping(a), a, array_agg(b),
0119 --        rank(a) within group (order by b nulls first),
0120 --        rank(a) within group (order by b nulls last)
0121 --   from (values (1,1),(1,4),(1,5),(3,1),(3,2)) v(a,b)
0122 --  group by rollup (a) order by a;
0123 
0124 -- nesting with window functions
0125 -- [SPARK-29699] Different answers in nested aggregates with window functions
0126 select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
0127   from gstest2 group by rollup (a,b) order by rsum, a, b;
0128 
0129 -- [SPARK-29700] Support nested grouping sets
0130 -- nesting with grouping sets
0131 -- select sum(c) from gstest2
0132 --   group by grouping sets((), grouping sets((), grouping sets(())))
0133 --   order by 1 desc;
0134 -- select sum(c) from gstest2
0135 --   group by grouping sets((), grouping sets((), grouping sets(((a, b)))))
0136 --   order by 1 desc;
0137 -- select sum(c) from gstest2
0138 --   group by grouping sets(grouping sets(rollup(c), grouping sets(cube(c))))
0139 --   order by 1 desc;
0140 -- select sum(c) from gstest2
0141 --   group by grouping sets(a, grouping sets(a, cube(b)))
0142 --   order by 1 desc;
0143 -- select sum(c) from gstest2
0144 --   group by grouping sets(grouping sets((a, (b))))
0145 --   order by 1 desc;
0146 -- select sum(c) from gstest2
0147 --   group by grouping sets(grouping sets((a, b)))
0148 --   order by 1 desc;
0149 -- select sum(c) from gstest2
0150 --   group by grouping sets(grouping sets(a, grouping sets(a), a))
0151 --   order by 1 desc;
0152 -- select sum(c) from gstest2
0153 --   group by grouping sets(grouping sets(a, grouping sets(a, grouping sets(a), ((a)), a, grouping sets(a), (a)), a))
0154 --   order by 1 desc;
0155 -- select sum(c) from gstest2
0156 --   group by grouping sets((a,(a,b)), grouping sets((a,(a,b)),a))
0157 --   order by 1 desc;
0158 
0159 -- empty input: first is 0 rows, second 1, third 3 etc.
0160 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),a);
0161 -- [SPARK-29701] Different answers when empty input given in GROUPING SETS
0162 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),());
0163 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
0164 select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
0165 
0166 -- empty input with joins tests some important code paths
0167 -- [SPARK-29701] Different answers when empty input given in GROUPING SETS
0168 select t1.a, t2.b, sum(t1.v), count(*) from gstest_empty t1, gstest_empty t2
0169  group by grouping sets ((t1.a,t2.b),());
0170 
0171 -- simple joins, var resolution, GROUPING on join vars
0172 -- [SPARK-29698] Support grouping function with multiple arguments
0173 -- select t1.a, t2.b, grouping(t1.a, t2.b), sum(t1.v), max(t2.a)
0174 select t1.a, t2.b, grouping(t1.a), grouping(t2.b), sum(t1.v), max(t2.a)
0175   from gstest1 t1, gstest2 t2
0176  group by grouping sets ((t1.a, t2.b), ());
0177 
0178 -- [SPARK-29698] Support grouping function with multiple arguments
0179 -- select t1.a, t2.b, grouping(t1.a, t2.b), sum(t1.v), max(t2.a)
0180 select t1.a, t2.b, grouping(t1.a), grouping(t2.b), sum(t1.v), max(t2.a)
0181   from gstest1 t1 join gstest2 t2 on (t1.a=t2.a)
0182  group by grouping sets ((t1.a, t2.b), ());
0183 
0184 -- [SPARK-29698] Support grouping function with multiple arguments
0185 -- select a, b, grouping(a, b), sum(t1.v), max(t2.c)
0186 select a, b, grouping(a), grouping(b), sum(t1.v), max(t2.c)
0187   from gstest1 t1 join gstest2 t2 using (a,b)
0188  group by grouping sets ((a, b), ());
0189 
0190 -- check that functionally dependent cols are not nulled
0191 -- [SPARK-29698] Support grouping function with multiple arguments
0192 -- [SPARK-19842] Informational Referential Integrity Constraints Support in Spark
0193 -- [SPARK-29702] Resolve group-by columns with functional dependencies
0194 -- select a, d, grouping(a,b,c)
0195 --   from gstest3
0196 --  group by grouping sets ((a,b), (a,c));
0197 
0198 -- check that distinct grouping columns are kept separate
0199 -- even if they are equal()
0200 -- explain (costs off)
0201 -- select g as alias1, g as alias2
0202 --   from generate_series(1,3) g
0203 --  group by alias1, rollup(alias2);
0204 
0205 -- [SPARK-27767] Built-in function: generate_series
0206 -- [SPARK-29704] Support the combinations of grouping operations
0207 -- select g as alias1, g as alias2
0208 --   from generate_series(1,3) g
0209 --  group by alias1, rollup(alias2);
0210 
0211 -- check that pulled-up subquery outputs still go to null when appropriate
0212 select four, x
0213   from (select four, ten, 'foo' as x from tenk1) as t
0214   group by grouping sets (four, x)
0215   having x = 'foo';
0216 
0217 select four, x || 'x'
0218   from (select four, ten, 'foo' as x from tenk1) as t
0219   group by grouping sets (four, x)
0220   order by four;
0221 
0222 select (x+y)*1, sum(z)
0223  from (select 1 as x, 2 as y, 3 as z) s
0224  group by grouping sets (x+y, x);
0225 
0226 CREATE TEMP VIEW int8_tbl AS SELECT * FROM VALUES
0227   (123L, 456L),
0228   (123L, 4567890123456789L),
0229   (4567890123456789L, 123L),
0230   (4567890123456789L, 4567890123456789L),
0231   (4567890123456789L, -4567890123456789L) as int8_tbl(q1, q2);
0232 
0233 select x, not x as not_x, q2 from
0234   (select *, q1 = 1 as x from int8_tbl i1) as t
0235   group by grouping sets(x, q2)
0236   order by x, q2;
0237 
0238 DROP VIEW int8_tbl;
0239 
0240 -- simple rescan tests
0241 
0242 -- Spark doesn't handle UDFs in SQL
0243 -- select a, b, sum(v.x)
0244 --   from (values (1),(2)) v(x), gstest_data(v.x)
0245 --  group by rollup (a,b);
0246 
0247 -- Spark doesn't handle UDFs in SQL
0248 -- select *
0249 --   from (values (1),(2)) v(x),
0250 --        lateral (select a, b, sum(v.x) from gstest_data(v.x) group by rollup (a,b)) s;
0251 
0252 -- min max optimization should still work with GROUP BY ()
0253 -- explain (costs off)
0254 --   select min(unique1) from tenk1 GROUP BY ();
0255 
0256 -- Views with GROUPING SET queries
0257 -- [SPARK-29698] Support grouping function with multiple arguments
0258 -- [SPARK-29705] Support more expressive forms in GroupingSets/Cube/Rollup
0259 -- CREATE VIEW gstest_view AS select a, b, grouping(a,b), sum(c), count(*), max(c)
0260 --   from gstest2 group by rollup ((a,b,c),(c,d));
0261 
0262 -- select pg_get_viewdef('gstest_view'::regclass, true);
0263 
0264 -- Nested queries with 3 or more levels of nesting
0265 -- [SPARK-29698] Support grouping function with multiple arguments
0266 -- [SPARK-29703] grouping() can only be used with GroupingSets/Cube/Rollup
0267 -- select(select (select grouping(a,b) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP(e,f);
0268 -- select(select (select grouping(e,f) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP(e,f);
0269 -- select(select (select grouping(c) from (values (1)) v2(c) GROUP BY c) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP(e,f);
0270 
0271 -- Combinations of operations
0272 -- [SPARK-29704] Support the combinations of grouping operations
0273 -- select a, b, c, d from gstest2 group by rollup(a,b),grouping sets(c,d);
0274 -- select a, b from (values (1,2),(2,3)) v(a,b) group by a,b, grouping sets(a);
0275 
0276 -- Spark doesn't handle UDFs in SQL
0277 -- Tests for chained aggregates
0278 -- select a, b, grouping(a,b), sum(v), count(*), max(v)
0279 --   from gstest1 group by grouping sets ((a,b),(a+1,b+1),(a+2,b+2)) order by 3,6;
0280 -- select(select (select grouping(a,b) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY ROLLUP((e+1),(f+1));
0281 -- select(select (select grouping(a,b) from (values (1)) v2(c)) from (values (1,2)) v1(a,b) group by (a,b)) from (values(6,7)) v3(e,f) GROUP BY CUBE((e+1),(f+1)) ORDER BY (e+1),(f+1);
0282 -- select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
0283 --   from gstest2 group by cube (a,b) order by rsum, a, b;
0284 -- select a, b, sum(c) from (values (1,1,10),(1,1,11),(1,2,12),(1,2,13),(1,3,14),(2,3,15),(3,3,16),(3,4,17),(4,1,18),(4,1,19)) v(a,b,c) group by rollup (a,b);
0285 -- select a, b, sum(v.x)
0286 --   from (values (1),(2)) v(x), gstest_data(v.x)
0287 --  group by cube (a,b) order by a,b;
0288 
0289 -- Test reordering of grouping sets
0290 -- explain (costs off)
0291 -- select * from gstest1 group by grouping sets((a,b,v),(v)) order by v,b,a;
0292 
0293 -- [SPARK-29698] Support grouping function with multiple arguments
0294 -- [SPARK-29703] grouping() can only be used with GroupingSets/Cube/Rollup
0295 -- Agg level check. This query should error out.
0296 -- select (select grouping(a), grouping(b) from gstest2) from gstest2 group by a,b;
0297 
0298 --Nested queries
0299 -- [SPARK-29700] Support nested grouping sets
0300 -- select a, b, sum(c), count(*) from gstest2 group by grouping sets (rollup(a,b),a);
0301 
0302 -- HAVING queries
0303 select ten, sum(distinct four) from onek a
0304 group by grouping sets((ten,four),(ten))
0305 having exists (select 1 from onek b where sum(distinct a.four) = b.four);
0306 
0307 -- Tests around pushdown of HAVING clauses, partially testing against previous bugs
0308 select a,count(*) from gstest2 group by rollup(a) order by a;
0309 select a,count(*) from gstest2 group by rollup(a) having a is distinct from 1 order by a;
0310 -- explain (costs off)
0311 --   select a,count(*) from gstest2 group by rollup(a) having a is distinct from 1 order by a;
0312 
0313 -- [SPARK-29706] Support an empty grouping expression
0314 -- select v.c, (select count(*) from gstest2 group by () having v.c)
0315 --   from (values (false),(true)) v(c) order by v.c;
0316 -- explain (costs off)
0317 --   select v.c, (select count(*) from gstest2 group by () having v.c)
0318 --     from (values (false),(true)) v(c) order by v.c;
0319 
0320 -- HAVING with GROUPING queries
0321 select ten, grouping(ten) from onek
0322 group by grouping sets(ten) having grouping(ten) >= 0
0323 order by 2,1;
0324 select ten, grouping(ten) from onek
0325 group by grouping sets(ten, four) having grouping(ten) > 0
0326 order by 2,1;
0327 select ten, grouping(ten) from onek
0328 group by rollup(ten) having grouping(ten) > 0
0329 order by 2,1;
0330 select ten, grouping(ten) from onek
0331 group by cube(ten) having grouping(ten) > 0
0332 order by 2,1;
0333 -- [SPARK-29703] grouping() can only be used with GroupingSets/Cube/Rollup
0334 -- select ten, grouping(ten) from onek
0335 -- group by (ten) having grouping(ten) >= 0
0336 -- order by 2,1;
0337 
0338 -- FILTER queries
0339 -- [SPARK-30276] Support Filter expression allows simultaneous use of DISTINCT
0340 -- select ten, sum(distinct four) filter (where string(four) like '123') from onek a
0341 -- group by rollup(ten);
0342 
0343 -- More rescan tests
0344 -- [SPARK-27877] ANSI SQL: LATERAL derived table(T491)
0345 -- select * from (values (1),(2)) v(a) left join lateral (select v.a, four, ten, count(*) from onek group by cube(four,ten)) s on true order by v.a,four,ten;
0346 -- [SPARK-27878] Support ARRAY(sub-SELECT) expressions
0347 -- select array(select row(v.a,s1.*) from (select two,four, count(*) from onek group by cube(two,four) order by two,four) s1) from (values (1),(2)) v(a);
0348 
0349 -- [SPARK-29704] Support the combinations of grouping operations
0350 -- Grouping on text columns
0351 -- select sum(ten) from onek group by two, rollup(string(four)) order by 1;
0352 -- select sum(ten) from onek group by rollup(string(four)), two order by 1;
0353 
0354 -- hashing support
0355 
0356 -- Ignore a PostgreSQL-specific option
0357 -- set enable_hashagg = true;
0358 
0359 -- failure cases
0360 
0361 -- Since this test is implementation specific for plans, it passes in Spark
0362 select count(*) from gstest4 group by rollup(unhashable_col,unsortable_col);
0363 -- [SPARK-27878] Support ARRAY(sub-SELECT) expressions
0364 -- select array_agg(v order by v) from gstest4 group by grouping sets ((id,unsortable_col),(id));
0365 
0366 -- simple cases
0367 
0368 -- [SPARK-29698] Support grouping function with multiple arguments
0369 -- select a, b, grouping(a,b), sum(v), count(*), max(v)
0370 select a, b, grouping(a), grouping(b), sum(v), count(*), max(v)
0371   from gstest1 group by grouping sets ((a),(b)) order by 3,4,1,2 /* 3,1,2 */;
0372 -- explain (costs off) select a, b, grouping(a,b), sum(v), count(*), max(v)
0373 --   from gstest1 group by grouping sets ((a),(b)) order by 3,1,2;
0374 
0375 -- [SPARK-29698] Support grouping function with multiple arguments
0376 -- select a, b, grouping(a,b), sum(v), count(*), max(v)
0377 select a, b, grouping(a), grouping(b), sum(v), count(*), max(v)
0378   from gstest1 group by cube(a,b) order by 3,4,1,2 /* 3,1,2 */;
0379 -- explain (costs off) select a, b, grouping(a,b), sum(v), count(*), max(v)
0380 --   from gstest1 group by cube(a,b) order by 3,1,2;
0381 
0382 -- shouldn't try and hash
0383 -- explain (costs off)
0384 --   select a, b, grouping(a,b), array_agg(v order by v)
0385 --     from gstest1 group by cube(a,b);
0386 
0387 -- unsortable cases
0388 select unsortable_col, count(*)
0389   from gstest4 group by grouping sets ((unsortable_col),(unsortable_col))
0390   order by string(unsortable_col);
0391 
0392 -- mixed hashable/sortable cases
0393 -- [SPARK-29698] Support grouping function with multiple arguments
0394 select unhashable_col, unsortable_col,
0395        -- grouping(unhashable_col, unsortable_col),
0396        grouping(unhashable_col), grouping(unsortable_col),
0397        count(*), sum(v)
0398   from gstest4 group by grouping sets ((unhashable_col),(unsortable_col))
0399  order by 3, 4, 6 /* 3, 5 */;
0400 -- explain (costs off)
0401 --   select unhashable_col, unsortable_col,
0402 --          grouping(unhashable_col, unsortable_col),
0403 --          count(*), sum(v)
0404 --     from gstest4 group by grouping sets ((unhashable_col),(unsortable_col))
0405 --    order by 3,5;
0406 
0407 -- [SPARK-29698] Support grouping function with multiple arguments
0408 select unhashable_col, unsortable_col,
0409        -- grouping(unhashable_col, unsortable_col),
0410        grouping(unhashable_col), grouping(unsortable_col),
0411        count(*), sum(v)
0412   from gstest4 group by grouping sets ((v,unhashable_col),(v,unsortable_col))
0413  order by 3, 4, 6 /* 3,5 */;
0414 -- explain (costs off)
0415 --   select unhashable_col, unsortable_col,
0416 --          grouping(unhashable_col, unsortable_col),
0417 --          count(*), sum(v)
0418 --     from gstest4 group by grouping sets ((v,unhashable_col),(v,unsortable_col))
0419 --    order by 3,5;
0420 
0421 -- empty input: first is 0 rows, second 1, third 3 etc.
0422 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),a);
0423 -- explain (costs off)
0424 --   select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),a);
0425 -- [SPARK-29701] Different answers when empty input given in GROUPING SETS
0426 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),());
0427 select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
0428 -- explain (costs off)
0429 --   select a, b, sum(v), count(*) from gstest_empty group by grouping sets ((a,b),(),(),());
0430 -- [SPARK-29701] Different answers when empty input given in GROUPING SETS
0431 select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
0432 -- explain (costs off)
0433 --   select sum(v), count(*) from gstest_empty group by grouping sets ((),(),());
0434 
0435 -- [SPARK-29698] Support grouping function with multiple arguments
0436 -- [SPARK-19842] Informational Referential Integrity Constraints Support in Spark
0437 -- [SPARK-29702] Resolve group-by columns with functional dependencies
0438 -- check that functionally dependent cols are not nulled
0439 -- select a, d, grouping(a,b,c)
0440 --   from gstest3
0441 --  group by grouping sets ((a,b), (a,c));
0442 -- explain (costs off)
0443 --   select a, d, grouping(a,b,c)
0444 --     from gstest3
0445 --    group by grouping sets ((a,b), (a,c));
0446 
0447 -- simple rescan tests
0448 
0449 -- select a, b, sum(v.x)
0450 --   from (values (1),(2)) v(x), gstest_data(v.x)
0451 --  group by grouping sets (a,b)
0452 --  order by 1, 2, 3;
0453 -- explain (costs off)
0454 --   select a, b, sum(v.x)
0455 --     from (values (1),(2)) v(x), gstest_data(v.x)
0456 --    group by grouping sets (a,b)
0457 --    order by 3, 1, 2;
0458 -- select *
0459 --   from (values (1),(2)) v(x),
0460 --        lateral (select a, b, sum(v.x) from gstest_data(v.x) group by grouping sets (a,b)) s;
0461 -- explain (costs off)
0462 --   select *
0463 --     from (values (1),(2)) v(x),
0464 --          lateral (select a, b, sum(v.x) from gstest_data(v.x) group by grouping sets (a,b)) s;
0465 
0466 -- Tests for chained aggregates
0467 -- [SPARK-29698] Support grouping function with multiple arguments
0468 -- select a, b, grouping(a,b), sum(v), count(*), max(v)
0469 select a, b, grouping(a), grouping(b), sum(v), count(*), max(v)
0470   from gstest1 group by grouping sets ((a,b),(a+1,b+1),(a+2,b+2)) order by 3,4,7 /* 3,6 */;
0471 -- explain (costs off)
0472 --   select a, b, grouping(a,b), sum(v), count(*), max(v)
0473 --     from gstest1 group by grouping sets ((a,b),(a+1,b+1),(a+2,b+2)) order by 3,6;
0474 -- [SPARK-29699] Different answers in nested aggregates with window functions
0475 select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
0476   from gstest2 group by cube (a,b) order by rsum, a, b;
0477 -- explain (costs off)
0478 --   select a, b, sum(c), sum(sum(c)) over (order by a,b) as rsum
0479 --     from gstest2 group by cube (a,b) order by rsum, a, b;
0480 -- select a, b, sum(v.x)
0481 --   from (values (1),(2)) v(x), gstest_data(v.x)
0482 --  group by cube (a,b) order by a,b;
0483 -- explain (costs off)
0484 --   select a, b, sum(v.x)
0485 --     from (values (1),(2)) v(x), gstest_data(v.x)
0486 --    group by cube (a,b) order by a,b;
0487 
0488 -- Verify that we correctly handle the child node returning a
0489 -- non-minimal slot, which happens if the input is pre-sorted,
0490 -- e.g. due to an index scan.
0491 -- BEGIN;
0492 -- Ignore a PostgreSQL-specific option
0493 -- SET LOCAL enable_hashagg = false;
0494 -- EXPLAIN (COSTS OFF) SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP BY GROUPING SETS(a, b,()) ORDER BY a, b;
0495 SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP BY GROUPING SETS(a, b,()) ORDER BY a, b;
0496 -- Ignore a PostgreSQL-specific option
0497 -- SET LOCAL enable_seqscan = false;
0498 -- EXPLAIN (COSTS OFF) SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP BY GROUPING SETS(a, b,()) ORDER BY a, b;
0499 -- SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP BY GROUPING SETS(a, b,()) ORDER BY a, b;
0500 -- COMMIT;
0501 
0502 -- More rescan tests
0503 -- [SPARK-27877] ANSI SQL: LATERAL derived table(T491)
0504 -- select * from (values (1),(2)) v(a) left join lateral (select v.a, four, ten, count(*) from onek group by cube(four,ten)) s on true order by v.a,four,ten;
0505 -- [SPARK-27878] Support ARRAY(sub-SELECT) expressions
0506 -- select array(select row(v.a,s1.*) from (select two,four, count(*) from onek group by cube(two,four) order by two,four) s1) from (values (1),(2)) v(a);
0507 
0508 -- Rescan logic changes when there are no empty grouping sets, so test
0509 -- that too:
0510 -- [SPARK-27877] ANSI SQL: LATERAL derived table(T491)
0511 -- select * from (values (1),(2)) v(a) left join lateral (select v.a, four, ten, count(*) from onek group by grouping sets(four,ten)) s on true order by v.a,four,ten;
0512 -- [SPARK-27878] Support ARRAY(sub-SELECT) expressions
0513 -- select array(select row(v.a,s1.*) from (select two,four, count(*) from onek group by grouping sets(two,four) order by two,four) s1) from (values (1),(2)) v(a);
0514 
0515 -- test the knapsack
0516 
0517 -- Ignore a PostgreSQL-specific option
0518 -- set enable_indexscan = false;
0519 -- set work_mem = '64kB';
0520 -- explain (costs off)
0521 --   select unique1,
0522 --          count(two), count(four), count(ten),
0523 --          count(hundred), count(thousand), count(twothousand),
0524 --          count(*)
0525 --     from tenk1 group by grouping sets (unique1,twothousand,thousand,hundred,ten,four,two);
0526 -- explain (costs off)
0527 --   select unique1,
0528 --          count(two), count(four), count(ten),
0529 --          count(hundred), count(thousand), count(twothousand),
0530 --          count(*)
0531 --     from tenk1 group by grouping sets (unique1,hundred,ten,four,two);
0532 
0533 -- Ignore a PostgreSQL-specific option
0534 -- set work_mem = '384kB';
0535 -- explain (costs off)
0536 --   select unique1,
0537 --          count(two), count(four), count(ten),
0538 --          count(hundred), count(thousand), count(twothousand),
0539 --          count(*)
0540 --     from tenk1 group by grouping sets (unique1,twothousand,thousand,hundred,ten,four,two);
0541 
0542 -- check collation-sensitive matching between grouping expressions
0543 -- (similar to a check for aggregates, but there are additional code
0544 -- paths for GROUPING, so check again here)
0545 
0546 -- [SPARK-28382] Array Functions: unnest
0547 select v||'a', case grouping(v||'a') when 1 then 1 else 0 end, count(*)
0548   -- from unnest(array[1,1], array['a','b']) u(i,v)
0549   from values (1, 'a'), (1, 'b') u(i,v)
0550  group by rollup(i, v||'a') order by 1,3;
0551 select v||'a', case when grouping(v||'a') = 1 then 1 else 0 end, count(*)
0552   -- from unnest(array[1,1], array['a','b']) u(i,v)
0553   from values (1, 'a'), (1, 'b') u(i,v)
0554  group by rollup(i, v||'a') order by 1,3;
0555 
0556 -- end
0557 
0558 DROP VIEW gstest1;
0559 DROP TABLE gstest2;
0560 DROP TABLE gstest3;
0561 DROP TABLE gstest4;
0562 DROP TABLE gstest_empty;