diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/BaseViewStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/BaseViewStmt.java index d78d8373f09443..0a992a83ce2d53 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/BaseViewStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/BaseViewStmt.java @@ -24,6 +24,7 @@ import org.apache.doris.common.ErrorCode; import org.apache.doris.common.ErrorReport; import org.apache.doris.common.UserException; +import org.apache.doris.common.util.ToSqlContext; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; @@ -46,7 +47,6 @@ public class BaseViewStmt extends DdlStmt { // Set during analyze protected final List finalCols; - protected String originalViewDef; protected String inlineViewDef; protected QueryStmt cloneStmt; @@ -111,33 +111,26 @@ protected void createColumnAndViewDefs(Analyzer analyzer) throws AnalysisExcepti } // format view def string - originalViewDef = viewDefStmt.toSql(); - if (cols == null) { - inlineViewDef = originalViewDef; + try (ToSqlContext toSqlContext = ToSqlContext.getOrNewThreadLocalContext()) { + // after being analyzed, the toSql() of SlotRef will output like " col as col", + // we don't need the slot id info, so using ToSqlContext to remove it. + toSqlContext.setNeedSlotRefId(false); + inlineViewDef = viewDefStmt.toSql(); + } return; } Analyzer tmpAnalyzer = new Analyzer(analyzer); List colNames = cols.stream().map(c -> c.getColName()).collect(Collectors.toList()); cloneStmt.substituteSelectList(tmpAnalyzer, colNames); - inlineViewDef = cloneStmt.toSql(); - -// StringBuilder sb = new StringBuilder(); -// sb.append("SELECT "); -// for (int i = 0; i < finalCols.size(); ++i) { -// if (i != 0) { -// sb.append(", "); -// } -// String colRef = viewDefStmt.getColLabels().get(i); -// if (!colRef.startsWith("`")) { -// colRef = "`" + colRef + "`"; -// } -// String colAlias = finalCols.get(i).getName(); -// sb.append(String.format("`%s`.%s AS `%s`", tableName.getTbl(), colRef, colAlias)); -// } -// sb.append(String.format(" FROM (%s) %s", originalViewDef, tableName.getTbl())); -// inlineViewDef = sb.toString(); + + try (ToSqlContext toSqlContext = ToSqlContext.getOrNewThreadLocalContext()) { + // after being analyzed, the toSql() of SlotRef will output like " col as col", + // we don't need the slot id info, so using ToSqlContext to remove it. + toSqlContext.setNeedSlotRefId(false); + inlineViewDef = cloneStmt.toSql(); + } } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateViewStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateViewStmt.java index 9567935e3ea4f9..395125756e65c0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateViewStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateViewStmt.java @@ -18,7 +18,6 @@ package org.apache.doris.analysis; import org.apache.doris.catalog.Catalog; -import org.apache.doris.common.AnalysisException; import org.apache.doris.common.ErrorCode; import org.apache.doris.common.ErrorReport; import org.apache.doris.common.UserException; @@ -54,7 +53,7 @@ public String getComment() { } @Override - public void analyze(Analyzer analyzer) throws AnalysisException, UserException { + public void analyze(Analyzer analyzer) throws UserException { tableName.analyze(analyzer); viewDefStmt.setNeedToSql(true); diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java index ad0606d8dc9a59..57c492f44e7a0d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java @@ -22,6 +22,7 @@ import org.apache.doris.catalog.Type; import org.apache.doris.common.AnalysisException; import org.apache.doris.common.io.Text; +import org.apache.doris.common.util.ToSqlContext; import org.apache.doris.thrift.TExprNode; import org.apache.doris.thrift.TExprNodeType; import org.apache.doris.thrift.TSlotRef; @@ -164,20 +165,15 @@ public String debugString() { public String toSqlImpl() { StringBuilder sb = new StringBuilder(); - // if (desc != null) { - // sb.append("[tupleId="); - // sb.append(desc.getParent().getId().asInt()); - // sb.append(",SlotId="); - // sb.append(desc.getId().asInt()); - // sb.append("]"); - // } if (tblName != null) { return tblName.toSql() + "." + label + sb.toString(); } else if (label != null) { return label + sb.toString(); } else if (desc.getSourceExprs() != null) { - if (desc.getId().asInt() != 1) { - sb.append(""); + if (ToSqlContext.get() == null || ToSqlContext.get().isNeedSlotRefId()) { + if (desc.getId().asInt() != 1) { + sb.append(""); + } } for (Expr expr : desc.getSourceExprs()) { sb.append(" "); @@ -185,7 +181,7 @@ public String toSqlImpl() { } return sb.toString(); } else { - return "" + sb.toString(); + return "" + sb.toString(); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/StatementBase.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/StatementBase.java index 7f787bba68f3a7..0ea0aa90a48154 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/StatementBase.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/StatementBase.java @@ -88,6 +88,7 @@ public void analyze(Analyzer analyzer) throws AnalysisException, UserException { * * @see org.apache.doris.parser.ParseNode#toSql() */ + @Override public String toSql() { return ""; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/common/util/ToSqlContext.java b/fe/fe-core/src/main/java/org/apache/doris/common/util/ToSqlContext.java new file mode 100644 index 00000000000000..acae3ec859f2b3 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/common/util/ToSqlContext.java @@ -0,0 +1,70 @@ +// 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.common.util; + +import java.io.Closeable; + +/* + * This class is used to control the behavior of certain toSql() methods. The usage is as follows: + * + * try (ToSqlContext toSqlContext =ToSqlContext.getOrNewThreadLocalContext()) { + * toSqlContext.setNeedSlotRefId(false); // set some property + * inlineViewDef = viewDefStmt.toSql(); // call some toSql() methods + * } + * + * This class implements "Closable" interface, and it should be closed right after being used. + * To prevent it from affecting other following logic. + * + */ +public class ToSqlContext implements Closeable { + + // Used to control whether to output the slotId in the toSql() method of SlotRef. + private boolean needSlotRefId; + + private static ThreadLocal threadLocalInfo = new ThreadLocal(); + + public ToSqlContext() { + + } + + public void setNeedSlotRefId(boolean needSlotRefId) { + this.needSlotRefId = needSlotRefId; + } + + public boolean isNeedSlotRefId() { + return needSlotRefId; + } + + public static ToSqlContext get() { + return threadLocalInfo.get(); + } + + public static ToSqlContext getOrNewThreadLocalContext() { + ToSqlContext toSqlContext = threadLocalInfo.get(); + if (toSqlContext == null) { + toSqlContext = new ToSqlContext(); + threadLocalInfo.set(toSqlContext); + } + return toSqlContext; + } + + @Override + public void close() { + threadLocalInfo.remove(); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/load/Load.java b/fe/fe-core/src/main/java/org/apache/doris/load/Load.java index 79e47877521774..d3d1f1cbf4e827 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/load/Load.java +++ b/fe/fe-core/src/main/java/org/apache/doris/load/Load.java @@ -3024,6 +3024,15 @@ private boolean processCancelled(LoadJob job, CancelType cancelType, String msg, // the transaction may already be aborted due to timeout by transaction manager. // just print a log and continue to cancel the job. LOG.info("transaction not found when try to abort it: {}", e.getTransactionId()); + } catch (AnalysisException e) { + // This is because the database has been dropped, so dbTransactionMgr can not be found. + // In this case, just continue to cancel the load. + if (!e.getMessage().contains("does not exist")) { + if (failedMsg != null) { + failedMsg.add("Abort transaction failed: " + e.getMessage()); + } + return false; + } } catch (Exception e) { LOG.info("errors while abort transaction", e); if (failedMsg != null) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/cache/CacheProxy.java b/fe/fe-core/src/main/java/org/apache/doris/qe/cache/CacheProxy.java index 535a0423fe961c..a750737469fc9d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/cache/CacheProxy.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/cache/CacheProxy.java @@ -17,7 +17,6 @@ package org.apache.doris.qe.cache; -import com.google.common.collect.Lists; import org.apache.doris.common.Status; import org.apache.doris.common.util.DebugUtil; import org.apache.doris.proto.PCacheParam; @@ -29,6 +28,9 @@ import org.apache.doris.proto.PUpdateCacheRequest; import org.apache.doris.qe.RowBatch; import org.apache.doris.thrift.TResultBatch; + +import com.google.common.collect.Lists; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -133,12 +135,10 @@ public static class UpdateCacheRequest extends PUpdateCacheRequest { public int value_count; public int row_count; public int data_size; - private String sqlStr; private List valueList; public UpdateCacheRequest(String sqlStr) { - this.sqlStr = sqlStr; - this.sql_key = getMd5(this.sqlStr); + this.sql_key = getMd5(sqlStr); this.valueList = Lists.newArrayList(); value_count = 0; row_count = 0; @@ -176,12 +176,10 @@ public void debug() { public static class FetchCacheRequest extends PFetchCacheRequest { - private String sqlStr; private List paramList; public FetchCacheRequest(String sqlStr) { - this.sqlStr = sqlStr; - this.sql_key = getMd5(this.sqlStr); + this.sql_key = getMd5(sqlStr); this.paramList = Lists.newArrayList(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/cache/PartitionCache.java b/fe/fe-core/src/main/java/org/apache/doris/qe/cache/PartitionCache.java index d88f6ac65ca7ff..068c93c7faef22 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/cache/PartitionCache.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/cache/PartitionCache.java @@ -17,7 +17,6 @@ package org.apache.doris.qe.cache; -import com.google.common.collect.Lists; import org.apache.doris.analysis.CompoundPredicate; import org.apache.doris.analysis.Expr; import org.apache.doris.analysis.InlineViewRef; @@ -32,6 +31,9 @@ import org.apache.doris.metric.MetricRepo; import org.apache.doris.qe.RowBatch; import org.apache.doris.thrift.TUniqueId; + +import com.google.common.collect.Lists; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;