drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ve...@apache.org
Subject [2/4] drill git commit: DRILL-3010: Convert bad command error messages into UserExceptions in SqlHandlers
Date Tue, 12 May 2015 08:32:45 GMT
DRILL-3010: Convert bad command error messages into UserExceptions in SqlHandlers


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

Branch: refs/heads/master
Commit: 6cc89e99c7f557ffb04f16c0857d3b03a744fad4
Parents: 7c5a1f5
Author: vkorukanti <venki.korukanti@gmail.com>
Authored: Fri Apr 17 17:20:10 2015 -0700
Committer: vkorukanti <venki.korukanti@gmail.com>
Committed: Mon May 11 22:40:05 2015 -0700

----------------------------------------------------------------------
 .../drill/common/exceptions/UserException.java  |  26 +++
 .../org/apache/drill/exec/ops/QueryContext.java |   1 -
 .../drill/exec/planner/sql/SchemaUtilites.java  | 179 +++++++++++++++
 .../sql/handlers/AbstractSqlHandler.java        |  55 -----
 .../sql/handlers/CreateTableHandler.java        |  66 +++---
 .../sql/handlers/DescribeTableHandler.java      |  30 ++-
 .../planner/sql/handlers/ShowFileHandler.java   |  64 +++---
 .../planner/sql/handlers/ShowTablesHandler.java |  17 +-
 .../planner/sql/handlers/SqlHandlerUtil.java    |  18 +-
 .../planner/sql/handlers/UseSchemaHandler.java  |  19 +-
 .../exec/planner/sql/handlers/ViewHandler.java  | 134 +++++-------
 .../apache/drill/exec/rpc/user/UserSession.java |  72 ++++--
 .../apache/drill/exec/store/AbstractSchema.java |  31 ++-
 .../exec/store/dfs/WorkspaceSchemaFactory.java  |   5 +-
 .../java/org/apache/drill/BaseTestQuery.java    |   2 +
 .../TestImpersonationMetadata.java              |   8 +-
 .../physical/impl/writer/TestParquetWriter.java |  46 ----
 .../org/apache/drill/exec/sql/TestCTAS.java     |  64 ++++--
 .../apache/drill/exec/sql/TestInfoSchema.java   |  28 ++-
 .../apache/drill/exec/sql/TestViewSupport.java  |  86 ++------
 .../apache/drill/exec/proto/UserBitShared.java  | 219 ++++++++++---------
 .../drill/exec/proto/beans/DrillPBError.java    |   4 +-
 protocol/src/main/protobuf/UserBitShared.proto  |   5 +
 23 files changed, 669 insertions(+), 510 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/common/src/main/java/org/apache/drill/common/exceptions/UserException.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/drill/common/exceptions/UserException.java b/common/src/main/java/org/apache/drill/common/exceptions/UserException.java
index 21859ed..a67cb3f 100644
--- a/common/src/main/java/org/apache/drill/common/exceptions/UserException.java
+++ b/common/src/main/java/org/apache/drill/common/exceptions/UserException.java
@@ -213,6 +213,32 @@ public class UserException extends DrillRuntimeException {
   /**
    * Creates a new user exception builder .
    *
+   * @see org.apache.drill.exec.proto.UserBitShared.DrillPBError.ErrorType#VALIDATION
+   * @return user exception builder
+   */
+  public static Builder validationError() {
+    return validationError(null);
+  }
+
+  /**
+   * wraps the passed exception inside a system error.
+   * <p>the cause message will be used unless {@link Builder#message(String, Object...)} is called.
+   * <p>if the wrapped exception is, or wraps, a user exception it will be returned by {@link Builder#build()} instead
+   * of creating a new exception. Any added context will be added to the user exception as well.
+   *
+   * @see org.apache.drill.exec.proto.UserBitShared.DrillPBError.ErrorType#VALIDATION
+   *
+   * @param cause exception we want the user exception to wrap. If cause is, or wrap, a user exception it will be
+   *              returned by the builder instead of creating a new user exception
+   * @return user exception builder
+   */
+  public static Builder validationError(Throwable cause) {
+    return new Builder(DrillPBError.ErrorType.VALIDATION, cause);
+  }
+
+  /**
+   * creates a new user exception builder .
+   *
    * @see org.apache.drill.exec.proto.UserBitShared.DrillPBError.ErrorType#PERMISSION
    * @return user exception builder
    */

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/ops/QueryContext.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/ops/QueryContext.java b/exec/java-exec/src/main/java/org/apache/drill/exec/ops/QueryContext.java
index 9e2f210..8917a24 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/ops/QueryContext.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/ops/QueryContext.java
@@ -27,7 +27,6 @@ import org.apache.calcite.jdbc.SimpleCalciteSchema;
 import org.apache.drill.common.config.DrillConfig;
 import org.apache.drill.exec.ExecConstants;
 import org.apache.drill.exec.expr.fn.FunctionImplementationRegistry;
-import org.apache.drill.exec.expr.fn.impl.DateUtility;
 import org.apache.drill.common.exceptions.DrillRuntimeException;
 import org.apache.drill.exec.memory.BufferAllocator;
 import org.apache.drill.exec.memory.OutOfMemoryException;

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/SchemaUtilites.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/SchemaUtilites.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/SchemaUtilites.java
new file mode 100644
index 0000000..655e135
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/SchemaUtilites.java
@@ -0,0 +1,179 @@
+/**
+ * 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.drill.exec.planner.sql;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Strings;
+import com.google.common.collect.Lists;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.tools.ValidationException;
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.exec.store.AbstractSchema;
+
+import java.util.Collections;
+import java.util.List;
+
+public class SchemaUtilites {
+  public static final Joiner SCHEMA_PATH_JOINER = Joiner.on(".").skipNulls();
+
+  /**
+   * Search and return schema with given schemaPath. First search in schema tree starting from defaultSchema,
+   * if not found search starting from rootSchema. Root schema tree is derived from the defaultSchema reference.
+   *
+   * @param defaultSchema Reference to the default schema in complete schema tree.
+   * @param schemaPath Schema path to search.
+   * @return SchemaPlus object.
+   */
+  public static SchemaPlus findSchema(final SchemaPlus defaultSchema, final List<String> schemaPath) {
+    if (schemaPath.size() == 0) {
+      return defaultSchema;
+    }
+
+    SchemaPlus schema;
+    if ((schema = searchSchemaTree(defaultSchema, schemaPath)) != null) {
+      return schema;
+    }
+
+    SchemaPlus rootSchema = defaultSchema;
+    while(rootSchema.getParentSchema() != null) {
+      rootSchema = rootSchema.getParentSchema();
+    }
+
+    if (rootSchema != defaultSchema &&
+        (schema = searchSchemaTree(rootSchema, schemaPath)) != null) {
+      return schema;
+    }
+
+    return null;
+  }
+
+  /**
+   * Same utility as {@link #findSchema(SchemaPlus, List)} except the search schema path given here is complete path
+   * instead of list. Use "." separator to divided the schema into nested schema names.
+   * @param defaultSchema
+   * @param schemaPath
+   * @return
+   * @throws ValidationException
+   */
+  public static SchemaPlus findSchema(final SchemaPlus defaultSchema, final String schemaPath) {
+    final List<String> schemaPathAsList = Lists.newArrayList(schemaPath.split("\\."));
+    return findSchema(defaultSchema, schemaPathAsList);
+  }
+
+  /** Utility method to search for schema path starting from the given <i>schema</i> reference */
+  private static SchemaPlus searchSchemaTree(SchemaPlus schema, final List<String> schemaPath) {
+    for (String schemaName : schemaPath) {
+      schema = schema.getSubSchema(schemaName);
+      if (schema == null) {
+        return null;
+      }
+    }
+    return schema;
+  }
+
+  /**
+   * Returns true if the given <i>schema</i> is root schema. False otherwise.
+   * @param schema
+   * @return
+   */
+  public static boolean isRootSchema(SchemaPlus schema) {
+    return schema.getParentSchema() == null;
+  }
+
+  /**
+   * Unwrap given <i>SchemaPlus</i> instance as Drill schema instance (<i>AbstractSchema</i>). Once unwrapped, return
+   * default schema from <i>AbstractSchema</i>. If the given schema is not an instance of <i>AbstractSchema</i> a
+   * {@link UserException} is thrown.
+   */
+  public static AbstractSchema unwrapAsDrillSchemaInstance(SchemaPlus schemaPlus)  {
+    try {
+      return schemaPlus.unwrap(AbstractSchema.class).getDefaultSchema();
+    } catch (ClassCastException e) {
+      throw UserException.validationError(e)
+          .message("Schema [%s] is not a Drill schema.", getSchemaPath(schemaPlus))
+          .build();
+    }
+  }
+
+  /** Utility method to get the schema path for given schema instance. */
+  public static String getSchemaPath(SchemaPlus schema) {
+    return SCHEMA_PATH_JOINER.join(getSchemaPathAsList(schema));
+  }
+
+  /** Utility method to get the schema path as list for given schema instance. */
+  public static List<String> getSchemaPathAsList(SchemaPlus schema) {
+    if (isRootSchema(schema)) {
+      return Collections.EMPTY_LIST;
+    }
+
+    List<String> path = Lists.newArrayListWithCapacity(5);
+    while(schema != null) {
+      final String name = schema.getName();
+      if (!Strings.isNullOrEmpty(name)) {
+        path.add(schema.getName());
+      }
+      schema = schema.getParentSchema();
+    }
+
+    return Lists.reverse(path);
+  }
+
+  /** Utility method to throw {@link UserException} with context information */
+  public static void throwSchemaNotFoundException(final SchemaPlus defaultSchema, final String givenSchemaPath) {
+    throw UserException.validationError()
+        .message("Schema [%s] is not valid with respect to either root schema or current default schema.",
+            givenSchemaPath)
+        .addContext("Current default schema: ",
+            isRootSchema(defaultSchema) ? "No default schema selected" : getSchemaPath(defaultSchema))
+        .build();
+  }
+
+  /**
+   * Given reference to default schema in schema tree, search for schema with given <i>schemaPath</i>. Once a schema is
+   * found resolve it into a mutable <i>AbstractDrillSchema</i> instance. A {@link UserException} is throws when:
+   *   1. No schema for given <i>schemaPath</i> is found,
+   *   2. Schema found for given <i>schemaPath</i> is a root schema
+   *   3. Resolved schema is not a mutable schema.
+   * @param defaultSchema
+   * @param schemaPath
+   * @return
+   */
+  public static AbstractSchema resolveToMutableDrillSchema(final SchemaPlus defaultSchema, List<String> schemaPath) {
+    final SchemaPlus schema = findSchema(defaultSchema, schemaPath);
+
+    if (schema == null) {
+      throwSchemaNotFoundException(defaultSchema, SCHEMA_PATH_JOINER.join(schemaPath));
+    }
+
+    if (isRootSchema(schema)) {
+      throw UserException.parseError()
+          .message("Root schema is immutable. Creating or dropping tables/views is not allowed in root schema." +
+              "Select a schema using 'USE schema' command.")
+          .build();
+    }
+
+    final AbstractSchema drillSchema = unwrapAsDrillSchemaInstance(schema);
+    if (!drillSchema.isMutable()) {
+      throw UserException.parseError()
+          .message("Unable to create or drop tables/views. Schema [%s] is immutable.", getSchemaPath(schema))
+          .build();
+    }
+
+    return drillSchema;
+  }
+}

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/AbstractSqlHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/AbstractSqlHandler.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/AbstractSqlHandler.java
index 96fd877..6ce25a4 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/AbstractSqlHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/AbstractSqlHandler.java
@@ -43,59 +43,4 @@ public abstract class AbstractSqlHandler {
       throw new ForemanSetupException(String.format("Failure trying to treat %s as type %s.", o.getClass().getSimpleName(), clazz.getSimpleName()));
     }
   }
-
-  /**
-   * From a given SchemaPlus return a Drill schema object of type AbstractSchema if exists.
-   * Otherwise throw errors.
-   */
-  public static AbstractSchema getDrillSchema(SchemaPlus schemaPlus) throws Exception {
-    AbstractSchema drillSchema;
-    try {
-      drillSchema = schemaPlus.unwrap(AbstractSchema.class);
-      drillSchema = drillSchema.getDefaultSchema();
-    } catch (ClassCastException e) {
-      throw new Exception("Current schema is not a Drill schema. " +
-              "Can't create new relations (tables or views) in non-Drill schemas.", e);
-    }
-
-    return drillSchema;
-  }
-
-  /**
-   * Search for a schema with given schemaPath. First search in schema tree rooted at defaultSchema,
-   * if not found search in rootSchema. If no schema found throw errors.
-   */
-  public static SchemaPlus findSchema(SchemaPlus rootSchema, SchemaPlus defaultSchema, List<String> schemaPath)
-      throws Exception {
-    if (schemaPath.size() == 0) {
-      return defaultSchema;
-    }
-
-    SchemaPlus schema;
-
-    if ((schema = searchSchemaTree(defaultSchema, schemaPath)) != null) {
-      return schema;
-    }
-
-    if ((schema = searchSchemaTree(rootSchema, schemaPath)) != null) {
-      return schema;
-    }
-
-    throw new Exception(String.format("Invalid schema path '%s'.", Joiner.on(".").join(schemaPath)));
-  }
-
-  public static boolean isRootSchema(SchemaPlus schema) {
-    return schema.getParentSchema() == null;
-  }
-
-  private static SchemaPlus searchSchemaTree(SchemaPlus schema, List<String> schemaPath) {
-    for (String schemaName : schemaPath) {
-      schema = schema.getSubSchema(schemaName);
-      if (schema == null) {
-        return null;
-      }
-    }
-    return schema;
-  }
-
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/CreateTableHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/CreateTableHandler.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/CreateTableHandler.java
index e9ac1e1..2866b8c 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/CreateTableHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/CreateTableHandler.java
@@ -19,12 +19,12 @@ package org.apache.drill.exec.planner.sql.handlers;
 
 import java.io.IOException;
 
-import org.apache.calcite.schema.SchemaPlus;
-import org.apache.calcite.sql.TypedSqlNode;
-import org.apache.calcite.tools.Planner;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.tools.RelConversionException;
 import org.apache.calcite.tools.ValidationException;
 
+import org.apache.drill.common.exceptions.UserException;
 import org.apache.drill.exec.physical.PhysicalPlan;
 import org.apache.drill.exec.physical.base.PhysicalOperator;
 import org.apache.drill.exec.planner.logical.DrillRel;
@@ -32,17 +32,12 @@ import org.apache.drill.exec.planner.logical.DrillScreenRel;
 import org.apache.drill.exec.planner.logical.DrillStoreRel;
 import org.apache.drill.exec.planner.logical.DrillWriterRel;
 import org.apache.drill.exec.planner.physical.Prel;
-import org.apache.drill.exec.planner.sql.DirectPlan;
 import org.apache.drill.exec.planner.sql.DrillSqlWorker;
+import org.apache.drill.exec.planner.sql.SchemaUtilites;
 import org.apache.drill.exec.planner.sql.parser.SqlCreateTable;
 import org.apache.drill.exec.store.AbstractSchema;
 import org.apache.drill.exec.util.Pointer;
 import org.apache.drill.exec.work.foreman.ForemanSetupException;
-import org.apache.calcite.rel.RelNode;
-import org.apache.calcite.plan.RelOptUtil;
-import org.apache.calcite.plan.hep.HepPlanner;
-import org.apache.calcite.rel.type.RelDataType;
-import org.apache.calcite.sql.SqlNode;
 
 public class CreateTableHandler extends DefaultSqlHandler {
   public CreateTableHandler(SqlHandlerConfig config, Pointer<String> textPlan) {
@@ -53,43 +48,32 @@ public class CreateTableHandler extends DefaultSqlHandler {
   public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
     SqlCreateTable sqlCreateTable = unwrap(sqlNode, SqlCreateTable.class);
 
-    try {
-      final RelNode newTblRelNode =
-          SqlHandlerUtil.resolveNewTableRel(false, planner, sqlCreateTable.getFieldNames(), sqlCreateTable.getQuery());
+    final String newTblName = sqlCreateTable.getName();
+    final RelNode newTblRelNode =
+        SqlHandlerUtil.resolveNewTableRel(false, planner, sqlCreateTable.getFieldNames(), sqlCreateTable.getQuery());
 
-      SchemaPlus schema = findSchema(context.getRootSchema(), context.getNewDefaultSchema(),
-          sqlCreateTable.getSchemaPath());
+    final AbstractSchema drillSchema =
+        SchemaUtilites.resolveToMutableDrillSchema(context.getNewDefaultSchema(), sqlCreateTable.getSchemaPath());
+    final String schemaPath = drillSchema.getFullSchemaName();
 
-      AbstractSchema drillSchema = getDrillSchema(schema);
-
-      if (!drillSchema.isMutable()) {
-        return DirectPlan.createDirectPlan(context, false, String.format("Unable to create table. " +
-            "Schema [%s] is immutable. ", drillSchema.getFullSchemaName()));
-      }
-
-      final String newTblName = sqlCreateTable.getName();
-      if (SqlHandlerUtil.getTableFromSchema(drillSchema, newTblName) != null) {
-        throw new ValidationException(
-            String.format("A table or view with given name [%s] already exists in schema [%s]",
-                newTblName, drillSchema.getFullSchemaName()));
-      }
+    if (SqlHandlerUtil.getTableFromSchema(drillSchema, newTblName) != null) {
+      throw UserException.validationError()
+          .message("A table or view with given name [%s] already exists in schema [%s]", newTblName, schemaPath)
+          .build();
+    }
 
-      log("Optiq Logical", newTblRelNode);
+    log("Optiq Logical", newTblRelNode);
 
-      // Convert the query to Drill Logical plan and insert a writer operator on top.
-      DrillRel drel = convertToDrel(newTblRelNode, drillSchema, newTblName);
-      log("Drill Logical", drel);
-      Prel prel = convertToPrel(drel);
-      log("Drill Physical", prel);
-      PhysicalOperator pop = convertToPop(prel);
-      PhysicalPlan plan = convertToPlan(pop);
-      log("Drill Plan", plan);
+    // Convert the query to Drill Logical plan and insert a writer operator on top.
+    DrillRel drel = convertToDrel(newTblRelNode, drillSchema, newTblName);
+    log("Drill Logical", drel);
+    Prel prel = convertToPrel(drel);
+    log("Drill Physical", prel);
+    PhysicalOperator pop = convertToPop(prel);
+    PhysicalPlan plan = convertToPlan(pop);
+    log("Drill Plan", plan);
 
-      return plan;
-    } catch(Exception e) {
-      logger.error("Failed to create table '{}'", sqlCreateTable.getName(), e);
-      return DirectPlan.createDirectPlan(context, false, String.format("Error: %s", e.getMessage()));
-    }
+    return plan;
   }
 
   private DrillRel convertToDrel(RelNode relNode, AbstractSchema schema, String tableName) throws RelConversionException {

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DescribeTableHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DescribeTableHandler.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DescribeTableHandler.java
index c76914b..81defa3 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DescribeTableHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DescribeTableHandler.java
@@ -18,18 +18,17 @@
 
 package org.apache.drill.exec.planner.sql.handlers;
 
-import static org.apache.drill.exec.planner.sql.parser.DrillParserUtil.CHARSET;
-
 import java.util.List;
 
 import org.apache.calcite.schema.SchemaPlus;
-import org.apache.calcite.tools.Planner;
 import org.apache.calcite.tools.RelConversionException;
 
 import static org.apache.drill.exec.store.ischema.InfoSchemaConstants.*;
+
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.exec.planner.sql.SchemaUtilites;
 import org.apache.drill.exec.planner.sql.parser.DrillParserUtil;
 import org.apache.drill.exec.planner.sql.parser.SqlDescribeTable;
-import org.apache.drill.exec.store.AbstractSchema;
 import org.apache.drill.exec.work.foreman.ForemanSetupException;
 import org.apache.calcite.sql.SqlIdentifier;
 import org.apache.calcite.sql.SqlLiteral;
@@ -42,6 +41,8 @@ import org.apache.calcite.util.Util;
 
 import com.google.common.collect.ImmutableList;
 
+import static org.apache.drill.exec.planner.sql.parser.DrillParserUtil.CHARSET;
+
 public class DescribeTableHandler extends DefaultSqlHandler {
 
   public DescribeTableHandler(SqlHandlerConfig config) { super(config); }
@@ -61,22 +62,25 @@ public class DescribeTableHandler extends DefaultSqlHandler {
           ImmutableList.of(IS_SCHEMA_NAME, TAB_COLUMNS), null, SqlParserPos.ZERO, null);
 
       final SqlIdentifier table = node.getTable();
-      final SchemaPlus schema = findSchema(context.getRootSchema(), context.getNewDefaultSchema(),
-          Util.skipLast(table.names));
+      final SchemaPlus schema = SchemaUtilites.findSchema(context.getNewDefaultSchema(), Util.skipLast(table.names));
+
       final String tableName = Util.last(table.names);
 
+      // find resolved schema path
+      final String schemaPath = SchemaUtilites.getSchemaPath(schema);
+
       if (schema.getTable(tableName) == null) {
-        throw new RelConversionException(String.format("Table %s is not valid", Util.sepList(table.names, ".")));
+        throw UserException.validationError()
+            .message("Unknown table [%s] in schema [%s]", tableName, schemaPath)
+            .build();
       }
 
       SqlNode schemaCondition = null;
-      if (!isRootSchema(schema)) {
-        AbstractSchema drillSchema = getDrillSchema(schema);
-
+      if (!SchemaUtilites.isRootSchema(schema)) {
         schemaCondition = DrillParserUtil.createCondition(
             new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO),
             SqlStdOperatorTable.EQUALS,
-            SqlLiteral.createCharString(drillSchema.getFullSchemaName(), CHARSET, SqlParserPos.ZERO)
+            SqlLiteral.createCharString(schemaPath, CHARSET, SqlParserPos.ZERO)
         );
       }
 
@@ -106,7 +110,9 @@ public class DescribeTableHandler extends DefaultSqlHandler {
       return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO),
           fromClause, where, null, null, null, null, null, null);
     } catch (Exception ex) {
-      throw new RelConversionException("Error while rewriting DESCRIBE query: " + ex.getMessage(), ex);
+      throw UserException.planError(ex)
+          .message("Error while rewriting DESCRIBE query: %d", ex.getMessage())
+          .build();
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowFileHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowFileHandler.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowFileHandler.java
index 7062375..c96dc73 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowFileHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowFileHandler.java
@@ -25,8 +25,10 @@ import org.apache.calcite.schema.SchemaPlus;
 import org.apache.calcite.tools.RelConversionException;
 import org.apache.calcite.tools.ValidationException;
 
+import org.apache.drill.common.exceptions.UserException;
 import org.apache.drill.exec.physical.PhysicalPlan;
 import org.apache.drill.exec.planner.sql.DirectPlan;
+import org.apache.drill.exec.planner.sql.SchemaUtilites;
 import org.apache.drill.exec.planner.sql.parser.SqlShowFiles;
 import org.apache.drill.exec.store.AbstractSchema;
 import org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.WorkspaceSchema;
@@ -53,43 +55,43 @@ public class ShowFileHandler extends DefaultSqlHandler {
     String defaultLocation = null;
     String fromDir = "./";
 
-    try {
-      SchemaPlus defaultSchema = context.getNewDefaultSchema();
-      SchemaPlus drillSchema = defaultSchema;
-
-      // Show files can be used without from clause, in which case we display the files in the default schema
-      if (from != null) {
-        // We are not sure if the full from clause is just the schema or includes table name, first try to see if the full path specified is a schema
-        try {
-          drillSchema = findSchema(context.getRootSchema(), defaultSchema, from.names);
-        } catch (Exception e) {
-            // Entire from clause is not a schema, try to obtain the schema without the last part of the specified clause.
-            drillSchema = findSchema(context.getRootSchema(), defaultSchema, from.names.subList(0, from.names.size() - 1));
-            fromDir = fromDir + from.names.get((from.names.size() - 1));
-        }
+    SchemaPlus defaultSchema = context.getNewDefaultSchema();
+    SchemaPlus drillSchema = defaultSchema;
+
+    // Show files can be used without from clause, in which case we display the files in the default schema
+    if (from != null) {
+      // We are not sure if the full from clause is just the schema or includes table name,
+      // first try to see if the full path specified is a schema
+      drillSchema = SchemaUtilites.findSchema(defaultSchema, from.names);
+      if (drillSchema == null) {
+        // Entire from clause is not a schema, try to obtain the schema without the last part of the specified clause.
+        drillSchema = SchemaUtilites.findSchema(defaultSchema, from.names.subList(0, from.names.size() - 1));
+        fromDir = fromDir + from.names.get((from.names.size() - 1));
       }
 
-      AbstractSchema tempSchema = getDrillSchema(drillSchema);
-      WorkspaceSchema schema = null;
-      if (tempSchema instanceof WorkspaceSchema) {
-        schema = ((WorkspaceSchema)tempSchema);
-      } else {
-        throw new ValidationException("Unsupported schema");
+      if (drillSchema == null) {
+        throw UserException.validationError()
+            .message("Invalid FROM/IN clause [%s]", from.toString())
+            .build();
       }
+    }
 
-      // Get the file system object
-      fs = schema.getFS();
-
-      // Get the default path
-      defaultLocation = schema.getDefaultLocation();
-    } catch (Exception e) {
-        if (from == null) {
-          return DirectPlan.createDirectPlan(context, false, "Show files without FROM / IN clause can be used only after specifying a default file system schema");
-        }
-        return DirectPlan.createDirectPlan(context, false, String.format("Current schema '%s' is not a file system schema. " +
-                                           "Can't execute show files on this schema.", from.toString()));
+    WorkspaceSchema wsSchema;
+    try {
+       wsSchema = (WorkspaceSchema) drillSchema.unwrap(AbstractSchema.class).getDefaultSchema();
+    } catch (ClassCastException e) {
+      throw UserException.validationError()
+          .message("SHOW FILES is supported in workspace type schema only. Schema [%s] is not a workspace schema.",
+              SchemaUtilites.getSchemaPath(drillSchema))
+          .build();
     }
 
+    // Get the file system object
+    fs = wsSchema.getFS();
+
+    // Get the default path
+    defaultLocation = wsSchema.getDefaultLocation();
+
     List<ShowFilesCommandResult> rows = new ArrayList<>();
 
     for (FileStatus fileStatus : fs.list(false, new Path(defaultLocation, fromDir))) {

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowTablesHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowTablesHandler.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowTablesHandler.java
index 3d42f76..055b761 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowTablesHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowTablesHandler.java
@@ -25,6 +25,8 @@ import java.util.List;
 import org.apache.calcite.schema.SchemaPlus;
 import org.apache.calcite.tools.RelConversionException;
 
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.exec.planner.sql.SchemaUtilites;
 import org.apache.drill.exec.planner.sql.parser.DrillParserUtil;
 import org.apache.drill.exec.planner.sql.parser.SqlShowTables;
 import org.apache.drill.exec.store.AbstractSchema;
@@ -67,19 +69,14 @@ public class ShowTablesHandler extends DefaultSqlHandler {
       // If no schema is given in SHOW TABLES command, list tables from current schema
       SchemaPlus schema = context.getNewDefaultSchema();
 
-      if (isRootSchema(schema)) {
+      if (SchemaUtilites.isRootSchema(schema)) {
         // If the default schema is a root schema, throw an error to select a default schema
-        throw new RelConversionException("No schema selected. Select a schema using 'USE schema' command");
-      }
-
-      AbstractSchema drillSchema;
-
-      try {
-        drillSchema = getDrillSchema(schema);
-      } catch(Exception ex) {
-        throw new RelConversionException("Error while rewriting SHOW TABLES query: " + ex.getMessage(), ex);
+        throw UserException.validationError()
+            .message("No default schema selected. Select a schema using 'USE schema' command")
+            .build();
       }
 
+      final AbstractSchema drillSchema = SchemaUtilites.unwrapAsDrillSchemaInstance(schema);
       tableSchema = drillSchema.getFullSchemaName();
     }
 

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/SqlHandlerUtil.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/SqlHandlerUtil.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/SqlHandlerUtil.java
index 7ae5e0d..3edcdb2 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/SqlHandlerUtil.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/SqlHandlerUtil.java
@@ -23,6 +23,8 @@ import org.apache.calcite.sql.TypedSqlNode;
 import org.apache.calcite.tools.Planner;
 import org.apache.calcite.tools.RelConversionException;
 import org.apache.drill.common.exceptions.DrillException;
+import org.apache.drill.common.exceptions.DrillRuntimeException;
+import org.apache.drill.common.exceptions.UserException;
 import org.apache.drill.exec.planner.common.DrillRelOptUtil;
 import org.apache.drill.exec.planner.sql.DirectPlan;
 import org.apache.drill.exec.planner.types.DrillFixedRelDataTypeImpl;
@@ -78,17 +80,19 @@ public class SqlHandlerUtil {
       // Field count should match.
       if (tableFieldNames.size() != queryRowType.getFieldCount()) {
         final String tblType = isNewTableView ? "view" : "table";
-        throw new ValidationException(
-            String.format("%s's field list and the %s's query field list have different counts.", tblType, tblType));
+        throw UserException.validationError()
+            .message("%s's field list and the %s's query field list have different counts.", tblType, tblType)
+            .build();
       }
 
       // CTAS's query field list shouldn't have "*" when table's field list is specified.
       for (String field : queryRowType.getFieldNames()) {
         if (field.equals("*")) {
           final String tblType = isNewTableView ? "view" : "table";
-          throw new ValidationException(
-              String.format("%s's query field list has a '*', which is invalid when %s's field list is specified.",
-                  tblType, tblType));
+          throw UserException.validationError()
+              .message("%s's query field list has a '*', which is invalid when %s's field list is specified.",
+                  tblType, tblType)
+              .build();
         }
       }
 
@@ -119,12 +123,12 @@ public class SqlHandlerUtil {
     }
   }
 
-  public static Table getTableFromSchema(AbstractSchema drillSchema, String tblName) throws DrillException {
+  public static Table getTableFromSchema(AbstractSchema drillSchema, String tblName) {
     try {
       return drillSchema.getTable(tblName);
     } catch (Exception e) {
       // TODO: Move to better exception types.
-      throw new DrillException(
+      throw new DrillRuntimeException(
           String.format("Failure while trying to check if a table or view with given name [%s] already exists " +
               "in schema [%s]: %s", tblName, drillSchema.getFullSchemaName(), e.getMessage()), e);
     }

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/UseSchemaHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/UseSchemaHandler.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/UseSchemaHandler.java
index e17e275..0ec6eb5 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/UseSchemaHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/UseSchemaHandler.java
@@ -19,9 +19,10 @@ package org.apache.drill.exec.planner.sql.handlers;
 
 import java.io.IOException;
 
+import com.google.common.base.Strings;
+
 import org.apache.calcite.tools.RelConversionException;
 import org.apache.calcite.tools.ValidationException;
-
 import org.apache.drill.exec.ops.QueryContext;
 import org.apache.drill.exec.physical.PhysicalPlan;
 import org.apache.drill.exec.planner.sql.DirectPlan;
@@ -38,18 +39,12 @@ public class UseSchemaHandler extends AbstractSqlHandler {
 
   @Override
   public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
-    SqlUseSchema useSchema = unwrap(sqlNode, SqlUseSchema.class);
-
-    String defaultSchema = useSchema.getSchema();
-    boolean status = context.getSession().setDefaultSchemaPath(defaultSchema, context.getRootSchema());
+    final SqlUseSchema useSchema = unwrap(sqlNode, SqlUseSchema.class);
+    final String newDefaultSchemaPath = useSchema.getSchema();
 
-    String msg;
-    if (status) {
-      msg = String.format("Default schema changed to '%s'", defaultSchema);
-    } else {
-      msg = String.format("Failed to change default schema to '%s'", defaultSchema);
-    }
+    context.getSession().setDefaultSchemaPath(newDefaultSchemaPath, context.getNewDefaultSchema());
 
-    return DirectPlan.createDirectPlan(context, status, msg);
+    return DirectPlan.createDirectPlan(context, true,
+        String.format("Default schema changed to [%s]", context.getSession().getDefaultSchemaPath()));
   }
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/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 c59c3a2..0a3393e 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
@@ -18,7 +18,6 @@
 package org.apache.drill.exec.planner.sql.handlers;
 
 import java.io.IOException;
-import java.util.List;
 
 import org.apache.calcite.schema.Schema;
 import org.apache.calcite.schema.Schema.TableType;
@@ -28,20 +27,19 @@ import org.apache.calcite.tools.Planner;
 import org.apache.calcite.tools.RelConversionException;
 import org.apache.calcite.tools.ValidationException;
 
+import org.apache.drill.common.exceptions.UserException;
 import org.apache.drill.exec.dotdrill.View;
 import org.apache.drill.exec.ops.QueryContext;
 import org.apache.drill.exec.physical.PhysicalPlan;
 import org.apache.drill.exec.planner.sql.DirectPlan;
+import org.apache.drill.exec.planner.sql.SchemaUtilites;
 import org.apache.drill.exec.planner.sql.parser.SqlCreateView;
 import org.apache.drill.exec.planner.sql.parser.SqlDropView;
 import org.apache.drill.exec.store.AbstractSchema;
-import org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.WorkspaceSchema;
 import org.apache.drill.exec.work.foreman.ForemanSetupException;
 import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.sql.SqlNode;
 
-import com.google.common.collect.ImmutableList;
-
 public abstract class ViewHandler extends AbstractSqlHandler {
   static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ViewHandler.class);
 
@@ -64,65 +62,46 @@ public abstract class ViewHandler extends AbstractSqlHandler {
     public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
       SqlCreateView createView = unwrap(sqlNode, SqlCreateView.class);
 
-      try {
-        // Store the viewSql as view def SqlNode is modified as part of the resolving the new table definition below.
-        final String viewSql = createView.getQuery().toString();
+      final String newViewName = createView.getName();
 
-        final RelNode newViewRelNode =
-            SqlHandlerUtil.resolveNewTableRel(true, planner, createView.getFieldNames(), createView.getQuery());
+      // Store the viewSql as view def SqlNode is modified as part of the resolving the new table definition below.
+      final String viewSql = createView.getQuery().toString();
 
-        SchemaPlus defaultSchema = context.getNewDefaultSchema();
-        SchemaPlus schema = findSchema(context.getRootSchema(), defaultSchema, createView.getSchemaPath());
-        AbstractSchema drillSchema = getDrillSchema(schema);
+      final RelNode newViewRelNode =
+          SqlHandlerUtil.resolveNewTableRel(true, planner, createView.getFieldNames(), createView.getQuery());
 
-        String schemaPath = drillSchema.getFullSchemaName();
-        if (!drillSchema.isMutable()) {
-          return DirectPlan.createDirectPlan(context, false, String.format("Unable to create view. " +
-            "Schema [%s] is immutable. ", schemaPath));
-        }
+      final SchemaPlus defaultSchema = context.getNewDefaultSchema();
+      final AbstractSchema drillSchema = SchemaUtilites.resolveToMutableDrillSchema(defaultSchema, createView.getSchemaPath());
 
-        // find current workspace schema path
-        List<String> workspaceSchemaPath = ImmutableList.of();
-        if (!isRootSchema(defaultSchema)) {
-          workspaceSchemaPath = getDrillSchema(defaultSchema).getSchemaPath();
-        }
+      final String schemaPath = drillSchema.getFullSchemaName();
+      final View view = new View(newViewName, viewSql, newViewRelNode.getRowType(),
+          SchemaUtilites.getSchemaPathAsList(defaultSchema));
+
+      final Table existingTable = SqlHandlerUtil.getTableFromSchema(drillSchema, newViewName);
 
-        View view = new View(createView.getName(), viewSql, newViewRelNode.getRowType(), workspaceSchemaPath);
-
-        final String viewName = view.getName();
-        final Table existingTable = SqlHandlerUtil.getTableFromSchema(drillSchema, viewName);
-
-        if (existingTable != null) {
-          if (existingTable.getJdbcTableType() != Schema.TableType.VIEW) {
-            // existing table is not a view
-            throw new ValidationException(
-                String.format("A non-view table with given name [%s] already exists in schema [%s]",
-                    viewName, schemaPath));
-          }
-
-          if (existingTable.getJdbcTableType() == Schema.TableType.VIEW && !createView.getReplace()) {
-            // existing table is a view and create view has no "REPLACE" clause
-            throw new ValidationException(
-                String.format("A view with given name [%s] already exists in schema [%s]",
-                    view.getName(), schemaPath));
-          }
+      if (existingTable != null) {
+        if (existingTable.getJdbcTableType() != Schema.TableType.VIEW) {
+          // existing table is not a view
+          throw UserException.validationError()
+              .message("A non-view table with given name [%s] already exists in schema [%s]",
+                  newViewName, schemaPath)
+              .build();
         }
 
-        boolean replaced;
-        if (drillSchema instanceof WorkspaceSchema) {
-          replaced = ((WorkspaceSchema) drillSchema).createView(view);
-        } else {
-          return DirectPlan.createDirectPlan(context, false, "Schema provided was not a workspace schema.");
+        if (existingTable.getJdbcTableType() == Schema.TableType.VIEW && !createView.getReplace()) {
+          // existing table is a view and create view has no "REPLACE" clause
+          throw UserException.validationError()
+              .message("A view with given name [%s] already exists in schema [%s]",
+                  newViewName, schemaPath)
+              .build();
         }
+      }
 
-        String summary = String.format("View '%s' %s successfully in '%s' schema",
-            createView.getName(), replaced ? "replaced" : "created", schemaPath);
+      final boolean replaced = drillSchema.createView(view);
+      final String summary = String.format("View '%s' %s successfully in '%s' schema",
+          createView.getName(), replaced ? "replaced" : "created", schemaPath);
 
-        return DirectPlan.createDirectPlan(context, true, summary);
-      } catch(Exception e) {
-        logger.error("Failed to create view '{}'", createView.getName(), e);
-        return DirectPlan.createDirectPlan(context, false, String.format("Error: %s", e.getMessage()));
-      }
+      return DirectPlan.createDirectPlan(context, true, summary);
     }
   }
 
@@ -136,39 +115,26 @@ public abstract class ViewHandler extends AbstractSqlHandler {
     public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
       SqlDropView dropView = unwrap(sqlNode, SqlDropView.class);
       final String viewToDrop = 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();
+      } else if (existingTable == null) {
+        throw UserException.validationError()
+            .message("Unknown view [%s] in schema [%s].", viewToDrop, schemaPath)
+            .build();
+      }
 
-      try {
-        SchemaPlus schema = findSchema(context.getRootSchema(), context.getNewDefaultSchema(), dropView.getSchemaPath());
-        AbstractSchema drillSchema = getDrillSchema(schema);
-
-        String schemaPath = drillSchema.getFullSchemaName();
-        if (!drillSchema.isMutable()) {
-          return DirectPlan.createDirectPlan(context, false, String.format("Schema [%s] is immutable.", schemaPath));
-        }
-
-        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);
+      drillSchema.dropView(viewToDrop);
 
-        return DirectPlan.createDirectPlan(context, true,
-            String.format("View [%s] deleted successfully from schema [%s].", viewToDrop, schemaPath));
-      } catch(Exception e) {
-        logger.debug("Failed to delete view {}", viewToDrop, e);
-        return DirectPlan.createDirectPlan(context, false, String.format("Error: %s", e.getMessage()));
-      }
+      return DirectPlan.createDirectPlan(context, true,
+          String.format("View [%s] deleted successfully from schema [%s].", viewToDrop, schemaPath));
     }
   }
-
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/rpc/user/UserSession.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/rpc/user/UserSession.java b/exec/java-exec/src/main/java/org/apache/drill/exec/rpc/user/UserSession.java
index 9f1a695..e717eaa 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/rpc/user/UserSession.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/rpc/user/UserSession.java
@@ -17,11 +17,17 @@
  */
 package org.apache.drill.exec.rpc.user;
 
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import org.apache.calcite.schema.SchemaPlus;
+import com.google.common.base.Strings;
+import com.google.common.collect.Lists;
 
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.tools.ValidationException;
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.exec.planner.sql.SchemaUtilites;
 import org.apache.drill.exec.proto.UserBitShared.UserCredentials;
 import org.apache.drill.exec.proto.UserProtos.Property;
 import org.apache.drill.exec.proto.UserProtos.UserProperties;
@@ -133,17 +139,37 @@ public class UserSession {
 
   /**
    * Update the schema path for the session.
-   * @param fullPath The desired path to set to.
-   * @param schema The root schema to find this path within.
-   * @return true if the path was set successfully.  false if this path was unavailable.
+   * @param newDefaultSchemaPath New default schema path to set. It could be relative to the current default schema or
+   *                             absolute schema.
+   * @param currentDefaultSchema Current default schema.
+   * @throws ValidationException If the given default schema path is invalid in current schema tree.
    */
-  public boolean setDefaultSchemaPath(String fullPath, SchemaPlus schema) {
-    SchemaPlus newDefault = findSchema(schema, fullPath);
+  public void setDefaultSchemaPath(String newDefaultSchemaPath, SchemaPlus currentDefaultSchema)
+      throws ValidationException {
+    final List<String> newDefaultPathAsList = Lists.newArrayList(newDefaultSchemaPath.split("\\."));
+    SchemaPlus newDefault;
+
+    // First try to find the given schema relative to the current default schema.
+    newDefault = SchemaUtilites.findSchema(currentDefaultSchema, newDefaultPathAsList);
+
     if (newDefault == null) {
-      return false;
+      // If we fail to find the schema relative to current default schema, consider the given new default schema path as
+      // absolute schema path.
+      newDefault = SchemaUtilites.findSchema(currentDefaultSchema, newDefaultPathAsList);
     }
-    setProp(SCHEMA, fullPath);
-    return true;
+
+    if (newDefault == null) {
+      SchemaUtilites.throwSchemaNotFoundException(currentDefaultSchema, newDefaultSchemaPath);
+    }
+
+    setProp(SCHEMA, SchemaUtilites.getSchemaPath(newDefault));
+  }
+
+  /**
+   * @return Get current default schema path.
+   */
+  public String getDefaultSchemaPath() {
+    return getProp(SCHEMA);
   }
 
   /**
@@ -152,7 +178,20 @@ public class UserSession {
    * @return A {@link org.apache.calcite.schema.SchemaPlus} object.
    */
   public SchemaPlus getDefaultSchema(SchemaPlus rootSchema) {
-    return findSchema(rootSchema, getProp(SCHEMA));
+    final String defaultSchemaPath = getProp(SCHEMA);
+
+    if (Strings.isNullOrEmpty(defaultSchemaPath)) {
+      return null;
+    }
+
+    final SchemaPlus defaultSchema = SchemaUtilites.findSchema(rootSchema, defaultSchemaPath);
+
+    if (defaultSchema == null) {
+      // If the current schema resolves to null, return root schema as the current default schema.
+      return defaultSchema;
+    }
+
+    return defaultSchema;
   }
 
   public boolean setSessionOption(String name, String value) {
@@ -166,17 +205,4 @@ public class UserSession {
   private void setProp(String key, String value) {
     properties.put(key, value);
   }
-
-  private SchemaPlus findSchema(SchemaPlus rootSchema, String schemaPath) {
-    String[] paths = schemaPath.split("\\.");
-    SchemaPlus schema = rootSchema;
-    for (String p : paths) {
-      schema = schema.getSubSchema(p);
-      if (schema == null) {
-        break;
-      }
-    }
-    return schema;
-  }
-
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/store/AbstractSchema.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/store/AbstractSchema.java b/exec/java-exec/src/main/java/org/apache/drill/exec/store/AbstractSchema.java
index 33ddea5..6afce1a 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/store/AbstractSchema.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/store/AbstractSchema.java
@@ -17,6 +17,7 @@
  */
 package org.apache.drill.exec.store;
 
+import java.io.IOException;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -29,6 +30,8 @@ import org.apache.calcite.schema.Schema;
 import org.apache.calcite.schema.SchemaPlus;
 import org.apache.calcite.schema.Table;
 
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.exec.dotdrill.View;
 import org.apache.drill.exec.planner.logical.CreateTableEntry;
 
 import com.google.common.base.Joiner;
@@ -88,8 +91,34 @@ public abstract class AbstractSchema implements Schema, SchemaPartitionExplorer
     return this;
   }
 
+  /**
+   * Create a new view given definition.
+   * @param view View info including name, definition etc.
+   * @return Returns true if an existing view is replaced with the given view. False otherwise.
+   * @throws IOException
+   */
+  public boolean createView(View view) throws IOException {
+    throw UserException.unsupportedError()
+        .message("Creating new view is not supported in schema [%s]", getSchemaPath())
+        .build();
+  }
+
+  /**
+   * Drop the view with given name.
+   *
+   * @param viewName
+   * @throws IOException
+   */
+  public void dropView(String viewName) throws IOException {
+    throw UserException.unsupportedError()
+        .message("Dropping a view is supported in schema [%s]", getSchemaPath())
+        .build();
+  }
+
   public CreateTableEntry createNewTable(String tableName) {
-    throw new UnsupportedOperationException("New tables are not allowed in this schema");
+    throw UserException.unsupportedError()
+        .message("Creating new tables is not supported in schema [%s]", getSchemaPath())
+        .build();
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/WorkspaceSchemaFactory.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/WorkspaceSchemaFactory.java b/exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/WorkspaceSchemaFactory.java
index 916564d..b1135d0 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/WorkspaceSchemaFactory.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/WorkspaceSchemaFactory.java
@@ -19,7 +19,6 @@ package org.apache.drill.exec.store.dfs;
 
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Set;
@@ -126,7 +125,8 @@ public class WorkspaceSchemaFactory {
       this.fs = ImpersonationUtil.createFileSystem(schemaConfig.getUserName(), fsConf);
     }
 
-    public boolean createView(View view) throws Exception {
+    @Override
+    public boolean createView(View view) throws IOException {
       Path viewPath = getViewPath(view.getName());
       boolean replaced = fs.exists(viewPath);
       final FsPermission viewPerms =
@@ -152,6 +152,7 @@ public class WorkspaceSchemaFactory {
       return new SubDirectoryList(fileStatuses);
     }
 
+    @Override
     public void dropView(String viewName) throws IOException {
       fs.delete(getViewPath(viewName), false);
     }

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/test/java/org/apache/drill/BaseTestQuery.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/BaseTestQuery.java b/exec/java-exec/src/test/java/org/apache/drill/BaseTestQuery.java
index 83134b3..a07f621 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/BaseTestQuery.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/BaseTestQuery.java
@@ -66,6 +66,8 @@ import static org.junit.Assert.assertThat;
 public class BaseTestQuery extends ExecTest {
   private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(BaseTestQuery.class);
 
+  protected static final String TEMP_SCHEMA = "dfs_test.tmp";
+
   private static final String ENABLE_FULL_CACHE = "drill.exec.test.use-full-cache";
   private static final int MAX_WIDTH_PER_NODE = 2;
 

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/test/java/org/apache/drill/exec/impersonation/TestImpersonationMetadata.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/impersonation/TestImpersonationMetadata.java b/exec/java-exec/src/test/java/org/apache/drill/exec/impersonation/TestImpersonationMetadata.java
index 411660f..122542a 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/exec/impersonation/TestImpersonationMetadata.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/impersonation/TestImpersonationMetadata.java
@@ -20,6 +20,7 @@ package org.apache.drill.exec.impersonation;
 import com.google.common.base.Joiner;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Maps;
+import org.apache.drill.common.exceptions.UserException;
 import org.apache.drill.common.exceptions.UserRemoteException;
 import org.apache.drill.exec.rpc.RpcException;
 import org.apache.drill.exec.store.StoragePluginRegistry;
@@ -207,8 +208,11 @@ public class TestImpersonationMetadata extends BaseTestImpersonation {
 
     test("USE " + viewSchema);
 
-    test("CREATE VIEW " + viewName + " AS SELECT " +
-        "c_custkey, c_nationkey FROM cp.`tpch/customer.parquet` ORDER BY c_custkey;");
+    final String query = "CREATE VIEW " + viewName + " AS SELECT " +
+        "c_custkey, c_nationkey FROM cp.`tpch/customer.parquet` ORDER BY c_custkey;";
+    final String expErrorMsg = "PERMISSION ERROR: Permission denied: user=drillTestUser2, access=WRITE, " +
+        "inode=\"/drillTestGrp0_755\"";
+    errorMsgTestHelper(query, expErrorMsg);
 
     // SHOW TABLES is expected to return no records as view creation fails above.
     testBuilder()

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/writer/TestParquetWriter.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/writer/TestParquetWriter.java b/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/writer/TestParquetWriter.java
index 958cf1a..5f57567 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/writer/TestParquetWriter.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/writer/TestParquetWriter.java
@@ -477,52 +477,6 @@ public class TestParquetWriter extends BaseTestQuery {
     }
   }
 
-  @Test // DRILL-2422
-  public void createTableWhenATableWithSameNameAlreadyExists() throws Exception{
-    final String newTblName = "createTableWhenTableAlreadyExists";
-
-    try {
-      test("USE dfs_test.tmp");
-      final String ctas = String.format("CREATE TABLE %s AS SELECT * from cp.`region.json`", newTblName);
-
-      test(ctas);
-
-      testBuilder()
-          .unOrdered()
-          .sqlQuery(ctas)
-          .baselineColumns("ok", "summary")
-          .baselineValues(false,
-              String.format("Error: A table or view with given name [%s] already exists in schema [%s]",
-                  newTblName, "dfs_test.tmp"))
-          .go();
-    } finally {
-      deleteTableIfExists(newTblName);
-    }
-  }
-
-  @Test // DRILL-2422
-  public void createTableWhenAViewWithSameNameAlreadyExists() throws Exception{
-    final String newTblName = "createTableWhenAViewWithSameNameAlreadyExists";
-
-    try {
-      test("USE dfs_test.tmp");
-      final String createView = String.format("CREATE VIEW %s AS SELECT * from cp.`region.json`", newTblName);
-
-      test(createView);
-
-      testBuilder()
-          .unOrdered()
-          .sqlQuery(String.format("CREATE TABLE %s AS SELECT * FROM cp.`employee.json`", newTblName))
-          .baselineColumns("ok", "summary")
-          .baselineValues(false,
-              String.format("Error: A table or view with given name [%s] already exists in schema [%s]",
-                  newTblName, "dfs_test.tmp"))
-          .go();
-    } finally {
-      test("DROP VIEW " + newTblName);
-    }
-  }
-
   private static void deleteTableIfExists(String tableName) {
     try {
       Path path = new Path(getDfsTestTmpSchemaLocation(), tableName);

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestCTAS.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestCTAS.java b/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestCTAS.java
index 5fff956..c4cc37a 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestCTAS.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestCTAS.java
@@ -17,22 +17,24 @@
  */
 package org.apache.drill.exec.sql;
 
-import com.google.common.collect.ImmutableList;
+import org.apache.commons.io.FileUtils;
 import org.apache.drill.BaseTestQuery;
 import org.junit.Test;
 
+import java.io.File;
+
 public class TestCTAS extends BaseTestQuery {
   @Test // DRILL-2589
   public void withDuplicateColumnsInDef1() throws Exception {
     ctasErrorTestHelper("CREATE TABLE %s.%s AS SELECT region_id, region_id FROM cp.`region.json`",
-        String.format("Error: Duplicate column name [%s]", "region_id")
+        String.format("Duplicate column name [%s]", "region_id")
     );
   }
 
   @Test // DRILL-2589
   public void withDuplicateColumnsInDef2() throws Exception {
     ctasErrorTestHelper("CREATE TABLE %s.%s AS SELECT region_id, sales_city, sales_city FROM cp.`region.json`",
-        String.format("Error: Duplicate column name [%s]", "sales_city")
+        String.format("Duplicate column name [%s]", "sales_city")
     );
   }
 
@@ -41,7 +43,7 @@ public class TestCTAS extends BaseTestQuery {
     ctasErrorTestHelper(
         "CREATE TABLE %s.%s(regionid, regionid) " +
             "AS SELECT region_id, sales_city FROM cp.`region.json`",
-        String.format("Error: Duplicate column name [%s]", "regionid")
+        String.format("Duplicate column name [%s]", "regionid")
     );
   }
 
@@ -50,7 +52,7 @@ public class TestCTAS extends BaseTestQuery {
     ctasErrorTestHelper(
         "CREATE TABLE %s.%s(regionid, salescity, salescity) " +
             "AS SELECT region_id, sales_city, sales_city FROM cp.`region.json`",
-        String.format("Error: Duplicate column name [%s]", "salescity")
+        String.format("Duplicate column name [%s]", "salescity")
     );
   }
 
@@ -59,7 +61,7 @@ public class TestCTAS extends BaseTestQuery {
     ctasErrorTestHelper(
         "CREATE TABLE %s.%s(regionid, salescity, SalesCity) " +
             "AS SELECT region_id, sales_city, sales_city FROM cp.`region.json`",
-        String.format("Error: Duplicate column name [%s]", "SalesCity")
+        String.format("Duplicate column name [%s]", "SalesCity")
     );
   }
 
@@ -68,7 +70,7 @@ public class TestCTAS extends BaseTestQuery {
     ctasErrorTestHelper(
         "CREATE TABLE %s.%s(regionid, salescity) " +
             "AS SELECT region_id, sales_city, sales_region FROM cp.`region.json`",
-        "Error: table's field list and the table's query field list have different counts."
+        "table's field list and the table's query field list have different counts."
     );
   }
 
@@ -77,19 +79,47 @@ public class TestCTAS extends BaseTestQuery {
     ctasErrorTestHelper(
         "CREATE TABLE %s.%s(regionid, salescity) " +
             "AS SELECT region_id, * FROM cp.`region.json`",
-        "Error: table's query field list has a '*', which is invalid when table's field list is specified."
+        "table's query field list has a '*', which is invalid when table's field list is specified."
     );
   }
 
-  private static void ctasErrorTestHelper(final String ctasSql, final String errorMsg)
-      throws Exception {
-    final String createTableSql = String.format(ctasSql, "dfs_test.tmp", "testTableName");
+  @Test // DRILL-2422
+  public void createTableWhenATableWithSameNameAlreadyExists() throws Exception{
+    final String newTblName = "createTableWhenTableAlreadyExists";
+
+    try {
+      final String ctasQuery =
+          String.format("CREATE TABLE %s.%s AS SELECT * from cp.`region.json`", TEMP_SCHEMA, newTblName);
+
+      test(ctasQuery);
+
+      errorMsgTestHelper(ctasQuery,
+          String.format("A table or view with given name [%s] already exists in schema [%s]", newTblName, TEMP_SCHEMA));
+    } finally {
+      FileUtils.deleteQuietly(new File(getDfsTestTmpSchemaLocation(), newTblName));
+    }
+  }
+
+  @Test // DRILL-2422
+  public void createTableWhenAViewWithSameNameAlreadyExists() throws Exception{
+    final String newTblName = "createTableWhenAViewWithSameNameAlreadyExists";
+
+    try {
+      test(String.format("CREATE VIEW %s.%s AS SELECT * from cp.`region.json`", TEMP_SCHEMA, newTblName));
+
+      final String ctasQuery =
+          String.format("CREATE TABLE %s.%s AS SELECT * FROM cp.`employee.json`", TEMP_SCHEMA, newTblName);
+
+      errorMsgTestHelper(ctasQuery,
+          String.format("A table or view with given name [%s] already exists in schema [%s]",
+              newTblName, "dfs_test.tmp"));
+    } finally {
+      test(String.format("DROP VIEW %s.%s", TEMP_SCHEMA, newTblName));
+    }
+  }
 
-    testBuilder()
-        .sqlQuery(createTableSql)
-        .unOrdered()
-        .baselineColumns("ok", "summary")
-        .baselineValues(false, errorMsg)
-        .go();
+  private static void ctasErrorTestHelper(final String ctasSql, final String expErrorMsg) throws Exception {
+    final String createTableSql = String.format(ctasSql, TEMP_SCHEMA, "testTableName");
+    errorMsgTestHelper(createTableSql, expErrorMsg);
   }
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestInfoSchema.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestInfoSchema.java b/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestInfoSchema.java
index 8bcbc7a..9a35be4 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestInfoSchema.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestInfoSchema.java
@@ -285,18 +285,38 @@ public class TestInfoSchema extends BaseTestQuery {
         .sqlQuery("USE dfs_test.`default`")
         .unOrdered()
         .baselineColumns("ok", "summary")
-        .baselineValues(true, "Default schema changed to 'dfs_test.default'")
+        .baselineValues(true, "Default schema changed to [dfs_test.default]")
         .go();
   }
 
   @Test
-  public void useSchemaNegative() throws Exception{
+  public void useSubSchemaWithinSchema() throws Exception{
+    testBuilder()
+        .sqlQuery("USE dfs_test")
+        .unOrdered()
+        .baselineColumns("ok", "summary")
+        .baselineValues(true, "Default schema changed to [dfs_test]")
+        .go();
+
     testBuilder()
-        .sqlQuery("USE invalid.schema")
+        .sqlQuery("USE tmp")
         .unOrdered()
         .baselineColumns("ok", "summary")
-        .baselineValues(false, "Failed to change default schema to 'invalid.schema'")
+        .baselineValues(true, "Default schema changed to [dfs_test.tmp]")
         .go();
+
+    testBuilder()
+        .sqlQuery("USE dfs.`default`")
+        .unOrdered()
+        .baselineColumns("ok", "summary")
+        .baselineValues(true, "Default schema changed to [dfs.default]")
+        .go();
+  }
+
+  @Test
+  public void useSchemaNegative() throws Exception{
+    errorMsgTestHelper("USE invalid.schema",
+        "Schema [invalid.schema] is not valid with respect to either root schema or current default schema.");
   }
 
   // Tests using backticks around the complete schema path

http://git-wip-us.apache.org/repos/asf/drill/blob/6cc89e99/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 0fc1f32..e3156d0 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
@@ -26,8 +26,6 @@ import java.io.File;
 import java.util.List;
 
 public class TestViewSupport extends TestBaseViewSupport {
-  private static final String TEMP_SCHEMA = "dfs_test.tmp";
-
   @Test
   public void referToSchemaInsideAndOutsideView() throws Exception {
     String use = "use dfs_test.tmp;";
@@ -273,13 +271,8 @@ public class TestViewSupport extends TestBaseViewSupport {
 
       // Try to create the view with same name in same schema.
       final String createViewSql = String.format("CREATE VIEW %s.`%s` AS %s", TEMP_SCHEMA, viewName, viewDef1);
-      testBuilder()
-          .sqlQuery(createViewSql)
-          .unOrdered()
-          .baselineColumns("ok", "summary")
-          .baselineValues(false,
-              String.format("Error: A view with given name [%s] already exists in schema [%s]", viewName, TEMP_SCHEMA))
-          .go();
+      errorMsgTestHelper(createViewSql,
+          String.format("A view with given name [%s] already exists in schema [%s]", viewName, TEMP_SCHEMA));
 
       // Try creating the view with same name in same schema, but with CREATE OR REPLACE VIEW clause
       final String viewDef2 = "SELECT sales_state_province FROM cp.`region.json` ORDER BY `region_id`";
@@ -296,7 +289,7 @@ public class TestViewSupport extends TestBaseViewSupport {
 
       // Make sure the new view created returns the data expected.
       queryViewHelper(String.format("SELECT * FROM %s.`%s` LIMIT 1", TEMP_SCHEMA, viewName),
-          new String[]{"sales_state_province" },
+          new String[]{"sales_state_province"},
           ImmutableList.of(new Object[]{"None"})
       );
     } finally {
@@ -315,25 +308,13 @@ public class TestViewSupport extends TestBaseViewSupport {
 
       // Try to create the view with same name in same schema.
       final String createViewSql = String.format("CREATE VIEW %s.`%s` AS %s", TEMP_SCHEMA, tableName, tableDef1);
-      testBuilder()
-          .sqlQuery(createViewSql)
-          .unOrdered()
-          .baselineColumns("ok", "summary")
-          .baselineValues(false,
-              String.format("Error: A non-view table with given name [%s] already exists in schema [%s]",
-                  tableName, TEMP_SCHEMA))
-          .go();
+      errorMsgTestHelper(createViewSql,
+          String.format("A non-view table with given name [%s] already exists in schema [%s]", tableName, TEMP_SCHEMA));
 
       // Try creating the view with same name in same schema, but with CREATE OR REPLACE VIEW clause
       final String viewDef2 = "SELECT sales_state_province FROM cp.`region.json` ORDER BY `region_id`";
-      testBuilder()
-          .sqlQuery(String.format("CREATE OR REPLACE VIEW %s.`%s` AS %s", TEMP_SCHEMA, tableName, viewDef2))
-          .unOrdered()
-          .baselineColumns("ok", "summary")
-          .baselineValues(false,
-              String.format("Error: A non-view table with given name [%s] already exists in schema [%s]",
-                  tableName, TEMP_SCHEMA))
-          .go();
+      errorMsgTestHelper(String.format("CREATE OR REPLACE VIEW %s.`%s` AS %s", TEMP_SCHEMA, tableName, viewDef2),
+          String.format("A non-view table with given name [%s] already exists in schema [%s]", tableName, TEMP_SCHEMA));
     } finally {
       FileUtils.deleteQuietly(new File(getDfsTestTmpSchemaLocation(), tableName));
     }
@@ -445,7 +426,7 @@ public class TestViewSupport extends TestBaseViewSupport {
       test("USE dfs_test");
       queryViewHelper(
           String.format("SELECT * FROM %s.`%s` ORDER BY region_id DESC LIMIT 1", "tmp", viewName),
-          baselineColumns,baselineValues);
+          baselineColumns, baselineValues);
 
     } finally {
       dropViewHelper(TEMP_SCHEMA, viewName, TEMP_SCHEMA);
@@ -485,14 +466,14 @@ public class TestViewSupport extends TestBaseViewSupport {
   public void createViewWithDuplicateColumnsInDef1() throws Exception {
     createViewErrorTestHelper(
         "CREATE VIEW %s.%s AS SELECT region_id, region_id FROM cp.`region.json`",
-        String.format("Error: Duplicate column name [%s]", "region_id")
+        String.format("Duplicate column name [%s]", "region_id")
     );
   }
 
   @Test // DRILL-2589
   public void createViewWithDuplicateColumnsInDef2() throws Exception {
     createViewErrorTestHelper("CREATE VIEW %s.%s AS SELECT region_id, sales_city, sales_city FROM cp.`region.json`",
-        String.format("Error: Duplicate column name [%s]", "sales_city")
+        String.format("Duplicate column name [%s]", "sales_city")
     );
   }
 
@@ -500,7 +481,7 @@ public class TestViewSupport extends TestBaseViewSupport {
   public void createViewWithDuplicateColumnsInDef3() throws Exception {
     createViewErrorTestHelper(
         "CREATE VIEW %s.%s(regionid, regionid) AS SELECT region_id, sales_city FROM cp.`region.json`",
-        String.format("Error: Duplicate column name [%s]", "regionid")
+        String.format("Duplicate column name [%s]", "regionid")
     );
   }
 
@@ -509,7 +490,7 @@ public class TestViewSupport extends TestBaseViewSupport {
     createViewErrorTestHelper(
         "CREATE VIEW %s.%s(regionid, salescity, salescity) " +
             "AS SELECT region_id, sales_city, sales_city FROM cp.`region.json`",
-        String.format("Error: Duplicate column name [%s]", "salescity")
+        String.format("Duplicate column name [%s]", "salescity")
     );
   }
 
@@ -518,7 +499,7 @@ public class TestViewSupport extends TestBaseViewSupport {
     createViewErrorTestHelper(
         "CREATE VIEW %s.%s(regionid, salescity, SalesCity) " +
             "AS SELECT region_id, sales_city, sales_city FROM cp.`region.json`",
-        String.format("Error: Duplicate column name [%s]", "SalesCity")
+        String.format("Duplicate column name [%s]", "SalesCity")
     );
   }
 
@@ -528,7 +509,7 @@ public class TestViewSupport extends TestBaseViewSupport {
         "CREATE VIEW %s.%s " +
             "AS SELECT t1.region_id, t2.region_id FROM cp.`region.json` t1 JOIN cp.`region.json` t2 " +
             "ON t1.region_id = t2.region_id LIMIT 1",
-        String.format("Error: Duplicate column name [%s]", "region_id")
+        String.format("Duplicate column name [%s]", "region_id")
     );
   }
 
@@ -566,7 +547,7 @@ public class TestViewSupport extends TestBaseViewSupport {
     createViewErrorTestHelper(
         "CREATE VIEW %s.%s(regionid, salescity) " +
             "AS SELECT region_id, sales_city, sales_region FROM cp.`region.json`",
-        "Error: view's field list and the view's query field list have different counts."
+        "view's field list and the view's query field list have different counts."
     );
   }
 
@@ -575,40 +556,25 @@ public class TestViewSupport extends TestBaseViewSupport {
     createViewErrorTestHelper(
         "CREATE VIEW %s.%s(regionid, salescity) " +
             "AS SELECT region_id, * FROM cp.`region.json`",
-        "Error: view's query field list has a '*', which is invalid when view's field list is specified."
+        "view's query field list has a '*', which is invalid when view's field list is specified."
     );
   }
 
-  private static void createViewErrorTestHelper(final String viewSql, final String errorMsg)
-      throws Exception {
+  private static void createViewErrorTestHelper(final String viewSql, final String expErrorMsg) throws Exception {
     final String createViewSql = String.format(viewSql, TEMP_SCHEMA, "duplicateColumnsInViewDef");
-
-    testBuilder()
-        .sqlQuery(createViewSql)
-        .unOrdered()
-        .baselineColumns("ok", "summary")
-        .baselineValues(false, errorMsg)
-        .go();
+    errorMsgTestHelper(createViewSql, expErrorMsg);
   }
 
   @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();
+    errorMsgTestHelper("DROP VIEW dfs_test.tmp.nonExistentView",
+        "Unknown view [nonExistentView] in schema [dfs_test.tmp].");
   }
 
   @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();
+    errorMsgTestHelper("DROP VIEW cp.nonExistentView",
+        "Unable to create or drop tables/views. Schema [cp] is immutable.");
   }
 
   @Test // DRILL-2423
@@ -618,12 +584,8 @@ public class TestViewSupport extends TestBaseViewSupport {
       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();
+      errorMsgTestHelper(String.format("DROP VIEW %s.%s", TEMP_SCHEMA, testTableName),
+          "[testTableShowErrorMsg] is not a VIEW in schema [dfs_test.tmp]");
     } finally {
       File tblPath = new File(getDfsTestTmpSchemaLocation(), testTableName);
       FileUtils.deleteQuietly(tblPath);


Mime
View raw message