1998-02-18 08:32:17 +01:00
|
|
|
--
|
2000-01-06 07:41:55 +01:00
|
|
|
-- SUBSELECT
|
1998-02-18 08:32:17 +01:00
|
|
|
--
|
|
|
|
|
|
|
|
SELECT 1 AS one WHERE 1 IN (SELECT 1);
|
|
|
|
|
|
|
|
SELECT 1 AS zero WHERE 1 NOT IN (SELECT 1);
|
|
|
|
|
|
|
|
SELECT 1 AS zero WHERE 1 IN (SELECT 2);
|
|
|
|
|
|
|
|
-- Set up some simple test tables
|
|
|
|
|
|
|
|
CREATE TABLE SUBSELECT_TBL (
|
|
|
|
f1 integer,
|
|
|
|
f2 integer,
|
|
|
|
f3 float
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (1, 2, 3);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (2, 3, 4);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (3, 4, 5);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (1, 1, 1);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (2, 2, 2);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (3, 3, 3);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (6, 7, 8);
|
|
|
|
INSERT INTO SUBSELECT_TBL VALUES (8, 9, NULL);
|
|
|
|
|
|
|
|
SELECT '' AS eight, * FROM SUBSELECT_TBL;
|
|
|
|
|
|
|
|
-- Uncorrelated subselects
|
|
|
|
|
|
|
|
SELECT '' AS two, f1 AS "Constant Select" FROM SUBSELECT_TBL
|
|
|
|
WHERE f1 IN (SELECT 1);
|
|
|
|
|
|
|
|
SELECT '' AS six, f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
|
|
|
|
WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL);
|
|
|
|
|
|
|
|
SELECT '' AS six, f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
|
|
|
|
WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE
|
|
|
|
f2 IN (SELECT f1 FROM SUBSELECT_TBL));
|
|
|
|
|
2000-03-23 08:42:13 +01:00
|
|
|
SELECT '' AS three, f1, f2
|
|
|
|
FROM SUBSELECT_TBL
|
|
|
|
WHERE (f1, f2) NOT IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
|
|
|
|
WHERE f3 IS NOT NULL);
|
|
|
|
|
1998-02-18 08:32:17 +01:00
|
|
|
-- Correlated subselects
|
|
|
|
|
2000-03-23 08:42:13 +01:00
|
|
|
SELECT '' AS six, f1 AS "Correlated Field", f2 AS "Second Field"
|
|
|
|
FROM SUBSELECT_TBL upper
|
|
|
|
WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE f1 = upper.f1);
|
1998-02-18 08:32:17 +01:00
|
|
|
|
|
|
|
SELECT '' AS six, f1 AS "Correlated Field", f3 AS "Second Field"
|
2000-03-23 08:42:13 +01:00
|
|
|
FROM SUBSELECT_TBL upper
|
|
|
|
WHERE f1 IN
|
|
|
|
(SELECT f2 FROM SUBSELECT_TBL WHERE CAST(upper.f2 AS float) = f3);
|
1998-02-18 08:32:17 +01:00
|
|
|
|
|
|
|
SELECT '' AS six, f1 AS "Correlated Field", f3 AS "Second Field"
|
2000-03-23 08:42:13 +01:00
|
|
|
FROM SUBSELECT_TBL upper
|
|
|
|
WHERE f3 IN (SELECT upper.f1 + f2 FROM SUBSELECT_TBL
|
|
|
|
WHERE f2 = CAST(f3 AS integer));
|
1998-02-18 08:32:17 +01:00
|
|
|
|
|
|
|
SELECT '' AS five, f1 AS "Correlated Field"
|
|
|
|
FROM SUBSELECT_TBL
|
2000-03-23 08:42:13 +01:00
|
|
|
WHERE (f1, f2) IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
|
|
|
|
WHERE f3 IS NOT NULL);
|
1998-02-18 08:32:17 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Use some existing tables in the regression test
|
|
|
|
--
|
|
|
|
|
|
|
|
SELECT '' AS eight, ss.f1 AS "Correlated Field", ss.f3 AS "Second Field"
|
|
|
|
FROM SUBSELECT_TBL ss
|
2004-10-04 16:42:48 +02:00
|
|
|
WHERE f1 NOT IN (SELECT f1+1 FROM INT4_TBL
|
|
|
|
WHERE f1 != ss.f1 AND f1 < 2147483647);
|
1998-02-18 08:32:17 +01:00
|
|
|
|
2000-03-23 08:42:13 +01:00
|
|
|
select q1, float8(count(*)) / (select count(*) from int8_tbl)
|
2002-11-21 01:42:20 +01:00
|
|
|
from int8_tbl group by q1 order by q1;
|
2003-10-18 18:52:15 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test cases to catch unpleasant interactions between IN-join processing
|
|
|
|
-- and subquery pullup.
|
|
|
|
--
|
|
|
|
|
|
|
|
select count(*) from
|
|
|
|
(select 1 from tenk1 a
|
|
|
|
where unique1 IN (select hundred from tenk1 b)) ss;
|
|
|
|
select count(distinct ss.ten) from
|
|
|
|
(select ten from tenk1 a
|
|
|
|
where unique1 IN (select hundred from tenk1 b)) ss;
|
|
|
|
select count(*) from
|
|
|
|
(select 1 from tenk1 a
|
|
|
|
where unique1 IN (select distinct hundred from tenk1 b)) ss;
|
|
|
|
select count(distinct ss.ten) from
|
|
|
|
(select ten from tenk1 a
|
|
|
|
where unique1 IN (select distinct hundred from tenk1 b)) ss;
|
2004-05-11 04:21:39 +02:00
|
|
|
|
2005-07-15 19:09:26 +02:00
|
|
|
--
|
|
|
|
-- Test cases to check for overenthusiastic optimization of
|
|
|
|
-- "IN (SELECT DISTINCT ...)" and related cases. Per example from
|
|
|
|
-- Luca Pireddu and Michael Fuhr.
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TEMP TABLE foo (id integer);
|
|
|
|
CREATE TEMP TABLE bar (id1 integer, id2 integer);
|
|
|
|
|
|
|
|
INSERT INTO foo VALUES (1);
|
|
|
|
|
|
|
|
INSERT INTO bar VALUES (1, 1);
|
|
|
|
INSERT INTO bar VALUES (2, 2);
|
|
|
|
INSERT INTO bar VALUES (3, 1);
|
|
|
|
|
|
|
|
-- These cases require an extra level of distinct-ing above subquery s
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT DISTINCT id1, id2 FROM bar) AS s);
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT id1,id2 FROM bar GROUP BY id1,id2) AS s);
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT id1, id2 FROM bar UNION
|
|
|
|
SELECT id1, id2 FROM bar) AS s);
|
|
|
|
|
|
|
|
-- These cases do not
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT DISTINCT ON (id2) id1, id2 FROM bar) AS s);
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT id2 FROM bar GROUP BY id2) AS s);
|
|
|
|
SELECT * FROM foo WHERE id IN
|
|
|
|
(SELECT id2 FROM (SELECT id2 FROM bar UNION
|
|
|
|
SELECT id2 FROM bar) AS s);
|
|
|
|
|
2004-05-11 04:21:39 +02:00
|
|
|
--
|
|
|
|
-- Test case to catch problems with multiply nested sub-SELECTs not getting
|
|
|
|
-- recalculated properly. Per bug report from Didier Moens.
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TABLE orderstest (
|
|
|
|
approver_ref integer,
|
|
|
|
po_ref integer,
|
|
|
|
ordercancelled boolean
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO orderstest VALUES (1, 1, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 5, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 6, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 7, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 1, true);
|
|
|
|
INSERT INTO orderstest VALUES (66, 8, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 1, false);
|
|
|
|
INSERT INTO orderstest VALUES (77, 1, false);
|
|
|
|
INSERT INTO orderstest VALUES (1, 1, false);
|
|
|
|
INSERT INTO orderstest VALUES (66, 1, false);
|
|
|
|
INSERT INTO orderstest VALUES (1, 1, false);
|
|
|
|
|
|
|
|
CREATE VIEW orders_view AS
|
|
|
|
SELECT *,
|
|
|
|
(SELECT CASE
|
|
|
|
WHEN ord.approver_ref=1 THEN '---' ELSE 'Approved'
|
|
|
|
END) AS "Approved",
|
|
|
|
(SELECT CASE
|
|
|
|
WHEN ord.ordercancelled
|
|
|
|
THEN 'Cancelled'
|
|
|
|
ELSE
|
|
|
|
(SELECT CASE
|
|
|
|
WHEN ord.po_ref=1
|
|
|
|
THEN
|
|
|
|
(SELECT CASE
|
|
|
|
WHEN ord.approver_ref=1
|
|
|
|
THEN '---'
|
|
|
|
ELSE 'Approved'
|
|
|
|
END)
|
|
|
|
ELSE 'PO'
|
|
|
|
END)
|
|
|
|
END) AS "Status",
|
|
|
|
(CASE
|
|
|
|
WHEN ord.ordercancelled
|
|
|
|
THEN 'Cancelled'
|
|
|
|
ELSE
|
|
|
|
(CASE
|
|
|
|
WHEN ord.po_ref=1
|
|
|
|
THEN
|
|
|
|
(CASE
|
|
|
|
WHEN ord.approver_ref=1
|
|
|
|
THEN '---'
|
|
|
|
ELSE 'Approved'
|
|
|
|
END)
|
|
|
|
ELSE 'PO'
|
|
|
|
END)
|
|
|
|
END) AS "Status_OK"
|
|
|
|
FROM orderstest ord;
|
|
|
|
|
|
|
|
SELECT * FROM orders_view;
|
|
|
|
|
|
|
|
DROP TABLE orderstest cascade;
|
2005-11-23 18:21:04 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test cases to catch situations where rule rewriter fails to propagate
|
|
|
|
-- hasSubLinks flag correctly. Per example from Kyle Bateman.
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table parts (
|
|
|
|
partnum text,
|
|
|
|
cost float8
|
|
|
|
);
|
|
|
|
|
|
|
|
create temp table shipped (
|
|
|
|
ttype char(2),
|
|
|
|
ordnum int4,
|
|
|
|
partnum text,
|
|
|
|
value float8
|
|
|
|
);
|
|
|
|
|
|
|
|
create temp view shipped_view as
|
|
|
|
select * from shipped where ttype = 'wt';
|
|
|
|
|
|
|
|
create rule shipped_view_insert as on insert to shipped_view do instead
|
|
|
|
insert into shipped values('wt', new.ordnum, new.partnum, new.value);
|
|
|
|
|
|
|
|
insert into parts (partnum, cost) values (1, 1234.56);
|
|
|
|
|
|
|
|
insert into shipped_view (ordnum, partnum, value)
|
2007-06-05 23:31:09 +02:00
|
|
|
values (0, 1, (select cost from parts where partnum = '1'));
|
2005-11-23 18:21:04 +01:00
|
|
|
|
|
|
|
select * from shipped_view;
|
|
|
|
|
|
|
|
create rule shipped_view_update as on update to shipped_view do instead
|
|
|
|
update shipped set partnum = new.partnum, value = new.value
|
|
|
|
where ttype = new.ttype and ordnum = new.ordnum;
|
|
|
|
|
|
|
|
update shipped_view set value = 11
|
|
|
|
from int4_tbl a join int4_tbl b
|
|
|
|
on (a.f1 = (select f1 from int4_tbl c where c.f1=b.f1))
|
|
|
|
where ordnum = a.f1;
|
|
|
|
|
|
|
|
select * from shipped_view;
|
|
|
|
|
|
|
|
select f1, ss1 as relabel from
|
|
|
|
(select *, (select sum(f1) from int4_tbl b where f1 >= a.f1) as ss1
|
|
|
|
from int4_tbl a) ss;
|
2007-07-18 23:40:57 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test cases involving PARAM_EXEC parameters and min/max index optimizations.
|
|
|
|
-- Per bug report from David Sanchez i Gregori.
|
|
|
|
--
|
|
|
|
|
|
|
|
select * from (
|
|
|
|
select max(unique1) from tenk1 as a
|
|
|
|
where exists (select 1 from tenk1 as b where b.thousand = a.unique2)
|
|
|
|
) ss;
|
|
|
|
|
|
|
|
select * from (
|
|
|
|
select min(unique1) from tenk1 as a
|
|
|
|
where not exists (select 1 from tenk1 as b where b.unique2 = 10000)
|
|
|
|
) ss;
|
Fix convert_IN_to_join to properly handle the case where the subselect's
output is not of the same type that's needed for the IN comparison (ie,
where the parser inserted an implicit coercion above the subselect result).
We should record the coerced expression, not just a raw Var referencing
the subselect output, as the quantity that needs to be unique-ified if
we choose to implement the IN as Unique followed by a plain join.
As of 8.3 this error was causing crashes, as seen in bug #4113 from Javier
Hernandez, because the executor was being told to hash or sort the raw
subselect output column using operators appropriate to the coerced type.
In prior versions there was no crash because the executor chose the
hash or sort operators for itself based on the column type it saw.
However, that's still not really right, because what's unique for one data
type might not be unique for another. In corner cases we could get multiple
outputs of a row that should appear only once, as demonstrated by the
regression test case included in this commit.
However, this patch doesn't apply cleanly to 8.2 or before, and the code
involved has shifted enough over time that I'm hesitant to try to back-patch.
Given the lack of complaints from the field about such corner cases, I think
the bug may not be important enough to risk breaking other things with a
back-patch.
2008-04-21 22:54:15 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test that an IN implemented using a UniquePath does unique-ification
|
|
|
|
-- with the right semantics, as per bug #4113. (Unfortunately we have
|
|
|
|
-- no simple way to ensure that this test case actually chooses that type
|
|
|
|
-- of plan, but it does in releases 7.4-8.3. Note that an ordering difference
|
|
|
|
-- here might mean that some other plan type is being used, rendering the test
|
|
|
|
-- pointless.)
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table numeric_table (num_col numeric);
|
|
|
|
insert into numeric_table values (1), (1.000000000000000000001), (2), (3);
|
|
|
|
|
|
|
|
create temp table float_table (float_col float8);
|
|
|
|
insert into float_table values (1), (2), (3);
|
|
|
|
|
|
|
|
select * from float_table
|
|
|
|
where float_col in (select num_col from numeric_table);
|
|
|
|
|
|
|
|
select * from numeric_table
|
|
|
|
where num_col in (select float_col from float_table);
|