From 2d7acd01beab07e8b9720d7cb7e40e205de4c927 Mon Sep 17 00:00:00 2001 From: seawinde <149132972+seawinde@users.noreply.github.com> Date: Tue, 16 Jul 2024 11:46:40 +0800 Subject: [PATCH] [improvement](mv) Catch exception when init sync materialized view context #37701 (#37872) ## Proposed changes cherry-pick 3.0 pr: https://github.com/apache/doris/pull/37701 commit: e478570e pr: https://github.com/apache/doris/pull/37154 commit: 841e39a2 --------- Co-authored-by: starocean999 <40539150+starocean999@users.noreply.github.com> --- .../org/apache/doris/catalog/OlapTable.java | 9 +++ .../mv/InitMaterializationContextHook.java | 64 +++++++++++-------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/OlapTable.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/OlapTable.java index 7d3b9baf3417ec..471cbdf0ec642a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/OlapTable.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/OlapTable.java @@ -2990,4 +2990,13 @@ public long getRemoteDataSize() { public long getReplicaCount() { return statistics.getReplicaCount(); } + + public boolean isShadowIndex(long indexId) { + String indexName = getIndexNameById(indexId); + if (indexName != null && indexName.startsWith(org.apache.doris.alter.SchemaChangeHandler.SHADOW_NAME_PREFIX)) { + return true; + } else { + return false; + } + } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/InitMaterializationContextHook.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/InitMaterializationContextHook.java index 072765fb2a4a49..53a352e2ef80e9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/InitMaterializationContextHook.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/InitMaterializationContextHook.java @@ -145,38 +145,46 @@ private List createSyncMvContexts(OlapTable olapTabl keyCount += column.isKey() ? 1 : 0; } for (Map.Entry entry : olapTable.getIndexNameToId().entrySet()) { - if (entry.getValue() != baseIndexId) { - MaterializedIndexMeta meta = olapTable.getIndexMetaByIndexId(entry.getValue()); - String createMvSql; - if (meta.getDefineStmt() != null) { - // get the original create mv sql - createMvSql = meta.getDefineStmt().originStmt; - } else { - // it's rollup, need assemble create mv sql manually - if (olapTable.getKeysType() == KeysType.AGG_KEYS) { - createMvSql = assembleCreateMvSqlForAggTable(olapTable.getQualifiedName(), - entry.getKey(), meta.getSchema(false), keyCount); + long indexId = entry.getValue(); + try { + // when doing schema change, a shadow index would be created and put together with mv indexes + // we must roll out these unexpected shadow indexes here + if (indexId != baseIndexId && !olapTable.isShadowIndex(indexId)) { + MaterializedIndexMeta meta = olapTable.getIndexMetaByIndexId(entry.getValue()); + String createMvSql; + if (meta.getDefineStmt() != null) { + // get the original create mv sql + createMvSql = meta.getDefineStmt().originStmt; } else { - createMvSql = - assembleCreateMvSqlForDupOrUniqueTable(olapTable.getQualifiedName(), - entry.getKey(), meta.getSchema(false)); + // it's rollup, need assemble create mv sql manually + if (olapTable.getKeysType() == KeysType.AGG_KEYS) { + createMvSql = assembleCreateMvSqlForAggTable(olapTable.getQualifiedName(), + entry.getKey(), meta.getSchema(false), keyCount); + } else { + createMvSql = + assembleCreateMvSqlForDupOrUniqueTable(olapTable.getQualifiedName(), + entry.getKey(), meta.getSchema(false)); + } } - } - if (createMvSql != null) { - Optional querySql = - new NereidsParser().parseForSyncMv(createMvSql); - if (!querySql.isPresent()) { - LOG.warn(String.format("can't parse %s ", createMvSql)); - continue; + if (createMvSql != null) { + Optional querySql = + new NereidsParser().parseForSyncMv(createMvSql); + if (!querySql.isPresent()) { + LOG.warn(String.format("can't parse %s ", createMvSql)); + continue; + } + MTMVCache mtmvCache = MaterializedViewUtils.createMTMVCache(querySql.get(), + cascadesContext.getConnectContext()); + contexts.add(new SyncMaterializationContext(mtmvCache.getLogicalPlan(), + mtmvCache.getOriginalPlan(), olapTable, meta.getIndexId(), entry.getKey(), + cascadesContext, mtmvCache.getStatistics())); + } else { + LOG.warn(String.format("can't assemble create mv sql for index ", entry.getKey())); } - MTMVCache mtmvCache = MaterializedViewUtils.createMTMVCache(querySql.get(), - cascadesContext.getConnectContext()); - contexts.add(new SyncMaterializationContext(mtmvCache.getLogicalPlan(), - mtmvCache.getOriginalPlan(), olapTable, meta.getIndexId(), entry.getKey(), - cascadesContext, mtmvCache.getStatistics())); - } else { - LOG.warn(String.format("can't assemble create mv sql for index ", entry.getKey())); } + } catch (Exception exception) { + LOG.warn(String.format("createSyncMvContexts exception, index id is %s, index name is %s", + entry.getValue(), entry.getValue()), exception); } } return contexts;