Skip to content

Commit

Permalink
[UT][CherryPick] add tests for all join types' mv rewrite for 2.5 (#2…
Browse files Browse the repository at this point in the history
…5936) (#26121)

support all join types's mv rewrite and add UTs and sql tests.

Signed-off-by: ABingHuang <[email protected]>
  • Loading branch information
ABingHuang authored Jun 28, 2023
1 parent dd43c53 commit 0a371e0
Show file tree
Hide file tree
Showing 4 changed files with 505 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private boolean isMVApplicable(OptExpression mvExpression,
// because optimizer will match MV's pattern which is subset of query opt tree
// from top-down iteration.
if (matchMode == MatchMode.COMPLETE) {
if (!isJoinMatch(queryExpression, mvExpression, queryTables, mvTables)) {
if (!isJoinMatch(queryExpression, mvExpression)) {
return false;
}
} else if (matchMode == MatchMode.VIEW_DELTA) {
Expand Down Expand Up @@ -893,9 +893,7 @@ private ColumnRefOperator getColumnRef(String columnName, LogicalScanOperator sc
}

private boolean isJoinMatch(OptExpression queryExpression,
OptExpression mvExpression,
List<Table> queryTables,
List<Table> mvTables) {
OptExpression mvExpression) {
boolean isQueryAllEqualInnerJoin = MvUtils.isAllEqualInnerOrCrossJoin(queryExpression);
boolean isMVAllEqualInnerJoin = MvUtils.isAllEqualInnerOrCrossJoin(mvExpression);
if (isQueryAllEqualInnerJoin && isMVAllEqualInnerJoin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3103,4 +3103,182 @@ public void testJoinDeriveRewrite() {
" partitions=1/1");
}
}

@Test
public void testRightOuterJoin() {
{
String q = "select empid, depts.deptno from emps\n"
+ "right outer join depts using (deptno)";
String m = "select empid, depts.deptno from emps\n"
+ "right outer join depts using (deptno)";
testRewriteOK(m, q);
}

{
String q = "select empid, depts.deptno from emps\n"
+ "right outer join depts using (deptno) where empid = 1";
String m = "select empid, depts.deptno from emps\n"
+ "right outer join depts using (deptno)";
testRewriteOK(m, q);
}
}

@Test
public void testFullOuterJoin() {
{
String q = "select empid, depts.deptno from emps\n"
+ "full outer join depts using (deptno)";
String m = "select empid, depts.deptno from emps\n"
+ "full outer join depts using (deptno)";
testRewriteOK(m, q);
}

{
String q = "select empid, depts.deptno from emps\n"
+ "full outer join depts using (deptno) where empid = 1";
String m = "select empid, depts.deptno from emps\n"
+ "full outer join depts using (deptno)";
testRewriteOK(m, q);
}
}

@Test
public void testLeftSemiJoinJoin() {
{
String q = "select empid from emps\n"
+ "left semi join depts using (deptno)";
String m = "select empid from emps\n"
+ "left semi join depts using (deptno)";
testRewriteOK(m, q);
}

{
String q = "select empid from emps\n"
+ "left semi join depts using (deptno) where empid = 1";
String m = "select empid from emps\n"
+ "left semi join depts using (deptno)";
testRewriteOK(m, q);
}
}

@Test
public void testLeftAntiJoinJoin() {
{
String q = "select empid from emps\n"
+ "left anti join depts using (deptno)";
String m = "select empid from emps\n"
+ "left anti join depts using (deptno)";
testRewriteOK(m, q);
}

{
String q = "select empid from emps\n"
+ "left anti join depts using (deptno) where empid = 1";
String m = "select empid from emps\n"
+ "left anti join depts using (deptno)";
testRewriteOK(m, q);
}
}

@Test
public void testRightSemiJoinJoin() {
{
String q = "select deptno from emps\n"
+ "right semi join depts using (deptno)";
String m = "select deptno from emps\n"
+ "right semi join depts using (deptno)";
testRewriteOK(m, q);
}

{
String q = "select deptno from emps\n"
+ "right semi join depts using (deptno) where deptno = 1";
String m = "select deptno from emps\n"
+ "right semi join depts using (deptno)";
testRewriteOK(m, q);
}
}

@Test
public void testRightAntiJoinJoin() {
{
String q = "select deptno from emps\n"
+ "left anti join depts using (deptno)";
String m = "select deptno from emps\n"
+ "left anti join depts using (deptno)";
testRewriteOK(m, q);
}

{
String q = "select deptno from emps\n"
+ "right anti join depts using (deptno) where deptno = 1";
String m = "select deptno from emps\n"
+ "right anti join depts using (deptno)";
testRewriteOK(m, q);
}
}

@Test
public void testMultiJoinTypes() {
{
testRewriteOK("select depts.deptno, dependents.empid\n"
+ "from depts\n"
+ "left outer join dependents on (depts.name = dependents.name)\n"
+ "right outer join emps on (emps.deptno = depts.deptno)\n",
"select dependents.empid\n"
+ "from depts\n"
+ "left outer join dependents on (depts.name = dependents.name)\n"
+ "right outer join emps on (emps.deptno = depts.deptno)\n"
+ "where depts.deptno > 10");
}

{
testRewriteOK("select depts.deptno, dependents.empid\n"
+ "from depts\n"
+ "left outer join dependents on (depts.name = dependents.name)\n"
+ "right outer join emps on (emps.deptno = depts.deptno)\n"
+ "where depts.deptno > 10",
"select dependents.empid\n"
+ "from depts\n"
+ "left outer join dependents on (depts.name = dependents.name)\n"
+ "right outer join emps on (emps.deptno = depts.deptno)\n"
+ "where depts.deptno > 10");
}

{
testRewriteOK("select depts.deptno, dependents.empid\n"
+ "from depts\n"
+ "full outer join dependents on (depts.name = dependents.name)\n"
+ "right outer join emps on (emps.deptno = depts.deptno)\n",
"select dependents.empid\n"
+ "from depts\n"
+ "full outer join dependents on (depts.name = dependents.name)\n"
+ "right outer join emps on (emps.deptno = depts.deptno)\n"
+ "where depts.deptno > 10");
}

{
testRewriteOK("select depts.deptno\n"
+ "from depts\n"
+ "left semi join dependents on (depts.name = dependents.name)\n"
+ "left anti join emps on (emps.deptno = depts.deptno)\n",
"select depts.deptno\n"
+ "from depts\n"
+ "left semi join dependents on (depts.name = dependents.name)\n"
+ "left anti join emps on (emps.deptno = depts.deptno)\n"
+ "where depts.deptno > 10");
}

{
testRewriteOK("select emps.empid\n"
+ "from depts\n"
+ "left semi join dependents on (depts.name = dependents.name)\n"
+ "right anti join emps on (emps.deptno = depts.deptno)\n",
"select emps.empid\n"
+ "from depts\n"
+ "left semi join dependents on (depts.name = dependents.name)\n"
+ "right anti join emps on (emps.deptno = depts.deptno)\n"
+ "where emps.empid > 10");
}
}
}
179 changes: 178 additions & 1 deletion test/sql/test_materialized_view/R/test_materialized_view_rewrite
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,181 @@ select user_id, hll_cardinality(hll_union(hll_hash(tag_id))) x from user_tags gr
-- !result
drop materialized view user_tags_mv3;
-- result:
-- !result
-- !result

-- name: test_all_join_type_rewrite
CREATE TABLE emps (
empid INT NOT NULL,
deptno INT NOT NULL,
locationid INT NOT NULL,
commission INT NOT NULL,
name VARCHAR(20) NOT NULL,
salary DECIMAL(18, 2)
) ENGINE=OLAP
DUPLICATE KEY(`empid`)
DISTRIBUTED BY HASH(`empid`) BUCKETS 12
PROPERTIES (
"replication_num" = "1"
);

CREATE TABLE depts(
deptno INT NOT NULL,
name VARCHAR(20)
) ENGINE=OLAP
DUPLICATE KEY(`deptno`)
DISTRIBUTED BY HASH(`deptno`) BUCKETS 12
PROPERTIES (
"replication_num" = "1"
);

CREATE TABLE dependents(
empid INT NOT NULL,
name VARCHAR(20)
) ENGINE=OLAP
DUPLICATE KEY(`empid`)
DISTRIBUTED BY HASH(`empid`) BUCKETS 12
PROPERTIES (
"replication_num" = "1"
);

insert into emps values(1, 1, 1, 10, "emp_name1", 1000);
insert into emps values(2, 1, 1, 10, "emp_name1", 1000);
insert into emps values(3, 1, 1, 10, "emp_name1", 1000);

insert into depts values(1, "dept_name1");
insert into depts values(2, "dept_name2");

insert into dependents values(1, "dependents_name1");
insert into dependents values(2, "dependents_name2");
insert into dependents values(3, "dependents_name3");

create materialized view mv_right_outer
distributed by hash(`empid`) buckets 10
refresh manual
as
select empid, depts.deptno
from emps right outer join depts using (deptno);

refresh materialized view mv_right_outer with sync mode;

explain logical select empid, depts.deptno
from emps right outer join depts using (deptno);
-- result:
[REGEX]mv_right_outer

select empid, depts.deptno
from emps right outer join depts using (deptno) order by empid;
-- result:
NULL 1
1 1
2 1
3 1

drop materialized view mv_right_outer;

create materialized view mv_full_outer
distributed by hash(`empid`) buckets 10
refresh manual
as
select empid, depts.deptno
from emps full outer join depts using (deptno);

refresh materialized view mv_full_outer with sync mode;

explain logical select empid, depts.deptno
from emps full outer join depts using (deptno);
-- result:
[REGEX]mv_full_outer

select empid, depts.deptno
from emps full outer join depts using (deptno) order by empid;
-- result:
NULL 1
1 1
2 1
3 1

drop materialized view mv_full_outer;

create materialized view mv_left_semi
distributed by hash(`empid`) buckets 10
refresh manual
as
select empid
from emps left semi join depts using (deptno);

refresh materialized view mv_left_semi with sync mode;

explain logical select empid
from emps left semi join depts using (deptno);
-- result:
[REGEX]mv_left_semi

select empid
from emps left semi join depts using (deptno) order by empid;
-- result:
1
2
3

drop materialized view mv_left_semi;

create materialized view mv_left_anti
distributed by hash(`empid`) buckets 10
refresh manual
as
select empid
from emps left anti join depts using (deptno);

refresh materialized view mv_left_anti with sync mode;

explain logical select empid
from emps left anti join depts using (deptno);
-- result:
[REGEX]mv_left_anti

select empid
from emps left anti join depts using (deptno) order by empid;
-- result:

drop materialized view mv_left_anti;

create materialized view mv_right_semi
distributed by hash(`deptno`) buckets 10
refresh manual
as
select deptno
from emps right semi join depts using (deptno);

refresh materialized view mv_right_semi with sync mode;

explain select deptno
from emps right semi join depts using (deptno);
-- result:
[REGEX]mv_right_semi

select deptno
from emps right semi join depts using (deptno) order by deptno;
-- result:
1

drop materialized view mv_right_semi;

create materialized view mv_right_anti
distributed by hash(`deptno`) buckets 10
refresh manual
as
select deptno
from emps right anti join depts using (deptno);

refresh materialized view mv_right_anti with sync mode;

explain select depts.deptno
from emps right anti join depts using (deptno);
-- result:
[REGEX]mv_right_anti

select depts.deptno
from emps right anti join depts using (deptno);
-- result:
2
Loading

0 comments on commit 0a371e0

Please sign in to comment.