forked from huawei/openGauss-server
1509 lines
47 KiB
Plaintext
1509 lines
47 KiB
Plaintext
create database pl_test_outerjoin DBCOMPATIBILITY 'pg';
|
|
\c pl_test_outerjoin;
|
|
create schema plus_outerjoin;
|
|
set current_schema='plus_outerjoin';
|
|
create table t11(c1 int, c2 int, c3 int);
|
|
create table t12(c1 int, c2 int, c3 int);
|
|
create table t13(c1 int, c2 int, c3 int);
|
|
create table t14(A4 int, B4 int, c4 int);
|
|
create table t_a (id int, name varchar(10), code int);
|
|
create table t_b (id int, name varchar(10), code int);
|
|
-- insert base
|
|
insert into t11 select v,v,v from generate_series(1,10) as v;
|
|
insert into t12 select * from t11;
|
|
insert into t13 select * from t11;
|
|
insert into t14 select v,v,v from generate_series(1,30) as v;
|
|
insert into t_a values (1, 'tom', 3);
|
|
insert into t_b values (1, 'bat', 6);
|
|
-- insert t11 t12, t13's not match values
|
|
insert into t11 select v,v,v from generate_series(11,15) as v;
|
|
insert into t12 select v,v,v from generate_series(16,20) as v;
|
|
insert into t13 select v,v,v from generate_series(21,25) as v;
|
|
/* "(+)" used in simple case: single join condition, two relation */
|
|
select * from t11, t12 where t11.c1 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
11 | 11 | 11 | | |
|
|
12 | 12 | 12 | | |
|
|
13 | 13 | 13 | | |
|
|
14 | 14 | 14 | | |
|
|
15 | 15 | 15 | | |
|
|
(15 rows)
|
|
|
|
explain(costs off)
|
|
select * from t11, t12 where t11.c1 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
QUERY PLAN
|
|
------------------------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1, t11.c2, t11.c3, t12.c1, t12.c2, t12.c3
|
|
-> Hash Left Join
|
|
Hash Cond: (t11.c1 = t12.c2)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> Seq Scan on t12
|
|
(7 rows)
|
|
|
|
select * from t11, t12 where t11.c1(+) = t12.c2 order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
| | | 16 | 16 | 16
|
|
| | | 17 | 17 | 17
|
|
| | | 18 | 18 | 18
|
|
| | | 19 | 19 | 19
|
|
| | | 20 | 20 | 20
|
|
(15 rows)
|
|
|
|
explain(costs off)
|
|
select * from t11, t12 where t11.c1(+) = t12.c2 order by 1,2,3,4,5,6;
|
|
QUERY PLAN
|
|
------------------------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1, t11.c2, t11.c3, t12.c1, t12.c2, t12.c3
|
|
-> Hash Left Join
|
|
Hash Cond: (t12.c2 = t11.c1)
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Seq Scan on t11
|
|
(7 rows)
|
|
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and t11.c1 > 10 order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
11 | 11 | 11 | | |
|
|
12 | 12 | 12 | | |
|
|
13 | 13 | 13 | | |
|
|
14 | 14 | 14 | | |
|
|
15 | 15 | 15 | | |
|
|
(5 rows)
|
|
|
|
explain(costs off)
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and t11.c1 > 10 order by 1,2,3,4,5,6;
|
|
QUERY PLAN
|
|
------------------------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1, t11.c2, t11.c3, t12.c1, t12.c2, t12.c3
|
|
-> Hash Right Join
|
|
Hash Cond: (t12.c1 = t11.c1)
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Seq Scan on t11
|
|
Filter: (c1 > 10)
|
|
(8 rows)
|
|
|
|
-- with "in" operator together--
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and t11.c1 in (select c3 from t13 where c1 > 8) order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
(2 rows)
|
|
|
|
explain(costs off)
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and t11.c1 in (select c3 from t13 where c1 > 8) order by 1,2,3,4,5,6;
|
|
QUERY PLAN
|
|
------------------------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1, t11.c2, t11.c3, t12.c1, t12.c2, t12.c3
|
|
-> Hash Right Join
|
|
Hash Cond: (t12.c1 = t11.c1)
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Hash Join
|
|
Hash Cond: (t11.c1 = t13.c3)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> HashAggregate
|
|
Group By Key: t13.c3
|
|
-> Seq Scan on t13
|
|
Filter: (c1 > 8)
|
|
(14 rows)
|
|
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and t12.c1 in (select c3 from t13 where c1 > 8) order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
(2 rows)
|
|
|
|
explain(costs off)
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and t12.c1 in (select c3 from t13 where c1 > 8) order by 1,2,3,4,5,6;
|
|
QUERY PLAN
|
|
--------------------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1, t11.c2, t11.c3, t12.c2, t12.c3
|
|
-> Hash Join
|
|
Hash Cond: (t11.c1 = t12.c1)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> Hash Join
|
|
Hash Cond: (t12.c1 = t13.c3)
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> HashAggregate
|
|
Group By Key: t13.c3
|
|
-> Seq Scan on t13
|
|
Filter: (c1 > 8)
|
|
(14 rows)
|
|
|
|
/* multi-join-condition */
|
|
select * from t11, t12 where t11.c1(+) = t12.c1 and t11.c2(+) = t12.c2 order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
| | | 16 | 16 | 16
|
|
| | | 17 | 17 | 17
|
|
| | | 18 | 18 | 18
|
|
| | | 19 | 19 | 19
|
|
| | | 20 | 20 | 20
|
|
(15 rows)
|
|
|
|
explain(costs off)
|
|
select * from t11, t12 where t11.c1(+) = t12.c1 and t11.c2(+) = t12.c2 order by 1,2,3,4,5,6;
|
|
QUERY PLAN
|
|
--------------------------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1, t11.c2, t11.c3, t12.c1, t12.c2, t12.c3
|
|
-> Hash Left Join
|
|
Hash Cond: ((t12.c1 = t11.c1) AND (t12.c2 = t11.c2))
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Seq Scan on t11
|
|
(7 rows)
|
|
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and t11.c2 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
11 | 11 | 11 | | |
|
|
12 | 12 | 12 | | |
|
|
13 | 13 | 13 | | |
|
|
14 | 14 | 14 | | |
|
|
15 | 15 | 15 | | |
|
|
(15 rows)
|
|
|
|
explain(costs off)
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and t11.c2 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
QUERY PLAN
|
|
--------------------------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1, t11.c2, t11.c3, t12.c1, t12.c2, t12.c3
|
|
-> Hash Left Join
|
|
Hash Cond: ((t11.c1 = t12.c1) AND (t11.c2 = t12.c2))
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> Seq Scan on t12
|
|
(7 rows)
|
|
|
|
/* (+) used in multi-relation join with multi-join-condition */
|
|
select t14.c4, t11.c1, t12.c2, t13.c3
|
|
from t11,t12,t13,t14
|
|
where t11.c1(+) = t14.a4 and
|
|
t12.c1(+) = t14.a4 and
|
|
t13.c1(+) = t14.a4 and
|
|
t14.b4 >= 8
|
|
order by t14.c4,t11.c1, t12.c2, t13.c3;
|
|
c4 | c1 | c2 | c3
|
|
----+----+----+----
|
|
8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10
|
|
11 | 11 | |
|
|
12 | 12 | |
|
|
13 | 13 | |
|
|
14 | 14 | |
|
|
15 | 15 | |
|
|
16 | | 16 |
|
|
17 | | 17 |
|
|
18 | | 18 |
|
|
19 | | 19 |
|
|
20 | | 20 |
|
|
21 | | | 21
|
|
22 | | | 22
|
|
23 | | | 23
|
|
24 | | | 24
|
|
25 | | | 25
|
|
26 | | |
|
|
27 | | |
|
|
28 | | |
|
|
29 | | |
|
|
30 | | |
|
|
(23 rows)
|
|
|
|
explain(costs off)
|
|
select t14.c4, t11.c1, t12.c2, t13.c3
|
|
from t11,t12,t13,t14
|
|
where t11.c1(+) = t14.a4 and
|
|
t12.c1(+) = t14.a4 and
|
|
t13.c1(+) = t14.a4 and
|
|
t14.b4 >= 8
|
|
order by t14.c4,t11.c1, t12.c2, t13.c3;
|
|
QUERY PLAN
|
|
---------------------------------------------------
|
|
Sort
|
|
Sort Key: t14.c4, t11.c1, t12.c2, t13.c3
|
|
-> Hash Left Join
|
|
Hash Cond: (t14.a4 = t13.c1)
|
|
-> Hash Left Join
|
|
Hash Cond: (t14.a4 = t12.c1)
|
|
-> Hash Right Join
|
|
Hash Cond: (t11.c1 = t14.a4)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> Seq Scan on t14
|
|
Filter: (b4 >= 8)
|
|
-> Hash
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Seq Scan on t13
|
|
(16 rows)
|
|
|
|
select t14.c4, t11.c1, t12.c2, t13.c3
|
|
from t11,t12,t13,t14
|
|
where t11.c1(+) = t14.a4 and
|
|
t14.a4 = t12.c1(+)and
|
|
t13.c1(+) = t14.a4 and
|
|
t14.b4 >= 8
|
|
order by t14.c4;
|
|
c4 | c1 | c2 | c3
|
|
----+----+----+----
|
|
8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10
|
|
11 | 11 | |
|
|
12 | 12 | |
|
|
13 | 13 | |
|
|
14 | 14 | |
|
|
15 | 15 | |
|
|
16 | | 16 |
|
|
17 | | 17 |
|
|
18 | | 18 |
|
|
19 | | 19 |
|
|
20 | | 20 |
|
|
21 | | | 21
|
|
22 | | | 22
|
|
23 | | | 23
|
|
24 | | | 24
|
|
25 | | | 25
|
|
26 | | |
|
|
27 | | |
|
|
28 | | |
|
|
29 | | |
|
|
30 | | |
|
|
(23 rows)
|
|
|
|
explain(costs off)
|
|
select t14.c4, t11.c1, t12.c2, t13.c3
|
|
from t11,t12,t13,t14
|
|
where t11.c1(+) = t14.a4 and
|
|
t14.a4 = t12.c1(+)and
|
|
t13.c1(+) = t14.a4 and
|
|
t14.b4 >= 8
|
|
order by t14.c4,t11.c1, t12.c2, t13.c3;
|
|
QUERY PLAN
|
|
---------------------------------------------------
|
|
Sort
|
|
Sort Key: t14.c4, t11.c1, t12.c2, t13.c3
|
|
-> Hash Left Join
|
|
Hash Cond: (t14.a4 = t13.c1)
|
|
-> Hash Left Join
|
|
Hash Cond: (t14.a4 = t12.c1)
|
|
-> Hash Right Join
|
|
Hash Cond: (t11.c1 = t14.a4)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> Seq Scan on t14
|
|
Filter: (b4 >= 8)
|
|
-> Hash
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Seq Scan on t13
|
|
(16 rows)
|
|
|
|
/* (+) used in subquery */
|
|
select * from t14, (select t12.c1 as dt_col1, t13.c2 as dt_col2 from t12,t13 where t12.c1 = t13.c1(+)) dt where t14.a4 = dt.dt_col1 order by 1,2,3,4,5;
|
|
a4 | b4 | c4 | dt_col1 | dt_col2
|
|
----+----+----+---------+---------
|
|
1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10
|
|
16 | 16 | 16 | 16 |
|
|
17 | 17 | 17 | 17 |
|
|
18 | 18 | 18 | 18 |
|
|
19 | 19 | 19 | 19 |
|
|
20 | 20 | 20 | 20 |
|
|
(15 rows)
|
|
|
|
explain(costs off)
|
|
select * from t14, (select t12.c1 as dt_col1, t13.c2 as dt_col2 from t12,t13 where t12.c1 = t13.c1(+)) dt where t14.a4 = dt.dt_col1 order by 1,2,3,4,5;
|
|
QUERY PLAN
|
|
--------------------------------------------
|
|
Sort
|
|
Sort Key: t14.a4, t14.b4, t14.c4, t13.c2
|
|
-> Hash Left Join
|
|
Hash Cond: (t12.c1 = t13.c1)
|
|
-> Hash Join
|
|
Hash Cond: (t14.a4 = t12.c1)
|
|
-> Seq Scan on t14
|
|
-> Hash
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Seq Scan on t13
|
|
(11 rows)
|
|
|
|
/* (+) used in subLink */
|
|
select t11.c1,t11.c2, t12.c1, t12.c2
|
|
from t11, t12
|
|
where t11.c1 = t12.c1(+) and t11.c2 in (
|
|
select t14.a4
|
|
from t13, t14
|
|
where t14.b4(+) = t13.c2
|
|
) order by 1,2,3,4;
|
|
c1 | c2 | c1 | c2
|
|
----+----+----+----
|
|
1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10
|
|
(10 rows)
|
|
|
|
explain (costs off)
|
|
select t11.c1,t11.c2, t12.c1, t12.c2
|
|
from t11, t12
|
|
where t11.c1 = t12.c1(+) and t11.c2 in (
|
|
select t14.a4
|
|
from t13, t14
|
|
where t14.b4(+) = t13.c2
|
|
) order by 1,2,3,4;
|
|
QUERY PLAN
|
|
--------------------------------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1, t11.c2, t12.c1, t12.c2
|
|
-> Hash Right Join
|
|
Hash Cond: (t12.c1 = t11.c1)
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Hash Join
|
|
Hash Cond: (t11.c2 = t14.a4)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> HashAggregate
|
|
Group By Key: t14.a4
|
|
-> Hash Join
|
|
Hash Cond: (t14.b4 = t13.c2)
|
|
-> Seq Scan on t14
|
|
-> Hash
|
|
-> Seq Scan on t13
|
|
(17 rows)
|
|
|
|
select t11.c1,t11.c2, t12.c1, t12.c2
|
|
from t11, t12
|
|
where t11.c1 = t12.c1(+) and t11.c2 in (
|
|
select t14.a4
|
|
from t13, t14
|
|
where t14.b4 = t13.c2(+)
|
|
) order by 1,2,3,4;
|
|
c1 | c2 | c1 | c2
|
|
----+----+----+----
|
|
1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10
|
|
11 | 11 | |
|
|
12 | 12 | |
|
|
13 | 13 | |
|
|
14 | 14 | |
|
|
15 | 15 | |
|
|
(15 rows)
|
|
|
|
explain (costs off)
|
|
select t11.c1,t11.c2, t12.c1, t12.c2
|
|
from t11, t12
|
|
where t11.c1 = t12.c1(+) and t11.c2 in (
|
|
select t14.a4
|
|
from t13, t14
|
|
where t14.b4 = t13.c2(+)
|
|
) order by 1,2,3,4;
|
|
QUERY PLAN
|
|
--------------------------------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1, t11.c2, t12.c1, t12.c2
|
|
-> Hash Right Join
|
|
Hash Cond: (t12.c1 = t11.c1)
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Hash Join
|
|
Hash Cond: (t11.c2 = t14.a4)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> HashAggregate
|
|
Group By Key: t14.a4
|
|
-> Hash Left Join
|
|
Hash Cond: (t14.b4 = t13.c2)
|
|
-> Seq Scan on t14
|
|
-> Hash
|
|
-> Seq Scan on t13
|
|
(17 rows)
|
|
|
|
select t11.c1,t11.c2, t12.c1, t12.c2
|
|
from t11, t12
|
|
where t11.c1 = t12.c1(+) and t11.c2 in (
|
|
select t14.a4
|
|
from t13, t14
|
|
where t14.b4 = t13.c2
|
|
) order by 1,2,3,4;
|
|
c1 | c2 | c1 | c2
|
|
----+----+----+----
|
|
1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10
|
|
(10 rows)
|
|
|
|
explain (costs off)
|
|
select t11.c1,t11.c2, t12.c1, t12.c2
|
|
from t11, t12
|
|
where t11.c1 = t12.c1(+) and t11.c2 in (
|
|
select t14.a4
|
|
from t13, t14
|
|
where t14.b4 = t13.c2
|
|
) order by 1,2,3,4;
|
|
QUERY PLAN
|
|
--------------------------------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1, t11.c2, t12.c1, t12.c2
|
|
-> Hash Right Join
|
|
Hash Cond: (t12.c1 = t11.c1)
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Hash Join
|
|
Hash Cond: (t11.c2 = t14.a4)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> HashAggregate
|
|
Group By Key: t14.a4
|
|
-> Hash Join
|
|
Hash Cond: (t13.c2 = t14.b4)
|
|
-> Seq Scan on t13
|
|
-> Hash
|
|
-> Seq Scan on t14
|
|
(17 rows)
|
|
|
|
/* (+) used in subquery-RTE join with relation-RTE */
|
|
select *
|
|
from t14, (
|
|
select t12.c1 as dt_col1, t13.c2 as dt_col2
|
|
from t12,t13
|
|
where t12.c1 = t13.c1(+)
|
|
) dt where t14.a4 = dt.dt_col1(+) order by 1,2,3,4;
|
|
a4 | b4 | c4 | dt_col1 | dt_col2
|
|
----+----+----+---------+---------
|
|
1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10
|
|
11 | 11 | 11 | |
|
|
12 | 12 | 12 | |
|
|
13 | 13 | 13 | |
|
|
14 | 14 | 14 | |
|
|
15 | 15 | 15 | |
|
|
16 | 16 | 16 | 16 |
|
|
17 | 17 | 17 | 17 |
|
|
18 | 18 | 18 | 18 |
|
|
19 | 19 | 19 | 19 |
|
|
20 | 20 | 20 | 20 |
|
|
21 | 21 | 21 | |
|
|
22 | 22 | 22 | |
|
|
23 | 23 | 23 | |
|
|
24 | 24 | 24 | |
|
|
25 | 25 | 25 | |
|
|
26 | 26 | 26 | |
|
|
27 | 27 | 27 | |
|
|
28 | 28 | 28 | |
|
|
29 | 29 | 29 | |
|
|
30 | 30 | 30 | |
|
|
(30 rows)
|
|
|
|
explain (costs off)
|
|
select *
|
|
from t14, (
|
|
select t12.c1 as dt_col1, t13.c2 as dt_col2
|
|
from t12,t13
|
|
where t12.c1 = t13.c1(+)
|
|
) dt where t14.a4 = dt.dt_col1(+) order by 1,2,3,4;
|
|
QUERY PLAN
|
|
--------------------------------------------
|
|
Sort
|
|
Sort Key: t14.a4, t14.b4, t14.c4, t12.c1
|
|
-> Hash Left Join
|
|
Hash Cond: (t12.c1 = t13.c1)
|
|
-> Hash Left Join
|
|
Hash Cond: (t14.a4 = t12.c1)
|
|
-> Seq Scan on t14
|
|
-> Hash
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Seq Scan on t13
|
|
(11 rows)
|
|
|
|
select *
|
|
from t14, (
|
|
select t12.c1 as dt_col1, t13.c2 as dt_col2
|
|
from t12,t13
|
|
where t12.c1 = t13.c1(+)
|
|
) dt where t14.a4(+) = dt.dt_col1 order by 1,2,3,4;
|
|
a4 | b4 | c4 | dt_col1 | dt_col2
|
|
----+----+----+---------+---------
|
|
1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10
|
|
16 | 16 | 16 | 16 |
|
|
17 | 17 | 17 | 17 |
|
|
18 | 18 | 18 | 18 |
|
|
19 | 19 | 19 | 19 |
|
|
20 | 20 | 20 | 20 |
|
|
(15 rows)
|
|
|
|
explain (costs off)
|
|
select *
|
|
from t14, (
|
|
select t12.c1 as dt_col1, t13.c2 as dt_col2
|
|
from t12,t13
|
|
where t12.c1 = t13.c1(+)
|
|
) dt where t14.a4(+) = dt.dt_col1 order by 1,2,3,4;
|
|
QUERY PLAN
|
|
--------------------------------------------
|
|
Sort
|
|
Sort Key: t14.a4, t14.b4, t14.c4, t12.c1
|
|
-> Hash Left Join
|
|
Hash Cond: (t12.c1 = t14.a4)
|
|
-> Hash Left Join
|
|
Hash Cond: (t12.c1 = t13.c1)
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Seq Scan on t13
|
|
-> Hash
|
|
-> Seq Scan on t14
|
|
(11 rows)
|
|
|
|
/* (+) used in setop */
|
|
select *
|
|
from
|
|
(
|
|
select t11.c1,t11.c2 from t11, t12 where t11.c1 = t12.c1(+)
|
|
union all
|
|
select t11.c1,t11.c2 from t11, t12 where t11.c1(+) = t12.c1
|
|
) order by 1,2;
|
|
c1 | c2
|
|
----+----
|
|
1 | 1
|
|
1 | 1
|
|
2 | 2
|
|
2 | 2
|
|
3 | 3
|
|
3 | 3
|
|
4 | 4
|
|
4 | 4
|
|
5 | 5
|
|
5 | 5
|
|
6 | 6
|
|
6 | 6
|
|
7 | 7
|
|
7 | 7
|
|
8 | 8
|
|
8 | 8
|
|
9 | 9
|
|
9 | 9
|
|
10 | 10
|
|
10 | 10
|
|
11 | 11
|
|
12 | 12
|
|
13 | 13
|
|
14 | 14
|
|
15 | 15
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(30 rows)
|
|
|
|
explain (costs off)
|
|
select *
|
|
from
|
|
(
|
|
select t11.c1,t11.c2 from t11, t12 where t11.c1 = t12.c1(+)
|
|
union all
|
|
select t11.c1,t11.c2 from t11, t12 where t11.c1(+) = t12.c1
|
|
) order by 1,2;
|
|
QUERY PLAN
|
|
--------------------------------------------------------------------------------
|
|
Sort
|
|
Sort Key: plus_outerjoin.t11.c1, plus_outerjoin.t11.c2
|
|
-> Result
|
|
-> Append
|
|
-> Hash Left Join
|
|
Hash Cond: (plus_outerjoin.t11.c1 = plus_outerjoin.t12.c1)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> Seq Scan on t12
|
|
-> Hash Left Join
|
|
Hash Cond: (plus_outerjoin.t12.c1 = plus_outerjoin.t11.c1)
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Seq Scan on t11
|
|
(14 rows)
|
|
|
|
select *
|
|
from
|
|
(
|
|
select t11.c1,t11.c2 from t11, t12 where t11.c1 = t12.c1(+)
|
|
union
|
|
select t11.c1,t11.c2 from t11, t12 where t11.c1(+) = t12.c1
|
|
) order by 1,2;
|
|
c1 | c2
|
|
----+----
|
|
1 | 1
|
|
2 | 2
|
|
3 | 3
|
|
4 | 4
|
|
5 | 5
|
|
6 | 6
|
|
7 | 7
|
|
8 | 8
|
|
9 | 9
|
|
10 | 10
|
|
11 | 11
|
|
12 | 12
|
|
13 | 13
|
|
14 | 14
|
|
15 | 15
|
|
|
|
|
(16 rows)
|
|
|
|
explain (costs off)
|
|
select *
|
|
from
|
|
(
|
|
select t11.c1,t11.c2 from t11, t12 where t11.c1 = t12.c1(+)
|
|
union
|
|
select t11.c1,t11.c2 from t11, t12 where t11.c1(+) = t12.c1
|
|
) order by 1,2;
|
|
QUERY PLAN
|
|
--------------------------------------------------------------------------------
|
|
Sort
|
|
Sort Key: plus_outerjoin.t11.c1, plus_outerjoin.t11.c2
|
|
-> HashAggregate
|
|
Group By Key: plus_outerjoin.t11.c1, plus_outerjoin.t11.c2
|
|
-> Append
|
|
-> Hash Left Join
|
|
Hash Cond: (plus_outerjoin.t11.c1 = plus_outerjoin.t12.c1)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> Seq Scan on t12
|
|
-> Hash Left Join
|
|
Hash Cond: (plus_outerjoin.t12.c1 = plus_outerjoin.t11.c1)
|
|
-> Seq Scan on t12
|
|
-> Hash
|
|
-> Seq Scan on t11
|
|
(15 rows)
|
|
|
|
--used in correlative query--
|
|
--treated as inner join when outer relation is in outer query--
|
|
select t11.c1, t11.c1 from t11 where t11.c1 > 5 and t11.c2 in (select t12.c2 from t12 where t11.c1 = t12.c1(+)) order by 1,2;
|
|
c1 | c1
|
|
----+----
|
|
6 | 6
|
|
7 | 7
|
|
8 | 8
|
|
9 | 9
|
|
10 | 10
|
|
(5 rows)
|
|
|
|
select t11.c1, t11.c1, (select t12.c2 from t12 where t11.c1 = t12.c1(+)) from t11 where t11.c1 > 5 order by 1,2,3;
|
|
c1 | c1 | c2
|
|
----+----+----
|
|
6 | 6 | 6
|
|
7 | 7 | 7
|
|
8 | 8 | 8
|
|
9 | 9 | 9
|
|
10 | 10 | 10
|
|
11 | 11 |
|
|
12 | 12 |
|
|
13 | 13 |
|
|
14 | 14 |
|
|
15 | 15 |
|
|
(10 rows)
|
|
|
|
explain (costs off)
|
|
select t11.c1, t11.c1 from t11 where t11.c1 > 5 and t11.c2 in (select t12.c2 from t12 where t11.c1 = t12.c1(+)) order by 1,2;
|
|
QUERY PLAN
|
|
--------------------------------------------
|
|
Sort
|
|
Sort Key: t11.c1
|
|
-> Seq Scan on t11
|
|
Filter: ((c1 > 5) AND (SubPlan 1))
|
|
SubPlan 1
|
|
-> Seq Scan on t12
|
|
Filter: (t11.c1 = c1)
|
|
(7 rows)
|
|
|
|
--report error, (+)can only be used in the relation in current query level --
|
|
select t11.c1, t11.c1 from t11 where t11.c1 > 5 and t11.c2 in (select t12.c2 from t12 where t12.c1 = t11.c1(+)) order by 1,2;
|
|
ERROR: Operator "(+)" can't specify on "t11" which cannot be referenced from this part of the query.
|
|
LINE 1: ... t11.c2 in (select t12.c2 from t12 where t12.c1 = t11.c1(+))...
|
|
^
|
|
HINT: "t11" should be in current query level.
|
|
-- (+) used in common relation join with tablefunc
|
|
select t11.c1, t11.c2, tf.c1 from t11, unnest(array[-2, -1, 0, 1,2,3]) as tf(c1) where t11.c1(+) = tf.c1 order by 1,3;
|
|
c1 | c2 | c1
|
|
----+----+----
|
|
1 | 1 | 1
|
|
2 | 2 | 2
|
|
3 | 3 | 3
|
|
| | -2
|
|
| | -1
|
|
| | 0
|
|
(6 rows)
|
|
|
|
select t11.c1, t11.c2, tf.c1 from t11, unnest(array[-2, -1, 0, 1,2,3]) as tf(c1) where t11.c1 = tf.c1(+) order by 1,3;
|
|
c1 | c2 | c1
|
|
----+----+----
|
|
1 | 1 | 1
|
|
2 | 2 | 2
|
|
3 | 3 | 3
|
|
4 | 4 |
|
|
5 | 5 |
|
|
6 | 6 |
|
|
7 | 7 |
|
|
8 | 8 |
|
|
9 | 9 |
|
|
10 | 10 |
|
|
11 | 11 |
|
|
12 | 12 |
|
|
13 | 13 |
|
|
14 | 14 |
|
|
15 | 15 |
|
|
(15 rows)
|
|
|
|
--used with or clause
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and (t12.c1 > 2 or t12.c1 < 13) order by 1,2,3,4;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
(10 rows)
|
|
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and (t11.c1 > 2 or t12.c1 < 13) order by 1,2,3,4;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
11 | 11 | 11 | | |
|
|
12 | 12 | 12 | | |
|
|
13 | 13 | 13 | | |
|
|
14 | 14 | 14 | | |
|
|
15 | 15 | 15 | | |
|
|
(15 rows)
|
|
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and (t11.c1 > 10 or t11.c1 < 13) order by 1,2,3,4;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
11 | 11 | 11 | | |
|
|
12 | 12 | 12 | | |
|
|
13 | 13 | 13 | | |
|
|
14 | 14 | 14 | | |
|
|
15 | 15 | 15 | | |
|
|
(15 rows)
|
|
|
|
-- Report Error with "or"
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) or 1=1;
|
|
ERROR: Operator "(+)" is not allowed used with "OR" together
|
|
select * from t11, t12 where t11.c2 > 1 or (t11.c2 > 10 and t11.c1 = t12.c1(+) and 1=1);
|
|
ERROR: Operator "(+)" is not allowed used with "OR" together
|
|
--other join condition kind
|
|
select * from t11, t12 where t11.c1 < t12.c2(+) order by 1,2,3,4,5,6 limit 3;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 2 | 2 | 2
|
|
1 | 1 | 1 | 3 | 3 | 3
|
|
1 | 1 | 1 | 4 | 4 | 4
|
|
(3 rows)
|
|
|
|
/* ignore (+) */
|
|
select * from t11, t12 where t11.c1(+) = 1 order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
1 | 1 | 1 | 2 | 2 | 2
|
|
1 | 1 | 1 | 3 | 3 | 3
|
|
1 | 1 | 1 | 4 | 4 | 4
|
|
1 | 1 | 1 | 5 | 5 | 5
|
|
1 | 1 | 1 | 6 | 6 | 6
|
|
1 | 1 | 1 | 7 | 7 | 7
|
|
1 | 1 | 1 | 8 | 8 | 8
|
|
1 | 1 | 1 | 9 | 9 | 9
|
|
1 | 1 | 1 | 10 | 10 | 10
|
|
1 | 1 | 1 | 16 | 16 | 16
|
|
1 | 1 | 1 | 17 | 17 | 17
|
|
1 | 1 | 1 | 18 | 18 | 18
|
|
1 | 1 | 1 | 19 | 19 | 19
|
|
1 | 1 | 1 | 20 | 20 | 20
|
|
(15 rows)
|
|
|
|
/* report error when some join condition with more than one "(+)" */
|
|
select * from t11, t12 where t11.c1 = t12.c1(+) and t11.c2(+) = t12.c2(+) order by 1,2,3,4,5,6;
|
|
ERROR: Operator "(+)" can't be specified on more than one relation in one join condition
|
|
HINT: "t11", "t12"...are specified Operator "(+)" in one condition.
|
|
--report error when there is "(+)" in whereclause and join in fromclause.
|
|
select * from t11 left join t12 on t11.c1 = t12.c2 where t11.c1 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
ERROR: Operator "(+)" and Join in FromClause can't be used together
|
|
--report error when there is more than one left-RTE with same right-RTE--
|
|
select * from t11, t13, t12 where t11.c1 = t12.c2(+) and t13.c3 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
ERROR: "t12" can't outer join with more than one relation.
|
|
--should specified "(+)" on all join condition, else it will treated as innner join--
|
|
select * from t11, t12 where t11.c1 = t12.c2(+) and t11.c3 = t12.c2 order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
(10 rows)
|
|
|
|
select * from t11, t12, t13 where t11.c1 = t12.c2(+) and t13.c3(+) = t12.c2 and t13.c2 = t12.c1 order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10 | 10 | 10 | 10
|
|
(10 rows)
|
|
|
|
--t13,t14 will treated as innner join , and t11,t12 will treated as outer join.--
|
|
select * from t11, t12, t13 , t14 where t11.c1 = t12.c2(+) and t13.c3(+) = t14.C4 and t13.c2 = t14.a4 order by 1,2,3,4,5,6,7,8,9,10,11,12 limit 3;
|
|
c1 | c2 | c3 | c1 | c2 | c3 | c1 | c2 | c3 | a4 | b4 | c4
|
|
----+----+----+----+----+----+----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
|
|
1 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 | 2 | 2
|
|
1 | 1 | 1 | 1 | 1 | 1 | 3 | 3 | 3 | 3 | 3 | 3
|
|
(3 rows)
|
|
|
|
--report error when there is "(+)" in whereclause and join in fromclause.--
|
|
select * from t11 left join t12 on t11.c1 = t12.c2 where t11.c1 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
ERROR: Operator "(+)" and Join in FromClause can't be used together
|
|
select * from t11 right join t12 on t11.c1 = t12.c2 where t11.c1 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
ERROR: Operator "(+)" and Join in FromClause can't be used together
|
|
select * from t11 full join t12 on t11.c1 = t12.c2 where t11.c1 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
ERROR: Operator "(+)" and Join in FromClause can't be used together
|
|
select * from t11 NATURAL join t12 where t11.c1 = t12.c2(+) order by t11.c1;
|
|
ERROR: Operator "(+)" and Join in FromClause can't be used together
|
|
--where condition treated as join filter (t11.c1(+) = 1)--
|
|
select * from t11 , t12 where t11.c2 (+)= t12.c2 and t11.c1(+) = 1 order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
| | | 2 | 2 | 2
|
|
| | | 3 | 3 | 3
|
|
| | | 4 | 4 | 4
|
|
| | | 5 | 5 | 5
|
|
| | | 6 | 6 | 6
|
|
| | | 7 | 7 | 7
|
|
| | | 8 | 8 | 8
|
|
| | | 9 | 9 | 9
|
|
| | | 10 | 10 | 10
|
|
| | | 16 | 16 | 16
|
|
| | | 17 | 17 | 17
|
|
| | | 18 | 18 | 18
|
|
| | | 19 | 19 | 19
|
|
| | | 20 | 20 | 20
|
|
(15 rows)
|
|
|
|
--(+) can used in fromClause--
|
|
select * from t11 inner join t12 on t11.c1 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
ERROR: Operator "(+)" can only be used in WhereClause of Select-Statement or Subquery.
|
|
LINE 1: select * from t11 inner join t12 on t11.c1 = t12.c2(+) order...
|
|
^
|
|
select * from t11 left join t12 on t11.c1 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
ERROR: Operator "(+)" can only be used in WhereClause of Select-Statement or Subquery.
|
|
LINE 1: select * from t11 left join t12 on t11.c1 = t12.c2(+) order ...
|
|
^
|
|
select * from t11 right join t12 on t11.c1 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
ERROR: Operator "(+)" can only be used in WhereClause of Select-Statement or Subquery.
|
|
LINE 1: select * from t11 right join t12 on t11.c1 = t12.c2(+) order...
|
|
^
|
|
----with out Identifier------
|
|
create table t1(a1 int, b1 int, c1 int) ;
|
|
create table t2(a2 int, b2 int, c2 int) ;
|
|
insert into t1 select v,v,v from generate_series(1,10) as v;
|
|
insert into t2 select * from t1;
|
|
insert into t1 select v,v,v from generate_series(11,15) as v;
|
|
insert into t2 select v,v,v from generate_series(16,20) as v;
|
|
--without specified relation name before column name.
|
|
select * from t1,t2 where t1.a1(+) = a2 order by 4;
|
|
a1 | b1 | c1 | a2 | b2 | c2
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
| | | 16 | 16 | 16
|
|
| | | 17 | 17 | 17
|
|
| | | 18 | 18 | 18
|
|
| | | 19 | 19 | 19
|
|
| | | 20 | 20 | 20
|
|
(15 rows)
|
|
|
|
select * from t1,t2 where t1.a1 = a2(+) order by 2;
|
|
a1 | b1 | c1 | a2 | b2 | c2
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
11 | 11 | 11 | | |
|
|
12 | 12 | 12 | | |
|
|
13 | 13 | 13 | | |
|
|
14 | 14 | 14 | | |
|
|
15 | 15 | 15 | | |
|
|
(15 rows)
|
|
|
|
select * from t1,t2 where a1 = a2(+) order by 3;
|
|
a1 | b1 | c1 | a2 | b2 | c2
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
11 | 11 | 11 | | |
|
|
12 | 12 | 12 | | |
|
|
13 | 13 | 13 | | |
|
|
14 | 14 | 14 | | |
|
|
15 | 15 | 15 | | |
|
|
(15 rows)
|
|
|
|
select * from t1,t2 where a1(+) = a2 order by 4;
|
|
a1 | b1 | c1 | a2 | b2 | c2
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10 | 10 | 10
|
|
| | | 16 | 16 | 16
|
|
| | | 17 | 17 | 17
|
|
| | | 18 | 18 | 18
|
|
| | | 19 | 19 | 19
|
|
| | | 20 | 20 | 20
|
|
(15 rows)
|
|
|
|
select * from (select a1 from t1) as dt1, t2 where dt1.a1 = a2(+) order by 1;
|
|
a1 | a2 | b2 | c2
|
|
----+----+----+----
|
|
1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10
|
|
11 | | |
|
|
12 | | |
|
|
13 | | |
|
|
14 | | |
|
|
15 | | |
|
|
(15 rows)
|
|
|
|
--without specified relation name before column name.
|
|
select * from (select a1 from t1) as dt1, t2 where a1(+) = a2 order by 1, 2;
|
|
a1 | a2 | b2 | c2
|
|
----+----+----+----
|
|
1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10
|
|
| 16 | 16 | 16
|
|
| 17 | 17 | 17
|
|
| 18 | 18 | 18
|
|
| 19 | 19 | 19
|
|
| 20 | 20 | 20
|
|
(15 rows)
|
|
|
|
select t1.a1, t1.b1, tf.x1 from t1, unnest(array[-2, -1, 0, 1,2,3]) as tf(x1) where a1(+) = x1 order by 1,3;
|
|
a1 | b1 | x1
|
|
----+----+----
|
|
1 | 1 | 1
|
|
2 | 2 | 2
|
|
3 | 3 | 3
|
|
| | -2
|
|
| | -1
|
|
| | 0
|
|
(6 rows)
|
|
|
|
select t1.a1, t1.b1, tf.x1 from t1, unnest(array[-2, -1, 0, 1,2,3]) as tf(x1) where a1 = x1(+) order by 1,3;
|
|
a1 | b1 | x1
|
|
----+----+----
|
|
1 | 1 | 1
|
|
2 | 2 | 2
|
|
3 | 3 | 3
|
|
4 | 4 |
|
|
5 | 5 |
|
|
6 | 6 |
|
|
7 | 7 |
|
|
8 | 8 |
|
|
9 | 9 |
|
|
10 | 10 |
|
|
11 | 11 |
|
|
12 | 12 |
|
|
13 | 13 |
|
|
14 | 14 |
|
|
15 | 15 |
|
|
(15 rows)
|
|
|
|
--report error when x2 is invalid.
|
|
select t1.a1, t1.b1, tf.x1 from t1, unnest(array[-2, -1, 0, 1,2,3]) as tf(x1) where a1 = x2(+) order by 1,3;
|
|
ERROR: column reference "x2" is ambiguous.
|
|
LINE 1: ...est(array[-2, -1, 0, 1,2,3]) as tf(x1) where a1 = x2(+) orde...
|
|
^
|
|
HINT: "x2" with "(+)" can only reference relation in current query level.
|
|
create table t15 (like t11);
|
|
--unsupport (+) in update/delete/merge-into whereclause--
|
|
update t15 set c2 = 1 from t11 where t11.c1 = t15.c2(+);
|
|
ERROR: Operator "(+)" can only be used in WhereClause of Select-Statement or Subquery.
|
|
LINE 1: update t15 set c2 = 1 from t11 where t11.c1 = t15.c2(+);
|
|
^
|
|
delete from t15 using t11 where t11.c1 = t15.c2(+);
|
|
ERROR: Operator "(+)" can only be used in WhereClause of Select-Statement or Subquery.
|
|
LINE 1: delete from t15 using t11 where t11.c1 = t15.c2(+);
|
|
^
|
|
merge into t15
|
|
using t11
|
|
on (t11.c1 = t15.c2)
|
|
when matched then
|
|
update set t15.c1 = t11.c2 where t11.c3 = t15.c3(+)
|
|
when not matched then
|
|
insert values(2,3,5);
|
|
ERROR: Operator "(+)" can only be used in WhereClause of Select-Statement or Subquery.
|
|
LINE 5: update set t15.c1 = t11.c2 where t11.c3 = t15.c3(+)
|
|
^
|
|
--support subquery in update/delete/merge-into whereclause--
|
|
explain(costs off)
|
|
update t15 set c2 = 1 where t15.c1 = (select t11.c2 from t11, t15 where t15.c2(+) = t11.c3);
|
|
QUERY PLAN
|
|
-------------------------------------------------------
|
|
Update on t15
|
|
InitPlan 1 (returns $0)
|
|
-> Hash Left Join
|
|
Hash Cond: (t11.c3 = plus_outerjoin.t15.c2)
|
|
-> Seq Scan on t11
|
|
-> Hash
|
|
-> Seq Scan on t15
|
|
-> Seq Scan on t15
|
|
Filter: (c1 = $0)
|
|
(9 rows)
|
|
|
|
explain(costs off)
|
|
delete from t15 using t11 where exists (select t11.c2 from t11, t15 where t15.c2(+) = t11.c3);
|
|
QUERY PLAN
|
|
------------------------------------------------------------------------
|
|
Delete on t15
|
|
InitPlan 1 (returns $0)
|
|
-> Nested Loop Left Join
|
|
Join Filter: (plus_outerjoin.t15.c2 = plus_outerjoin.t11.c3)
|
|
-> Seq Scan on t11
|
|
-> Materialize
|
|
-> Seq Scan on t15
|
|
-> Result
|
|
One-Time Filter: $0
|
|
-> Nested Loop
|
|
-> Seq Scan on t15
|
|
-> Materialize
|
|
-> Seq Scan on t11
|
|
(13 rows)
|
|
|
|
-----used in procedure------
|
|
CREATE OR REPLACE function plus_join_test_1 (var1 in int) return int
|
|
AS
|
|
var2 int;
|
|
BEGIN
|
|
var2 :=var1+1 ;
|
|
select t11.c1 into var2 from t11, t12 where t11.c1 = t12.c2(+) and t11.c2(+) = var2 order by 1;
|
|
return var2;
|
|
END;
|
|
/
|
|
select plus_join_test_1(1);
|
|
plus_join_test_1
|
|
------------------
|
|
2
|
|
(1 row)
|
|
|
|
----used with "IN" condition------
|
|
select * from t11, t12 where t12.c1(+) in (1,2,3) and t11.c2 = t12.c2(+) order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3 | 3 | 3
|
|
4 | 4 | 4 | | |
|
|
5 | 5 | 5 | | |
|
|
6 | 6 | 6 | | |
|
|
7 | 7 | 7 | | |
|
|
8 | 8 | 8 | | |
|
|
9 | 9 | 9 | | |
|
|
10 | 10 | 10 | | |
|
|
11 | 11 | 11 | | |
|
|
12 | 12 | 12 | | |
|
|
13 | 13 | 13 | | |
|
|
14 | 14 | 14 | | |
|
|
15 | 15 | 15 | | |
|
|
(15 rows)
|
|
|
|
----used with join clause, only const in whereclause-------
|
|
select * from t11 left join t12 on t11.c1 = t12.c2 where t11.c2(+) >2 order by 1,2,3,4,5,6;
|
|
ERROR: Operator "(+)" and Join in FromClause can't be used together
|
|
--------unsupport used in outer join with SubQuery----------
|
|
select * from t11, t12 where t12.c2(+) + t11.c1(+) = (select min(1) from t11) + t11.c1;
|
|
ERROR: Operator "(+)" can not be used in outer join with SubQuery.
|
|
LINE 1: ...lect * from t11, t12 where t12.c2(+) + t11.c1(+) = (select ...
|
|
^
|
|
select * from t11, t12 where t11.c2(+) = (select min(1) from t11);
|
|
ERROR: Operator "(+)" can not be used in outer join with SubQuery.
|
|
LINE 1: select * from t11, t12 where t11.c2(+) = (select min(1) fro...
|
|
^
|
|
select * from t11, t12 where t11.c2(+) + 1 = (select min(1) from t11)+1;
|
|
ERROR: Operator "(+)" can not be used in outer join with SubQuery.
|
|
LINE 1: select * from t11, t12 where t11.c2(+) + 1 = (select min(1)...
|
|
^
|
|
select * from t11, t12 where t11.c2(+) + 1 = (select min(1) from t11);
|
|
ERROR: Operator "(+)" can not be used in outer join with SubQuery.
|
|
LINE 1: select * from t11, t12 where t11.c2(+) + 1 = (select min(1)...
|
|
^
|
|
select * from t11, t12 where t12.c2 = (select min(1) from t11) + t11.c1(+) order by 1,2,3,4,5,6;
|
|
c1 | c2 | c3 | c1 | c2 | c3
|
|
----+----+----+----+----+----
|
|
1 | 1 | 1 | 2 | 2 | 2
|
|
2 | 2 | 2 | 3 | 3 | 3
|
|
3 | 3 | 3 | 4 | 4 | 4
|
|
4 | 4 | 4 | 5 | 5 | 5
|
|
5 | 5 | 5 | 6 | 6 | 6
|
|
6 | 6 | 6 | 7 | 7 | 7
|
|
7 | 7 | 7 | 8 | 8 | 8
|
|
8 | 8 | 8 | 9 | 9 | 9
|
|
9 | 9 | 9 | 10 | 10 | 10
|
|
15 | 15 | 15 | 16 | 16 | 16
|
|
| | | 1 | 1 | 1
|
|
| | | 17 | 17 | 17
|
|
| | | 18 | 18 | 18
|
|
| | | 19 | 19 | 19
|
|
| | | 20 | 20 | 20
|
|
(15 rows)
|
|
|
|
------JoinExpr and (+) not in same query level is valid---
|
|
--select * from (select t11.c1 a from t11,t12 where t11.c1 = t12.c2(+)) inner join t13 on (a = c3) order by 1,2,3,4;
|
|
select * from (select t11.c1 a from t11 left join t12 on t11.c1 = t12.c2) inner join t13 on (a = c3) order by 1,2,3,4;
|
|
a | c1 | c2 | c3
|
|
----+----+----+----
|
|
1 | 1 | 1 | 1
|
|
2 | 2 | 2 | 2
|
|
3 | 3 | 3 | 3
|
|
4 | 4 | 4 | 4
|
|
5 | 5 | 5 | 5
|
|
6 | 6 | 6 | 6
|
|
7 | 7 | 7 | 7
|
|
8 | 8 | 8 | 8
|
|
9 | 9 | 9 | 9
|
|
10 | 10 | 10 | 10
|
|
(10 rows)
|
|
|
|
explain (verbose on, costs off, analyze on, timing off, cpu off)
|
|
select * from (select t11.c1 a from t11 left join t12 on t11.c1 = t12.c2) inner join t13 on (a = c3) order by 1,2,3,4;
|
|
QUERY PLAN
|
|
---------------------------------------------------------------------------------
|
|
Sort (actual rows=10 loops=1)
|
|
Output: t11.c1, t13.c1, t13.c2, t13.c3
|
|
Sort Key: t11.c1, t13.c1, t13.c2
|
|
Sort Method: quicksort Memory: 26kB
|
|
-> Hash Join (actual rows=10 loops=1)
|
|
Output: t11.c1, t13.c1, t13.c2, t13.c3
|
|
Hash Cond: (t11.c1 = t13.c3)
|
|
-> Hash Left Join (actual rows=15 loops=1)
|
|
Output: t11.c1
|
|
Hash Cond: (t11.c1 = t12.c2)
|
|
-> Seq Scan on plus_outerjoin.t11 (actual rows=15 loops=1)
|
|
Output: t11.c1, t11.c2, t11.c3
|
|
-> Hash (actual rows=15 loops=1)
|
|
Output: t12.c2
|
|
--?.*
|
|
-> Seq Scan on plus_outerjoin.t12 (actual rows=15 loops=1)
|
|
Output: t12.c2
|
|
-> Hash (actual rows=15 loops=1)
|
|
Output: t13.c1, t13.c2, t13.c3
|
|
--?.*
|
|
-> Seq Scan on plus_outerjoin.t13 (actual rows=15 loops=1)
|
|
Output: t13.c1, t13.c2, t13.c3
|
|
--? Total runtime: .* ms
|
|
(23 rows)
|
|
|
|
explain(verbose on, costs off)
|
|
select * from (select t11.c1 a from t11,t12 where t11.c1 = t12.c2(+)) inner join t13 on (a = c3) order by 1,2,3,4;
|
|
QUERY PLAN
|
|
--------------------------------------------------------
|
|
Sort
|
|
Output: t11.c1, t13.c1, t13.c2, t13.c3
|
|
Sort Key: t11.c1, t13.c1, t13.c2
|
|
-> Hash Join
|
|
Output: t11.c1, t13.c1, t13.c2, t13.c3
|
|
Hash Cond: (t11.c1 = t13.c3)
|
|
-> Hash Left Join
|
|
Output: t11.c1
|
|
Hash Cond: (t11.c1 = t12.c2)
|
|
-> Seq Scan on plus_outerjoin.t11
|
|
Output: t11.c1, t11.c2, t11.c3
|
|
-> Hash
|
|
Output: t12.c2
|
|
-> Seq Scan on plus_outerjoin.t12
|
|
Output: t12.c2
|
|
-> Hash
|
|
Output: t13.c1, t13.c2, t13.c3
|
|
-> Seq Scan on plus_outerjoin.t13
|
|
Output: t13.c1, t13.c2, t13.c3
|
|
(19 rows)
|
|
|
|
----used in create view ---------
|
|
create view plus_v as select t11.c1, t12.c2 from t11, t12 where t11.c1 = t12.c2(+) and t12.c2(+) in (1,2,3);
|
|
\d+ plus_v
|
|
View "plus_outerjoin.plus_v"
|
|
Column | Type | Modifiers | Storage | Description
|
|
--------+---------+-----------+---------+-------------
|
|
c1 | integer | | plain |
|
|
c2 | integer | | plain |
|
|
View definition:
|
|
SELECT t11.c1, t12.c2
|
|
FROM t12
|
|
RIGHT JOIN t11 ON t11.c1 = t12.c2 AND (t12.c2 = ANY (ARRAY[1, 2, 3]))
|
|
WHERE 1 = 1 AND 1 = 1;
|
|
|
|
-------used with is (not) null --------
|
|
select t11.c1, t12.c2 from t11, t12 where t11.c2 = t12.c3(+) and t12.c2 is not null order by 1,2;
|
|
c1 | c2
|
|
----+----
|
|
1 | 1
|
|
2 | 2
|
|
3 | 3
|
|
4 | 4
|
|
5 | 5
|
|
6 | 6
|
|
7 | 7
|
|
8 | 8
|
|
9 | 9
|
|
10 | 10
|
|
(10 rows)
|
|
|
|
select t11.c1, t12.c2 from t11, t12 where t11.c2 = t12.c3(+) and t11.c3 is not null order by 1,2;
|
|
c1 | c2
|
|
----+----
|
|
1 | 1
|
|
2 | 2
|
|
3 | 3
|
|
4 | 4
|
|
5 | 5
|
|
6 | 6
|
|
7 | 7
|
|
8 | 8
|
|
9 | 9
|
|
10 | 10
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
(15 rows)
|
|
|
|
select t11.c1, t12.c2, t13.c2 from t11, t12, t13 where t11.c2 = t12.c3(+) and t11.c3 = t13.c1(+) and (t13.c2 > t12.c1)::bool;
|
|
c1 | c2 | c2
|
|
----+----+----
|
|
(0 rows)
|
|
|
|
-------used (+) with un common expression, like is null, is not null--------
|
|
select t11.c1, t12.c2, t13.c2 from t11, t12, t13 where t11.c2 = t12.c3(+) and t11.c3 = t13.c1(+) and t13.c2(+) is not null;
|
|
c1 | c2 | c2
|
|
----+----+----
|
|
1 | 1 | 1
|
|
2 | 2 | 2
|
|
3 | 3 | 3
|
|
4 | 4 | 4
|
|
5 | 5 | 5
|
|
6 | 6 | 6
|
|
7 | 7 | 7
|
|
8 | 8 | 8
|
|
9 | 9 | 9
|
|
10 | 10 | 10
|
|
(10 rows)
|
|
|
|
select t11.c1, t12.c2, t13.c2 from t11, t12, t13 where t11.c2 = t12.c3(+) and t11.c3 = t13.c1(+) and (t13.c2(+) > t12.c1)::bool;
|
|
ERROR: Operator "(+)" can only be used in common expression.
|
|
select * from t_a a,t_b b where b.id=a.id(+) and a.code(+) + 1 * 2 + a.code(+) IS NOT NULL ;
|
|
id | name | code | id | name | code
|
|
----+------+------+----+------+------
|
|
1 | tom | 3 | 1 | bat | 6
|
|
(1 row)
|
|
|
|
drop view plus_v;
|
|
drop function plus_join_test_1();
|
|
drop table t1;
|
|
drop table t2;
|
|
drop table t11;
|
|
drop table t12;
|
|
drop table t13;
|
|
drop table t14;
|
|
drop table t15;
|
|
drop table t_a;
|
|
drop table t_b;
|
|
drop schema plus_outerjoin;
|
|
\c regression;
|