Skip to content

Commit

Permalink
[fix](nereids) Fix query rewrite fail when query cache build quickly
Browse files Browse the repository at this point in the history
  • Loading branch information
seawinde committed Dec 22, 2023
1 parent d75300f commit 9f5c97e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.mtmv.BaseTableInfo;
import org.apache.doris.mtmv.MTMVCache;
import org.apache.doris.mtmv.MTMVPartitionInfo;
import org.apache.doris.mtmv.MTMVUtil;
import org.apache.doris.nereids.CascadesContext;
Expand Down Expand Up @@ -104,13 +103,8 @@ protected List<Plan> rewrite(Plan queryPlan, CascadesContext cascadesContext) {
logger.debug(currentClassName + " this group is already rewritten so skip");
continue;
}
MTMV mtmv = materializationContext.getMTMV();
MTMVCache mtmvCache = getCacheFromMTMV(mtmv);
if (mtmvCache == null) {
logger.warn(currentClassName + " mv cache is null so return");
return rewriteResults;
}
List<StructInfo> viewStructInfos = extractStructInfo(mtmvCache.getLogicalPlan(), cascadesContext);
List<StructInfo> viewStructInfos = extractStructInfo(materializationContext.getMvPlan(),
cascadesContext);
if (viewStructInfos.size() > 1) {
// view struct info should only have one
logger.warn(currentClassName + " the num of view struct info is more then one so return");
Expand Down Expand Up @@ -289,17 +283,6 @@ protected boolean checkPartitionIsValid(
&& relatedTalbeValidSet.containsAll(relatedTableSelectedPartitionToCheck);
}

private MTMVCache getCacheFromMTMV(MTMV mtmv) {
MTMVCache cache;
try {
cache = mtmv.getOrGenerateCache();
} catch (AnalysisException analysisException) {
logger.warn(this.getClass().getSimpleName() + " get mtmv cache analysisException", analysisException);
return null;
}
return cache;
}

/**
* Rewrite query by view, for aggregate or join rewriting should be different inherit class implementation
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class MaterializationContext {
// generate form mv scan plan
private ExpressionMapping mvExprToMvScanExprMapping;
private boolean available = true;
// the mv plan from cache at present, record it to make sure query rewrite by mv is right when cache change.
private Plan mvPlan;

/**
* MaterializationContext, this contains necessary info for query rewriting by mv
Expand Down Expand Up @@ -81,6 +83,8 @@ public MaterializationContext(MTMV mtmv,
mtmvCache.getMvOutputExpressions(),
mtmvCache.getLogicalPlan()),
mvScanPlan.getExpressions());
// copy the plan from cache, which the plan in cache may change
this.mvPlan = mtmvCache.getLogicalPlan();
}

public Set<GroupId> getMatchedGroups() {
Expand Down Expand Up @@ -119,6 +123,10 @@ public boolean isAvailable() {
return available;
}

public Plan getMvPlan() {
return mvPlan;
}

/**
* MaterializationContext fromMaterializedView
*/
Expand Down

0 comments on commit 9f5c97e

Please sign in to comment.