This is an automated email from the ASF dual-hosted git repository. lingmiao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-doris.git
The following commit(s) were added to refs/heads/master by this push: new 1810f10 [Bug] Fix bug that failed to create view with complex select stmt (#4840) 1810f10 is described below commit 1810f104970f05fc1c767c38cf3c4ca2b3f06205 Author: Mingyu Chen <morningman....@gmail.com> AuthorDate: Thu Nov 12 10:04:00 2020 +0800 [Bug] Fix bug that failed to create view with complex select stmt (#4840) Fix bug that failed to create view with complex select stmt. Fix #4839 --- .../org/apache/doris/analysis/BaseViewStmt.java | 35 +++++------ .../org/apache/doris/analysis/CreateViewStmt.java | 3 +- .../java/org/apache/doris/analysis/SlotRef.java | 16 ++--- .../org/apache/doris/analysis/StatementBase.java | 1 + .../org/apache/doris/common/util/ToSqlContext.java | 70 ++++++++++++++++++++++ .../src/main/java/org/apache/doris/load/Load.java | 9 +++ .../java/org/apache/doris/qe/cache/CacheProxy.java | 12 ++-- .../org/apache/doris/qe/cache/PartitionCache.java | 4 +- 8 files changed, 109 insertions(+), 41 deletions(-) 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 d78d837..0a992a8 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.AnalysisException; 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<Column> finalCols; - protected String originalViewDef; protected String inlineViewDef; protected QueryStmt cloneStmt; @@ -111,33 +111,26 @@ public class BaseViewStmt extends DdlStmt { } // 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 "<slot 10> 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<String> 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 "<slot 10> 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 9567935..3951257 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 class CreateViewStmt extends BaseViewStmt { } @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 ad0606d..57c492f 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.Table; 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 class SlotRef extends Expr { 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("<slot " + Integer.toString(desc.getId().asInt()) + ">"); + if (ToSqlContext.get() == null || ToSqlContext.get().isNeedSlotRefId()) { + if (desc.getId().asInt() != 1) { + sb.append("<slot " + desc.getId().asInt() + ">"); + } } for (Expr expr : desc.getSourceExprs()) { sb.append(" "); @@ -185,7 +181,7 @@ public class SlotRef extends Expr { } return sb.toString(); } else { - return "<slot " + Integer.toString(desc.getId().asInt()) + ">" + sb.toString(); + return "<slot " + desc.getId().asInt() + ">" + 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 7f787bb..0ea0aa9 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 abstract class StatementBase implements ParseNode { * * @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 0000000..acae3ec --- /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<ToSqlContext> threadLocalInfo = new ThreadLocal<ToSqlContext>(); + + 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 79e4787..d3d1f1c 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 @@ public class Load { // 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 535a042..a750737 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.PUniqueId; 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 abstract class CacheProxy { public int value_count; public int row_count; public int data_size; - private String sqlStr; private List<CacheValue> 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 abstract class CacheProxy { public static class FetchCacheRequest extends PFetchCacheRequest { - private String sqlStr; private List<CacheParam> 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 d88f6ac..068c93c 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.common.util.DebugUtil; 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; --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org