From 7e6701865fe8d89a083d396cddfb091002302684 Mon Sep 17 00:00:00 2001 From: seawinde Date: Mon, 24 Feb 2025 11:41:51 +0800 Subject: [PATCH] [fix](mtmv) Fix nest mtmv rewrite fail when bottom mtmv cache is invalid --- .../java/org/apache/doris/catalog/MTMV.java | 4 -- .../trees/plans/logical/LogicalOlapScan.java | 54 +++++++++++++++---- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java index c540ee5c5417dd..d96a4997a80e12 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java @@ -332,10 +332,6 @@ public MTMVCache getOrGenerateCache(ConnectContext connectionContext) throws Ana } } - public MTMVCache getCache() { - return cache; - } - public Map getMvProperties() { readMvLock(); try { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java index 4f0492ec63dc8a..7a81b2438b8cf1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java @@ -22,6 +22,7 @@ import org.apache.doris.catalog.MTMV; import org.apache.doris.catalog.OlapTable; import org.apache.doris.catalog.Table; +import org.apache.doris.common.AnalysisException; import org.apache.doris.mtmv.MTMVCache; import org.apache.doris.nereids.memo.GroupExpression; import org.apache.doris.nereids.properties.DataTrait; @@ -48,6 +49,8 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.apache.commons.lang3.tuple.Pair; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.json.JSONObject; import java.util.ArrayList; @@ -64,6 +67,8 @@ */ public class LogicalOlapScan extends LogicalCatalogRelation implements OlapScan { + private static final Logger LOG = LogManager.getLogger(LogicalOlapScan.class); + /////////////////////////////////////////////////////////////////////////// // Members for materialized index. /////////////////////////////////////////////////////////////////////////// @@ -529,9 +534,15 @@ AGGREGATE KEY (siteid,citycode,username) Set outputSet = Utils.fastToImmutableSet(getOutputSet()); if (getTable() instanceof MTMV) { MTMV mtmv = (MTMV) getTable(); - MTMVCache cache = mtmv.getCache(); + MTMVCache cache; + try { + cache = mtmv.getOrGenerateCache(ConnectContext.get()); + } catch (AnalysisException e) { + LOG.warn(String.format("LogicalOlapScan computeUnique fail, mv name is %s", mtmv.getName()), e); + return; + } // Maybe query specified index, should not calc, such as select count(*) from t1 index col_index - if (cache == null || this.getSelectedIndexId() != this.getTable().getBaseIndexId()) { + if (this.getSelectedIndexId() != this.getTable().getBaseIndexId()) { return; } Plan originalPlan = cache.getOriginalPlan(); @@ -562,9 +573,15 @@ && getTable().getKeysType() == KeysType.UNIQUE_KEYS) { public void computeUniform(DataTrait.Builder builder) { if (getTable() instanceof MTMV) { MTMV mtmv = (MTMV) getTable(); - MTMVCache cache = mtmv.getCache(); + MTMVCache cache; + try { + cache = mtmv.getOrGenerateCache(ConnectContext.get()); + } catch (AnalysisException e) { + LOG.warn(String.format("LogicalOlapScan computeUniform fail, mv name is %s", mtmv.getName()), e); + return; + } // Maybe query specified index, should not calc, such as select count(*) from t1 index col_index - if (cache == null || this.getSelectedIndexId() != this.getTable().getBaseIndexId()) { + if (this.getSelectedIndexId() != this.getTable().getBaseIndexId()) { return; } Plan originalPlan = cache.getOriginalPlan(); @@ -577,9 +594,15 @@ public void computeUniform(DataTrait.Builder builder) { public void computeEqualSet(DataTrait.Builder builder) { if (getTable() instanceof MTMV) { MTMV mtmv = (MTMV) getTable(); - MTMVCache cache = mtmv.getCache(); + MTMVCache cache; + try { + cache = mtmv.getOrGenerateCache(ConnectContext.get()); + } catch (AnalysisException e) { + LOG.warn(String.format("LogicalOlapScan computeEqualSet fail, mv name is %s", mtmv.getName()), e); + return; + } // Maybe query specified index, should not calc, such as select count(*) from t1 index col_index - if (cache == null || this.getSelectedIndexId() != this.getTable().getBaseIndexId()) { + if (this.getSelectedIndexId() != this.getTable().getBaseIndexId()) { return; } Plan originalPlan = cache.getOriginalPlan(); @@ -592,9 +615,15 @@ public void computeEqualSet(DataTrait.Builder builder) { public void computeFd(DataTrait.Builder builder) { if (getTable() instanceof MTMV) { MTMV mtmv = (MTMV) getTable(); - MTMVCache cache = mtmv.getCache(); + MTMVCache cache; + try { + cache = mtmv.getOrGenerateCache(ConnectContext.get()); + } catch (AnalysisException e) { + LOG.warn(String.format("LogicalOlapScan computeFd fail, mv name is %s", mtmv.getName()), e); + return; + } // Maybe query specified index, should not calc, such as select count(*) from t1 index col_index - if (cache == null || this.getSelectedIndexId() != this.getTable().getBaseIndexId()) { + if (this.getSelectedIndexId() != this.getTable().getBaseIndexId()) { return; } Plan originalPlan = cache.getOriginalPlan(); @@ -607,7 +636,14 @@ Map constructReplaceMap(MTMV mtmv) { Map replaceMap = new HashMap<>(); // Need remove invisible column, and then mapping them List originOutputs = new ArrayList<>(); - for (Slot originSlot : mtmv.getCache().getOriginalPlan().getOutput()) { + MTMVCache cache; + try { + cache = mtmv.getOrGenerateCache(ConnectContext.get()); + } catch (AnalysisException e) { + LOG.warn(String.format("LogicalOlapScan constructReplaceMap fail, mv name is %s", mtmv.getName()), e); + return replaceMap; + } + for (Slot originSlot : cache.getOriginalPlan().getOutput()) { if (!(originSlot instanceof SlotReference) || (((SlotReference) originSlot).isVisible())) { originOutputs.add(originSlot); }