From 5453337e7bd3b68eab552e63379161f8e7b2c9d2 Mon Sep 17 00:00:00 2001 From: zhangdong <493738387@qq.com> Date: Thu, 24 Oct 2024 11:16:01 +0800 Subject: [PATCH] =?UTF-8?q?[fix](mtmv)Fix=20the=20old=20version=20of=20the?= =?UTF-8?q?=20materialized=20view=20error=20'Curren=E2=80=A6=20(#42307)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …t database is not set'` (#42152) pick: https://github.com/apache/doris/pull/42152 --- .../java/org/apache/doris/catalog/MTMV.java | 14 +++++ .../java/org/apache/doris/mtmv/EnvInfo.java | 52 +++++++++++++++++++ .../org/apache/doris/mtmv/MTMVPlanUtil.java | 28 ++++++++++ 3 files changed, 94 insertions(+) create mode 100644 fe/fe-core/src/main/java/org/apache/doris/mtmv/EnvInfo.java 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 fee3ec9b6f..d32853b112 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 @@ -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; diff --git a/fe/fe-core/src/main/java/org/apache/doris/mtmv/EnvInfo.java b/fe/fe-core/src/main/java/org/apache/doris/mtmv/EnvInfo.java new file mode 100644 index 0000000000..c00ae6e513 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/mtmv/EnvInfo.java @@ -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 + '\'' + + '}'; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPlanUtil.java b/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPlanUtil.java index 1e8df7b699..eb47999117 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPlanUtil.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPlanUtil.java @@ -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> 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