2021-04-26 20:07:00 +02:00
|
|
|
#coding:utf-8
|
|
|
|
|
2022-01-23 20:41:55 +01:00
|
|
|
"""
|
|
|
|
ID: issue-4846
|
|
|
|
ISSUE: 4846
|
|
|
|
TITLE: Allow hash/merge joins for non-field (dbkey or derived expression) equalities
|
|
|
|
DESCRIPTION:
|
|
|
|
NB: currently join oth three and more sources made by USING or NATURAL clauses will use NL.
|
|
|
|
See CORE-4809
|
|
|
|
JIRA: CORE-4528
|
2022-02-02 15:46:19 +01:00
|
|
|
FBTEST: bugs.core_4528
|
2022-04-07 20:23:35 +02:00
|
|
|
NOTES:
|
|
|
|
[07.04.2022] pzotov
|
|
|
|
FB 5.0.0.455 and later: data sources with equal cardinality now present in the HASH plan in order they are specified in the query.
|
|
|
|
Reversed order was used before this build. Because of this, two cases of expected stdout must be taken in account, see variables
|
|
|
|
'fb3x_checked_stdout' and 'fb5x_checked_stdout'.
|
2022-01-23 20:41:55 +01:00
|
|
|
"""
|
2021-04-26 20:07:00 +02:00
|
|
|
|
2022-01-23 20:41:55 +01:00
|
|
|
import pytest
|
|
|
|
from firebird.qa import *
|
2021-04-26 20:07:00 +02:00
|
|
|
|
2022-01-23 20:41:55 +01:00
|
|
|
db = db_factory()
|
2021-04-26 20:07:00 +02:00
|
|
|
|
2022-01-23 20:41:55 +01:00
|
|
|
test_script = """
|
|
|
|
recreate table tn(x int primary key using index tn_x);
|
2021-04-26 20:07:00 +02:00
|
|
|
commit;
|
|
|
|
insert into tn select row_number()over() from rdb$types;
|
|
|
|
commit;
|
2022-01-23 20:41:55 +01:00
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
-- this will NOT pruduce HJ, at least for WI-T3.0.0.31840
|
2022-01-23 20:41:55 +01:00
|
|
|
-- select * from (select rdb$db_key a from tn) r join (select rdb$db_key b from tn) s on r.a||'' = s.b||'';
|
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
set planonly;
|
|
|
|
--set echo on;
|
2022-01-23 20:41:55 +01:00
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
-- ### 1. Columns from derived tables ###
|
2022-01-23 20:41:55 +01:00
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
select count(*)
|
2022-01-23 20:41:55 +01:00
|
|
|
from (select x a from tn) r
|
|
|
|
join (select x a from tn) s on r.a + 0 = s.a + 0;
|
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
select count(*)
|
2022-01-23 20:41:55 +01:00
|
|
|
from (select x a from tn) r
|
2021-04-26 20:07:00 +02:00
|
|
|
join (select x a from tn) s on r.a + 0 = s.a + 0
|
2022-01-23 20:41:55 +01:00
|
|
|
join (select x a from tn) t on s.a + 0 = t.a + 0;
|
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
-- ### 2. RDB$DB_KEY ###
|
2022-01-23 20:41:55 +01:00
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
----------- test `traditional` join form -----------------
|
|
|
|
select count(*)
|
2022-01-23 20:41:55 +01:00
|
|
|
from (select rdb$db_key||'' a from tn) r
|
2021-04-26 20:07:00 +02:00
|
|
|
join (select rdb$db_key||'' a from tn) s on r.a = s.a;
|
2022-01-23 20:41:55 +01:00
|
|
|
|
|
|
|
select count(*)
|
|
|
|
from (select rdb$db_key||'' a from tn) r
|
2021-04-26 20:07:00 +02:00
|
|
|
join (select rdb$db_key||'' a from tn) s on r.a = s.a
|
|
|
|
join (select rdb$db_key||'' a from tn) t on s.a = t.a;
|
2022-01-23 20:41:55 +01:00
|
|
|
|
|
|
|
select count(*)
|
|
|
|
from (select rdb$db_key||'' a from tn) r
|
2021-04-26 20:07:00 +02:00
|
|
|
join (select rdb$db_key||'' a from tn) s on r.a = s.a
|
|
|
|
join (select rdb$db_key||'' a from tn) t on s.a = t.a
|
2022-01-23 20:41:55 +01:00
|
|
|
join (select rdb$db_key||'' a from tn) u on t.a = u.a;
|
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
----------- test join on named columns form -----------------
|
|
|
|
select count(*)
|
2022-01-23 20:41:55 +01:00
|
|
|
from (select rdb$db_key||'' a from tn) r
|
|
|
|
join (select rdb$db_key||'' a from tn) s using(a);
|
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
-- Currently these will produce NESTED LOOPS, SEE CORE-4809.
|
|
|
|
-- Uncomment these statements and correct expected_output when
|
|
|
|
-- (and if) core-4809 will be fixed:
|
|
|
|
--select count(*)
|
2022-01-23 20:41:55 +01:00
|
|
|
--from (select rdb$db_key||'' a from tn) r
|
2021-04-26 20:07:00 +02:00
|
|
|
--join (select rdb$db_key||'' a from tn) s using(a)
|
2022-01-23 20:41:55 +01:00
|
|
|
--join (select rdb$db_key||'' a from tn) t using(a);
|
2021-04-26 20:07:00 +02:00
|
|
|
--
|
2022-01-23 20:41:55 +01:00
|
|
|
--select count(*)
|
|
|
|
--from (select rdb$db_key||'' a from tn) r
|
2021-04-26 20:07:00 +02:00
|
|
|
--join (select rdb$db_key||'' a from tn) s using(a)
|
|
|
|
--join (select rdb$db_key||'' a from tn) t using(a)
|
2022-01-23 20:41:55 +01:00
|
|
|
--join (select rdb$db_key||'' a from tn) u using(a);
|
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
----------- test natural join form -----------------
|
2022-01-23 20:41:55 +01:00
|
|
|
|
|
|
|
select count(*)
|
2021-04-26 20:07:00 +02:00
|
|
|
from (select rdb$db_key||'' a from tn) r
|
2022-01-23 20:41:55 +01:00
|
|
|
natural join (select rdb$db_key||'' a from tn) s;
|
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
-- Currently these will produce NESTED LOOPS, SEE CORE-4809.
|
|
|
|
-- Uncomment these statements and correct expected_output when
|
|
|
|
-- (and if) core-4809 will be fixed:
|
|
|
|
--select count(*)
|
|
|
|
--from (select rdb$db_key||'' a from tn) r
|
|
|
|
--natural join (select rdb$db_key||'' a from tn) s
|
|
|
|
--natural join (select rdb$db_key||'' a from tn) t;
|
|
|
|
--
|
2022-01-23 20:41:55 +01:00
|
|
|
--select count(*)
|
2021-04-26 20:07:00 +02:00
|
|
|
--from (select rdb$db_key||'' a from tn) r
|
|
|
|
--natural join (select rdb$db_key||'' a from tn) s
|
|
|
|
--natural join (select rdb$db_key||'' a from tn) t
|
2022-01-23 20:41:55 +01:00
|
|
|
--natural join (select rdb$db_key||'' a from tn) u;
|
2021-12-22 20:23:11 +01:00
|
|
|
"""
|
2021-04-26 20:07:00 +02:00
|
|
|
|
2022-01-23 20:41:55 +01:00
|
|
|
act = isql_act('db', test_script)
|
2021-04-26 20:07:00 +02:00
|
|
|
|
2022-04-07 20:23:35 +02:00
|
|
|
fb3x_checked_stdout = """
|
2021-04-26 20:07:00 +02:00
|
|
|
PLAN HASH (S TN NATURAL, R TN NATURAL)
|
|
|
|
PLAN HASH (HASH (T TN NATURAL, S TN NATURAL), R TN NATURAL)
|
|
|
|
PLAN HASH (S TN NATURAL, R TN NATURAL)
|
|
|
|
PLAN HASH (HASH (T TN NATURAL, S TN NATURAL), R TN NATURAL)
|
|
|
|
PLAN HASH (HASH (HASH (U TN NATURAL, T TN NATURAL), S TN NATURAL), R TN NATURAL)
|
|
|
|
PLAN HASH (S TN NATURAL, R TN NATURAL)
|
|
|
|
PLAN HASH (S TN NATURAL, R TN NATURAL)
|
2021-12-22 20:23:11 +01:00
|
|
|
"""
|
2021-04-26 20:07:00 +02:00
|
|
|
|
2022-04-07 20:23:35 +02:00
|
|
|
|
|
|
|
fb5x_checked_stdout = """
|
|
|
|
PLAN HASH (R TN NATURAL, S TN NATURAL)
|
|
|
|
PLAN HASH (R TN NATURAL, S TN NATURAL, T TN NATURAL)
|
|
|
|
PLAN HASH (R TN NATURAL, S TN NATURAL)
|
|
|
|
PLAN HASH (R TN NATURAL, S TN NATURAL, T TN NATURAL)
|
|
|
|
PLAN HASH (R TN NATURAL, S TN NATURAL, T TN NATURAL, U TN NATURAL)
|
|
|
|
PLAN HASH (R TN NATURAL, S TN NATURAL)
|
|
|
|
PLAN HASH (R TN NATURAL, S TN NATURAL)
|
|
|
|
"""
|
|
|
|
|
2021-04-26 20:07:00 +02:00
|
|
|
@pytest.mark.version('>=3.0')
|
2022-01-23 20:41:55 +01:00
|
|
|
def test_1(act: Action):
|
2022-04-08 13:16:01 +02:00
|
|
|
act.expected_stdout = fb3x_checked_stdout if act.is_version('<5') else fb5x_checked_stdout
|
2022-01-23 20:41:55 +01:00
|
|
|
act.execute()
|
|
|
|
assert act.clean_stdout == act.clean_expected_stdout
|
2021-04-26 20:07:00 +02:00
|
|
|
|