|
| 1 | +-- |
| 2 | +-- Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group |
| 3 | +-- |
| 4 | +-- |
| 5 | +-- AGGREGATES [Part 2] |
| 6 | +-- https://github.com/postgres/postgres/blob/REL_12_BETA1/src/test/regress/sql/aggregates.sql#L145-L350 |
| 7 | +-- |
| 8 | +-- This test file was converted from pgSQL/aggregates_part2.sql. |
| 9 | +-- Note that currently registered UDF returns a string. So there are some differences, for instance |
| 10 | +-- in string cast within UDF in Scala and Python. |
| 11 | + |
| 12 | +create temporary view int4_tbl as select * from values |
| 13 | + (0), |
| 14 | + (123456), |
| 15 | + (-123456), |
| 16 | + (2147483647), |
| 17 | + (-2147483647) |
| 18 | + as int4_tbl(f1); |
| 19 | + |
| 20 | +-- Test handling of Params within aggregate arguments in hashed aggregation. |
| 21 | +-- Per bug report from Jeevan Chalke. |
| 22 | +-- [SPARK-27877] Implement SQL-standard LATERAL subqueries |
| 23 | +-- explain (verbose, costs off) |
| 24 | +-- select s1, s2, sm |
| 25 | +-- from generate_series(1, 3) s1, |
| 26 | +-- lateral (select s2, sum(s1 + s2) sm |
| 27 | +-- from generate_series(1, 3) s2 group by s2) ss |
| 28 | +-- order by 1, 2; |
| 29 | +-- select s1, s2, sm |
| 30 | +-- from generate_series(1, 3) s1, |
| 31 | +-- lateral (select s2, sum(s1 + s2) sm |
| 32 | +-- from generate_series(1, 3) s2 group by s2) ss |
| 33 | +-- order by 1, 2; |
| 34 | + |
| 35 | +-- [SPARK-27878] Support ARRAY(sub-SELECT) expressions |
| 36 | +-- explain (verbose, costs off) |
| 37 | +-- select array(select sum(x+y) s |
| 38 | +-- from generate_series(1,3) y group by y order by s) |
| 39 | +-- from generate_series(1,3) x; |
| 40 | +-- select array(select sum(x+y) s |
| 41 | +-- from generate_series(1,3) y group by y order by s) |
| 42 | +-- from generate_series(1,3) x; |
| 43 | + |
| 44 | +-- [SPARK-27879] Implement bitwise integer aggregates(BIT_AND and BIT_OR) |
| 45 | +-- |
| 46 | +-- test for bitwise integer aggregates |
| 47 | +-- |
| 48 | +-- CREATE TEMPORARY TABLE bitwise_test( |
| 49 | +-- i2 INT2, |
| 50 | +-- i4 INT4, |
| 51 | +-- i8 INT8, |
| 52 | +-- i INTEGER, |
| 53 | +-- x INT2, |
| 54 | +-- y BIT(4) |
| 55 | +-- ); |
| 56 | + |
| 57 | +-- empty case |
| 58 | +-- SELECT |
| 59 | +-- BIT_AND(i2) AS "?", |
| 60 | +-- BIT_OR(i4) AS "?" |
| 61 | +-- FROM bitwise_test; |
| 62 | + |
| 63 | +-- COPY bitwise_test FROM STDIN NULL 'null'; |
| 64 | +-- 1 1 1 1 1 B0101 |
| 65 | +-- 3 3 3 null 2 B0100 |
| 66 | +-- 7 7 7 3 4 B1100 |
| 67 | +-- \. |
| 68 | + |
| 69 | +-- SELECT |
| 70 | +-- BIT_AND(i2) AS "1", |
| 71 | +-- BIT_AND(i4) AS "1", |
| 72 | +-- BIT_AND(i8) AS "1", |
| 73 | +-- BIT_AND(i) AS "?", |
| 74 | +-- BIT_AND(x) AS "0", |
| 75 | +-- BIT_AND(y) AS "0100", |
| 76 | +-- |
| 77 | +-- BIT_OR(i2) AS "7", |
| 78 | +-- BIT_OR(i4) AS "7", |
| 79 | +-- BIT_OR(i8) AS "7", |
| 80 | +-- BIT_OR(i) AS "?", |
| 81 | +-- BIT_OR(x) AS "7", |
| 82 | +-- BIT_OR(y) AS "1101" |
| 83 | +-- FROM bitwise_test; |
| 84 | + |
| 85 | +-- |
| 86 | +-- test boolean aggregates |
| 87 | +-- |
| 88 | +-- first test all possible transition and final states |
| 89 | + |
| 90 | +-- The result is inconsistent with PostgreSQL because our AND does not have strict mode |
| 91 | +SELECT |
| 92 | + -- boolean and transitions |
| 93 | + -- null because strict |
| 94 | + (NULL AND NULL) IS NULL AS `t`, |
| 95 | + (TRUE AND NULL) IS NULL AS `t`, |
| 96 | + (FALSE AND NULL) IS NULL AS `t`, |
| 97 | + (NULL AND TRUE) IS NULL AS `t`, |
| 98 | + (NULL AND FALSE) IS NULL AS `t`, |
| 99 | + -- and actual computations |
| 100 | + (TRUE AND TRUE) AS `t`, |
| 101 | + NOT (TRUE AND FALSE) AS `t`, |
| 102 | + NOT (FALSE AND TRUE) AS `t`, |
| 103 | + NOT (FALSE AND FALSE) AS `t`; |
| 104 | + |
| 105 | +-- The result is inconsistent with PostgreSQL because our OR does not have strict mode |
| 106 | +SELECT |
| 107 | + -- boolean or transitions |
| 108 | + -- null because strict |
| 109 | + (NULL OR NULL) IS NULL AS `t`, |
| 110 | + (TRUE OR NULL) IS NULL AS `t`, |
| 111 | + (FALSE OR NULL) IS NULL AS `t`, |
| 112 | + (NULL OR TRUE) IS NULL AS `t`, |
| 113 | + (NULL OR FALSE) IS NULL AS `t`, |
| 114 | + -- actual computations |
| 115 | + (TRUE OR TRUE) AS `t`, |
| 116 | + (TRUE OR FALSE) AS `t`, |
| 117 | + (FALSE OR TRUE) AS `t`, |
| 118 | + NOT (FALSE OR FALSE) AS `t`; |
| 119 | + |
| 120 | +-- [SPARK-27880] Implement boolean aggregates(BOOL_AND, BOOL_OR and EVERY) |
| 121 | +-- CREATE TEMPORARY TABLE bool_test( |
| 122 | +-- b1 BOOL, |
| 123 | +-- b2 BOOL, |
| 124 | +-- b3 BOOL, |
| 125 | +-- b4 BOOL); |
| 126 | + |
| 127 | +-- empty case |
| 128 | +-- SELECT |
| 129 | +-- BOOL_AND(b1) AS "n", |
| 130 | +-- BOOL_OR(b3) AS "n" |
| 131 | +-- FROM bool_test; |
| 132 | + |
| 133 | +-- COPY bool_test FROM STDIN NULL 'null'; |
| 134 | +-- TRUE null FALSE null |
| 135 | +-- FALSE TRUE null null |
| 136 | +-- null TRUE FALSE null |
| 137 | +-- \. |
| 138 | + |
| 139 | +-- SELECT |
| 140 | +-- BOOL_AND(b1) AS "f", |
| 141 | +-- BOOL_AND(b2) AS "t", |
| 142 | +-- BOOL_AND(b3) AS "f", |
| 143 | +-- BOOL_AND(b4) AS "n", |
| 144 | +-- BOOL_AND(NOT b2) AS "f", |
| 145 | +-- BOOL_AND(NOT b3) AS "t" |
| 146 | +-- FROM bool_test; |
| 147 | + |
| 148 | +-- SELECT |
| 149 | +-- EVERY(b1) AS "f", |
| 150 | +-- EVERY(b2) AS "t", |
| 151 | +-- EVERY(b3) AS "f", |
| 152 | +-- EVERY(b4) AS "n", |
| 153 | +-- EVERY(NOT b2) AS "f", |
| 154 | +-- EVERY(NOT b3) AS "t" |
| 155 | +-- FROM bool_test; |
| 156 | + |
| 157 | +-- SELECT |
| 158 | +-- BOOL_OR(b1) AS "t", |
| 159 | +-- BOOL_OR(b2) AS "t", |
| 160 | +-- BOOL_OR(b3) AS "f", |
| 161 | +-- BOOL_OR(b4) AS "n", |
| 162 | +-- BOOL_OR(NOT b2) AS "f", |
| 163 | +-- BOOL_OR(NOT b3) AS "t" |
| 164 | +-- FROM bool_test; |
| 165 | + |
| 166 | +-- |
| 167 | +-- Test cases that should be optimized into indexscans instead of |
| 168 | +-- the generic aggregate implementation. |
| 169 | +-- |
| 170 | + |
| 171 | +-- Basic cases |
| 172 | +-- explain |
| 173 | +-- select min(unique1) from tenk1; |
| 174 | +select min(udf(unique1)) from tenk1; |
| 175 | +-- explain |
| 176 | +-- select max(unique1) from tenk1; |
| 177 | +select udf(max(unique1)) from tenk1; |
| 178 | +-- explain |
| 179 | +-- select max(unique1) from tenk1 where unique1 < 42; |
| 180 | +select max(unique1) from tenk1 where udf(unique1) < 42; |
| 181 | +-- explain |
| 182 | +-- select max(unique1) from tenk1 where unique1 > 42; |
| 183 | +select max(unique1) from tenk1 where unique1 > udf(42); |
| 184 | + |
| 185 | +-- the planner may choose a generic aggregate here if parallel query is |
| 186 | +-- enabled, since that plan will be parallel safe and the "optimized" |
| 187 | +-- plan, which has almost identical cost, will not be. we want to test |
| 188 | +-- the optimized plan, so temporarily disable parallel query. |
| 189 | +-- begin; |
| 190 | +-- set local max_parallel_workers_per_gather = 0; |
| 191 | +-- explain |
| 192 | +-- select max(unique1) from tenk1 where unique1 > 42000; |
| 193 | +select max(unique1) from tenk1 where udf(unique1) > 42000; |
| 194 | +-- rollback; |
| 195 | + |
| 196 | +-- multi-column index (uses tenk1_thous_tenthous) |
| 197 | +-- explain |
| 198 | +-- select max(tenthous) from tenk1 where thousand = 33; |
| 199 | +select max(tenthous) from tenk1 where udf(thousand) = 33; |
| 200 | +-- explain |
| 201 | +-- select min(tenthous) from tenk1 where thousand = 33; |
| 202 | +select min(tenthous) from tenk1 where udf(thousand) = 33; |
| 203 | + |
| 204 | +-- [SPARK-17348] Correlated column is not allowed in a non-equality predicate |
| 205 | +-- check parameter propagation into an indexscan subquery |
| 206 | +-- explain |
| 207 | +-- select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt |
| 208 | +-- from int4_tbl; |
| 209 | +-- select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt |
| 210 | +-- from int4_tbl; |
| 211 | + |
| 212 | +-- check some cases that were handled incorrectly in 8.3.0 |
| 213 | +-- explain |
| 214 | +-- select distinct max(unique2) from tenk1; |
| 215 | +select distinct max(udf(unique2)) from tenk1; |
| 216 | +-- explain |
| 217 | +-- select max(unique2) from tenk1 order by 1; |
| 218 | +select max(unique2) from tenk1 order by udf(1); |
| 219 | +-- explain |
| 220 | +-- select max(unique2) from tenk1 order by max(unique2); |
| 221 | +select max(unique2) from tenk1 order by max(udf(unique2)); |
| 222 | +-- explain |
| 223 | +-- select max(unique2) from tenk1 order by max(unique2)+1; |
| 224 | +select udf(max(udf(unique2))) from tenk1 order by udf(max(unique2))+1; |
| 225 | +-- explain |
| 226 | +-- select max(unique2), generate_series(1,3) as g from tenk1 order by g desc; |
| 227 | +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; |
| 228 | + |
| 229 | +-- interesting corner case: constant gets optimized into a seqscan |
| 230 | +-- explain |
| 231 | +-- select max(100) from tenk1; |
| 232 | +select udf(max(100)) from tenk1; |
0 commit comments