TAJO-860: Implements TRUNCATE table. (Hyoungjun Kim via hyunsik)

Project: http://git-wip-us.apache.org/repos/asf/tajo/repo
Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/13af1fa1
Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/13af1fa1
Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/13af1fa1

Branch: refs/heads/window_function
Commit: 13af1fa129ac13f6f04eab7ef4b44d605a9fcc49
Parents: 692ae66
Author: Hyunsik Choi <[email protected]>
Authored: Fri Jun 6 15:21:44 2014 -0700
Committer: Hyunsik Choi <[email protected]>
Committed: Fri Jun 6 15:22:40 2014 -0700

----------------------------------------------------------------------
 CHANGES                                         |   2 +
 .../java/org/apache/tajo/algebra/OpType.java    |   1 +
 .../org/apache/tajo/algebra/TruncateTable.java  |  51 +++++++++
 .../org/apache/tajo/engine/parser/SQLLexer.g4   |   1 +
 .../org/apache/tajo/engine/parser/SQLParser.g4  |   5 +
 .../apache/tajo/engine/parser/SQLAnalyzer.java  |  12 +++
 .../tajo/engine/planner/AlgebraVisitor.java     |   1 +
 .../tajo/engine/planner/BaseAlgebraVisitor.java |   6 ++
 .../engine/planner/BasicLogicalPlanVisitor.java |   9 ++
 .../engine/planner/LogicalPlanPreprocessor.java |   7 ++
 .../tajo/engine/planner/LogicalPlanVisitor.java |   3 +
 .../tajo/engine/planner/LogicalPlanner.java     |   8 ++
 .../apache/tajo/engine/planner/PlannerUtil.java |   3 +-
 .../tajo/engine/planner/logical/NodeType.java   |   3 +-
 .../planner/logical/TruncateTableNode.java      |  62 +++++++++++
 .../org/apache/tajo/master/GlobalEngine.java    |  55 ++++++++++
 .../tajo/engine/query/TestTruncateTable.java    | 104 +++++++++++++++++++
 .../truncate_table2/table2.tbl                  |   4 +
 .../queries/TestTruncateTable/table1_ddl.sql    |   1 +
 .../queries/TestTruncateTable/table2_ddl.sql    |   1 +
 20 files changed, 337 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 36345b2..82466fe 100644
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,8 @@ Release 0.9.0 - unreleased
 
   NEW FEATURES
 
+    TAJO-860: Implements TRUNCATE table. (Hyoungjun Kim via hyunsik)
+
     TAJO-849: Add Parquet storage to HCatalogStore. (jaehwa)
 
     TAJO-494: Extend TajoClient to run a query with a plan context serialized 

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-algebra/src/main/java/org/apache/tajo/algebra/OpType.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/OpType.java 
b/tajo-algebra/src/main/java/org/apache/tajo/algebra/OpType.java
index aeb28f0..0ba087a 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/OpType.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/OpType.java
@@ -49,6 +49,7 @@ public enum OpType {
   DropTable(DropTable.class),
   AlterTablespace(AlterTablespace.class),
   AlterTable(AlterTable.class),
+  TruncateTable(TruncateTable.class),
 
   // Insert or Update
   Insert(Insert.class),

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-algebra/src/main/java/org/apache/tajo/algebra/TruncateTable.java
----------------------------------------------------------------------
diff --git 
a/tajo-algebra/src/main/java/org/apache/tajo/algebra/TruncateTable.java 
b/tajo-algebra/src/main/java/org/apache/tajo/algebra/TruncateTable.java
new file mode 100644
index 0000000..ae6efa7
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/TruncateTable.java
@@ -0,0 +1,51 @@
+/**
+ * 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.tajo.algebra;
+
+import com.google.gson.annotations.Expose;
+import com.google.gson.annotations.SerializedName;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class TruncateTable extends Expr {
+  @Expose
+  @SerializedName("TableNames")
+  private List<String> tableNames;
+
+  public TruncateTable(final List<String> tableNames) {
+    super(OpType.TruncateTable);
+    this.tableNames = tableNames;
+  }
+
+  @Override
+  public int hashCode() {
+    return tableNames.hashCode();
+  }
+
+  public List<String> getTableNames() {
+    return tableNames;
+  }
+
+  @Override
+  boolean equalsTo(Expr expr) {
+    TruncateTable another = (TruncateTable) expr;
+    return Arrays.equals(tableNames.toArray(new String[]{}), 
another.tableNames.toArray(new String[]{}));
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 
b/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4
index e26f921..da5f355 100644
--- a/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4
+++ b/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4
@@ -287,6 +287,7 @@ TIMEZONE_HOUR: T I M E Z O N E UNDERLINE H O U R;
 TIMEZONE_MINUTE: T I M E Z O N E UNDERLINE M I N U T E;
 TRIM : T R I M;
 TO : T O;
+TRUNCATE : T R U N C A T E;
 
 UNKNOWN : U N K N O W N;
 

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 
b/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4
index 9a8f38e..c601d37 100644
--- a/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4
+++ b/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4
@@ -64,6 +64,7 @@ schema_statement
   | drop_table_statement
   | alter_tablespace_statement
   | alter_table_statement
+  | truncate_table_statement
   ;
 
 index_statement
@@ -186,6 +187,10 @@ partition_name
   : identifier
   ;
 
+truncate_table_statement
+  : TRUNCATE (TABLE)? table_name (COMMA table_name)*
+  ;
+
 /*
 ===============================================================================
   11.21 <data types>

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java 
b/tajo-core/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
index f0b5aa0..a638735 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
@@ -1054,6 +1054,18 @@ public class SQLAnalyzer extends 
SQLParserBaseVisitor<Expr> {
     return createTable;
   }
 
+  @Override
+  public Expr visitTruncate_table_statement(@NotNull 
SQLParser.Truncate_table_statementContext ctx) {
+    List<Table_nameContext> tableNameContexts = ctx.table_name();
+    List<String> tableNames = new ArrayList<String>();
+
+    for (Table_nameContext eachTableNameContext: tableNameContexts) {
+      tableNames.add(eachTableNameContext.getChild(0).getText());
+    }
+
+    return new TruncateTable(tableNames);
+  }
+
   private ColumnDefinition[] getDefinitions(SQLParser.Table_elementsContext 
ctx) {
     int size = ctx.field_element().size();
     ColumnDefinition[] elements = new ColumnDefinition[size];

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/engine/planner/AlgebraVisitor.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/AlgebraVisitor.java 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/AlgebraVisitor.java
index 0ef8a26..5811d36 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/planner/AlgebraVisitor.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/planner/AlgebraVisitor.java
@@ -48,6 +48,7 @@ public interface AlgebraVisitor<CONTEXT, RESULT> {
   RESULT visitDropTable(CONTEXT ctx, Stack<Expr> stack, DropTable expr) throws 
PlanningException;
   RESULT visitAlterTablespace(CONTEXT ctx, Stack<Expr> stack, AlterTablespace 
expr) throws PlanningException;
   RESULT visitAlterTable(CONTEXT ctx, Stack<Expr> stack, AlterTable expr) 
throws PlanningException;
+  RESULT visitTruncateTable(CONTEXT ctx, Stack<Expr> stack, TruncateTable 
expr) throws PlanningException;
 
     // Insert or Update
   RESULT visitInsert(CONTEXT ctx, Stack<Expr> stack, Insert expr) throws 
PlanningException;

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/engine/planner/BaseAlgebraVisitor.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/BaseAlgebraVisitor.java
 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/BaseAlgebraVisitor.java
index 841ea22..907042a 100644
--- 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/BaseAlgebraVisitor.java
+++ 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/BaseAlgebraVisitor.java
@@ -118,6 +118,9 @@ public class BaseAlgebraVisitor<CONTEXT, RESULT> implements 
AlgebraVisitor<CONTE
     case AlterTable:
       current = visitAlterTable(ctx, stack, (AlterTable) expr);
       break;
+    case TruncateTable:
+      current = visitTruncateTable(ctx, stack, (TruncateTable)expr);
+      break;
 
     case Insert:
       current = visitInsert(ctx, stack, (Insert) expr);
@@ -463,6 +466,9 @@ public class BaseAlgebraVisitor<CONTEXT, RESULT> implements 
AlgebraVisitor<CONTE
     return null;
   }
 
+  public RESULT visitTruncateTable(CONTEXT ctx, Stack<Expr> stack, 
TruncateTable expr) throws PlanningException {
+    return null;
+  }
   
///////////////////////////////////////////////////////////////////////////////////////////////////////////
   // Insert or Update Section
   
///////////////////////////////////////////////////////////////////////////////////////////////////////////

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/engine/planner/BasicLogicalPlanVisitor.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/BasicLogicalPlanVisitor.java
 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/BasicLogicalPlanVisitor.java
index 331b865..2112615 100644
--- 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/BasicLogicalPlanVisitor.java
+++ 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/BasicLogicalPlanVisitor.java
@@ -125,6 +125,9 @@ public class BasicLogicalPlanVisitor<CONTEXT, RESULT> 
implements LogicalPlanVisi
       case ALTER_TABLE:
         current = visitAlterTable(context, plan, block, (AlterTableNode) node, 
stack);
         break;
+      case TRUNCATE_TABLE:
+        current = visitTruncateTable(context, plan, block, (TruncateTableNode) 
node, stack);
+        break;
       default:
         throw new PlanningException("Unknown logical node type: " + 
node.getType());
     }
@@ -328,4 +331,10 @@ public class BasicLogicalPlanVisitor<CONTEXT, RESULT> 
implements LogicalPlanVisi
                                  Stack<LogicalNode> stack) {
         return null;
     }
+
+  @Override
+  public RESULT visitTruncateTable(CONTEXT context, LogicalPlan plan, 
LogicalPlan.QueryBlock block,
+                                   TruncateTableNode node, Stack<LogicalNode> 
stack) throws PlanningException {
+    return null;
+  }
 }

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanPreprocessor.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanPreprocessor.java
 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanPreprocessor.java
index a061b5a..2de96c4 100644
--- 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanPreprocessor.java
+++ 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanPreprocessor.java
@@ -431,6 +431,13 @@ class LogicalPlanPreprocessor extends 
BaseAlgebraVisitor<LogicalPlanPreprocessor
     return alterTableNode;
   }
 
+  @Override
+  public LogicalNode visitTruncateTable(PreprocessContext ctx, Stack<Expr> 
stack, TruncateTable expr)
+      throws PlanningException {
+    TruncateTableNode truncateTableNode = 
ctx.plan.createNode(TruncateTableNode.class);
+    return truncateTableNode;
+  }
+
   
///////////////////////////////////////////////////////////////////////////////////////////////////////////
   // Insert or Update Section
   
///////////////////////////////////////////////////////////////////////////////////////////////////////////

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanVisitor.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanVisitor.java
 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanVisitor.java
index 6850046..963e9f1 100644
--- 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanVisitor.java
+++ 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanVisitor.java
@@ -91,4 +91,7 @@ public interface LogicalPlanVisitor<CONTEXT, RESULT> {
 
   RESULT visitAlterTable(CONTEXT context, LogicalPlan plan, 
LogicalPlan.QueryBlock block, AlterTableNode node,
                          Stack<LogicalNode> stack) throws PlanningException;
+
+  RESULT visitTruncateTable(CONTEXT context, LogicalPlan plan, 
LogicalPlan.QueryBlock block, TruncateTableNode node,
+                         Stack<LogicalNode> stack) throws PlanningException;
 }

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanner.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanner.java 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanner.java
index b19efe1..6474444 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanner.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/planner/LogicalPlanner.java
@@ -1591,6 +1591,14 @@ public class LogicalPlanner extends 
BaseAlgebraVisitor<LogicalPlanner.PlanContex
     return alterTableNode;
   }
 
+  @Override
+  public LogicalNode visitTruncateTable(PlanContext context, Stack<Expr> 
stack, TruncateTable truncateTable)
+      throws PlanningException {
+    TruncateTableNode truncateTableNode = 
context.queryBlock.getNodeFromExpr(truncateTable);
+    truncateTableNode.setTableNames(truncateTable.getTableNames());
+    return truncateTableNode;
+  }
+
   
/*===============================================================================================
     Util SECTION
   
===============================================================================================*/

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/engine/planner/PlannerUtil.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/PlannerUtil.java 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/PlannerUtil.java
index a1ff0f0..4037df3 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/planner/PlannerUtil.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/planner/PlannerUtil.java
@@ -53,7 +53,8 @@ public class PlannerUtil {
             (type == NodeType.CREATE_TABLE && !((CreateTableNode) 
baseNode).hasSubQuery()) ||
             baseNode.getType() == NodeType.DROP_TABLE ||
             baseNode.getType() == NodeType.ALTER_TABLESPACE ||
-            baseNode.getType() == NodeType.ALTER_TABLE;
+            baseNode.getType() == NodeType.ALTER_TABLE ||
+            baseNode.getType() == NodeType.TRUNCATE_TABLE;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/engine/planner/logical/NodeType.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/logical/NodeType.java 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/logical/NodeType.java
index cc43912..de79f93 100644
--- 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/logical/NodeType.java
+++ 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/logical/NodeType.java
@@ -53,7 +53,8 @@ public enum NodeType {
   CREATE_TABLE(CreateTableNode.class),
   DROP_TABLE(DropTableNode.class),
   ALTER_TABLESPACE (AlterTablespaceNode.class),
-  ALTER_TABLE (AlterTableNode.class);
+  ALTER_TABLE (AlterTableNode.class),
+  TRUNCATE_TABLE (TruncateTableNode.class);
 
   private final Class<? extends LogicalNode> baseClass;
 

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/engine/planner/logical/TruncateTableNode.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/engine/planner/logical/TruncateTableNode.java
 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/logical/TruncateTableNode.java
new file mode 100644
index 0000000..b8d9cad
--- /dev/null
+++ 
b/tajo-core/src/main/java/org/apache/tajo/engine/planner/logical/TruncateTableNode.java
@@ -0,0 +1,62 @@
+/**
+ * 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.tajo.engine.planner.logical;
+
+import com.google.gson.annotations.Expose;
+import org.apache.tajo.engine.planner.PlanString;
+import org.apache.tajo.util.TUtil;
+
+import java.util.List;
+
+public class TruncateTableNode extends LogicalNode {
+  @Expose
+  private List<String> tableNames;
+
+  public TruncateTableNode(int pid) {
+    super(pid, NodeType.TRUNCATE_TABLE);
+  }
+
+  public List<String> getTableNames() {
+    return tableNames;
+  }
+
+  public void setTableNames(List<String> tableNames) {
+    this.tableNames = tableNames;
+  }
+
+  @Override
+  public String toString() {
+    return "TruncateTable (table=" + TUtil.collectionToString(tableNames) + 
")";
+  }
+
+  @Override
+  public void preOrder(LogicalNodeVisitor visitor) {
+    visitor.visit(this);
+  }
+
+  @Override
+  public void postOrder(LogicalNodeVisitor visitor) {
+    visitor.visit(this);
+  }
+
+  @Override
+  public PlanString getPlanString() {
+    return new PlanString(this);
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/main/java/org/apache/tajo/master/GlobalEngine.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/master/GlobalEngine.java 
b/tajo-core/src/main/java/org/apache/tajo/master/GlobalEngine.java
index 2b06a42..09e0e01 100644
--- a/tajo-core/src/main/java/org/apache/tajo/master/GlobalEngine.java
+++ b/tajo-core/src/main/java/org/apache/tajo/master/GlobalEngine.java
@@ -456,6 +456,10 @@ public class GlobalEngine extends AbstractService {
         AlterTableNode alterTable = (AlterTableNode) root;
         alterTable(session,alterTable);
         return true;
+      case TRUNCATE_TABLE:
+        TruncateTableNode truncateTable = (TruncateTableNode) root;
+        truncateTable(session, truncateTable);
+        return true;
       default:
         throw new InternalError("updateQuery cannot handle such query: \n" + 
root.toJson());
     }
@@ -592,6 +596,57 @@ public class GlobalEngine extends AbstractService {
     }
   }
 
+  /**
+   * Truncate table a given table
+   */
+  public void truncateTable(final Session session, final TruncateTableNode 
truncateTableNode) throws IOException {
+    List<String> tableNames = truncateTableNode.getTableNames();
+    final CatalogService catalog = context.getCatalog();
+
+    String databaseName;
+    String simpleTableName;
+
+    List<TableDesc> tableDescList = new ArrayList<TableDesc>();
+    for (String eachTableName: tableNames) {
+      if (CatalogUtil.isFQTableName(eachTableName)) {
+        String[] split = CatalogUtil.splitFQTableName(eachTableName);
+        databaseName = split[0];
+        simpleTableName = split[1];
+      } else {
+        databaseName = session.getCurrentDatabase();
+        simpleTableName = eachTableName;
+      }
+      final String qualifiedName = CatalogUtil.buildFQName(databaseName, 
simpleTableName);
+
+      if (!catalog.existsTable(databaseName, simpleTableName)) {
+        throw new NoSuchTableException(qualifiedName);
+      }
+
+      Path warehousePath = new 
Path(TajoConf.getWarehouseDir(context.getConf()), databaseName);
+      TableDesc tableDesc = catalog.getTableDesc(databaseName, 
simpleTableName);
+      Path tablePath = tableDesc.getPath();
+      if (tablePath.getParent() == null ||
+          
!tablePath.getParent().toUri().getPath().equals(warehousePath.toUri().getPath()))
 {
+        throw new IOException("Can't truncate external table:" + eachTableName 
+ ", data dir=" + tablePath +
+            ", warehouse dir=" + warehousePath);
+      }
+      tableDescList.add(tableDesc);
+    }
+
+    for (TableDesc eachTable: tableDescList) {
+      Path path = eachTable.getPath();
+      LOG.info("Truncate table: " + eachTable.getName() + ", delete all data 
files in " + path);
+      FileSystem fs = path.getFileSystem(context.getConf());
+
+      FileStatus[] files = fs.listStatus(path);
+      if (files != null) {
+        for (FileStatus eachFile: files) {
+          fs.delete(eachFile.getPath(), true);
+        }
+      }
+    }
+  }
+
   private boolean existColumnName(String tableName, String columnName) {
     final TableDesc tableDesc = catalog.getTableDesc(tableName);
     return tableDesc.getSchema().containsByName(columnName) ? true : false;

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/test/java/org/apache/tajo/engine/query/TestTruncateTable.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestTruncateTable.java 
b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestTruncateTable.java
new file mode 100644
index 0000000..455213b
--- /dev/null
+++ 
b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestTruncateTable.java
@@ -0,0 +1,104 @@
+/**
+ * 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.tajo.engine.query;
+
+import org.apache.tajo.IntegrationTest;
+import org.apache.tajo.QueryTestCaseBase;
+import org.apache.tajo.TajoConstants;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.sql.ResultSet;
+
+import static org.junit.Assert.assertEquals;
+
+@Category(IntegrationTest.class)
+public class TestTruncateTable extends QueryTestCaseBase {
+  public TestTruncateTable() {
+    super(TajoConstants.DEFAULT_DATABASE_NAME);
+  }
+  @Test
+  public final void testTruncateTable() throws Exception {
+    try {
+      ResultSet res = executeFile("table1_ddl.sql");
+      res.close();
+      assertTableExists("truncate_table1");
+
+      res = executeString("select * from truncate_table1");
+      int numRows = 0;
+      while (res.next()) {
+        numRows++;
+      }
+      assertEquals(5, numRows);
+      res.close();
+
+      executeString("truncate table truncate_table1");
+      assertTableExists("truncate_table1");
+
+      res = executeString("select * from truncate_table1");
+      numRows = 0;
+      while (res.next()) {
+        numRows++;
+      }
+      assertEquals(0, numRows);
+      res.close();
+    } finally {
+      executeString("DROP TABLE truncate_table1 PURGE");
+    }
+  }
+
+
+  /*
+  Currently TajoClient can't throw exception when plan error.
+  The following test cast should be uncommented after 
https://issues.apache.org/jira/browse/TAJO-762
+
+  @Test
+  public final void testTruncateExternalTable() throws Exception {
+    try {
+      List<String> createdNames = executeDDL("table2_ddl.sql", 
"truncate_table2", "truncate_table2");
+      assertTableExists(createdNames.get(0));
+
+      ResultSet res = executeString("select * from truncate_table2");
+      int numRows = 0;
+      while (res.next()) {
+        numRows++;
+      }
+      assertEquals(4, numRows);
+      res.close();
+
+      executeString("truncate table truncate_table2");
+
+      fail("Can't truncate external table");
+    } catch (Exception e) {
+      // succeeded
+      assertTableExists("truncate_table2");
+
+      ResultSet res = executeString("select * from truncate_table2");
+      int numRows = 0;
+      while (res.next()) {
+        numRows++;
+      }
+      assertEquals(4, numRows);
+      res.close();
+    } finally {
+      executeString("DROP TABLE truncate_table2 PURGE");
+    }
+  }
+  */
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/test/resources/dataset/TestTruncateTable/truncate_table2/table2.tbl
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/test/resources/dataset/TestTruncateTable/truncate_table2/table2.tbl
 
b/tajo-core/src/test/resources/dataset/TestTruncateTable/truncate_table2/table2.tbl
new file mode 100644
index 0000000..f99db9c
--- /dev/null
+++ 
b/tajo-core/src/test/resources/dataset/TestTruncateTable/truncate_table2/table2.tbl
@@ -0,0 +1,4 @@
+4|jkl1|7
+5|opq2|8
+6|stu3|9
+7|kkk4|10

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/test/resources/queries/TestTruncateTable/table1_ddl.sql
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/test/resources/queries/TestTruncateTable/table1_ddl.sql 
b/tajo-core/src/test/resources/queries/TestTruncateTable/table1_ddl.sql
new file mode 100644
index 0000000..dc67e75
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestTruncateTable/table1_ddl.sql
@@ -0,0 +1 @@
+CREATE TABLE truncate_table1 AS SELECT * FROM lineitem;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af1fa1/tajo-core/src/test/resources/queries/TestTruncateTable/table2_ddl.sql
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/test/resources/queries/TestTruncateTable/table2_ddl.sql 
b/tajo-core/src/test/resources/queries/TestTruncateTable/table2_ddl.sql
new file mode 100644
index 0000000..8d7fba0
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestTruncateTable/table2_ddl.sql
@@ -0,0 +1 @@
+CREATE EXTERNAL TABLE ${0} (id int, str text, num int) using csv location 
${table.path};

Reply via email to