drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject [2/3] drill git commit: DRILL-3316: Ensure different Sql handlers go through same planning phases.
Date Mon, 22 Jun 2015 18:00:56 GMT
DRILL-3316: Ensure different Sql handlers go through same planning phases.


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

Branch: refs/heads/master
Commit: ffae1691c0cd526ed1095fbabbc0855d016790d7
Parents: 4c44466
Author: Jinfeng Ni <jni@apache.org>
Authored: Mon Jun 15 11:57:49 2015 -0700
Committer: Jinfeng Ni <jni@apache.org>
Committed: Sat Jun 20 15:42:47 2015 -0700

----------------------------------------------------------------------
 .../sql/handlers/CreateTableHandler.java        |  30 +--
 .../planner/sql/handlers/DefaultSqlHandler.java | 266 ++++++++++---------
 .../planner/sql/handlers/ExplainHandler.java    |  17 +-
 .../planner/sql/handlers/SqlHandlerUtil.java    |  22 +-
 .../exec/planner/sql/handlers/ViewHandler.java  |  27 +-
 .../exec/planner/sql/parser/SqlCreateView.java  |   2 +-
 .../exec/planner/sql/parser/SqlDropView.java    |   2 +-
 7 files changed, 188 insertions(+), 178 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/ffae1691/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 b867549..920b284 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
@@ -18,8 +18,6 @@
 package org.apache.drill.exec.planner.sql.handlers;
 
 import java.io.IOException;
-import java.math.BigDecimal;
-import java.util.AbstractList;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -27,7 +25,6 @@ import java.util.List;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import org.apache.calcite.plan.RelOptCluster;
-import org.apache.calcite.plan.RelOptUtil;
 import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rel.type.RelDataTypeField;
@@ -39,16 +36,11 @@ import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.tools.RelConversionException;
 import org.apache.calcite.tools.ValidationException;
 
-import org.apache.calcite.util.Pair;
 import org.apache.drill.common.exceptions.UserException;
-import org.apache.drill.common.expression.SchemaPath;
-import org.apache.drill.common.types.TypeProtos;
-import org.apache.drill.exec.expr.TypeHelper;
 import org.apache.drill.exec.physical.PhysicalPlan;
 import org.apache.drill.exec.physical.base.PhysicalOperator;
 import org.apache.drill.exec.planner.logical.DrillRel;
 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.physical.ProjectAllowDupPrel;
@@ -56,7 +48,6 @@ import org.apache.drill.exec.planner.physical.ProjectPrel;
 import org.apache.drill.exec.planner.physical.WriterPrel;
 import org.apache.drill.exec.planner.physical.visitor.BasePrelVisitor;
 import org.apache.drill.exec.planner.sql.DrillSqlOperator;
-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;
@@ -72,10 +63,15 @@ public class CreateTableHandler extends DefaultSqlHandler {
   @Override
   public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException,
IOException, ForemanSetupException {
     SqlCreateTable sqlCreateTable = unwrap(sqlNode, SqlCreateTable.class);
-
     final String newTblName = sqlCreateTable.getName();
+
+    final ConvertedRelNode convertedRelNode = validateAndConvert(sqlCreateTable.getQuery());
+    final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
+    final RelNode queryRelNode = convertedRelNode.getConvertedNode();
+
+
     final RelNode newTblRelNode =
-        SqlHandlerUtil.resolveNewTableRel(false, planner, sqlCreateTable.getFieldNames(),
sqlCreateTable.getQuery());
+        SqlHandlerUtil.resolveNewTableRel(false, sqlCreateTable.getFieldNames(), validatedRowType,
queryRelNode);
 
 
     final AbstractSchema drillSchema =
@@ -93,7 +89,7 @@ public class CreateTableHandler extends DefaultSqlHandler {
     log("Optiq Logical", newTblRelNodeWithPCol);
 
     // Convert the query to Drill Logical plan and insert a writer operator on top.
-    DrillRel drel = convertToDrel(newTblRelNodeWithPCol, drillSchema, newTblName, sqlCreateTable.getPartitionColumns());
+    DrillRel drel = convertToDrel(newTblRelNodeWithPCol, drillSchema, newTblName, sqlCreateTable.getPartitionColumns(),
newTblRelNode.getRowType());
     log("Drill Logical", drel);
     Prel prel = convertToPrel(drel, newTblRelNode.getRowType(), sqlCreateTable.getPartitionColumns());
     log("Drill Physical", prel);
@@ -104,14 +100,10 @@ public class CreateTableHandler extends DefaultSqlHandler {
     return plan;
   }
 
-  private DrillRel convertToDrel(RelNode relNode, AbstractSchema schema, String tableName,
List<String> partitionColumns)
-      throws RelConversionException {
-    RelNode convertedRelNode = planner.transform(DrillSqlWorker.LOGICAL_RULES,
-        relNode.getTraitSet().plus(DrillRel.DRILL_LOGICAL), relNode);
+  private DrillRel convertToDrel(RelNode relNode, AbstractSchema schema, String tableName,
List<String> partitionColumns, RelDataType queryRowType)
+      throws RelConversionException, SqlUnsupportedException {
 
-    if (convertedRelNode instanceof DrillStoreRel) {
-      throw new UnsupportedOperationException();
-    }
+    final DrillRel convertedRelNode = convertToDrel(relNode);
 
     DrillWriterRel writerRel = new DrillWriterRel(convertedRelNode.getCluster(), convertedRelNode.getTraitSet(),
         convertedRelNode, schema.createNewTable(tableName, partitionColumns));

http://git-wip-us.apache.org/repos/asf/drill/blob/ffae1691/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DefaultSqlHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DefaultSqlHandler.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DefaultSqlHandler.java
index 5e685c8..a2858b8 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DefaultSqlHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/DefaultSqlHandler.java
@@ -27,38 +27,21 @@ import org.apache.calcite.plan.RelOptUtil;
 import org.apache.calcite.plan.RelTraitSet;
 import org.apache.calcite.plan.hep.HepPlanner;
 import org.apache.calcite.rel.RelNode;
-import com.google.common.collect.ImmutableList;
-import org.apache.calcite.plan.RelOptRule;
 import org.apache.calcite.plan.hep.HepMatchOrder;
 import org.apache.calcite.plan.hep.HepProgram;
 import org.apache.calcite.plan.hep.HepProgramBuilder;
 import org.apache.calcite.rel.RelShuttleImpl;
-import org.apache.calcite.rel.core.Filter;
 import org.apache.calcite.rel.core.Join;
-import org.apache.calcite.rel.core.Project;
 import org.apache.calcite.rel.core.RelFactories;
 import org.apache.calcite.rel.core.TableFunctionScan;
 import org.apache.calcite.rel.core.TableScan;
-import org.apache.calcite.rel.logical.LogicalJoin;
-import org.apache.calcite.rel.logical.LogicalProject;
 import org.apache.calcite.rel.logical.LogicalValues;
 import org.apache.calcite.rel.metadata.CachingRelMetadataProvider;
 import org.apache.calcite.rel.metadata.ChainedRelMetadataProvider;
-import org.apache.calcite.rel.metadata.DefaultRelMetadataProvider;
 import org.apache.calcite.rel.metadata.RelMetadataProvider;
-import org.apache.calcite.rel.rules.FilterAggregateTransposeRule;
-import org.apache.calcite.rel.rules.FilterJoinRule;
-import org.apache.calcite.rel.rules.FilterMergeRule;
-import org.apache.calcite.rel.rules.FilterProjectTransposeRule;
-import org.apache.calcite.rel.rules.JoinPushThroughJoinRule;
-import org.apache.calcite.rel.rules.JoinPushTransitivePredicatesRule;
 import org.apache.calcite.rel.rules.JoinToMultiJoinRule;
 import org.apache.calcite.rel.rules.LoptOptimizeJoinRule;
-import org.apache.calcite.rel.rules.ProjectMergeRule;
 import org.apache.calcite.rel.rules.ProjectRemoveRule;
-import org.apache.calcite.rel.rules.SemiJoinFilterTransposeRule;
-import org.apache.calcite.rel.rules.SemiJoinJoinTransposeRule;
-import org.apache.calcite.rel.rules.SemiJoinProjectTransposeRule;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rex.RexBuilder;
 import org.apache.calcite.rex.RexNode;
@@ -67,7 +50,6 @@ import org.apache.calcite.sql.SqlExplainLevel;
 import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.sql.TypedSqlNode;
 import org.apache.calcite.sql.validate.SqlValidatorUtil;
-import org.apache.calcite.sql2rel.RelFieldTrimmer;
 import org.apache.calcite.tools.Planner;
 import org.apache.calcite.tools.RelConversionException;
 import org.apache.calcite.tools.ValidationException;
@@ -83,12 +65,8 @@ import org.apache.drill.exec.physical.base.AbstractPhysicalVisitor;
 import org.apache.drill.exec.physical.base.PhysicalOperator;
 import org.apache.drill.exec.physical.impl.join.JoinUtils;
 import org.apache.drill.exec.planner.cost.DrillDefaultRelMetadataProvider;
-import org.apache.drill.exec.planner.logical.DrillFilterJoinRules;
 import org.apache.drill.exec.planner.logical.DrillJoinRel;
-import org.apache.drill.exec.planner.logical.DrillMergeProjectRule;
 import org.apache.drill.exec.planner.logical.DrillProjectRel;
-import org.apache.drill.exec.planner.logical.DrillPushFilterPastProjectRule;
-import org.apache.drill.exec.planner.logical.DrillPushProjectPastFilterRule;
 import org.apache.drill.exec.planner.logical.DrillRel;
 import org.apache.drill.exec.planner.logical.DrillRelFactories;
 import org.apache.drill.exec.planner.logical.DrillScreenRel;
@@ -172,19 +150,15 @@ public class DefaultSqlHandler extends AbstractSqlHandler {
     }
   }
 
+
   @Override
   public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException,
IOException, ForemanSetupException {
-    SqlNode rewrittenSqlNode = rewrite(sqlNode);
-    TypedSqlNode validatedTypedSqlNode = validateNode(rewrittenSqlNode);
-    SqlNode validated = validatedTypedSqlNode.getSqlNode();
-    RelDataType validatedRowType = validatedTypedSqlNode.getType();
-
-    RelNode rel = convertToRel(validated);
-    rel = preprocessNode(rel);
-
+    final ConvertedRelNode convertedRelNode = validateAndConvert(sqlNode);
+    final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
+    final RelNode queryRelNode = convertedRelNode.getConvertedNode();
 
-    log("Optiq Logical", rel);
-    DrillRel drel = convertToDrel(rel, validatedRowType);
+    log("Optiq Logical", queryRelNode);
+    DrillRel drel = convertToDrel(queryRelNode, validatedRowType);
 
     log("Drill Logical", drel);
     Prel prel = convertToPrel(drel);
@@ -195,90 +169,45 @@ public class DefaultSqlHandler extends AbstractSqlHandler {
     return plan;
   }
 
-  protected DrillRel addRenamedProject(DrillRel rel, RelDataType validatedRowType) {
-    RelDataType t = rel.getRowType();
-
-    RexBuilder b = rel.getCluster().getRexBuilder();
-    List<RexNode> projections = Lists.newArrayList();
-    int projectCount = t.getFieldList().size();
-
-    for (int i =0; i < projectCount; i++) {
-      projections.add(b.makeInputRef(rel, i));
-    }
-
-    final List<String> fieldNames2 = SqlValidatorUtil.uniquify(validatedRowType.getFieldNames(),
SqlValidatorUtil.F_SUGGESTER2);
-
-    RelDataType newRowType = RexUtil.createStructType(rel.getCluster().getTypeFactory(),
projections, fieldNames2);
-
-    DrillProjectRel topProj = DrillProjectRel.create(rel.getCluster(), rel.getTraitSet(),
rel, projections, newRowType);
-
-    if (ProjectRemoveRule.isTrivial(topProj, true)) {
-      return rel;
-    } else{
-      return topProj;
-    }
-    //return RelOptUtil.createProject(rel, projections, fieldNames2);
-
-  }
-
-
-  protected TypedSqlNode validateNode(SqlNode sqlNode) throws ValidationException, RelConversionException,
ForemanSetupException {
-    TypedSqlNode typedSqlNode = planner.validateAndGetType(sqlNode);
-
-    SqlNode sqlNodeValidated = typedSqlNode.getSqlNode();
-
-    // Check if the unsupported functionality is used
-    UnsupportedOperatorsVisitor visitor = UnsupportedOperatorsVisitor.createVisitor(context);
-    try {
-      sqlNodeValidated.accept(visitor);
-    } catch (UnsupportedOperationException ex) {
-      // If the exception due to the unsupported functionalities
-      visitor.convertException();
-
-      // If it is not, let this exception move forward to higher logic
-      throw ex;
-    }
-
-    return typedSqlNode;
-  }
-
-  protected RelNode convertToRel(SqlNode node) throws RelConversionException {
-    RelNode convertedNode = planner.convert(node);
-    hepPlanner.setRoot(convertedNode);
-    RelNode rel = hepPlanner.findBestExp();
 
-    return rel;
+  /**
+   * Rewrite the parse tree. Used before validating the parse tree. Useful if a particular
statement needs to converted
+   * into another statement.
+   *
+   * @param node
+   * @return Rewritten sql parse tree
+   * @throws RelConversionException
+   */
+  protected SqlNode rewrite(SqlNode node) throws RelConversionException, ForemanSetupException
{
+    return node;
   }
 
-  protected RelNode preprocessNode(RelNode rel) throws SqlUnsupportedException {
-    /*
-     * Traverse the tree to do the following pre-processing tasks: 1. replace the convert_from,
convert_to function to
-     * actual implementations Eg: convert_from(EXPR, 'JSON') be converted to convert_fromjson(EXPR);
TODO: Ideally all
-     * function rewrites would move here instead of DrillOptiq.
-     *
-     * 2. see where the tree contains unsupported functions; throw SqlUnsupportedException
if there is any.
-     */
+  protected ConvertedRelNode validateAndConvert(SqlNode sqlNode) throws ForemanSetupException,
RelConversionException, ValidationException {
+    final SqlNode rewrittenSqlNode = rewrite(sqlNode);
+    final TypedSqlNode validatedTypedSqlNode = validateNode(rewrittenSqlNode);
+    final SqlNode validated = validatedTypedSqlNode.getSqlNode();
 
-    PreProcessLogicalRel visitor = PreProcessLogicalRel.createVisitor(planner.getTypeFactory(),
-        context.getDrillOperatorTable());
-    try {
-      rel = rel.accept(visitor);
-    } catch (UnsupportedOperationException ex) {
-      visitor.convertException();
-      throw ex;
-    }
+    RelNode rel = convertToRel(validated);
+    rel = preprocessNode(rel);
 
-    return rel;
+    return new ConvertedRelNode(rel, validatedTypedSqlNode.getType());
   }
 
-  protected DrillRel convertToDrel(RelNode relNode, RelDataType validatedRowType) throws
RelConversionException, SqlUnsupportedException {
+  /**
+   *  Given a relNode tree for SELECT statement, convert to Drill Logical RelNode tree.
+   * @param relNode
+   * @return
+   * @throws SqlUnsupportedException
+   * @throws RelConversionException
+   */
+  protected DrillRel convertToDrel(RelNode relNode) throws SqlUnsupportedException, RelConversionException
{
     try {
-      RelNode convertedRelNode;
+      final DrillRel convertedRelNode;
 
       if (! context.getPlannerSettings().isHepJoinOptEnabled()) {
-        convertedRelNode = logicalPlanningVolcano(relNode);
+        convertedRelNode = (DrillRel) logicalPlanningVolcano(relNode);
       } else {
-        convertedRelNode = logicalPlanningVolcanoAndLopt(relNode);
+        convertedRelNode = (DrillRel) logicalPlanningVolcanoAndLopt(relNode);
       }
 
       if (convertedRelNode instanceof DrillStoreRel) {
@@ -290,10 +219,7 @@ public class DefaultSqlHandler extends AbstractSqlHandler {
           context.getPlannerSettings().forceSingleMode();
         }
 
-        // Put a non-trivial topProject to ensure the final output field name is preserved,
when necessary.
-        DrillRel topPreservedNameProj = addRenamedProject((DrillRel) convertedRelNode, validatedRowType);
-        return new DrillScreenRel(topPreservedNameProj.getCluster(), topPreservedNameProj.getTraitSet(),
-            topPreservedNameProj);
+        return convertedRelNode;
       }
     } catch (RelOptPlanner.CannotPlanException ex) {
       logger.error(ex.getMessage());
@@ -306,6 +232,24 @@ public class DefaultSqlHandler extends AbstractSqlHandler {
     }
   }
 
+  /**
+   * Return Drill Logical RelNode tree for a SELECT statement, when it is executed / explained
directly.
+   *
+   * @param relNode : root RelNode corresponds to Calcite Logical RelNode.
+   * @param validatedRowType : the rowType for the final field names. A rename project may
be placed on top of the root.
+   * @return
+   * @throws RelConversionException
+   * @throws SqlUnsupportedException
+   */
+  protected DrillRel convertToDrel(RelNode relNode, RelDataType validatedRowType) throws
RelConversionException, SqlUnsupportedException {
+    final DrillRel convertedRelNode = convertToDrel(relNode);
+
+    // Put a non-trivial topProject to ensure the final output field name is preserved, when
necessary.
+    DrillRel topPreservedNameProj = addRenamedProject((DrillRel) convertedRelNode, validatedRowType);
+    return new DrillScreenRel(topPreservedNameProj.getCluster(), topPreservedNameProj.getTraitSet(),
+        topPreservedNameProj);
+  }
+
 
   protected Prel convertToPrel(RelNode drel) throws RelConversionException, SqlUnsupportedException
{
     Preconditions.checkArgument(drel.getConvention() == DrillRel.DRILL_LOGICAL);
@@ -479,18 +423,80 @@ public class DefaultSqlHandler extends AbstractSqlHandler {
 
   }
 
-  /**
-   * Rewrite the parse tree. Used before validating the parse tree. Useful if a particular
statement needs to converted
-   * into another statement.
-   *
-   * @param node
-   * @return Rewritten sql parse tree
-   * @throws RelConversionException
-   */
-  public SqlNode rewrite(SqlNode node) throws RelConversionException, ForemanSetupException
{
-    return node;
+  private TypedSqlNode validateNode(SqlNode sqlNode) throws ValidationException, RelConversionException,
ForemanSetupException {
+    TypedSqlNode typedSqlNode = planner.validateAndGetType(sqlNode);
+
+    SqlNode sqlNodeValidated = typedSqlNode.getSqlNode();
+
+    // Check if the unsupported functionality is used
+    UnsupportedOperatorsVisitor visitor = UnsupportedOperatorsVisitor.createVisitor(context);
+    try {
+      sqlNodeValidated.accept(visitor);
+    } catch (UnsupportedOperationException ex) {
+      // If the exception due to the unsupported functionalities
+      visitor.convertException();
+
+      // If it is not, let this exception move forward to higher logic
+      throw ex;
+    }
+
+    return typedSqlNode;
+  }
+
+  private RelNode convertToRel(SqlNode node) throws RelConversionException {
+    RelNode convertedNode = planner.convert(node);
+    hepPlanner.setRoot(convertedNode);
+    RelNode rel = hepPlanner.findBestExp();
+
+    return rel;
+  }
+
+  private RelNode preprocessNode(RelNode rel) throws SqlUnsupportedException {
+    /*
+     * Traverse the tree to do the following pre-processing tasks: 1. replace the convert_from,
convert_to function to
+     * actual implementations Eg: convert_from(EXPR, 'JSON') be converted to convert_fromjson(EXPR);
TODO: Ideally all
+     * function rewrites would move here instead of DrillOptiq.
+     *
+     * 2. see where the tree contains unsupported functions; throw SqlUnsupportedException
if there is any.
+     */
+
+    PreProcessLogicalRel visitor = PreProcessLogicalRel.createVisitor(planner.getTypeFactory(),
+        context.getDrillOperatorTable());
+    try {
+      rel = rel.accept(visitor);
+    } catch (UnsupportedOperationException ex) {
+      visitor.convertException();
+      throw ex;
+    }
+
+    return rel;
+  }
+
+  private DrillRel addRenamedProject(DrillRel rel, RelDataType validatedRowType) {
+    RelDataType t = rel.getRowType();
+
+    RexBuilder b = rel.getCluster().getRexBuilder();
+    List<RexNode> projections = Lists.newArrayList();
+    int projectCount = t.getFieldList().size();
+
+    for (int i =0; i < projectCount; i++) {
+      projections.add(b.makeInputRef(rel, i));
+    }
+
+    final List<String> fieldNames2 = SqlValidatorUtil.uniquify(validatedRowType.getFieldNames(),
SqlValidatorUtil.F_SUGGESTER2);
+
+    RelDataType newRowType = RexUtil.createStructType(rel.getCluster().getTypeFactory(),
projections, fieldNames2);
+
+    DrillProjectRel topProj = DrillProjectRel.create(rel.getCluster(), rel.getTraitSet(),
rel, projections, newRowType);
+
+    if (ProjectRemoveRule.isTrivial(topProj, true)) {
+      return rel;
+    } else{
+      return topProj;
+    }
   }
 
+
   private RelNode logicalPlanningVolcano(RelNode relNode) throws RelConversionException,
SqlUnsupportedException {
     return planner.transform(DrillSqlWorker.LOGICAL_RULES, relNode.getTraitSet().plus(DrillRel.DRILL_LOGICAL),
relNode);
   }
@@ -523,7 +529,7 @@ public class DefaultSqlHandler extends AbstractSqlHandler {
   /**
    * Appy Join Order Optimizations using Hep Planner.
    */
-  public RelNode getLoptJoinOrderTree(RelNode root,
+  private RelNode getLoptJoinOrderTree(RelNode root,
                                       Class<? extends Join> joinClass,
                                              RelFactories.JoinFactory joinFactory,
                                              RelFactories.FilterFactory filterFactory,
@@ -589,4 +595,24 @@ public class DefaultSqlHandler extends AbstractSqlHandler {
       return parent;
     }
   }
+
+  protected class ConvertedRelNode {
+    private final RelNode relNode;
+    private final RelDataType validatedRowType;
+
+    public ConvertedRelNode(RelNode relNode, RelDataType validatedRowType) {
+      this.relNode = relNode;
+      this.validatedRowType = validatedRowType;
+    }
+
+    public RelNode getConvertedNode() {
+      return this.relNode;
+    }
+
+    public RelDataType getValidatedRowType() {
+      return this.validatedRowType;
+    }
+  }
+
+
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/ffae1691/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ExplainHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ExplainHandler.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ExplainHandler.java
index 5924c7e..efc4b36 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ExplainHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ExplainHandler.java
@@ -20,7 +20,6 @@ package org.apache.drill.exec.planner.sql.handlers;
 import java.io.IOException;
 
 import org.apache.calcite.rel.type.RelDataType;
-import org.apache.calcite.sql.TypedSqlNode;
 import org.apache.calcite.tools.RelConversionException;
 import org.apache.calcite.tools.ValidationException;
 
@@ -53,17 +52,13 @@ public class ExplainHandler extends DefaultSqlHandler {
   }
 
   @Override
-  public PhysicalPlan getPlan(SqlNode node) throws ValidationException, RelConversionException,
IOException, ForemanSetupException {
-    SqlNode sqlNode = rewrite(node);
-    TypedSqlNode validatedTypedSqlNode = validateNode(sqlNode);
-    SqlNode validated = validatedTypedSqlNode.getSqlNode();
-    RelDataType validatedRowType = validatedTypedSqlNode.getType();
+  public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException,
IOException, ForemanSetupException {
+    final ConvertedRelNode convertedRelNode = validateAndConvert(sqlNode);
+    final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
+    final RelNode queryRelNode = convertedRelNode.getConvertedNode();
 
-    RelNode rel = convertToRel(validated);
-    rel = preprocessNode(rel);
-
-    log("Optiq Logical", rel);
-    DrillRel drel = convertToDrel(rel, validatedRowType);
+    log("Optiq Logical", queryRelNode);
+    DrillRel drel = convertToDrel(queryRelNode, validatedRowType);
     log("Drill Logical", drel);
 
     if (mode == ResultMode.LOGICAL) {

http://git-wip-us.apache.org/repos/asf/drill/blob/ffae1691/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 60287dc..9e7be7f 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
@@ -58,21 +58,18 @@ public class SqlHandlerUtil {
    *
    * @param isNewTableView Is the new table created a view? This doesn't affect the functionality,
but it helps format
    *                       better error messages.
-   * @param planner Planner instance.
    * @param tableFieldNames List of fields specified in new table/view field list. These
are the fields given just after
    *                        new table name.
    *                        Ex. CREATE TABLE newTblName(col1, medianOfCol2, avgOfCol3) AS
    *                        SELECT col1, median(col2), avg(col3) FROM sourcetbl GROUP BY
col1;
-   * @param newTableQueryDef  Sql tree of definition of the new table or view (query after
the AS keyword). This tree is
-   *                          modified, so it is responsibility of caller's to make a copy
if needed.
    * @throws ValidationException If table's fields list and field list specified in table
definition are not valid.
    * @throws RelConversionException If failed to convert the table definition into a RelNode.
    */
-  public static RelNode resolveNewTableRel(boolean isNewTableView, Planner planner, List<String>
tableFieldNames,
-      SqlNode newTableQueryDef) throws ValidationException, RelConversionException {
+  public static RelNode resolveNewTableRel(boolean isNewTableView, List<String> tableFieldNames,
+      RelDataType validatedRowtype, RelNode queryRelNode) throws ValidationException, RelConversionException
{
 
 
-    TypedSqlNode validatedSqlNodeWithType = planner.validateAndGetType(newTableQueryDef);
+//    TypedSqlNode validatedSqlNodeWithType = planner.validateAndGetType(newTableQueryDef);
 
     // Get the row type of view definition query.
     // Reason for getting the row type from validated SqlNode than RelNode is because SqlNode
-> RelNode involves
@@ -84,12 +81,11 @@ public class SqlHandlerUtil {
     //  | 0          | 0          |
     //  +------------+------------+
     // which is not desired when creating new views or tables.
-    final RelDataType queryRowType = validatedSqlNodeWithType.getType();
-    final RelNode validatedQueryRelNode = planner.convert(validatedSqlNodeWithType.getSqlNode());
+//    final RelDataType queryRowType = validatedRowtype;
 
     if (tableFieldNames.size() > 0) {
       // Field count should match.
-      if (tableFieldNames.size() != queryRowType.getFieldCount()) {
+      if (tableFieldNames.size() != validatedRowtype.getFieldCount()) {
         final String tblType = isNewTableView ? "view" : "table";
         throw UserException.validationError()
             .message("%s's field list and the %s's query field list have different counts.",
tblType, tblType)
@@ -97,7 +93,7 @@ public class SqlHandlerUtil {
       }
 
       // CTAS's query field list shouldn't have "*" when table's field list is specified.
-      for (String field : queryRowType.getFieldNames()) {
+      for (String field : validatedRowtype.getFieldNames()) {
         if (field.equals("*")) {
           final String tblType = isNewTableView ? "view" : "table";
           throw UserException.validationError()
@@ -115,13 +111,13 @@ public class SqlHandlerUtil {
       //        SELECT col1, median(col2), avg(col3) FROM sourcetbl GROUP BY col1 ;
       // Similary for CREATE VIEW.
 
-      return DrillRelOptUtil.createRename(validatedQueryRelNode, tableFieldNames);
+      return DrillRelOptUtil.createRename(queryRelNode, tableFieldNames);
     }
 
     // As the column names of the view are derived from SELECT query, make sure the query
has no duplicate column names
-    ensureNoDuplicateColumnNames(queryRowType.getFieldNames());
+    ensureNoDuplicateColumnNames(validatedRowtype.getFieldNames());
 
-    return validatedQueryRelNode;
+    return queryRelNode;
   }
 
   private static void ensureNoDuplicateColumnNames(List<String> fieldNames) throws
ValidationException {

http://git-wip-us.apache.org/repos/asf/drill/blob/ffae1691/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 0a3393e..36287a4 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
@@ -19,11 +19,10 @@ package org.apache.drill.exec.planner.sql.handlers;
 
 import java.io.IOException;
 
+import org.apache.calcite.rel.type.RelDataType;
 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;
 import org.apache.calcite.tools.RelConversionException;
 import org.apache.calcite.tools.ValidationException;
 
@@ -40,22 +39,21 @@ import org.apache.drill.exec.work.foreman.ForemanSetupException;
 import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.sql.SqlNode;
 
-public abstract class ViewHandler extends AbstractSqlHandler {
+public abstract class ViewHandler extends DefaultSqlHandler {
   static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ViewHandler.class);
 
-  protected Planner planner;
   protected QueryContext context;
 
-  public ViewHandler(Planner planner, QueryContext context) {
-    this.planner = planner;
-    this.context = context;
+  public ViewHandler(SqlHandlerConfig config) {
+    super(config);
+    this.context = config.getContext();
   }
 
   /** Handler for Create View DDL command */
   public static class CreateView extends ViewHandler {
 
-    public CreateView(Planner planner, QueryContext context) {
-      super(planner, context);
+    public CreateView(SqlHandlerConfig config) {
+      super(config);
     }
 
     @Override
@@ -67,8 +65,11 @@ public abstract class ViewHandler extends AbstractSqlHandler {
       // 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 RelNode newViewRelNode =
-          SqlHandlerUtil.resolveNewTableRel(true, planner, createView.getFieldNames(), createView.getQuery());
+      final ConvertedRelNode convertedRelNode = validateAndConvert(createView.getQuery());
+      final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
+      final RelNode queryRelNode = convertedRelNode.getConvertedNode();
+
+      final RelNode newViewRelNode = SqlHandlerUtil.resolveNewTableRel(true, createView.getFieldNames(),
validatedRowType, queryRelNode);
 
       final SchemaPlus defaultSchema = context.getNewDefaultSchema();
       final AbstractSchema drillSchema = SchemaUtilites.resolveToMutableDrillSchema(defaultSchema,
createView.getSchemaPath());
@@ -107,8 +108,8 @@ public abstract class ViewHandler extends AbstractSqlHandler {
 
   /** Handler for Drop View DDL command. */
   public static class DropView extends ViewHandler {
-    public DropView(QueryContext context) {
-      super(null, context);
+    public DropView(SqlHandlerConfig config) {
+      super(config);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/drill/blob/ffae1691/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlCreateView.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlCreateView.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlCreateView.java
index a140829..cc6118a 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlCreateView.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/parser/SqlCreateView.java
@@ -96,7 +96,7 @@ public class SqlCreateView extends DrillSqlCall {
 
   @Override
   public AbstractSqlHandler getSqlHandler(SqlHandlerConfig config) {
-    return new ViewHandler.CreateView(config.getPlanner(), config.getContext());
+    return new ViewHandler.CreateView(config);
   }
 
   public List<String> getSchemaPath() {

http://git-wip-us.apache.org/repos/asf/drill/blob/ffae1691/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 473dbcb..0e775b8 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
@@ -73,7 +73,7 @@ public class SqlDropView extends DrillSqlCall {
 
   @Override
   public AbstractSqlHandler getSqlHandler(SqlHandlerConfig config) {
-    return new DropView(config.getContext());
+    return new DropView(config);
   }
 
   public List<String> getSchemaPath() {


Mime
View raw message