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