ORs in JOINs tests

This commit is contained in:
Ilya Golshtein 2021-06-25 15:04:23 +03:00
parent 3766d47f31
commit ba8d91245c
17 changed files with 496 additions and 6 deletions

View File

@ -10,7 +10,7 @@ insert into test1_00863 (id, code) select number, toString(number) FROM numbers(
insert into test3_00863 (id, code) select number, toString(number) FROM numbers(100000);
insert into test2_00863 (id, code, test1_id, test3_id) select number, toString(number), number, number FROM numbers(100000);
SET max_memory_usage = 50000000;
SET max_memory_usage = 51000000;
select test2_00863.id
from test1_00863, test2_00863, test3_00863

View File

@ -17,6 +17,33 @@ n rj t id id
n fj t 1 1
n fj t id id
n fj t \N \N
on with or
n rj n 1 1
n rj n id id
n rj n \N \N
n a rj n 1 1
n a rj n id id
n a rj n \N \N
n fj n 1 1
n fj n id id
n fj n \N \N
n fj n \N \N
t rj n 1 1
t rj n id id
t rj n \N \N
t fj n 1 1
t fj n id id
t fj n \N \N
n rj t 1 1
n rj t id id
n a rj t 1 1
n a rj t id id
n fj t 1 1
n fj t id id
n fj t \N \N
n fj t 1 1
n fj t id id
n fj t \N \N
using
n rj n 1 1
n rj n id id

View File

@ -1,11 +1,15 @@
DROP TABLE IF EXISTS t;
DROP TABLE IF EXISTS nt;
DROP TABLE IF EXISTS ntxy;
CREATE TABLE t (x String) ENGINE = Memory;
CREATE TABLE nt (x Nullable(String)) ENGINE = Memory;
CREATE TABLE t (x String) ENGINE = Log();
CREATE TABLE nt (x Nullable(String)) ENGINE = Log();
CREATE TABLE ntxy (x Nullable(String), y Nullable(String)) ENGINE = Log();
INSERT INTO t (x) VALUES ('id'), ('1');
INSERT INTO nt (x) VALUES ('id'), (NULL), ('1');
INSERT INTO ntxy (x, y) VALUES ('id', 'id'), (NULL, NULL), ('1', '1');
SET join_use_nulls = 1;
@ -20,6 +24,19 @@ SELECT 't fj n', t1.x, t2.x FROM t AS t1 FULL JOIN nt AS t2 ON t1.x = t2.x ORDER
SELECT 'n rj t', t1.x, t2.x FROM nt AS t1 RIGHT JOIN t AS t2 ON t1.x = t2.x ORDER BY t1.x;
SELECT 'n fj t', t1.x, t2.x FROM nt AS t1 FULL JOIN t AS t2 ON t1.x = t2.x ORDER BY t1.x;
SELECT 'on with or';
SELECT 'n rj n', t1.x, t2.x FROM nt AS t1 RIGHT JOIN ntxy AS t2 ON t1.x = t2.x OR t1.x = t2.y ORDER BY t1.x;
SELECT 'n a rj n', t1.x, t2.x FROM nt AS t1 ANY RIGHT JOIN ntxy AS t2 ON t1.x = t2.x OR t1.x = t2.y ORDER BY t1.x;
SELECT 'n fj n', t1.x, t2.x FROM nt AS t1 FULL JOIN ntxy AS t2 ON t1.x = t2.x OR t1.x = t2.y ORDER BY t1.x;
SELECT 't rj n', t1.x, t2.x FROM t AS t1 RIGHT JOIN ntxy AS t2 ON t1.x = t2.x OR t1.x = t2.y ORDER BY t1.x;
SELECT 't fj n', t1.x, t2.x FROM t AS t1 FULL JOIN ntxy AS t2 ON t1.x = t2.x OR t1.x = t2.y ORDER BY t1.x;
SELECT 'n rj t', t1.x, t2.x FROM ntxy AS t1 RIGHT JOIN t AS t2 ON t1.x = t2.x OR t1.y = t2.x ORDER BY t1.x;
SELECT 'n a rj t', t1.x, t2.x FROM ntxy AS t1 ANY RIGHT JOIN t AS t2 ON t1.x = t2.x OR t1.y = t2.x ORDER BY t1.x;
SELECT 'n fj t', t1.x, t2.x FROM ntxy AS t1 FULL JOIN t AS t2 ON t1.x = t2.x OR t2.x = t1.y ORDER BY t1.x;
SELECT 'n fj t', t1.x, t2.x FROM ntxy AS t1 FULL JOIN t AS t2 ON t2.x = t1.y OR t1.x = t2.x ORDER BY t1.x;
SELECT 'using';
SELECT 'n rj n', t1.x, t2.x FROM nt AS t1 RIGHT JOIN nt AS t2 USING(x) ORDER BY t1.x;
@ -46,3 +63,4 @@ SELECT sum(isNull(t1.x)), count(t1.x) FROM nt AS t1 FULL JOIN nt AS t2 USING(x);
DROP TABLE t;
DROP TABLE nt;
DROP TABLE ntxy;

View File

@ -4,7 +4,6 @@ SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON (A.a = arrayJoin([1])); -- {
SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON equals(a); -- { serverError 62 }
SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON less(a); -- { serverError 62 }
SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON a = b OR a = b; -- { serverError 403 }
SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON a = b AND a > b; -- { serverError 403 }
SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON a = b AND a < b; -- { serverError 403 }
SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON a = b AND a >= b; -- { serverError 403 }

View File

@ -0,0 +1,68 @@
join on OR chain (all left)
2 3
6 4
6 4
==
2 3
5 4
100 4
==
2 3 2 3
6 4 5 4
6 4 100 4
==
1
==
1 3
==
1 2 2 3
==
2 4
join on OR chain (all right)
2 3
6 4
6 4
==
2 3
5 4
100 4
==
2 3 2 3
6 4 5 4
6 4 100 4
==
1
0
==
1 3
0 4
==
1 2 2 3
0 0 6 4
==
2 4
6 5
join on OR chain (full)
2 3
6 4
6 4
==
2 3
5 4
100 4
==
2 3 2 3
6 4 5 4
6 4 100 4
==
1
0
==
1 3
0 4
==
1 2 2 3
0 0 6 4
==
2 4
6 5

View File

@ -0,0 +1,68 @@
SET joined_subquery_requires_alias = 0;
SET max_threads = 1;
drop table if exists tab1;
drop table if exists tab2;
drop table if exists tab3;
create table tab1 (a1 Int32, b1 Int32) engine = MergeTree order by a1;
create table tab2 (a2 Int32, b2 Int32) engine = MergeTree order by a2;
create table tab3 (a3 Int32, b3 Int32) engine = MergeTree order by a3;
insert into tab1 values (1, 2);
insert into tab2 values (2, 3);
insert into tab2 values (6, 4);
insert into tab3 values (2, 3);
insert into tab3 values (5, 4);
insert into tab3 values (100, 4);
select 'join on OR chain (all left)';
select a2, b2 from tab2 all left join tab3 on a2 = a3 or b2 = b3;
select '==';
select a3, b3 from tab2 all left join tab3 on a2 = a3 or b2 = b3;
select '==';
select a2, b2, a3, b3 from tab2 all left join tab3 on a2 = a3 or b2 = b3;
select '==';
select a1 from tab1 all left join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a1, b2 from tab1 all left join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a1, b1, a2, b2 from tab1 all left join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a2, b2 + 1 from tab1 all left join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select 'join on OR chain (all right)';
select a2, b2 from tab2 all right join tab3 on a2 = a3 or b2 = b3;
select '==';
select a3, b3 from tab2 all right join tab3 on a2 = a3 or b2 = b3;
select '==';
select a2, b2, a3, b3 from tab2 all right join tab3 on a2 = a3 or b2 = b3;
select '==';
select a1 from tab1 all right join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a1, b2 from tab1 all right join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a1, b1, a2, b2 from tab1 all right join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a2, b2 + 1 from tab1 all right join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select 'join on OR chain (full)';
select a2, b2 from tab2 full join tab3 on a2 = a3 or b2 = b3;
select '==';
select a3, b3 from tab2 full join tab3 on a2 = a3 or b2 = b3;
select '==';
select a2, b2, a3, b3 from tab2 full join tab3 on a2 = a3 or b2 = b3;
select '==';
select a1 from tab1 full join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a1, b2 from tab1 full join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a1, b1, a2, b2 from tab1 full join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a2, b2 + 1 from tab1 full join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
drop table tab1;
drop table tab2;
drop table tab3;

View File

@ -0,0 +1,41 @@
join on OR chain (any left)
2 3
6 4
==
2 3
5 4
==
2 3 2 3
6 4 5 4
==
1
==
1 3
==
1 2 2 3
==
2 4
join on OR chain (any right)
2 3
6 4
6 4
==
2 3
5 4
100 4
==
2 3 2 3
6 4 5 4
6 4 100 4
==
1
0
==
1 3
0 4
==
1 2 2 3
0 0 6 4
==
2 4
6 5

View File

@ -0,0 +1,52 @@
SET joined_subquery_requires_alias = 0;
SET max_threads = 1;
drop table if exists tab1;
drop table if exists tab2;
drop table if exists tab3;
create table tab1 (a1 Int32, b1 Int32) engine = MergeTree order by a1;
create table tab2 (a2 Int32, b2 Int32) engine = MergeTree order by a2;
create table tab3 (a3 Int32, b3 Int32) engine = MergeTree order by a3;
insert into tab1 values (1, 2);
insert into tab2 values (2, 3);
insert into tab2 values (6, 4);
insert into tab3 values (2, 3);
insert into tab3 values (5, 4);
insert into tab3 values (100, 4);
select 'join on OR chain (any left)';
select a2, b2 from tab2 any left join tab3 on a2 = a3 or b2 = b3;
select '==';
select a3, b3 from tab2 any left join tab3 on a2 = a3 or b2 = b3;
select '==';
select a2, b2, a3, b3 from tab2 any left join tab3 on a2 = a3 or b2 = b3;
select '==';
select a1 from tab1 any left join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a1, b2 from tab1 any left join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a1, b1, a2, b2 from tab1 any left join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a2, b2 + 1 from tab1 any left join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select 'join on OR chain (any right)';
select a2, b2 from tab2 any right join tab3 on a2 = a3 or b2 = b3;
select '==';
select a3, b3 from tab2 any right join tab3 on a2 = a3 or b2 = b3;
select '==';
select a2, b2, a3, b3 from tab2 any right join tab3 on a2 = a3 or b2 = b3;
select '==';
select a1 from tab1 any right join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a1, b2 from tab1 any right join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a1, b1, a2, b2 from tab1 any right join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
select '==';
select a2, b2 + 1 from tab1 any right join tab2 on b1 + 1 = a2 + 1 or a1 + 4 = b2 + 2;
drop table tab1;
drop table tab2;
drop table tab3;

View File

@ -0,0 +1,10 @@
subqueries with OR
1
==
1
==
1 2
subquery column alias with OR
1 2 2 3
==
1 2 2 3

View File

@ -0,0 +1,27 @@
SET joined_subquery_requires_alias = 0;
SET max_threads = 1;
drop table if exists tab1;
drop table if exists tab2;
create table tab1 (a1 Int32, b1 Int32) engine = MergeTree order by a1;
create table tab2 (a2 Int32, b2 Int32) engine = MergeTree order by a2;
insert into tab1 values (1, 2);
insert into tab2 values (2, 3);
insert into tab2 values (6, 4);
select 'subqueries with OR';
select a1 from tab1 any left join (select * from tab2) on b1 = a2 or b2 = a1;
select '==';
select a1 from tab1 any left join (select a2, b2 from tab2) on b1 = a2 or b2 = a1;
select '==';
select a1, b1 from tab1 any left join (select * from tab2) on b1 = a2 or b2 = a1;
select 'subquery column alias with OR';
select a1, b1, a2, b2 from tab1 any left join (select *, a2 as z from tab2) on b1 + 1 = z + 1 or b1 = z * 2;
select '==';
select a1, b1, a2, b2 from tab1 any left join (select *, a2 + 1 as z from tab2) on b1 + 1 = z or b1 = z * 2;
drop table tab1;
drop table tab2;

View File

@ -0,0 +1,8 @@
a b c d e f a b c d e f
a b c d e f a b c d e f
a b c d e f a b c d e f
a b c d e f a b c d e f
a b c d e f a b c d e f
join on OR/AND chain
2 3 2 3
6 4 0 0

View File

@ -0,0 +1,28 @@
select * from (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t1 inner join (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t2 on t1.b = t2.b and t1.c = t2.b and t1.d = t2.b or t1.e = t2.e;
select * from (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t1 inner join (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t2 on t1.d = t2.b or t1.c = t2.b or t1.d = t2.b and t1.d = t2.b or t1.e = t2.e and t1.a=t2.a and t2.f=t1.f;
select * from (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t1 left join (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t2 on (t1.d = t2.b or t1.c = t2.b or t1.d = t2.b and t1.d = t2.b) or (t1.e = t2.e and t1.a=t2.a and t2.f=t1.f);
select * from (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t1 right join (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t2 on (t1.d = t2.b or t1.c = t2.b) or t1.e = t2.e;
select * from (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t1 inner join (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t2 on (t1.d = t2.b or t1.c = t2.b) or (t1.e = t2.e and t1.a=t2.a and t2.f=t1.f);
select * from (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t1 inner join (select 'a' as a, 'b' as b, 'c' as c, 'd' as d, 'e' as e, 'f' as f) as t2 on (t1.d = t2.b or t1.c = t2.b) and (t1.e = t2.e or t1.f=t2.f);
SET joined_subquery_requires_alias = 0;
SET max_threads = 1;
drop table if exists tab2;
drop table if exists tab3;
create table tab2 (a2 Int32, b2 Int32) engine = MergeTree order by a2;
create table tab3 (a3 Int32, b3 Int32) engine = MergeTree order by a3;
insert into tab2 values (2, 3);
insert into tab2 values (6, 4);
insert into tab3 values (2, 3);
insert into tab3 values (5, 4);
insert into tab3 values (100, 4);
select 'join on OR/AND chain';
select a2, b2, a3, b3 from tab2 any left join tab3 on (a2=a3 or b2=b3) and a2 +1 = b3 + 0;
drop table tab2;
drop table tab3;

View File

@ -0,0 +1,3 @@
a b 42 a b 42
===
a b 42 a b 42

View File

@ -0,0 +1,3 @@
select * from (select 'a' as a, 'b' as b, 42 as forty_two) as t1 inner join (select 'a' as a, 'b' as b, 42 as forty_two) as t2 on t1.b = t2.a or t1.forty_two = t2.forty_two;
select '===';
select * from (select 'a' as a, 'b' as b, 42 as forty_two) as t1 inner join (select 'a' as a, 'b' as b, 42 as forty_two) as t2 on t1.b = t2.b or t1.forty_two = t2.forty_two;

View File

@ -0,0 +1,115 @@
1 left 1 2 1 2
5 left 1 2 1 2
5 left 1 2 1 2
5 left 1 2 1 2
5 left 1 2 1 2
5 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
15 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
16 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 left 1 2 1 2
17 any left 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 any right 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
17 full 1 2 1 2
555
a 0 a 0
a 0 a 1
a 1 a 1
a 1 a 0
a 0 a 0
a 0 a 1
a 1 a 0
a 1 a 1

View File

@ -0,0 +1,23 @@
select '1 left', * from (select 1 as x, 2 as y) t1 left join (select 1 as xx, 2 as yy from numbers(1)) t2 on x = xx or y = yy;
select '5 left', * from (select 1 as x, 2 as y) t1 left join (select 1 as xx, 2 as yy from numbers(5)) t2 on x = xx or y = yy;
select '15 left', * from (select 1 as x, 2 as y) t1 left join (select 1 as xx, 2 as yy from numbers(15)) t2 on x = xx or y = yy;
select '16 left', * from (select 1 as x, 2 as y) t1 left join (select 1 as xx, 2 as yy from numbers(16)) t2 on x = xx or y = yy;
select '17 left', * from (select 1 as x, 2 as y) t1 left join (select 1 as xx, 2 as yy from numbers(17)) t2 on x = xx or y = yy;
select '17 any left', * from (select 1 as x, 2 as y) t1 any left join (select 1 as xx, 2 as yy from numbers(17)) t2 on x = xx or y = yy;
select '17 right', * from (select 1 as x, 2 as y) t1 right join (select 1 as xx, 2 as yy from numbers(17)) t2 on x = xx or y = yy;
select '17 any right', * from (select 1 as x, 2 as y) t1 any right join (select 1 as xx, 2 as yy from numbers(17)) t2 on x = xx or y = yy;
select '17 full', * from (select 1 as x, 2 as y) t1 full join (select 1 as xx, 2 as yy from numbers(17)) t2 on x = xx or y = yy;
select count(1) from (select * from (select 1 as x, 2 as y) t1 left join (select 1 as xx, 2 as yy from numbers(555)) t2 on x = xx or y = yy);
select * from (select 'a' as a, number as c from numbers(2)) as t1 join (select 'a' as a, number as c from numbers(2)) as t2 on t1.c=t2.c or t1.a = t2.a;
select * from (select 'a' as a, number as c from numbers(2)) as t1 join (select 'a' as a, number as c from numbers(2)) as t2 on t1.a = t2.a or t1.c=t2.c;