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:16 UTC

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

DRILL-450: Add exchange rules, move from BasicOptimizer to Optiq


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

Branch: refs/heads/master
Commit: 6b517daad2a817231df9959f2f3e5c53bd728117
Parents: 22c4190
Author: Jinfeng Ni <jn...@maprtech.com>
Authored: Wed Feb 26 17:04:37 2014 -0800
Committer: Jacques Nadeau <ja...@apache.org>
Committed: Sat Apr 19 18:07:08 2014 -0700

----------------------------------------------------------------------
 .../drill/common/expression/parser/ExprLexer.g  |  27 +--
 .../apache/drill/common/logical/data/Order.java |   6 +-
 .../drill/exec/store/hbase/HBaseGroupScan.java  |  16 +-
 .../exec/store/hbase/HBaseSchemaFactory.java    |  15 +-
 .../exec/store/hbase/HBaseStoragePlugin.java    |  29 ++-
 .../drill/exec/store/hbase/HBaseSubScan.java    |  15 +-
 .../drill/exec/expr/fn/impl/HashFunctions.java  |  31 +++
 .../org/apache/drill/exec/ops/QueryContext.java |   4 +
 .../apache/drill/exec/opt/BasicOptimizer.java   |   6 +-
 .../drill/exec/physical/base/AbstractBase.java  |   6 +
 .../exec/physical/base/AbstractSubScan.java     |   6 +
 .../drill/exec/physical/base/GroupScan.java     |   3 +-
 .../exec/physical/base/PhysicalOperator.java    |   9 +
 .../drill/exec/physical/config/Filter.java      |  15 +-
 .../physical/config/HashToRandomExchange.java   |   5 +-
 .../exec/physical/config/MergeJoinPOP.java      |   1 -
 .../drill/exec/physical/config/Project.java     |   6 +-
 .../drill/exec/physical/config/Screen.java      |   1 +
 .../physical/config/SingleMergeExchange.java    |   5 +
 .../apache/drill/exec/physical/config/Sort.java |   6 +-
 .../exec/physical/impl/TopN/TopNBatch.java      |   2 +-
 .../exec/physical/impl/join/JoinStatus.java     |   4 +-
 .../exec/physical/impl/join/MergeJoinBatch.java |  18 +-
 .../physical/impl/limit/LimitRecordBatch.java   |   1 +
 .../impl/mergereceiver/MergingRecordBatch.java  |   4 +-
 .../OrderedPartitionRecordBatch.java            |   2 +-
 .../exec/physical/impl/sort/SortBatch.java      |   2 +-
 .../physical/impl/xsort/ExternalSortBatch.java  |   4 +-
 .../exec/planner/common/BaseFilterRel.java      |  53 -----
 .../exec/planner/common/BaseProjectRel.java     |  68 ------
 .../drill/exec/planner/common/BaseScanRel.java  |  36 ---
 .../exec/planner/common/BaseScreenRel.java      |  41 ----
 .../planner/common/DrillAggregateRelBase.java   |  42 ++++
 .../exec/planner/common/DrillFilterRelBase.java |  55 +++++
 .../exec/planner/common/DrillJoinRelBase.java   |  80 +++++++
 .../exec/planner/common/DrillLimitRel.java      |  75 -------
 .../exec/planner/common/DrillLimitRelBase.java  |  57 +++++
 .../planner/common/DrillProjectRelBase.java     |  69 ++++++
 .../drill/exec/planner/common/DrillRelNode.java |  25 +++
 .../exec/planner/common/DrillScanRelBase.java   |  39 ++++
 .../exec/planner/common/DrillScreenRelBase.java |  43 ++++
 .../exec/planner/common/DrillStoreRel.java      |  47 ----
 .../exec/planner/common/DrillStoreRelBase.java  |  42 ++++
 .../exec/planner/common/DrillUnionRel.java      |  73 -------
 .../exec/planner/common/DrillUnionRelBase.java  |  37 ++++
 .../exec/planner/logical/DrillAggregateRel.java |   8 +-
 .../exec/planner/logical/DrillFilterRel.java    |   4 +-
 .../exec/planner/logical/DrillJoinRel.java      |  28 +--
 .../exec/planner/logical/DrillLimitRel.java     |  10 +-
 .../exec/planner/logical/DrillLimitRule.java    |   1 -
 .../exec/planner/logical/DrillProjectRel.java   |  11 +-
 .../drill/exec/planner/logical/DrillRel.java    |   6 +-
 .../exec/planner/logical/DrillRuleSets.java     |  49 +++--
 .../exec/planner/logical/DrillScanRel.java      |  10 +-
 .../exec/planner/logical/DrillScanRule.java     |   2 +-
 .../exec/planner/logical/DrillScreenRel.java    |   4 +-
 .../exec/planner/logical/DrillStoreRel.java     |   4 +-
 .../drill/exec/planner/logical/DrillTable.java  |  10 +-
 .../exec/planner/logical/DrillUnionRel.java     |   6 +-
 .../exec/planner/logical/DrillUnionRule.java    |   4 +-
 .../exec/planner/logical/DynamicDrillTable.java |   6 +-
 .../planner/logical/EnumerableDrillRule.java    |   4 +-
 .../planner/logical/RelDataTypeDrillImpl.java   |  78 -------
 .../exec/planner/logical/RelDataTypeHolder.java |  85 --------
 .../physical/DrillDistributionTrait.java        | 142 ++++++++++++
 .../physical/DrillDistributionTraitDef.java     |  88 ++++++++
 .../drill/exec/planner/physical/FilterPrel.java |  58 +++++
 .../exec/planner/physical/FilterPrule.java      |  58 +++++
 .../drill/exec/planner/physical/HashPrel.java   |  37 ----
 .../physical/HashToRandomExchangePrel.java      |  77 +++++++
 .../drill/exec/planner/physical/LimitPrel.java  |  95 ++++++++
 .../drill/exec/planner/physical/LimitPrule.java |  50 +++++
 .../exec/planner/physical/MergeJoinPrel.java    | 119 ++++++++++
 .../exec/planner/physical/MergeJoinPrule.java   | 101 +++++++++
 .../physical/OrderedPartitionExchangePrel.java  |  53 +++++
 .../planner/physical/PhysicalPlanCreator.java   |  79 ++++++-
 .../drill/exec/planner/physical/Prel.java       |  25 ++-
 .../drill/exec/planner/physical/PrelUtil.java   |  72 ++++++
 .../exec/planner/physical/ProjectPrel.java      |  44 +++-
 .../exec/planner/physical/ProjectPrule.java     |  26 ++-
 .../drill/exec/planner/physical/ScanPrel.java   |  39 +++-
 .../drill/exec/planner/physical/ScanPrule.java  |  32 ++-
 .../drill/exec/planner/physical/ScreenPrel.java |  46 +++-
 .../exec/planner/physical/ScreenPrule.java      |  49 +++++
 .../physical/SingleMergeExchangePrel.java       |  80 +++++++
 .../exec/planner/physical/SortConvertPrule.java |  57 +++++
 .../drill/exec/planner/physical/SortPrel.java   |  78 +++++--
 .../drill/exec/planner/physical/SortPrule.java  |  83 +++++++
 .../exec/planner/physical/StorageRule.java      |   5 -
 .../exec/planner/physical/StreamAggPrel.java    |  83 ++++++-
 .../exec/planner/physical/StreamAggPrule.java   |  94 ++++++++
 .../exec/planner/physical/TopNSortPrel.java     |  36 ---
 .../planner/physical/UnionExchangePrel.java     |  69 ++++++
 .../exec/planner/sql/DrillOperatorTable.java    |  80 +++++++
 .../exec/planner/sql/DrillSqlOperator.java      | 155 +++++++++++++
 .../drill/exec/planner/sql/DrillSqlWorker.java  | 101 +++++++--
 .../exec/planner/torel/ConversionContext.java   |  16 +-
 .../planner/types/RelDataTypeDrillImpl.java     |  86 ++++++++
 .../exec/planner/types/RelDataTypeHolder.java   |  86 ++++++++
 .../org/apache/drill/exec/schema/Field.java     |  12 +-
 .../dfs/shim/fallback/FallbackFileSystem.java   |   2 +-
 .../exec/store/easy/json/JSONRecordReader.java  |   1 +
 .../store/ischema/InfoSchemaDrillTable.java     |   4 +-
 .../store/ischema/InfoSchemaStoragePlugin.java  |  17 +-
 .../exec/store/parquet/ParquetGroupScan.java    |   7 +-
 .../apache/drill/exec/work/foreman/Foreman.java |  10 +
 .../drill/exec/work/foreman/QueryManager.java   |   2 +-
 .../org/apache/drill/TestExampleQueries.java    |  21 +-
 .../java/org/apache/drill/TestTpchQueries.java  |   2 +
 .../exec/physical/impl/join/TestMergeJoin.java  |  31 ++-
 .../planner/physical/TestPhysicalPlanning.java  |  39 ----
 .../exec/store/json/JSONRecordReaderTest.java   |   2 +-
 .../store/parquet/ParquetRecordReaderTest.java  |   3 +-
 .../resources/join/merge_join_empty_batch.json  |  47 ++++
 pom.xml                                         |   2 +-
 sample-data/nationsMF/nation.parquet            | Bin 0 -> 3109 bytes
 sample-data/nationsMF/nation2.parquet           | Bin 0 -> 3109 bytes
 sample-data/nationsSF/nation.parquet            | Bin 0 -> 3109 bytes
 sample-data/regionsMF/region.parquet            | Bin 0 -> 819 bytes
 sample-data/regionsMF/region2.parquet           | Bin 0 -> 819 bytes
 sample-data/regionsSF/region.parquet            | Bin 0 -> 819 bytes
 .../org/apache/drill/jdbc/ConnectionConfig.java |  41 ----
 .../org/apache/drill/jdbc/DrillConnection.java  |   2 +-
 .../drill/jdbc/DrillConnectionConfig.java       |  45 ++++
 .../apache/drill/jdbc/DrillConnectionImpl.java  |   6 +-
 .../drill/jdbc/test/TestJdbcDistQuery.java      | 218 +++++++++++++++++++
 .../apache/drill/jdbc/test/TestJdbcQuery.java   |   9 +-
 127 files changed, 3372 insertions(+), 1011 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/common/src/main/antlr3/org/apache/drill/common/expression/parser/ExprLexer.g
----------------------------------------------------------------------
diff --git a/common/src/main/antlr3/org/apache/drill/common/expression/parser/ExprLexer.g b/common/src/main/antlr3/org/apache/drill/common/expression/parser/ExprLexer.g
index 05fb32e..78c356a 100644
--- a/common/src/main/antlr3/org/apache/drill/common/expression/parser/ExprLexer.g
+++ b/common/src/main/antlr3/org/apache/drill/common/expression/parser/ExprLexer.g
@@ -40,19 +40,20 @@ Nullable: 'nullable';
 Repeat: 'repeat';
 As: 'as';
 
-INT		 : 'int';
-BIGINT	 : 'bigint';
-FLOAT4   : 'float4';
-FLOAT8   : 'float8';
-VARCHAR  : 'varchar';
-VARBINARY: 'varbinary';
-DATE     : 'date';
-TIMESTAMP: 'timestamp';
-TIME     : 'time';
-TIMESTAMPTZ: 'timestamptz';
-INTERVAL : 'interval';
-INTERVALYEAR : 'intervalyear';
-INTERVALDAY : 'intervalday';
+INT    : 'int' | 'INT';
+BIGINT   : 'bigint' | 'BIGINT';
+FLOAT4   : 'float4' | 'FLOAT4';
+FLOAT8   : 'float8' | 'FLOAT8';
+VARCHAR  : 'varchar' | 'VARCHAR';
+VARBINARY: 'varbinary' | 'VARBINARY';
+DATE     : 'date' | 'DATE';
+TIMESTAMP: 'timestamp' | 'TIMESTAMP';
+TIME     : 'time' | 'TIME';
+TIMESTAMPTZ: 'timestamptz' | 'TIMESTAMPTZ';
+INTERVAL : 'interval' | 'INTERVAL';
+INTERVALYEAR : 'intervalyear' | 'INTERVALYEAR';
+INTERVALDAY : 'intervalday' | 'INTERVALDAY';
+
 
 Or       : '||' | 'or' | 'OR' | 'Or';
 And      : '&&' | 'and' | 'AND' ;

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/common/src/main/java/org/apache/drill/common/logical/data/Order.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/drill/common/logical/data/Order.java b/common/src/main/java/org/apache/drill/common/logical/data/Order.java
index 3c864b0..06f1144 100644
--- a/common/src/main/java/org/apache/drill/common/logical/data/Order.java
+++ b/common/src/main/java/org/apache/drill/common/logical/data/Order.java
@@ -100,7 +100,7 @@ public class Order extends SingleInputOperator {
     public String getOrder() {
       
       switch(direction){
-      case Descending: return "DESC";
+      case DESCENDING: return "DESC";
       default: return "ASC";
       }
     }
@@ -140,10 +140,10 @@ public class Order extends SingleInputOperator {
   }
   
   public static Direction getDirectionFromString(String direction){
-    return "DESC".equalsIgnoreCase(direction) ? Direction.Descending : Direction.Ascending;
+    return "DESC".equalsIgnoreCase(direction) ? Direction.DESCENDING : Direction.ASCENDING;
   }
   
   public static String getStringFromDirection(Direction direction){
-    return direction == Direction.Descending ? "DESC" : "ASC";
+    return direction == Direction.DESCENDING ? "DESC" : "ASC";
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseGroupScan.java
----------------------------------------------------------------------
diff --git a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseGroupScan.java b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseGroupScan.java
index 67191ec..8c05124 100644
--- a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseGroupScan.java
+++ b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseGroupScan.java
@@ -25,7 +25,6 @@ import com.google.common.collect.Iterators;
 import com.google.common.collect.Lists;
 import org.apache.drill.common.config.DrillConfig;
 import org.apache.drill.common.exceptions.ExecutionSetupException;
-import org.apache.drill.common.expression.FieldReference;
 import org.apache.drill.common.expression.SchemaPath;
 import org.apache.drill.exec.physical.EndpointAffinity;
 import org.apache.drill.exec.physical.OperatorCost;
@@ -70,7 +69,6 @@ public class HBaseGroupScan extends AbstractGroupScan {
   private String tableName;
   private HBaseStoragePlugin storagePlugin;
   private HBaseStoragePluginConfig storagePluginConfig;
-  private final FieldReference ref;
   private List<EndpointAffinity> endpointAffinities;
   private List<SchemaPath> columns;
 
@@ -80,24 +78,21 @@ public class HBaseGroupScan extends AbstractGroupScan {
   public HBaseGroupScan(@JsonProperty("entries") List<HTableReadEntry> entries,
                           @JsonProperty("storage") HBaseStoragePluginConfig storageEngineConfig,
                           @JsonProperty("columns") List<SchemaPath> columns,
-                          @JacksonInject StoragePluginRegistry engineRegistry,
-                          @JsonProperty("ref") FieldReference ref
+                          @JacksonInject StoragePluginRegistry engineRegistry
                            )throws IOException, ExecutionSetupException {
     Preconditions.checkArgument(entries.size() == 1);
     engineRegistry.init(DrillConfig.create());
     this.storagePlugin = (HBaseStoragePlugin) engineRegistry.getEngine(storageEngineConfig);
     this.storagePluginConfig = storageEngineConfig;
     this.tableName = entries.get(0).getTableName();
-    this.ref = ref;
     this.columns = columns;
     getRegionInfos();
   }
 
-  public HBaseGroupScan(String tableName, HBaseStoragePlugin storageEngine, FieldReference ref, List<SchemaPath> columns) throws IOException {
+  public HBaseGroupScan(String tableName, HBaseStoragePlugin storageEngine, List<SchemaPath> columns) throws IOException {
     this.storagePlugin = storageEngine;
-    this.storagePluginConfig = storageEngine.getEngineConfig();
+    this.storagePluginConfig = storageEngine.getConfig();
     this.tableName = tableName;
-    this.ref = ref;
     this.columns = columns;
     getRegionInfos();
   }
@@ -167,12 +162,9 @@ public class HBaseGroupScan extends AbstractGroupScan {
 
   @Override
   public HBaseSubScan getSpecificScan(int minorFragmentId) {
-    return new HBaseSubScan(storagePlugin, storagePluginConfig, mappings.get(minorFragmentId), ref, columns);
+    return new HBaseSubScan(storagePlugin, storagePluginConfig, mappings.get(minorFragmentId), columns);
   }
 
-  public FieldReference getRef() {
-    return ref;
-  }
 
   @Override
   public int getMaxParallelizationWidth() {

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseSchemaFactory.java
----------------------------------------------------------------------
diff --git a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseSchemaFactory.java b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseSchemaFactory.java
index 895433f..d97f3d0 100644
--- a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseSchemaFactory.java
+++ b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseSchemaFactory.java
@@ -35,11 +35,11 @@ import org.apache.hadoop.hbase.client.HBaseAdmin;
 public class HBaseSchemaFactory implements SchemaFactory {
   static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(HBaseSchemaFactory.class);
 
-  final HBaseStoragePluginConfig configuration;
   final String schemaName;
-
-  public HBaseSchemaFactory(HBaseStoragePluginConfig configuration, String name) throws IOException {
-    this.configuration = configuration;
+  final HBaseStoragePlugin plugin;
+  
+  public HBaseSchemaFactory(HBaseStoragePlugin plugin, String name) throws IOException {
+    this.plugin = plugin;
     this.schemaName = name;
   }
 
@@ -65,7 +65,7 @@ public class HBaseSchemaFactory implements SchemaFactory {
 
     @Override
     public Schema getSubSchema(String name) {
-      throw new UnsupportedOperationException();
+      return null;
     }
 
     @Override
@@ -76,13 +76,12 @@ public class HBaseSchemaFactory implements SchemaFactory {
     @Override
     public DrillTable getTable(String name) {
       Object selection = new HTableReadEntry(name);
-      return new DynamicDrillTable(schemaName, selection, configuration);
+      return new DynamicDrillTable(plugin, schemaName, selection, plugin.getConfig());
     }
 
     @Override
     public Set<String> getTableNames() {
-      try {
-        HBaseAdmin admin = new HBaseAdmin(configuration.conf);
+      try(HBaseAdmin admin = new HBaseAdmin(plugin.getConfig().conf)) {
         HTableDescriptor[] tables = admin.listTables();
         Set<String> tableNames = Sets.newHashSet();
         for (HTableDescriptor table : tables) {

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseStoragePlugin.java
----------------------------------------------------------------------
diff --git a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseStoragePlugin.java b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseStoragePlugin.java
index 3bbae2d..1fab650 100644
--- a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseStoragePlugin.java
+++ b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseStoragePlugin.java
@@ -18,18 +18,17 @@
 package org.apache.drill.exec.store.hbase;
 
 import java.io.IOException;
-import java.util.ArrayList;
-
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.ObjectMapper;
 
 import net.hydromatic.optiq.Schema;
 import net.hydromatic.optiq.SchemaPlus;
-import org.apache.drill.common.logical.data.Scan;
+
+import org.apache.drill.common.JSONOptions;
+import org.apache.drill.common.logical.StoragePluginConfig;
 import org.apache.drill.exec.server.DrillbitContext;
 import org.apache.drill.exec.store.AbstractStoragePlugin;
 
-import com.google.common.base.Preconditions;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 public class HBaseStoragePlugin extends AbstractStoragePlugin {
   static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(HBaseStoragePlugin.class);
@@ -42,15 +41,11 @@ public class HBaseStoragePlugin extends AbstractStoragePlugin {
   public HBaseStoragePlugin(HBaseStoragePluginConfig configuration, DrillbitContext context, String name)
       throws IOException {
     this.context = context;
-    this.schemaFactory = new HBaseSchemaFactory(configuration, name);
+    this.schemaFactory = new HBaseSchemaFactory(this, name);
     this.engineConfig = configuration;
     this.name = name;
   }
 
-  public HBaseStoragePluginConfig getEngineConfig() {
-    return engineConfig;
-  }
-
   public DrillbitContext getContext() {
     return this.context;
   }
@@ -61,11 +56,10 @@ public class HBaseStoragePlugin extends AbstractStoragePlugin {
   }
 
   @Override
-  public HBaseGroupScan getPhysicalScan(Scan scan) throws IOException {
-    HTableReadEntry readEntry = scan.getSelection().getListWith(new ObjectMapper(),
+  public HBaseGroupScan getPhysicalScan(JSONOptions selection) throws IOException {
+    HTableReadEntry readEntry = selection.getListWith(new ObjectMapper(),
         new TypeReference<HTableReadEntry>() {});
-
-    return new HBaseGroupScan(readEntry.getTableName(), this, scan.getOutputReference(), null);
+    return new HBaseGroupScan(readEntry.getTableName(), this, null);
   }
 
   @Override
@@ -73,4 +67,9 @@ public class HBaseStoragePlugin extends AbstractStoragePlugin {
     return schemaFactory.add(parent);
   }
 
+  @Override
+  public HBaseStoragePluginConfig getConfig() {
+    return engineConfig;
+  }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseSubScan.java
----------------------------------------------------------------------
diff --git a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseSubScan.java b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseSubScan.java
index 8c4eea0..0e8a934 100644
--- a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseSubScan.java
+++ b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseSubScan.java
@@ -51,28 +51,24 @@ public class HBaseSubScan extends AbstractBase implements SubScan {
   @JsonIgnore
   private final HBaseStoragePlugin hbaseStoragePlugin;
   private final List<HBaseSubScanReadEntry> rowGroupReadEntries;
-  private final FieldReference ref;
   private final List<SchemaPath> columns;
 
   @JsonCreator
   public HBaseSubScan(@JacksonInject StoragePluginRegistry registry, @JsonProperty("storage") StoragePluginConfig storage,
                       @JsonProperty("rowGroupReadEntries") LinkedList<HBaseSubScanReadEntry> rowGroupReadEntries,
-                      @JsonProperty("columns") List<SchemaPath> columns,
-                      @JsonProperty("ref") FieldReference ref) throws ExecutionSetupException {
+                      @JsonProperty("columns") List<SchemaPath> columns) throws ExecutionSetupException {
     hbaseStoragePlugin = (HBaseStoragePlugin) registry.getEngine(storage);
     this.rowGroupReadEntries = rowGroupReadEntries;
     this.storage = storage;
-    this.ref = ref;
     this.columns = columns;
   }
 
   public HBaseSubScan(HBaseStoragePlugin plugin, HBaseStoragePluginConfig config,
-                      List<HBaseSubScanReadEntry> regionInfoList, FieldReference ref,
+                      List<HBaseSubScanReadEntry> regionInfoList,
                       List<SchemaPath> columns) {
     hbaseStoragePlugin = plugin;
     storage = config;
     this.rowGroupReadEntries = regionInfoList;
-    this.ref = ref;
     this.columns = columns;
   }
 
@@ -94,11 +90,6 @@ public class HBaseSubScan extends AbstractBase implements SubScan {
     return null;
   }
 
-  
-  public FieldReference getRef() {
-    return ref;
-  }
-
   @Override
   public Size getSize() {
     return null;
@@ -122,7 +113,7 @@ public class HBaseSubScan extends AbstractBase implements SubScan {
   @Override
   public PhysicalOperator getNewWithChildren(List<PhysicalOperator> children) {
     Preconditions.checkArgument(children.isEmpty());
-    return new HBaseSubScan(hbaseStoragePlugin, (HBaseStoragePluginConfig) storage, rowGroupReadEntries, ref, columns);
+    return new HBaseSubScan(hbaseStoragePlugin, (HBaseStoragePluginConfig) storage, rowGroupReadEntries, columns);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/HashFunctions.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/HashFunctions.java b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/HashFunctions.java
index 5a6943e..36300ee 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/HashFunctions.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/HashFunctions.java
@@ -106,6 +106,23 @@ public class HashFunctions {
     }
   }
 
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL )
+  public static class NullableVarCharHash implements DrillSimpleFunc {
+
+    @Param NullableVarCharHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      if (in.isSet == 0)
+        out.value = 0;
+      else
+        out.value = org.apache.drill.exec.expr.fn.impl.HashHelper.hash(in.buffer.nioBuffer(), 0);
+    }
+  }
+
   @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
   public static class NullableBigIntHash implements DrillSimpleFunc {
 
@@ -154,6 +171,20 @@ public class HashFunctions {
       out.value = org.apache.drill.exec.expr.fn.impl.HashHelper.hash(in.buffer.nioBuffer(in.start, in.end - in.start), 0);
     }
   }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class VarCharHash implements DrillSimpleFunc {
+
+    @Param VarCharHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      out.value = org.apache.drill.exec.expr.fn.impl.HashHelper.hash(in.buffer.nioBuffer(in.start, in.end - in.start), 0);
+    }
+  }
   
   @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
   public static class HashBigInt implements DrillSimpleFunc {

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/ops/QueryContext.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/ops/QueryContext.java b/exec/java-exec/src/main/java/org/apache/drill/exec/ops/QueryContext.java
index fa66926..1564b09 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/ops/QueryContext.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/ops/QueryContext.java
@@ -21,6 +21,7 @@ import java.util.Collection;
 
 import org.apache.drill.common.config.DrillConfig;
 import org.apache.drill.exec.cache.DistributedCache;
+import org.apache.drill.exec.expr.fn.FunctionImplementationRegistry;
 import org.apache.drill.exec.planner.PhysicalPlanReader;
 import org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint;
 import org.apache.drill.exec.proto.UserBitShared.QueryId;
@@ -85,4 +86,7 @@ public class QueryContext {
     return drillbitContext.getSchemaFactory();
   }
   
+  public FunctionImplementationRegistry getFunctionRegistry(){
+    return drillbitContext.getFunctionImplementationRegistry();
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/opt/BasicOptimizer.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/opt/BasicOptimizer.java b/exec/java-exec/src/main/java/org/apache/drill/exec/opt/BasicOptimizer.java
index 36b8373..97ec026 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/opt/BasicOptimizer.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/opt/BasicOptimizer.java
@@ -129,7 +129,7 @@ public class BasicOptimizer extends Optimizer{
 
       if(groupBy.getKeys().length > 0){
         for(NamedExpression e : groupBy.getKeys()){
-          orderDefs.add(new Ordering(Direction.Ascending, e.getExpr(), NullDirection.FIRST));
+          orderDefs.add(new Ordering(Direction.ASCENDING, e.getExpr(), NullDirection.FIRST));
         }
         input = new Sort(input, orderDefs, false);
       }
@@ -161,7 +161,7 @@ public class BasicOptimizer extends Optimizer{
       PhysicalOperator leftOp = join.getLeft().accept(this, value);
       List<Ordering> leftOrderDefs = Lists.newArrayList();
       for(JoinCondition jc : join.getConditions()){
-        leftOrderDefs.add(new Ordering(Direction.Ascending, jc.getLeft()));
+        leftOrderDefs.add(new Ordering(Direction.ASCENDING, jc.getLeft()));
       }
       leftOp = new Sort(leftOp, leftOrderDefs, false);
       leftOp = new SelectionVectorRemover(leftOp);
@@ -169,7 +169,7 @@ public class BasicOptimizer extends Optimizer{
       PhysicalOperator rightOp = join.getRight().accept(this, value);
       List<Ordering> rightOrderDefs = Lists.newArrayList();
       for(JoinCondition jc : join.getConditions()){
-        rightOrderDefs.add(new Ordering(Direction.Ascending, jc.getRight()));
+        rightOrderDefs.add(new Ordering(Direction.ASCENDING, jc.getRight()));
       }
       rightOp = new Sort(rightOp, rightOrderDefs, false);
       rightOp = new SelectionVectorRemover(rightOp);

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractBase.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractBase.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractBase.java
index f7a6f51..7eced4d 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractBase.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractBase.java
@@ -19,6 +19,7 @@ package org.apache.drill.exec.physical.base;
 
 import org.apache.drill.common.graph.GraphVisitor;
 import org.apache.drill.exec.physical.OperatorCost;
+import org.apache.drill.exec.record.BatchSchema.SelectionVectorMode;
 
 import com.google.common.base.Preconditions;
 
@@ -43,4 +44,9 @@ public abstract class AbstractBase implements PhysicalOperator{
     return true;
   }
   
+  @Override
+  public SelectionVectorMode getSVMode() {
+    return SelectionVectorMode.NONE;
+  }
+  
 }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractSubScan.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractSubScan.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractSubScan.java
index c6e11d1..57b9c18 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractSubScan.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractSubScan.java
@@ -23,6 +23,7 @@ import java.util.List;
 import org.apache.drill.common.exceptions.ExecutionSetupException;
 import org.apache.drill.common.graph.GraphVisitor;
 import org.apache.drill.exec.physical.OperatorCost;
+import org.apache.drill.exec.record.BatchSchema.SelectionVectorMode;
 
 import com.google.common.collect.Iterators;
 
@@ -67,4 +68,9 @@ public abstract class AbstractSubScan implements SubScan{
     return Iterators.emptyIterator();
   }
   
+  @Override
+  public SelectionVectorMode getSVMode() {
+    return SelectionVectorMode.NONE;
+  }
+  
 }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/GroupScan.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/GroupScan.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/GroupScan.java
index 314bc84..314e889 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/GroupScan.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/GroupScan.java
@@ -21,7 +21,6 @@ import java.util.List;
 
 import org.apache.drill.common.exceptions.ExecutionSetupException;
 import org.apache.drill.common.exceptions.PhysicalOperatorSetupException;
-import org.apache.drill.exec.planner.physical.Prel;
 import org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
@@ -30,7 +29,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
  * A GroupScan operator represents all data which will be scanned by a given physical
  * plan.  It is the superset of all SubScans for the plan.
  */
-public interface GroupScan extends Scan, HasAffinity, Prel{
+public interface GroupScan extends Scan, HasAffinity{ 
 
   public abstract void applyAssignments(List<DrillbitEndpoint> endpoints) throws PhysicalOperatorSetupException;
 

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/PhysicalOperator.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/PhysicalOperator.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/PhysicalOperator.java
index 6872595..66e1b46 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/PhysicalOperator.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/PhysicalOperator.java
@@ -22,6 +22,7 @@ import java.util.List;
 import org.apache.drill.common.exceptions.ExecutionSetupException;
 import org.apache.drill.common.graph.GraphValue;
 import org.apache.drill.exec.physical.OperatorCost;
+import org.apache.drill.exec.record.BatchSchema.SelectionVectorMode;
 
 import com.fasterxml.jackson.annotation.JsonIdentityInfo;
 import com.fasterxml.jackson.annotation.JsonIgnore;
@@ -63,6 +64,14 @@ public interface  PhysicalOperator extends GraphValue<PhysicalOperator> {
   public boolean isExecutable();
 
   /**
+   * Describes the SelectionVector Mode for the output steam from this physical op.
+   * This property is used during physical plan creating using {@link PhysicalPlanCreator}.
+   * @return
+   */
+  @JsonIgnore
+  public SelectionVectorMode getSVMode();
+  
+  /**
    * Provides capability to build a set of output based on traversing a query graph tree.
    * 
    * @param physicalVisitor

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Filter.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Filter.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Filter.java
index aba1747..f125d05 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Filter.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Filter.java
@@ -23,6 +23,7 @@ import org.apache.drill.exec.physical.base.AbstractSingle;
 import org.apache.drill.exec.physical.base.PhysicalOperator;
 import org.apache.drill.exec.physical.base.PhysicalVisitor;
 import org.apache.drill.exec.physical.base.Size;
+import org.apache.drill.exec.record.BatchSchema.SelectionVectorMode;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
@@ -66,10 +67,14 @@ public class Filter extends AbstractSingle {
   public Size getSize() {
     return new Size( (long) (child.getSize().getRecordCount()*selectivity), child.getSize().getRecordSize());
   }
-  
-  
-
-  
-  
+   
+  @Override
+  public SelectionVectorMode getSVMode() {
+    if (child.getSVMode().equals(SelectionVectorMode.FOUR_BYTE)) {
+      return SelectionVectorMode.FOUR_BYTE;
+    } else {
+      return SelectionVectorMode.TWO_BYTE;
+    }
+  }
   
 }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/HashToRandomExchange.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/HashToRandomExchange.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/HashToRandomExchange.java
index 9d5f3d6..61c2f1a 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/HashToRandomExchange.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/HashToRandomExchange.java
@@ -84,7 +84,10 @@ public class HashToRandomExchange extends AbstractExchange{
     return true;
   }
 
-  
+  @JsonProperty("expr")
+  public LogicalExpression getExpression(){
+    return expr;
+  }
 
   
   

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/MergeJoinPOP.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/MergeJoinPOP.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/MergeJoinPOP.java
index fde88a9..a9a0f6d 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/MergeJoinPOP.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/MergeJoinPOP.java
@@ -28,7 +28,6 @@ import org.apache.drill.exec.physical.base.PhysicalOperator;
 import org.apache.drill.exec.physical.base.PhysicalVisitor;
 import org.apache.drill.exec.physical.base.Size;
 import org.eigenbase.rel.JoinRelType;
-import org.eigenbase.sql.SqlJoinOperator.JoinType;
 
 import com.beust.jcommander.internal.Lists;
 import com.fasterxml.jackson.annotation.JsonCreator;

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Project.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Project.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Project.java
index 7ab0620..0e6b0fd 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Project.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Project.java
@@ -25,6 +25,7 @@ import org.apache.drill.exec.physical.base.AbstractSingle;
 import org.apache.drill.exec.physical.base.PhysicalOperator;
 import org.apache.drill.exec.physical.base.PhysicalVisitor;
 import org.apache.drill.exec.physical.base.Size;
+import org.apache.drill.exec.record.BatchSchema.SelectionVectorMode;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
@@ -67,5 +68,8 @@ public class Project extends AbstractSingle{
     return new Project(exprs, child);
   }
   
-  
+  @Override
+  public SelectionVectorMode getSVMode() {
+    return child.getSVMode();
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Screen.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Screen.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Screen.java
index 654d1f7..c4d78f9 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Screen.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Screen.java
@@ -45,6 +45,7 @@ public class Screen extends AbstractStore {
 
   public Screen(@JsonProperty("child") PhysicalOperator child, @JacksonInject DrillbitEndpoint endpoint) {
     super(child);
+    assert (endpoint!=null);
     this.endpoint = endpoint;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/SingleMergeExchange.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/SingleMergeExchange.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/SingleMergeExchange.java
index b8073c1..df13a84 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/SingleMergeExchange.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/SingleMergeExchange.java
@@ -89,4 +89,9 @@ public class SingleMergeExchange extends AbstractExchange {
     return true;
   }
 
+  @JsonProperty("orderings")
+  public List<Ordering> getOrderings() {
+    return this.orderExpr;
+  }
+
 }
\ 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/physical/config/Sort.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Sort.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Sort.java
index 507a960..c9ac137 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Sort.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/Sort.java
@@ -25,6 +25,7 @@ import org.apache.drill.exec.physical.base.AbstractSingle;
 import org.apache.drill.exec.physical.base.PhysicalOperator;
 import org.apache.drill.exec.physical.base.PhysicalVisitor;
 import org.apache.drill.exec.physical.base.Size;
+import org.apache.drill.exec.record.BatchSchema.SelectionVectorMode;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
@@ -79,7 +80,10 @@ public class Sort extends AbstractSingle{
     return new Sort(child, orderings, reverse);
   }
 
-    
+  @Override
+  public SelectionVectorMode getSVMode() {
+    return SelectionVectorMode.FOUR_BYTE;
+  }
   
   
 }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/TopN/TopNBatch.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/TopN/TopNBatch.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/TopN/TopNBatch.java
index 3a53555..138c1cf 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/TopN/TopNBatch.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/TopN/TopNBatch.java
@@ -250,7 +250,7 @@ public class TopNBatch extends AbstractRecordBatch<TopN> {
       HoldingContainer out = g.addExpr(fh, false);
       JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
       
-      if(od.getDirection() == Direction.Ascending){
+      if(od.getDirection() == Direction.ASCENDING){
         jc._then()._return(out.getValue());
       }else{
         jc._then()._return(out.getValue().minus());

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/join/JoinStatus.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/join/JoinStatus.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/join/JoinStatus.java
index fb91b2e..5a83b48 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/join/JoinStatus.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/join/JoinStatus.java
@@ -206,12 +206,12 @@ public final class JoinStatus {
       return JoinOutcome.FAILURE;
     if (lastLeft == IterOutcome.OK && lastRight == IterOutcome.OK)
       return JoinOutcome.BATCH_RETURNED;
+    if (eitherMatches(IterOutcome.NONE))
+      return JoinOutcome.NO_MORE_DATA;    
     if (eitherMatches(IterOutcome.OK_NEW_SCHEMA))
       return JoinOutcome.SCHEMA_CHANGED;
     if (eitherMatches(IterOutcome.NOT_YET))
       return JoinOutcome.WAITING;
-    if (eitherMatches(IterOutcome.NONE))
-      return JoinOutcome.NO_MORE_DATA;
     return JoinOutcome.FAILURE;
   }
   

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/join/MergeJoinBatch.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/join/MergeJoinBatch.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/join/MergeJoinBatch.java
index db5bccf..0cb2f7d 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/join/MergeJoinBatch.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/join/MergeJoinBatch.java
@@ -128,17 +128,25 @@ public class MergeJoinBatch extends AbstractRecordBatch<MergeJoinPOP> {
     // loop so we can start over again if we find a new batch was created.
     while(true){
 
+      JoinOutcome outcome = status.getOutcome();
       // if the previous outcome was a change in schema or we sent a batch, we have to set up a new batch.
-      if (status.getOutcome() == JoinOutcome.BATCH_RETURNED ||
-          status.getOutcome() == JoinOutcome.SCHEMA_CHANGED)
+      if (outcome == JoinOutcome.BATCH_RETURNED ||
+          outcome == JoinOutcome.SCHEMA_CHANGED)
         allocateBatch();
 
       // reset the output position to zero after our parent iterates this RecordBatch
-      if (status.getOutcome() == JoinOutcome.BATCH_RETURNED ||
-          status.getOutcome() == JoinOutcome.SCHEMA_CHANGED ||
-          status.getOutcome() == JoinOutcome.NO_MORE_DATA)
+      if (outcome == JoinOutcome.BATCH_RETURNED ||
+          outcome == JoinOutcome.SCHEMA_CHANGED ||
+          outcome == JoinOutcome.NO_MORE_DATA)
         status.resetOutputPos();
 
+      if (outcome == JoinOutcome.NO_MORE_DATA) {
+        left.cleanup();
+        right.cleanup();
+        logger.debug("NO MORE DATA; returning {}  NONE");
+        return IterOutcome.NONE;
+      }
+      
       boolean first = false;
       if(worker == null){
         try {

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/limit/LimitRecordBatch.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/limit/LimitRecordBatch.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/limit/LimitRecordBatch.java
index bf2bcf8..dcd452e 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/limit/LimitRecordBatch.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/limit/LimitRecordBatch.java
@@ -80,6 +80,7 @@ public class LimitRecordBatch extends AbstractSingleRecordBatch<Limit> {
   public IterOutcome next() {
     if(!noEndLimit && recordsLeft <= 0) {
       killIncoming();
+      cleanup();
       return IterOutcome.NONE;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/mergereceiver/MergingRecordBatch.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/mergereceiver/MergingRecordBatch.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/mergereceiver/MergingRecordBatch.java
index d6d42b6..13ed4c9 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/mergereceiver/MergingRecordBatch.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/mergereceiver/MergingRecordBatch.java
@@ -546,11 +546,11 @@ public class MergingRecordBatch implements RecordBatch {
       // generate less than/greater than checks (fixing results for ASCending vs. DESCending)
       cg.getEvalBlock()._if(out.getValue().eq(JExpr.lit(1)))
                        ._then()
-                       ._return(JExpr.lit(config.getOrderings().get(comparisonVectorIndex).getDirection() == Direction.Ascending ? 1 : -1));
+                       ._return(JExpr.lit(config.getOrderings().get(comparisonVectorIndex).getDirection() == Direction.ASCENDING ? 1 : -1));
 
       cg.getEvalBlock()._if(out.getValue().eq(JExpr.lit(-1)))
                        ._then()
-                       ._return(JExpr.lit(config.getOrderings().get(comparisonVectorIndex).getDirection() == Direction.Ascending ? -1 : 1));
+                       ._return(JExpr.lit(config.getOrderings().get(comparisonVectorIndex).getDirection() == Direction.ASCENDING ? -1 : 1));
 
       ++comparisonVectorIndex;
     }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/orderedpartitioner/OrderedPartitionRecordBatch.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/orderedpartitioner/OrderedPartitionRecordBatch.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/orderedpartitioner/OrderedPartitionRecordBatch.java
index 4eee6af..5fc3733 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/orderedpartitioner/OrderedPartitionRecordBatch.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/orderedpartitioner/OrderedPartitionRecordBatch.java
@@ -533,7 +533,7 @@ public class OrderedPartitionRecordBatch extends AbstractRecordBatch<OrderedPart
       ClassGenerator.HoldingContainer out = cg.addExpr(fh, false);
       JConditional jc = cg.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
 
-      if (od.getDirection() == Direction.Ascending) {
+      if (od.getDirection() == Direction.ASCENDING) {
         jc._then()._return(out.getValue());
       } else {
         jc._then()._return(out.getValue().minus());

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/sort/SortBatch.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/sort/SortBatch.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/sort/SortBatch.java
index 51ba418..5cdab96 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/sort/SortBatch.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/sort/SortBatch.java
@@ -189,7 +189,7 @@ public class SortBatch extends AbstractRecordBatch<Sort> {
       HoldingContainer out = g.addExpr(fh, false);
       JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
       
-      if(od.getDirection() == Direction.Ascending){
+      if(od.getDirection() == Direction.ASCENDING){
         jc._then()._return(out.getValue());
       }else{
         jc._then()._return(out.getValue().minus());

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/xsort/ExternalSortBatch.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/xsort/ExternalSortBatch.java b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/xsort/ExternalSortBatch.java
index 4dc47b6..0a3b768 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/xsort/ExternalSortBatch.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/xsort/ExternalSortBatch.java
@@ -389,7 +389,7 @@ public class ExternalSortBatch extends AbstractRecordBatch<ExternalSort> {
       HoldingContainer out = g.addExpr(fh, false);
       JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
 
-      if(od.getDirection() == Direction.Ascending){
+      if(od.getDirection() == Direction.ASCENDING){
         jc._then()._return(out.getValue());
       }else{
         jc._then()._return(out.getValue().minus());
@@ -432,7 +432,7 @@ public class ExternalSortBatch extends AbstractRecordBatch<ExternalSort> {
       HoldingContainer out = g.addExpr(fh, false);
       JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
 
-      if(od.getDirection() == Direction.Ascending){
+      if(od.getDirection() == Direction.ASCENDING){
         jc._then()._return(out.getValue());
       }else{
         jc._then()._return(out.getValue().minus());

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseFilterRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseFilterRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseFilterRel.java
deleted file mode 100644
index 42c0ec5..0000000
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseFilterRel.java
+++ /dev/null
@@ -1,53 +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 org.apache.drill.common.expression.LogicalExpression;
-import org.apache.drill.common.logical.data.Filter;
-import org.apache.drill.common.logical.data.LogicalOperator;
-import org.apache.drill.exec.planner.logical.DrillImplementor;
-import org.apache.drill.exec.planner.logical.DrillOptiq;
-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.FilterRelBase;
-import org.eigenbase.rel.InvalidRelException;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.relopt.Convention;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptCost;
-import org.eigenbase.relopt.RelOptPlanner;
-import org.eigenbase.relopt.RelTraitSet;
-import org.eigenbase.rex.RexNode;
-
-
-public abstract class BaseFilterRel extends FilterRelBase{
-  protected BaseFilterRel(Convention convention, RelOptCluster cluster, RelTraitSet traits, RelNode child, RexNode condition) {
-    super(cluster, traits, child, condition);
-    assert getConvention() == convention;
-  }
-  
-  @Override
-  public RelOptCost computeSelfCost(RelOptPlanner planner) {
-    return super.computeSelfCost(planner).multiplyBy(0.1);
-  }
-
-  protected LogicalExpression getFilterExpression(DrillParseContext context){
-    return DrillOptiq.toDrill(context, getChild(), getCondition());
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseProjectRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseProjectRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseProjectRel.java
deleted file mode 100644
index 9314a29..0000000
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseProjectRel.java
+++ /dev/null
@@ -1,68 +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 org.apache.drill.common.expression.FieldReference;
-import org.apache.drill.common.expression.LogicalExpression;
-import org.apache.drill.common.logical.data.NamedExpression;
-import org.apache.drill.exec.planner.logical.DrillOptiq;
-import org.apache.drill.exec.planner.logical.DrillParseContext;
-import org.eigenbase.rel.ProjectRelBase;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.relopt.Convention;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptCost;
-import org.eigenbase.relopt.RelOptPlanner;
-import org.eigenbase.relopt.RelTraitSet;
-import org.eigenbase.reltype.RelDataType;
-import org.eigenbase.rex.RexNode;
-import org.eigenbase.util.Pair;
-
-import com.google.common.collect.Lists;
-
-/**
- * Project implemented in Drill.
- */
-public class BaseProjectRel extends ProjectRelBase{
-  protected BaseProjectRel(Convention convention, RelOptCluster cluster, RelTraitSet traits, RelNode child, List<RexNode> exps,
-      RelDataType rowType) {
-    super(cluster, traits, child, exps, rowType, Flags.BOXED);
-    assert getConvention() == convention;
-  }
-
-  @Override
-  public RelOptCost computeSelfCost(RelOptPlanner planner) {
-    return super.computeSelfCost(planner).multiplyBy(0.1);
-  }
-
-  private List<Pair<RexNode, String>> projects() {
-    return Pair.zip(exps, getRowType().getFieldNames());
-  }
-
-  protected List<NamedExpression> getProjectExpressions(DrillParseContext context){
-    List<NamedExpression> expressions = Lists.newArrayList();
-    for (Pair<RexNode, String> pair : projects()) {
-      LogicalExpression expr = DrillOptiq.toDrill(context, getChild(), pair.left);
-      expressions.add(new NamedExpression(expr, new FieldReference("output." + pair.right)));
-    }
-    return expressions;
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseScanRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseScanRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseScanRel.java
deleted file mode 100644
index 69b47ab..0000000
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseScanRel.java
+++ /dev/null
@@ -1,36 +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 org.apache.drill.exec.planner.logical.DrillTable;
-import org.eigenbase.rel.TableAccessRelBase;
-import org.eigenbase.relopt.Convention;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptTable;
-import org.eigenbase.relopt.RelTraitSet;
-
-public abstract class BaseScanRel extends TableAccessRelBase{
-  protected final DrillTable drillTable;
-
-  public BaseScanRel(Convention convention, RelOptCluster cluster, RelTraitSet traits, RelOptTable table) {
-    super(cluster, traits, table);
-    this.drillTable = table.unwrap(DrillTable.class);
-    assert drillTable != 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/BaseScreenRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseScreenRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseScreenRel.java
deleted file mode 100644
index 4c1a318..0000000
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/BaseScreenRel.java
+++ /dev/null
@@ -1,41 +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 org.apache.drill.exec.planner.logical.DrillRel;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.SingleRel;
-import org.eigenbase.relopt.Convention;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptCost;
-import org.eigenbase.relopt.RelOptPlanner;
-import org.eigenbase.relopt.RelTraitSet;
-
-public abstract class BaseScreenRel extends SingleRel {
-
-  public BaseScreenRel(Convention convention, RelOptCluster cluster, RelTraitSet traitSet, RelNode input) {
-    super(cluster, traitSet, input);
-    assert input.getConvention() == convention;
-  }
-
-  @Override
-  public RelOptCost computeSelfCost(RelOptPlanner planner) {
-    return super.computeSelfCost(planner).multiplyBy(.1);
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillAggregateRelBase.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillAggregateRelBase.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillAggregateRelBase.java
new file mode 100644
index 0000000..4854307
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillAggregateRelBase.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.BitSet;
+import java.util.List;
+
+
+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;
+
+
+/**
+ * Base class for logical and physical Aggregations implemented in Drill
+ */
+public abstract class DrillAggregateRelBase extends AggregateRelBase implements DrillRelNode {
+
+  public DrillAggregateRelBase(RelOptCluster cluster, RelTraitSet traits, RelNode child, BitSet groupSet,
+      List<AggregateCall> aggCalls) throws InvalidRelException {
+    super(cluster, traits, child, groupSet, aggCalls);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillFilterRelBase.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillFilterRelBase.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillFilterRelBase.java
new file mode 100644
index 0000000..955729b
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillFilterRelBase.java
@@ -0,0 +1,55 @@
+/**
+ * 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 org.apache.drill.common.expression.LogicalExpression;
+import org.apache.drill.common.logical.data.Filter;
+import org.apache.drill.common.logical.data.LogicalOperator;
+import org.apache.drill.exec.planner.logical.DrillImplementor;
+import org.apache.drill.exec.planner.logical.DrillOptiq;
+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.FilterRelBase;
+import org.eigenbase.rel.InvalidRelException;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.Convention;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptCost;
+import org.eigenbase.relopt.RelOptPlanner;
+import org.eigenbase.relopt.RelTraitSet;
+import org.eigenbase.rex.RexNode;
+
+/**
+ * Base class for logical and physical Filters implemented in Drill
+ */
+public abstract class DrillFilterRelBase extends FilterRelBase implements DrillRelNode {
+  protected DrillFilterRelBase(Convention convention, RelOptCluster cluster, RelTraitSet traits, RelNode child, RexNode condition) {
+    super(cluster, traits, child, condition);
+    assert getConvention() == convention;
+  }
+  
+  @Override
+  public RelOptCost computeSelfCost(RelOptPlanner planner) {
+    return super.computeSelfCost(planner).multiplyBy(0.1);
+  }
+
+  protected LogicalExpression getFilterExpression(DrillParseContext context){
+    return DrillOptiq.toDrill(context, getChild(), getCondition());
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillJoinRelBase.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillJoinRelBase.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillJoinRelBase.java
new file mode 100644
index 0000000..e88faf9
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillJoinRelBase.java
@@ -0,0 +1,80 @@
+/**
+ * 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.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+
+import org.apache.drill.common.expression.LogicalExpression;
+import org.apache.drill.common.logical.data.NamedExpression;
+import org.apache.drill.exec.planner.logical.DrillOptiq;
+import org.apache.drill.exec.planner.logical.DrillParseContext;
+import org.eigenbase.rel.InvalidRelException;
+import org.eigenbase.rel.JoinRelBase;
+import org.eigenbase.rel.JoinRelType;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.Convention;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptCost;
+import org.eigenbase.relopt.RelOptPlanner;
+import org.eigenbase.relopt.RelTraitSet;
+import org.eigenbase.reltype.RelDataType;
+import org.eigenbase.rex.RexNode;
+import org.eigenbase.util.Pair;
+
+import com.google.common.collect.Lists;
+
+/**
+ * Base class for logical and physical Joins implemented in Drill.
+ */
+public abstract class DrillJoinRelBase extends JoinRelBase implements DrillRelNode {
+  protected final List<Integer> leftKeys = new ArrayList<>();
+  protected final List<Integer> rightKeys = new ArrayList<>();
+
+  public DrillJoinRelBase(RelOptCluster cluster, RelTraitSet traits, RelNode left, RelNode right, RexNode condition,
+      JoinRelType joinType) throws InvalidRelException {
+    super(cluster, traits, left, right, condition, joinType, Collections.<String> emptySet());
+  }
+  
+  
+  /**
+   * 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);
+  }
+
+  protected boolean uniqueFieldNames(RelDataType rowType) {
+    return isUnique(rowType.getFieldNames());
+  }
+
+  protected static <T> boolean isUnique(List<T> list) {
+    return new HashSet<>(list).size() == list.size();
+  }
+  
+  public List<Integer> getLeftKeys() {
+    return this.leftKeys;
+  }
+  
+  public List<Integer> getRightKeys() {
+    return this.rightKeys;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillLimitRel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillLimitRel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillLimitRel.java
deleted file mode 100644
index cfaf350..0000000
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillLimitRel.java
+++ /dev/null
@@ -1,75 +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.math.BigDecimal;
-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.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 DrillLimitRel(RelOptCluster cluster, RelTraitSet traitSet, RelNode child, RexNode offset, RexNode fetch) {
-    super(cluster, traitSet, child);
-    this.offset = offset;
-    this.fetch = fetch;
-  }
-
-  @Override
-  public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
-    return new DrillLimitRel(getCluster(), traitSet, sole(inputs), offset, fetch);
-  }
-
-  @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 DrillLimitRel 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 DrillLimitRel(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/common/DrillLimitRelBase.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillLimitRelBase.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillLimitRelBase.java
new file mode 100644
index 0000000..c64b79b
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillLimitRelBase.java
@@ -0,0 +1,57 @@
+/**
+ * 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.math.BigDecimal;
+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.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;
+
+/**
+ * Base class for logical and physical Limits implemented in Drill
+ */
+public abstract class DrillLimitRelBase extends SingleRel implements DrillRelNode {
+  protected RexNode offset;
+  protected RexNode fetch;
+
+  public DrillLimitRelBase(RelOptCluster cluster, RelTraitSet traitSet, RelNode child, RexNode offset, RexNode fetch) {
+    super(cluster, traitSet, child);
+    this.offset = offset;
+    this.fetch = fetch;
+  }
+
+  public RexNode getOffset() {
+    return this.offset;
+  }
+  
+  public RexNode getFetch() {
+    return this.fetch;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillProjectRelBase.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillProjectRelBase.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillProjectRelBase.java
new file mode 100644
index 0000000..9d02c44
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillProjectRelBase.java
@@ -0,0 +1,69 @@
+/**
+ * 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.apache.drill.common.expression.FieldReference;
+import org.apache.drill.common.expression.LogicalExpression;
+import org.apache.drill.common.logical.data.NamedExpression;
+import org.apache.drill.exec.planner.logical.DrillOptiq;
+import org.apache.drill.exec.planner.logical.DrillParseContext;
+import org.eigenbase.rel.ProjectRelBase;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.Convention;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptCost;
+import org.eigenbase.relopt.RelOptPlanner;
+import org.eigenbase.relopt.RelTraitSet;
+import org.eigenbase.reltype.RelDataType;
+import org.eigenbase.rex.RexNode;
+import org.eigenbase.util.Pair;
+
+import com.google.common.collect.Lists;
+
+/**
+ *
+ * Base class for logical and physical Project implemented in Drill
+ */ 
+public abstract class DrillProjectRelBase extends ProjectRelBase implements DrillRelNode {
+  protected DrillProjectRelBase(Convention convention, RelOptCluster cluster, RelTraitSet traits, RelNode child, List<RexNode> exps,
+      RelDataType rowType) {
+    super(cluster, traits, child, exps, rowType, Flags.BOXED);
+    assert getConvention() == convention;
+  }
+
+  @Override
+  public RelOptCost computeSelfCost(RelOptPlanner planner) {
+    return super.computeSelfCost(planner).multiplyBy(0.1);
+  }
+
+  private List<Pair<RexNode, String>> projects() {
+    return Pair.zip(exps, getRowType().getFieldNames());
+  }
+
+  protected List<NamedExpression> getProjectExpressions(DrillParseContext context) {
+    List<NamedExpression> expressions = Lists.newArrayList();
+    for (Pair<RexNode, String> pair : projects()) {
+      LogicalExpression expr = DrillOptiq.toDrill(context, getChild(), pair.left);
+      expressions.add(new NamedExpression(expr, new FieldReference("output." + pair.right)));
+    }
+    return expressions;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillRelNode.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillRelNode.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillRelNode.java
new file mode 100644
index 0000000..fd04cdd
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillRelNode.java
@@ -0,0 +1,25 @@
+/**
+ * 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 org.eigenbase.rel.RelNode;
+
+public interface DrillRelNode extends RelNode {
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/6b517daa/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillScanRelBase.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillScanRelBase.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillScanRelBase.java
new file mode 100644
index 0000000..b370352
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillScanRelBase.java
@@ -0,0 +1,39 @@
+/**
+ * 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 org.apache.drill.exec.planner.logical.DrillTable;
+import org.eigenbase.rel.TableAccessRelBase;
+import org.eigenbase.relopt.Convention;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptTable;
+import org.eigenbase.relopt.RelTraitSet;
+
+/**
+ * Base class for logical and physical Scans implemented in Drill
+ */
+public abstract class DrillScanRelBase extends TableAccessRelBase implements DrillRelNode {
+  protected final DrillTable drillTable;
+
+  public DrillScanRelBase(Convention convention, RelOptCluster cluster, RelTraitSet traits, RelOptTable table) {
+    super(cluster, traits, table);
+    this.drillTable = table.unwrap(DrillTable.class);
+    assert drillTable != 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/DrillScreenRelBase.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillScreenRelBase.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillScreenRelBase.java
new file mode 100644
index 0000000..51ed442
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillScreenRelBase.java
@@ -0,0 +1,43 @@
+/**
+ * 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 org.eigenbase.rel.RelNode;
+import org.eigenbase.rel.SingleRel;
+import org.eigenbase.relopt.Convention;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptCost;
+import org.eigenbase.relopt.RelOptPlanner;
+import org.eigenbase.relopt.RelTraitSet;
+
+/**
+ * Base class for logical and physical Screen implemented in Drill
+ */
+public abstract class DrillScreenRelBase extends SingleRel implements DrillRelNode {
+
+  public DrillScreenRelBase(Convention convention, RelOptCluster cluster, RelTraitSet traitSet, RelNode input) {
+    super(cluster, traitSet, input);
+    assert input.getConvention() == convention;
+  }
+
+  @Override
+  public RelOptCost computeSelfCost(RelOptPlanner planner) {
+    return super.computeSelfCost(planner).multiplyBy(.1);
+  }
+
+}