[fix](mtmv)Fix the old version of the materialized view error 'Curren… (#42307)

…t database is not set'` (#42152)

pick: https://github.com/apache/doris/pull/42152
This commit is contained in:
zhangdong
2024-10-24 11:16:01 +08:00
committed by GitHub
parent cbdaaa62b2
commit 5453337e7b
3 changed files with 94 additions and 0 deletions

View File

@ -26,6 +26,7 @@ import org.apache.doris.common.util.PropertyAnalyzer;
import org.apache.doris.datasource.CatalogMgr;
import org.apache.doris.job.common.TaskStatus;
import org.apache.doris.job.extensions.mtmv.MTMVTask;
import org.apache.doris.mtmv.EnvInfo;
import org.apache.doris.mtmv.MTMVCache;
import org.apache.doris.mtmv.MTMVJobInfo;
import org.apache.doris.mtmv.MTMVJobManager;
@ -71,6 +72,9 @@ public class MTMV extends OlapTable {
private String querySql;
@SerializedName("s")
private MTMVStatus status;
@Deprecated
@SerializedName("ei")
private EnvInfo envInfo;
@SerializedName("ji")
private MTMVJobInfo jobInfo;
@SerializedName("mp")
@ -108,6 +112,7 @@ public class MTMV extends OlapTable {
this.mvPartitionInfo = params.mvPartitionInfo;
this.relation = params.relation;
this.refreshSnapshot = new MTMVRefreshSnapshot();
this.envInfo = new EnvInfo(-1L, -1L);
mvRwLock = new ReentrantReadWriteLock(true);
}
@ -138,6 +143,10 @@ public class MTMV extends OlapTable {
}
}
public EnvInfo getEnvInfo() {
return envInfo;
}
public MTMVJobInfo getJobInfo() {
readMvLock();
try {
@ -490,6 +499,11 @@ public class MTMV extends OlapTable {
refreshInfo = materializedView.refreshInfo;
querySql = materializedView.querySql;
status = materializedView.status;
if (materializedView.envInfo != null) {
envInfo = materializedView.envInfo;
} else {
envInfo = new EnvInfo(-1L, -1L);
}
jobInfo = materializedView.jobInfo;
mvProperties = materializedView.mvProperties;
relation = materializedView.relation;

View File

@ -0,0 +1,52 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.doris.mtmv;
import com.google.gson.annotations.SerializedName;
/**
* EnvInfo
*/
@Deprecated
public class EnvInfo {
@SerializedName("ci")
private long ctlId;
@SerializedName("di")
private long dbId;
public EnvInfo(long ctlId, long dbId) {
this.ctlId = ctlId;
this.dbId = dbId;
}
public long getCtlId() {
return ctlId;
}
public long getDbId() {
return dbId;
}
@Override
public String toString() {
return "EnvInfo{"
+ "ctlId='" + ctlId + '\''
+ ", dbId='" + dbId + '\''
+ '}';
}
}

View File

@ -19,10 +19,12 @@ package org.apache.doris.mtmv;
import org.apache.doris.analysis.StatementBase;
import org.apache.doris.analysis.UserIdentity;
import org.apache.doris.catalog.DatabaseIf;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.MTMV;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.datasource.CatalogIf;
import org.apache.doris.mysql.privilege.Auth;
import org.apache.doris.nereids.NereidsPlanner;
import org.apache.doris.nereids.StatementContext;
@ -66,9 +68,35 @@ public class MTMVPlanUtil {
if (workloadGroup.isPresent()) {
ctx.getSessionVariable().setWorkloadGroup(workloadGroup.get());
}
ctx.setStartTime();
// Set db&catalog to be used when creating materialized views to avoid SQL statements not writing the full path
// After https://github.com/apache/doris/pull/36543,
// After 1, this logic is no longer needed. This is to be compatible with older versions
setCatalogAndDb(ctx, mtmv);
return ctx;
}
private static void setCatalogAndDb(ConnectContext ctx, MTMV mtmv) {
EnvInfo envInfo = mtmv.getEnvInfo();
if (envInfo == null) {
return;
}
// switch catalog;
CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalog(envInfo.getCtlId());
// if catalog not exist, it may not have any impact, so there is no error and it will be returned directly
if (catalog == null) {
return;
}
ctx.changeDefaultCatalog(catalog.getName());
// use db
Optional<? extends DatabaseIf<? extends TableIf>> databaseIf = catalog.getDb(envInfo.getDbId());
// if db not exist, it may not have any impact, so there is no error and it will be returned directly
if (!databaseIf.isPresent()) {
return;
}
ctx.setDatabase(databaseIf.get().getFullName());
}
public static MTMVRelation generateMTMVRelation(MTMV mtmv, ConnectContext ctx) {
// Should not make table without data to empty relation when analyze the related table,
// so add disable rules