This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 69d3878d9b [Bug](CTAS): Ctas rollback ignore some case (#16255)
69d3878d9b is described below

commit 69d3878d9bd229e3fc453ac9968c35c3a46667bc
Author: Stalary <[email protected]>
AuthorDate: Tue Feb 14 09:19:37 2023 +0800

    [Bug](CTAS): Ctas rollback ignore some case (#16255)
    
    Currently, some error are caught due to table can not drop when execute 
ctas,
    I add a session variable to control drop or not table.
---
 docs/en/docs/advanced/variables.md                 |  4 +++
 docs/zh-CN/docs/advanced/variables.md              |  6 ++++-
 .../java/org/apache/doris/qe/SessionVariable.java  |  9 +++++++
 .../java/org/apache/doris/qe/StmtExecutor.java     | 29 ++++++++++++++--------
 4 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/docs/en/docs/advanced/variables.md 
b/docs/en/docs/advanced/variables.md
index cc434c6ff6..59e8bf1fac 100644
--- a/docs/en/docs/advanced/variables.md
+++ b/docs/en/docs/advanced/variables.md
@@ -580,3 +580,7 @@ Translated with www.DeepL.com/Translator (free version)
 * `topn_opt_limit_threshold`
 
     Set threshold for limit of topn query (eg. SELECT * FROM t ORDER BY k 
LIMIT n). If n <= threshold, topn optimizations(runtime predicate pushdown, two 
phase result fetch and read order by key) will enable automatically, otherwise 
disable. Default value is 1024.
+
+* `drop_table_if_ctas_failed`
+
+    Controls whether create table as select deletes created tables when a 
insert error occurs, the default value is true.
diff --git a/docs/zh-CN/docs/advanced/variables.md 
b/docs/zh-CN/docs/advanced/variables.md
index 6bd438af60..a660683362 100644
--- a/docs/zh-CN/docs/advanced/variables.md
+++ b/docs/zh-CN/docs/advanced/variables.md
@@ -558,7 +558,7 @@ SELECT /*+ SET_VAR(query_timeout = 1, 
enable_partition_cache=true) */ sleep(3);
 
 *   `group_by_and_having_use_alias_first`
 
-       指定group by和having语句是否优先使用列的别名,而非从From语句里寻找列的名字。默认为false。
+    指定group by和having语句是否优先使用列的别名,而非从From语句里寻找列的名字。默认为false。
 
 * `enable_file_cache`
 
@@ -567,3 +567,7 @@ SELECT /*+ SET_VAR(query_timeout = 1, 
enable_partition_cache=true) */ sleep(3);
 * `topn_opt_limit_threshold`
 
     设置topn优化的limit阈值 (例如:SELECT * FROM t ORDER BY k LIMIT n). 
如果limit的n小于等于阈值,topn相关优化(动态过滤下推、两阶段获取结果、按key的顺序读数据)会自动启用,否则会禁用。默认值是1024。
+
+* `drop_table_if_ctas_failed`
+
+    控制create table as select在写入发生错误时是否删除已创建的表,默认为true。
diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java 
b/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
index 17d5273da2..e9f903ae9c 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
@@ -265,6 +265,7 @@ public class SessionVariable implements Serializable, 
Writable {
     public static final String ENABLE_FILE_CACHE = "enable_file_cache";
 
     public static final String GROUP_BY_AND_HAVING_USE_ALIAS_FIRST = 
"group_by_and_having_use_alias_first";
+    public static final String DROP_TABLE_IF_CTAS_FAILED = 
"drop_table_if_ctas_failed";
 
     // session origin value
     public Map<Field, String> sessionOriginValue = new HashMap<Field, 
String>();
@@ -698,6 +699,10 @@ public class SessionVariable implements Serializable, 
Writable {
     @VariableMgr.VarAttr(name = ENABLE_FILE_CACHE, needForward = true)
     public boolean enableFileCache = true;
 
+    // Whether drop table when create table as select insert data appear error.
+    @VariableMgr.VarAttr(name = DROP_TABLE_IF_CTAS_FAILED, needForward = true)
+    public boolean dropTableIfCtasFailed = true;
+
     // If this fe is in fuzzy mode, then will use initFuzzyModeVariables to 
generate some variables,
     // not the default value set in the code.
     public void initFuzzyModeVariables() {
@@ -1438,6 +1443,10 @@ public class SessionVariable implements Serializable, 
Writable {
         this.fragmentTransmissionCompressionCodec = codec;
     }
 
+    public boolean isDropTableIfCtasFailed() {
+        return dropTableIfCtasFailed;
+    }
+
     public void checkExternalSortBytesThreshold(String 
externalSortBytesThreshold) {
         long value = Long.valueOf(externalSortBytesThreshold);
         if (value > 0 && value < MIN_EXTERNAL_SORT_BYTES_THRESHOLD) {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java 
b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
index 30ba377155..4d33587fc5 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
@@ -52,6 +52,7 @@ import org.apache.doris.analysis.StatementBase;
 import org.apache.doris.analysis.StmtRewriter;
 import org.apache.doris.analysis.StringLiteral;
 import org.apache.doris.analysis.SwitchStmt;
+import org.apache.doris.analysis.TableName;
 import org.apache.doris.analysis.TransactionBeginStmt;
 import org.apache.doris.analysis.TransactionCommitStmt;
 import org.apache.doris.analysis.TransactionRollbackStmt;
@@ -1959,18 +1960,26 @@ public class StmtExecutor implements ProfileWriter {
             try {
                 parsedStmt = ctasStmt.getInsertStmt();
                 execute();
+                if 
(MysqlStateType.ERR.equals(context.getState().getStateType())) {
+                    LOG.warn("CTAS insert data error, stmt={}", 
ctasStmt.toSql());
+                    
handleCtasRollback(ctasStmt.getCreateTableStmt().getDbTbl());
+                }
             } catch (Exception e) {
-                e.printStackTrace();
                 LOG.warn("CTAS insert data error, stmt={}", ctasStmt.toSql(), 
e);
-                // insert error drop table
-                DropTableStmt dropTableStmt = new DropTableStmt(true, 
ctasStmt.getCreateTableStmt().getDbTbl(), true);
-                try {
-                    DdlExecutor.execute(context.getEnv(), dropTableStmt);
-                } catch (Exception ex) {
-                    LOG.warn("CTAS drop table error, stmt={}", 
parsedStmt.toSql(), ex);
-                    context.getState().setError(ErrorCode.ERR_UNKNOWN_ERROR,
-                            "Unexpected exception: " + ex.getMessage());
-                }
+                handleCtasRollback(ctasStmt.getCreateTableStmt().getDbTbl());
+            }
+        }
+    }
+
+    private void handleCtasRollback(TableName table) {
+        if (context.getSessionVariable().isDropTableIfCtasFailed()) {
+            // insert error drop table
+            DropTableStmt dropTableStmt = new DropTableStmt(true, table, true);
+            try {
+                DdlExecutor.execute(context.getEnv(), dropTableStmt);
+            } catch (Exception ex) {
+                LOG.warn("CTAS drop table error, stmt={}", parsedStmt.toSql(), 
ex);
+                context.getState().setError(ErrorCode.ERR_UNKNOWN_ERROR, 
"Unexpected exception: " + ex.getMessage());
             }
         }
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to