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

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


The following commit(s) were added to refs/heads/master by this push:
     new 379e4910547 [FLINK-32232][table] Supports parse truncate table 
statement (#22691)
379e4910547 is described below

commit 379e4910547cbbcf79dcbb251aa4b3c1cf80c6a0
Author: yuxia Luo <[email protected]>
AuthorDate: Thu Jun 8 16:30:14 2023 +0800

    [FLINK-32232][table] Supports parse truncate table statement (#22691)
---
 .../src/main/codegen/data/Parser.tdd               |  2 +
 .../src/main/codegen/includes/parserImpls.ftl      | 15 +++++
 .../flink/sql/parser/dml/SqlTruncateTable.java     | 71 ++++++++++++++++++++++
 .../flink/sql/parser/FlinkSqlParserImplTest.java   |  5 ++
 4 files changed, 93 insertions(+)

diff --git a/flink-table/flink-sql-parser/src/main/codegen/data/Parser.tdd 
b/flink-table/flink-sql-parser/src/main/codegen/data/Parser.tdd
index 6715f858666..27289112a3e 100644
--- a/flink-table/flink-sql-parser/src/main/codegen/data/Parser.tdd
+++ b/flink-table/flink-sql-parser/src/main/codegen/data/Parser.tdd
@@ -81,6 +81,7 @@
     "org.apache.flink.sql.parser.dml.RichSqlInsert"
     "org.apache.flink.sql.parser.dml.RichSqlInsertKeyword"
     "org.apache.flink.sql.parser.dml.SqlBeginStatementSet"
+    "org.apache.flink.sql.parser.dml.SqlTruncateTable"
     "org.apache.flink.sql.parser.dml.SqlCompileAndExecutePlan"
     "org.apache.flink.sql.parser.dml.SqlEndStatementSet"
     "org.apache.flink.sql.parser.dml.SqlExecute"
@@ -575,6 +576,7 @@
     "SqlAnalyzeTable()"
     "SqlStopJob()"
     "SqlShowJobs()"
+    "SqlTruncateTable()"
   ]
 
   # List of methods for parsing custom literals.
diff --git 
a/flink-table/flink-sql-parser/src/main/codegen/includes/parserImpls.ftl 
b/flink-table/flink-sql-parser/src/main/codegen/includes/parserImpls.ftl
index a7954f6182d..60cb2e038c9 100644
--- a/flink-table/flink-sql-parser/src/main/codegen/includes/parserImpls.ftl
+++ b/flink-table/flink-sql-parser/src/main/codegen/includes/parserImpls.ftl
@@ -2437,3 +2437,18 @@ SqlStopJob SqlStopJob() :
         return new SqlStopJob(getPos(), jobId, isWithSavepoint, isWithDrain);
     }
 }
+
+/**
+ * Parses a TRUNCATE TABLE statement.
+ */
+SqlTruncateTable SqlTruncateTable() :
+{
+    SqlIdentifier sqlIdentifier;
+}
+{
+    <TRUNCATE> <TABLE>
+    sqlIdentifier = CompoundIdentifier()
+    {
+        return new SqlTruncateTable(getPos(), sqlIdentifier);
+    }
+}
diff --git 
a/flink-table/flink-sql-parser/src/main/java/org/apache/flink/sql/parser/dml/SqlTruncateTable.java
 
b/flink-table/flink-sql-parser/src/main/java/org/apache/flink/sql/parser/dml/SqlTruncateTable.java
new file mode 100644
index 00000000000..6084f6cc1b6
--- /dev/null
+++ 
b/flink-table/flink-sql-parser/src/main/java/org/apache/flink/sql/parser/dml/SqlTruncateTable.java
@@ -0,0 +1,71 @@
+/*
+ * 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.flink.sql.parser.dml;
+
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * SqlNode to describe TRUNCATE TABLE statement.
+ *
+ * <p>We parse TRUNCATE TABLE statement in Flink since Calcite doesn't support 
TRUNCATE TABLE
+ * statement currently. Should remove the parse logic for TRUNCATE TABLE 
statement from Flink after
+ * the Calcite used by Flink includes [CALCITE-5688].
+ */
+public class SqlTruncateTable extends SqlCall {
+
+    private final SqlIdentifier tableNameIdentifier;
+
+    public static final SqlSpecialOperator OPERATOR =
+            new SqlSpecialOperator("TRUNCATE TABLE", SqlKind.OTHER);
+
+    public SqlTruncateTable(SqlParserPos pos, SqlIdentifier 
tableNameIdentifier) {
+        super(pos);
+        this.tableNameIdentifier = tableNameIdentifier;
+    }
+
+    @Override
+    public SqlOperator getOperator() {
+        return OPERATOR;
+    }
+
+    @Override
+    public List<SqlNode> getOperandList() {
+        return Collections.singletonList(tableNameIdentifier);
+    }
+
+    public String[] fullTableName() {
+        return tableNameIdentifier.names.toArray(new String[0]);
+    }
+
+    @Override
+    public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
+        writer.keyword("TRUNCATE TABLE");
+        tableNameIdentifier.unparse(writer, leftPrec, rightPrec);
+    }
+}
diff --git 
a/flink-table/flink-sql-parser/src/test/java/org/apache/flink/sql/parser/FlinkSqlParserImplTest.java
 
b/flink-table/flink-sql-parser/src/test/java/org/apache/flink/sql/parser/FlinkSqlParserImplTest.java
index 86901433b68..1074bc681e3 100644
--- 
a/flink-table/flink-sql-parser/src/test/java/org/apache/flink/sql/parser/FlinkSqlParserImplTest.java
+++ 
b/flink-table/flink-sql-parser/src/test/java/org/apache/flink/sql/parser/FlinkSqlParserImplTest.java
@@ -2415,6 +2415,11 @@ class FlinkSqlParserImplTest extends SqlParserTest {
                 .fails("WITH DRAIN could only be used after WITH SAVEPOINT.");
     }
 
+    @Test
+    void testTruncateTable() {
+        sql("truncate table t1").ok("TRUNCATE TABLE `T1`");
+    }
+
     public static BaseMatcher<SqlNode> validated(String validatedSql) {
         return new TypeSafeDiagnosingMatcher<SqlNode>() {
             @Override

Reply via email to