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

[BugFix] Only enable query context cache when there are more than one related mvs #54627

Merged
merged 2 commits into from
Jan 3, 2025
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 @@ -108,6 +108,9 @@ public static ConnectorPartitionTraits buildWithCache(ConnectContext ctx, Materi
if (Config.enable_mv_query_context_cache && ctx != null && ctx.getQueryMVContext() != null) {
QueryMaterializationContext queryMVContext = ctx.getQueryMVContext();
Cache<Object, Object> cache = queryMVContext.getMvQueryContextCache();
if (cache == null || queryMVContext.getQueryCacheStats() == null) {
return delegate;
}
return new CachedPartitionTraits(cache, delegate, queryMVContext.getQueryCacheStats(), mv);
} else {
return delegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,26 +233,38 @@ public void prepare(OptExpression queryOptExpression) {
try {
// 1. get related mvs for all input tables
Set<MaterializedView> relatedMVs = getRelatedMVs(queryTables, context.getOptimizerConfig().isRuleBased());
if (relatedMVs.isEmpty()) {
return;
}

// filter mvs which is set by config: including/excluding mvs
Set<MaterializedView> selectedRelatedMVs = getRelatedMVsByConfig(relatedMVs);
logMVPrepare(connectContext, "Choose {}/{} mvs after user config", selectedRelatedMVs.size(), relatedMVs.size());
// add into queryMaterializationContext for later use
this.queryMaterializationContext.addRelatedMVs(selectedRelatedMVs);
if (selectedRelatedMVs.isEmpty()) {
return;
}

// 2. choose best related mvs by user's config or related mv limit
try (Timer t1 = Tracers.watchScope("MVChooseCandidates")) {
selectedRelatedMVs = chooseBestRelatedMVs(queryTables, selectedRelatedMVs, queryOptExpression);
}
if (selectedRelatedMVs.isEmpty()) {
return;
}

// 3. convert to mv with planContext, skip if mv has no valid plan(not SPJG)
Set<MvWithPlanContext> mvWithPlanContexts;
try (Timer t2 = Tracers.watchScope("MVGenerateMvPlan")) {
mvWithPlanContexts = getMvWithPlanContext(selectedRelatedMVs);
}
if (mvWithPlanContexts.isEmpty()) {
return;
}

// 4. process related mvs to candidates
try (Timer t3 = Tracers.watchScope("MVValidateMv")) {
try (Timer t3 = Tracers.watchScope("MVPrepareRelatedMVs")) {
prepareRelatedMVs(queryTables, mvWithPlanContexts);
}

Expand All @@ -269,6 +281,10 @@ public void prepare(OptExpression queryOptExpression) {
processPlanWithView(queryMaterializationContext, connectContext, queryOptExpression,
queryColumnRefFactory, requiredColumns);
}

if (queryMaterializationContext.getValidCandidateMVs().size() > 1) {
queryMaterializationContext.setEnableQueryContextCache(true);
}
} catch (Exception e) {
List<String> tableNames = queryTables.stream().map(Table::getName).collect(Collectors.toList());
logMVPrepare(connectContext, "Prepare query tables {} for mv failed:{}", tableNames, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.api.client.util.Lists;
import com.google.api.client.util.Sets;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps;
import com.google.gson.annotations.SerializedName;
import com.starrocks.catalog.MaterializedView;
import com.starrocks.catalog.MvRefreshArbiter;
import com.starrocks.catalog.MvUpdateInfo;
import com.starrocks.common.Config;
import com.starrocks.common.profile.Tracers;
Expand All @@ -41,7 +43,6 @@
import java.util.Map;
import java.util.Set;

import static com.starrocks.catalog.MvRefreshArbiter.getMVTimelinessUpdateInfo;

/**
* Store materialized view context during the query lifecycle which is seperated from per materialized view's context.
Expand Down Expand Up @@ -69,6 +70,10 @@ public class QueryMaterializationContext {
// It can be be used for more situations later.
private Cache<Object, Object> mvQueryContextCache = null;

// mvQueryContextCache is enabled when related mv is more than 1 since the cache is used to
// cache query predicates rewrite for different mvs.
private boolean isEnableQueryContextCache = false;
// used to cache partition traits result for the connector
private final QueryCacheStats queryCacheStats = new QueryCacheStats();

/**
Expand Down Expand Up @@ -99,8 +104,17 @@ public String toString() {
public QueryMaterializationContext() {
}

public void setEnableQueryContextCache(boolean enableQueryContextCache) {
isEnableQueryContextCache = enableQueryContextCache;
}

@VisibleForTesting
public boolean isEnableQueryContextCache() {
return isEnableQueryContextCache;
}

public Cache<Object, Object> getMvQueryContextCache() {
if (mvQueryContextCache == null) {
if (isEnableQueryContextCache() && mvQueryContextCache == null) {
mvQueryContextCache = Caffeine.newBuilder()
.maximumSize(Config.mv_query_context_cache_max_size)
.recordStats()
Expand All @@ -112,16 +126,21 @@ public Cache<Object, Object> getMvQueryContextCache() {
public PredicateSplit getPredicateSplit(Set<ScalarOperator> predicates,
ReplaceColumnRefRewriter columnRefRewriter) {
// Cache predicate split for predicates because it's time costing if there are too many materialized views.
Object cached = getMvQueryContextCache().getIfPresent(predicates);
if (cached != null) {
return (PredicateSplit) cached;
}
ScalarOperator queryPredicate = rewriteOptExprCompoundPredicate(predicates, columnRefRewriter);
PredicateSplit predicateSplit = PredicateSplit.splitPredicate(queryPredicate);
if (predicateSplit != null) {
getMvQueryContextCache().put(predicates, predicateSplit);
var cache = getMvQueryContextCache();
if (cache == null) {
return PredicateSplit.splitPredicate(rewriteOptExprCompoundPredicate(predicates, columnRefRewriter));
} else {
Object cached = cache.getIfPresent(predicates);
if (cached != null) {
return (PredicateSplit) cached;
}
ScalarOperator queryPredicate = rewriteOptExprCompoundPredicate(predicates, columnRefRewriter);
PredicateSplit predicateSplit = PredicateSplit.splitPredicate(queryPredicate);
if (predicateSplit != null) {
cache.put(predicates, predicateSplit);
}
return predicateSplit;
}
return predicateSplit;
}

private ScalarOperator rewriteOptExprCompoundPredicate(Set<ScalarOperator> conjuncts,
Expand All @@ -139,15 +158,20 @@ public ScalarOperator getCanonizedPredicate(ScalarOperator predicate) {
return null;
}

return (ScalarOperator) getMvQueryContextCache().get(predicate, x -> {
ScalarOperator rewritten = new ScalarOperatorRewriter()
.rewrite(predicate.clone(), ScalarOperatorRewriter.MV_SCALAR_REWRITE_RULES);
return rewritten;
});
var cache = getMvQueryContextCache();
if (cache == null) {
return new ScalarOperatorRewriter().rewrite(predicate.clone(), ScalarOperatorRewriter.MV_SCALAR_REWRITE_RULES);
} else {
return (ScalarOperator) getMvQueryContextCache().get(predicate, x -> {
ScalarOperator rewritten = new ScalarOperatorRewriter()
.rewrite(predicate.clone(), ScalarOperatorRewriter.MV_SCALAR_REWRITE_RULES);
return rewritten;
});
}
}

public QueryCacheStats getQueryCacheStats() {
return queryCacheStats;
return mvQueryContextCache == null ? null : queryCacheStats;
}

public OptExpression getQueryOptPlanWithView() {
Expand Down Expand Up @@ -191,13 +215,8 @@ public MvUpdateInfo getOrInitMVTimelinessInfos(MaterializedView mv) {
if (mv == null) {
return null;
}
if (!mvTimelinessInfos.containsKey(mv)) {
MvUpdateInfo result = getMVTimelinessUpdateInfo(mv, true);
mvTimelinessInfos.put(mv, result);
return result;
} else {
return mvTimelinessInfos.get(mv);
}
return mvTimelinessInfos.computeIfAbsent(mv,
ignored -> MvRefreshArbiter.getMVTimelinessUpdateInfo(mv, true));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

package com.starrocks.sql.optimizer.rule.transformation.materialization;

import com.github.benmanes.caffeine.cache.Cache;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -1422,8 +1421,6 @@ public static PredicateSplit getQuerySplitPredicate(OptimizerContext optimizerCo
}

QueryMaterializationContext queryMaterializationContext = optimizerContext.getQueryMaterializationContext();
Cache<Object, Object> predicateSplitCache = queryMaterializationContext.getMvQueryContextCache();
Preconditions.checkArgument(predicateSplitCache != null);
// Cache predicate split for predicates because it's time costing if there are too many materialized views.
return queryMaterializationContext.getPredicateSplit(queryConjuncts, queryColumnRefRewriter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ public void testRefreshWithCachePartitionTraits() {
"REFRESH DEFERRED MANUAL\n" +
"AS SELECT id, data, date FROM `iceberg0`.`partitioned_db`.`t1` as a;",
() -> {
UtFrameUtils.mockEnableQueryContextCache();
MaterializedView mv = getMv("test", "test_mv1");
PartitionBasedMvRefreshProcessor processor = refreshMV("test", mv);
RuntimeProfile runtimeProfile = processor.getRuntimeProfile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.starrocks.sql.plan.ExecPlan;
import com.starrocks.sql.plan.PlanTestBase;
import com.starrocks.thrift.TGetTasksParams;
import com.starrocks.utframe.UtFrameUtils;
import mockit.Mock;
import mockit.MockUp;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -274,6 +275,7 @@ public void testRefreshWithCachePartitionTraits() throws Exception {
"refresh deferred manual\n" +
"as select k1, k2, sum(v1) as total from tbl1 group by k1, k2;",
() -> {
UtFrameUtils.mockEnableQueryContextCache();
executeInsertSql(connectContext, "insert into tbl1 values(\"2022-02-20\", 1, 10)");
OlapTable table = (OlapTable) getTable("test", "tbl1");
MaterializedView mv = getMv("test", "test_mv1");
Expand Down
10 changes: 10 additions & 0 deletions fe/fe-core/src/test/java/com/starrocks/utframe/UtFrameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
import com.starrocks.sql.optimizer.OptExpression;
import com.starrocks.sql.optimizer.Optimizer;
import com.starrocks.sql.optimizer.OptimizerConfig;
import com.starrocks.sql.optimizer.QueryMaterializationContext;
import com.starrocks.sql.optimizer.base.ColumnRefFactory;
import com.starrocks.sql.optimizer.base.ColumnRefSet;
import com.starrocks.sql.optimizer.base.PhysicalPropertySet;
Expand Down Expand Up @@ -1340,6 +1341,15 @@ public void execute() throws Exception {
};
}

public static void mockEnableQueryContextCache() {
new MockUp<QueryMaterializationContext>() {
@Mock
public boolean isEnableQueryContextCache() {
return true;
}
};
}

public static void mockDML() {
new MockUp<StmtExecutor>() {
/**
Expand Down
Loading