You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by ja...@apache.org on 2014/04/20 03:18:15 UTC

[07/51] [abbrv] DRILL-450: Add exchange rules, move from BasicOptimizer to Optiq

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStoreRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStoreRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStoreRel.java
deleted file mode 100644
index ffbfa94..0000000
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStoreRel.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- * 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.common;
-
-import java.util.List;
-
-import net.hydromatic.optiq.prepare.Prepare.CatalogReader;
-
-import org.apache.drill.common.logical.data.LogicalOperator;
-import org.apache.drill.exec.planner.logical.DrillImplementor;
-import org.apache.drill.exec.planner.logical.DrillRel;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.TableModificationRelBase;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptTable;
-import org.eigenbase.relopt.RelTraitSet;
-
-public class DrillStoreRel extends TableModificationRelBase implements DrillRel{
-  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillStoreRel.class);
-
-  protected DrillStoreRel(RelOptCluster cluster, RelTraitSet traits, RelOptTable table, CatalogReader catalogReader,
-      RelNode child, Operation operation, List<String> updateColumnList, boolean flattened) {
-    super(cluster, traits, table, catalogReader, child, operation, updateColumnList, flattened);
-    
-  }
-
-  @Override
-  public LogicalOperator implement(DrillImplementor implementor) {
-    return null;
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStoreRelBase.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStoreRelBase.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStoreRelBase.java
new file mode 100644
index 0000000..c6e3ae1
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStoreRelBase.java
@@ -0,0 +1,42 @@
+/**
+ * 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.common;
+
+import java.util.List;
+
+import net.hydromatic.optiq.prepare.Prepare.CatalogReader;
+
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.rel.TableModificationRelBase;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptTable;
+import org.eigenbase.relopt.RelTraitSet;
+
+/**
+ * Base class for logical and physical Store implemented in Drill
+ */
+public abstract class DrillStoreRelBase extends TableModificationRelBase implements DrillRelNode {
+  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillStoreRelBase.class);
+
+  protected DrillStoreRelBase(RelOptCluster cluster, RelTraitSet traits, RelOptTable table, CatalogReader catalogReader,
+      RelNode child, Operation operation, List<String> updateColumnList, boolean flattened) {
+    super(cluster, traits, table, catalogReader, child, operation, updateColumnList, flattened);
+    
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillUnionRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillUnionRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillUnionRel.java
deleted file mode 100644
index 2794560..0000000
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillUnionRel.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/**
- * 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.common;
-
-import java.util.List;
-
-import net.hydromatic.linq4j.Ord;
-
-import org.apache.drill.common.logical.data.Limit;
-import org.apache.drill.common.logical.data.LogicalOperator;
-import org.apache.drill.common.logical.data.Union;
-import org.apache.drill.exec.planner.logical.DrillImplementor;
-import org.apache.drill.exec.planner.logical.DrillRel;
-import org.apache.drill.exec.planner.torel.ConversionContext;
-import org.eigenbase.rel.InvalidRelException;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.UnionRelBase;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptCost;
-import org.eigenbase.relopt.RelOptPlanner;
-import org.eigenbase.relopt.RelTraitSet;
-
-/**
- * Union implemented in Drill.
- */
-public class DrillUnionRel extends UnionRelBase implements DrillRel {
-  /** Creates a DrillUnionRel. */
-  public DrillUnionRel(RelOptCluster cluster, RelTraitSet traits,
-      List<RelNode> inputs, boolean all) {
-    super(cluster, traits, inputs, all);
-  }
-
-  @Override
-  public DrillUnionRel copy(RelTraitSet traitSet, List<RelNode> inputs,
-      boolean all) {
-    return new DrillUnionRel(getCluster(), traitSet, inputs, all);
-  }
-
-  @Override
-  public RelOptCost computeSelfCost(RelOptPlanner planner) {
-    // divide cost by two to ensure cheaper than EnumerableDrillRel
-    return super.computeSelfCost(planner).multiplyBy(.5);
-  }
-
-  @Override
-  public LogicalOperator implement(DrillImplementor implementor) {
-    Union.Builder builder = Union.builder();
-    for (Ord<RelNode> input : Ord.zip(inputs)) {
-      builder.addInput(implementor.visitChild(this, input.i, input.e));
-    }
-    builder.setDistinct(!all);
-    return builder.build();
-  }
-  
-  public static DrillUnionRel convert(Union union, ConversionContext context) throws InvalidRelException{
-    throw new UnsupportedOperationException();
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillUnionRelBase.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillUnionRelBase.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillUnionRelBase.java
new file mode 100644
index 0000000..a16b8ee
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillUnionRelBase.java
@@ -0,0 +1,37 @@
+/**
+ * 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.common;
+
+import java.util.List;
+
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.rel.UnionRelBase;
+import org.eigenbase.relopt.RelOptCluster;
+
+import org.eigenbase.relopt.RelTraitSet;
+
+/**
+ * Base class for logical and physical Union implemented in Drill
+ */
+public abstract class DrillUnionRelBase extends UnionRelBase implements DrillRelNode {
+ 
+  public DrillUnionRelBase(RelOptCluster cluster, RelTraitSet traits,
+      List<RelNode> inputs, boolean all) {
+    super(cluster, traits, inputs, all);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillAggregateRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillAggregateRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillAggregateRel.java
index e1337ef..c2833c1 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillAggregateRel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillAggregateRel.java
@@ -30,6 +30,7 @@ import org.apache.drill.common.expression.LogicalExpression;
 import org.apache.drill.common.expression.ValueExpressions;
 import org.apache.drill.common.logical.data.GroupingAggregate;
 import org.apache.drill.common.logical.data.LogicalOperator;
+import org.apache.drill.exec.planner.common.DrillAggregateRelBase;
 import org.apache.drill.exec.planner.torel.ConversionContext;
 import org.eigenbase.rel.AggregateCall;
 import org.eigenbase.rel.AggregateRelBase;
@@ -43,7 +44,7 @@ import com.google.common.collect.Lists;
 /**
  * Aggregation implemented in Drill.
  */
-public class DrillAggregateRel extends AggregateRelBase implements DrillRel {
+public class DrillAggregateRel extends DrillAggregateRelBase implements DrillRel {
   /** Creates a DrillAggregateRel. */
   public DrillAggregateRel(RelOptCluster cluster, RelTraitSet traits, RelNode child, BitSet groupSet,
       List<AggregateCall> aggCalls) throws InvalidRelException {
@@ -55,10 +56,9 @@ public class DrillAggregateRel extends AggregateRelBase implements DrillRel {
     }
   }
 
-  @Override
-  public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
+  public AggregateRelBase copy(RelTraitSet traitSet, RelNode input, BitSet groupSet, List<AggregateCall> aggCalls) {
     try {
-      return new DrillAggregateRel(getCluster(), traitSet, sole(inputs), getGroupSet(), aggCalls);
+      return new DrillAggregateRel(getCluster(), traitSet, input, getGroupSet(), aggCalls);
     } catch (InvalidRelException e) {
       throw new AssertionError(e);
     }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillFilterRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillFilterRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillFilterRel.java
index 867def4..fee62c9 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillFilterRel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillFilterRel.java
@@ -21,7 +21,7 @@ import java.util.List;
 
 import org.apache.drill.common.logical.data.Filter;
 import org.apache.drill.common.logical.data.LogicalOperator;
-import org.apache.drill.exec.planner.common.BaseFilterRel;
+import org.apache.drill.exec.planner.common.DrillFilterRelBase;
 import org.apache.drill.exec.planner.torel.ConversionContext;
 import org.eigenbase.rel.InvalidRelException;
 import org.eigenbase.rel.RelNode;
@@ -30,7 +30,7 @@ import org.eigenbase.relopt.RelTraitSet;
 import org.eigenbase.rex.RexNode;
 
 
-public class DrillFilterRel extends BaseFilterRel implements DrillRel {
+public class DrillFilterRel extends DrillFilterRelBase implements DrillRel {
   protected DrillFilterRel(RelOptCluster cluster, RelTraitSet traits, RelNode child, RexNode condition) {
     super(DRILL_LOGICAL, cluster, traits, child, condition);
   }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillJoinRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillJoinRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillJoinRel.java
index 16e9d47..31a8e6e 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillJoinRel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillJoinRel.java
@@ -27,9 +27,9 @@ import org.apache.drill.common.logical.data.Join;
 import org.apache.drill.common.logical.data.JoinCondition;
 import org.apache.drill.common.logical.data.LogicalOperator;
 import org.apache.drill.common.logical.data.Project;
+import org.apache.drill.exec.planner.common.DrillJoinRelBase;
 import org.apache.drill.exec.planner.torel.ConversionContext;
 import org.eigenbase.rel.InvalidRelException;
-import org.eigenbase.rel.JoinRelBase;
 import org.eigenbase.rel.JoinRelType;
 import org.eigenbase.rel.RelNode;
 import org.eigenbase.relopt.RelOptCluster;
@@ -45,14 +45,11 @@ import org.eigenbase.util.Pair;
 /**
  * Join implemented in Drill.
  */
-public class DrillJoinRel extends JoinRelBase implements DrillRel {
-  private final List<Integer> leftKeys = new ArrayList<>();
-  private final List<Integer> rightKeys = new ArrayList<>();
-
+public class DrillJoinRel extends DrillJoinRelBase implements DrillRel {
   /** Creates a DrillJoinRel. */
   public DrillJoinRel(RelOptCluster cluster, RelTraitSet traits, RelNode left, RelNode right, RexNode condition,
       JoinRelType joinType) throws InvalidRelException {
-    super(cluster, traits, left, right, condition, joinType, Collections.<String> emptySet());
+    super(cluster, traits, left, right, condition, joinType);
 
     RexNode remaining = RelOptUtil.splitJoinCondition(left, right, condition, leftKeys, rightKeys);
     if (!remaining.isAlwaysTrue()) {
@@ -88,7 +85,8 @@ public class DrillJoinRel extends JoinRelBase implements DrillRel {
     for (Pair<Integer, Integer> pair : Pair.zip(leftKeys, rightKeys)) {
       builder.addCondition("==", new FieldReference(leftFields.get(pair.left)), new FieldReference(rightFields.get(pair.right)));
     }
-    return builder.build();
+    
+    return builder.build();  
   }
 
   /**
@@ -146,20 +144,4 @@ public class DrillJoinRel extends JoinRelBase implements DrillRel {
     return new DrillJoinRel(context.getCluster(), context.getLogicalTraits(), left, right, rexCondition, join.getJoinType());
   }
   
-  
-  /**
-   * Returns whether there are any elements in common between left and right.
-   */
-  private static <T> boolean intersects(List<T> left, List<T> right) {
-    return new HashSet<>(left).removeAll(right);
-  }
-
-  private boolean uniqueFieldNames(RelDataType rowType) {
-    return isUnique(rowType.getFieldNames());
-  }
-
-  private static <T> boolean isUnique(List<T> list) {
-    return new HashSet<>(list).size() == list.size();
-  }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillLimitRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillLimitRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillLimitRel.java
index 137df72..9f08117 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillLimitRel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillLimitRel.java
@@ -22,26 +22,22 @@ import java.util.List;
 
 import org.apache.drill.common.logical.data.Limit;
 import org.apache.drill.common.logical.data.LogicalOperator;
+import org.apache.drill.exec.planner.common.DrillLimitRelBase;
 import org.apache.drill.exec.planner.logical.DrillImplementor;
 import org.apache.drill.exec.planner.logical.DrillRel;
 import org.apache.drill.exec.planner.torel.ConversionContext;
 import org.eigenbase.rel.InvalidRelException;
 import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.SingleRel;
 import org.eigenbase.relopt.RelOptCluster;
 import org.eigenbase.relopt.RelTraitSet;
 import org.eigenbase.rex.RexLiteral;
 import org.eigenbase.rex.RexNode;
 import org.eigenbase.sql.type.SqlTypeName;
 
-public class DrillLimitRel extends SingleRel implements DrillRel {
-  private RexNode offset;
-  private RexNode fetch;
+public class DrillLimitRel extends DrillLimitRelBase implements DrillRel {
 
   public DrillLimitRel(RelOptCluster cluster, RelTraitSet traitSet, RelNode child, RexNode offset, RexNode fetch) {
-    super(cluster, traitSet, child);
-    this.offset = offset;
-    this.fetch = fetch;
+    super(cluster, traitSet, child, offset, fetch);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillLimitRule.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillLimitRule.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillLimitRule.java
index f1d3bae..d8396b5 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillLimitRule.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillLimitRule.java
@@ -17,7 +17,6 @@
  */
 package org.apache.drill.exec.planner.logical;
 
-import org.apache.drill.exec.planner.common.DrillLimitRel;
 import org.eigenbase.rel.RelNode;
 import org.eigenbase.rel.SortRel;
 import org.eigenbase.relopt.Convention;

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillProjectRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillProjectRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillProjectRel.java
index eb9086d..ae777cb 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillProjectRel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillProjectRel.java
@@ -23,9 +23,10 @@ import java.util.List;
 import org.apache.drill.common.logical.data.LogicalOperator;
 import org.apache.drill.common.logical.data.NamedExpression;
 import org.apache.drill.common.logical.data.Project;
-import org.apache.drill.exec.planner.common.BaseProjectRel;
+import org.apache.drill.exec.planner.common.DrillProjectRelBase;
 import org.apache.drill.exec.planner.torel.ConversionContext;
 import org.eigenbase.rel.InvalidRelException;
+import org.eigenbase.rel.ProjectRelBase;
 import org.eigenbase.rel.RelNode;
 import org.eigenbase.relopt.RelOptCluster;
 import org.eigenbase.relopt.RelTraitSet;
@@ -41,15 +42,15 @@ import com.google.common.collect.Lists;
 /**
  * Project implemented in Drill.
  */
-public class DrillProjectRel extends BaseProjectRel implements DrillRel {
+public class DrillProjectRel extends DrillProjectRelBase implements DrillRel {
   protected DrillProjectRel(RelOptCluster cluster, RelTraitSet traits, RelNode child, List<RexNode> exps,
       RelDataType rowType) {
     super(DRILL_LOGICAL, cluster, traits, child, exps, rowType);
   }
 
-  @Override
-  public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
-    return new DrillProjectRel(getCluster(), traitSet, sole(inputs), new ArrayList<RexNode>(exps), rowType);
+
+  public ProjectRelBase copy(RelTraitSet traitSet, RelNode input, List<RexNode> exps, RelDataType rowType) {
+    return new DrillProjectRel(getCluster(), traitSet, input, exps, rowType);
   }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillRel.java
index 153b147..7eca54e 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillRel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillRel.java
@@ -18,16 +18,16 @@
 package org.apache.drill.exec.planner.logical;
 
 import org.apache.drill.common.logical.data.LogicalOperator;
-import org.eigenbase.rel.RelNode;
+import org.apache.drill.exec.planner.common.DrillRelNode;
 import org.eigenbase.relopt.Convention;
 
 /**
  * Relational expression that is implemented in Drill.
  */
-public interface DrillRel extends RelNode {
+public interface DrillRel extends DrillRelNode {
   /** Calling convention for relational expressions that are "implemented" by
    * generating Drill logical plans. */
-  Convention DRILL_LOGICAL = new Convention.Impl("DRILL", DrillRel.class);
+  Convention DRILL_LOGICAL = new Convention.Impl("LOGICAL", DrillRel.class);
 
   LogicalOperator implement(DrillImplementor implementor);
 }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillRuleSets.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillRuleSets.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillRuleSets.java
index 5bd1386..9ab5308 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillRuleSets.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillRuleSets.java
@@ -21,23 +21,17 @@ import java.util.Iterator;
 
 import net.hydromatic.optiq.tools.RuleSet;
 
+import org.apache.drill.exec.planner.physical.FilterPrule;
+import org.apache.drill.exec.planner.physical.LimitPrule;
+import org.apache.drill.exec.planner.physical.MergeJoinPrule;
 import org.apache.drill.exec.planner.physical.ProjectPrule;
 import org.apache.drill.exec.planner.physical.ScanPrule;
-import org.eigenbase.rel.rules.MergeProjectRule;
-import org.eigenbase.rel.rules.PushFilterPastJoinRule;
-import org.eigenbase.rel.rules.PushFilterPastProjectRule;
-import org.eigenbase.rel.rules.PushJoinThroughJoinRule;
-import org.eigenbase.rel.rules.PushSortPastProjectRule;
-import org.eigenbase.rel.rules.ReduceAggregatesRule;
-import org.eigenbase.rel.rules.RemoveDistinctAggregateRule;
-import org.eigenbase.rel.rules.RemoveDistinctRule;
-import org.eigenbase.rel.rules.RemoveSortRule;
-import org.eigenbase.rel.rules.RemoveTrivialCalcRule;
-import org.eigenbase.rel.rules.RemoveTrivialProjectRule;
-import org.eigenbase.rel.rules.SwapJoinRule;
-import org.eigenbase.rel.rules.TableAccessRule;
-import org.eigenbase.rel.rules.UnionToDistinctRule;
+import org.apache.drill.exec.planner.physical.ScreenPrule;
+import org.apache.drill.exec.planner.physical.SortConvertPrule;
+import org.apache.drill.exec.planner.physical.SortPrule;
+import org.apache.drill.exec.planner.physical.StreamAggPrule;
 import org.eigenbase.relopt.RelOptRule;
+import org.eigenbase.relopt.volcano.AbstractConverter;
 import org.eigenbase.relopt.volcano.AbstractConverter.ExpandConversionRule;
 
 import com.google.common.collect.ImmutableSet;
@@ -59,17 +53,26 @@ public class DrillRuleSets {
       ));
   
   public static final RuleSet DRILL_PHYSICAL_MEM = new DrillRuleSet(ImmutableSet.of( //
-      DrillScanRule.INSTANCE,
-      DrillFilterRule.INSTANCE,
-      DrillProjectRule.INSTANCE,
-      DrillAggregateRule.INSTANCE,
+//      DrillScanRule.INSTANCE,
+//      DrillFilterRule.INSTANCE,
+//      DrillProjectRule.INSTANCE,
+//      DrillAggregateRule.INSTANCE,
+//
+//      DrillLimitRule.INSTANCE,
+//      DrillSortRule.INSTANCE,
+//      DrillJoinRule.INSTANCE,
+//      DrillUnionRule.INSTANCE,
 
-      DrillLimitRule.INSTANCE,
-      DrillSortRule.INSTANCE,
-      DrillJoinRule.INSTANCE,
-      DrillUnionRule.INSTANCE,
+      SortConvertPrule.INSTANCE,
+      SortPrule.INSTANCE,
       ProjectPrule.INSTANCE,
-      ScanPrule.INSTANCE
+      ScanPrule.INSTANCE,
+      ScreenPrule.INSTANCE,
+      ExpandConversionRule.INSTANCE,
+      StreamAggPrule.INSTANCE,
+      MergeJoinPrule.INSTANCE,
+      FilterPrule.INSTANCE,
+      LimitPrule.INSTANCE
 
 //    ExpandConversionRule.instance,
 //    SwapJoinRule.instance,

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRel.java
index 2629c77..619e76d 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRel.java
@@ -20,7 +20,7 @@ package org.apache.drill.exec.planner.logical;
 import org.apache.drill.common.JSONOptions;
 import org.apache.drill.common.logical.data.LogicalOperator;
 import org.apache.drill.common.logical.data.Scan;
-import org.apache.drill.exec.planner.common.BaseScanRel;
+import org.apache.drill.exec.planner.common.DrillScanRelBase;
 import org.apache.drill.exec.planner.torel.ConversionContext;
 import org.eigenbase.relopt.RelOptCluster;
 import org.eigenbase.relopt.RelOptTable;
@@ -29,14 +29,14 @@ import org.eigenbase.relopt.RelTraitSet;
 /**
  * GroupScan of a Drill table.
  */
-public class DrillScanRel extends BaseScanRel implements DrillRel {
-  private final DrillTable drillTable;
+public class DrillScanRel extends DrillScanRelBase implements DrillRel {
+  //private final DrillTable drillTable;
 
   /** Creates a DrillScan. */
   public DrillScanRel(RelOptCluster cluster, RelTraitSet traits, RelOptTable table) {
     super(DRILL_LOGICAL, cluster, traits, table);
-    this.drillTable = table.unwrap(DrillTable.class);
-    assert drillTable != null;
+    //this.drillTable = table.unwrap(DrillTable.class);
+    //assert drillTable != null;
   }
 
   public LogicalOperator implement(DrillImplementor implementor) {

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRule.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRule.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRule.java
index e472daa..4896ac9 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRule.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScanRule.java
@@ -19,7 +19,7 @@ package org.apache.drill.exec.planner.logical;
 
 import net.hydromatic.optiq.rules.java.JavaRules.EnumerableTableAccessRel;
 
-import org.apache.drill.exec.planner.common.BaseScanRel;
+import org.apache.drill.exec.planner.common.DrillScanRelBase;
 import org.eigenbase.relopt.Convention;
 import org.eigenbase.relopt.RelOptRule;
 import org.eigenbase.relopt.RelOptRuleCall;

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScreenRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScreenRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScreenRel.java
index abfcd10..a63cae0 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScreenRel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillScreenRel.java
@@ -21,14 +21,14 @@ import java.util.List;
 
 import org.apache.drill.common.logical.data.LogicalOperator;
 import org.apache.drill.common.logical.data.Store;
-import org.apache.drill.exec.planner.common.BaseScreenRel;
+import org.apache.drill.exec.planner.common.DrillScreenRelBase;
 import org.eigenbase.rel.RelNode;
 import org.eigenbase.relopt.RelOptCluster;
 import org.eigenbase.relopt.RelTraitSet;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class DrillScreenRel extends BaseScreenRel implements DrillRel {
+public class DrillScreenRel extends DrillScreenRelBase implements DrillRel {
   private static final Logger logger = LoggerFactory.getLogger(DrillScreenRel.class);
 
   public DrillScreenRel(RelOptCluster cluster, RelTraitSet traitSet, RelNode input) {

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillStoreRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillStoreRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillStoreRel.java
index 8f9841c..830c34b 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillStoreRel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillStoreRel.java
@@ -22,15 +22,15 @@ import java.util.List;
 import net.hydromatic.optiq.prepare.Prepare.CatalogReader;
 
 import org.apache.drill.common.logical.data.LogicalOperator;
+import org.apache.drill.exec.planner.common.DrillStoreRelBase;
 import org.apache.drill.exec.planner.logical.DrillImplementor;
 import org.apache.drill.exec.planner.logical.DrillRel;
 import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.TableModificationRelBase;
 import org.eigenbase.relopt.RelOptCluster;
 import org.eigenbase.relopt.RelOptTable;
 import org.eigenbase.relopt.RelTraitSet;
 
-public class DrillStoreRel extends TableModificationRelBase implements DrillRel{
+public class DrillStoreRel extends DrillStoreRelBase implements DrillRel{
   static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillStoreRel.class);
 
   protected DrillStoreRel(RelOptCluster cluster, RelTraitSet traits, RelOptTable table, CatalogReader catalogReader,

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillTable.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillTable.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillTable.java
index c111306..8e6b914 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillTable.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillTable.java
@@ -27,7 +27,6 @@ import net.hydromatic.optiq.Table;
 import org.apache.drill.common.JSONOptions;
 import org.apache.drill.common.logical.StoragePluginConfig;
 import org.apache.drill.exec.physical.base.GroupScan;
-import org.apache.drill.exec.planner.common.BaseScanRel;
 import org.apache.drill.exec.store.StoragePlugin;
 import org.eigenbase.rel.RelNode;
 import org.eigenbase.relopt.RelOptTable;
@@ -39,15 +38,24 @@ public abstract class DrillTable implements Table{
   public final StoragePluginConfig storageEngineConfig;
   private Object selection;
   private StoragePlugin plugin;
+  private GroupScan scan; 
   
   /** Creates a DrillTable. */
   public DrillTable(String storageEngineName, StoragePlugin plugin, Object selection) {
     this.selection = selection;
+    this.plugin = plugin;
     
     this.storageEngineConfig = plugin.getConfig();
     this.storageEngineName = storageEngineName;
   }
 
+  public GroupScan getGroupScan() throws IOException{
+    if(scan == null){
+      this.scan = plugin.getPhysicalScan(new JSONOptions(selection));
+    }
+    return scan;
+  }
+  
   public StoragePluginConfig getStorageEngineConfig(){
     return storageEngineConfig;
   }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillUnionRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillUnionRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillUnionRel.java
index c366341..93c4ca7 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillUnionRel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillUnionRel.java
@@ -21,15 +21,13 @@ import java.util.List;
 
 import net.hydromatic.linq4j.Ord;
 
-import org.apache.drill.common.logical.data.Limit;
 import org.apache.drill.common.logical.data.LogicalOperator;
 import org.apache.drill.common.logical.data.Union;
+import org.apache.drill.exec.planner.common.DrillUnionRelBase;
 import org.apache.drill.exec.planner.logical.DrillImplementor;
-import org.apache.drill.exec.planner.logical.DrillRel;
 import org.apache.drill.exec.planner.torel.ConversionContext;
 import org.eigenbase.rel.InvalidRelException;
 import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.UnionRelBase;
 import org.eigenbase.relopt.RelOptCluster;
 import org.eigenbase.relopt.RelOptCost;
 import org.eigenbase.relopt.RelOptPlanner;
@@ -38,7 +36,7 @@ import org.eigenbase.relopt.RelTraitSet;
 /**
  * Union implemented in Drill.
  */
-public class DrillUnionRel extends UnionRelBase implements DrillRel {
+public class DrillUnionRel extends DrillUnionRelBase implements DrillRel {
   /** Creates a DrillUnionRel. */
   public DrillUnionRel(RelOptCluster cluster, RelTraitSet traits,
       List<RelNode> inputs, boolean all) {

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillUnionRule.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillUnionRule.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillUnionRule.java
index 99f2c98..25515c0 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillUnionRule.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillUnionRule.java
@@ -17,7 +17,7 @@
  */
 package org.apache.drill.exec.planner.logical;
 
-import org.apache.drill.exec.planner.common.DrillUnionRel;
+import org.apache.drill.exec.planner.common.DrillUnionRelBase;
 import org.eigenbase.rel.UnionRel;
 import org.eigenbase.rel.RelNode;
 import org.eigenbase.relopt.*;
@@ -26,7 +26,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 /**
- * Rule that converts a {@link UnionRel} to a {@link DrillUnionRel}, implemented by a "union" operation.
+ * Rule that converts a {@link UnionRel} to a {@link DrillUnionRelBase}, implemented by a "union" operation.
  */
 public class DrillUnionRule extends RelOptRule {
   public static final RelOptRule INSTANCE = new DrillUnionRule();

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DynamicDrillTable.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DynamicDrillTable.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DynamicDrillTable.java
index d309405..b830b1a 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DynamicDrillTable.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DynamicDrillTable.java
@@ -18,7 +18,9 @@
 package org.apache.drill.exec.planner.logical;
 
 import org.apache.drill.common.logical.StoragePluginConfig;
-import org.apache.drill.exec.store.dfs.FileSystemPlugin;
+import org.apache.drill.exec.planner.types.RelDataTypeDrillImpl;
+import org.apache.drill.exec.planner.types.RelDataTypeHolder;
+import org.apache.drill.exec.store.StoragePlugin;
 import org.eigenbase.reltype.RelDataType;
 import org.eigenbase.reltype.RelDataTypeFactory;
 
@@ -27,7 +29,7 @@ public class DynamicDrillTable extends DrillTable{
 
   private RelDataTypeHolder holder = new RelDataTypeHolder();
   
-  public DynamicDrillTable(FileSystemPlugin plugin, String storageEngineName, Object selection, StoragePluginConfig storageEngineConfig) {
+  public DynamicDrillTable(StoragePlugin plugin, String storageEngineName, Object selection, StoragePluginConfig storageEngineConfig) {
     super(storageEngineName, plugin, selection);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/EnumerableDrillRule.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/EnumerableDrillRule.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/EnumerableDrillRule.java
index d54ee79..efc29f4 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/EnumerableDrillRule.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/EnumerableDrillRule.java
@@ -19,12 +19,12 @@ package org.apache.drill.exec.planner.logical;
 
 import net.hydromatic.optiq.rules.java.EnumerableConvention;
 
-import org.apache.drill.exec.planner.common.BaseScreenRel;
+import org.apache.drill.exec.planner.common.DrillScreenRelBase;
 import org.eigenbase.rel.RelNode;
 import org.eigenbase.rel.convert.ConverterRule;
 
 /**
- * Rule that converts any Drill relational expression to enumerable format by adding a {@link BaseScreenRel}.
+ * Rule that converts any Drill relational expression to enumerable format by adding a {@link DrillScreenRelBase}.
  */
 public class EnumerableDrillRule extends ConverterRule {
 

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/RelDataTypeDrillImpl.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/RelDataTypeDrillImpl.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/RelDataTypeDrillImpl.java
deleted file mode 100644
index bc0f34e..0000000
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/RelDataTypeDrillImpl.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/**
- * 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.logical;
-
-import java.util.List;
-
-import org.eigenbase.reltype.RelDataTypeFactory;
-import org.eigenbase.reltype.RelDataTypeField;
-import org.eigenbase.reltype.RelDataTypeImpl;
-import org.eigenbase.sql.type.SqlTypeName;
-
-/* We use an instance of this class as the row type for
- * Drill table. Since we don't know the schema before hand
- * whenever optiq requires us to validate that a field exists
- * we always return true and indicate that the type of that
- * field is 'ANY'
- */
-public class RelDataTypeDrillImpl extends RelDataTypeImpl {
-
-    private final RelDataTypeFactory typeFactory;
-    private final RelDataTypeHolder holder;
-    
-    public RelDataTypeDrillImpl(RelDataTypeHolder holder, RelDataTypeFactory typeFactory) {
-        this.typeFactory = typeFactory;
-        this.holder = holder;
-        computeDigest();
-    }
-    
-    @Override
-    public List<RelDataTypeField> getFieldList() {
-      return holder.getFieldList(typeFactory);
-    }
-    
-    @Override
-    public int getFieldCount() {
-      return holder.getFieldCount();
-    }
-
-    @Override
-    public RelDataTypeField getField(String fieldName, boolean caseSensitive) {
-      return holder.getField(typeFactory, fieldName);
-    }
-
-    @Override
-    public List<String> getFieldNames() {
-      return holder.getFieldNames();
-    }
-
-    @Override
-    public SqlTypeName getSqlTypeName() {
-        return null;
-    }
-
-    @Override
-    protected void generateTypeString(StringBuilder sb, boolean withDetail) {
-       sb.append("DrillRecordRow");
-    }
-
-    @Override
-    public boolean isStruct() {
-        return true;
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/RelDataTypeHolder.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/RelDataTypeHolder.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/RelDataTypeHolder.java
deleted file mode 100644
index 038f3de..0000000
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/RelDataTypeHolder.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/**
- * 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.logical;
-
-import java.util.List;
-
-import org.eigenbase.reltype.RelDataTypeFactory;
-import org.eigenbase.reltype.RelDataTypeField;
-import org.eigenbase.reltype.RelDataTypeFieldImpl;
-import org.eigenbase.sql.type.SqlTypeName;
-
-import com.google.common.collect.Lists;
-
-public class RelDataTypeHolder {
-  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(RelDataTypeHolder.class);
-
-  List<String> fieldNames = Lists.newArrayList();
-
-  public List<RelDataTypeField> getFieldList(RelDataTypeFactory typeFactory) {
-
-    addStarIfEmpty();
-
-    List<RelDataTypeField> fields = Lists.newArrayList();
-
-    int i = 0;
-    for (String fieldName : fieldNames) {
-
-      RelDataTypeField field = new RelDataTypeFieldImpl(fieldName, i, typeFactory.createSqlType(SqlTypeName.ANY));
-      fields.add(field);
-      i++;
-    }
-
-    return fields;
-  }
-
-  public int getFieldCount() {
-    addStarIfEmpty();
-    return fieldNames.size();
-  }
-
-  private void addStarIfEmpty(){
-    if (fieldNames.isEmpty()) fieldNames.add("*");
-  }
-  
-  public RelDataTypeField getField(RelDataTypeFactory typeFactory, String fieldName) {
-
-    /* First check if this field name exists in our field list */
-    int i = 0;
-    for (String name : fieldNames) {
-      if (name.equalsIgnoreCase(fieldName)) {
-        return new RelDataTypeFieldImpl(name, i, typeFactory.createSqlType(SqlTypeName.ANY));
-      }
-      i++;
-    }
-
-    /* This field does not exist in our field list add it */
-    RelDataTypeField newField = new RelDataTypeFieldImpl(fieldName, fieldNames.size(),
-        typeFactory.createSqlType(SqlTypeName.ANY));
-
-    /* Add the name to our list of field names */
-    fieldNames.add(fieldName);
-
-    return newField;
-  }
-
-  public List<String> getFieldNames() {
-    addStarIfEmpty();
-    return fieldNames;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/DrillDistributionTrait.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/DrillDistributionTrait.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/DrillDistributionTrait.java
new file mode 100644
index 0000000..aa2dad5
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/DrillDistributionTrait.java
@@ -0,0 +1,142 @@
+/**
+ * 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.physical;
+
+import org.eigenbase.rel.RelFieldCollation;
+import org.eigenbase.relopt.RelTrait;
+import org.eigenbase.relopt.RelTraitDef;
+
+import com.google.common.collect.ImmutableList;
+
+public class DrillDistributionTrait implements RelTrait {
+  public static enum DistributionType {SINGLETON, HASH_DISTRIBUTED, RANGE_DISTRIBUTED, RANDOM_DISTRIBUTED,
+                                       ROUND_ROBIN_DISTRIBUTED, BROADCAST_DISTRIBUTED, ANY};
+
+  public static DrillDistributionTrait SINGLETON = new DrillDistributionTrait(DistributionType.SINGLETON);
+  public static DrillDistributionTrait RANDOM_DISTRIBUTED = new DrillDistributionTrait(DistributionType.RANDOM_DISTRIBUTED);
+  public static DrillDistributionTrait ANY = new DrillDistributionTrait(DistributionType.ANY);
+  
+  public static DrillDistributionTrait DEFAULT = ANY;
+  
+  private DistributionType type;  
+  private final ImmutableList<DistributionField> fields;
+  
+  private DrillDistributionTrait(DistributionType type) {
+    assert (type == DistributionType.SINGLETON || type == DistributionType.RANDOM_DISTRIBUTED || type == DistributionType.ANY
+            || type == DistributionType.ROUND_ROBIN_DISTRIBUTED || type == DistributionType.BROADCAST_DISTRIBUTED);
+    this.type = type;
+    this.fields = ImmutableList.<DistributionField>of();
+  }
+
+  public DrillDistributionTrait(DistributionType type, ImmutableList<DistributionField> fields) {
+    assert (type == DistributionType.HASH_DISTRIBUTED || type == DistributionType.RANGE_DISTRIBUTED);   
+    this.type = type;
+    this.fields = fields;
+  }
+
+  public boolean subsumes(RelTrait trait) {
+
+    if (trait instanceof DrillDistributionTrait) {
+      DistributionType requiredDist = ((DrillDistributionTrait) trait).getType();
+      if (requiredDist == DistributionType.ANY) {
+        return true;
+      }
+
+      if (this.type == DistributionType.HASH_DISTRIBUTED) {
+        if (requiredDist == DistributionType.HASH_DISTRIBUTED) {
+          ImmutableList<DistributionField> thisFields = this.fields;
+          ImmutableList<DistributionField> requiredFields = ((DrillDistributionTrait)trait).getFields();
+
+          assert(thisFields.size() > 0 && requiredFields.size() > 0);
+
+          // A subset of the required distribution columns can satisfy (subsume) the requirement
+          // e.g: required distribution: {a, b, c} 
+          // Following can satisfy the requirements: {a}, {b}, {c}, {a, b}, {b, c}, {a, c} or {a, b, c}
+          return (requiredFields.containsAll(thisFields));
+        }
+        else if (requiredDist == DistributionType.RANDOM_DISTRIBUTED) {
+          return true; // hash distribution subsumes random distribution and ANY distribution 
+        }
+      }
+    }
+
+    return this.equals(trait);
+  }
+  
+  public RelTraitDef<DrillDistributionTrait> getTraitDef() {
+    return DrillDistributionTraitDef.INSTANCE;
+  }
+
+  public DistributionType getType() {
+    return this.type;
+  }
+
+  public ImmutableList<DistributionField> getFields() {
+    return fields;
+  }
+
+  public int hashCode() {
+    return  fields == null ? type.hashCode() : type.hashCode() | fields.hashCode() << 4 ;
+  }
+  
+  public boolean equals(Object obj) {
+    if (this == obj) {
+      return true;
+    }
+    if (obj instanceof DrillDistributionTrait) {
+      DrillDistributionTrait that = (DrillDistributionTrait) obj;
+      return this.type == that.type && this.fields.equals(that.fields) ;
+    }
+    return false;
+  }
+
+  @Override
+  public String toString() {
+    return fields == null ? this.type.toString() : this.type.toString() + "(" + fields + ")";
+  }
+
+  
+  public static class DistributionField {
+    /**
+     * 0-based index of field being DISTRIBUTED.
+     */
+    private final int fieldId;
+    
+    public DistributionField (int fieldId) {
+      this.fieldId = fieldId;
+    }
+
+    public boolean equals(Object obj) {
+      if (!(obj instanceof DistributionField)) {
+        return false;
+      }
+      DistributionField other = (DistributionField) obj;
+      return this.fieldId == other.fieldId;
+    }
+    
+    public int hashCode() {
+      return this.fieldId;
+    }
+    
+    public int getFieldId() {
+      return this.fieldId;
+    }
+    
+  }
+  
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/DrillDistributionTraitDef.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/DrillDistributionTraitDef.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/DrillDistributionTraitDef.java
new file mode 100644
index 0000000..c2ebb7a
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/DrillDistributionTraitDef.java
@@ -0,0 +1,88 @@
+/**
+ * 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.physical;
+
+import org.eigenbase.rel.RelCollation;
+import org.eigenbase.rel.RelCollationImpl;
+import org.eigenbase.rel.RelCollationTraitDef;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.RelOptPlanner;
+import org.eigenbase.relopt.RelTraitDef;
+
+public class DrillDistributionTraitDef extends RelTraitDef<DrillDistributionTrait>{
+  public static final DrillDistributionTraitDef INSTANCE = new DrillDistributionTraitDef();
+  
+  private DrillDistributionTraitDef() {
+    super();
+  }
+  
+  public boolean canConvert(
+      RelOptPlanner planner, DrillDistributionTrait fromTrait, DrillDistributionTrait toTrait) {
+    return true;
+  }  
+
+  public Class<DrillDistributionTrait> getTraitClass(){
+    return DrillDistributionTrait.class;
+  }
+  
+  public DrillDistributionTrait getDefault() {
+    return DrillDistributionTrait.DEFAULT;
+  }
+
+  public String getSimpleName() {
+    return this.getClass().getSimpleName();
+  }
+
+  // implement RelTraitDef
+  public RelNode convert(
+      RelOptPlanner planner,
+      RelNode rel,
+      DrillDistributionTrait toDist,
+      boolean allowInfiniteCostConverters) {
+    
+    DrillDistributionTrait currentDist = rel.getTraitSet().getTrait(DrillDistributionTraitDef.INSTANCE);
+    
+    // Source and Target have the same trait.
+    if (currentDist.equals(toDist)) {
+      return rel;
+    }
+    
+    // Source trait is "ANY", which is abstract type of distribution.
+    // We do not want to convert from "ANY", since it's abstract. 
+    // Source trait should be concrete type: SINGLETON, HASH_DISTRIBUTED, etc.
+    if (currentDist.equals(DrillDistributionTrait.DEFAULT)) {
+      return null;
+    }
+    
+    RelCollation collation = null;
+    
+    switch(toDist.getType()){
+      // UnionExchange, HashToRandomExchange, OrderedPartitionExchange destroy the ordering property, therefore RelCollation is set to default, which is EMPTY.
+      case SINGLETON:         
+        return new UnionExchangePrel(rel.getCluster(), planner.emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(toDist), rel);
+      case HASH_DISTRIBUTED:
+        return new HashToRandomExchangePrel(rel.getCluster(), planner.emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(toDist), rel, toDist.getFields());
+      case RANGE_DISTRIBUTED:
+        return new OrderedPartitionExchangePrel(rel.getCluster(), planner.emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(toDist), rel);
+      default:
+        return null;
+    }
+
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/FilterPrel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/FilterPrel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/FilterPrel.java
new file mode 100644
index 0000000..f392a18
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/FilterPrel.java
@@ -0,0 +1,58 @@
+/**
+ * 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.physical;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.drill.exec.physical.base.PhysicalOperator;
+import org.apache.drill.exec.physical.config.Filter;
+import org.apache.drill.exec.planner.common.DrillFilterRelBase;
+import org.apache.drill.exec.planner.logical.DrillParseContext;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelTraitSet;
+import org.eigenbase.rex.RexNode;
+
+
+public class FilterPrel extends DrillFilterRelBase implements Prel {
+  protected FilterPrel(RelOptCluster cluster, RelTraitSet traits, RelNode child, RexNode condition) {
+    super(Prel.DRILL_PHYSICAL, cluster, traits, child, condition);
+  }
+
+  @Override
+  public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
+    return new FilterPrel(getCluster(), traitSet, sole(inputs), getCondition());
+  }
+  
+  @Override
+  public PhysicalOperator getPhysicalOperator(PhysicalPlanCreator creator) throws IOException {
+    Prel child = (Prel) this.getChild();
+    
+    PhysicalOperator childPOP = child.getPhysicalOperator(creator);
+    
+    //Currently, Filter only accepts "NONE", SV2, SV4. 
+    
+    Filter p = new Filter(childPOP, getFilterExpression(new DrillParseContext()), 1.0f);
+    creator.addPhysicalOperator(p);
+    
+    return p;
+  }
+  
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/FilterPrule.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/FilterPrule.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/FilterPrule.java
new file mode 100644
index 0000000..9496ba2
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/FilterPrule.java
@@ -0,0 +1,58 @@
+/**
+ * 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.physical;
+
+import org.apache.drill.exec.planner.common.DrillProjectRelBase;
+import org.apache.drill.exec.planner.logical.DrillFilterRel;
+import org.apache.drill.exec.planner.logical.DrillRel;
+import org.apache.drill.exec.planner.logical.RelOptHelper;
+import org.eigenbase.rel.ProjectRel;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.Convention;
+import org.eigenbase.relopt.RelOptRule;
+import org.eigenbase.relopt.RelOptRuleCall;
+import org.eigenbase.relopt.RelTraitSet;
+import org.eigenbase.relopt.volcano.RelSubset;
+
+public class FilterPrule extends RelOptRule {
+  public static final RelOptRule INSTANCE = new FilterPrule();
+
+  private FilterPrule() {
+    super(RelOptHelper.some(DrillFilterRel.class, RelOptHelper.any(RelNode.class)), "FilterPrule");
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    final DrillFilterRel  filter = (DrillFilterRel) call.rel(0);
+    final RelNode input = call.rel(1);
+
+    RelTraitSet traits = input.getTraitSet().plus(Prel.DRILL_PHYSICAL);
+    RelNode convertedInput = convert(input, traits);
+    
+    if (convertedInput instanceof RelSubset) {
+      RelSubset subset = (RelSubset) convertedInput;
+      for (RelNode rel : subset.getRelList()) {
+        if (!rel.getTraitSet().getTrait(DrillDistributionTraitDef.INSTANCE).equals(DrillDistributionTrait.DEFAULT)) {
+          call.transformTo(new FilterPrel(filter.getCluster(), rel.getTraitSet(), convertedInput, filter.getCondition()));
+        }
+      }      
+    } else{
+      call.transformTo(new FilterPrel(filter.getCluster(), convertedInput.getTraitSet(), convertedInput, filter.getCondition()));        
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/HashPrel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/HashPrel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/HashPrel.java
deleted file mode 100644
index 27b6be9..0000000
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/HashPrel.java
+++ /dev/null
@@ -1,37 +0,0 @@
-package org.apache.drill.exec.planner.physical;
-
-import java.util.BitSet;
-import java.util.List;
-
-import org.apache.drill.exec.planner.logical.DrillAggregateRel;
-import org.eigenbase.rel.AggregateCall;
-import org.eigenbase.rel.AggregateRelBase;
-import org.eigenbase.rel.InvalidRelException;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelTraitSet;
-
-public class HashPrel extends AggregateRelBase implements Prel{
-
-  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(HashPrel.class);
-
-  public HashPrel(RelOptCluster cluster, RelTraitSet traits, RelNode child, BitSet groupSet,
-      List<AggregateCall> aggCalls) throws InvalidRelException {
-    super(cluster, traits, child, groupSet, aggCalls);
-    for (AggregateCall aggCall : aggCalls) {
-      if (aggCall.isDistinct()) {
-        throw new InvalidRelException("HashPrel does not support DISTINCT aggregates");
-      }
-    }
-    assert getConvention() == DRILL_PHYSICAL;
-  }
-
-  @Override
-  public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
-    try {
-      return new HashPrel(getCluster(), traitSet, sole(inputs), getGroupSet(), aggCalls);
-    } catch (InvalidRelException e) {
-      throw new AssertionError(e);
-    }
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/HashToRandomExchangePrel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/HashToRandomExchangePrel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/HashToRandomExchangePrel.java
new file mode 100644
index 0000000..c2b6c68
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/HashToRandomExchangePrel.java
@@ -0,0 +1,77 @@
+/**
+ * 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.physical;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.drill.exec.physical.base.PhysicalOperator;
+import org.apache.drill.exec.physical.config.HashToRandomExchange;
+import org.apache.drill.exec.physical.config.SelectionVectorRemover;
+import org.apache.drill.exec.planner.physical.DrillDistributionTrait.DistributionField;
+import org.apache.drill.exec.record.BatchSchema.SelectionVectorMode;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.rel.SingleRel;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptCost;
+import org.eigenbase.relopt.RelOptPlanner;
+import org.eigenbase.relopt.RelTraitSet;
+
+
+public class HashToRandomExchangePrel extends SingleRel implements Prel {
+
+  private final List<DistributionField> fields;
+  
+  public HashToRandomExchangePrel(RelOptCluster cluster, RelTraitSet traitSet, RelNode input, List<DistributionField> fields) {
+    super(cluster, traitSet, input);
+    this.fields = fields;
+    assert input.getConvention() == Prel.DRILL_PHYSICAL;
+  }
+
+  @Override
+  public RelOptCost computeSelfCost(RelOptPlanner planner) {
+    return super.computeSelfCost(planner).multiplyBy(.1);    
+    //return planner.getCostFactory().makeZeroCost();
+  }
+
+  @Override
+  public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
+    return new HashToRandomExchangePrel(getCluster(), traitSet, sole(inputs), fields);
+  }
+  
+  public PhysicalOperator getPhysicalOperator(PhysicalPlanCreator creator) throws IOException {
+    Prel child = (Prel) this.getChild();
+    
+    PhysicalOperator childPOP = child.getPhysicalOperator(creator);
+    
+    //Currently, only accepts "NONE". For other, requires SelectionVectorRemover
+    if (!childPOP.getSVMode().equals(SelectionVectorMode.NONE)) {
+      childPOP = new SelectionVectorRemover(childPOP);
+      creator.addPhysicalOperator(childPOP);
+    }
+
+    HashToRandomExchange g = new HashToRandomExchange(childPOP, PrelUtil.getHashExpression(this.fields, getChild().getRowType()));
+    creator.addPhysicalOperator(g);
+    return g;    
+  }
+  
+  public List<DistributionField> getFields() {
+    return this.fields;
+  }
+  
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/LimitPrel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/LimitPrel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/LimitPrel.java
new file mode 100644
index 0000000..f2f46c1
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/LimitPrel.java
@@ -0,0 +1,95 @@
+/**
+ * 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.physical;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+
+import org.apache.drill.common.logical.data.LogicalOperator;
+import org.apache.drill.exec.physical.base.PhysicalOperator;
+import org.apache.drill.exec.physical.config.Filter;
+import org.apache.drill.exec.physical.config.Limit;
+import org.apache.drill.exec.planner.common.DrillLimitRelBase;
+import org.apache.drill.exec.planner.logical.DrillImplementor;
+import org.apache.drill.exec.planner.logical.DrillParseContext;
+import org.apache.drill.exec.planner.logical.DrillRel;
+import org.apache.drill.exec.planner.torel.ConversionContext;
+import org.eigenbase.rel.InvalidRelException;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelTraitSet;
+import org.eigenbase.rex.RexLiteral;
+import org.eigenbase.rex.RexNode;
+import org.eigenbase.sql.type.SqlTypeName;
+
+public class LimitPrel extends DrillLimitRelBase implements Prel {
+
+  public LimitPrel(RelOptCluster cluster, RelTraitSet traitSet, RelNode child, RexNode offset, RexNode fetch) {
+    super(cluster, traitSet, child, offset, fetch);
+  }
+
+  @Override
+  public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
+    return new LimitPrel(getCluster(), traitSet, sole(inputs), offset, fetch);
+  }
+
+  @Override
+  public PhysicalOperator getPhysicalOperator(PhysicalPlanCreator creator) throws IOException {
+    Prel child = (Prel) this.getChild();
+    
+    PhysicalOperator childPOP = child.getPhysicalOperator(creator);
+    
+    // First offset to include into results (inclusive). Null implies it is starting from offset 0
+    int first = offset != null ? Math.max(0, RexLiteral.intValue(offset)) : 0;
+
+    // Last offset to stop including into results (exclusive), translating fetch row counts into an offset.
+    // Null value implies including entire remaining result set from first offset
+    Integer last = fetch != null ? Math.max(0, RexLiteral.intValue(fetch)) + first : null;
+
+    Limit limit = new Limit(childPOP, first, last);
+    
+    creator.addPhysicalOperator(limit);
+    
+    return limit;
+  }
+  
+//  @Override
+//  public LogicalOperator implement(DrillImplementor implementor) {
+//    LogicalOperator inputOp = implementor.visitChild(this, 0, getChild());
+//    
+//    // First offset to include into results (inclusive). Null implies it is starting from offset 0
+//    int first = offset != null ? Math.max(0, RexLiteral.intValue(offset)) : 0;
+//
+//    // Last offset to stop including into results (exclusive), translating fetch row counts into an offset.
+//    // Null value implies including entire remaining result set from first offset
+//    Integer last = fetch != null ? Math.max(0, RexLiteral.intValue(fetch)) + first : null;
+//    Limit limit = new Limit(first, last);
+//    limit.setInput(inputOp);
+//    return limit;
+//  }
+  
+//  public static LimitPrel convert(Limit limit, ConversionContext context) throws InvalidRelException{
+//    RelNode input = context.toRel(limit.getInput());
+//    RexNode first = context.getRexBuilder().makeExactLiteral(BigDecimal.valueOf(limit.getFirst()), context.getTypeFactory().createSqlType(SqlTypeName.INTEGER));
+//    RexNode last = context.getRexBuilder().makeExactLiteral(BigDecimal.valueOf(limit.getLast()), context.getTypeFactory().createSqlType(SqlTypeName.INTEGER));
+//    return new LimitPrel(context.getCluster(), context.getLogicalTraits(), input, first, last);
+//  }
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/LimitPrule.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/LimitPrule.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/LimitPrule.java
new file mode 100644
index 0000000..8076db5
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/LimitPrule.java
@@ -0,0 +1,50 @@
+/**
+ * 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.physical;
+
+import org.apache.drill.exec.planner.common.DrillScreenRelBase;
+import org.apache.drill.exec.planner.logical.DrillLimitRel;
+import org.apache.drill.exec.planner.logical.DrillRel;
+import org.apache.drill.exec.planner.logical.DrillScreenRel;
+import org.apache.drill.exec.planner.logical.RelOptHelper;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.RelOptRule;
+import org.eigenbase.relopt.RelOptRuleCall;
+import org.eigenbase.relopt.RelTraitSet;
+
+public class LimitPrule extends RelOptRule{
+  public static final RelOptRule INSTANCE = new LimitPrule();
+
+  
+  public LimitPrule() {
+    super(RelOptHelper.some(DrillLimitRel.class, DrillRel.DRILL_LOGICAL, RelOptHelper.any(RelNode.class)), "Prel.LimitPrule");    
+  }
+  
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    final DrillLimitRel limit = (DrillLimitRel) call.rel(0);
+    final RelNode input = call.rel(1);
+    
+    final RelTraitSet traits = input.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(DrillDistributionTrait.SINGLETON);
+    final RelNode convertedInput = convert(input, traits);
+    LimitPrel newLimit = new LimitPrel(limit.getCluster(), limit.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(DrillDistributionTrait.SINGLETON), convertedInput, limit.getOffset(), limit.getFetch());
+    call.transformTo(newLimit);
+  }
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/MergeJoinPrel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/MergeJoinPrel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/MergeJoinPrel.java
new file mode 100644
index 0000000..e21ab4d
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/MergeJoinPrel.java
@@ -0,0 +1,119 @@
+/**
+ * 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.physical;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.drill.common.expression.FieldReference;
+import org.apache.drill.common.logical.data.JoinCondition;
+import org.apache.drill.common.logical.data.LogicalOperator;
+import org.apache.drill.exec.physical.base.PhysicalOperator;
+import org.apache.drill.exec.physical.config.MergeJoinPOP;
+import org.apache.drill.exec.physical.config.SelectionVectorRemover;
+import org.apache.drill.exec.planner.common.DrillJoinRelBase;
+import org.apache.drill.exec.record.BatchSchema.SelectionVectorMode;
+import org.eigenbase.rel.InvalidRelException;
+import org.eigenbase.rel.JoinRelBase;
+import org.eigenbase.rel.JoinRelType;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptUtil;
+import org.eigenbase.relopt.RelTraitSet;
+import org.eigenbase.rex.RexNode;
+import org.eigenbase.util.Pair;
+
+import com.beust.jcommander.internal.Lists;
+
+public class MergeJoinPrel  extends DrillJoinRelBase implements Prel {
+
+  //private final JoinCondition[] joinConditions; // Drill's representation of join conditions
+  
+  /** Creates a MergeJoiPrel. */
+  public MergeJoinPrel(RelOptCluster cluster, RelTraitSet traits, RelNode left, RelNode right, RexNode condition,
+      JoinRelType joinType) throws InvalidRelException {
+    super(cluster, traits, left, right, condition, joinType);
+
+    RexNode remaining = RelOptUtil.splitJoinCondition(left, right, condition, leftKeys, rightKeys);
+    if (!remaining.isAlwaysTrue()) {
+      throw new InvalidRelException("MergeJoinPrel only supports equi-join");
+    }
+    //this.joinConditions = joinConditions;
+  }
+
+  
+  @Override 
+  public JoinRelBase copy(RelTraitSet traitSet, RexNode conditionExpr, RelNode left, RelNode right, JoinRelType joinType) {
+    try {
+      return new MergeJoinPrel(this.getCluster(), traitSet, left, right, conditionExpr, joinType);
+    }catch (InvalidRelException e) {
+      throw new AssertionError(e);
+    }
+  }
+
+  @Override  
+  public PhysicalOperator getPhysicalOperator(PhysicalPlanCreator creator) throws IOException {    
+    PhysicalOperator leftPop = ((Prel) getLeft()).getPhysicalOperator(creator);
+
+    //Currently, only accepts "NONE" or "SV2". For other, requires SelectionVectorRemover
+    if (leftPop.getSVMode().equals(SelectionVectorMode.FOUR_BYTE)) {
+      leftPop = new SelectionVectorRemover(leftPop);
+      creator.addPhysicalOperator(leftPop);
+    }
+
+    PhysicalOperator rightPop = ((Prel) getRight()).getPhysicalOperator(creator);
+
+    //Currently, only accepts "NONE" or "SV2". For other, requires SelectionVectorRemover
+    if (rightPop.getSVMode().equals(SelectionVectorMode.FOUR_BYTE)) {
+      rightPop = new SelectionVectorRemover(rightPop);
+      creator.addPhysicalOperator(rightPop);
+    }
+    
+    JoinRelType jtype = this.getJoinType();
+    
+    final List<String> fields = getRowType().getFieldNames();
+    assert isUnique(fields);
+    final int leftCount = left.getRowType().getFieldCount();
+    final List<String> leftFields = fields.subList(0, leftCount);
+    final List<String> rightFields = fields.subList(leftCount, fields.size());
+    
+    List<JoinCondition> conditions = Lists.newArrayList();
+    
+    for (Pair<Integer, Integer> pair : Pair.zip(leftKeys, rightKeys)) {
+      conditions.add(new JoinCondition("==", new FieldReference(leftFields.get(pair.left)), new FieldReference(rightFields.get(pair.right))));
+    }
+    MergeJoinPOP mjoin = new MergeJoinPOP(leftPop, rightPop, conditions, jtype);
+    creator.addPhysicalOperator(mjoin);
+   
+    return mjoin;
+  }
+
+  public List<Integer> getLeftKeys() {
+    return this.leftKeys;
+  }
+  
+  public List<Integer> getRightKeys() {
+    return this.rightKeys;
+  }
+  
+//  public JoinCondition[] getJoinConditions() {
+//    return joinConditions;
+//  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/MergeJoinPrule.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/MergeJoinPrule.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/MergeJoinPrule.java
new file mode 100644
index 0000000..e693ec6
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/MergeJoinPrule.java
@@ -0,0 +1,101 @@
+/**
+ * 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.physical;
+
+import java.util.List;
+import java.util.logging.Logger;
+
+import net.hydromatic.optiq.util.BitSets;
+
+import org.apache.drill.exec.planner.logical.DrillAggregateRel;
+import org.apache.drill.exec.planner.logical.DrillJoinRel;
+import org.apache.drill.exec.planner.logical.DrillJoinRule;
+import org.apache.drill.exec.planner.logical.DrillRel;
+import org.apache.drill.exec.planner.logical.RelOptHelper;
+import org.apache.drill.exec.planner.physical.DrillDistributionTrait.DistributionField;
+import org.eigenbase.rel.InvalidRelException;
+import org.eigenbase.rel.JoinRel;
+import org.eigenbase.rel.RelCollation;
+import org.eigenbase.rel.RelCollationImpl;
+import org.eigenbase.rel.RelFieldCollation;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.Convention;
+import org.eigenbase.relopt.RelOptRule;
+import org.eigenbase.relopt.RelOptRuleCall;
+import org.eigenbase.relopt.RelTraitSet;
+import org.eigenbase.trace.EigenbaseTrace;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+
+public class MergeJoinPrule extends RelOptRule {
+  public static final RelOptRule INSTANCE = new MergeJoinPrule();
+  protected static final Logger tracer = EigenbaseTrace.getPlannerTracer();
+
+  private MergeJoinPrule() {
+    super(
+        RelOptHelper.some(DrillJoinRel.class, RelOptHelper.any(RelNode.class), RelOptHelper.any(RelNode.class)),
+        "Prel.MergeJoinPrule");
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    final DrillJoinRel join = (DrillJoinRel) call.rel(0);
+    final RelNode left = call.rel(1);
+    final RelNode right = call.rel(2);
+
+    RelCollation collationLeft = getCollation(join.getLeftKeys());
+    RelCollation collationRight = getCollation(join.getRightKeys());
+    DrillDistributionTrait hashLeftPartition = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED, ImmutableList.copyOf(getDistributionField(join.getLeftKeys())));
+    DrillDistributionTrait hashRightPartition = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED, ImmutableList.copyOf(getDistributionField(join.getRightKeys())));
+    
+    final RelTraitSet traitsLeft = left.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(collationLeft).plus(hashLeftPartition);   
+    final RelTraitSet traitsRight = right.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(collationRight).plus(hashRightPartition);
+    
+    final RelNode convertedLeft = convert(left, traitsLeft);
+    final RelNode convertedRight = convert(right, traitsRight);
+    
+    try {          
+      MergeJoinPrel newJoin = new MergeJoinPrel(join.getCluster(), traitsLeft, convertedLeft, convertedRight, join.getCondition(),
+                                                join.getJoinType());
+      call.transformTo(newJoin);
+    } catch (InvalidRelException e) {
+      tracer.warning(e.toString());
+    }
+
+  }
+  
+  private RelCollation getCollation(List<Integer> keys){    
+    List<RelFieldCollation> fields = Lists.newArrayList();
+    for (int key : keys) {
+      fields.add(new RelFieldCollation(key));
+    }
+    return RelCollationImpl.of(fields);
+  }
+
+  private List<DistributionField> getDistributionField(List<Integer> keys) {
+    List<DistributionField> distFields = Lists.newArrayList();
+
+    for (int key : keys) {
+      distFields.add(new DistributionField(key));
+    }
+     
+    return distFields;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/OrderedPartitionExchangePrel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/OrderedPartitionExchangePrel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/OrderedPartitionExchangePrel.java
new file mode 100644
index 0000000..b1dacc5
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/OrderedPartitionExchangePrel.java
@@ -0,0 +1,53 @@
+/**
+ * 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.physical;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.drill.exec.physical.base.PhysicalOperator;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.rel.SingleRel;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptCost;
+import org.eigenbase.relopt.RelOptPlanner;
+import org.eigenbase.relopt.RelTraitSet;
+
+public class OrderedPartitionExchangePrel extends SingleRel implements Prel {
+
+  public OrderedPartitionExchangePrel(RelOptCluster cluster, RelTraitSet traitSet, RelNode input) {
+    super(cluster, traitSet, input);
+    assert input.getConvention() == Prel.DRILL_PHYSICAL;
+  }
+
+  @Override
+  public RelOptCost computeSelfCost(RelOptPlanner planner) {
+    return super.computeSelfCost(planner).multiplyBy(0.1);
+    //return planner.getCostFactory().makeCost(50, 50, 50);
+  }
+
+  @Override
+  public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
+    return new OrderedPartitionExchangePrel(getCluster(), traitSet, sole(inputs));
+  }
+  
+  public PhysicalOperator getPhysicalOperator(PhysicalPlanCreator creator) throws IOException {
+    throw new IOException(this.getClass().getSimpleName() + " not supported yet!");
+  }
+  
+}