aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java27
-rw-r--r--exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestBaseViewSupport.java2
-rw-r--r--exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java39
3 files changed, 59 insertions, 9 deletions
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 2428b45f0..c59c3a2f9 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
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.util.List;
import org.apache.calcite.schema.Schema;
+import org.apache.calcite.schema.Schema.TableType;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.schema.Table;
import org.apache.calcite.tools.Planner;
@@ -134,6 +135,7 @@ public abstract class ViewHandler extends AbstractSqlHandler {
@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
SqlDropView dropView = unwrap(sqlNode, SqlDropView.class);
+ final String viewToDrop = dropView.getName();
try {
SchemaPlus schema = findSchema(context.getRootSchema(), context.getNewDefaultSchema(), dropView.getSchemaPath());
@@ -141,20 +143,29 @@ public abstract class ViewHandler extends AbstractSqlHandler {
String schemaPath = drillSchema.getFullSchemaName();
if (!drillSchema.isMutable()) {
- return DirectPlan.createDirectPlan(context, false, String.format("Schema '%s' is not a mutable schema. " +
- "Views don't exist in this schema", schemaPath));
+ return DirectPlan.createDirectPlan(context, false, String.format("Schema [%s] is immutable.", schemaPath));
}
- if (drillSchema instanceof WorkspaceSchema) {
- ((WorkspaceSchema) drillSchema).dropView(dropView.getName());;
- } else {
- return DirectPlan.createDirectPlan(context, false, "Schema provided was not a workspace schema.");
+ if (!(drillSchema instanceof WorkspaceSchema)) {
+ return DirectPlan.createDirectPlan(context, false,
+ String.format("Schema [%s] doesn't support creating/dropping views.", schemaPath));
}
+ final Table existingTable = SqlHandlerUtil.getTableFromSchema(drillSchema, viewToDrop);
+ if (existingTable != null && existingTable.getJdbcTableType() != Schema.TableType.VIEW) {
+ return DirectPlan.createDirectPlan(context, false,
+ String.format("[%s] is not a VIEW in schema [%s]", viewToDrop, schemaPath));
+ } else if (existingTable == null) {
+ return DirectPlan.createDirectPlan(context, false,
+ String.format("Unknown view [%s] in schema [%s].", viewToDrop, schemaPath));
+ }
+
+ ((WorkspaceSchema) drillSchema).dropView(viewToDrop);
+
return DirectPlan.createDirectPlan(context, true,
- String.format("View '%s' deleted successfully from '%s' schema", dropView.getName(), schemaPath));
+ String.format("View [%s] deleted successfully from schema [%s].", viewToDrop, schemaPath));
} catch(Exception e) {
- logger.debug("Failed to delete view {}", dropView.getName(), e);
+ logger.debug("Failed to delete view {}", viewToDrop, e);
return DirectPlan.createDirectPlan(context, false, String.format("Error: %s", e.getMessage()));
}
}
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 bfe113bb3..a8f5bbb2f 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
@@ -100,7 +100,7 @@ public class TestBaseViewSupport extends BaseTestQuery {
.sqlQuery(String.format("DROP VIEW %s", viewFullName))
.unOrdered()
.baselineColumns("ok", "summary")
- .baselineValues(true, String.format("View '%s' deleted successfully from '%s' schema", viewName, finalSchema))
+ .baselineValues(true, String.format("View [%s] deleted successfully from schema [%s].", viewName, finalSchema))
.go();
}
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 5c2dc9070..0fc1f32ba 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
@@ -590,4 +590,43 @@ public class TestViewSupport extends TestBaseViewSupport {
.baselineValues(false, errorMsg)
.go();
}
+
+ @Test // DRILL-2423
+ public void showProperMsgWhenDroppingNonExistentView() throws Exception{
+ testBuilder()
+ .sqlQuery("DROP VIEW dfs_test.tmp.nonExistentView")
+ .unOrdered()
+ .baselineColumns("ok", "summary")
+ .baselineValues(false, "Unknown view [nonExistentView] in schema [dfs_test.tmp].")
+ .go();
+ }
+
+ @Test // DRILL-2423
+ public void showProperMsgWhenTryingToDropAViewInImmutableSchema() throws Exception{
+ testBuilder()
+ .sqlQuery("DROP VIEW cp.nonExistentView")
+ .unOrdered()
+ .baselineColumns("ok", "summary")
+ .baselineValues(false, "Schema [cp.default] is immutable.")
+ .go();
+ }
+
+ @Test // DRILL-2423
+ public void showProperMsgWhenTryingToDropANonViewTable() throws Exception{
+ final String testTableName = "testTableShowErrorMsg";
+ try {
+ test(String.format("CREATE TABLE %s.%s AS SELECT c_custkey, c_nationkey from cp.`tpch/customer.parquet`",
+ TEMP_SCHEMA, testTableName));
+
+ testBuilder()
+ .sqlQuery(String.format("DROP VIEW %s.%s", TEMP_SCHEMA, testTableName))
+ .unOrdered()
+ .baselineColumns("ok", "summary")
+ .baselineValues(false, "[testTableShowErrorMsg] is not a VIEW in schema [dfs_test.tmp]")
+ .go();
+ } finally {
+ File tblPath = new File(getDfsTestTmpSchemaLocation(), testTableName);
+ FileUtils.deleteQuietly(tblPath);
+ }
+ }
}