Skip to content

Commit

Permalink
[fix](nereids) mv selection should only consider visible indexes (#38148
Browse files Browse the repository at this point in the history
)

the indexes in olap table are not always visible. Sometimes, schema
change will introduce invisible index into olap table. And we should
only use getVisibleIndexIdToMeta() to get visible indexes as candidates
for mv selection.
  • Loading branch information
starocean999 authored and dataroaring committed Jul 24, 2024
1 parent 31bc598 commit 9ec31c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,12 @@ private List<SyncMaterializationContext> createSyncMvContexts(OlapTable olapTabl
for (Column column : olapTable.getFullSchema()) {
keyCount += column.isKey() ? 1 : 0;
}
for (Map.Entry<String, Long> entry : olapTable.getIndexNameToId().entrySet()) {
long indexId = entry.getValue();
for (Map.Entry<Long, MaterializedIndexMeta> entry : olapTable.getVisibleIndexIdToMeta().entrySet()) {
long indexId = entry.getKey();
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());
if (indexId != baseIndexId) {
MaterializedIndexMeta meta = entry.getValue();
String indexName = olapTable.getIndexNameById(indexId);
String createMvSql;
if (meta.getDefineStmt() != null) {
// get the original create mv sql
Expand All @@ -183,11 +182,11 @@ private List<SyncMaterializationContext> createSyncMvContexts(OlapTable olapTabl
// 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);
indexName, meta.getSchema(false), keyCount);
} else {
createMvSql =
assembleCreateMvSqlForDupOrUniqueTable(olapTable.getQualifiedName(),
entry.getKey(), meta.getSchema(false));
indexName, meta.getSchema(false));
}
}
if (createMvSql != null) {
Expand All @@ -200,10 +199,10 @@ private List<SyncMaterializationContext> createSyncMvContexts(OlapTable olapTabl
MTMVCache mtmvCache = MaterializedViewUtils.createMTMVCache(querySql.get(),
cascadesContext.getConnectContext());
contexts.add(new SyncMaterializationContext(mtmvCache.getLogicalPlan(),
mtmvCache.getOriginalPlan(), olapTable, meta.getIndexId(), entry.getKey(),
mtmvCache.getOriginalPlan(), olapTable, meta.getIndexId(), indexName,
cascadesContext, mtmvCache.getStatistics()));
} else {
LOG.warn(String.format("can't assemble create mv sql for index ", entry.getKey()));
LOG.warn(String.format("can't assemble create mv sql for index ", indexName));
}
}
} catch (Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ suite ("distinctQuery") {
createMV("create materialized view distinctQuery_mv2 as select empid, deptno, count(salary) from distinctQuery group by empid, deptno;")

sql """insert into distinctQuery values("2020-01-01",1,"a",1,1,1);"""
sql """insert into distinctQuery values("2020-01-01",2,"a",1,1,1);"""

sql "analyze table distinctQuery with sync;"
sql """set enable_stats=false;"""
Expand Down

0 comments on commit 9ec31c9

Please sign in to comment.