Skip to content

Commit

Permalink
[improvement](mtmv) Support to add is_used_in_rewrite property when c…
Browse files Browse the repository at this point in the history
…reate materialized view
  • Loading branch information
seawinde committed Sep 3, 2024
1 parent 895dddf commit bdacce6
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ public class PropertyAnalyzer {

public static final String PROPERTIES_ENABLE_NONDETERMINISTIC_FUNCTION =
"enable_nondeterministic_function";

public static final String PROPERTIES_IS_USED_IN_REWRITE =
"is_used_in_rewritten";
public static final String PROPERTIES_EXCLUDED_TRIGGER_TABLES = "excluded_trigger_tables";
public static final String PROPERTIES_REFRESH_PARTITION_NUM = "refresh_partition_num";
public static final String PROPERTIES_WORKLOAD_GROUP = "workload_group";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class MTMVPropertyUtil {
PropertyAnalyzer.PROPERTIES_PARTITION_SYNC_LIMIT,
PropertyAnalyzer.PROPERTIES_PARTITION_TIME_UNIT,
PropertyAnalyzer.PROPERTIES_PARTITION_DATE_FORMAT,
PropertyAnalyzer.PROPERTIES_ENABLE_NONDETERMINISTIC_FUNCTION
PropertyAnalyzer.PROPERTIES_ENABLE_NONDETERMINISTIC_FUNCTION,
PropertyAnalyzer.PROPERTIES_IS_USED_IN_REWRITE
);

public static void analyzeProperty(String key, String value) {
Expand Down Expand Up @@ -66,6 +67,8 @@ public static void analyzeProperty(String key, String value) {
break;
case PropertyAnalyzer.PROPERTIES_ENABLE_NONDETERMINISTIC_FUNCTION:
break;
case PropertyAnalyzer.PROPERTIES_IS_USED_IN_REWRITE:
break;
default:
throw new AnalysisException("illegal key:" + key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.doris.catalog.MaterializedIndexMeta;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.util.PropertyAnalyzer;
import org.apache.doris.mtmv.BaseTableInfo;
import org.apache.doris.mtmv.MTMVCache;
import org.apache.doris.mtmv.MTMVUtil;
Expand All @@ -41,6 +42,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -138,6 +140,16 @@ private List<MaterializationContext> createAsyncMaterializationContext(CascadesC
MTMVCache mtmvCache = null;
try {
mtmvCache = materializedView.getOrGenerateCache(cascadesContext.getConnectContext());
// If mv property is_used_in_rewritten_by_materialized_view is set false, should not partition in
// query rewrite by materialized view
String isUsedByRewritten = materializedView.getMvProperties().get(
PropertyAnalyzer.PROPERTIES_IS_USED_IN_REWRITE);
if (!StringUtils.isEmpty(isUsedByRewritten) && !Boolean.parseBoolean(isUsedByRewritten)) {
LOG.debug(String.format("mv doesn't part in query rewrite because "
+ "is_used_in_rewrite is false, mv is %s",
materializedView.getName()));
continue;
}
if (mtmvCache == null) {
continue;
}
Expand Down
103 changes: 103 additions & 0 deletions regression-test/suites/mtmv_p0/test_is_used_in_rewrite.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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("test_is_used_in_rewrite","mtmv") {
String suiteName = "test_is_used_rewritten"
String tableName = "${suiteName}_table"
String mvName = "${suiteName}_mv"
String db = context.config.getDbNameByFile(context.file)
sql """drop table if exists `${tableName}`"""
sql """drop materialized view if exists ${mvName};"""

sql """
CREATE TABLE ${tableName}
(
k1 TINYINT,
k2 INT not null,
k3 DATE NOT NULL
)
COMMENT "my first table"
DISTRIBUTED BY HASH(k2) BUCKETS 2
PROPERTIES (
"replication_num" = "1"
);
"""
sql """
insert into ${tableName} values(1,1, '2024-05-01'),(2,2, '2024-05-02'),(3,3, '2024-05-03'),
(1,1, '2024-05-01'),(2,2, '2024-05-02'), (3,3, '2024-05-03');
"""

// when not set is_used_in_rewrite, should rewrite successfully
sql """
CREATE MATERIALIZED VIEW ${mvName}
BUILD IMMEDIATE REFRESH AUTO ON MANUAL
DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES (
'replication_num' = '1'
)
AS
SELECT k1, k2, count(*) from ${tableName} group by k1, k2;
"""

waitingMTMVTaskFinished(getJobName(db, mvName))
mv_rewrite_success_without_check_chosen("""
SELECT k2, count(*) from ${tableName} group by k2;
""" ,mvName)
sql """drop materialized view if exists ${mvName};"""


// when set is_used_in_rewrite = true, should rewrite successfully
sql """
CREATE MATERIALIZED VIEW ${mvName}
BUILD IMMEDIATE REFRESH AUTO ON MANUAL
DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES (
'replication_num' = '1',
'is_used_in_rewrite' = 'true'
)
AS
SELECT k1, k2, count(*) from ${tableName} group by k1, k2;
"""

waitingMTMVTaskFinished(getJobName(db, mvName))
mv_rewrite_success_without_check_chosen("""
SELECT k2, count(*) from ${tableName} group by k2;
""" ,mvName)


// when set is_used_in_rewrite = false, should not partition in rewritten
sql """drop materialized view if exists ${mvName};"""
sql """
CREATE MATERIALIZED VIEW ${mvName}
BUILD IMMEDIATE REFRESH AUTO ON MANUAL
DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES (
'replication_num' = '1',
'is_used_in_rewrite' = 'false'
)
AS
SELECT k1, k2, count(*) from ${tableName} group by k1, k2;
"""

waitingMTMVTaskFinished(getJobName(db, mvName))
mv_not_part_in("""
SELECT k2, count(*) from ${tableName} group by k2;
""" ,mvName)

sql """drop materialized view if exists ${mvName};"""
sql """drop table if exists `${tableName}`"""
}

0 comments on commit bdacce6

Please sign in to comment.