Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](mtmv) Fix rewrite wrongly when sync and async materialized view name is same #37311

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ boolean isFinalChosen(Relation relation) {
if (!(relation instanceof PhysicalCatalogRelation)) {
return false;
}
return ((PhysicalCatalogRelation) relation).getTable() instanceof MTMV;
if (!(((PhysicalCatalogRelation) relation).getTable() instanceof MTMV)) {
return false;
}
return ((PhysicalCatalogRelation) relation).getTable().getFullQualifiers().equals(
this.getMaterializationQualifier()
);
}

public Plan getScanPlan() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ Plan doGenerateScanPlan(CascadesContext cascadesContext) {
@Override
List<String> getMaterializationQualifier() {
return ImmutableList.of(olapTable.getDatabase().getCatalog().getName(),
ClusterNamespace.getNameFromFullName(olapTable.getDatabase().getFullName()), indexName);
ClusterNamespace.getNameFromFullName(olapTable.getDatabase().getFullName()),
olapTable.getName(), indexName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some cases?

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !query_mv_before --
1 yy 77.50 33.50 9.50 5
2 mi 57.40 56.20 1.20 2
2 mm 43.20 43.20 43.20 1

-- !query_mtmv_before --
1 yy 0 0 11.50 11.50 11.50 1

-- !query_mv_after --
1 yy 77.50 33.50 9.50 5
2 mi 57.40 56.20 1.20 2
2 mm 43.20 43.20 43.20 1

-- !query_mtmv_after --
1 yy 0 0 11.50 11.50 11.50 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package mv.same_name
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

suite("sync_async_same_name") {
String db = context.config.getDbNameByFile(context.file)
sql "use ${db}"
sql "set runtime_filter_mode=OFF";
sql "SET ignore_shape_nodes='PhysicalDistribute,PhysicalProject'"

sql """
drop table if exists orders
"""

sql """
CREATE TABLE IF NOT EXISTS orders (
o_orderkey INTEGER NOT NULL,
o_custkey INTEGER NOT NULL,
o_orderstatus CHAR(1) NOT NULL,
o_totalprice DECIMALV3(15,2) NOT NULL,
o_orderdate DATE NOT NULL,
o_orderpriority CHAR(15) NOT NULL,
o_clerk CHAR(15) NOT NULL,
o_shippriority INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL
)
DUPLICATE KEY(o_orderkey, o_custkey)
PARTITION BY RANGE(o_orderdate) (
PARTITION `day_2` VALUES LESS THAN ('2023-12-9'),
PARTITION `day_3` VALUES LESS THAN ("2023-12-11"),
PARTITION `day_4` VALUES LESS THAN ("2023-12-30")
)
DISTRIBUTED BY HASH(o_orderkey) BUCKETS 3
PROPERTIES (
"replication_num" = "1"
);
"""

sql """
insert into orders values
(1, 1, 'o', 9.5, '2023-12-08', 'a', 'b', 1, 'yy'),
(1, 1, 'o', 10.5, '2023-12-08', 'a', 'b', 1, 'yy'),
(2, 1, 'o', 11.5, '2023-12-09', 'a', 'b', 1, 'yy'),
(3, 1, 'o', 12.5, '2023-12-10', 'a', 'b', 1, 'yy'),
(3, 1, 'o', 33.5, '2023-12-10', 'a', 'b', 1, 'yy'),
(4, 2, 'o', 43.2, '2023-12-11', 'c','d',2, 'mm'),
(5, 2, 'o', 56.2, '2023-12-12', 'c','d',2, 'mi'),
(5, 2, 'o', 1.2, '2023-12-12', 'c','d',2, 'mi');
"""

sql """analyze table orders with sync;"""

def check_rewrite_but_not_chose = { mv_sql, query_sql, mv_name ->

sql """DROP MATERIALIZED VIEW IF EXISTS ${mv_name}"""
sql"""
CREATE MATERIALIZED VIEW ${mv_name}
BUILD IMMEDIATE REFRESH COMPLETE ON MANUAL
DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES ('replication_num' = '1')
AS ${mv_sql}
"""

def job_name = getJobName(db, mv_name);
waitingMTMVTaskFinished(job_name)
explain {
sql("${query_sql}")
check {result ->
def splitResult = result.split("MaterializedViewRewriteFail")
splitResult.length == 2 ? splitResult[0].contains(mv_name) : false
}
}
}

def common_mv_name = 'common_mv_name'

def mtmv_sql = """
select o_orderdate, o_shippriority, o_comment,
sum(o_totalprice) as sum_total,
max(o_totalprice) as max_total,
min(o_totalprice) as min_total,
count(*) as count_all,
bitmap_union(to_bitmap(case when o_shippriority > 1 and o_orderkey IN (1, 3) then o_custkey else null end)) cnt_1,
bitmap_union(to_bitmap(case when o_shippriority > 2 and o_orderkey IN (2) then o_custkey else null end)) as cnt_2
from orders
group by
o_orderdate,
o_shippriority,
o_comment;
"""
def mtmv_query = """
select o_shippriority, o_comment,
count(distinct case when o_shippriority > 1 and o_orderkey IN (1, 3) then o_custkey else null end) as cnt_1,
count(distinct case when O_SHIPPRIORITY > 2 and o_orderkey IN (2) then o_custkey else null end) as cnt_2,
sum(o_totalprice),
max(o_totalprice),
min(o_totalprice),
count(*)
from orders
where o_orderdate = '2023-12-09'
group by
o_shippriority,
o_comment;
"""

def mv_query = """
select o_shippriority, o_comment,
sum(o_totalprice),
max(o_totalprice),
min(o_totalprice),
count(*)
from orders
group by
o_shippriority,
o_comment;
"""


order_qt_query_mv_before "${mv_query}"
order_qt_query_mtmv_before "${mtmv_query}"


// create sync mv
sql """drop materialized view if exists ${common_mv_name} on orders;"""
createMV ("create materialized view ${common_mv_name} as ${mv_query};")

// create async mv
sql """DROP MATERIALIZED VIEW IF EXISTS ${common_mv_name}"""
sql"""
CREATE MATERIALIZED VIEW ${common_mv_name}
BUILD IMMEDIATE REFRESH COMPLETE ON MANUAL
DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES ('replication_num' = '1')
AS ${mtmv_sql}
"""
def job_name = getJobName(db, common_mv_name);
waitingMTMVTaskFinished(job_name)

// only async mv rewrite successfully
explain {
sql("${mtmv_query}")
check {result ->
def splitResult = result.split("MaterializedViewRewriteFail")
splitResult.length == 2 ? splitResult[0].contains(common_mv_name) : false
}
}

// both sync and async mv rewrite successfully
explain {
sql("${mv_query}")
check {result ->
def splitResult = result.split("MaterializedViewRewriteFail")
splitResult.length == 2 ? splitResult[0].contains(common_mv_name)
&& splitResult[0].contains("orders#${common_mv_name}") : false
}
}


order_qt_query_mv_after "${mv_query}"
order_qt_query_mtmv_after "${mtmv_query}"

sql """DROP MATERIALIZED VIEW IF EXISTS ${common_mv_name}"""
sql """drop materialized view if exists ${common_mv_name} on orders;"""
}
Loading