Back to home page

OSCL-LXR

 
 

    


0001 --
0002 -- Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
0003 --
0004 --
0005 -- SELECT_HAVING
0006 -- https://github.com/postgres/postgres/blob/REL_12_BETA2/src/test/regress/sql/select_having.sql
0007 --
0008 
0009 -- load test data
0010 CREATE TABLE test_having (a int, b int, c string, d string) USING parquet;
0011 INSERT INTO test_having VALUES (0, 1, 'XXXX', 'A');
0012 INSERT INTO test_having VALUES (1, 2, 'AAAA', 'b');
0013 INSERT INTO test_having VALUES (2, 2, 'AAAA', 'c');
0014 INSERT INTO test_having VALUES (3, 3, 'BBBB', 'D');
0015 INSERT INTO test_having VALUES (4, 3, 'BBBB', 'e');
0016 INSERT INTO test_having VALUES (5, 3, 'bbbb', 'F');
0017 INSERT INTO test_having VALUES (6, 4, 'cccc', 'g');
0018 INSERT INTO test_having VALUES (7, 4, 'cccc', 'h');
0019 INSERT INTO test_having VALUES (8, 4, 'CCCC', 'I');
0020 INSERT INTO test_having VALUES (9, 4, 'CCCC', 'j');
0021 
0022 SELECT b, c FROM test_having
0023         GROUP BY b, c HAVING count(*) = 1 ORDER BY b, c;
0024 
0025 -- HAVING is effectively equivalent to WHERE in this case
0026 SELECT b, c FROM test_having
0027         GROUP BY b, c HAVING b = 3 ORDER BY b, c;
0028 
0029 -- [SPARK-28386] Cannot resolve ORDER BY columns with GROUP BY and HAVING
0030 -- SELECT lower(c), count(c) FROM test_having
0031 --      GROUP BY lower(c) HAVING count(*) > 2 OR min(a) = max(a)
0032 --      ORDER BY lower(c);
0033 
0034 SELECT c, max(a) FROM test_having
0035         GROUP BY c HAVING count(*) > 2 OR min(a) = max(a)
0036         ORDER BY c;
0037 
0038 -- test degenerate cases involving HAVING without GROUP BY
0039 -- Per SQL spec, these should generate 0 or 1 row, even without aggregates
0040 
0041 SELECT min(a), max(a) FROM test_having HAVING min(a) = max(a);
0042 SELECT min(a), max(a) FROM test_having HAVING min(a) < max(a);
0043 
0044 -- errors: ungrouped column references
0045 SELECT a FROM test_having HAVING min(a) < max(a);
0046 SELECT 1 AS one FROM test_having HAVING a > 1;
0047 
0048 -- the really degenerate case: need not scan table at all
0049 SELECT 1 AS one FROM test_having HAVING 1 > 2;
0050 SELECT 1 AS one FROM test_having HAVING 1 < 2;
0051 
0052 -- and just to prove that we aren't scanning the table:
0053 SELECT 1 AS one FROM test_having WHERE 1/a = 1 HAVING 1 < 2;
0054 
0055 DROP TABLE test_having;