3 -- Test cases for queries with ordering terms missing from the target list.
4 -- This used to be called "junkfilter.sql".
5 -- The parser uses the term "resjunk" to handle these cases.
9 CREATE TABLE test_missing_target (a int, b int, c char(8), d char);
10 INSERT INTO test_missing_target VALUES (0, 1, 'XXXX', 'A');
11 INSERT INTO test_missing_target VALUES (1, 2, 'ABAB', 'b');
12 INSERT INTO test_missing_target VALUES (2, 2, 'ABAB', 'c');
13 INSERT INTO test_missing_target VALUES (3, 3, 'BBBB', 'D');
14 INSERT INTO test_missing_target VALUES (4, 3, 'BBBB', 'e');
15 INSERT INTO test_missing_target VALUES (5, 3, 'bbbb', 'F');
16 INSERT INTO test_missing_target VALUES (6, 4, 'cccc', 'g');
17 INSERT INTO test_missing_target VALUES (7, 4, 'cccc', 'h');
18 INSERT INTO test_missing_target VALUES (8, 4, 'CCCC', 'I');
19 INSERT INTO test_missing_target VALUES (9, 4, 'CCCC', 'j');
20 -- w/ existing GROUP BY target
21 SELECT c, count(*) FROM test_missing_target GROUP BY test_missing_target.c ORDER BY c;
32 -- w/o existing GROUP BY target using a relation name in GROUP BY clause
33 SELECT count(*) FROM test_missing_target GROUP BY test_missing_target.c ORDER BY c;
44 -- w/o existing GROUP BY target and w/o existing a different ORDER BY target
46 SELECT count(*) FROM test_missing_target GROUP BY a ORDER BY b;
47 ERROR: column "test_missing_target.b" must appear in the GROUP BY clause or be used in an aggregate function
48 LINE 1: ...ECT count(*) FROM test_missing_target GROUP BY a ORDER BY b;
50 -- w/o existing GROUP BY target and w/o existing same ORDER BY target
51 SELECT count(*) FROM test_missing_target GROUP BY b ORDER BY b;
60 -- w/ existing GROUP BY target using a relation name in target
61 SELECT test_missing_target.b, count(*)
62 FROM test_missing_target GROUP BY b ORDER BY b;
71 -- w/o existing GROUP BY target
72 SELECT c FROM test_missing_target ORDER BY a;
87 -- w/o existing ORDER BY target
88 SELECT count(*) FROM test_missing_target GROUP BY b ORDER BY b desc;
97 -- group using reference number
98 SELECT count(*) FROM test_missing_target ORDER BY 1 desc;
104 -- order using reference number
105 SELECT c, count(*) FROM test_missing_target GROUP BY 1 ORDER BY 1;
116 -- group using reference number out of range
118 SELECT c, count(*) FROM test_missing_target GROUP BY 3;
119 ERROR: GROUP BY position 3 is not in select list
120 LINE 1: SELECT c, count(*) FROM test_missing_target GROUP BY 3;
122 -- group w/o existing GROUP BY and ORDER BY target under ambiguous condition
124 SELECT count(*) FROM test_missing_target x, test_missing_target y
126 GROUP BY b ORDER BY b;
127 ERROR: column reference "b" is ambiguous
128 LINE 3: GROUP BY b ORDER BY b;
130 -- order w/ target under ambiguous condition
131 -- failure NOT expected
132 SELECT a, a FROM test_missing_target
148 -- order expression w/ target under ambiguous condition
149 -- failure NOT expected
150 SELECT a/2, a/2 FROM test_missing_target
153 ----------+----------
166 -- group expression w/ target under ambiguous condition
167 -- failure NOT expected
168 SELECT a/2, a/2 FROM test_missing_target
169 GROUP BY a/2 ORDER BY a/2;
171 ----------+----------
179 -- group w/ existing GROUP BY target under ambiguous condition
180 SELECT x.b, count(*) FROM test_missing_target x, test_missing_target y
182 GROUP BY x.b ORDER BY x.b;
191 -- group w/o existing GROUP BY target under ambiguous condition
192 SELECT count(*) FROM test_missing_target x, test_missing_target y
194 GROUP BY x.b ORDER BY x.b;
203 -- group w/o existing GROUP BY target under ambiguous condition
205 CREATE TABLE test_missing_target2 AS
207 FROM test_missing_target x, test_missing_target y
209 GROUP BY x.b ORDER BY x.b;
210 SELECT * FROM test_missing_target2;
219 -- Functions and expressions
220 -- w/ existing GROUP BY target
221 SELECT a%2, count(b) FROM test_missing_target
222 GROUP BY test_missing_target.a%2
223 ORDER BY test_missing_target.a%2;
230 -- w/o existing GROUP BY target using a relation name in GROUP BY clause
231 SELECT count(c) FROM test_missing_target
232 GROUP BY lower(test_missing_target.c)
233 ORDER BY lower(test_missing_target.c);
242 -- w/o existing GROUP BY target and w/o existing a different ORDER BY target
244 SELECT count(a) FROM test_missing_target GROUP BY a ORDER BY b;
245 ERROR: column "test_missing_target.b" must appear in the GROUP BY clause or be used in an aggregate function
246 LINE 1: ...ECT count(a) FROM test_missing_target GROUP BY a ORDER BY b;
248 -- w/o existing GROUP BY target and w/o existing same ORDER BY target
249 SELECT count(b) FROM test_missing_target GROUP BY b/2 ORDER BY b/2;
257 -- w/ existing GROUP BY target using a relation name in target
258 SELECT lower(test_missing_target.c), count(c)
259 FROM test_missing_target GROUP BY lower(c) ORDER BY lower(c);
268 -- w/o existing GROUP BY target
269 SELECT a FROM test_missing_target ORDER BY upper(d);
284 -- w/o existing ORDER BY target
285 SELECT count(b) FROM test_missing_target
286 GROUP BY (b + 1) / 2 ORDER BY (b + 1) / 2 desc;
293 -- group w/o existing GROUP BY and ORDER BY target under ambiguous condition
295 SELECT count(x.a) FROM test_missing_target x, test_missing_target y
297 GROUP BY b/2 ORDER BY b/2;
298 ERROR: column reference "b" is ambiguous
299 LINE 3: GROUP BY b/2 ORDER BY b/2;
301 -- group w/ existing GROUP BY target under ambiguous condition
302 SELECT x.b/2, count(x.b) FROM test_missing_target x, test_missing_target y
304 GROUP BY x.b/2 ORDER BY x.b/2;
312 -- group w/o existing GROUP BY target under ambiguous condition
313 -- failure expected due to ambiguous b in count(b)
314 SELECT count(b) FROM test_missing_target x, test_missing_target y
317 ERROR: column reference "b" is ambiguous
318 LINE 1: SELECT count(b) FROM test_missing_target x, test_missing_tar...
320 -- group w/o existing GROUP BY target under ambiguous condition
322 CREATE TABLE test_missing_target3 AS
324 FROM test_missing_target x, test_missing_target y
326 GROUP BY x.b/2 ORDER BY x.b/2;
327 SELECT * FROM test_missing_target3;
336 DROP TABLE test_missing_target;
337 DROP TABLE test_missing_target2;
338 DROP TABLE test_missing_target3;