Back to home page

OSCL-LXR

 
 

    


0001 --
0002 -- Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
0003 --
0004 --
0005 -- AGGREGATES [Part 2]
0006 -- https://github.com/postgres/postgres/blob/REL_12_BETA2/src/test/regress/sql/aggregates.sql#L145-L350
0007 
0008 -- Test aggregate operator with codegen on and off.
0009 --CONFIG_DIM1 spark.sql.codegen.wholeStage=true
0010 --CONFIG_DIM1 spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=CODEGEN_ONLY
0011 --CONFIG_DIM1 spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=NO_CODEGEN
0012 
0013 create temporary view int4_tbl as select * from values
0014   (0),
0015   (123456),
0016   (-123456),
0017   (2147483647),
0018   (-2147483647)
0019   as int4_tbl(f1);
0020 
0021 -- Test handling of Params within aggregate arguments in hashed aggregation.
0022 -- Per bug report from Jeevan Chalke.
0023 -- [SPARK-27877] Implement SQL-standard LATERAL subqueries
0024 -- explain (verbose, costs off)
0025 -- select s1, s2, sm
0026 -- from generate_series(1, 3) s1,
0027 --      lateral (select s2, sum(s1 + s2) sm
0028 --               from generate_series(1, 3) s2 group by s2) ss
0029 -- order by 1, 2;
0030 -- select s1, s2, sm
0031 -- from generate_series(1, 3) s1,
0032 --      lateral (select s2, sum(s1 + s2) sm
0033 --               from generate_series(1, 3) s2 group by s2) ss
0034 -- order by 1, 2;
0035 
0036 -- [SPARK-27878] Support ARRAY(sub-SELECT) expressions
0037 -- explain (verbose, costs off)
0038 -- select array(select sum(x+y) s
0039 --             from generate_series(1,3) y group by y order by s)
0040 --   from generate_series(1,3) x;
0041 -- select array(select sum(x+y) s
0042 --             from generate_series(1,3) y group by y order by s)
0043 --   from generate_series(1,3) x;
0044 
0045 -- [SPARK-27879] Implement bitwise integer aggregates(BIT_AND and BIT_OR)
0046 --
0047 -- test for bitwise integer aggregates
0048 --
0049 CREATE OR REPLACE TEMPORARY VIEW bitwise_test AS SELECT * FROM VALUES
0050   (1, 1, 1, 1L),
0051   (3, 3, 3, null),
0052   (7, 7, 7, 3L) AS bitwise_test(b1, b2, b3, b4);
0053 
0054 -- empty case
0055 SELECT BIT_AND(b1) AS n1, BIT_OR(b2)  AS n2 FROM bitwise_test where 1 = 0;
0056 
0057 -- null case
0058 SELECT BIT_AND(b4) AS n1, BIT_OR(b4)  AS n2 FROM bitwise_test where b4 is null;
0059 
0060 SELECT
0061  BIT_AND(cast(b1 as tinyint)) AS a1,
0062  BIT_AND(cast(b2 as smallint)) AS b1,
0063  BIT_AND(b3) AS c1,
0064  BIT_AND(b4) AS d1,
0065  BIT_OR(cast(b1 as tinyint))  AS e7,
0066  BIT_OR(cast(b2 as smallint))  AS f7,
0067  BIT_OR(b3)  AS g7,
0068  BIT_OR(b4)  AS h3
0069 FROM bitwise_test;
0070 
0071 -- group by
0072 SELECT b1 , bit_and(b2), bit_or(b4) FROM bitwise_test GROUP BY b1;
0073 
0074 --having
0075 SELECT b1, bit_and(b2) FROM bitwise_test GROUP BY b1 HAVING bit_and(b2) < 7;
0076 
0077 -- window
0078 SELECT b1, b2, bit_and(b2) OVER (PARTITION BY b1 ORDER BY b2) FROM bitwise_test;
0079 SELECT b1, b2, bit_or(b2) OVER (PARTITION BY b1 ORDER BY b2) FROM bitwise_test;
0080 
0081 --
0082 -- test boolean aggregates
0083 --
0084 -- first test all possible transition and final states
0085 
0086 -- The result is inconsistent with PostgreSQL because our AND does not have strict mode
0087 SELECT
0088   -- boolean and transitions
0089   -- null because strict
0090   (NULL AND NULL) IS NULL AS `t`,
0091   (TRUE AND NULL) IS NULL AS `t`,
0092   (FALSE AND NULL) IS NULL AS `t`,
0093   (NULL AND TRUE) IS NULL AS `t`,
0094   (NULL AND FALSE) IS NULL AS `t`,
0095   -- and actual computations
0096   (TRUE AND TRUE) AS `t`,
0097   NOT (TRUE AND FALSE) AS `t`,
0098   NOT (FALSE AND TRUE) AS `t`,
0099   NOT (FALSE AND FALSE) AS `t`;
0100 
0101 -- The result is inconsistent with PostgreSQL because our OR does not have strict mode
0102 SELECT
0103   -- boolean or transitions
0104   -- null because strict
0105   (NULL OR NULL) IS NULL AS `t`,
0106   (TRUE OR NULL) IS NULL AS `t`,
0107   (FALSE OR NULL) IS NULL AS `t`,
0108   (NULL OR TRUE) IS NULL AS `t`,
0109   (NULL OR FALSE) IS NULL AS `t`,
0110   -- actual computations
0111   (TRUE OR TRUE) AS `t`,
0112   (TRUE OR FALSE) AS `t`,
0113   (FALSE OR TRUE) AS `t`,
0114   NOT (FALSE OR FALSE) AS `t`;
0115 
0116 -- [SPARK-27880] Implement boolean aggregates(BOOL_AND, BOOL_OR and EVERY)
0117 CREATE OR REPLACE TEMPORARY VIEW bool_test AS SELECT * FROM VALUES
0118   (TRUE, null, FALSE, null),
0119   (FALSE, TRUE, null, null),
0120   (null, TRUE, FALSE, null) AS bool_test(b1, b2, b3, b4);
0121 
0122 -- empty case
0123 SELECT BOOL_AND(b1) AS n1, BOOL_OR(b3) AS n2 FROM bool_test WHERE 1 = 0;
0124 
0125 SELECT
0126   BOOL_AND(b1)     AS f1,
0127   BOOL_AND(b2)     AS t2,
0128   BOOL_AND(b3)     AS f3,
0129   BOOL_AND(b4)     AS n4,
0130   BOOL_AND(NOT b2) AS f5,
0131   BOOL_AND(NOT b3) AS t6
0132 FROM bool_test;
0133 
0134 SELECT
0135   EVERY(b1)     AS f1,
0136   EVERY(b2)     AS t2,
0137   EVERY(b3)     AS f3,
0138   EVERY(b4)     AS n4,
0139   EVERY(NOT b2) AS f5,
0140   EVERY(NOT b3) AS t6
0141 FROM bool_test;
0142 
0143 SELECT
0144   BOOL_OR(b1)      AS t1,
0145   BOOL_OR(b2)      AS t2,
0146   BOOL_OR(b3)      AS f3,
0147   BOOL_OR(b4)      AS n4,
0148   BOOL_OR(NOT b2)  AS f5,
0149   BOOL_OR(NOT b3)  AS t6
0150 FROM bool_test;
0151 
0152 --
0153 -- Test cases that should be optimized into indexscans instead of
0154 -- the generic aggregate implementation.
0155 --
0156 
0157 -- Basic cases
0158 -- explain
0159 --  select min(unique1) from tenk1;
0160 select min(unique1) from tenk1;
0161 -- explain
0162 --  select max(unique1) from tenk1;
0163 select max(unique1) from tenk1;
0164 -- explain
0165 --  select max(unique1) from tenk1 where unique1 < 42;
0166 select max(unique1) from tenk1 where unique1 < 42;
0167 -- explain
0168 --  select max(unique1) from tenk1 where unique1 > 42;
0169 select max(unique1) from tenk1 where unique1 > 42;
0170 
0171 -- the planner may choose a generic aggregate here if parallel query is
0172 -- enabled, since that plan will be parallel safe and the "optimized"
0173 -- plan, which has almost identical cost, will not be.  we want to test
0174 -- the optimized plan, so temporarily disable parallel query.
0175 -- begin;
0176 -- set local max_parallel_workers_per_gather = 0;
0177 -- explain
0178 --  select max(unique1) from tenk1 where unique1 > 42000;
0179 select max(unique1) from tenk1 where unique1 > 42000;
0180 -- rollback;
0181 
0182 -- multi-column index (uses tenk1_thous_tenthous)
0183 -- explain
0184 --  select max(tenthous) from tenk1 where thousand = 33;
0185 select max(tenthous) from tenk1 where thousand = 33;
0186 -- explain
0187 --  select min(tenthous) from tenk1 where thousand = 33;
0188 select min(tenthous) from tenk1 where thousand = 33;
0189 
0190 -- [SPARK-17348] Correlated column is not allowed in a non-equality predicate
0191 -- check parameter propagation into an indexscan subquery
0192 -- explain
0193 --  select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
0194 --    from int4_tbl;
0195 -- select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
0196 --  from int4_tbl;
0197 
0198 -- check some cases that were handled incorrectly in 8.3.0
0199 -- explain
0200 --  select distinct max(unique2) from tenk1;
0201 select distinct max(unique2) from tenk1;
0202 -- explain
0203 --  select max(unique2) from tenk1 order by 1;
0204 select max(unique2) from tenk1 order by 1;
0205 -- explain
0206 --  select max(unique2) from tenk1 order by max(unique2);
0207 select max(unique2) from tenk1 order by max(unique2);
0208 -- explain
0209 --  select max(unique2) from tenk1 order by max(unique2)+1;
0210 select max(unique2) from tenk1 order by max(unique2)+1;
0211 -- explain
0212 --  select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
0213 select t1.max_unique2, g from (select max(unique2) as max_unique2 FROM tenk1) t1 LATERAL VIEW explode(array(1,2,3)) t2 AS g order by g desc;
0214 
0215 -- interesting corner case: constant gets optimized into a seqscan
0216 -- explain
0217 --  select max(100) from tenk1;
0218 select max(100) from tenk1;