drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From par...@apache.org
Subject [3/6] drill git commit: DRILL-4673: Implement "DROP TABLE IF EXISTS" for drill to prevent FAILED status on command return - implement DROP TABLE IF EXISTS and DROP VIEW IF EXISTS; - added unit test for DROP TABLE IF EXISTS; - added unit test for DROP VIE
Date Sat, 23 Jul 2016 04:58:43 GMT
DRILL-4673: Implement "DROP TABLE IF EXISTS" for drill to prevent FAILED status on command
return - implement DROP TABLE IF EXISTS and DROP VIEW IF EXISTS; - added unit test for DROP
TABLE IF EXISTS; - added unit test for DROP VIEW IF EXISTS; - added unit test for "IF" hive
UDF.

This closes #541


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

Branch: refs/heads/master
Commit: f36fec9750fedd00a063f26b9998f9a994d025ad
Parents: 5ca2340
Author: Vitalii Diravka <vitalii.diravka@gmail.com>
Authored: Fri May 13 19:24:31 2016 +0000
Committer: Parth Chandra <parthc@apache.org>
Committed: Fri Jul 22 21:28:08 2016 -0700

----------------------------------------------------------------------
 .../drill/exec/fn/hive/TestInbuiltHiveUDFs.java | 12 ++++-
 exec/java-exec/src/main/codegen/data/Parser.tdd |  3 +-
 .../src/main/codegen/includes/parserImpls.ftl   | 16 +++---
 .../planner/sql/handlers/DropTableHandler.java  | 22 +++++++--
 .../exec/planner/sql/handlers/ViewHandler.java  | 33 ++++++++-----
 .../sql/parser/CompoundIdentifierConverter.java |  4 +-
 .../exec/planner/sql/parser/SqlDropTable.java   | 27 +++++++++--
 .../exec/planner/sql/parser/SqlDropView.java    | 30 +++++++++---
 .../java/org/apache/drill/TestDropTable.java    | 51 ++++++++++++++++++++
 .../drill/exec/sql/TestBaseViewSupport.java     | 44 +++++++++++++++++
 .../apache/drill/exec/sql/TestViewSupport.java  | 31 ++++++++++++
 11 files changed, 233 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/fn/hive/TestInbuiltHiveUDFs.java
----------------------------------------------------------------------
diff --git a/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/fn/hive/TestInbuiltHiveUDFs.java
b/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/fn/hive/TestInbuiltHiveUDFs.java
index 93c9374..9e13844 100644
--- a/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/fn/hive/TestInbuiltHiveUDFs.java
+++ b/contrib/storage-hive/core/src/test/java/org/apache/drill/exec/fn/hive/TestInbuiltHiveUDFs.java
@@ -23,7 +23,6 @@ import org.apache.drill.TestBuilder;
 import org.apache.drill.common.expression.SchemaPath;
 import org.apache.drill.common.types.TypeProtos;
 import org.apache.drill.exec.hive.HiveTestBase;
-import org.apache.drill.exec.rpc.user.QueryDataBatch;
 import org.junit.Test;
 
 import java.util.List;
@@ -84,4 +83,15 @@ public class TestInbuiltHiveUDFs extends HiveTestBase {
             "first_name","Bh","last_name","Venkata","position","Store"))
         .go();
   }
+
+  @Test // DRILL-3272
+  public void testIf() throws Exception {
+    testBuilder()
+        .sqlQuery("select `if`(1999 > 2000, 'latest', 'old') Period from hive.kv limit
1")
+        .ordered()
+        .baselineColumns("Period")
+        .baselineValues("old")
+        .go();
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/exec/java-exec/src/main/codegen/data/Parser.tdd
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/codegen/data/Parser.tdd b/exec/java-exec/src/main/codegen/data/Parser.tdd
index 84b3dce..ce3ee4c 100644
--- a/exec/java-exec/src/main/codegen/data/Parser.tdd
+++ b/exec/java-exec/src/main/codegen/data/Parser.tdd
@@ -37,7 +37,8 @@
     "FILES",
     "REFRESH",
     "METADATA",
-    "DATABASE"
+    "DATABASE",
+    "IF"
   ]
 
   # List of methods for parsing custom SQL statements.

http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/exec/java-exec/src/main/codegen/includes/parserImpls.ftl
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/codegen/includes/parserImpls.ftl b/exec/java-exec/src/main/codegen/includes/parserImpls.ftl
index 3a9424b..9901098 100644
--- a/exec/java-exec/src/main/codegen/includes/parserImpls.ftl
+++ b/exec/java-exec/src/main/codegen/includes/parserImpls.ftl
@@ -196,18 +196,20 @@ SqlNode SqlCreateOrReplaceView() :
 }
 
 /**
- * Parses a drop view statement.
- * DROP VIEW view_name;
+ * Parses a drop view or drop view if exists statement.
+ * DROP VIEW [IF EXISTS] view_name;
  */
 SqlNode SqlDropView() :
 {
     SqlParserPos pos;
+    boolean viewExistenceCheck = false;
 }
 {
     <DROP> { pos = getPos(); }
     <VIEW>
+    [ <IF> <EXISTS> { viewExistenceCheck = true; } ]
     {
-        return new SqlDropView(pos, CompoundIdentifier());
+        return new SqlDropView(pos, CompoundIdentifier(), viewExistenceCheck);
     }
 }
 
@@ -242,18 +244,20 @@ SqlNode SqlCreateTable() :
 }
 
 /**
- * Parses a drop table statement.
- * DROP TABLE table_name;
+ * Parses a drop table or drop table if exists statement.
+ * DROP TABLE [IF EXISTS] table_name;
  */
 SqlNode SqlDropTable() :
 {
     SqlParserPos pos;
+    boolean tableExistenceCheck = false;
 }
 {
     <DROP> { pos = getPos(); }
     <TABLE>
+    [ <IF> <EXISTS> { tableExistenceCheck = true; } ]
     {
-        return new SqlDropTable(pos, CompoundIdentifier());
+        return new SqlDropTable(pos, CompoundIdentifier(), tableExistenceCheck);
     }
 }
 

http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DropTableHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DropTableHandler.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DropTableHandler.java
index 7684cb3..517c183 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DropTableHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DropTableHandler.java
@@ -19,7 +19,9 @@ package org.apache.drill.exec.planner.sql.handlers;
 
 import java.io.IOException;
 
+import org.apache.calcite.schema.Schema;
 import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.Table;
 import org.apache.calcite.sql.SqlIdentifier;
 import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.tools.RelConversionException;
@@ -41,10 +43,12 @@ public class DropTableHandler extends DefaultSqlHandler {
   }
 
   /**
-   * Function resolves the schema and invokes the drop method. Raises an exception if the
schema is
-   * immutable.
-   * @param sqlNode - Table name identifier
-   * @return - Single row indicating drop succeeded, raise exception otherwise
+   * Function resolves the schema and invokes the drop method
+   * (while IF EXISTS statement is used function invokes the drop method only if table exists).
+   * Raises an exception if the schema is immutable.
+   * @param sqlNode - SqlDropTable (SQL parse tree of drop table [if exists] query)
+   * @return - Single row indicating drop succeeded or table is not found while IF EXISTS
statement is used,
+   * raise exception otherwise
    * @throws ValidationException
    * @throws RelConversionException
    * @throws IOException
@@ -62,13 +66,21 @@ public class DropTableHandler extends DefaultSqlHandler {
       drillSchema = SchemaUtilites.resolveToMutableDrillSchema(defaultSchema, dropTableNode.getSchema());
     }
 
-    String tableName = ((SqlDropTable) sqlNode).getName();
+    String tableName = dropTableNode.getName();
     if (drillSchema == null) {
       throw UserException.validationError()
           .message("Invalid table_name [%s]", tableName)
           .build(logger);
     }
 
+    if (dropTableNode.checkTableExistence()) {
+      final Table tableToDrop = SqlHandlerUtil.getTableFromSchema(drillSchema, tableName);
+      if (tableToDrop == null || tableToDrop.getJdbcTableType() != Schema.TableType.TABLE)
{
+        return DirectPlan.createDirectPlan(context, true,
+            String.format("Table [%s] not found", tableName));
+      }
+    }
+
     drillSchema.dropTable(tableName);
 
     return DirectPlan.createDirectPlan(context, true,

http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
index 6024fa5..b8396e6 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
@@ -106,7 +106,7 @@ public abstract class ViewHandler extends DefaultSqlHandler {
     }
   }
 
-  /** Handler for Drop View DDL command. */
+  /** Handler for Drop View [If Exists] DDL command. */
   public static class DropView extends ViewHandler {
     public DropView(SqlHandlerConfig config) {
       super(config);
@@ -115,27 +115,34 @@ public abstract class ViewHandler extends DefaultSqlHandler {
     @Override
     public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException,
IOException, ForemanSetupException {
       SqlDropView dropView = unwrap(sqlNode, SqlDropView.class);
-      final String viewToDrop = dropView.getName();
+      final String viewName = dropView.getName();
       final AbstractSchema drillSchema =
           SchemaUtilites.resolveToMutableDrillSchema(context.getNewDefaultSchema(), dropView.getSchemaPath());
 
       final String schemaPath = drillSchema.getFullSchemaName();
 
-      final Table existingTable = SqlHandlerUtil.getTableFromSchema(drillSchema, viewToDrop);
-      if (existingTable != null && existingTable.getJdbcTableType() != Schema.TableType.VIEW)
{
-        throw UserException.validationError()
-            .message("[%s] is not a VIEW in schema [%s]", viewToDrop, schemaPath)
-            .build(logger);
-      } else if (existingTable == null) {
-        throw UserException.validationError()
-            .message("Unknown view [%s] in schema [%s].", viewToDrop, schemaPath)
-            .build(logger);
+      final Table viewToDrop = SqlHandlerUtil.getTableFromSchema(drillSchema, viewName);
+      if (dropView.checkViewExistence()) {
+        if (viewToDrop == null || viewToDrop.getJdbcTableType() != Schema.TableType.VIEW){
+          return DirectPlan.createDirectPlan(context, true,
+              String.format("View [%s] not found in schema [%s].", viewName, schemaPath));
+        }
+      } else {
+        if (viewToDrop != null && viewToDrop.getJdbcTableType() != Schema.TableType.VIEW)
{
+          throw UserException.validationError()
+              .message("[%s] is not a VIEW in schema [%s]", viewName, schemaPath)
+              .build(logger);
+        } else if (viewToDrop == null) {
+          throw UserException.validationError()
+              .message("Unknown view [%s] in schema [%s].", viewName, schemaPath)
+              .build(logger);
+        }
       }
 
-      drillSchema.dropView(viewToDrop);
+      drillSchema.dropView(viewName);
 
       return DirectPlan.createDirectPlan(context, true,
-          String.format("View [%s] deleted successfully from schema [%s].", viewToDrop, schemaPath));
+          String.format("View [%s] deleted successfully from schema [%s].", viewName, schemaPath));
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/CompoundIdentifierConverter.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/CompoundIdentifierConverter.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/CompoundIdentifierConverter.java
index a125628..fa0d319 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/CompoundIdentifierConverter.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/CompoundIdentifierConverter.java
@@ -164,13 +164,13 @@ public class CompoundIdentifierConverter extends SqlShuttle {
     rules.put(SqlCreateTable.class, R(D, D, D, E));
     rules.put(SqlCreateView.class, R(D, E, E, D));
     rules.put(SqlDescribeTable.class, R(D, D, E));
-    rules.put(SqlDropView.class, R(D));
+    rules.put(SqlDropView.class, R(D, D));
     rules.put(SqlShowFiles.class, R(D));
     rules.put(SqlShowSchemas.class, R(D, D));
     rules.put(SqlUseSchema.class, R(D));
     rules.put(SqlJoin.class, R(D, D, D, D, D, E));
     rules.put(SqlOrderBy.class, R(D, E, D, D));
-    rules.put(SqlDropTable.class, R(D));
+    rules.put(SqlDropTable.class, R(D, D));
     rules.put(SqlRefreshMetadata.class, R(D));
     rules.put(SqlSetOption.class, R(D, D, D));
     rules.put(SqlDescribeSchema.class, R(D));

http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlDropTable.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlDropTable.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlDropTable.java
index de272a1..c5e9ce3 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlDropTable.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlDropTable.java
@@ -17,9 +17,9 @@
  */
 package org.apache.drill.exec.planner.sql.parser;
 
-import java.util.Collections;
 import java.util.List;
 
+import com.google.common.collect.Lists;
 import org.apache.drill.exec.planner.sql.handlers.AbstractSqlHandler;
 import org.apache.drill.exec.planner.sql.handlers.DropTableHandler;
 import org.apache.drill.exec.planner.sql.handlers.SqlHandlerConfig;
@@ -39,15 +39,21 @@ public class SqlDropTable extends DrillSqlCall {
   public static final SqlSpecialOperator OPERATOR = new SqlSpecialOperator("DROP_TABLE",
SqlKind.OTHER) {
     @Override
     public SqlCall createCall(SqlLiteral functionQualifier, SqlParserPos pos, SqlNode...
operands) {
-      return new SqlDropTable(pos, (SqlIdentifier) operands[0]);
+      return new SqlDropTable(pos, (SqlIdentifier) operands[0], (SqlLiteral) operands[1]);
     }
   };
 
   private SqlIdentifier tableName;
+  private boolean tableExistenceCheck;
 
-  public SqlDropTable(SqlParserPos pos, SqlIdentifier tableName) {
+  public SqlDropTable(SqlParserPos pos, SqlIdentifier tableName, SqlLiteral tableExistenceCheck)
{
+    this(pos, tableName, tableExistenceCheck.booleanValue());
+  }
+
+  public SqlDropTable(SqlParserPos pos, SqlIdentifier tableName, boolean tableExistenceCheck)
{
     super(pos);
     this.tableName = tableName;
+    this.tableExistenceCheck = tableExistenceCheck;
   }
 
   @Override
@@ -57,13 +63,22 @@ public class SqlDropTable extends DrillSqlCall {
 
   @Override
   public List<SqlNode> getOperandList() {
-    return Collections.singletonList((SqlNode) tableName);
+    final List<SqlNode> ops =
+        ImmutableList.of(
+            tableName,
+            SqlLiteral.createBoolean(tableExistenceCheck, SqlParserPos.ZERO)
+        );
+    return ops;
   }
 
   @Override
   public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
     writer.keyword("DROP");
     writer.keyword("TABLE");
+    if (tableExistenceCheck) {
+      writer.keyword("IF");
+      writer.keyword("EXISTS");
+    }
     tableName.unparse(writer, leftPrec, rightPrec);
   }
 
@@ -92,4 +107,8 @@ public class SqlDropTable extends DrillSqlCall {
     return tableName;
   }
 
+  public boolean checkTableExistence() {
+    return tableExistenceCheck;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlDropView.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlDropView.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlDropView.java
index 0e775b8..6fa6dbe 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlDropView.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlDropView.java
@@ -17,16 +17,11 @@
  */
 package org.apache.drill.exec.planner.sql.parser;
 
-import java.util.Collections;
 import java.util.List;
 
-import org.apache.calcite.tools.Planner;
-
-import org.apache.drill.exec.ops.QueryContext;
 import org.apache.drill.exec.planner.sql.handlers.AbstractSqlHandler;
 import org.apache.drill.exec.planner.sql.handlers.SqlHandlerConfig;
 import org.apache.drill.exec.planner.sql.handlers.ViewHandler.DropView;
-import org.apache.calcite.plan.hep.HepPlanner;
 import org.apache.calcite.sql.SqlCall;
 import org.apache.calcite.sql.SqlIdentifier;
 import org.apache.calcite.sql.SqlKind;
@@ -43,15 +38,21 @@ public class SqlDropView extends DrillSqlCall {
   public static final SqlSpecialOperator OPERATOR = new SqlSpecialOperator("DROP_VIEW", SqlKind.OTHER)
{
     @Override
     public SqlCall createCall(SqlLiteral functionQualifier, SqlParserPos pos, SqlNode...
operands) {
-      return new SqlDropView(pos, (SqlIdentifier) operands[0]);
+      return new SqlDropView(pos, (SqlIdentifier) operands[0], (SqlLiteral) operands[1]);
     }
   };
 
   private SqlIdentifier viewName;
+  private boolean viewExistenceCheck;
+
+  public SqlDropView(SqlParserPos pos, SqlIdentifier viewName, SqlLiteral viewExistenceCheck)
{
+    this(pos, viewName, viewExistenceCheck.booleanValue());
+  }
 
-  public SqlDropView(SqlParserPos pos, SqlIdentifier viewName) {
+  public SqlDropView(SqlParserPos pos, SqlIdentifier viewName, boolean viewExistenceCheck)
{
     super(pos);
     this.viewName = viewName;
+    this.viewExistenceCheck = viewExistenceCheck;
   }
 
   @Override
@@ -61,13 +62,22 @@ public class SqlDropView extends DrillSqlCall {
 
   @Override
   public List<SqlNode> getOperandList() {
-    return Collections.singletonList((SqlNode)viewName);
+    final List<SqlNode> ops =
+        ImmutableList.of(
+            viewName,
+            SqlLiteral.createBoolean(viewExistenceCheck, SqlParserPos.ZERO)
+        );
+    return ops;
   }
 
   @Override
   public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
     writer.keyword("DROP");
     writer.keyword("VIEW");
+    if (viewExistenceCheck) {
+      writer.keyword("IF");
+      writer.keyword("EXISTS");
+    }
     viewName.unparse(writer, leftPrec, rightPrec);
   }
 
@@ -92,4 +102,8 @@ public class SqlDropView extends DrillSqlCall {
     return viewName.names.get(viewName.names.size() - 1);
   }
 
+  public boolean checkViewExistence() {
+    return viewExistenceCheck;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/exec/java-exec/src/test/java/org/apache/drill/TestDropTable.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/TestDropTable.java b/exec/java-exec/src/test/java/org/apache/drill/TestDropTable.java
index 4f8fe1a..e9a38b0 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/TestDropTable.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/TestDropTable.java
@@ -26,7 +26,10 @@ import org.junit.Assert;
 public class TestDropTable extends PlanTestBase {
 
   private static final String CREATE_SIMPLE_TABLE = "create table %s as select 1 from cp.`employee.json`";
+  private static final String CREATE_SIMPLE_VIEW = "create view %s as select 1 from cp.`employee.json`";
   private static final String DROP_TABLE = "drop table %s";
+  private static final String DROP_TABLE_IF_EXISTS = "drop table if exists %s";
+  private static final String DROP_VIEW_IF_EXISTS = "drop view if exists %s";
   private static final String BACK_TICK = "`";
 
   @Test
@@ -171,4 +174,52 @@ public class TestDropTable extends PlanTestBase {
 
     Assert.assertTrue("Dropping table on immutable schema failed", dropFailed);
   }
+
+  @Test // DRILL-4673
+  public void testDropTableIfExistsWhileTableExists() throws Exception {
+    final String existentTableName = "test_table";
+    test("use dfs_test.tmp");
+
+    // successful dropping of existent table
+    test(String.format(CREATE_SIMPLE_TABLE, existentTableName));
+    testBuilder()
+        .sqlQuery(String.format(DROP_TABLE_IF_EXISTS, existentTableName))
+        .unOrdered()
+        .baselineColumns("ok", "summary")
+        .baselineValues(true, String.format("Table [%s] dropped", existentTableName))
+        .go();
+  }
+
+  @Test // DRILL-4673
+  public void testDropTableIfExistsWhileTableDoesNotExist() throws Exception {
+    final String nonExistentTableName = "test_table";
+    test("use dfs_test.tmp");
+
+    // dropping of non existent table without error
+    testBuilder()
+        .sqlQuery(String.format(DROP_TABLE_IF_EXISTS, nonExistentTableName))
+        .unOrdered()
+        .baselineColumns("ok", "summary")
+        .baselineValues(true, String.format("Table [%s] not found", nonExistentTableName))
+        .go();
+  }
+
+  @Test // DRILL-4673
+  public void testDropTableIfExistsWhileItIsAView() throws Exception {
+    final String viewName = "test_view";
+    try{
+      test("use dfs_test.tmp");
+
+      // dropping of non existent table without error if the view with such name is existed
+      test(String.format(CREATE_SIMPLE_VIEW, viewName));
+      testBuilder()
+          .sqlQuery(String.format(DROP_TABLE_IF_EXISTS, viewName))
+          .unOrdered()
+          .baselineColumns("ok", "summary")
+          .baselineValues(true, String.format("Table [%s] not found", viewName))
+          .go();
+    } finally {
+      test(String.format(DROP_VIEW_IF_EXISTS, viewName));
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestBaseViewSupport.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestBaseViewSupport.java
b/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestBaseViewSupport.java
index a8f5bbb..43d8d57 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestBaseViewSupport.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestBaseViewSupport.java
@@ -105,6 +105,50 @@ public class TestBaseViewSupport extends BaseTestQuery {
   }
 
   /**
+   * Drop view if exists with given parameters.
+   *
+   * Current schema "dfs_test"
+   * DROP VIEW IF EXISTS tmp.viewName
+   *
+   * For the above DROP VIEW IF EXISTS query, function parameters values are:
+   *  viewSchema = "tmp"
+   *  "viewName" = "viewName"
+   *  "finalSchema" = "dfs_test.tmp"
+   *  "ifViewExists" = null
+   *
+   * @param viewSchema
+   * @param viewName
+   * @param finalSchema
+   * @param ifViewExists Helps to check query result depending from the existing of the view.
+   * @throws Exception
+   */
+  protected static void dropViewIfExistsHelper(final String viewSchema, final String viewName,
final String finalSchema, Boolean ifViewExists) throws
+      Exception{
+    String viewFullName = "`" + viewName + "`";
+    if (!Strings.isNullOrEmpty(viewSchema)) {
+      viewFullName = viewSchema + "." + viewFullName;
+    }
+    if (ifViewExists == null) {
+      // ifViewExists == null: we do not know whether the table exists. Just drop it if exists
or skip dropping if doesn't exist
+      test(String.format("DROP VIEW IF EXISTS %s", viewFullName));
+    } else if (ifViewExists) {
+      testBuilder()
+          .sqlQuery(String.format("DROP VIEW IF EXISTS %s", viewFullName))
+          .unOrdered()
+          .baselineColumns("ok", "summary")
+          .baselineValues(true, String.format("View [%s] deleted successfully from schema
[%s].", viewName, finalSchema))
+          .go();
+    } else {
+      testBuilder()
+          .sqlQuery(String.format("DROP VIEW IF EXISTS %s", viewFullName))
+          .unOrdered()
+          .baselineColumns("ok", "summary")
+          .baselineValues(true, String.format("View [%s] not found in schema [%s].", viewName,
finalSchema))
+          .go();
+    }
+  }
+
+  /**
    * Execute the given query and check against the given baseline.
    *
    * @param query

http://git-wip-us.apache.org/repos/asf/drill/blob/f36fec97/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java b/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java
index 955da48..92f5c91 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java
@@ -591,4 +591,35 @@ public class TestViewSupport extends TestBaseViewSupport {
       FileUtils.deleteQuietly(tblPath);
     }
   }
+
+  @Test // DRILL-4673
+  public void dropViewIfExistsWhenViewExists() throws Exception {
+    final String existentViewName = generateViewName();
+
+    // successful dropping of existent view
+    createViewHelper(TEMP_SCHEMA, existentViewName, TEMP_SCHEMA, null,
+        "SELECT c_custkey, c_nationkey from cp.`tpch/customer.parquet`");
+    dropViewIfExistsHelper(TEMP_SCHEMA, existentViewName, TEMP_SCHEMA, true);
+  }
+
+  @Test // DRILL-4673
+  public void dropViewIfExistsWhenViewDoesNotExist() throws Exception {
+    final String nonExistentViewName = generateViewName();
+
+    // dropping of non existent view without error
+    dropViewIfExistsHelper(TEMP_SCHEMA, nonExistentViewName, TEMP_SCHEMA, false);
+  }
+
+  @Test // DRILL-4673
+  public void dropViewIfExistsWhenItIsATable() throws Exception {
+    final String tableName = "table_name";
+    try{
+      // dropping of non existent view without error if the table with such name is existed
+      test(String.format("CREATE TABLE %s.%s as SELECT region_id, sales_city FROM cp.`region.json`",
+          TEMP_SCHEMA, tableName));
+      dropViewIfExistsHelper(TEMP_SCHEMA, tableName, TEMP_SCHEMA, false);
+    } finally {
+      test(String.format("DROP TABLE IF EXISTS %s.%s ", TEMP_SCHEMA, tableName));
+    }
+  }
 }


Mime
View raw message