You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by li...@apache.org on 2015/07/04 00:25:16 UTC

[01/10] incubator-kylin git commit: KYLIN-780 Upgrade query module, all query test pass

Repository: incubator-kylin
Updated Branches:
  refs/heads/KYLIN-780-0.8 [created] 58b550e06


http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/relnode/OLAPLimitRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPLimitRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPLimitRel.java
index e638fe7..a31cc21 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPLimitRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPLimitRel.java
@@ -20,30 +20,25 @@ package org.apache.kylin.query.relnode;
 
 import java.util.List;
 
-import net.hydromatic.optiq.rules.java.EnumerableConvention;
-import net.hydromatic.optiq.rules.java.EnumerableRel;
-import net.hydromatic.optiq.rules.java.EnumerableRelImplementor;
-import net.hydromatic.optiq.rules.java.JavaRules.EnumerableLimitRel;
-
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.RelWriter;
-import org.eigenbase.rel.SingleRel;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptCost;
-import org.eigenbase.relopt.RelOptPlanner;
-import org.eigenbase.relopt.RelTrait;
-import org.eigenbase.relopt.RelTraitSet;
-import org.eigenbase.rex.RexLiteral;
-import org.eigenbase.rex.RexNode;
+import org.apache.calcite.adapter.enumerable.EnumerableConvention;
+import org.apache.calcite.adapter.enumerable.EnumerableLimit;
+import org.apache.calcite.adapter.enumerable.EnumerableRel;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTrait;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelWriter;
+import org.apache.calcite.rel.SingleRel;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
 
 import com.google.common.base.Preconditions;
 
 /**
- * 
- * @author xjiang
- * 
  */
-public class OLAPLimitRel extends SingleRel implements OLAPRel, EnumerableRel {
+public class OLAPLimitRel extends SingleRel implements OLAPRel {
 
     private final RexNode localOffset; // avoid same name in parent class
     private final RexNode localFetch; // avoid same name in parent class
@@ -75,7 +70,7 @@ public class OLAPLimitRel extends SingleRel implements OLAPRel, EnumerableRel {
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
-        implementor.visitChild(getChild(), this);
+        implementor.visitChild(getInput(), this);
 
         this.columnRowType = buildColumnRowType();
 
@@ -86,30 +81,23 @@ public class OLAPLimitRel extends SingleRel implements OLAPRel, EnumerableRel {
     }
 
     private ColumnRowType buildColumnRowType() {
-        OLAPRel olapChild = (OLAPRel) getChild();
+        OLAPRel olapChild = (OLAPRel) getInput();
         ColumnRowType inputColumnRowType = olapChild.getColumnRowType();
         return inputColumnRowType;
     }
 
     @Override
     public void implementRewrite(RewriteImplementor implementor) {
-        implementor.visitChild(this, getChild());
+        implementor.visitChild(this, getInput());
 
         this.rowType = this.deriveRowType();
         this.columnRowType = buildColumnRowType();
     }
 
     @Override
-    public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
-        OLAPRel childRel = (OLAPRel) getChild();
-        childRel.replaceTraitSet(EnumerableConvention.INSTANCE);
-
-        EnumerableLimitRel enumLimit = new EnumerableLimitRel(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), getChild(), localOffset, localFetch);
-        Result res = enumLimit.implement(implementor, pref);
-
-        childRel.replaceTraitSet(CONVENTION);
-
-        return res;
+    public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
+        return new EnumerableLimit(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
+                sole(inputs), localOffset, localFetch);
     }
 
     @Override
@@ -124,7 +112,7 @@ public class OLAPLimitRel extends SingleRel implements OLAPRel, EnumerableRel {
 
     @Override
     public boolean hasSubQuery() {
-        OLAPRel olapChild = (OLAPRel) getChild();
+        OLAPRel olapChild = (OLAPRel) getInput();
         return olapChild.hasSubQuery();
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java
index 568ecaf..11f995a 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java
@@ -25,44 +25,39 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import net.hydromatic.optiq.rules.java.EnumerableConvention;
-import net.hydromatic.optiq.rules.java.EnumerableRel;
-import net.hydromatic.optiq.rules.java.EnumerableRelImplementor;
-import net.hydromatic.optiq.rules.java.JavaRules.EnumerableCalcRel;
-
-import org.eigenbase.rel.ProjectRelBase;
-import org.eigenbase.rel.RelCollation;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptCost;
-import org.eigenbase.relopt.RelOptPlanner;
-import org.eigenbase.relopt.RelTrait;
-import org.eigenbase.relopt.RelTraitSet;
-import org.eigenbase.reltype.RelDataType;
-import org.eigenbase.reltype.RelDataTypeFactory.FieldInfoBuilder;
-import org.eigenbase.reltype.RelDataTypeField;
-import org.eigenbase.reltype.RelDataTypeFieldImpl;
-import org.eigenbase.rex.RexCall;
-import org.eigenbase.rex.RexInputRef;
-import org.eigenbase.rex.RexLiteral;
-import org.eigenbase.rex.RexNode;
-import org.eigenbase.rex.RexProgram;
-import org.eigenbase.sql.SqlOperator;
-import org.eigenbase.sql.fun.SqlCaseOperator;
-import org.eigenbase.sql.fun.SqlStdOperatorTable;
-import org.eigenbase.sql.validate.SqlUserDefinedFunction;
+import org.apache.calcite.adapter.enumerable.EnumerableCalc;
+import org.apache.calcite.adapter.enumerable.EnumerableConvention;
+import org.apache.calcite.adapter.enumerable.EnumerableRel;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTrait;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory.FieldInfoBuilder;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rel.type.RelDataTypeFieldImpl;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexProgram;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.fun.SqlCaseOperator;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.validate.SqlUserDefinedFunction;
+import org.apache.kylin.metadata.model.TblColRef;
+import org.apache.kylin.metadata.model.TblColRef.InnerDataTypeEnum;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
-import org.apache.kylin.metadata.model.TblColRef;
-import org.apache.kylin.metadata.model.TblColRef.InnerDataTypeEnum;
 
 /**
- * 
- * @author xjiang
- * 
  */
-public class OLAPProjectRel extends ProjectRelBase implements OLAPRel, EnumerableRel {
+public class OLAPProjectRel extends Project implements OLAPRel {
 
     private OLAPContext context;
     private List<RexNode> rewriteProjects;
@@ -98,13 +93,13 @@ public class OLAPProjectRel extends ProjectRelBase implements OLAPRel, Enumerabl
     }
 
     @Override
-    public ProjectRelBase copy(RelTraitSet traitSet, RelNode child, List<RexNode> exps, RelDataType rowType) {
+    public Project copy(RelTraitSet traitSet, RelNode child, List<RexNode> exps, RelDataType rowType) {
         return new OLAPProjectRel(getCluster(), traitSet, child, exps, rowType, this.flags);
     }
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
-        implementor.visitChild(getChild(), this);
+        implementor.visitChild(getInput(), this);
 
         this.context = implementor.getContext();
         this.hasJoin = context.hasJoin;
@@ -117,7 +112,7 @@ public class OLAPProjectRel extends ProjectRelBase implements OLAPRel, Enumerabl
     private ColumnRowType buildColumnRowType() {
         List<TblColRef> columns = new ArrayList<TblColRef>();
         List<Set<TblColRef>> sourceColumns = new ArrayList<Set<TblColRef>>();
-        OLAPRel olapChild = (OLAPRel) getChild();
+        OLAPRel olapChild = (OLAPRel) getInput();
         ColumnRowType inputColumnRowType = olapChild.getColumnRowType();
         for (int i = 0; i < this.rewriteProjects.size(); i++) {
             RexNode rex = this.rewriteProjects.get(i);
@@ -212,24 +207,21 @@ public class OLAPProjectRel extends ProjectRelBase implements OLAPRel, Enumerabl
     }
 
     @Override
-    public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
-        EnumerableCalcRel enumCalcRel;
-
-        RelNode child = getChild();
-        if (child instanceof OLAPFilterRel) {
+    public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
+        if (getInput() instanceof OLAPFilterRel) {
             // merge project & filter
-            OLAPFilterRel filter = (OLAPFilterRel) getChild();
-            RexProgram program = RexProgram.create(filter.getChild().getRowType(), this.rewriteProjects, filter.getCondition(), this.rowType, getCluster().getRexBuilder());
-
-            enumCalcRel = new EnumerableCalcRel(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), filter.getChild(), this.rowType, program, ImmutableList.<RelCollation> of());
+            OLAPFilterRel filter = (OLAPFilterRel) getInput();
+            RelNode inputOfFilter = inputs.get(0).getInput(0);
+            RexProgram program = RexProgram.create(inputOfFilter.getRowType(), this.rewriteProjects, filter.getCondition(), this.rowType, getCluster().getRexBuilder());
+            return new EnumerableCalc(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
+                    inputOfFilter, program, ImmutableList.<RelCollation> of());
         } else {
-            // keep project for tablescan
-            RexProgram program = RexProgram.create(child.getRowType(), this.rewriteProjects, null, this.rowType, getCluster().getRexBuilder());
-
-            enumCalcRel = new EnumerableCalcRel(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), child, this.rowType, program, ImmutableList.<RelCollation> of());
+            // keep project for table scan
+            EnumerableRel input = sole(inputs);
+            RexProgram program = RexProgram.create(input.getRowType(), this.rewriteProjects, null, this.rowType, getCluster().getRexBuilder());
+            return new EnumerableCalc(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
+                    input, program, ImmutableList.<RelCollation> of());
         }
-
-        return enumCalcRel.implement(implementor, pref);
     }
 
     @Override
@@ -239,7 +231,7 @@ public class OLAPProjectRel extends ProjectRelBase implements OLAPRel, Enumerabl
 
     @Override
     public void implementRewrite(RewriteImplementor implementor) {
-        implementor.visitChild(this, getChild());
+        implementor.visitChild(this, getInput());
 
         this.rewriting = true;
 
@@ -253,7 +245,7 @@ public class OLAPProjectRel extends ProjectRelBase implements OLAPRel, Enumerabl
         int paramIndex = this.rowType.getFieldList().size();
         List<RelDataTypeField> newFieldList = new LinkedList<RelDataTypeField>();
         List<RexNode> newExpList = new LinkedList<RexNode>();
-        ColumnRowType inputColumnRowType = ((OLAPRel) getChild()).getColumnRowType();
+        ColumnRowType inputColumnRowType = ((OLAPRel) getInput()).getColumnRowType();
 
         for (Map.Entry<String, RelDataType> rewriteField : this.context.rewriteFields.entrySet()) {
             String rewriteFieldName = rewriteField.getKey();
@@ -266,7 +258,7 @@ public class OLAPProjectRel extends ProjectRelBase implements OLAPRel, Enumerabl
                     RelDataTypeField newField = new RelDataTypeFieldImpl(rewriteFieldName, paramIndex++, fieldType);
                     newFieldList.add(newField);
                     // new project
-                    RelDataTypeField inputField = getChild().getRowType().getFieldList().get(inputIndex);
+                    RelDataTypeField inputField = getInput().getRowType().getFieldList().get(inputIndex);
                     RexInputRef newFieldRef = new RexInputRef(inputField.getIndex(), inputField.getType());
                     newExpList.add(newFieldRef);
                 }
@@ -299,7 +291,7 @@ public class OLAPProjectRel extends ProjectRelBase implements OLAPRel, Enumerabl
 
     @Override
     public boolean hasSubQuery() {
-        OLAPRel olapChild = (OLAPRel) getChild();
+        OLAPRel olapChild = (OLAPRel) getInput();
         return olapChild.hasSubQuery();
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/relnode/OLAPRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPRel.java
index 06cad92..59179df 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPRel.java
@@ -18,23 +18,25 @@
 
 package org.apache.kylin.query.relnode;
 
+import java.util.ArrayList;
+import java.util.IdentityHashMap;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Stack;
 
-import net.hydromatic.optiq.rules.java.EnumerableRel;
-import net.hydromatic.optiq.rules.java.EnumerableRelImplementor;
-
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.relopt.Convention;
-import org.eigenbase.relopt.RelTrait;
-import org.eigenbase.relopt.RelTraitSet;
+import org.apache.calcite.adapter.enumerable.EnumerableRel;
+import org.apache.calcite.adapter.enumerable.EnumerableRelImplementor;
+import org.apache.calcite.plan.Convention;
+import org.apache.calcite.plan.RelTrait;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
 /**
- * 
- * @author xjiang
- * 
  */
 public interface OLAPRel extends RelNode {
 
@@ -137,24 +139,52 @@ public interface OLAPRel extends RelNode {
      */
     public static class JavaImplementor extends EnumerableRelImplementor {
 
-        private OLAPContext parentContext;
+        private IdentityHashMap<EnumerableRel, OLAPContext> relContexts = Maps.newIdentityHashMap();
+        private boolean calciteDebug = System.getProperty("calcite.debug") != null;
 
         public JavaImplementor(EnumerableRelImplementor enumImplementor) {
             super(enumImplementor.getRexBuilder(), new LinkedHashMap<String, Object>());
         }
 
-        public OLAPContext getParentContext() {
-            return parentContext;
+        public EnumerableRel createEnumerable(OLAPRel parent) {
+            ArrayList<EnumerableRel> enumInputs = null;
+            List<RelNode> children = parent.getInputs();
+            if (children != null) {
+                enumInputs = Lists.newArrayListWithCapacity(children.size());
+                for (RelNode child : children) {
+                    enumInputs.add(createEnumerable((OLAPRel) child));
+                }
+            }
+
+            EnumerableRel result = parent.implementEnumerable(enumInputs);
+            relContexts.put(result, parent.getContext());
+            return result;
         }
 
         @Override
         public EnumerableRel.Result visitChild(EnumerableRel parent, int ordinal, EnumerableRel child, EnumerableRel.Prefer prefer) {
-            if (parent instanceof OLAPRel) {
-                OLAPRel olapRel = (OLAPRel) parent;
-                this.parentContext = olapRel.getContext();
+            // OLAPTableScan is shared instance when the same table appears multiple times in the tree.
+            // Its context must be set (or corrected) right before visiting.
+            if (child instanceof OLAPTableScan) {
+                OLAPContext parentContext = relContexts.get(parent);
+                if (parentContext != null) {
+                    ((OLAPTableScan) child).overrideContext(parentContext);
+                }
             }
+
+            if (calciteDebug) {
+                OLAPContext context;
+                if (child instanceof OLAPRel)
+                    context = ((OLAPRel) child).getContext();
+                else
+                    context = relContexts.get(child);
+                System.out.println(context + " - " + child);
+            }
+
             return super.visitChild(parent, ordinal, child, prefer);
         }
     }
 
+    public EnumerableRel implementEnumerable(List<EnumerableRel> inputs);
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java
index 6287f68..fa5dc1d 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java
@@ -18,32 +18,30 @@
 
 package org.apache.kylin.query.relnode;
 
-import net.hydromatic.optiq.rules.java.EnumerableConvention;
-import net.hydromatic.optiq.rules.java.EnumerableRel;
-import net.hydromatic.optiq.rules.java.EnumerableRelImplementor;
-import net.hydromatic.optiq.rules.java.JavaRules.EnumerableSortRel;
-
-import org.eigenbase.rel.RelCollation;
-import org.eigenbase.rel.RelFieldCollation;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.SortRel;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptCost;
-import org.eigenbase.relopt.RelOptPlanner;
-import org.eigenbase.relopt.RelTrait;
-import org.eigenbase.relopt.RelTraitSet;
-import org.eigenbase.rex.RexNode;
-
-import com.google.common.base.Preconditions;
+import java.util.List;
+
+import org.apache.calcite.adapter.enumerable.EnumerableConvention;
+import org.apache.calcite.adapter.enumerable.EnumerableRel;
+import org.apache.calcite.adapter.enumerable.EnumerableSort;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTrait;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rex.RexNode;
 import org.apache.kylin.metadata.model.MeasureDesc;
 import org.apache.kylin.metadata.model.TblColRef;
 import org.apache.kylin.storage.StorageContext;
 
+import com.google.common.base.Preconditions;
+
 /**
- * @author xjiang
- * 
  */
-public class OLAPSortRel extends SortRel implements EnumerableRel, OLAPRel {
+public class OLAPSortRel extends Sort implements OLAPRel {
 
     private ColumnRowType columnRowType;
     private OLAPContext context;
@@ -66,26 +64,26 @@ public class OLAPSortRel extends SortRel implements EnumerableRel, OLAPRel {
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
-        implementor.visitChild(getChild(), this);
+        implementor.visitChild(getInput(), this);
 
         this.context = implementor.getContext();
         this.columnRowType = buildColumnRowType();
     }
 
     private ColumnRowType buildColumnRowType() {
-        OLAPRel olapChild = (OLAPRel) getChild();
+        OLAPRel olapChild = (OLAPRel) getInput();
         ColumnRowType inputColumnRowType = olapChild.getColumnRowType();
         return inputColumnRowType;
     }
 
     @Override
     public void implementRewrite(RewriteImplementor implementor) {
-        implementor.visitChild(this, getChild());
+        implementor.visitChild(this, getInput());
 
         for (RelFieldCollation fieldCollation : this.collation.getFieldCollations()) {
             int index = fieldCollation.getFieldIndex();
             StorageContext.OrderEnum order = getOrderEnum(fieldCollation.getDirection());
-            OLAPRel olapChild = (OLAPRel) this.getChild();
+            OLAPRel olapChild = (OLAPRel) this.getInput();
             TblColRef orderCol = olapChild.getColumnRowType().getAllColumns().get(index);
             MeasureDesc measure = findMeasure(orderCol);
             if (measure != null) {
@@ -116,17 +114,9 @@ public class OLAPSortRel extends SortRel implements EnumerableRel, OLAPRel {
     }
 
     @Override
-    public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
-        OLAPRel childRel = (OLAPRel) getChild();
-        childRel.replaceTraitSet(EnumerableConvention.INSTANCE);
-
-        EnumerableSortRel enumSort = new EnumerableSortRel(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE, collation), getChild(), collation, offset, fetch);
-
-        Result res = enumSort.implement(implementor, pref);
-
-        childRel.replaceTraitSet(OLAPRel.CONVENTION);
-
-        return res;
+    public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
+        return new EnumerableSort(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE, collation), //
+                sole(inputs), collation, offset, fetch);
     }
 
     @Override
@@ -141,7 +131,7 @@ public class OLAPSortRel extends SortRel implements EnumerableRel, OLAPRel {
 
     @Override
     public boolean hasSubQuery() {
-        OLAPRel olapChild = (OLAPRel) getChild();
+        OLAPRel olapChild = (OLAPRel) getInput();
         return olapChild.hasSubQuery();
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/relnode/OLAPTableScan.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPTableScan.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPTableScan.java
index b360ce4..9d62ebc 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPTableScan.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPTableScan.java
@@ -22,14 +22,32 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
-import net.hydromatic.linq4j.expressions.Blocks;
-import net.hydromatic.linq4j.expressions.Expressions;
-import net.hydromatic.linq4j.expressions.Primitive;
-import net.hydromatic.optiq.rules.java.EnumerableRel;
-import net.hydromatic.optiq.rules.java.EnumerableRelImplementor;
-import net.hydromatic.optiq.rules.java.PhysType;
-import net.hydromatic.optiq.rules.java.PhysTypeImpl;
-
+import org.apache.calcite.adapter.enumerable.EnumerableRel;
+import org.apache.calcite.adapter.enumerable.EnumerableRelImplementor;
+import org.apache.calcite.adapter.enumerable.PhysType;
+import org.apache.calcite.adapter.enumerable.PhysTypeImpl;
+import org.apache.calcite.linq4j.tree.Blocks;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.linq4j.tree.MethodCallExpression;
+import org.apache.calcite.linq4j.tree.Primitive;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.plan.RelTrait;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.plan.volcano.AbstractConverter.ExpandConversionRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelWriter;
+import org.apache.calcite.rel.core.TableScan;
+import org.apache.calcite.rel.rules.AggregateExpandDistinctAggregatesRule;
+import org.apache.calcite.rel.rules.FilterJoinRule;
+import org.apache.calcite.rel.rules.FilterProjectTransposeRule;
+import org.apache.calcite.rel.rules.JoinCommuteRule;
+import org.apache.calcite.rel.rules.JoinPushThroughJoinRule;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeField;
 import org.apache.kylin.metadata.model.ColumnDesc;
 import org.apache.kylin.metadata.model.TblColRef;
 import org.apache.kylin.query.optrule.OLAPAggregateRule;
@@ -41,31 +59,12 @@ import org.apache.kylin.query.optrule.OLAPSortRule;
 import org.apache.kylin.query.optrule.OLAPToEnumerableConverterRule;
 import org.apache.kylin.query.schema.OLAPSchema;
 import org.apache.kylin.query.schema.OLAPTable;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.RelWriter;
-import org.eigenbase.rel.TableAccessRelBase;
-import org.eigenbase.rel.rules.PushFilterPastJoinRule;
-import org.eigenbase.rel.rules.PushFilterPastProjectRule;
-import org.eigenbase.rel.rules.PushJoinThroughJoinRule;
-import org.eigenbase.rel.rules.RemoveDistinctAggregateRule;
-import org.eigenbase.rel.rules.SwapJoinRule;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptCost;
-import org.eigenbase.relopt.RelOptPlanner;
-import org.eigenbase.relopt.RelOptTable;
-import org.eigenbase.relopt.RelTrait;
-import org.eigenbase.relopt.RelTraitSet;
-import org.eigenbase.relopt.volcano.AbstractConverter.ExpandConversionRule;
-import org.eigenbase.reltype.RelDataType;
-import org.eigenbase.reltype.RelDataTypeFactory;
-import org.eigenbase.reltype.RelDataTypeField;
 
 import com.google.common.base.Preconditions;
 
 /**
- * @author xjiang
  */
-public class OLAPTableScan extends TableAccessRelBase implements OLAPRel, EnumerableRel {
+public class OLAPTableScan extends TableScan implements OLAPRel, EnumerableRel {
 
     private final OLAPTable olapTable;
     private final String tableName;
@@ -98,6 +97,10 @@ public class OLAPTableScan extends TableAccessRelBase implements OLAPRel, Enumer
         return context;
     }
 
+    void overrideContext(OLAPContext context) {
+        this.context = context;
+    }
+
     @Override
     public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
         Preconditions.checkArgument(inputs.isEmpty());
@@ -119,20 +122,19 @@ public class OLAPTableScan extends TableAccessRelBase implements OLAPRel, Enumer
         planner.addRule(OLAPSortRule.INSTANCE);
 
         // since join is the entry point, we can't push filter past join
-        planner.removeRule(PushFilterPastJoinRule.FILTER_ON_JOIN);
-        planner.removeRule(PushFilterPastJoinRule.JOIN);
+        planner.removeRule(FilterJoinRule.FILTER_ON_JOIN);
+        planner.removeRule(FilterJoinRule.JOIN);
 
         // TODO : since we don't have statistic of table, the optimization of join is too cost
-        planner.removeRule(SwapJoinRule.INSTANCE);
-        planner.removeRule(PushJoinThroughJoinRule.LEFT);
-        planner.removeRule(PushJoinThroughJoinRule.RIGHT);
+        planner.removeRule(JoinCommuteRule.INSTANCE);
+        planner.removeRule(JoinPushThroughJoinRule.LEFT);
+        planner.removeRule(JoinPushThroughJoinRule.RIGHT);
 
-        // for columns in having clause will enable table scan filter rule
-        // cause kylin does not depend on MPP
-        planner.removeRule(PushFilterPastProjectRule.INSTANCE);
+        // for columns in having clause will enable table scan filter rule cause kylin does not depend on MPP
+        planner.removeRule(FilterProjectTransposeRule.INSTANCE);
         // distinct count will be split into a separated query that is joined with the left query
-        planner.removeRule(RemoveDistinctAggregateRule.INSTANCE);
-        
+        planner.removeRule(AggregateExpandDistinctAggregatesRule.INSTANCE);
+
         // see Dec 26th email @ http://mail-archives.apache.org/mod_mbox/calcite-dev/201412.mbox/browser
         planner.removeRule(ExpandConversionRule.INSTANCE);
     }
@@ -189,27 +191,22 @@ public class OLAPTableScan extends TableAccessRelBase implements OLAPRel, Enumer
     }
 
     @Override
-    public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
-        if (!(implementor instanceof JavaImplementor))
-            throw new IllegalStateException("implementor is not JavaImplementor");
-        
-        JavaImplementor javaImplementor = (JavaImplementor) implementor;
-
-        int ctxId = this.context.id;
-        if (javaImplementor.getParentContext() != null) {
-            ctxId = javaImplementor.getParentContext().id;
-        }
+    public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
+        return this;
+    }
 
-        PhysType physType = PhysTypeImpl.of(javaImplementor.getTypeFactory(), this.rowType, pref.preferArray());
+    @Override
+    public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
+        PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), this.rowType, pref.preferArray());
 
         String execFunction = genExecFunc();
 
-        return javaImplementor.result(physType, Blocks.toBlock(Expressions.call(table.getExpression(OLAPTable.class), execFunction, javaImplementor.getRootExpression(), Expressions.constant(ctxId))));
+        MethodCallExpression exprCall = Expressions.call(table.getExpression(OLAPTable.class), execFunction, implementor.getRootExpression(), Expressions.constant(context.id));
+        return implementor.result(physType, Blocks.toBlock(exprCall));
     }
 
     private String genExecFunc() {
-        // if the table to scan is not the fact table of cube, then it's a
-        // lookup table
+        // if the table to scan is not the fact table of cube, then it's a lookup table
         if (context.hasJoin == false && tableName.equalsIgnoreCase(context.realization.getFactTable()) == false) {
             return "executeLookupTableQuery";
         } else {
@@ -236,7 +233,7 @@ public class OLAPTableScan extends TableAccessRelBase implements OLAPRel, Enumer
 
         for (Map.Entry<String, RelDataType> rewriteField : rewriteFields.entrySet()) {
             String fieldName = rewriteField.getKey();
-            RelDataTypeField field = rowType.getField(fieldName, true);
+            RelDataTypeField field = rowType.getField(fieldName, true, false);
             if (field != null) {
                 RelDataType fieldType = field.getType();
                 rewriteField.setValue(fieldType);

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java
index 7a38f40..19eb7f9 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java
@@ -20,27 +20,31 @@ package org.apache.kylin.query.relnode;
 
 import java.util.List;
 
-import net.hydromatic.linq4j.expressions.Blocks;
-import net.hydromatic.linq4j.expressions.Expressions;
-import net.hydromatic.optiq.rules.java.EnumerableRel;
-import net.hydromatic.optiq.rules.java.EnumerableRelImplementor;
-import net.hydromatic.optiq.rules.java.PhysType;
-import net.hydromatic.optiq.rules.java.PhysTypeImpl;
-
+import org.apache.calcite.adapter.enumerable.EnumerableRel;
+import org.apache.calcite.adapter.enumerable.EnumerableRelImplementor;
+import org.apache.calcite.adapter.enumerable.PhysType;
+import org.apache.calcite.adapter.enumerable.PhysTypeImpl;
+import org.apache.calcite.linq4j.tree.Blocks;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.plan.ConventionTraitDef;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.convert.ConverterImpl;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.SqlExplainLevel;
+import org.apache.kylin.metadata.realization.IRealization;
 import org.apache.kylin.query.routing.NoRealizationFoundException;
 import org.apache.kylin.query.routing.QueryRouter;
 import org.apache.kylin.query.schema.OLAPTable;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.convert.ConverterRelImpl;
-import org.eigenbase.relopt.*;
-import org.eigenbase.reltype.RelDataType;
-
-import org.apache.kylin.metadata.realization.IRealization;
 
 /**
- * @author xjiang
  */
-public class OLAPToEnumerableConverter extends ConverterRelImpl implements EnumerableRel {
+public class OLAPToEnumerableConverter extends ConverterImpl implements EnumerableRel {
 
     public OLAPToEnumerableConverter(RelOptCluster cluster, RelTraitSet traits, RelNode input) {
         super(cluster, ConventionTraitDef.INSTANCE, traits, input);
@@ -60,7 +64,7 @@ public class OLAPToEnumerableConverter extends ConverterRelImpl implements Enume
     public Result implement(EnumerableRelImplementor enumImplementor, Prefer pref) {
         // post-order travel children
         OLAPRel.OLAPImplementor olapImplementor = new OLAPRel.OLAPImplementor();
-        olapImplementor.visitChild(getChild(), this);
+        olapImplementor.visitChild(getInput(), this);
 
         // find cube from olap context
         try {
@@ -80,13 +84,20 @@ public class OLAPToEnumerableConverter extends ConverterRelImpl implements Enume
 
         // rewrite query if necessary
         OLAPRel.RewriteImplementor rewriteImplementor = new OLAPRel.RewriteImplementor();
-        rewriteImplementor.visitChild(this, getChild());
-
-        // build java implementation
-        EnumerableRel child = (EnumerableRel) getChild();
-        OLAPRel.JavaImplementor javaImplementor = new OLAPRel.JavaImplementor(enumImplementor);
-        return javaImplementor.visitChild(this, 0, child, pref);
+        rewriteImplementor.visitChild(this, getInput());
+
+        // implement as EnumerableRel
+        OLAPRel.JavaImplementor impl = new OLAPRel.JavaImplementor(enumImplementor);
+        EnumerableRel inputAsEnum = impl.createEnumerable((OLAPRel) getInput());
+        this.replaceInput(0, inputAsEnum);
+        
+        if (System.getProperty("calcite.debug") != null) {
+            String dumpPlan = RelOptUtil.dumpPlan("", this, false, SqlExplainLevel.DIGEST_ATTRIBUTES);
+            System.out.println("EXECUTION PLAN AFTER REWRITE");
+            System.out.println(dumpPlan);
+        }
 
+        return impl.visitChild(this, 0, inputAsEnum, pref);
     }
 
     private Result buildHiveResult(EnumerableRelImplementor enumImplementor, Prefer pref, OLAPContext context) {

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/schema/OLAPSchema.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/schema/OLAPSchema.java b/query/src/main/java/org/apache/kylin/query/schema/OLAPSchema.java
index 332a5ff..27d456f 100644
--- a/query/src/main/java/org/apache/kylin/query/schema/OLAPSchema.java
+++ b/query/src/main/java/org/apache/kylin/query/schema/OLAPSchema.java
@@ -22,9 +22,8 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
-import net.hydromatic.optiq.Table;
-import net.hydromatic.optiq.impl.AbstractSchema;
-
+import org.apache.calcite.schema.Table;
+import org.apache.calcite.schema.impl.AbstractSchema;
 import org.apache.kylin.common.KylinConfig;
 import org.apache.kylin.cube.CubeManager;
 import org.apache.kylin.metadata.MetadataManager;
@@ -33,7 +32,6 @@ import org.apache.kylin.metadata.project.ProjectInstance;
 import org.apache.kylin.metadata.project.ProjectManager;
 
 /**
- * @author xjiang
  */
 public class OLAPSchema extends AbstractSchema {
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/schema/OLAPSchemaFactory.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/schema/OLAPSchemaFactory.java b/query/src/main/java/org/apache/kylin/query/schema/OLAPSchemaFactory.java
index 1845beb..f8711b1 100644
--- a/query/src/main/java/org/apache/kylin/query/schema/OLAPSchemaFactory.java
+++ b/query/src/main/java/org/apache/kylin/query/schema/OLAPSchemaFactory.java
@@ -25,12 +25,10 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
-import net.hydromatic.optiq.Schema;
-import net.hydromatic.optiq.SchemaFactory;
-import net.hydromatic.optiq.SchemaPlus;
-
-import org.eigenbase.util14.ConversionUtil;
-
+import org.apache.calcite.schema.Schema;
+import org.apache.calcite.schema.SchemaFactory;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.util.ConversionUtil;
 import org.apache.kylin.common.KylinConfig;
 import org.apache.kylin.metadata.model.DatabaseDesc;
 import org.apache.kylin.metadata.model.TableDesc;
@@ -38,7 +36,6 @@ import org.apache.kylin.metadata.project.ProjectInstance;
 import org.apache.kylin.metadata.project.ProjectManager;
 
 /**
- * @author xjiang
  */
 public class OLAPSchemaFactory implements SchemaFactory {
 
@@ -52,7 +49,8 @@ public class OLAPSchemaFactory implements SchemaFactory {
          * object and does not require additional codec.
          * 
          * Ref SaffronProperties.defaultCharset
-         * SqlUtil.translateCharacterSetName() NlsString constructor()
+         * Ref SqlUtil.translateCharacterSetName() 
+         * Ref NlsString constructor()
          */
         System.setProperty("saffron.default.charset", ConversionUtil.NATIVE_UTF16_CHARSET_NAME);
         System.setProperty("saffron.default.nationalcharset", ConversionUtil.NATIVE_UTF16_CHARSET_NAME);
@@ -109,13 +107,7 @@ public class OLAPSchemaFactory implements SchemaFactory {
                 out.write("            \"factory\": \"org.apache.kylin.query.schema.OLAPSchemaFactory\",\n");
                 out.write("            \"operand\": {\n");
                 out.write("                \"" + SCHEMA_PROJECT + "\": \"" + project + "\"\n");
-                out.write("            },\n");
-                out.write("           \"functions\": [\n");
-                out.write("                 {\n");
-                out.write("                     \"name\": \"QUARTER\",\n");
-                out.write("                     \"className\": \"org.apache.kylin.query.sqlfunc.QuarterFunc\"\n");
-                out.write("                 }\n");
-                out.write("            ]\n");
+                out.write("            }\n");
                 out.write("        }\n");
 
                 if (++counter != schemaCounts.size()) {

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/schema/OLAPTable.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/schema/OLAPTable.java b/query/src/main/java/org/apache/kylin/query/schema/OLAPTable.java
index 6979a9b..25a7b24 100644
--- a/query/src/main/java/org/apache/kylin/query/schema/OLAPTable.java
+++ b/query/src/main/java/org/apache/kylin/query/schema/OLAPTable.java
@@ -19,33 +19,30 @@
 package org.apache.kylin.query.schema;
 
 import java.util.ArrayList;
-import java.util.BitSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 
-import net.hydromatic.linq4j.Enumerable;
-import net.hydromatic.linq4j.Enumerator;
-import net.hydromatic.linq4j.QueryProvider;
-import net.hydromatic.linq4j.Queryable;
-import net.hydromatic.optiq.DataContext;
-import net.hydromatic.optiq.SchemaPlus;
-import net.hydromatic.optiq.Statistic;
-import net.hydromatic.optiq.Statistics;
-import net.hydromatic.optiq.TranslatableTable;
-import net.hydromatic.optiq.impl.AbstractTableQueryable;
-import net.hydromatic.optiq.impl.java.AbstractQueryableTable;
-
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.relopt.RelOptTable;
-import org.eigenbase.relopt.RelOptTable.ToRelContext;
-import org.eigenbase.reltype.RelDataType;
-import org.eigenbase.reltype.RelDataTypeFactory;
-import org.eigenbase.sql.type.SqlTypeName;
-import org.eigenbase.sql.type.SqlTypeUtil;
-
-import com.google.common.collect.Lists;
+import org.apache.calcite.DataContext;
+import org.apache.calcite.adapter.java.AbstractQueryableTable;
+import org.apache.calcite.linq4j.Enumerable;
+import org.apache.calcite.linq4j.Enumerator;
+import org.apache.calcite.linq4j.QueryProvider;
+import org.apache.calcite.linq4j.Queryable;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.plan.RelOptTable.ToRelContext;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.Statistic;
+import org.apache.calcite.schema.Statistics;
+import org.apache.calcite.schema.TranslatableTable;
+import org.apache.calcite.schema.impl.AbstractTableQueryable;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.calcite.util.ImmutableBitSet;
 import org.apache.kylin.metadata.model.ColumnDesc;
 import org.apache.kylin.metadata.model.FunctionDesc;
 import org.apache.kylin.metadata.model.MeasureDesc;
@@ -55,10 +52,9 @@ import org.apache.kylin.query.enumerator.OLAPQuery;
 import org.apache.kylin.query.enumerator.OLAPQuery.EnumeratorTypeEnum;
 import org.apache.kylin.query.relnode.OLAPTableScan;
 
+import com.google.common.collect.Lists;
+
 /**
- * 
- * @author xjiang
- * 
  */
 public class OLAPTable extends AbstractQueryableTable implements TranslatableTable {
 
@@ -122,12 +118,12 @@ public class OLAPTable extends AbstractQueryableTable implements TranslatableTab
         if (this.rowType == null) {
             // always build exposedColumns and rowType together
             this.exposedColumns = listSourceColumns();
-            this.rowType = deduceRowType(typeFactory);
+            this.rowType = deriveRowType(typeFactory);
         }
         return this.rowType;
     }
 
-    private RelDataType deduceRowType(RelDataTypeFactory typeFactory) {
+    private RelDataType deriveRowType(RelDataTypeFactory typeFactory) {
         RelDataTypeFactory.FieldInfoBuilder fieldInfo = typeFactory.builder();
         for (ColumnDesc column : exposedColumns) {
             RelDataType sqlType = createSqlType(typeFactory, column);
@@ -210,7 +206,7 @@ public class OLAPTable extends AbstractQueryableTable implements TranslatableTab
 
     @Override
     public Statistic getStatistic() {
-        List<BitSet> keys = new ArrayList<BitSet>();
+        List<ImmutableBitSet> keys = new ArrayList<ImmutableBitSet>();
         return Statistics.of(100, keys);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/sqlfunc/QuarterBase.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/sqlfunc/QuarterBase.java b/query/src/main/java/org/apache/kylin/query/sqlfunc/QuarterBase.java
deleted file mode 100644
index 1bd0dff..0000000
--- a/query/src/main/java/org/apache/kylin/query/sqlfunc/QuarterBase.java
+++ /dev/null
@@ -1,38 +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.kylin.query.sqlfunc;
-
-import net.hydromatic.optiq.runtime.SqlFunctions;
-import net.hydromatic.optiq.runtime.SqlFunctions.TimeUnitRange;
-
-/**
- * @author xjiang
- * 
- */
-public abstract class QuarterBase {
-
-    /**
-     * According to jvm spec, it return self method before parent.
-     * So, we keep Date in parent and int in child
-     */
-    public static long eval(int date) {
-        long month = SqlFunctions.unixDateExtract(TimeUnitRange.MONTH, date);
-        return month / 4 + 1;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/sqlfunc/QuarterFunc.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/sqlfunc/QuarterFunc.java b/query/src/main/java/org/apache/kylin/query/sqlfunc/QuarterFunc.java
deleted file mode 100644
index 179caf1..0000000
--- a/query/src/main/java/org/apache/kylin/query/sqlfunc/QuarterFunc.java
+++ /dev/null
@@ -1,34 +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.kylin.query.sqlfunc;
-
-import java.sql.Date;
-
-/**
- * @author xjiang
- * 
- */
-public class QuarterFunc extends QuarterBase {
-    private QuarterFunc() {
-    }
-
-    public static long eval(Date date) {
-        throw new UnsupportedOperationException();
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/test/java/org/apache/kylin/query/test/KylinTestBase.java
----------------------------------------------------------------------
diff --git a/query/src/test/java/org/apache/kylin/query/test/KylinTestBase.java b/query/src/test/java/org/apache/kylin/query/test/KylinTestBase.java
index 0f5c1ec..14c16b8 100644
--- a/query/src/test/java/org/apache/kylin/query/test/KylinTestBase.java
+++ b/query/src/test/java/org/apache/kylin/query/test/KylinTestBase.java
@@ -403,7 +403,7 @@ public class KylinTestBase {
         int count = executeQuery(sql, true);
 
         if (debug) {
-            System.clearProperty("optiq.debug");
+            System.clearProperty("calcite.debug");
         }
         return count;
     }

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/server/src/main/java/org/apache/kylin/rest/controller/QueryController.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/kylin/rest/controller/QueryController.java b/server/src/main/java/org/apache/kylin/rest/controller/QueryController.java
index ca27b20..9088c95 100644
--- a/server/src/main/java/org/apache/kylin/rest/controller/QueryController.java
+++ b/server/src/main/java/org/apache/kylin/rest/controller/QueryController.java
@@ -28,7 +28,6 @@ import org.apache.kylin.common.KylinConfig;
 import org.apache.kylin.common.debug.BackdoorToggles;
 import org.apache.kylin.cube.CubeInstance;
 import org.apache.kylin.rest.constant.Constant;
-import org.apache.kylin.rest.exception.ForbiddenException;
 import org.apache.kylin.rest.exception.InternalErrorException;
 import org.apache.kylin.rest.model.Query;
 import org.apache.kylin.rest.model.SelectedColumnMeta;
@@ -204,25 +203,24 @@ public class QueryController extends BasicController {
             }
 
             checkQueryAuth(sqlResponse);
-            return sqlResponse;
-        } catch (AccessDeniedException ade) {
-            // Access exception is bind with each user, it will not be cached
-            logger.error("Exception when execute sql", ade);
-            throw new ForbiddenException(ade.getLocalizedMessage());
-        } catch (ScanOutOfLimitException e) {
-
-            //for exception queries, only cache ScanOutOfLimitException
-            SQLResponse exceptionRes = new SQLResponse(null, null, 0, true, e.getMessage());
-            Cache exceptionCache = cacheManager.getCache(EXCEPTION_QUERY_CACHE);
-            exceptionCache.put(new Element(sqlRequest, exceptionRes));
-
-            logger.error("Exception when execute sql", e);
-            throw new InternalErrorException(QueryUtil.makeErrorMsgUserFriendly(e.getLocalizedMessage()));
 
         } catch (Throwable e) { // calcite may throw AssertError
             logger.error("Exception when execute sql", e);
-            throw new InternalErrorException(QueryUtil.makeErrorMsgUserFriendly(e.getLocalizedMessage()));
+            String errMsg = QueryUtil.makeErrorMsgUserFriendly(e);
+
+            sqlResponse = new SQLResponse(null, null, 0, true, errMsg);
+
+            // for exception queries, only cache ScanOutOfLimitException
+            if (e instanceof ScanOutOfLimitException) {
+                Cache exceptionCache = cacheManager.getCache(EXCEPTION_QUERY_CACHE);
+                exceptionCache.put(new Element(sqlRequest, sqlResponse));
+            }
         }
+
+        if (sqlResponse.getIsException())
+            throw new InternalErrorException(sqlResponse.getExceptionMessage());
+        else
+            return sqlResponse;
     }
 
     private SQLResponse searchQueryInCache(SQLRequest sqlRequest) {

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/server/src/main/java/org/apache/kylin/rest/service/BasicService.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/kylin/rest/service/BasicService.java b/server/src/main/java/org/apache/kylin/rest/service/BasicService.java
index 23705da..e9e35e2 100644
--- a/server/src/main/java/org/apache/kylin/rest/service/BasicService.java
+++ b/server/src/main/java/org/apache/kylin/rest/service/BasicService.java
@@ -18,18 +18,32 @@
 
 package org.apache.kylin.rest.service;
 
-import com.google.common.base.Function;
-import com.google.common.base.Predicate;
-import com.google.common.collect.FluentIterable;
-import com.google.common.collect.Lists;
-import com.google.common.io.Files;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import javax.sql.DataSource;
+
 import net.sf.ehcache.CacheManager;
+
 import org.apache.commons.lang3.StringUtils;
 import org.apache.kylin.common.KylinConfig;
 import org.apache.kylin.cube.CubeDescManager;
 import org.apache.kylin.cube.CubeManager;
 import org.apache.kylin.invertedindex.IIDescManager;
 import org.apache.kylin.invertedindex.IIManager;
+import org.apache.kylin.jdbc.Driver;
 import org.apache.kylin.job.cube.CubingJob;
 import org.apache.kylin.job.execution.AbstractExecutable;
 import org.apache.kylin.job.execution.ExecutableState;
@@ -47,17 +61,11 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.datasource.DriverManagerDataSource;
 
-import javax.sql.DataSource;
-import java.io.File;
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.*;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.Lists;
+import com.google.common.io.Files;
 
 public abstract class BasicService {
 
@@ -68,9 +76,6 @@ public abstract class BasicService {
     @Autowired
     private CacheManager cacheManager;
 
-    //    @Autowired
-    //    protected JdbcTemplate jdbcTemplate;
-
     public KylinConfig getConfig() {
         return KylinConfig.getInstanceFromEnv();
     }
@@ -132,7 +137,7 @@ public abstract class BasicService {
             Properties props = new Properties();
             props.setProperty(OLAPQuery.PROP_SCAN_THRESHOLD, String.valueOf(KylinConfig.getInstanceFromEnv().getScanThreshold()));
             ds.setConnectionProperties(props);
-            ds.setDriverClassName("net.hydromatic.optiq.jdbc.Driver");
+            ds.setDriverClassName(Driver.class.getName());
             ds.setUrl("jdbc:calcite:model=" + modelJson.getAbsolutePath());
 
             ret = olapDataSources.putIfAbsent(project, ds);

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/server/src/main/java/org/apache/kylin/rest/service/ModelService.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/kylin/rest/service/ModelService.java b/server/src/main/java/org/apache/kylin/rest/service/ModelService.java
index 68a05fb..fdf09f6 100644
--- a/server/src/main/java/org/apache/kylin/rest/service/ModelService.java
+++ b/server/src/main/java/org/apache/kylin/rest/service/ModelService.java
@@ -19,6 +19,10 @@
 package org.apache.kylin.rest.service;
 
 
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.kylin.cube.model.CubeDesc;
 import org.apache.kylin.invertedindex.model.IIDesc;
 import org.apache.kylin.metadata.model.DataModelDesc;
@@ -26,26 +30,18 @@ import org.apache.kylin.metadata.project.ProjectInstance;
 import org.apache.kylin.rest.constant.Constant;
 import org.apache.kylin.rest.exception.InternalErrorException;
 import org.apache.kylin.rest.security.AclPermission;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PostFilter;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Component;
 
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
 /**
  * @author jiazhong
  */
 @Component("modelMgmtService")
 public class ModelService extends BasicService {
 
-    private static final Logger logger = LoggerFactory.getLogger(ModelService.class);
-
     @Autowired
     private AccessService accessService;
 
@@ -61,7 +57,7 @@ public class ModelService extends BasicService {
             project.getModels();
         }
 
-        List<DataModelDesc> filterModels = new ArrayList();
+        List<DataModelDesc> filterModels = new ArrayList<DataModelDesc>();
         for (DataModelDesc modelDesc : models) {
             boolean isModelMatch = (null == modelName) || modelDesc.getName().toLowerCase().contains(modelName.toLowerCase());
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/server/src/main/java/org/apache/kylin/rest/service/QueryService.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/kylin/rest/service/QueryService.java b/server/src/main/java/org/apache/kylin/rest/service/QueryService.java
index 8f81d2d..de413c7 100644
--- a/server/src/main/java/org/apache/kylin/rest/service/QueryService.java
+++ b/server/src/main/java/org/apache/kylin/rest/service/QueryService.java
@@ -32,14 +32,19 @@ import java.util.*;
 
 import javax.sql.DataSource;
 
-import net.hydromatic.avatica.ColumnMetaData.Rep;
-
+import org.apache.calcite.avatica.ColumnMetaData.Rep;
 import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.hbase.client.Get;
 import org.apache.hadoop.hbase.client.HTableInterface;
 import org.apache.hadoop.hbase.client.Put;
 import org.apache.hadoop.hbase.client.Result;
+import org.apache.kylin.common.KylinConfig;
+import org.apache.kylin.common.persistence.HBaseConnection;
 import org.apache.kylin.common.util.Bytes;
+import org.apache.kylin.cube.CubeInstance;
+import org.apache.kylin.cube.CubeManager;
+import org.apache.kylin.cube.cuboid.Cuboid;
+import org.apache.kylin.query.relnode.OLAPContext;
 import org.apache.kylin.rest.constant.Constant;
 import org.apache.kylin.rest.metrics.QueryMetrics;
 import org.apache.kylin.rest.model.ColumnMeta;
@@ -59,13 +64,6 @@ import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Component;
 
-import org.apache.kylin.common.KylinConfig;
-import org.apache.kylin.common.persistence.HBaseConnection;
-import org.apache.kylin.cube.CubeInstance;
-import org.apache.kylin.cube.CubeManager;
-import org.apache.kylin.cube.cuboid.Cuboid;
-import org.apache.kylin.query.relnode.OLAPContext;
-
 /**
  * @author xduo
  */

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/server/src/main/java/org/apache/kylin/rest/util/QueryUtil.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/kylin/rest/util/QueryUtil.java b/server/src/main/java/org/apache/kylin/rest/util/QueryUtil.java
index dd3ffa9..994fe03 100644
--- a/server/src/main/java/org/apache/kylin/rest/util/QueryUtil.java
+++ b/server/src/main/java/org/apache/kylin/rest/util/QueryUtil.java
@@ -149,12 +149,22 @@ public class QueryUtil {
         return null;
     }
 
-    /**
-     * adjust error message order
-     * 
-     * @param errorMsg
-     * @return
-     */
+    public static String makeErrorMsgUserFriendly(Throwable e) {
+        String msg = e.getMessage();
+        
+        // pick ParseException error message if possible
+        Throwable cause = e;
+        while (cause != null) {
+            if (cause.getClass().getName().contains("ParseException")) {
+                msg = cause.getMessage();
+                break;
+            }
+            cause = cause.getCause();
+        }
+        
+        return makeErrorMsgUserFriendly(msg);
+    }
+
     public static String makeErrorMsgUserFriendly(String errorMsg) {
         try {
             // make one line


[10/10] incubator-kylin git commit: KYLIN-780 bug fix, replace 'executeIndexQuery' with 'executeOLAPQuery'

Posted by li...@apache.org.
KYLIN-780 bug fix, replace 'executeIndexQuery' with 'executeOLAPQuery'


Project: http://git-wip-us.apache.org/repos/asf/incubator-kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kylin/commit/58b550e0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kylin/tree/58b550e0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kylin/diff/58b550e0

Branch: refs/heads/KYLIN-780-0.8
Commit: 58b550e0612dac5c3aa30fc6df39363e4e68ebc2
Parents: 22a0bc3
Author: Li, Yang <ya...@ebay.com>
Authored: Sat Jul 4 06:23:33 2015 +0800
Committer: Li, Yang <ya...@ebay.com>
Committed: Sat Jul 4 06:23:33 2015 +0800

----------------------------------------------------------------------
 .../src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/58b550e0/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
index 3c57fdb..24fd28e 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
@@ -231,7 +231,7 @@ public class OLAPJoinRel extends EnumerableJoin implements OLAPRel {
     public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
         PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());
         RelOptTable factTable = context.firstTableScan.getTable();
-        MethodCallExpression exprCall = Expressions.call(factTable.getExpression(OLAPTable.class), "executeIndexQuery", implementor.getRootExpression(), Expressions.constant(context.id));
+        MethodCallExpression exprCall = Expressions.call(factTable.getExpression(OLAPTable.class), "executeOLAPQuery", implementor.getRootExpression(), Expressions.constant(context.id));
         return implementor.result(physType, Blocks.toBlock(exprCall));
     }
 


[03/10] incubator-kylin git commit: KYLIN-780 Upgrade query module, all query test pass

Posted by li...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/stub/KylinClient.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/KylinClient.java b/jdbc/src/main/java/org/apache/kylin/jdbc/stub/KylinClient.java
deleted file mode 100644
index ccbfe5d..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/KylinClient.java
+++ /dev/null
@@ -1,374 +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.kylin.jdbc.stub;
-
-import java.io.IOException;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.List;
-
-import net.hydromatic.avatica.AvaticaStatement;
-import net.hydromatic.avatica.ColumnMetaData;
-import net.hydromatic.avatica.ColumnMetaData.Rep;
-import net.hydromatic.avatica.ColumnMetaData.ScalarType;
-
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpException;
-import org.apache.commons.httpclient.HttpMethodBase;
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.StringRequestEntity;
-import org.apache.commons.httpclient.protocol.Protocol;
-import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
-import org.apache.kylin.jdbc.KylinConnectionImpl;
-import org.apache.kylin.jdbc.KylinEnumerator;
-import org.apache.kylin.jdbc.KylinMetaImpl;
-import org.apache.kylin.jdbc.util.DefaultSslProtocolSocketFactory;
-import org.apache.kylin.jdbc.util.SQLTypeMap;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.collect.HashMultimap;
-import org.apache.kylin.jdbc.KylinJdbc41Factory.KylinJdbc41PreparedStatement;
-import org.apache.kylin.jdbc.stub.TableMetaStub.ColumnMetaStub;
-
-/**
- * @author xduo
- * 
- */
-public class KylinClient implements RemoteClient {
-    private static final Logger logger = LoggerFactory.getLogger(KylinClient.class);
-
-    private final KylinConnectionImpl conn;
-
-    public KylinClient(KylinConnectionImpl conn) {
-        this.conn = conn;
-    }
-
-    @Override
-    public void connect() throws ConnectionException {
-        PostMethod post = new PostMethod(conn.getConnectUrl());
-        HttpClient httpClient = new HttpClient();
-
-        if (conn.getConnectUrl().toLowerCase().startsWith("https://")) {
-            registerSsl();
-        }
-        addPostHeaders(post);
-
-        try {
-            StringRequestEntity requestEntity = new StringRequestEntity("{}", "application/json", "UTF-8");
-            post.setRequestEntity(requestEntity);
-            httpClient.executeMethod(post);
-
-            if (post.getStatusCode() != 200 && post.getStatusCode() != 201) {
-                logger.error("Connect failed with error code " + post.getStatusCode() + " and message:\n" + post.getResponseBodyAsString());
-
-                throw new ConnectionException("Connect failed, error code " + post.getStatusCode() + " and message: " + post.getResponseBodyAsString());
-            }
-        } catch (HttpException e) {
-            logger.error(e.getLocalizedMessage(), e);
-            throw new ConnectionException(e.getLocalizedMessage());
-        } catch (IOException e) {
-            logger.error(e.getLocalizedMessage(), e);
-            throw new ConnectionException(e.getLocalizedMessage());
-        }
-    }
-
-    @Override
-    public KylinMetaImpl.MetaProject getMetadata(String project) throws ConnectionException {
-        GetMethod get = new GetMethod(conn.getMetaProjectUrl(project));
-        HttpClient httpClient = new HttpClient();
-
-        if (conn.getConnectUrl().toLowerCase().startsWith("https://")) {
-            registerSsl();
-        }
-        addPostHeaders(get);
-
-        List<TableMetaStub> tableMetaStubs = null;
-        try {
-            httpClient.executeMethod(get);
-
-            if (get.getStatusCode() != 200 && get.getStatusCode() != 201) {
-                logger.error("Connect failed with error code " + get.getStatusCode() + " and message:\n" + get.getResponseBodyAsString());
-
-                throw new ConnectionException("Connect failed, error code " + get.getStatusCode() + " and message: " + get.getResponseBodyAsString());
-            }
-
-            tableMetaStubs = new ObjectMapper().readValue(get.getResponseBodyAsString(), new TypeReference<List<TableMetaStub>>() {
-            });
-
-            List<KylinMetaImpl.MetaTable> tables = new ArrayList<KylinMetaImpl.MetaTable>();
-            HashMultimap<String, KylinMetaImpl.MetaTable> schemasMap = HashMultimap.create();
-
-            for (TableMetaStub tableMetaStub : tableMetaStubs) {
-                List<KylinMetaImpl.MetaColumn> columns = new ArrayList<KylinMetaImpl.MetaColumn>();
-
-                for (ColumnMetaStub columnMetaStub : tableMetaStub.getColumns()) {
-                    KylinMetaImpl.MetaColumn column = createNewColumn(columnMetaStub);
-                    columns.add(column);
-                }
-
-                KylinMetaImpl.MetaTable table = createNewTable(tableMetaStub, columns);
-                tables.add(table);
-                schemasMap.put(tableMetaStub.getTABLE_CAT() + "#" + tableMetaStub.getTABLE_SCHEM(), table);
-            }
-
-            HashMultimap<String, KylinMetaImpl.MetaSchema> catalogMap = HashMultimap.create();
-            List<KylinMetaImpl.MetaSchema> schemas = new ArrayList<KylinMetaImpl.MetaSchema>();
-            for (String key : schemasMap.keySet()) {
-                String cat = key.split("#")[0];
-                String schema = key.split("#")[1];
-                KylinMetaImpl.MetaSchema metaSchema = new KylinMetaImpl.MetaSchema(cat, schema, new ArrayList<KylinMetaImpl.MetaTable>(schemasMap.get(key)));
-                schemas.add(metaSchema);
-                catalogMap.put(cat, metaSchema);
-            }
-
-            List<KylinMetaImpl.MetaCatalog> catalogs = new ArrayList<KylinMetaImpl.MetaCatalog>();
-            for (String key : catalogMap.keySet()) {
-                KylinMetaImpl.MetaCatalog metaCatalog = new KylinMetaImpl.MetaCatalog(key, new ArrayList<KylinMetaImpl.MetaSchema>(catalogMap.get(key)));
-                catalogs.add(metaCatalog);
-            }
-
-            return new KylinMetaImpl.MetaProject(project, catalogs);
-        } catch (HttpException e) {
-            logger.error(e.getLocalizedMessage(), e);
-            throw new ConnectionException(e.getLocalizedMessage());
-        } catch (IOException e) {
-            logger.error(e.getLocalizedMessage(), e);
-            throw new ConnectionException(e.getLocalizedMessage());
-        }
-    }
-
-    private KylinMetaImpl.MetaTable createNewTable(TableMetaStub tableMetaStub, List<KylinMetaImpl.MetaColumn> columns) {
-        KylinMetaImpl.MetaTable table = new KylinMetaImpl.MetaTable(tableMetaStub.getTABLE_CAT(), tableMetaStub.getTABLE_SCHEM(), tableMetaStub.getTABLE_NAME(), tableMetaStub.getTABLE_TYPE(), tableMetaStub.getREMARKS(), tableMetaStub.getTYPE_CAT(), tableMetaStub.getTYPE_SCHEM(), tableMetaStub.getTYPE_NAME(), tableMetaStub.getSELF_REFERENCING_COL_NAME(), tableMetaStub.getREF_GENERATION(), columns);
-        return table;
-    }
-
-    private KylinMetaImpl.MetaColumn createNewColumn(ColumnMetaStub columnMetaStub) {
-        KylinMetaImpl.MetaColumn column = new KylinMetaImpl.MetaColumn(columnMetaStub.getTABLE_CAT(), columnMetaStub.getTABLE_SCHEM(), columnMetaStub.getTABLE_NAME(), columnMetaStub.getCOLUMN_NAME(), columnMetaStub.getDATA_TYPE(), columnMetaStub.getTYPE_NAME(), columnMetaStub.getCOLUMN_SIZE(), columnMetaStub.getBUFFER_LENGTH(), columnMetaStub.getDECIMAL_DIGITS(), columnMetaStub.getNUM_PREC_RADIX(), columnMetaStub.getNULLABLE(), columnMetaStub.getREMARKS(), columnMetaStub.getCOLUMN_DEF(), columnMetaStub.getSQL_DATA_TYPE(), columnMetaStub.getSQL_DATETIME_SUB(), columnMetaStub.getCHAR_OCTET_LENGTH(), columnMetaStub.getORDINAL_POSITION(), columnMetaStub.getIS_NULLABLE(), columnMetaStub.getSCOPE_CATLOG(), columnMetaStub.getSCOPE_TABLE(), columnMetaStub.getSOURCE_DATA_TYPE(), columnMetaStub.getIS_AUTOINCREMENT(), columnMetaStub.getSCOPE_SCHEMA());
-        return column;
-    }
-
-    @Override
-    public DataSet<Object[]> query(AvaticaStatement statement, String sql) throws SQLException {
-        SQLResponseStub queryRes = null;
-
-        List<StateParam> params = null;
-        if (statement instanceof KylinJdbc41PreparedStatement) {
-            params = genPrestateStates(statement);
-        }
-
-        queryRes = runKylinQuery(sql, params);
-
-        List<ColumnMetaData> metas = genColumnMeta(queryRes);
-        List<Object[]> data = genResultData(queryRes, metas);
-
-        return new DataSet<Object[]>(metas, new KylinEnumerator<Object[]>(data));
-    }
-
-    /**
-     * @param queryRes
-     * @param metas
-     * @return
-     */
-    private List<Object[]> genResultData(SQLResponseStub queryRes, List<ColumnMetaData> metas) {
-        List<Object[]> data = new ArrayList<Object[]>();
-        for (String[] result : queryRes.getResults()) {
-            Object[] row = new Object[result.length];
-
-            for (int i = 0; i < result.length; i++) {
-                ColumnMetaData meta = metas.get(i);
-                row[i] = SQLTypeMap.wrapObject(result[i], meta.type.type);
-            }
-
-            data.add(row);
-        }
-        return data;
-    }
-
-    /**
-     * @param statement
-     * @param params
-     */
-    private List<StateParam> genPrestateStates(AvaticaStatement statement) {
-        List<StateParam> params = new ArrayList<StateParam>();
-        List<Object> values = ((KylinJdbc41PreparedStatement) statement).getParameterValues();
-
-        for (int i = 0; i < values.size(); i++) {
-            Object value = values.get(i);
-            params.add(new StateParam(value.getClass().getCanonicalName(), String.valueOf(value)));
-        }
-
-        return params;
-    }
-
-    /**
-     * @param queryRes
-     * @return
-     */
-    private List<ColumnMetaData> genColumnMeta(SQLResponseStub queryRes) {
-        List<ColumnMetaData> metas = new ArrayList<ColumnMetaData>();
-        for (int i = 0; i < queryRes.getColumnMetas().size(); i++) {
-            SQLResponseStub.ColumnMetaStub scm = queryRes.getColumnMetas().get(i);
-            ScalarType type = ColumnMetaData.scalar(scm.getColumnType(), scm.getColumnTypeName(), Rep.of(SQLTypeMap.convert(scm.getColumnType())));
-
-            ColumnMetaData meta = new ColumnMetaData(i, scm.isAutoIncrement(), scm.isCaseSensitive(), scm.isSearchable(), scm.isCurrency(), scm.getIsNullable(), scm.isSigned(), scm.getDisplaySize(), scm.getLabel(), scm.getName(), scm.getSchemaName(), scm.getPrecision(), scm.getScale(), scm.getTableName(), scm.getSchemaName(), type, scm.isReadOnly(), scm.isWritable(), scm.isWritable(), null);
-
-            metas.add(meta);
-        }
-
-        return metas;
-    }
-
-    /**
-     * @param sql
-     * @return
-     * @throws IOException
-     */
-    private SQLResponseStub runKylinQuery(String sql, List<StateParam> params) throws SQLException {
-        String url = conn.getQueryUrl();
-        String project = conn.getProject();
-        QueryRequest request = null;
-
-        if (null != params) {
-            request = new PreQueryRequest();
-            ((PreQueryRequest) request).setParams(params);
-            url += "/prestate";
-        } else {
-            request = new QueryRequest();
-        }
-        request.setSql(sql);
-        request.setProject(project);
-
-        PostMethod post = new PostMethod(url);
-        addPostHeaders(post);
-        HttpClient httpClient = new HttpClient();
-        if (conn.getQueryUrl().toLowerCase().startsWith("https://")) {
-            registerSsl();
-        }
-
-        String postBody = null;
-        ObjectMapper mapper = new ObjectMapper();
-        try {
-            postBody = mapper.writeValueAsString(request);
-            logger.debug("Post body:\n " + postBody);
-        } catch (JsonProcessingException e) {
-            logger.error(e.getLocalizedMessage(), e);
-        }
-        String response = null;
-        SQLResponseStub queryRes = null;
-
-        try {
-            StringRequestEntity requestEntity = new StringRequestEntity(postBody, "application/json", "UTF-8");
-            post.setRequestEntity(requestEntity);
-
-            httpClient.executeMethod(post);
-            response = post.getResponseBodyAsString();
-
-            if (post.getStatusCode() != 200 && post.getStatusCode() != 201) {
-                logger.error("Failed to query", response);
-                throw new SQLException(response);
-            }
-
-            queryRes = new ObjectMapper().readValue(response, SQLResponseStub.class);
-
-        } catch (HttpException e) {
-            logger.error(e.getLocalizedMessage(), e);
-            throw new SQLException(e.getLocalizedMessage());
-        } catch (IOException e) {
-            logger.error(e.getLocalizedMessage(), e);
-            throw new SQLException(e.getLocalizedMessage());
-        }
-
-        return queryRes;
-    }
-
-    private void addPostHeaders(HttpMethodBase method) {
-        method.addRequestHeader("Accept", "application/json, text/plain, */*");
-        method.addRequestHeader("Content-Type", "application/json");
-        method.addRequestHeader("Authorization", "Basic " + conn.getBasicAuthHeader());
-    }
-
-    private void registerSsl() {
-        Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) new DefaultSslProtocolSocketFactory(), 443));
-    }
-
-    public class QueryRequest {
-        private String sql;
-        private String project;
-
-        public String getSql() {
-            return sql;
-        }
-
-        public void setSql(String sql) {
-            this.sql = sql;
-        }
-
-        public String getProject() {
-            return project;
-        }
-
-        public void setProject(String project) {
-            this.project = project;
-        }
-    }
-
-    public class PreQueryRequest extends QueryRequest {
-        private List<StateParam> params;
-
-        public List<StateParam> getParams() {
-            return params;
-        }
-
-        public void setParams(List<StateParam> params) {
-            this.params = params;
-        }
-    }
-
-    public class StateParam {
-        private String className;
-        private String value;
-
-        public StateParam(String className, String value) {
-            super();
-            this.className = className;
-            this.value = value;
-        }
-
-        public String getClassName() {
-            return className;
-        }
-
-        public void setClazz(String className) {
-            this.className = className;
-        }
-
-        public String getValue() {
-            return value;
-        }
-
-        public void setValue(String value) {
-            this.value = value;
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/stub/KylinColumnMetaData.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/KylinColumnMetaData.java b/jdbc/src/main/java/org/apache/kylin/jdbc/stub/KylinColumnMetaData.java
deleted file mode 100644
index e0e14e7..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/KylinColumnMetaData.java
+++ /dev/null
@@ -1,38 +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.kylin.jdbc.stub;
-
-import java.sql.DatabaseMetaData;
-
-import net.hydromatic.avatica.ColumnMetaData;
-
-/**
- * @author xduo
- * 
- */
-public class KylinColumnMetaData extends ColumnMetaData {
-
-    public KylinColumnMetaData(int ordinal, boolean autoIncrement, boolean caseSensitive, boolean searchable, boolean currency, int nullable, boolean signed, int displaySize, String label, String columnName, String schemaName, int precision, int scale, String tableName, String catalogName, AvaticaType type, boolean readOnly, boolean writable, boolean definitelyWritable, String columnClassName) {
-        super(ordinal, autoIncrement, caseSensitive, searchable, currency, nullable, signed, displaySize, label, columnName, schemaName, precision, scale, tableName, catalogName, type, readOnly, writable, definitelyWritable, columnClassName);
-    }
-
-    public static ColumnMetaData dummy(int ordinal, String label, String columnName, AvaticaType type, boolean nullable) {
-        return new ColumnMetaData(ordinal, false, true, false, false, nullable ? DatabaseMetaData.columnNullable : DatabaseMetaData.columnNoNulls, true, -1, label, columnName, null, -1, -1, null, null, type, true, false, false, null);
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/stub/RemoteClient.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/RemoteClient.java b/jdbc/src/main/java/org/apache/kylin/jdbc/stub/RemoteClient.java
deleted file mode 100644
index dae4126..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/RemoteClient.java
+++ /dev/null
@@ -1,59 +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.kylin.jdbc.stub;
-
-import java.sql.SQLException;
-
-import net.hydromatic.avatica.AvaticaStatement;
-
-import org.apache.kylin.jdbc.KylinMetaImpl.MetaProject;
-
-/**
- * Remote query stub of kylin restful service
- * 
- * @author xduo
- * 
- */
-public interface RemoteClient {
-
-    /**
-     * Connect to kylin restful service. ConnectionException will be thrown if
-     * authentication failed.
-     * 
-     * @throws ConnectionException
-     */
-    public void connect() throws ConnectionException;
-
-    /**
-     * @param project
-     * @return
-     */
-    public MetaProject getMetadata(String project) throws ConnectionException;
-
-    /**
-     * Run query
-     * 
-     * @param statement
-     * @param sql
-     * @return
-     * @throws SQLException
-     */
-    public DataSet<Object[]> query(AvaticaStatement statement, String sql) throws SQLException;
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/stub/SQLResponseStub.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/SQLResponseStub.java b/jdbc/src/main/java/org/apache/kylin/jdbc/stub/SQLResponseStub.java
deleted file mode 100644
index a9c6c02..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/SQLResponseStub.java
+++ /dev/null
@@ -1,323 +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.kylin.jdbc.stub;
-
-import java.io.Serializable;
-import java.util.List;
-
-/**
- * @author xduo
- * 
- */
-public class SQLResponseStub implements Serializable {
-    private static final long serialVersionUID = 1L;
-
-    // private static final Logger logger =
-    // LoggerFactory.getLogger(SQLResponse.class);
-
-    // the data type for each column
-    private List<ColumnMetaStub> columnMetas;
-
-    // the results rows, each row contains several columns
-    private List<String[]> results;
-
-    private String cube;
-
-    // if not select query, only return affected row count
-    private int affectedRowCount;
-
-    // if isException, the detailed exception message
-    private String exceptionMessage;
-
-    private boolean isException;
-
-    private long duration;
-
-    private boolean isPartial = false;
-
-    private long totalScanCount;
-
-    private boolean hitCache = false;
-
-    public SQLResponseStub() {
-    }
-
-    public List<ColumnMetaStub> getColumnMetas() {
-        return columnMetas;
-    }
-
-    public void setColumnMetas(List<ColumnMetaStub> columnMetas) {
-        this.columnMetas = columnMetas;
-    }
-
-    public List<String[]> getResults() {
-        return results;
-    }
-
-    public void setResults(List<String[]> results) {
-        this.results = results;
-    }
-
-    public String getCube() {
-        return cube;
-    }
-
-    public void setCube(String cube) {
-        this.cube = cube;
-    }
-
-    public int getAffectedRowCount() {
-        return affectedRowCount;
-    }
-
-    public void setAffectedRowCount(int affectedRowCount) {
-        this.affectedRowCount = affectedRowCount;
-    }
-
-    public boolean getIsException() {
-        return isException;
-    }
-
-    public void setIsException(boolean isException) {
-        this.isException = isException;
-    }
-
-    public String getExceptionMessage() {
-        return exceptionMessage;
-    }
-
-    public void setExceptionMessage(String exceptionMessage) {
-        this.exceptionMessage = exceptionMessage;
-    }
-
-    public long getDuration() {
-        return duration;
-    }
-
-    public void setDuration(long duration) {
-        this.duration = duration;
-    }
-
-    public boolean isPartial() {
-        return isPartial;
-    }
-
-    public void setPartial(boolean isPartial) {
-        this.isPartial = isPartial;
-    }
-
-    public long getTotalScanCount() {
-        return totalScanCount;
-    }
-
-    public void setTotalScanCount(long totalScanCount) {
-        this.totalScanCount = totalScanCount;
-    }
-
-    public boolean isHitCache() {
-        return hitCache;
-    }
-
-    public void setHitCache(boolean hitCache) {
-        this.hitCache = hitCache;
-    }
-
-    public static long getSerialversionuid() {
-        return serialVersionUID;
-    }
-
-    public static class ColumnMetaStub {
-
-        private boolean isAutoIncrement;
-        private boolean isCaseSensitive;
-        private boolean isSearchable;
-        private boolean isCurrency;
-        private int isNullable;// 0:nonull, 1:nullable, 2: nullableunknown
-        private boolean isSigned;
-        private int displaySize;
-        private String label;// AS keyword
-        private String name;
-        private String schemaName;
-        private String catelogName;
-        private String tableName;
-        private int precision;
-        private int scale;
-        private int columnType;// as defined in java.sql.Types
-        private String columnTypeName;
-        private boolean isReadOnly;
-        private boolean isWritable;
-        private boolean isDefinitelyWritable;
-
-        public ColumnMetaStub() {
-        }
-
-        public boolean isAutoIncrement() {
-            return isAutoIncrement;
-        }
-
-        public void setAutoIncrement(boolean isAutoIncrement) {
-            this.isAutoIncrement = isAutoIncrement;
-        }
-
-        public boolean isCaseSensitive() {
-            return isCaseSensitive;
-        }
-
-        public void setCaseSensitive(boolean isCaseSensitive) {
-            this.isCaseSensitive = isCaseSensitive;
-        }
-
-        public boolean isSearchable() {
-            return isSearchable;
-        }
-
-        public void setSearchable(boolean isSearchable) {
-            this.isSearchable = isSearchable;
-        }
-
-        public boolean isCurrency() {
-            return isCurrency;
-        }
-
-        public void setCurrency(boolean isCurrency) {
-            this.isCurrency = isCurrency;
-        }
-
-        public int getIsNullable() {
-            return isNullable;
-        }
-
-        public void setIsNullable(int isNullable) {
-            this.isNullable = isNullable;
-        }
-
-        public boolean isSigned() {
-            return isSigned;
-        }
-
-        public void setSigned(boolean isSigned) {
-            this.isSigned = isSigned;
-        }
-
-        public int getDisplaySize() {
-            return displaySize;
-        }
-
-        public void setDisplaySize(int displaySize) {
-            this.displaySize = displaySize;
-        }
-
-        public String getLabel() {
-            return label;
-        }
-
-        public void setLabel(String label) {
-            this.label = label;
-        }
-
-        public String getName() {
-            return name;
-        }
-
-        public void setName(String name) {
-            this.name = name;
-        }
-
-        public String getSchemaName() {
-            return schemaName;
-        }
-
-        public void setSchemaName(String schemaName) {
-            this.schemaName = schemaName;
-        }
-
-        public String getCatelogName() {
-            return catelogName;
-        }
-
-        public void setCatelogName(String catelogName) {
-            this.catelogName = catelogName;
-        }
-
-        public String getTableName() {
-            return tableName;
-        }
-
-        public void setTableName(String tableName) {
-            this.tableName = tableName;
-        }
-
-        public int getPrecision() {
-            return precision;
-        }
-
-        public void setPrecision(int precision) {
-            this.precision = precision;
-        }
-
-        public int getScale() {
-            return scale;
-        }
-
-        public void setScale(int scale) {
-            this.scale = scale;
-        }
-
-        public int getColumnType() {
-            return columnType;
-        }
-
-        public void setColumnType(int columnType) {
-            this.columnType = columnType;
-        }
-
-        public String getColumnTypeName() {
-            return columnTypeName;
-        }
-
-        public void setColumnTypeName(String columnTypeName) {
-            this.columnTypeName = columnTypeName;
-        }
-
-        public boolean isReadOnly() {
-            return isReadOnly;
-        }
-
-        public void setReadOnly(boolean isReadOnly) {
-            this.isReadOnly = isReadOnly;
-        }
-
-        public boolean isWritable() {
-            return isWritable;
-        }
-
-        public void setWritable(boolean isWritable) {
-            this.isWritable = isWritable;
-        }
-
-        public boolean isDefinitelyWritable() {
-            return isDefinitelyWritable;
-        }
-
-        public void setDefinitelyWritable(boolean isDefinitelyWritable) {
-            this.isDefinitelyWritable = isDefinitelyWritable;
-        }
-
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/stub/TableMetaStub.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/TableMetaStub.java b/jdbc/src/main/java/org/apache/kylin/jdbc/stub/TableMetaStub.java
deleted file mode 100644
index 524fa81..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/TableMetaStub.java
+++ /dev/null
@@ -1,347 +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.kylin.jdbc.stub;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import com.fasterxml.jackson.annotation.JsonProperty;
-
-/**
- * @author xduo
- * 
- */
-public class TableMetaStub {
-
-    private static final long serialVersionUID = 1L;
-    private String TABLE_CAT;
-    private String TABLE_SCHEM;
-    private String TABLE_NAME;
-    private String TABLE_TYPE;
-    private String REMARKS;
-    private String TYPE_CAT;
-    private String TYPE_SCHEM;
-    private String TYPE_NAME;
-    private String SELF_REFERENCING_COL_NAME;
-    private String REF_GENERATION;
-    @JsonProperty("columns")
-    private List<ColumnMetaStub> columns = new ArrayList<ColumnMetaStub>();
-
-    public String getTABLE_CAT() {
-        return TABLE_CAT;
-    }
-
-    public void setTABLE_CAT(String tABLE_CAT) {
-        TABLE_CAT = tABLE_CAT;
-    }
-
-    public String getTABLE_SCHEM() {
-        return TABLE_SCHEM;
-    }
-
-    public void setTABLE_SCHEM(String tABLE_SCHEM) {
-        TABLE_SCHEM = tABLE_SCHEM;
-    }
-
-    public String getTABLE_NAME() {
-        return TABLE_NAME;
-    }
-
-    public void setTABLE_NAME(String tABLE_NAME) {
-        TABLE_NAME = tABLE_NAME;
-    }
-
-    public String getTABLE_TYPE() {
-        return TABLE_TYPE;
-    }
-
-    public void setTABLE_TYPE(String tABLE_TYPE) {
-        TABLE_TYPE = tABLE_TYPE;
-    }
-
-    public String getREMARKS() {
-        return REMARKS;
-    }
-
-    public void setREMARKS(String rEMARKS) {
-        REMARKS = rEMARKS;
-    }
-
-    public String getTYPE_CAT() {
-        return TYPE_CAT;
-    }
-
-    public void setTYPE_CAT(String tYPE_CAT) {
-        TYPE_CAT = tYPE_CAT;
-    }
-
-    public String getTYPE_SCHEM() {
-        return TYPE_SCHEM;
-    }
-
-    public void setTYPE_SCHEM(String tYPE_SCHEM) {
-        TYPE_SCHEM = tYPE_SCHEM;
-    }
-
-    public String getTYPE_NAME() {
-        return TYPE_NAME;
-    }
-
-    public void setTYPE_NAME(String tYPE_NAME) {
-        TYPE_NAME = tYPE_NAME;
-    }
-
-    public String getSELF_REFERENCING_COL_NAME() {
-        return SELF_REFERENCING_COL_NAME;
-    }
-
-    public void setSELF_REFERENCING_COL_NAME(String sELF_REFERENCING_COL_NAME) {
-        SELF_REFERENCING_COL_NAME = sELF_REFERENCING_COL_NAME;
-    }
-
-    public String getREF_GENERATION() {
-        return REF_GENERATION;
-    }
-
-    public void setREF_GENERATION(String rEF_GENERATION) {
-        REF_GENERATION = rEF_GENERATION;
-    }
-
-    public List<ColumnMetaStub> getColumns() {
-        return columns;
-    }
-
-    public void setColumns(List<ColumnMetaStub> columns) {
-        this.columns = columns;
-    }
-
-    public static long getSerialversionuid() {
-        return serialVersionUID;
-    }
-
-    public static class ColumnMetaStub {
-        private String TABLE_CAT;
-        private String TABLE_SCHEM;
-        private String TABLE_NAME;
-        private String COLUMN_NAME;
-        private int DATA_TYPE;
-        private String TYPE_NAME;
-        private int COLUMN_SIZE;
-        private int BUFFER_LENGTH;
-        private int DECIMAL_DIGITS;
-        private int NUM_PREC_RADIX;
-        private int NULLABLE;
-        private String REMARKS;
-        private String COLUMN_DEF;
-        private int SQL_DATA_TYPE;
-        private int SQL_DATETIME_SUB;
-        private int CHAR_OCTET_LENGTH;
-        private int ORDINAL_POSITION;
-        private String IS_NULLABLE;
-        private String SCOPE_CATLOG;
-        private String SCOPE_SCHEMA;
-        private String SCOPE_TABLE;
-        private short SOURCE_DATA_TYPE;
-        private String IS_AUTOINCREMENT;
-
-        public String getTABLE_CAT() {
-            return TABLE_CAT;
-        }
-
-        public void setTABLE_CAT(String tABLE_CAT) {
-            TABLE_CAT = tABLE_CAT;
-        }
-
-        public String getTABLE_SCHEM() {
-            return TABLE_SCHEM;
-        }
-
-        public void setTABLE_SCHEM(String tABLE_SCHEM) {
-            TABLE_SCHEM = tABLE_SCHEM;
-        }
-
-        public String getTABLE_NAME() {
-            return TABLE_NAME;
-        }
-
-        public void setTABLE_NAME(String tABLE_NAME) {
-            TABLE_NAME = tABLE_NAME;
-        }
-
-        public String getCOLUMN_NAME() {
-            return COLUMN_NAME;
-        }
-
-        public void setCOLUMN_NAME(String cOLUMN_NAME) {
-            COLUMN_NAME = cOLUMN_NAME;
-        }
-
-        public int getDATA_TYPE() {
-            return DATA_TYPE;
-        }
-
-        public void setDATA_TYPE(int dATA_TYPE) {
-            DATA_TYPE = dATA_TYPE;
-        }
-
-        public String getTYPE_NAME() {
-            return TYPE_NAME;
-        }
-
-        public void setTYPE_NAME(String tYPE_NAME) {
-            TYPE_NAME = tYPE_NAME;
-        }
-
-        public int getCOLUMN_SIZE() {
-            return COLUMN_SIZE;
-        }
-
-        public void setCOLUMN_SIZE(int cOLUMN_SIZE) {
-            COLUMN_SIZE = cOLUMN_SIZE;
-        }
-
-        public int getBUFFER_LENGTH() {
-            return BUFFER_LENGTH;
-        }
-
-        public void setBUFFER_LENGTH(int bUFFER_LENGTH) {
-            BUFFER_LENGTH = bUFFER_LENGTH;
-        }
-
-        public int getDECIMAL_DIGITS() {
-            return DECIMAL_DIGITS;
-        }
-
-        public void setDECIMAL_DIGITS(int dECIMAL_DIGITS) {
-            DECIMAL_DIGITS = dECIMAL_DIGITS;
-        }
-
-        public int getNUM_PREC_RADIX() {
-            return NUM_PREC_RADIX;
-        }
-
-        public void setNUM_PREC_RADIX(int nUM_PREC_RADIX) {
-            NUM_PREC_RADIX = nUM_PREC_RADIX;
-        }
-
-        public int getNULLABLE() {
-            return NULLABLE;
-        }
-
-        public void setNULLABLE(int nULLABLE) {
-            NULLABLE = nULLABLE;
-        }
-
-        public String getREMARKS() {
-            return REMARKS;
-        }
-
-        public void setREMARKS(String rEMARKS) {
-            REMARKS = rEMARKS;
-        }
-
-        public String getCOLUMN_DEF() {
-            return COLUMN_DEF;
-        }
-
-        public void setCOLUMN_DEF(String cOLUMN_DEF) {
-            COLUMN_DEF = cOLUMN_DEF;
-        }
-
-        public int getSQL_DATA_TYPE() {
-            return SQL_DATA_TYPE;
-        }
-
-        public void setSQL_DATA_TYPE(int sQL_DATA_TYPE) {
-            SQL_DATA_TYPE = sQL_DATA_TYPE;
-        }
-
-        public int getSQL_DATETIME_SUB() {
-            return SQL_DATETIME_SUB;
-        }
-
-        public void setSQL_DATETIME_SUB(int sQL_DATETIME_SUB) {
-            SQL_DATETIME_SUB = sQL_DATETIME_SUB;
-        }
-
-        public int getCHAR_OCTET_LENGTH() {
-            return CHAR_OCTET_LENGTH;
-        }
-
-        public void setCHAR_OCTET_LENGTH(int cHAR_OCTET_LENGTH) {
-            CHAR_OCTET_LENGTH = cHAR_OCTET_LENGTH;
-        }
-
-        public int getORDINAL_POSITION() {
-            return ORDINAL_POSITION;
-        }
-
-        public void setORDINAL_POSITION(int oRDINAL_POSITION) {
-            ORDINAL_POSITION = oRDINAL_POSITION;
-        }
-
-        public String getIS_NULLABLE() {
-            return IS_NULLABLE;
-        }
-
-        public void setIS_NULLABLE(String iS_NULLABLE) {
-            IS_NULLABLE = iS_NULLABLE;
-        }
-
-        public String getSCOPE_CATLOG() {
-            return SCOPE_CATLOG;
-        }
-
-        public void setSCOPE_CATLOG(String sCOPE_CATLOG) {
-            SCOPE_CATLOG = sCOPE_CATLOG;
-        }
-
-        public String getSCOPE_SCHEMA() {
-            return SCOPE_SCHEMA;
-        }
-
-        public void setSCOPE_SCHEMA(String sCOPE_SCHEMA) {
-            SCOPE_SCHEMA = sCOPE_SCHEMA;
-        }
-
-        public String getSCOPE_TABLE() {
-            return SCOPE_TABLE;
-        }
-
-        public void setSCOPE_TABLE(String sCOPE_TABLE) {
-            SCOPE_TABLE = sCOPE_TABLE;
-        }
-
-        public short getSOURCE_DATA_TYPE() {
-            return SOURCE_DATA_TYPE;
-        }
-
-        public void setSOURCE_DATA_TYPE(short sOURCE_DATA_TYPE) {
-            SOURCE_DATA_TYPE = sOURCE_DATA_TYPE;
-        }
-
-        public String getIS_AUTOINCREMENT() {
-            return IS_AUTOINCREMENT;
-        }
-
-        public void setIS_AUTOINCREMENT(String iS_AUTOINCREMENT) {
-            this.IS_AUTOINCREMENT = iS_AUTOINCREMENT;
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/util/DefaultSslProtocolSocketFactory.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/util/DefaultSslProtocolSocketFactory.java b/jdbc/src/main/java/org/apache/kylin/jdbc/util/DefaultSslProtocolSocketFactory.java
index 9ed24cc..3f40d42 100644
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/util/DefaultSslProtocolSocketFactory.java
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/util/DefaultSslProtocolSocketFactory.java
@@ -1,150 +1,148 @@
-/*
- * 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.kylin.jdbc.util;
-
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.Socket;
-import java.net.UnknownHostException;
-
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.TrustManager;
-
-import org.apache.commons.httpclient.ConnectTimeoutException;
-import org.apache.commons.httpclient.HttpClientError;
-import org.apache.commons.httpclient.params.HttpConnectionParams;
-import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
-import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * @author xduo
- * 
- */
-public class DefaultSslProtocolSocketFactory implements SecureProtocolSocketFactory {
-    /** Log object for this class. */
-    private static Logger LOG = LoggerFactory.getLogger(DefaultSslProtocolSocketFactory.class);
-    private SSLContext sslcontext = null;
-
-    /**
-     * Constructor for DefaultSslProtocolSocketFactory.
-     */
-    public DefaultSslProtocolSocketFactory() {
-        super();
-    }
-
-    /**
-     * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
-     */
-    public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException {
-        return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
-    }
-
-    /**
-     * Attempts to get a new socket connection to the given host within the
-     * given time limit.
-     * 
-     * <p>
-     * To circumvent the limitations of older JREs that do not support connect
-     * timeout a controller thread is executed. The controller thread attempts
-     * to create a new socket within the given limit of time. If socket
-     * constructor does not return until the timeout expires, the controller
-     * terminates and throws an {@link ConnectTimeoutException}
-     * </p>
-     * 
-     * @param host
-     *            the host name/IP
-     * @param port
-     *            the port on the host
-     * @param localAddress
-     *            the local host name/IP to bind the socket to
-     * @param localPort
-     *            the port on the local machine
-     * @param params
-     *            {@link HttpConnectionParams Http connection parameters}
-     * 
-     * @return Socket a new socket
-     * 
-     * @throws IOException
-     *             if an I/O error occurs while creating the socket
-     * @throws UnknownHostException
-     *             if the IP address of the host cannot be determined
-     * @throws ConnectTimeoutException
-     *             DOCUMENT ME!
-     * @throws IllegalArgumentException
-     *             DOCUMENT ME!
-     */
-    public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
-        if (params == null) {
-            throw new IllegalArgumentException("Parameters may not be null");
-        }
-
-        int timeout = params.getConnectionTimeout();
-
-        if (timeout == 0) {
-            return createSocket(host, port, localAddress, localPort);
-        } else {
-            // To be eventually deprecated when migrated to Java 1.4 or above
-            return ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout);
-        }
-    }
-
-    /**
-     * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
-     */
-    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
-        return getSSLContext().getSocketFactory().createSocket(host, port);
-    }
-
-    /**
-     * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
-     */
-    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
-        return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
-    }
-
-    public boolean equals(Object obj) {
-        return ((obj != null) && obj.getClass().equals(DefaultX509TrustManager.class));
-    }
-
-    public int hashCode() {
-        return DefaultX509TrustManager.class.hashCode();
-    }
-
-    private static SSLContext createEasySSLContext() {
-        try {
-            SSLContext context = SSLContext.getInstance("TLS");
-            context.init(null, new TrustManager[] { new DefaultX509TrustManager(null) }, null);
-
-            return context;
-        } catch (Exception e) {
-            LOG.error(e.getMessage(), e);
-            throw new HttpClientError(e.toString());
-        }
-    }
-
-    private SSLContext getSSLContext() {
-        if (this.sslcontext == null) {
-            this.sslcontext = createEasySSLContext();
-        }
-
-        return this.sslcontext;
-    }
-}
+/*
+ * 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.kylin.jdbc.util;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+
+import org.apache.commons.httpclient.ConnectTimeoutException;
+import org.apache.commons.httpclient.HttpClientError;
+import org.apache.commons.httpclient.params.HttpConnectionParams;
+import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
+import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ */
+public class DefaultSslProtocolSocketFactory implements SecureProtocolSocketFactory {
+    /** Log object for this class. */
+    private static Logger LOG = LoggerFactory.getLogger(DefaultSslProtocolSocketFactory.class);
+    private SSLContext sslcontext = null;
+
+    /**
+     * Constructor for DefaultSslProtocolSocketFactory.
+     */
+    public DefaultSslProtocolSocketFactory() {
+        super();
+    }
+
+    /**
+     * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
+     */
+    public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException {
+        return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
+    }
+
+    /**
+     * Attempts to get a new socket connection to the given host within the
+     * given time limit.
+     * 
+     * <p>
+     * To circumvent the limitations of older JREs that do not support connect
+     * timeout a controller thread is executed. The controller thread attempts
+     * to create a new socket within the given limit of time. If socket
+     * constructor does not return until the timeout expires, the controller
+     * terminates and throws an {@link ConnectTimeoutException}
+     * </p>
+     * 
+     * @param host
+     *            the host name/IP
+     * @param port
+     *            the port on the host
+     * @param localAddress
+     *            the local host name/IP to bind the socket to
+     * @param localPort
+     *            the port on the local machine
+     * @param params
+     *            {@link HttpConnectionParams Http connection parameters}
+     * 
+     * @return Socket a new socket
+     * 
+     * @throws IOException
+     *             if an I/O error occurs while creating the socket
+     * @throws UnknownHostException
+     *             if the IP address of the host cannot be determined
+     * @throws ConnectTimeoutException
+     *             DOCUMENT ME!
+     * @throws IllegalArgumentException
+     *             DOCUMENT ME!
+     */
+    public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
+        if (params == null) {
+            throw new IllegalArgumentException("Parameters may not be null");
+        }
+
+        int timeout = params.getConnectionTimeout();
+
+        if (timeout == 0) {
+            return createSocket(host, port, localAddress, localPort);
+        } else {
+            // To be eventually deprecated when migrated to Java 1.4 or above
+            return ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout);
+        }
+    }
+
+    /**
+     * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
+     */
+    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
+        return getSSLContext().getSocketFactory().createSocket(host, port);
+    }
+
+    /**
+     * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
+     */
+    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
+        return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
+    }
+
+    public boolean equals(Object obj) {
+        return ((obj != null) && obj.getClass().equals(DefaultX509TrustManager.class));
+    }
+
+    public int hashCode() {
+        return DefaultX509TrustManager.class.hashCode();
+    }
+
+    private static SSLContext createEasySSLContext() {
+        try {
+            SSLContext context = SSLContext.getInstance("TLS");
+            context.init(null, new TrustManager[] { new DefaultX509TrustManager(null) }, null);
+
+            return context;
+        } catch (Exception e) {
+            LOG.error(e.getMessage(), e);
+            throw new HttpClientError(e.toString());
+        }
+    }
+
+    private SSLContext getSSLContext() {
+        if (this.sslcontext == null) {
+            this.sslcontext = createEasySSLContext();
+        }
+
+        return this.sslcontext;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/util/SQLTypeMap.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/util/SQLTypeMap.java b/jdbc/src/main/java/org/apache/kylin/jdbc/util/SQLTypeMap.java
deleted file mode 100644
index c4090ab..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/util/SQLTypeMap.java
+++ /dev/null
@@ -1,183 +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.kylin.jdbc.util;
-
-import java.math.BigDecimal;
-import java.sql.Date;
-import java.sql.Time;
-import java.sql.Timestamp;
-import java.sql.Types;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-import net.hydromatic.avatica.ColumnMetaData;
-import net.hydromatic.avatica.ColumnMetaData.Rep;
-
-import org.apache.kylin.jdbc.stub.KylinColumnMetaData;
-
-/**
- * Util class to handle type gap between sql types and java types.
- * 
- * @author xduo
- * 
- */
-public class SQLTypeMap {
-
-    public final static Map<String, ColumnMetaData> schemaMetaTypeMapping = new LinkedHashMap<String, ColumnMetaData>();
-
-    public final static Map<String, ColumnMetaData> columnMetaTypeMapping = new LinkedHashMap<String, ColumnMetaData>();
-
-    public final static Map<String, ColumnMetaData> tableMetaTypeMapping = new LinkedHashMap<String, ColumnMetaData>();
-
-    static {
-        schemaMetaTypeMapping.put("TABLE_CAT", KylinColumnMetaData.dummy(0, "TABLE_SCHEM", "TABLE_SCHEM", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        schemaMetaTypeMapping.put("TABLE_SCHEM", KylinColumnMetaData.dummy(1, "TABLE_CATALOG", "TABLE_CATALOG", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-
-        tableMetaTypeMapping.put("TABLE_CAT", KylinColumnMetaData.dummy(0, "TABLE_CAT", "TABLE_CAT", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        tableMetaTypeMapping.put("TABLE_SCHEM", KylinColumnMetaData.dummy(1, "TABLE_SCHEM", "TABLE_SCHEM", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        tableMetaTypeMapping.put("TABLE_NAME", KylinColumnMetaData.dummy(2, "TABLE_NAME", "TABLE_NAME", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        tableMetaTypeMapping.put("TABLE_TYPE", KylinColumnMetaData.dummy(3, "TABLE_TYPE", "TABLE_TYPE", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        tableMetaTypeMapping.put("REMARKS", KylinColumnMetaData.dummy(4, "REMARKS", "REMARKS", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        tableMetaTypeMapping.put("TYPE_CAT", KylinColumnMetaData.dummy(5, "TYPE_CAT", "TYPE_CAT", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        tableMetaTypeMapping.put("TYPE_SCHEM", KylinColumnMetaData.dummy(6, "TYPE_SCHEM", "TYPE_SCHEM", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        tableMetaTypeMapping.put("TYPE_NAME", KylinColumnMetaData.dummy(7, "TYPE_NAME", "TYPE_NAME", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        tableMetaTypeMapping.put("SELF_REFERENCING_COL_NAME", KylinColumnMetaData.dummy(8, "SELF_REFERENCING_COL_NAME", "SELF_REFERENCING_COL_NAME", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        tableMetaTypeMapping.put("REF_GENERATION", KylinColumnMetaData.dummy(9, "REF_GENERATION", "REF_GENERATION", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-
-        columnMetaTypeMapping.put("TABLE_CAT", KylinColumnMetaData.dummy(0, "TABLE_CAT", "TABLE_CAT", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("TABLE_SCHEM", KylinColumnMetaData.dummy(1, "TABLE_SCHEM", "TABLE_SCHEM", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("TABLE_NAME", KylinColumnMetaData.dummy(2, "TABLE_NAME", "TABLE_NAME", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("COLUMN_NAME", KylinColumnMetaData.dummy(3, "COLUMN_NAME", "COLUMN_NAME", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("DATA_TYPE", KylinColumnMetaData.dummy(4, "DATA_TYPE", "DATA_TYPE", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("TYPE_NAME", KylinColumnMetaData.dummy(5, "TYPE_NAME", "TYPE_NAME", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("COLUMN_SIZE", KylinColumnMetaData.dummy(6, "COLUMN_SIZE", "COLUMN_SIZE", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("BUFFER_LENGTH", KylinColumnMetaData.dummy(7, "BUFFER_LENGTH", "BUFFER_LENGTH", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("DECIMAL_DIGITS", KylinColumnMetaData.dummy(8, "DECIMAL_DIGITS", "DECIMAL_DIGITS", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("NUM_PREC_RADIX", KylinColumnMetaData.dummy(9, "NUM_PREC_RADIX", "NUM_PREC_RADIX", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("NULLABLE", KylinColumnMetaData.dummy(10, "NULLABLE", "NULLABLE", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("REMARKS", KylinColumnMetaData.dummy(11, "REMARKS", "REMARKS", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("COLUMN_DEF", KylinColumnMetaData.dummy(12, "COLUMN_DEF", "COLUMN_DEF", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("SQL_DATA_TYPE", KylinColumnMetaData.dummy(13, "SQL_DATA_TYPE", "SQL_DATA_TYPE", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("SQL_DATETIME_SUB", KylinColumnMetaData.dummy(14, "SQL_DATETIME_SUB", "SQL_DATETIME_SUB", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("CHAR_OCTET_LENGTH", KylinColumnMetaData.dummy(15, "CHAR_OCTET_LENGTH", "CHAR_OCTET_LENGTH", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("ORDINAL_POSITION", KylinColumnMetaData.dummy(16, "ORDINAL_POSITION", "ORDINAL_POSITION", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("IS_NULLABLE", KylinColumnMetaData.dummy(17, "IS_NULLABLE", "IS_NULLABLE", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("SCOPE_CATALOG", KylinColumnMetaData.dummy(18, "SCOPE_CATALOG", "SCOPE_CATALOG", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("SCOPE_TABLE", KylinColumnMetaData.dummy(19, "SCOPE_TABLE", "SCOPE_TABLE", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("SOURCE_DATA_TYPE", KylinColumnMetaData.dummy(20, "SOURCE_DATA_TYPE", "SOURCE_DATA_TYPE", ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER), true));
-        columnMetaTypeMapping.put("IS_AUTOINCREMENT", KylinColumnMetaData.dummy(21, "IS_AUTOINCREMENT", "IS_AUTOINCREMENT", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        columnMetaTypeMapping.put("IS_GENERATEDCOLUMN", KylinColumnMetaData.dummy(22, "IS_GENERATEDCOLUMN", "IS_GENERATEDCOLUMN", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-    }
-
-    @SuppressWarnings("rawtypes")
-    public static Class convert(int sqlType) {
-        Class result = Object.class;
-
-        switch (sqlType) {
-        case Types.CHAR:
-        case Types.VARCHAR:
-        case Types.LONGVARCHAR:
-            result = String.class;
-            break;
-        case Types.NUMERIC:
-        case Types.DECIMAL:
-            result = BigDecimal.class;
-            break;
-        case Types.BIT:
-            result = Boolean.class;
-            break;
-        case Types.TINYINT:
-            result = Byte.class;
-            break;
-        case Types.SMALLINT:
-            result = Short.class;
-            break;
-        case Types.INTEGER:
-            result = Integer.class;
-            break;
-        case Types.BIGINT:
-            result = Long.class;
-            break;
-        case Types.REAL:
-        case Types.FLOAT:
-        case Types.DOUBLE:
-            result = Double.class;
-            break;
-        case Types.BINARY:
-        case Types.VARBINARY:
-        case Types.LONGVARBINARY:
-            result = Byte[].class;
-            break;
-        case Types.DATE:
-            result = Date.class;
-            break;
-        case Types.TIME:
-            result = Time.class;
-            break;
-        case Types.TIMESTAMP:
-            result = Timestamp.class;
-            break;
-        }
-
-        return result;
-    }
-
-    public static Object wrapObject(String value, int sqlType) {
-        if (null == value) {
-            return null;
-        }
-
-        switch (sqlType) {
-        case Types.CHAR:
-        case Types.VARCHAR:
-        case Types.LONGVARCHAR:
-            return value;
-        case Types.NUMERIC:
-        case Types.DECIMAL:
-            return new BigDecimal(value);
-        case Types.BIT:
-            return Boolean.parseBoolean(value);
-        case Types.TINYINT:
-            return Byte.valueOf(value);
-        case Types.SMALLINT:
-            return Short.valueOf(value);
-        case Types.INTEGER:
-            return Integer.parseInt(value);
-        case Types.BIGINT:
-            return Long.parseLong(value);
-        case Types.FLOAT:
-            return Float.parseFloat(value);
-        case Types.REAL:
-        case Types.DOUBLE:
-            return Double.parseDouble(value);
-        case Types.BINARY:
-        case Types.VARBINARY:
-        case Types.LONGVARBINARY:
-            return value.getBytes();
-        case Types.DATE:
-            return Date.valueOf(value);
-        case Types.TIME:
-            return Time.valueOf(value);
-        case Types.TIMESTAMP:
-            return Timestamp.valueOf(value);
-        }
-
-        return value;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/test/java/org/apache/kylin/jdbc/DriverTest.java
----------------------------------------------------------------------
diff --git a/jdbc/src/test/java/org/apache/kylin/jdbc/DriverTest.java b/jdbc/src/test/java/org/apache/kylin/jdbc/DriverTest.java
index 4594e1a..c17efb1 100644
--- a/jdbc/src/test/java/org/apache/kylin/jdbc/DriverTest.java
+++ b/jdbc/src/test/java/org/apache/kylin/jdbc/DriverTest.java
@@ -1,174 +1,165 @@
-/*
- * 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.kylin.jdbc;
-
-import static org.junit.Assert.*;
-
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Properties;
-
-import org.junit.Ignore;
-import org.junit.Test;
-
-/**
- * Unit test for Driver.
- * 
- * @author xduo
- * 
- */
-public class DriverTest {
-
-    @Test
-    public void testStatementWithMockData() throws SQLException {
-        Driver driver = new DummyDriver();
-
-        Connection conn = driver.connect("jdbc:kylin://test_url/test_db", null);
-
-        ResultSet tables = conn.getMetaData().getTables(null, null, null, null);
-        while (tables.next()) {
-            for (int i = 0; i < 10; i++) {
-                assertEquals("dummy", tables.getString(i + 1));
-            }
-        }
-
-        Statement state = conn.createStatement();
-        ResultSet resultSet = state.executeQuery("select * from test_table");
-
-        ResultSetMetaData metadata = resultSet.getMetaData();
-        assertEquals(12, metadata.getColumnType(1));
-        assertEquals("varchar", metadata.getColumnTypeName(1));
-        assertEquals(1, metadata.isNullable(1));
-
-        while (resultSet.next()) {
-            assertEquals("foo", resultSet.getString(1));
-            assertEquals("bar", resultSet.getString(2));
-            assertEquals("tool", resultSet.getString(3));
-        }
-    }
-
-    @Test
-    public void testPreStatementWithMockData() throws SQLException {
-        Driver driver = new DummyDriver();
-
-        Connection conn = driver.connect("jdbc:kylin://test_url/test_db", null);
-        PreparedStatement state = conn.prepareStatement("select * from test_table where id=?");
-        state.setInt(1, 10);
-        ResultSet resultSet = state.executeQuery();
-
-        ResultSetMetaData metadata = resultSet.getMetaData();
-        assertEquals(12, metadata.getColumnType(1));
-        assertEquals("varchar", metadata.getColumnTypeName(1));
-        assertEquals(1, metadata.isNullable(1));
-
-        while (resultSet.next()) {
-            assertEquals("foo", resultSet.getString(1));
-            assertEquals("bar", resultSet.getString(2));
-            assertEquals("tool", resultSet.getString(3));
-        }
-    }
-
-    @Ignore("not maintaining")
-    @Test
-    public void testWithCubeData() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
-        Driver driver = (Driver) Class.forName("org.apache.kylin.kylin.jdbc.Driver").newInstance();
-        Properties info = new Properties();
-        info.put("user", "");
-        info.put("password", "");
-        Connection conn = driver.connect("jdbc:kylin://localhost/default", info);
-
-        ResultSet catalogs = conn.getMetaData().getCatalogs();
-        while (catalogs.next()) {
-            System.out.println(catalogs.getString("TABLE_CAT"));
-        }
-
-        ResultSet schemas = conn.getMetaData().getSchemas();
-        while (schemas.next()) {
-            System.out.println(schemas.getString(1));
-            System.out.println(schemas.getString(2));
-        }
-
-        ResultSet tables = conn.getMetaData().getTables(null, null, null, null);
-        while (tables.next()) {
-            String tableName = tables.getString(3);
-            assertEquals(tables.getString("TABLE_NAME"), tableName);
-            ResultSet columns = conn.getMetaData().getColumns(null, null, tableName, null);
-
-            while (columns.next()) {
-                System.out.println(columns.getString("COLUMN_NAME"));
-                String column = "";
-                for (int i = 0; i < 23; i++) {
-                    column += columns.getString(i + 1) + ", ";
-                }
-
-                System.out.println("Column in table " + tableName + ": " + column);
-            }
-        }
-
-        for (int j = 0; j < 3; j++) {
-            Statement state = conn.createStatement();
-            ResultSet resultSet = state.executeQuery("select * from test_kylin_fact");
-
-            ResultSetMetaData metadata = resultSet.getMetaData();
-            System.out.println("Metadata:");
-
-            for (int i = 0; i < metadata.getColumnCount(); i++) {
-                String metaStr = metadata.getCatalogName(i + 1) + " " + metadata.getColumnClassName(i + 1) + " " + metadata.getColumnDisplaySize(i + 1) + " " + metadata.getColumnLabel(i + 1) + " " + metadata.getColumnName(i + 1) + " " + metadata.getColumnType(i + 1) + " " + metadata.getColumnTypeName(i + 1) + " " + metadata.getPrecision(i + 1) + " " + metadata.getScale(i + 1) + " " + metadata.getSchemaName(i + 1) + " " + metadata.getTableName(i + 1);
-                System.out.println(metaStr);
-            }
-
-            System.out.println("Data:");
-            while (resultSet.next()) {
-                String dataStr = resultSet.getFloat(1) + " " + resultSet.getInt(2) + " " + resultSet.getInt(3) + " " + resultSet.getLong(4) + " " + resultSet.getDate(5) + " " + resultSet.getString(6);
-                System.out.println(dataStr);
-            }
-        }
-    }
-
-    @Ignore("not maintaining")
-    @Test
-    public void testPreStatementWithCubeData() throws SQLException {
-        Driver driver = new Driver();
-        Properties info = new Properties();
-        info.put("user", "");
-        info.put("password", "");
-        Connection conn = driver.connect("jdbc:kylin://localhost/default", info);
-        PreparedStatement state = conn.prepareStatement("select * from test_kylin_fact where seller_id=?");
-        state.setLong(1, 10000001);
-        ResultSet resultSet = state.executeQuery();
-
-        ResultSetMetaData metadata = resultSet.getMetaData();
-        System.out.println("Metadata:");
-
-        for (int i = 0; i < metadata.getColumnCount(); i++) {
-            String metaStr = metadata.getCatalogName(i + 1) + " " + metadata.getColumnClassName(i + 1) + " " + metadata.getColumnDisplaySize(i + 1) + " " + metadata.getColumnLabel(i + 1) + " " + metadata.getColumnName(i + 1) + " " + metadata.getColumnType(i + 1) + " " + metadata.getColumnTypeName(i + 1) + " " + metadata.getPrecision(i + 1) + " " + metadata.getScale(i + 1) + " " + metadata.getSchemaName(i + 1) + " " + metadata.getTableName(i + 1);
-            System.out.println(metaStr);
-        }
-
-        System.out.println("Data:");
-        while (resultSet.next()) {
-            String dataStr = resultSet.getFloat(1) + " " + resultSet.getInt(2) + " " + resultSet.getInt(3) + " " + resultSet.getLong(4) + " " + resultSet.getDate(5) + " " + resultSet.getString(6);
-            System.out.println(dataStr);
-        }
-    }
-}
+/*
+ * 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.kylin.jdbc;
+
+import static org.junit.Assert.*;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Properties;
+
+import org.apache.kylin.jdbc.Driver;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * Unit test for Driver.
+ */
+public class DriverTest {
+
+    @Test
+    public void testStatementWithMockData() throws SQLException {
+        Driver driver = new DummyDriver();
+
+        Connection conn = driver.connect("jdbc:kylin://test_url/test_db", null);
+
+        ResultSet tables = conn.getMetaData().getTables(null, null, null, null);
+        while (tables.next()) {
+            for (int i = 0; i < 4; i++) {
+                assertEquals("dummy", tables.getString(i + 1));
+            }
+            for (int i = 4; i < 10; i++) {
+                assertEquals(null, tables.getString(i + 1));
+            }
+        }
+
+        Statement state = conn.createStatement();
+        ResultSet resultSet = state.executeQuery("select * from test_table");
+
+        ResultSetMetaData metadata = resultSet.getMetaData();
+        assertEquals(12, metadata.getColumnType(1));
+        assertEquals("varchar", metadata.getColumnTypeName(1));
+        assertEquals(1, metadata.isNullable(1));
+
+        while (resultSet.next()) {
+            assertEquals("foo", resultSet.getString(1));
+            assertEquals("bar", resultSet.getString(2));
+            assertEquals("tool", resultSet.getString(3));
+        }
+    }
+
+    @Test
+    public void testPreparedStatementWithMockData() throws SQLException {
+        Driver driver = new DummyDriver();
+
+        Connection conn = driver.connect("jdbc:kylin://test_url/test_db", null);
+        PreparedStatement state = conn.prepareStatement("select * from test_table where id=?");
+        state.setInt(1, 10);
+        ResultSet resultSet = state.executeQuery();
+
+        ResultSetMetaData metadata = resultSet.getMetaData();
+        assertEquals(12, metadata.getColumnType(1));
+        assertEquals("varchar", metadata.getColumnTypeName(1));
+        assertEquals(1, metadata.isNullable(1));
+
+        while (resultSet.next()) {
+            assertEquals("foo", resultSet.getString(1));
+            assertEquals("bar", resultSet.getString(2));
+            assertEquals("tool", resultSet.getString(3));
+        }
+    }
+
+    @Ignore("require dev sandbox")
+    @Test
+    public void testWithCubeData() throws Exception {
+        Driver driver = new Driver();
+        Properties info = new Properties();
+        info.put("user", "ADMIN");
+        info.put("password", "KYLIN");
+        Connection conn = driver.connect("jdbc:kylin://localhost:7070/default", info);
+
+        ResultSet catalogs = conn.getMetaData().getCatalogs();
+        System.out.println("CATALOGS");
+        printResultSet(catalogs);
+
+        ResultSet schemas = conn.getMetaData().getSchemas();
+        System.out.println("SCHEMAS");
+        printResultSet(schemas);
+
+        ResultSet tables = conn.getMetaData().getTables(null, null, null, null);
+        System.out.println("TABLES");
+        printResultSet(tables);
+
+        for (int j = 0; j < 3; j++) {
+            Statement state = conn.createStatement();
+            ResultSet resultSet = state.executeQuery("select * from test_kylin_fact");
+
+            printResultSetMetaData(resultSet);
+            printResultSet(resultSet);
+        }
+    }
+
+    @Ignore("require dev sandbox")
+    @Test
+    public void testPreparedStatementWithCubeData() throws SQLException {
+        Driver driver = new Driver();
+        Properties info = new Properties();
+        info.put("user", "ADMIN");
+        info.put("password", "KYLIN");
+        Connection conn = driver.connect("jdbc:kylin://localhost:7070/default", info);
+        
+        PreparedStatement state = conn.prepareStatement("select cal_dt, count(*) from test_kylin_fact where seller_id=? group by cal_dt");
+        state.setLong(1, 10000001);
+        ResultSet resultSet = state.executeQuery();
+
+        printResultSetMetaData(resultSet);
+        printResultSet(resultSet);
+    }
+
+    private void printResultSet(ResultSet rs) throws SQLException {
+        ResultSetMetaData meta = rs.getMetaData();
+        System.out.println("Data:");
+        
+        while (rs.next()) {
+            StringBuilder buf = new StringBuilder();
+            buf.append("[");
+            for (int i = 0; i < meta.getColumnCount(); i++) {
+                if (i > 0)
+                    buf.append(", ");
+                buf.append(rs.getString(i + 1));
+            }
+            buf.append("]");
+            System.out.println(buf);
+        }
+    }
+
+    private void printResultSetMetaData(ResultSet rs) throws SQLException {
+        ResultSetMetaData metadata = rs.getMetaData();
+        System.out.println("Metadata:");
+
+        for (int i = 0; i < metadata.getColumnCount(); i++) {
+            String metaStr = metadata.getCatalogName(i + 1) + " " + metadata.getColumnClassName(i + 1) + " " + metadata.getColumnDisplaySize(i + 1) + " " + metadata.getColumnLabel(i + 1) + " " + metadata.getColumnName(i + 1) + " " + metadata.getColumnType(i + 1) + " " + metadata.getColumnTypeName(i + 1) + " " + metadata.getPrecision(i + 1) + " " + metadata.getScale(i + 1) + " " + metadata.getSchemaName(i + 1) + " " + metadata.getTableName(i + 1);
+            System.out.println(metaStr);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/test/java/org/apache/kylin/jdbc/DummyClient.java
----------------------------------------------------------------------
diff --git a/jdbc/src/test/java/org/apache/kylin/jdbc/DummyClient.java b/jdbc/src/test/java/org/apache/kylin/jdbc/DummyClient.java
index 7af3cb7..0dcd624 100644
--- a/jdbc/src/test/java/org/apache/kylin/jdbc/DummyClient.java
+++ b/jdbc/src/test/java/org/apache/kylin/jdbc/DummyClient.java
@@ -1,80 +1,85 @@
-/*
- * 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.kylin.jdbc;
-
-import java.sql.Types;
-import java.util.ArrayList;
-import java.util.List;
-
-import net.hydromatic.avatica.AvaticaStatement;
-import net.hydromatic.avatica.ColumnMetaData;
-import net.hydromatic.avatica.ColumnMetaData.Rep;
-import net.hydromatic.linq4j.Enumerator;
-
-import org.apache.kylin.jdbc.stub.ConnectionException;
-import org.apache.kylin.jdbc.stub.DataSet;
-import org.apache.kylin.jdbc.stub.RemoteClient;
-
-/**
- * @author xduo
- * 
- */
-public class DummyClient implements RemoteClient {
-
-    public DummyClient(KylinConnectionImpl conn) {
-    }
-
-    @Override
-    public void connect() throws ConnectionException {
-    }
-
-    @Override
-    public KylinMetaImpl.MetaProject getMetadata(String project) throws ConnectionException {
-        List<ColumnMetaData> meta = new ArrayList<ColumnMetaData>();
-        for (int i = 0; i < 10; i++) {
-            meta.add(ColumnMetaData.dummy(ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        }
-
-        List<KylinMetaImpl.MetaTable> tables = new ArrayList<KylinMetaImpl.MetaTable>();
-        KylinMetaImpl.MetaTable table = new KylinMetaImpl.MetaTable("dummy", "dummy", "dummy", "dummy", "dummy", "dummy", "dummy", "dummy", "dummy", "dummy", new ArrayList<KylinMetaImpl.MetaColumn>());
-        tables.add(table);
-
-        List<KylinMetaImpl.MetaSchema> schemas = new ArrayList<KylinMetaImpl.MetaSchema>();
-        schemas.add(new KylinMetaImpl.MetaSchema("dummy", "dummy", tables));
-        List<KylinMetaImpl.MetaCatalog> catalogs = new ArrayList<KylinMetaImpl.MetaCatalog>();
-        catalogs.add(new KylinMetaImpl.MetaCatalog("dummy", schemas));
-
-        return new KylinMetaImpl.MetaProject(null, catalogs);
-    }
-
-    @Override
-    public DataSet<Object[]> query(AvaticaStatement statement, String sql) {
-        List<Object[]> data = new ArrayList<Object[]>();
-        Object[] row = new Object[] { "foo", "bar", "tool" };
-        data.add(row);
-        Enumerator<Object[]> enumerator = new KylinEnumerator<Object[]>(data);
-        List<ColumnMetaData> meta = new ArrayList<ColumnMetaData>();
-        meta.add(ColumnMetaData.dummy(ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        meta.add(ColumnMetaData.dummy(ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        meta.add(ColumnMetaData.dummy(ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-
-        return new DataSet<Object[]>(meta, enumerator);
-    }
-
-}
+/*
+ * 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.kylin.jdbc;
+
+import java.io.IOException;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.calcite.avatica.AvaticaParameter;
+import org.apache.calcite.avatica.ColumnMetaData;
+import org.apache.calcite.avatica.ColumnMetaData.Rep;
+import org.apache.kylin.jdbc.IRemoteClient;
+import org.apache.kylin.jdbc.KylinConnection;
+import org.apache.kylin.jdbc.KylinMeta.KMetaCatalog;
+import org.apache.kylin.jdbc.KylinMeta.KMetaColumn;
+import org.apache.kylin.jdbc.KylinMeta.KMetaProject;
+import org.apache.kylin.jdbc.KylinMeta.KMetaSchema;
+import org.apache.kylin.jdbc.KylinMeta.KMetaTable;
+
+/**
+ */
+public class DummyClient implements IRemoteClient {
+
+    public DummyClient(KylinConnection conn) {
+    }
+
+    @Override
+    public void connect() throws IOException {
+    }
+
+    @Override
+    public KMetaProject retrieveMetaData(String project) throws IOException {
+        List<KMetaColumn> columns = new ArrayList<KMetaColumn>();
+        
+        KMetaTable table = new KMetaTable("dummy", "dummy", "dummy", "dummy", columns);
+        List<KMetaTable> tables = new ArrayList<KMetaTable>();
+        tables.add(table);
+
+        KMetaSchema schema = new KMetaSchema("dummy", "dummy", tables);
+        List<KMetaSchema> schemas = new ArrayList<KMetaSchema>();
+        schemas.add(schema);
+        
+        KMetaCatalog catalog = new KMetaCatalog("dummay", schemas);
+        List<KMetaCatalog> catalogs = new ArrayList<KMetaCatalog>();
+        catalogs.add(catalog);
+        
+        return new KMetaProject(project, catalogs);
+    }
+
+    @Override
+    public QueryResult executeQuery(String sql, List<AvaticaParameter> params, List<Object> paramValues) throws IOException {
+        List<Object> data = new ArrayList<Object>();
+        Object[] row = new Object[] { "foo", "bar", "tool" };
+        data.add(row);
+        
+        List<ColumnMetaData> meta = new ArrayList<ColumnMetaData>();
+        meta.add(ColumnMetaData.dummy(ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
+        meta.add(ColumnMetaData.dummy(ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
+        meta.add(ColumnMetaData.dummy(ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
+
+        return new QueryResult(meta, data);
+    }
+
+    @Override
+    public void close() throws IOException {
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/test/java/org/apache/kylin/jdbc/DummyDriver.java
----------------------------------------------------------------------
diff --git a/jdbc/src/test/java/org/apache/kylin/jdbc/DummyDriver.java b/jdbc/src/test/java/org/apache/kylin/jdbc/DummyDriver.java
index a5f5383..43c7b75 100644
--- a/jdbc/src/test/java/org/apache/kylin/jdbc/DummyDriver.java
+++ b/jdbc/src/test/java/org/apache/kylin/jdbc/DummyDriver.java
@@ -1,32 +1,32 @@
-/*
- * 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.kylin.jdbc;
-
-/**
- * @author xduo
- * 
- */
-public class DummyDriver extends Driver {
-
-    @Override
-    protected String getFactoryClassName(JdbcVersion jdbcVersion) {
-        return "org.apache.kylin.jdbc.DummyJdbc41Factory";
-    }
-
-}
+/*
+ * 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.kylin.jdbc;
+
+import org.apache.kylin.jdbc.Driver;
+
+/**
+ */
+public class DummyDriver extends Driver {
+
+    @Override
+    protected String getFactoryClassName(JdbcVersion jdbcVersion) {
+        return DummyJdbcFactory.class.getName();
+    }
+
+}


[06/10] incubator-kylin git commit: KYLIN-780 Upgrade query module, all query test pass

Posted by li...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/atopcalcite/src/main/java/org/eigenbase/sql2rel/SqlToRelConverter.java
----------------------------------------------------------------------
diff --git a/atopcalcite/src/main/java/org/eigenbase/sql2rel/SqlToRelConverter.java b/atopcalcite/src/main/java/org/eigenbase/sql2rel/SqlToRelConverter.java
deleted file mode 100644
index dd7ee01..0000000
--- a/atopcalcite/src/main/java/org/eigenbase/sql2rel/SqlToRelConverter.java
+++ /dev/null
@@ -1,4795 +0,0 @@
-/*
- * OVERRIDE POINT:
- * - getInSubqueryThreshold(), was `20`, now `Integer.MAX_VALUE`
- * - isTrimUnusedFields(), override to false
- * - AggConverter.visit(SqlCall), skip column reading for COUNT(COL), for https://jirap.corp.ebay.com/browse/KYLIN-104
- */
-
-/*
- * 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.eigenbase.sql2rel;
-
-import java.lang.reflect.Type;
-import java.math.*;
-import java.util.*;
-import java.util.logging.*;
-
-import org.eigenbase.rel.*;
-import org.eigenbase.rel.metadata.*;
-import org.eigenbase.relopt.*;
-import org.eigenbase.reltype.*;
-import org.eigenbase.rex.*;
-import org.eigenbase.sql.*;
-import org.eigenbase.sql.fun.*;
-import org.eigenbase.sql.parser.*;
-import org.eigenbase.sql.type.*;
-import org.eigenbase.sql.util.*;
-import org.eigenbase.sql.validate.*;
-import org.eigenbase.trace.*;
-import org.eigenbase.util.*;
-import org.eigenbase.util.mapping.Mappings;
-import org.eigenbase.util14.*;
-
-import net.hydromatic.linq4j.Ord;
-
-import net.hydromatic.optiq.ModifiableTable;
-import net.hydromatic.optiq.TranslatableTable;
-import net.hydromatic.optiq.prepare.Prepare;
-import net.hydromatic.optiq.prepare.RelOptTableImpl;
-import net.hydromatic.optiq.util.BitSets;
-
-import com.google.common.base.Function;
-import com.google.common.collect.*;
-
-import static org.eigenbase.sql.SqlUtil.stripAs;
-import static org.eigenbase.util.Static.RESOURCE;
-
-/**
- * Converts a SQL parse tree (consisting of {@link org.eigenbase.sql.SqlNode}
- * objects) into a relational algebra expression (consisting of
- * {@link org.eigenbase.rel.RelNode} objects).
- *
- * <p>The public entry points are: {@link #convertQuery},
- * {@link #convertExpression(SqlNode)}.
- */
-public class SqlToRelConverter {
-  //~ Static fields/initializers ---------------------------------------------
-
-  protected static final Logger SQL2REL_LOGGER =
-      EigenbaseTrace.getSqlToRelTracer();
-
-  private static final Function<SubQuery, SqlNode> FN =
-      new Function<SubQuery, SqlNode>() {
-        public SqlNode apply(SubQuery input) {
-          return input.node;
-        }
-      };
-
-  //~ Instance fields --------------------------------------------------------
-
-  protected final SqlValidator validator;
-  protected final RexBuilder rexBuilder;
-  protected final Prepare.CatalogReader catalogReader;
-  protected final RelOptCluster cluster;
-  private DefaultValueFactory defaultValueFactory;
-  private SubqueryConverter subqueryConverter;
-  protected final List<RelNode> leaves = new ArrayList<RelNode>();
-  private final List<SqlDynamicParam> dynamicParamSqlNodes =
-      new ArrayList<SqlDynamicParam>();
-  private final SqlOperatorTable opTab;
-  private boolean shouldConvertTableAccess;
-  protected final RelDataTypeFactory typeFactory;
-  private final SqlNodeToRexConverter exprConverter;
-  private boolean decorrelationEnabled;
-  private boolean trimUnusedFields;
-  private boolean shouldCreateValuesRel;
-  private boolean isExplain;
-  private int nDynamicParamsInExplain;
-
-  /**
-   * Fields used in name resolution for correlated subqueries.
-   */
-  private final Map<String, DeferredLookup> mapCorrelToDeferred =
-      new HashMap<String, DeferredLookup>();
-  private int nextCorrel = 0;
-
-  private static final String CORREL_PREFIX = "$cor";
-
-  /**
-   * Stack of names of datasets requested by the <code>
-   * TABLE(SAMPLE(&lt;datasetName&gt;, &lt;query&gt;))</code> construct.
-   */
-  private final Stack<String> datasetStack = new Stack<String>();
-
-  /**
-   * Mapping of non-correlated subqueries that have been converted to their
-   * equivalent constants. Used to avoid re-evaluating the subquery if it's
-   * already been evaluated.
-   */
-  private final Map<SqlNode, RexNode> mapConvertedNonCorrSubqs =
-      new HashMap<SqlNode, RexNode>();
-
-  public final RelOptTable.ViewExpander viewExpander;
-
-  //~ Constructors -----------------------------------------------------------
-  /**
-   * Creates a converter.
-   *
-   * @param viewExpander    Preparing statement
-   * @param validator       Validator
-   * @param catalogReader   Schema
-   * @param planner         Planner
-   * @param rexBuilder      Rex builder
-   * @param convertletTable Expression converter
-   */
-  public SqlToRelConverter(
-      RelOptTable.ViewExpander viewExpander,
-      SqlValidator validator,
-      Prepare.CatalogReader catalogReader,
-      RelOptPlanner planner,
-      RexBuilder rexBuilder,
-      SqlRexConvertletTable convertletTable) {
-    this.viewExpander = viewExpander;
-    this.opTab =
-        (validator
-            == null) ? SqlStdOperatorTable.instance()
-            : validator.getOperatorTable();
-    this.validator = validator;
-    this.catalogReader = catalogReader;
-    this.defaultValueFactory = new NullDefaultValueFactory();
-    this.subqueryConverter = new NoOpSubqueryConverter();
-    this.rexBuilder = rexBuilder;
-    this.typeFactory = rexBuilder.getTypeFactory();
-    RelOptQuery query = new RelOptQuery(planner);
-    this.cluster = query.createCluster(typeFactory, rexBuilder);
-    this.shouldConvertTableAccess = true;
-    this.exprConverter =
-        new SqlNodeToRexConverterImpl(convertletTable);
-    decorrelationEnabled = true;
-    trimUnusedFields = false;
-    shouldCreateValuesRel = true;
-    isExplain = false;
-    nDynamicParamsInExplain = 0;
-  }
-
-  //~ Methods ----------------------------------------------------------------
-
-  /**
-   * @return the RelOptCluster in use.
-   */
-  public RelOptCluster getCluster() {
-    return cluster;
-  }
-
-  /**
-   * Returns the row-expression builder.
-   */
-  public RexBuilder getRexBuilder() {
-    return rexBuilder;
-  }
-
-  /**
-   * Returns the number of dynamic parameters encountered during translation;
-   * this must only be called after {@link #convertQuery}.
-   *
-   * @return number of dynamic parameters
-   */
-  public int getDynamicParamCount() {
-    return dynamicParamSqlNodes.size();
-  }
-
-  /**
-   * Returns the type inferred for a dynamic parameter.
-   *
-   * @param index 0-based index of dynamic parameter
-   * @return inferred type, never null
-   */
-  public RelDataType getDynamicParamType(int index) {
-    SqlNode sqlNode = dynamicParamSqlNodes.get(index);
-    if (sqlNode == null) {
-      throw Util.needToImplement("dynamic param type inference");
-    }
-    return validator.getValidatedNodeType(sqlNode);
-  }
-
-  /**
-   * Returns the current count of the number of dynamic parameters in an
-   * EXPLAIN PLAN statement.
-   *
-   * @param increment if true, increment the count
-   * @return the current count before the optional increment
-   */
-  public int getDynamicParamCountInExplain(boolean increment) {
-    int retVal = nDynamicParamsInExplain;
-    if (increment) {
-      ++nDynamicParamsInExplain;
-    }
-    return retVal;
-  }
-
-  /**
-   * @return mapping of non-correlated subqueries that have been converted to
-   * the constants that they evaluate to
-   */
-  public Map<SqlNode, RexNode> getMapConvertedNonCorrSubqs() {
-    return mapConvertedNonCorrSubqs;
-  }
-
-  /**
-   * Adds to the current map of non-correlated converted subqueries the
-   * elements from another map that contains non-correlated subqueries that
-   * have been converted by another SqlToRelConverter.
-   *
-   * @param alreadyConvertedNonCorrSubqs the other map
-   */
-  public void addConvertedNonCorrSubqs(
-      Map<SqlNode, RexNode> alreadyConvertedNonCorrSubqs) {
-    mapConvertedNonCorrSubqs.putAll(alreadyConvertedNonCorrSubqs);
-  }
-
-  /**
-   * Set a new DefaultValueFactory. To have any effect, this must be called
-   * before any convert method.
-   *
-   * @param factory new DefaultValueFactory
-   */
-  public void setDefaultValueFactory(DefaultValueFactory factory) {
-    defaultValueFactory = factory;
-  }
-
-  /**
-   * Sets a new SubqueryConverter. To have any effect, this must be called
-   * before any convert method.
-   *
-   * @param converter new SubqueryConverter
-   */
-  public void setSubqueryConverter(SubqueryConverter converter) {
-    subqueryConverter = converter;
-  }
-
-  /**
-   * Indicates that the current statement is part of an EXPLAIN PLAN statement
-   *
-   * @param nDynamicParams number of dynamic parameters in the statement
-   */
-  public void setIsExplain(int nDynamicParams) {
-    isExplain = true;
-    nDynamicParamsInExplain = nDynamicParams;
-  }
-
-  /**
-   * Controls whether table access references are converted to physical rels
-   * immediately. The optimizer doesn't like leaf rels to have
-   * {@link Convention#NONE}. However, if we are doing further conversion
-   * passes (e.g. {@link RelStructuredTypeFlattener}), then we may need to
-   * defer conversion. To have any effect, this must be called before any
-   * convert method.
-   *
-   * @param enabled true for immediate conversion (the default); false to
-   *                generate logical TableAccessRel instances
-   */
-  public void enableTableAccessConversion(boolean enabled) {
-    shouldConvertTableAccess = enabled;
-  }
-
-  /**
-   * Controls whether instances of {@link ValuesRel} are generated. These may
-   * not be supported by all physical implementations. To have any effect,
-   * this must be called before any convert method.
-   *
-   * @param enabled true to allow ValuesRel to be generated (the default);
-   *                false to force substitution of ProjectRel+OneRowRel instead
-   */
-  public void enableValuesRelCreation(boolean enabled) {
-    shouldCreateValuesRel = enabled;
-  }
-
-  private void checkConvertedType(SqlNode query, RelNode result) {
-    if (!query.isA(SqlKind.DML)) {
-      // Verify that conversion from SQL to relational algebra did
-      // not perturb any type information.  (We can't do this if the
-      // SQL statement is something like an INSERT which has no
-      // validator type information associated with its result,
-      // hence the namespace check above.)
-      RelDataType convertedRowType = result.getRowType();
-      if (!checkConvertedRowType(query, convertedRowType)) {
-        RelDataType validatedRowType =
-            validator.getValidatedNodeType(query);
-        validatedRowType = uniquifyFields(validatedRowType);
-        throw Util.newInternal(
-            "Conversion to relational algebra failed to preserve "
-            + "datatypes:\n"
-            + "validated type:\n"
-            + validatedRowType.getFullTypeString()
-            + "\nconverted type:\n"
-            + convertedRowType.getFullTypeString()
-            + "\nrel:\n"
-            + RelOptUtil.toString(result));
-      }
-    }
-  }
-
-  public RelNode flattenTypes(
-      RelNode rootRel,
-      boolean restructure) {
-    RelStructuredTypeFlattener typeFlattener =
-        new RelStructuredTypeFlattener(rexBuilder, createToRelContext());
-    return typeFlattener.rewrite(rootRel, restructure);
-  }
-
-  /**
-   * If subquery is correlated and decorrelation is enabled, performs
-   * decorrelation.
-   *
-   * @param query   Query
-   * @param rootRel Root relational expression
-   * @return New root relational expression after decorrelation
-   */
-  public RelNode decorrelate(SqlNode query, RelNode rootRel) {
-    if (!enableDecorrelation()) {
-      return rootRel;
-    }
-    final RelNode result = decorrelateQuery(rootRel);
-    if (result != rootRel) {
-      checkConvertedType(query, result);
-    }
-    return result;
-  }
-
-  /**
-   * Walks over a tree of relational expressions, replacing each
-   * {@link RelNode} with a 'slimmed down' relational expression that projects
-   * only the fields required by its consumer.
-   *
-   * <p>This may make things easier for the optimizer, by removing crud that
-   * would expand the search space, but is difficult for the optimizer itself
-   * to do it, because optimizer rules must preserve the number and type of
-   * fields. Hence, this transform that operates on the entire tree, similar
-   * to the {@link RelStructuredTypeFlattener type-flattening transform}.
-   *
-   * <p>Currently this functionality is disabled in farrago/luciddb; the
-   * default implementation of this method does nothing.
-   *
-   * @param rootRel Relational expression that is at the root of the tree
-   * @return Trimmed relational expression
-   */
-  public RelNode trimUnusedFields(RelNode rootRel) {
-    // Trim fields that are not used by their consumer.
-    if (isTrimUnusedFields()) {
-      final RelFieldTrimmer trimmer = newFieldTrimmer();
-      rootRel = trimmer.trim(rootRel);
-      boolean dumpPlan = SQL2REL_LOGGER.isLoggable(Level.FINE);
-      if (dumpPlan) {
-        SQL2REL_LOGGER.fine(
-            RelOptUtil.dumpPlan(
-                "Plan after trimming unused fields",
-                rootRel,
-                false,
-                SqlExplainLevel.EXPPLAN_ATTRIBUTES));
-      }
-    }
-    return rootRel;
-  }
-
-  /**
-   * Creates a RelFieldTrimmer.
-   *
-   * @return Field trimmer
-   */
-  protected RelFieldTrimmer newFieldTrimmer() {
-    return new RelFieldTrimmer(validator);
-  }
-
-  /**
-   * Converts an unvalidated query's parse tree into a relational expression.
-   *
-   * @param query           Query to convert
-   * @param needsValidation Whether to validate the query before converting;
-   *                        <code>false</code> if the query has already been
-   *                        validated.
-   * @param top             Whether the query is top-level, say if its result
-   *                        will become a JDBC result set; <code>false</code> if
-   *                        the query will be part of a view.
-   */
-  public RelNode convertQuery(
-      SqlNode query,
-      final boolean needsValidation,
-      final boolean top) {
-    if (needsValidation) {
-      query = validator.validate(query);
-    }
-
-    RelNode result = convertQueryRecursive(query, top, null);
-    checkConvertedType(query, result);
-
-    boolean dumpPlan = SQL2REL_LOGGER.isLoggable(Level.FINE);
-    if (dumpPlan) {
-      SQL2REL_LOGGER.fine(
-          RelOptUtil.dumpPlan(
-              "Plan after converting SqlNode to RelNode",
-              result,
-              false,
-              SqlExplainLevel.EXPPLAN_ATTRIBUTES));
-    }
-
-    return result;
-  }
-
-  protected boolean checkConvertedRowType(
-      SqlNode query,
-      RelDataType convertedRowType) {
-    RelDataType validatedRowType = validator.getValidatedNodeType(query);
-    validatedRowType = uniquifyFields(validatedRowType);
-
-    return RelOptUtil.equal(
-        "validated row type",
-        validatedRowType,
-        "converted row type",
-        convertedRowType,
-        false);
-  }
-
-  protected RelDataType uniquifyFields(RelDataType rowType) {
-    return validator.getTypeFactory().createStructType(
-        RelOptUtil.getFieldTypeList(rowType),
-        SqlValidatorUtil.uniquify(rowType.getFieldNames()));
-  }
-
-  /**
-   * Converts a SELECT statement's parse tree into a relational expression.
-   */
-  public RelNode convertSelect(SqlSelect select) {
-    final SqlValidatorScope selectScope = validator.getWhereScope(select);
-    final Blackboard bb = createBlackboard(selectScope, null);
-    convertSelectImpl(bb, select);
-    return bb.root;
-  }
-
-  /**
-   * Factory method for creating translation workspace.
-   */
-  protected Blackboard createBlackboard(
-      SqlValidatorScope scope,
-      Map<String, RexNode> nameToNodeMap) {
-    return new Blackboard(scope, nameToNodeMap);
-  }
-
-  /**
-   * Implementation of {@link #convertSelect(SqlSelect)}; derived class may
-   * override.
-   */
-  protected void convertSelectImpl(
-      final Blackboard bb,
-      SqlSelect select) {
-    convertFrom(
-        bb,
-        select.getFrom());
-    convertWhere(
-        bb,
-        select.getWhere());
-
-    List<SqlNode> orderExprList = new ArrayList<SqlNode>();
-    List<RelFieldCollation> collationList =
-        new ArrayList<RelFieldCollation>();
-    gatherOrderExprs(
-        bb,
-        select,
-        select.getOrderList(),
-        orderExprList,
-        collationList);
-    final RelCollation collation =
-        cluster.traitSetOf().canonize(RelCollationImpl.of(collationList));
-
-    if (validator.isAggregate(select)) {
-      convertAgg(
-          bb,
-          select,
-          orderExprList);
-    } else {
-      convertSelectList(
-          bb,
-          select,
-          orderExprList);
-    }
-
-    if (select.isDistinct()) {
-      distinctify(bb, true);
-    }
-    convertOrder(
-        select, bb, collation, orderExprList, select.getOffset(),
-        select.getFetch());
-    bb.setRoot(bb.root, true);
-  }
-
-  /**
-   * Having translated 'SELECT ... FROM ... [GROUP BY ...] [HAVING ...]', adds
-   * a relational expression to make the results unique.
-   *
-   * <p>If the SELECT clause contains duplicate expressions, adds {@link
-   * ProjectRel}s so that we are grouping on the minimal set of keys. The
-   * performance gain isn't huge, but it is difficult to detect these
-   * duplicate expressions later.
-   *
-   * @param bb               Blackboard
-   * @param checkForDupExprs Check for duplicate expressions
-   */
-  private void distinctify(
-      Blackboard bb,
-      boolean checkForDupExprs) {
-    // Look for duplicate expressions in the project.
-    // Say we have 'select x, y, x, z'.
-    // Then dups will be {[2, 0]}
-    // and oldToNew will be {[0, 0], [1, 1], [2, 0], [3, 2]}
-    RelNode rel = bb.root;
-    if (checkForDupExprs && (rel instanceof ProjectRel)) {
-      ProjectRel project = (ProjectRel) rel;
-      final List<RexNode> projectExprs = project.getProjects();
-      List<Integer> origins = new ArrayList<Integer>();
-      int dupCount = 0;
-      for (int i = 0; i < projectExprs.size(); i++) {
-        int x = findExpr(projectExprs.get(i), projectExprs, i);
-        if (x >= 0) {
-          origins.add(x);
-          ++dupCount;
-        } else {
-          origins.add(i);
-        }
-      }
-      if (dupCount == 0) {
-        distinctify(bb, false);
-        return;
-      }
-
-      final Map<Integer, Integer> squished = Maps.newHashMap();
-      final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
-      final List<Pair<RexNode, String>> newProjects = Lists.newArrayList();
-      for (int i = 0; i < fields.size(); i++) {
-        if (origins.get(i) == i) {
-          squished.put(i, newProjects.size());
-          newProjects.add(RexInputRef.of2(i, fields));
-        }
-      }
-      rel =
-          new ProjectRel(
-              cluster,
-              rel,
-              Pair.left(newProjects),
-              Pair.right(newProjects),
-              ProjectRel.Flags.BOXED);
-
-      bb.root = rel;
-      distinctify(bb, false);
-      rel = bb.root;
-
-      // Create the expressions to reverse the mapping.
-      // Project($0, $1, $0, $2).
-      final List<Pair<RexNode, String>> undoProjects = Lists.newArrayList();
-      for (int i = 0; i < fields.size(); i++) {
-        final int origin = origins.get(i);
-        RelDataTypeField field = fields.get(i);
-        undoProjects.add(
-            Pair.of(
-                (RexNode) new RexInputRef(
-                    squished.get(origin), field.getType()),
-                field.getName()));
-      }
-
-      rel =
-          new ProjectRel(
-              cluster,
-              rel,
-              Pair.left(undoProjects),
-              Pair.right(undoProjects),
-              ProjectRel.Flags.BOXED);
-
-      bb.setRoot(
-          rel,
-          false);
-
-      return;
-    }
-
-    // Usual case: all of the expressions in the SELECT clause are
-    // different.
-    rel =
-        createAggregate(
-            bb,
-            BitSets.range(rel.getRowType().getFieldCount()),
-            ImmutableList.<AggregateCall>of());
-
-    bb.setRoot(
-        rel,
-        false);
-  }
-
-  private int findExpr(RexNode seek, List<RexNode> exprs, int count) {
-    for (int i = 0; i < count; i++) {
-      RexNode expr = exprs.get(i);
-      if (expr.toString().equals(seek.toString())) {
-        return i;
-      }
-    }
-    return -1;
-  }
-
-  /**
-   * Converts a query's ORDER BY clause, if any.
-   *
-   * @param select        Query
-   * @param bb            Blackboard
-   * @param collation     Collation list
-   * @param orderExprList Method populates this list with orderBy expressions
-   *                      not present in selectList
-   * @param offset        Expression for number of rows to discard before
-   *                      returning first row
-   * @param fetch         Expression for number of rows to fetch
-   */
-  protected void convertOrder(
-      SqlSelect select,
-      Blackboard bb,
-      RelCollation collation,
-      List<SqlNode> orderExprList,
-      SqlNode offset,
-      SqlNode fetch) {
-    if (select.getOrderList() == null) {
-      assert collation.getFieldCollations().isEmpty();
-      if (offset == null && fetch == null) {
-        return;
-      }
-    }
-
-    // Create a sorter using the previously constructed collations.
-    bb.setRoot(
-        new SortRel(
-            cluster,
-            cluster.traitSetOf(Convention.NONE, collation),
-            bb.root,
-            collation,
-            offset == null ? null : convertExpression(offset),
-            fetch == null ? null : convertExpression(fetch)),
-        false);
-
-    // If extra expressions were added to the project list for sorting,
-    // add another project to remove them.
-    if (orderExprList.size() > 0) {
-      List<RexNode> exprs = new ArrayList<RexNode>();
-      final RelDataType rowType = bb.root.getRowType();
-      final int fieldCount =
-          rowType.getFieldCount() - orderExprList.size();
-      for (int i = 0; i < fieldCount; i++) {
-        exprs.add(rexBuilder.makeInputRef(bb.root, i));
-      }
-      bb.setRoot(
-          new ProjectRel(
-              cluster,
-              cluster.traitSetOf(RelCollationImpl.PRESERVE),
-              bb.root,
-              exprs,
-              cluster.getTypeFactory().createStructType(
-                  rowType.getFieldList().subList(0, fieldCount)),
-              ProjectRelBase.Flags.BOXED),
-          false);
-    }
-  }
-
-  /**
-   * Returns whether a given node contains a {@link SqlInOperator}.
-   *
-   * @param node a RexNode tree
-   */
-  private static boolean containsInOperator(
-      SqlNode node) {
-    try {
-      SqlVisitor<Void> visitor =
-          new SqlBasicVisitor<Void>() {
-            public Void visit(SqlCall call) {
-              if (call.getOperator() instanceof SqlInOperator) {
-                throw new Util.FoundOne(call);
-              }
-              return super.visit(call);
-            }
-          };
-      node.accept(visitor);
-      return false;
-    } catch (Util.FoundOne e) {
-      Util.swallow(e, null);
-      return true;
-    }
-  }
-
-  /**
-   * Push down all the NOT logical operators into any IN/NOT IN operators.
-   *
-   * @param sqlNode the root node from which to look for NOT operators
-   * @return the transformed SqlNode representation with NOT pushed down.
-   */
-  private static SqlNode pushDownNotForIn(SqlNode sqlNode) {
-    if ((sqlNode instanceof SqlCall) && containsInOperator(sqlNode)) {
-      SqlCall sqlCall = (SqlCall) sqlNode;
-      if ((sqlCall.getOperator() == SqlStdOperatorTable.AND)
-          || (sqlCall.getOperator() == SqlStdOperatorTable.OR)) {
-        SqlNode[] sqlOperands = ((SqlBasicCall) sqlCall).operands;
-        for (int i = 0; i < sqlOperands.length; i++) {
-          sqlOperands[i] = pushDownNotForIn(sqlOperands[i]);
-        }
-        return sqlNode;
-      } else if (sqlCall.getOperator() == SqlStdOperatorTable.NOT) {
-        SqlNode childNode = sqlCall.operand(0);
-        assert childNode instanceof SqlCall;
-        SqlBasicCall childSqlCall = (SqlBasicCall) childNode;
-        if (childSqlCall.getOperator() == SqlStdOperatorTable.AND) {
-          SqlNode[] andOperands = childSqlCall.getOperands();
-          SqlNode[] orOperands = new SqlNode[andOperands.length];
-          for (int i = 0; i < orOperands.length; i++) {
-            orOperands[i] =
-                SqlStdOperatorTable.NOT.createCall(
-                    SqlParserPos.ZERO,
-                    andOperands[i]);
-          }
-          for (int i = 0; i < orOperands.length; i++) {
-            orOperands[i] = pushDownNotForIn(orOperands[i]);
-          }
-          return SqlStdOperatorTable.OR.createCall(SqlParserPos.ZERO,
-              orOperands[0], orOperands[1]);
-        } else if (childSqlCall.getOperator() == SqlStdOperatorTable.OR) {
-          SqlNode[] orOperands = childSqlCall.getOperands();
-          SqlNode[] andOperands = new SqlNode[orOperands.length];
-          for (int i = 0; i < andOperands.length; i++) {
-            andOperands[i] =
-                SqlStdOperatorTable.NOT.createCall(
-                    SqlParserPos.ZERO,
-                    orOperands[i]);
-          }
-          for (int i = 0; i < andOperands.length; i++) {
-            andOperands[i] = pushDownNotForIn(andOperands[i]);
-          }
-          return SqlStdOperatorTable.AND.createCall(SqlParserPos.ZERO,
-              andOperands[0], andOperands[1]);
-        } else if (childSqlCall.getOperator() == SqlStdOperatorTable.NOT) {
-          SqlNode[] notOperands = childSqlCall.getOperands();
-          assert notOperands.length == 1;
-          return pushDownNotForIn(notOperands[0]);
-        } else if (childSqlCall.getOperator() instanceof SqlInOperator) {
-          SqlNode[] inOperands = childSqlCall.getOperands();
-          SqlInOperator inOp =
-              (SqlInOperator) childSqlCall.getOperator();
-          if (inOp.isNotIn()) {
-            return SqlStdOperatorTable.IN.createCall(
-                SqlParserPos.ZERO,
-                inOperands[0],
-                inOperands[1]);
-          } else {
-            return SqlStdOperatorTable.NOT_IN.createCall(
-                SqlParserPos.ZERO,
-                inOperands[0],
-                inOperands[1]);
-          }
-        } else {
-          // childSqlCall is "leaf" node in a logical expression tree
-          // (only considering AND, OR, NOT)
-          return sqlNode;
-        }
-      } else {
-        // sqlNode is "leaf" node in a logical expression tree
-        // (only considering AND, OR, NOT)
-        return sqlNode;
-      }
-    } else {
-      // tree rooted at sqlNode does not contain inOperator
-      return sqlNode;
-    }
-  }
-
-  /**
-   * Converts a WHERE clause.
-   *
-   * @param bb    Blackboard
-   * @param where WHERE clause, may be null
-   */
-  private void convertWhere(
-      final Blackboard bb,
-      final SqlNode where) {
-    if (where == null) {
-      return;
-    }
-    SqlNode newWhere = pushDownNotForIn(where);
-    replaceSubqueries(bb, newWhere, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
-    final RexNode convertedWhere = bb.convertExpression(newWhere);
-
-    // only allocate filter if the condition is not TRUE
-    if (!convertedWhere.isAlwaysTrue()) {
-      bb.setRoot(
-          RelOptUtil.createFilter(bb.root, convertedWhere),
-          false);
-    }
-  }
-
-  private void replaceSubqueries(
-      final Blackboard bb,
-      final SqlNode expr,
-      RelOptUtil.Logic logic) {
-    findSubqueries(bb, expr, logic, false);
-    for (SubQuery node : bb.subqueryList) {
-      substituteSubquery(bb, node);
-    }
-  }
-
-  private void substituteSubquery(Blackboard bb, SubQuery subQuery) {
-    final RexNode expr = subQuery.expr;
-    if (expr != null) {
-      // Already done.
-      return;
-    }
-
-    final SqlBasicCall call;
-    final RelNode rel;
-    final SqlNode query;
-    final Pair<RelNode, Boolean> converted;
-    switch (subQuery.node.getKind()) {
-    case CURSOR:
-      convertCursor(bb, subQuery);
-      return;
-
-    case MULTISET_QUERY_CONSTRUCTOR:
-    case MULTISET_VALUE_CONSTRUCTOR:
-      rel = convertMultisets(ImmutableList.of(subQuery.node), bb);
-      subQuery.expr = bb.register(rel, JoinRelType.INNER);
-      return;
-
-    case IN:
-      call = (SqlBasicCall) subQuery.node;
-      final SqlNode[] operands = call.getOperands();
-
-      SqlNode leftKeyNode = operands[0];
-      query = operands[1];
-
-      final List<RexNode> leftKeys;
-      switch (leftKeyNode.getKind()) {
-      case ROW:
-        leftKeys = Lists.newArrayList();
-        for (SqlNode sqlExpr : ((SqlBasicCall) leftKeyNode).getOperandList()) {
-          leftKeys.add(bb.convertExpression(sqlExpr));
-        }
-        break;
-      default:
-        leftKeys = ImmutableList.of(bb.convertExpression(leftKeyNode));
-      }
-
-      final boolean isNotIn = ((SqlInOperator) call.getOperator()).isNotIn();
-      if (query instanceof SqlNodeList) {
-        SqlNodeList valueList = (SqlNodeList) query;
-        if (!containsNullLiteral(valueList)
-            && valueList.size() < getInSubqueryThreshold()) {
-          // We're under the threshold, so convert to OR.
-          subQuery.expr =
-              convertInToOr(
-                  bb,
-                  leftKeys,
-                  valueList,
-                  isNotIn);
-          return;
-        }
-
-        // Otherwise, let convertExists translate
-        // values list into an inline table for the
-        // reference to Q below.
-      }
-
-      // Project out the search columns from the left side
-
-      //  Q1:
-      // "select from emp where emp.deptno in (select col1 from T)"
-      //
-      // is converted to
-      //
-      // "select from
-      //   emp inner join (select distinct col1 from T)) q
-      //   on emp.deptno = q.col1
-      //
-      // Q2:
-      // "select from emp where emp.deptno not in (Q)"
-      //
-      // is converted to
-      //
-      // "select from
-      //   emp left outer join (select distinct col1, TRUE from T) q
-      //   on emp.deptno = q.col1
-      //   where emp.deptno <> null
-      //         and q.indicator <> TRUE"
-      //
-      final boolean outerJoin = bb.subqueryNeedsOuterJoin
-          || isNotIn
-          || subQuery.logic == RelOptUtil.Logic.TRUE_FALSE_UNKNOWN;
-      converted =
-          convertExists(query, RelOptUtil.SubqueryType.IN, subQuery.logic,
-              outerJoin);
-      if (converted.right) {
-        // Generate
-        //    emp CROSS JOIN (SELECT COUNT(*) AS c,
-        //                       COUNT(deptno) AS ck FROM dept)
-        final RelDataType longType =
-            typeFactory.createSqlType(SqlTypeName.BIGINT);
-        final RelNode seek = converted.left.getInput(0); // fragile
-        final int keyCount = leftKeys.size();
-        final List<Integer> args = ImmutableIntList.range(0, keyCount);
-        AggregateRel aggregate =
-            new AggregateRel(cluster, seek, BitSets.of(),
-                ImmutableList.of(
-                    new AggregateCall(SqlStdOperatorTable.COUNT, false,
-                        ImmutableList.<Integer>of(), longType, null),
-                    new AggregateCall(SqlStdOperatorTable.COUNT, false,
-                        args, longType, null)));
-        JoinRel join =
-            new JoinRel(cluster, bb.root, aggregate,
-                rexBuilder.makeLiteral(true), JoinRelType.INNER,
-                ImmutableSet.<String>of());
-        bb.setRoot(join, false);
-      }
-      RexNode rex =
-          bb.register(converted.left,
-              outerJoin ? JoinRelType.LEFT : JoinRelType.INNER, leftKeys);
-
-      subQuery.expr = translateIn(subQuery, bb.root, rex);
-      if (isNotIn) {
-        subQuery.expr =
-            rexBuilder.makeCall(SqlStdOperatorTable.NOT, subQuery.expr);
-      }
-      return;
-
-    case EXISTS:
-      // "select from emp where exists (select a from T)"
-      //
-      // is converted to the following if the subquery is correlated:
-      //
-      // "select from emp left outer join (select AGG_TRUE() as indicator
-      // from T group by corr_var) q where q.indicator is true"
-      //
-      // If there is no correlation, the expression is replaced with a
-      // boolean indicating whether the subquery returned 0 or >= 1 row.
-      call = (SqlBasicCall) subQuery.node;
-      query = call.getOperands()[0];
-      converted = convertExists(query, RelOptUtil.SubqueryType.EXISTS,
-          subQuery.logic, true);
-      assert !converted.right;
-      if (convertNonCorrelatedSubQuery(subQuery, bb, converted.left, true)) {
-        return;
-      }
-      subQuery.expr = bb.register(converted.left, JoinRelType.LEFT);
-      return;
-
-    case SCALAR_QUERY:
-      // Convert the subquery.  If it's non-correlated, convert it
-      // to a constant expression.
-      call = (SqlBasicCall) subQuery.node;
-      query = call.getOperands()[0];
-      converted = convertExists(query, RelOptUtil.SubqueryType.SCALAR,
-          subQuery.logic, true);
-      assert !converted.right;
-      if (convertNonCorrelatedSubQuery(subQuery, bb, converted.left, false)) {
-        return;
-      }
-      rel = convertToSingleValueSubq(query, converted.left);
-      subQuery.expr = bb.register(rel, JoinRelType.LEFT);
-      return;
-
-    case SELECT:
-      // This is used when converting multiset queries:
-      //
-      // select * from unnest(select multiset[deptno] from emps);
-      //
-      converted = convertExists(subQuery.node, RelOptUtil.SubqueryType.SCALAR,
-          subQuery.logic, true);
-      assert !converted.right;
-      subQuery.expr = bb.register(converted.left, JoinRelType.LEFT);
-      return;
-
-    default:
-      throw Util.newInternal("unexpected kind of subquery :" + subQuery.node);
-    }
-  }
-
-  private RexNode translateIn(SubQuery subQuery, RelNode root,
-      final RexNode rex) {
-    switch (subQuery.logic) {
-    case TRUE:
-      return rexBuilder.makeLiteral(true);
-
-    case UNKNOWN_AS_FALSE:
-      assert rex instanceof RexRangeRef;
-      final int fieldCount = rex.getType().getFieldCount();
-      RexNode rexNode = rexBuilder.makeFieldAccess(rex, fieldCount - 1);
-      rexNode = rexBuilder.makeCall(SqlStdOperatorTable.IS_TRUE, rexNode);
-
-      // Then append the IS NOT NULL(leftKeysForIn).
-      //
-      // RexRangeRef contains the following fields:
-      //   leftKeysForIn,
-      //   rightKeysForIn (the original subquery select list),
-      //   nullIndicator
-      //
-      // The first two lists contain the same number of fields.
-      final int k = (fieldCount - 1) / 2;
-      for (int i = 0; i < k; i++) {
-        rexNode =
-            rexBuilder.makeCall(
-                SqlStdOperatorTable.AND,
-                rexNode,
-                rexBuilder.makeCall(
-                    SqlStdOperatorTable.IS_NOT_NULL,
-                    rexBuilder.makeFieldAccess(rex, i)));
-      }
-      return rexNode;
-
-    case TRUE_FALSE_UNKNOWN:
-    case UNKNOWN_AS_TRUE:
-      // select e.deptno,
-      //   case
-      //   when ct.c = 0 then false
-      //   when dt.i is not null then true
-      //   when e.deptno is null then null
-      //   when ct.ck < ct.c then null
-      //   else false
-      //   end
-      // from e
-      // cross join (select count(*) as c, count(deptno) as ck from v) as ct
-      // left join (select distinct deptno, true as i from v) as dt
-      //   on e.deptno = dt.deptno
-      final JoinRelBase join = (JoinRelBase) root;
-      final ProjectRelBase left = (ProjectRelBase) join.getLeft();
-      final RelNode leftLeft = ((JoinRelBase) left.getInput(0)).getLeft();
-      final int leftLeftCount = leftLeft.getRowType().getFieldCount();
-      final RelDataType nullableBooleanType =
-          typeFactory.createTypeWithNullability(
-              typeFactory.createSqlType(SqlTypeName.BOOLEAN), true);
-      final RelDataType longType =
-          typeFactory.createSqlType(SqlTypeName.BIGINT);
-      final RexNode cRef = rexBuilder.makeInputRef(root, leftLeftCount);
-      final RexNode ckRef = rexBuilder.makeInputRef(root, leftLeftCount + 1);
-      final RexNode iRef =
-          rexBuilder.makeInputRef(root, root.getRowType().getFieldCount() - 1);
-
-      final RexLiteral zero =
-          rexBuilder.makeExactLiteral(BigDecimal.ZERO, longType);
-      final RexLiteral trueLiteral = rexBuilder.makeLiteral(true);
-      final RexLiteral falseLiteral = rexBuilder.makeLiteral(false);
-      final RexNode unknownLiteral =
-          rexBuilder.makeNullLiteral(SqlTypeName.BOOLEAN);
-
-      final ImmutableList.Builder<RexNode> args = ImmutableList.builder();
-      args.add(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, cRef, zero),
-          falseLiteral,
-          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, iRef),
-          trueLiteral);
-      final JoinInfo joinInfo = join.analyzeCondition();
-      for (int leftKey : joinInfo.leftKeys) {
-        final RexNode kRef = rexBuilder.makeInputRef(root, leftKey);
-        args.add(rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, kRef),
-            unknownLiteral);
-      }
-      args.add(rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN, ckRef, cRef),
-          unknownLiteral,
-          falseLiteral);
-
-      return rexBuilder.makeCall(
-          nullableBooleanType,
-          SqlStdOperatorTable.CASE,
-          args.build());
-
-    default:
-      throw new AssertionError(subQuery.logic);
-    }
-  }
-
-  private static boolean containsNullLiteral(SqlNodeList valueList) {
-    for (SqlNode node : valueList.getList()) {
-      if (node instanceof SqlLiteral) {
-        SqlLiteral lit = (SqlLiteral) node;
-        if (lit.getValue() == null) {
-          return true;
-        }
-      }
-    }
-    return false;
-  }
-
-  /**
-   * Determines if a subquery is non-correlated and if so, converts it to a
-   * constant.
-   *
-   * @param subQuery  the call that references the subquery
-   * @param bb        blackboard used to convert the subquery
-   * @param converted RelNode tree corresponding to the subquery
-   * @param isExists  true if the subquery is part of an EXISTS expression
-   * @return if the subquery can be converted to a constant
-   */
-  private boolean convertNonCorrelatedSubQuery(
-      SubQuery subQuery,
-      Blackboard bb,
-      RelNode converted,
-      boolean isExists) {
-    SqlCall call = (SqlBasicCall) subQuery.node;
-    if (subqueryConverter.canConvertSubquery()
-        && isSubqNonCorrelated(converted, bb)) {
-      // First check if the subquery has already been converted
-      // because it's a nested subquery.  If so, don't re-evaluate
-      // it again.
-      RexNode constExpr = mapConvertedNonCorrSubqs.get(call);
-      if (constExpr == null) {
-        constExpr =
-            subqueryConverter.convertSubquery(
-                call,
-                this,
-                isExists,
-                isExplain);
-      }
-      if (constExpr != null) {
-        subQuery.expr = constExpr;
-        mapConvertedNonCorrSubqs.put(call, constExpr);
-        return true;
-      }
-    }
-    return false;
-  }
-
-  /**
-   * Converts the RelNode tree for a select statement to a select that
-   * produces a single value.
-   *
-   * @param query the query
-   * @param plan   the original RelNode tree corresponding to the statement
-   * @return the converted RelNode tree
-   */
-  public RelNode convertToSingleValueSubq(
-      SqlNode query,
-      RelNode plan) {
-    // Check whether query is guaranteed to produce a single value.
-    if (query instanceof SqlSelect) {
-      SqlSelect select = (SqlSelect) query;
-      SqlNodeList selectList = select.getSelectList();
-      SqlNodeList groupList = select.getGroup();
-
-      if ((selectList.size() == 1)
-          && ((groupList == null) || (groupList.size() == 0))) {
-        SqlNode selectExpr = selectList.get(0);
-        if (selectExpr instanceof SqlCall) {
-          SqlCall selectExprCall = (SqlCall) selectExpr;
-          if (selectExprCall.getOperator()
-              instanceof SqlAggFunction) {
-            return plan;
-          }
-        }
-      }
-    }
-
-    // If not, project SingleValueAgg
-    return RelOptUtil.createSingleValueAggRel(
-        cluster,
-        plan);
-  }
-
-  /**
-   * Converts "x IN (1, 2, ...)" to "x=1 OR x=2 OR ...".
-   *
-   * @param leftKeys   LHS
-   * @param valuesList RHS
-   * @param isNotIn    is this a NOT IN operator
-   * @return converted expression
-   */
-  private RexNode convertInToOr(
-      final Blackboard bb,
-      final List<RexNode> leftKeys,
-      SqlNodeList valuesList,
-      boolean isNotIn) {
-    List<RexNode> comparisons = new ArrayList<RexNode>();
-    for (SqlNode rightVals : valuesList) {
-      RexNode rexComparison;
-      if (leftKeys.size() == 1) {
-        rexComparison =
-            rexBuilder.makeCall(
-                SqlStdOperatorTable.EQUALS,
-                leftKeys.get(0),
-                bb.convertExpression(rightVals));
-      } else {
-        assert rightVals instanceof SqlCall;
-        final SqlBasicCall call = (SqlBasicCall) rightVals;
-        assert (call.getOperator() instanceof SqlRowOperator)
-            && call.getOperands().length == leftKeys.size();
-        rexComparison =
-            RexUtil.composeConjunction(
-                rexBuilder,
-                Iterables.transform(
-                    Pair.zip(leftKeys, call.getOperandList()),
-                    new Function<Pair<RexNode, SqlNode>, RexNode>() {
-                      public RexNode apply(Pair<RexNode, SqlNode> pair) {
-                        return rexBuilder.makeCall(SqlStdOperatorTable.EQUALS,
-                            pair.left, bb.convertExpression(pair.right));
-                      }
-                    }),
-                false);
-      }
-      comparisons.add(rexComparison);
-    }
-
-    RexNode result =
-        RexUtil.composeDisjunction(rexBuilder, comparisons, true);
-    assert result != null;
-
-    if (isNotIn) {
-      result =
-          rexBuilder.makeCall(
-              SqlStdOperatorTable.NOT,
-              result);
-    }
-
-    return result;
-  }
-
-  /**
-   * Gets the list size threshold under which {@link #convertInToOr} is used.
-   * Lists of this size or greater will instead be converted to use a join
-   * against an inline table ({@link ValuesRel}) rather than a predicate. A
-   * threshold of 0 forces usage of an inline table in all cases; a threshold
-   * of Integer.MAX_VALUE forces usage of OR in all cases
-   *
-   * @return threshold, default 20
-   */
-  protected int getInSubqueryThreshold() {
-    // OVERRIDE POINT
-    return Integer.MAX_VALUE; // was 20
-  }
-
-  /**
-   * Converts an EXISTS or IN predicate into a join. For EXISTS, the subquery
-   * produces an indicator variable, and the result is a relational expression
-   * which outer joins that indicator to the original query. After performing
-   * the outer join, the condition will be TRUE if the EXISTS condition holds,
-   * NULL otherwise.
-   *
-   * @param seek           A query, for example 'select * from emp' or
-   *                       'values (1,2,3)' or '('Foo', 34)'.
-   * @param subqueryType   Whether sub-query is IN, EXISTS or scalar
-   * @param logic Whether the answer needs to be in full 3-valued logic (TRUE,
-   *     FALSE, UNKNOWN) will be required, or whether we can accept an
-   *     approximation (say representing UNKNOWN as FALSE)
-   * @param needsOuterJoin Whether an outer join is needed
-   * @return join expression
-   * @pre extraExpr == null || extraName != null
-   */
-  private Pair<RelNode, Boolean> convertExists(
-      SqlNode seek,
-      RelOptUtil.SubqueryType subqueryType,
-      RelOptUtil.Logic logic,
-      boolean needsOuterJoin) {
-    final SqlValidatorScope seekScope =
-        (seek instanceof SqlSelect)
-            ? validator.getSelectScope((SqlSelect) seek)
-            : null;
-    final Blackboard seekBb = createBlackboard(seekScope, null);
-    RelNode seekRel = convertQueryOrInList(seekBb, seek);
-
-    return RelOptUtil.createExistsPlan(seekRel, subqueryType, logic,
-        needsOuterJoin);
-  }
-
-  private RelNode convertQueryOrInList(
-      Blackboard bb,
-      SqlNode seek) {
-    // NOTE: Once we start accepting single-row queries as row constructors,
-    // there will be an ambiguity here for a case like X IN ((SELECT Y FROM
-    // Z)).  The SQL standard resolves the ambiguity by saying that a lone
-    // select should be interpreted as a table expression, not a row
-    // expression.  The semantic difference is that a table expression can
-    // return multiple rows.
-    if (seek instanceof SqlNodeList) {
-      return convertRowValues(
-          bb,
-          seek,
-          ((SqlNodeList) seek).getList(),
-          false,
-          null);
-    } else {
-      return convertQueryRecursive(seek, false, null);
-    }
-  }
-
-  private RelNode convertRowValues(
-      Blackboard bb,
-      SqlNode rowList,
-      Collection<SqlNode> rows,
-      boolean allowLiteralsOnly,
-      RelDataType targetRowType) {
-    // NOTE jvs 30-Apr-2006: We combine all rows consisting entirely of
-    // literals into a single ValuesRel; this gives the optimizer a smaller
-    // input tree.  For everything else (computed expressions, row
-    // subqueries), we union each row in as a projection on top of a
-    // OneRowRel.
-
-    final List<List<RexLiteral>> tupleList =
-        new ArrayList<List<RexLiteral>>();
-    final RelDataType rowType;
-    if (targetRowType != null) {
-      rowType = targetRowType;
-    } else {
-      rowType =
-          SqlTypeUtil.promoteToRowType(
-              typeFactory,
-              validator.getValidatedNodeType(rowList),
-              null);
-    }
-
-    List<RelNode> unionInputs = new ArrayList<RelNode>();
-    for (SqlNode node : rows) {
-      SqlBasicCall call;
-      if (isRowConstructor(node)) {
-        call = (SqlBasicCall) node;
-        List<RexLiteral> tuple = new ArrayList<RexLiteral>();
-        for (SqlNode operand : call.operands) {
-          RexLiteral rexLiteral =
-              convertLiteralInValuesList(
-                  operand,
-                  bb,
-                  rowType,
-                  tuple.size());
-          if ((rexLiteral == null) && allowLiteralsOnly) {
-            return null;
-          }
-          if ((rexLiteral == null) || !shouldCreateValuesRel) {
-            // fallback to convertRowConstructor
-            tuple = null;
-            break;
-          }
-          tuple.add(rexLiteral);
-        }
-        if (tuple != null) {
-          tupleList.add(tuple);
-          continue;
-        }
-      } else {
-        RexLiteral rexLiteral =
-            convertLiteralInValuesList(
-                node,
-                bb,
-                rowType,
-                0);
-        if ((rexLiteral != null) && shouldCreateValuesRel) {
-          tupleList.add(
-              Collections.singletonList(rexLiteral));
-          continue;
-        } else {
-          if ((rexLiteral == null) && allowLiteralsOnly) {
-            return null;
-          }
-        }
-
-        // convert "1" to "row(1)"
-        call =
-            (SqlBasicCall) SqlStdOperatorTable.ROW.createCall(
-                SqlParserPos.ZERO,
-                node);
-      }
-      unionInputs.add(convertRowConstructor(bb, call));
-    }
-    ValuesRel valuesRel =
-        new ValuesRel(
-            cluster,
-            rowType,
-            tupleList);
-    RelNode resultRel;
-    if (unionInputs.isEmpty()) {
-      resultRel = valuesRel;
-    } else {
-      if (!tupleList.isEmpty()) {
-        unionInputs.add(valuesRel);
-      }
-      UnionRel unionRel =
-          new UnionRel(
-              cluster,
-              unionInputs,
-              true);
-      resultRel = unionRel;
-    }
-    leaves.add(resultRel);
-    return resultRel;
-  }
-
-  private RexLiteral convertLiteralInValuesList(
-      SqlNode sqlNode,
-      Blackboard bb,
-      RelDataType rowType,
-      int iField) {
-    if (!(sqlNode instanceof SqlLiteral)) {
-      return null;
-    }
-    RelDataTypeField field = rowType.getFieldList().get(iField);
-    RelDataType type = field.getType();
-    if (type.isStruct()) {
-      // null literals for weird stuff like UDT's need
-      // special handling during type flattening, so
-      // don't use ValuesRel for those
-      return null;
-    }
-
-    RexNode literalExpr =
-        exprConverter.convertLiteral(
-            bb,
-            (SqlLiteral) sqlNode);
-
-    if (!(literalExpr instanceof RexLiteral)) {
-      assert literalExpr.isA(SqlKind.CAST);
-      RexNode child = ((RexCall) literalExpr).getOperands().get(0);
-      assert RexLiteral.isNullLiteral(child);
-
-      // NOTE jvs 22-Nov-2006:  we preserve type info
-      // in ValuesRel digest, so it's OK to lose it here
-      return (RexLiteral) child;
-    }
-
-    RexLiteral literal = (RexLiteral) literalExpr;
-
-    Comparable value = literal.getValue();
-
-    if (SqlTypeUtil.isExactNumeric(type)) {
-      BigDecimal roundedValue =
-          NumberUtil.rescaleBigDecimal(
-              (BigDecimal) value,
-              type.getScale());
-      return rexBuilder.makeExactLiteral(
-          roundedValue,
-          type);
-    }
-
-    if ((value instanceof NlsString)
-        && (type.getSqlTypeName() == SqlTypeName.CHAR)) {
-      // pad fixed character type
-      NlsString unpadded = (NlsString) value;
-      return rexBuilder.makeCharLiteral(
-          new NlsString(
-              Util.rpad(unpadded.getValue(), type.getPrecision()),
-              unpadded.getCharsetName(),
-              unpadded.getCollation()));
-    }
-    return literal;
-  }
-
-  private boolean isRowConstructor(SqlNode node) {
-    if (!(node.getKind() == SqlKind.ROW)) {
-      return false;
-    }
-    SqlCall call = (SqlCall) node;
-    return call.getOperator().getName().equalsIgnoreCase("row");
-  }
-
-  /**
-   * Builds a list of all <code>IN</code> or <code>EXISTS</code> operators
-   * inside SQL parse tree. Does not traverse inside queries.
-   *
-   * @param bb                           blackboard
-   * @param node                         the SQL parse tree
-   * @param logic Whether the answer needs to be in full 3-valued logic (TRUE,
-   *              FALSE, UNKNOWN) will be required, or whether we can accept
-   *              an approximation (say representing UNKNOWN as FALSE)
-   * @param registerOnlyScalarSubqueries if set to true and the parse tree
-   *                                     corresponds to a variation of a select
-   *                                     node, only register it if it's a scalar
-   *                                     subquery
-   */
-  private void findSubqueries(
-      Blackboard bb,
-      SqlNode node,
-      RelOptUtil.Logic logic,
-      boolean registerOnlyScalarSubqueries) {
-    final SqlKind kind = node.getKind();
-    switch (kind) {
-    case EXISTS:
-    case SELECT:
-    case MULTISET_QUERY_CONSTRUCTOR:
-    case MULTISET_VALUE_CONSTRUCTOR:
-    case CURSOR:
-    case SCALAR_QUERY:
-      if (!registerOnlyScalarSubqueries
-          || (kind == SqlKind.SCALAR_QUERY)) {
-        bb.registerSubquery(node, RelOptUtil.Logic.TRUE_FALSE);
-      }
-      return;
-    case IN:
-      if (((SqlCall) node).getOperator() == SqlStdOperatorTable.NOT_IN) {
-        logic = logic.negate();
-      }
-      break;
-    case NOT:
-      logic = logic.negate();
-      break;
-    }
-    if (node instanceof SqlCall) {
-      if (kind == SqlKind.OR
-          || kind == SqlKind.NOT) {
-        // It's always correct to outer join subquery with
-        // containing query; however, when predicates involve Or
-        // or NOT, outer join might be necessary.
-        bb.subqueryNeedsOuterJoin = true;
-      }
-      for (SqlNode operand : ((SqlCall) node).getOperandList()) {
-        if (operand != null) {
-          // In the case of an IN expression, locate scalar
-          // subqueries so we can convert them to constants
-          findSubqueries(
-              bb,
-              operand,
-              logic,
-              kind == SqlKind.IN || registerOnlyScalarSubqueries);
-        }
-      }
-    } else if (node instanceof SqlNodeList) {
-      for (SqlNode child : (SqlNodeList) node) {
-        findSubqueries(
-            bb,
-            child,
-            logic,
-            kind == SqlKind.IN || registerOnlyScalarSubqueries);
-      }
-    }
-
-    // Now that we've located any scalar subqueries inside the IN
-    // expression, register the IN expression itself.  We need to
-    // register the scalar subqueries first so they can be converted
-    // before the IN expression is converted.
-    if (kind == SqlKind.IN) {
-      if (logic == RelOptUtil.Logic.TRUE_FALSE_UNKNOWN
-          && !validator.getValidatedNodeType(node).isNullable()) {
-        logic = RelOptUtil.Logic.UNKNOWN_AS_FALSE;
-      }
-      // TODO: This conversion is only valid in the WHERE clause
-      if (logic == RelOptUtil.Logic.UNKNOWN_AS_FALSE
-          && !bb.subqueryNeedsOuterJoin) {
-        logic = RelOptUtil.Logic.TRUE;
-      }
-      bb.registerSubquery(node, logic);
-    }
-  }
-
-  /**
-   * Converts an expression from {@link SqlNode} to {@link RexNode} format.
-   *
-   * @param node Expression to translate
-   * @return Converted expression
-   */
-  public RexNode convertExpression(
-      SqlNode node) {
-    Map<String, RelDataType> nameToTypeMap = Collections.emptyMap();
-    Blackboard bb =
-        createBlackboard(
-            new ParameterScope((SqlValidatorImpl) validator, nameToTypeMap),
-            null);
-    return bb.convertExpression(node);
-  }
-
-  /**
-   * Converts an expression from {@link SqlNode} to {@link RexNode} format,
-   * mapping identifier references to predefined expressions.
-   *
-   * @param node          Expression to translate
-   * @param nameToNodeMap map from String to {@link RexNode}; when an
-   *                      {@link SqlIdentifier} is encountered, it is used as a
-   *                      key and translated to the corresponding value from
-   *                      this map
-   * @return Converted expression
-   */
-  public RexNode convertExpression(
-      SqlNode node,
-      Map<String, RexNode> nameToNodeMap) {
-    final Map<String, RelDataType> nameToTypeMap =
-        new HashMap<String, RelDataType>();
-    for (Map.Entry<String, RexNode> entry : nameToNodeMap.entrySet()) {
-      nameToTypeMap.put(entry.getKey(), entry.getValue().getType());
-    }
-    Blackboard bb =
-        createBlackboard(
-            new ParameterScope((SqlValidatorImpl) validator, nameToTypeMap),
-            nameToNodeMap);
-    return bb.convertExpression(node);
-  }
-
-  /**
-   * Converts a non-standard expression.
-   *
-   * <p>This method is an extension-point that derived classes can override. If
-   * this method returns a null result, the normal expression translation
-   * process will proceed. The default implementation always returns null.
-   *
-   * @param node Expression
-   * @param bb   Blackboard
-   * @return null to proceed with the usual expression translation process
-   */
-  protected RexNode convertExtendedExpression(
-      SqlNode node,
-      Blackboard bb) {
-    return null;
-  }
-
-  private RexNode convertOver(Blackboard bb, SqlNode node) {
-    SqlCall call = (SqlCall) node;
-    SqlCall aggCall = call.operand(0);
-    SqlNode windowOrRef = call.operand(1);
-    final SqlWindow window =
-        validator.resolveWindow(windowOrRef, bb.scope, true);
-    final SqlNodeList partitionList = window.getPartitionList();
-    final ImmutableList.Builder<RexNode> partitionKeys =
-        ImmutableList.builder();
-    for (SqlNode partition : partitionList) {
-      partitionKeys.add(bb.convertExpression(partition));
-    }
-    RexNode lowerBound = bb.convertExpression(window.getLowerBound());
-    RexNode upperBound = bb.convertExpression(window.getUpperBound());
-    SqlNodeList orderList = window.getOrderList();
-    if ((orderList.size() == 0) && !window.isRows()) {
-      // A logical range requires an ORDER BY clause. Use the implicit
-      // ordering of this relation. There must be one, otherwise it would
-      // have failed validation.
-      orderList = bb.scope.getOrderList();
-      if (orderList == null) {
-        throw new AssertionError(
-            "Relation should have sort key for implicit ORDER BY");
-      }
-    }
-    final ImmutableList.Builder<RexFieldCollation> orderKeys =
-        ImmutableList.builder();
-    final Set<SqlKind> flags = EnumSet.noneOf(SqlKind.class);
-    for (SqlNode order : orderList) {
-      flags.clear();
-      RexNode e = bb.convertSortExpression(order, flags);
-      orderKeys.add(new RexFieldCollation(e, flags));
-    }
-    try {
-      Util.permAssert(bb.window == null, "already in window agg mode");
-      bb.window = window;
-      RexNode rexAgg = exprConverter.convertCall(bb, aggCall);
-      rexAgg =
-          rexBuilder.ensureType(
-              validator.getValidatedNodeType(call), rexAgg, false);
-
-      // Walk over the tree and apply 'over' to all agg functions. This is
-      // necessary because the returned expression is not necessarily a call
-      // to an agg function. For example, AVG(x) becomes SUM(x) / COUNT(x).
-      final RexShuttle visitor =
-          new HistogramShuttle(
-              partitionKeys.build(), orderKeys.build(),
-              RexWindowBound.create(window.getLowerBound(), lowerBound),
-              RexWindowBound.create(window.getUpperBound(), upperBound),
-              window);
-      return rexAgg.accept(visitor);
-    } finally {
-      bb.window = null;
-    }
-  }
-
-  /**
-   * Converts a FROM clause into a relational expression.
-   *
-   * @param bb   Scope within which to resolve identifiers
-   * @param from FROM clause of a query. Examples include:
-   *
-   *             <ul>
-   *             <li>a single table ("SALES.EMP"),
-   *             <li>an aliased table ("EMP AS E"),
-   *             <li>a list of tables ("EMP, DEPT"),
-   *             <li>an ANSI Join expression ("EMP JOIN DEPT ON EMP.DEPTNO =
-   *             DEPT.DEPTNO"),
-   *             <li>a VALUES clause ("VALUES ('Fred', 20)"),
-   *             <li>a query ("(SELECT * FROM EMP WHERE GENDER = 'F')"),
-   *             <li>or any combination of the above.
-   *             </ul>
-   */
-  protected void convertFrom(
-      Blackboard bb,
-      SqlNode from) {
-    SqlCall call;
-    final SqlNode[] operands;
-    switch (from.getKind()) {
-    case AS:
-      operands = ((SqlBasicCall) from).getOperands();
-      convertFrom(bb, operands[0]);
-      return;
-
-    case WITH_ITEM:
-      convertFrom(bb, ((SqlWithItem) from).query);
-      return;
-
-    case WITH:
-      convertFrom(bb, ((SqlWith) from).body);
-      return;
-
-    case TABLESAMPLE:
-      operands = ((SqlBasicCall) from).getOperands();
-      SqlSampleSpec sampleSpec = SqlLiteral.sampleValue(operands[1]);
-      if (sampleSpec instanceof SqlSampleSpec.SqlSubstitutionSampleSpec) {
-        String sampleName =
-            ((SqlSampleSpec.SqlSubstitutionSampleSpec) sampleSpec)
-                .getName();
-        datasetStack.push(sampleName);
-        convertFrom(bb, operands[0]);
-        datasetStack.pop();
-      } else if (sampleSpec instanceof SqlSampleSpec.SqlTableSampleSpec) {
-        SqlSampleSpec.SqlTableSampleSpec tableSampleSpec =
-            (SqlSampleSpec.SqlTableSampleSpec) sampleSpec;
-        convertFrom(bb, operands[0]);
-        RelOptSamplingParameters params =
-            new RelOptSamplingParameters(
-                tableSampleSpec.isBernoulli(),
-                tableSampleSpec.getSamplePercentage(),
-                tableSampleSpec.isRepeatable(),
-                tableSampleSpec.getRepeatableSeed());
-        bb.setRoot(new SamplingRel(cluster, bb.root, params), false);
-      } else {
-        throw Util.newInternal(
-            "unknown TABLESAMPLE type: " + sampleSpec);
-      }
-      return;
-
-    case IDENTIFIER:
-      final SqlValidatorNamespace fromNamespace =
-          validator.getNamespace(from).resolve();
-      if (fromNamespace.getNode() != null) {
-        convertFrom(bb, fromNamespace.getNode());
-        return;
-      }
-      final String datasetName =
-          datasetStack.isEmpty() ? null : datasetStack.peek();
-      boolean[] usedDataset = {false};
-      RelOptTable table =
-          SqlValidatorUtil.getRelOptTable(
-              fromNamespace,
-              catalogReader,
-              datasetName,
-              usedDataset);
-      final RelNode tableRel;
-      if (shouldConvertTableAccess) {
-        tableRel = toRel(table);
-      } else {
-        tableRel = new TableAccessRel(cluster, table);
-      }
-      bb.setRoot(tableRel, true);
-      if (usedDataset[0]) {
-        bb.setDataset(datasetName);
-      }
-      return;
-
-    case JOIN:
-      final SqlJoin join = (SqlJoin) from;
-      final Blackboard fromBlackboard =
-          createBlackboard(validator.getJoinScope(from), null);
-      SqlNode left = join.getLeft();
-      SqlNode right = join.getRight();
-      final boolean isNatural = join.isNatural();
-      final JoinType joinType = join.getJoinType();
-      final Blackboard leftBlackboard =
-          createBlackboard(
-              Util.first(validator.getJoinScope(left),
-                  ((DelegatingScope) bb.scope).getParent()), null);
-      final Blackboard rightBlackboard =
-          createBlackboard(
-              Util.first(validator.getJoinScope(right),
-                  ((DelegatingScope) bb.scope).getParent()), null);
-      convertFrom(leftBlackboard, left);
-      RelNode leftRel = leftBlackboard.root;
-      convertFrom(rightBlackboard, right);
-      RelNode rightRel = rightBlackboard.root;
-      JoinRelType convertedJoinType = convertJoinType(joinType);
-      RexNode conditionExp;
-      if (isNatural) {
-        final List<String> columnList =
-            SqlValidatorUtil.deriveNaturalJoinColumnList(
-                validator.getNamespace(left).getRowType(),
-                validator.getNamespace(right).getRowType());
-        conditionExp = convertUsing(leftRel, rightRel, columnList);
-      } else {
-        conditionExp =
-            convertJoinCondition(
-                fromBlackboard,
-                join.getCondition(),
-                join.getConditionType(),
-                leftRel,
-                rightRel);
-      }
-
-      final RelNode joinRel =
-          createJoin(
-              fromBlackboard,
-              leftRel,
-              rightRel,
-              conditionExp,
-              convertedJoinType);
-      bb.setRoot(joinRel, false);
-      return;
-
-    case SELECT:
-    case INTERSECT:
-    case EXCEPT:
-    case UNION:
-      final RelNode rel = convertQueryRecursive(from, false, null);
-      bb.setRoot(rel, true);
-      return;
-
-    case VALUES:
-      convertValuesImpl(bb, (SqlCall) from, null);
-      return;
-
-    case UNNEST:
-      final SqlNode node = ((SqlCall) from).operand(0);
-      replaceSubqueries(bb, node, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
-      final RelNode childRel =
-          RelOptUtil.createProject(
-              (null != bb.root) ? bb.root : new OneRowRel(cluster),
-              Collections.singletonList(bb.convertExpression(node)),
-              Collections.singletonList(validator.deriveAlias(node, 0)),
-              true);
-
-      UncollectRel uncollectRel =
-          new UncollectRel(cluster, cluster.traitSetOf(Convention.NONE),
-              childRel);
-      bb.setRoot(uncollectRel, true);
-      return;
-
-    case COLLECTION_TABLE:
-      call = (SqlCall) from;
-
-      // Dig out real call; TABLE() wrapper is just syntactic.
-      assert call.getOperandList().size() == 1;
-      call = call.operand(0);
-      convertCollectionTable(bb, call);
-      return;
-
-    default:
-      throw Util.newInternal("not a join operator " + from);
-    }
-  }
-
-  protected void convertCollectionTable(
-      Blackboard bb,
-      SqlCall call) {
-    final SqlOperator operator = call.getOperator();
-    if (operator == SqlStdOperatorTable.TABLESAMPLE) {
-      final String sampleName =
-          SqlLiteral.stringValue(call.operand(0));
-      datasetStack.push(sampleName);
-      SqlCall cursorCall = call.operand(1);
-      SqlNode query = cursorCall.operand(0);
-      RelNode converted = convertQuery(query, false, false);
-      bb.setRoot(converted, false);
-      datasetStack.pop();
-      return;
-    }
-    replaceSubqueries(bb, call, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
-
-    // Expand table macro if possible. It's more efficient than
-    // TableFunctionRel.
-    if (operator instanceof SqlUserDefinedTableMacro) {
-      final SqlUserDefinedTableMacro udf =
-          (SqlUserDefinedTableMacro) operator;
-      final TranslatableTable table = udf.getTable(typeFactory,
-        call.getOperandList());
-      final RelDataType rowType = table.getRowType(typeFactory);
-      RelOptTable relOptTable = RelOptTableImpl.create(null, rowType, table);
-      RelNode converted = toRel(relOptTable);
-      bb.setRoot(converted, true);
-      return;
-    }
-
-    Type elementType;
-    if (operator instanceof SqlUserDefinedTableFunction) {
-      SqlUserDefinedTableFunction udtf = (SqlUserDefinedTableFunction) operator;
-      elementType = udtf.getElementType(typeFactory, call.getOperandList());
-    } else {
-      elementType = null;
-    }
-
-    RexNode rexCall = bb.convertExpression(call);
-    final List<RelNode> inputs = bb.retrieveCursors();
-    Set<RelColumnMapping> columnMappings =
-        getColumnMappings(operator);
-    TableFunctionRel callRel =
-        new TableFunctionRel(
-            cluster,
-            inputs,
-            rexCall,
-            elementType,
-            validator.getValidatedNodeType(call),
-            columnMappings);
-    bb.setRoot(callRel, true);
-    afterTableFunction(bb, call, callRel);
-  }
-
-  protected void afterTableFunction(
-      SqlToRelConverter.Blackboard bb,
-      SqlCall call,
-      TableFunctionRel callRel) {
-  }
-
-  private Set<RelColumnMapping> getColumnMappings(SqlOperator op) {
-    SqlReturnTypeInference rti = op.getReturnTypeInference();
-    if (rti == null) {
-      return null;
-    }
-    if (rti instanceof TableFunctionReturnTypeInference) {
-      TableFunctionReturnTypeInference tfrti =
-          (TableFunctionReturnTypeInference) rti;
-      return tfrti.getColumnMappings();
-    } else {
-      return null;
-    }
-  }
-
-  protected RelNode createJoin(
-      Blackboard bb,
-      RelNode leftRel,
-      RelNode rightRel,
-      RexNode joinCond,
-      JoinRelType joinType) {
-    assert joinCond != null;
-
-    Set<String> correlatedVariables = RelOptUtil.getVariablesUsed(rightRel);
-    if (correlatedVariables.size() > 0) {
-      final List<Correlation> correlations = Lists.newArrayList();
-
-      for (String correlName : correlatedVariables) {
-        DeferredLookup lookup = mapCorrelToDeferred.get(correlName);
-        RexFieldAccess fieldAccess = lookup.getFieldAccess(correlName);
-        String originalRelName = lookup.getOriginalRelName();
-        String originalFieldName = fieldAccess.getField().getName();
-
-        int[] nsIndexes = {-1};
-        final SqlValidatorScope[] ancestorScopes = {null};
-        SqlValidatorNamespace foundNs =
-            lookup.bb.scope.resolve(
-                originalRelName,
-                ancestorScopes,
-                nsIndexes);
-
-        assert foundNs != null;
-        assert nsIndexes.length == 1;
-
-        int childNamespaceIndex = nsIndexes[0];
-
-        SqlValidatorScope ancestorScope = ancestorScopes[0];
-        boolean correlInCurrentScope = ancestorScope == bb.scope;
-
-        if (correlInCurrentScope) {
-          int namespaceOffset = 0;
-          if (childNamespaceIndex > 0) {
-            // If not the first child, need to figure out the width
-            // of output types from all the preceding namespaces
-            assert ancestorScope instanceof ListScope;
-            List<SqlValidatorNamespace> children =
-                ((ListScope) ancestorScope).getChildren();
-
-            for (int i = 0; i < childNamespaceIndex; i++) {
-              SqlValidatorNamespace child = children.get(i);
-              namespaceOffset +=
-                  child.getRowType().getFieldCount();
-            }
-          }
-
-          RelDataTypeField field =
-              catalogReader.field(foundNs.getRowType(), originalFieldName);
-          int pos = namespaceOffset + field.getIndex();
-
-          assert field.getType()
-              == lookup.getFieldAccess(correlName).getField().getType();
-
-          assert pos != -1;
-
-          if (bb.mapRootRelToFieldProjection.containsKey(bb.root)) {
-            // bb.root is an aggregate and only projects group by
-            // keys.
-            Map<Integer, Integer> exprProjection =
-                bb.mapRootRelToFieldProjection.get(bb.root);
-
-            // subquery can reference group by keys projected from
-            // the root of the outer relation.
-            if (exprProjection.containsKey(pos)) {
-              pos = exprProjection.get(pos);
-            } else {
-              // correl not grouped
-              throw Util.newInternal(
-                  "Identifier '" + originalRelName + "."
-                  + originalFieldName + "' is not a group expr");
-            }
-          }
-
-          Correlation newCorVar =
-              new Correlation(
-                  getCorrelOrdinal(correlName),
-                  pos);
-
-          correlations.add(newCorVar);
-        }
-      }
-
-      if (!correlations.isEmpty()) {
-        return new CorrelatorRel(
-            rightRel.getCluster(),
-            leftRel,
-            rightRel,
-            joinCond,
-            correlations,
-            joinType);
-      }
-    }
-
-    final List<RexNode> extraLeftExprs = new ArrayList<RexNode>();
-    final List<RexNode> extraRightExprs = new ArrayList<RexNode>();
-    final int leftCount = leftRel.getRowType().getFieldCount();
-    final int rightCount = rightRel.getRowType().getFieldCount();
-    if (!containsGet(joinCond)) {
-      joinCond = pushDownJoinConditions(
-          joinCond, leftCount, rightCount, extraLeftExprs, extraRightExprs);
-    }
-    if (!extraLeftExprs.isEmpty()) {
-      final List<RelDataTypeField> fields =
-          leftRel.getRowType().getFieldList();
-      leftRel = RelOptUtil.createProject(
-          leftRel,
-          new AbstractList<Pair<RexNode, String>>() {
-            @Override
-            public int size() {
-              return leftCount + extraLeftExprs.size();
-            }
-
-            @Override
-            public Pair<RexNode, String> get(int index) {
-              if (index < leftCount) {
-                RelDataTypeField field = fields.get(index);
-                return Pair.<RexNode, String>of(
-                    new RexInputRef(index, field.getType()),
-                    field.getName());
-              } else {
-                return Pair.<RexNode, String>of(
-                    extraLeftExprs.get(index - leftCount), null);
-              }
-            }
-          },
-          true);
-    }
-    if (!extraRightExprs.isEmpty()) {
-      final List<RelDataTypeField> fields =
-          rightRel.getRowType().getFieldList();
-      final int newLeftCount = leftCount + extraLeftExprs.size();
-      rightRel = RelOptUtil.createProject(
-          rightRel,
-          new AbstractList<Pair<RexNode, String>>() {
-            @Override
-            public int size() {
-              return rightCount + extraRightExprs.size();
-            }
-
-            @Override
-            public Pair<RexNode, String> get(int index) {
-              if (index < rightCount) {
-                RelDataTypeField field = fields.get(index);
-                return Pair.<RexNode, String>of(
-                    new RexInputRef(index, field.getType()),
-                    field.getName());
-              } else {
-                return Pair.of(
-                    RexUtil.shift(
-                        extraRightExprs.get(index - rightCount),
-                        -newLeftCount),
-                    null);
-              }
-            }
-          },
-          true);
-    }
-    RelNode join = createJoin(
-        leftRel,
-        rightRel,
-        joinCond,
-        joinType,
-        ImmutableSet.<String>of());
-    if (!extraLeftExprs.isEmpty() || !extraRightExprs.isEmpty()) {
-      Mappings.TargetMapping mapping =
-          Mappings.createShiftMapping(
-              leftCount + extraLeftExprs.size()
-                  + rightCount + extraRightExprs.size(),
-              0, 0, leftCount,
-              leftCount, leftCount + extraLeftExprs.size(), rightCount);
-      return RelOptUtil.project(join, mapping);
-    }
-    return join;
-  }
-
-  private static boolean containsGet(RexNode node) {
-    try {
-      node.accept(
-          new RexVisitorImpl<Void>(true) {
-            @Override public Void visitCall(RexCall call) {
-              if (call.getOperator() == RexBuilder.GET_OPERATOR) {
-                throw Util.FoundOne.NULL;
-              }
-              return super.visitCall(call);
-            }
-          });
-      return false;
-    } catch (Util.FoundOne e) {
-      return true;
-    }
-  }
-
-  /**
-   * Pushes down parts of a join condition. For example, given
-   * "emp JOIN dept ON emp.deptno + 1 = dept.deptno", adds a project above
-   * "emp" that computes the expression
-   * "emp.deptno + 1". The resulting join condition is a simple combination
-   * of AND, equals, and input fields.
-   */
-  private RexNode pushDownJoinConditions(
-      RexNode node,
-      int leftCount,
-      int rightCount,
-      List<RexNode> extraLeftExprs,
-      List<RexNode> extraRightExprs) {
-    switch (node.getKind()) {
-    case AND:
-    case OR:
-    case EQUALS:
-      RexCall call = (RexCall) node;
-      List<RexNode> list = new ArrayList<RexNode>();
-      List<RexNode> operands = Lists.newArrayList(call.getOperands());
-      for (int i = 0; i < operands.size(); i++) {
-        RexNode operand = operands.get(i);
-        final int left2 = leftCount + extraLeftExprs.size();
-        final int right2 = rightCount + extraRightExprs.size();
-        final RexNode e =
-            pushDownJoinConditions(
-                operand,
-                leftCount,
-                rightCount,
-                extraLeftExprs,
-                extraRightExprs);
-        final List<RexNode> remainingOperands = Util.skip(operands, i + 1);
-        final int left3 = leftCount + extraLeftExprs.size();
-        final int right3 = rightCount + extraRightExprs.size();
-        fix(remainingOperands, left2, left3);
-        fix(list, left2, left3);
-        list.add(e);
-      }
-      if (!list.equals(call.getOperands())) {
-        return call.clone(call.getType(), list);
-      }
-      return call;
-    case INPUT_REF:
-    case LITERAL:
-      return node;
-    default:
-      BitSet bits = RelOptUtil.InputFinder.bits(node);
-      final int mid = leftCount + extraLeftExprs.size();
-      switch (Side.of(bits, mid)) {
-      case LEFT:
-        fix(extraRightExprs, mid, mid + 1);
-        extraLeftExprs.add(node);
-        return new RexInputRef(mid, node.getType());
-      case RIGHT:
-        final int index2 = mid + rightCount + extraRightExprs.size();
-        extraRightExprs.add(node);
-        return new RexInputRef(index2, node.getType());
-      case BOTH:
-      case EMPTY:
-      default:
-        return node;
-      }
-    }
-  }
-
-  private void fix(List<RexNode> operands, int before, int after) {
-    if (before == after) {
-      return;
-    }
-    for (int i = 0; i < operands.size(); i++) {
-      RexNode node = operands.get(i);
-      operands.set(i, RexUtil.shift(node, before, after - before));
-    }
-  }
-
-  /**
-   * Categorizes whether a bit set contains bits left and right of a
-   * line.
-   */
-  enum Side {
-    LEFT, RIGHT, BOTH, EMPTY;
-
-    static Side of(BitSet bitSet, int middle) {
-      final int firstBit = bitSet.nextSetBit(0);
-      if (firstBit < 0) {
-        return EMPTY;
-      }
-      if (firstBit >= middle) {
-        return RIGHT;
-      }
-      if (bitSet.nextSetBit(middle) < 0) {
-        return LEFT;
-      }
-      return BOTH;
-    }
-  }
-
-  /**
-   * Determines whether a subquery is non-correlated. Note that a
-   * non-correlated subquery can contain correlated references, provided those
-   * references do not reference select statements that are parents of the
-   * subquery.
-   *
-   * @param subq the subquery
-   * @param bb   blackboard used while converting the subquery, i.e., the
-   *             blackboard of the parent query of this subquery
-   * @return true if the subquery is non-correlated.
-   */
-  private boolean isSubqNonCorrelated(RelNode subq, Blackboard bb) {
-    Set<String> correlatedVariables = RelOptUtil.getVariablesUsed(subq);
-    for (String correlName : correlatedVariables) {
-      DeferredLookup lookup = mapCorrelToDeferred.get(correlName);
-      String originalRelName = lookup.getOriginalRelName();
-
-      int[] nsIndexes = {-1};
-      final SqlValidatorScope[] ancestorScopes = {null};
-      SqlValidatorNamespace foundNs =
-          lookup.bb.scope.resolve(
-              originalRelName,
-              ancestorScopes,
-              nsIndexes);
-
-      assert foundNs != null;
-      assert nsIndexes.length == 1;
-
-      SqlValidatorScope ancestorScope = ancestorScopes[0];
-
-      // If the correlated reference is in a scope that's "above" the
-      // subquery, then this is a correlated subquery.
-      SqlValidatorScope parentScope = bb.scope;
-      do {
-        if (ancestorScope == parentScope) {
-          return false;
-        }
-        if (parentScope instanceof DelegatingScope) {
-          parentScope = ((DelegatingScope) parentScope).getParent();
-        } else {
-          break;
-        }
-      } while (parentScope != null);
-    }
-    return true;
-  }
-
-  /**
-   * Returns a list of fields to be prefixed to each relational expression.
-   *
-   * @return List of system fields
-   */
-  protected List<RelDataTypeField> getSystemFields() {
-    return Collections.emptyList();
-  }
-
-  private RexNode convertJoinCondition(
-      Blackboard bb,
-      SqlNode condition,
-      JoinConditionType conditionType,
-      RelNode leftRel,
-      RelNode rightRel) {
-    if (condition == null) {
-      return rexBuilder.makeLiteral(true);
-    }
-    bb.setRoot(ImmutableList.of(leftRel, rightRel));
-    replaceSubqueries(bb, condition, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
-    switch (conditionType) {
-    case ON:
-      bb.setRoot(ImmutableList.of(leftRel, rightRel));
-      return bb.convertExpression(condition);
-    case USING:
-      SqlNodeList list = (SqlNodeList) condition;
-      List<String> nameList = new ArrayList<String>();
-      for (SqlNode columnName : list) {
-        final SqlIdentifier id = (SqlIdentifier) columnName;
-        String name = id.getSimple();
-        nameList.add(name);
-      }
-      return convertUsing(leftRel, rightRel, nameList);
-    default:
-      throw Util.unexpected(conditionType);
-    }
-  }
-
-  /**
-   * Returns an expression for matching columns of a USING clause or inferred
-   * from NATURAL JOIN. "a JOIN b USING (x, y)" becomes "a.x = b.x AND a.y =
-   * b.y". Returns null if the column list is empty.
-   *
-   * @param leftRel  Left input to the join
-   * @param rightRel Right input to the join
-   * @param nameList List of column names to join on
-   * @return Expression to match columns from name list, or true if name list
-   * is empty
-   */
-  private RexNode convertUsing(
-      RelNode leftRel,
-      RelNode rightRel,
-      List<String> nameList) {
-    final List<RexNode> list = Lists.newArrayList();
-    for (String name : nameList) {
-      final RelDataType leftRowType = leftRel.getRowType();
-      RelDataTypeField leftField = catalogReader.field(leftRowType, name);
-      RexNode left =
-          rexBuilder.makeInputRef(
-              leftField.getType(),
-              leftField.getIndex());
-      final RelDataType rightRowType = rightRel.getRowType();
-      RelDataTypeField rightField =
-          catalogReader.field(rightRowType, name);
-      RexNode right =
-          rexBuilder.makeInputRef(
-              rightField.getType(),
-              leftRowType.getFieldList().size() + rightField.getIndex());
-      RexNode equalsCall =
-          rexBuilder.makeCall(
-              SqlStdOperatorTable.EQUALS,
-              left,
-              right);
-      list.add(equalsCall);
-    }
-    return RexUtil.composeConjunction(rexBuilder, list, false);
-  }
-
-  private static JoinRelType convertJoinType(JoinType joinType) {
-    switch (joinType) {
-    case COMMA:
-    case INNER:
-    case CROSS:
-      return JoinRelType.INNER;
-    case FULL:
-      return JoinRelType.FULL;
-    case LEFT:
-      return JoinRelType.LEFT;
-    case RIGHT:
-      return JoinRelType.RIGHT;
-    default:
-      throw Util.unexpected(joinType);
-    }
-  }
-
-  /**
-   * Converts the SELECT, GROUP BY and HAVING clauses of an aggregate query.
-   *
-   * <p>This method extracts SELECT, GROUP BY and HAVING clauses, and creates
-   * an {@link AggConverter}, then delegates to {@link #createAggImpl}.
-   * Derived class may override this method to change any of those clauses or
-   * specify a different {@link AggConverter}.
-   *
-   * @param bb            Scope within which to resolve identifiers
-   * @param select        Query
-   * @param orderExprList Additional expressions needed to implement ORDER BY
-   */
-  protected void convertAgg(
-      Blackboard bb,
-      SqlSelect select,
-      List<SqlNode> orderExprList) {
-    assert bb.root != null : "precondition: child != null";
-    SqlNodeList groupList = select.getGroup();
-    SqlNodeList selectList = select.getSelectList();
-    SqlNode having = select.getHaving();
-
-    final AggConverter aggConverter = new AggConverter(bb, select);
-    createAggImpl(
-        bb,
-        aggConverter,
-        selectList,
-        groupList,
-        having,
-        orderExprList);
-  }
-
-  protected final void createAggImpl(
-      Blackboard bb,
-      AggConverter aggConverter,
-      SqlNodeList selectList,
-      SqlNodeList groupList,
-      SqlNode having,
-      List<SqlNode> orderExprList) {
-    SqlNodeList aggList = new SqlNodeList(SqlParserPos.ZERO);
-
-    for (SqlNode selectNode : selectList) {
-      if (validator.isAggregate(selectNode)) {
-        aggList.add(selectNode);
-      }
-    }
-
-    // first replace the subqueries inside the aggregates
-    // because they will provide input rows to the aggregates.
-    replaceSubqueries(bb, aggList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
-
-    // If group-by clause is missing, pretend that it has zero elements.
-    if (groupList == null) {
-      groupList = SqlNodeList.EMPTY;
-    }
-
-    // register the group exprs
-
-    // build a map to remember the projections from the top scope to the
-    // output of the current root.
-    //
-    // Currently farrago allows expressions, not just column references in
-    // group by list. This is not SQL 2003 compliant.
-
-    Map<Integer, Integer> groupExprProjection =
-        new HashMap<Integer, Integer>();
-
-    int i = -1;
-    for (SqlNode groupExpr : groupList) {
-      ++i;
-      final SqlNode expandedGroupExpr =
-          validator.expand(groupExpr, bb.scope);
-      aggConverter.addGroupExpr(expandedGroupExpr);
-
-      if (expandedGroupExpr instanceof SqlIdentifier) {
-        // SQL 2003 does not allow expressions of column references
-        SqlIdentifier expr = (SqlIdentifier) expandedGroupExpr;
-
-        // column references should be fully qualified.
-        assert expr.names.size() == 2;
-        String originalRelName = expr.names.get(0);
-        String originalFieldName = expr.names.get(1);
-
-        int[] nsIndexes = {-1};
-        final SqlValidatorScope[] ancestorScopes = {null};
-        SqlValidatorNamespace foundNs =
-            bb.scope.resolve(
-                originalRelName,
-                ancestorScopes,
-                nsIndexes);
-
-        assert foundNs != null;
-        assert nsIndexes.length == 1;
-        int childNamespaceIndex = nsIndexes[0];
-
-        int namespaceOffset = 0;
-
-        if (childNamespaceIndex > 0) {
-          // If not the first child, need to figure out the width of
-          // output types from all the preceding namespaces
-          assert ancestorScopes[0] instanceof ListScope;
-          List<SqlValidatorNamespace> children =
-              ((ListScope) ancestorScopes[0]).getChildren();
-
-          for (int j = 0; j < childNamespaceIndex; j++) {
-            namespaceOffset +=
-                children.get(j).getRowType().getFieldCount();
-          }
-        }
-
-        RelDataTypeField field =
-            catalogReader.field(foundNs.getRowType(), originalFieldName);
-        int origPos = namespaceOffset + field.getIndex();
-
-        groupExprProjection.put(origPos, i);
-      }
-    }
-
-    RexNode havingExpr = null;
-    List<RexNode> selectExprs = new ArrayList<RexNode>();
-    List<String> selectNames = new ArrayList<String>();
-
-    try {
-      Util.permAssert(bb.agg == null, "already in agg mode");
-      bb.agg = aggConverter;
-
-      // convert the select and having expressions, so that the
-      // agg converter knows which aggregations are required
-
-      selectList.accept(aggConverter);
-      for (SqlNode expr : orderExprList) {
-        expr.accept(aggConverter);
-      }
-      if (having != null) {
-        having.accept(aggConverter);
-      }
-
-      // compute inputs to the aggregator
-      List<RexNode> preExprs = aggConverter.getPreExprs();
-      List<String> preNames = aggConverter.getPreNames();
-
-      if (preExprs.size() == 0) {
-        // Special case for COUNT(*), where we can end up with no inputs
-        // at all.  The rest of the system doesn't like 0-tuples, so we
-        // select a dummy constant here.
-        preExprs =
-            Collections.singletonList(
-                (RexNode) rexBuilder.makeExactLiteral(BigDecimal.ZERO));
-        preNames = Collections.singletonList(null);
-      }
-
-      final RelNode inputRel = bb.root;
-
-      // Project the expressions required by agg and having.
-      bb.setRoot(
-          RelOptUtil.createProject(
-              inputRel,
-              preExprs,
-              preNames,
-              true),
-          false);
-      bb.mapRootRelToFieldProjection.put(bb.root, groupExprProjection);
-
-      // REVIEW jvs 31-Oct-2007:  doesn't the declaration of
-      // monotonicity here assume sort-based aggregation at
-      // the physical level?
-
-      // Tell bb which of group columns are sorted.
-      bb.columnMonotonicities.clear();
-      for (SqlNode groupItem : groupList) {
-        bb.columnMonotonicities.add(
-            bb.scope.getMonotonicity(groupItem));
-      }
-
-      // Add the aggregator
-      bb.setRoot(
-          createAggregate(
-              bb,
-              BitSets.range(aggConverter.groupExprs.size()),
-              aggConverter.getAggCalls()),
-          false);
-
-      bb.mapRootRelToFieldProjection.put(bb.root, groupExprProjection);
-
-      // Replace subqueries in having here and modify having to use
-      // the replaced expressions
-      if (having

<TRUNCATED>

[05/10] incubator-kylin git commit: KYLIN-780 Upgrade query module, all query test pass

Posted by li...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/pom.xml
----------------------------------------------------------------------
diff --git a/jdbc/pom.xml b/jdbc/pom.xml
index 7f707c7..485ed3d 100644
--- a/jdbc/pom.xml
+++ b/jdbc/pom.xml
@@ -22,7 +22,7 @@
 	<artifactId>kylin-jdbc</artifactId>
 	<packaging>jar</packaging>
 	<name>Kylin:JDBC</name>
-	<description>Kylin JDBC Driver on optiq avatica</description>
+	<description>Kylin JDBC Driver on Calcite Avatica</description>
 
 	<parent>
 		<groupId>org.apache.kylin</groupId>

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/Driver.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/Driver.java b/jdbc/src/main/java/org/apache/kylin/jdbc/Driver.java
index 5198fd7..4cc0445 100644
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/Driver.java
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/Driver.java
@@ -1,147 +1,140 @@
-/*
- * 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.kylin.jdbc;
-
-import java.sql.DriverManager;
-import java.sql.SQLException;
-
-import net.hydromatic.avatica.AvaticaConnection;
-import net.hydromatic.avatica.AvaticaStatement;
-import net.hydromatic.avatica.DriverVersion;
-import net.hydromatic.avatica.Handler;
-import net.hydromatic.avatica.HandlerImpl;
-import net.hydromatic.avatica.UnregisteredDriver;
-
-import org.apache.kylin.jdbc.stub.RemoteClient;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import org.apache.kylin.jdbc.stub.ConnectionException;
-
-/**
- * <p>
- * Kylin JDBC Driver based on optiq avatica and kylin restful api.<br>
- * Supported versions:
- * </p>
- * <ul>
- * <li>jdbc 4.0</li>
- * <li>jdbc 4.1</li>
- * </ul>
- * 
- * <p>
- * Supported Statements:
- * </p>
- * <ul>
- * <li>{@link KylinStatementImpl}</li>
- * <li>{@link KylinPrepareStatementImpl}</li>
- * </ul>
- * 
- * <p>
- * Supported properties:
- * <ul>
- * <li>user: username</li>
- * <li>password: password</li>
- * <li>ssl: true/false</li>
- * </ul>
- * </p>
- * 
- * <p>
- * Driver init code sample:<br>
- * 
- * <pre>
- * Driver driver = (Driver) Class.forName(&quot;org.apache.kylin.kylin.jdbc.Driver&quot;).newInstance();
- * Properties info = new Properties();
- * info.put(&quot;user&quot;, &quot;user&quot;);
- * info.put(&quot;password&quot;, &quot;password&quot;);
- * info.put(&quot;ssl&quot;, true);
- * Connection conn = driver.connect(&quot;jdbc:kylin://{domain}/{project}&quot;, info);
- * </pre>
- * 
- * </p>
- * 
- * @author xduo
- * 
- */
-public class Driver extends UnregisteredDriver {
-    private static final Logger logger = LoggerFactory.getLogger(Driver.class);
-
-    public static final String CONNECT_STRING_PREFIX = "jdbc:kylin:";
-    static {
-        try {
-            DriverManager.registerDriver(new Driver());
-        } catch (SQLException e) {
-            throw new RuntimeException("Error occurred while registering JDBC driver " + Driver.class.getName() + ": " + e.toString());
-        }
-    }
-    
-    @Override
-    protected DriverVersion createDriverVersion() {
-        return DriverVersion.load(Driver.class, "org-apache-kylin-jdbc.properties", "Kylin JDBC Driver", "unknown version", "Kylin", "unknown version");
-    }
-
-    @Override
-    protected String getFactoryClassName(JdbcVersion jdbcVersion) {
-        switch (jdbcVersion) {
-        case JDBC_30:
-            throw new UnsupportedOperationException();
-        case JDBC_40:
-            return "org.apache.kylin.jdbc.KylinJdbc40Factory";
-        case JDBC_41:
-        default:
-            return "org.apache.kylin.jdbc.KylinJdbc41Factory";
-        }
-    }
-
-    @Override
-    protected Handler createHandler() {
-        return new HandlerImpl() {
-            @Override
-            public void onConnectionInit(AvaticaConnection connection_) throws SQLException {
-                KylinConnectionImpl kylinConn = (KylinConnectionImpl) connection_;
-                RemoteClient runner = ((KylinJdbc41Factory) factory).newRemoteClient(kylinConn);
-                try {
-                    runner.connect();
-                    kylinConn.setMetaProject(runner.getMetadata(kylinConn.getProject()));
-                    logger.debug("Connection inited.");
-                } catch (ConnectionException e) {
-                    logger.error(e.getLocalizedMessage(), e);
-                    throw new SQLException(e.getLocalizedMessage());
-                }
-            }
-
-            public void onConnectionClose(AvaticaConnection connection) {
-                logger.debug("Connection closed.");
-            }
-
-            public void onStatementExecute(AvaticaStatement statement, ResultSink resultSink) {
-                logger.debug("statement executed.");
-            }
-
-            public void onStatementClose(AvaticaStatement statement) {
-                logger.debug("statement closed.");
-            }
-        };
-    }
-
-    @Override
-    protected String getConnectStringPrefix() {
-        return CONNECT_STRING_PREFIX;
-    }
-
-}
+/*
+ * 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.kylin.jdbc;
+
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import org.apache.calcite.avatica.AvaticaConnection;
+import org.apache.calcite.avatica.DriverVersion;
+import org.apache.calcite.avatica.Meta;
+import org.apache.calcite.avatica.UnregisteredDriver;
+
+/**
+ * <p>
+ * Kylin JDBC Driver based on Calcite Avatica and Kylin restful API.<br>
+ * Supported versions:
+ * </p>
+ * <ul>
+ * <li>jdbc 4.0</li>
+ * <li>jdbc 4.1</li>
+ * </ul>
+ * 
+ * <p>
+ * Supported Statements:
+ * </p>
+ * <ul>
+ * <li>{@link KylinStatementImpl}</li>
+ * <li>{@link KylinPrepareStatementImpl}</li>
+ * </ul>
+ * 
+ * <p>
+ * Supported properties:
+ * <ul>
+ * <li>user: username</li>
+ * <li>password: password</li>
+ * <li>ssl: true/false</li>
+ * </ul>
+ * </p>
+ * 
+ * <p>
+ * Driver init code sample:<br>
+ * 
+ * <pre>
+ * Driver driver = (Driver) Class.forName(&quot;org.apache.kylin.kylin.jdbc.Driver&quot;).newInstance();
+ * Properties info = new Properties();
+ * info.put(&quot;user&quot;, &quot;user&quot;);
+ * info.put(&quot;password&quot;, &quot;password&quot;);
+ * info.put(&quot;ssl&quot;, true);
+ * Connection conn = driver.connect(&quot;jdbc:kylin://{domain}/{project}&quot;, info);
+ * </pre>
+ * 
+ * </p>
+ */
+public class Driver extends UnregisteredDriver {
+
+    public static final String CONNECT_STRING_PREFIX = "jdbc:kylin:";
+    static {
+        try {
+            DriverManager.registerDriver(new Driver());
+        } catch (SQLException e) {
+            throw new RuntimeException("Error occurred while registering JDBC driver " + Driver.class.getName() + ": " + e.toString());
+        }
+    }
+    
+    @Override
+    protected String getConnectStringPrefix() {
+        return CONNECT_STRING_PREFIX;
+    }
+
+    @Override
+    protected DriverVersion createDriverVersion() {
+        return DriverVersion.load(Driver.class, "kylin-jdbc.properties", "Kylin JDBC Driver", "unknown version", "Kylin", "unknown version");
+    }
+
+    @Override
+    protected String getFactoryClassName(JdbcVersion jdbcVersion) {
+        switch (jdbcVersion) {
+        case JDBC_30:
+            throw new UnsupportedOperationException();
+        case JDBC_40:
+            return KylinJdbcFactory.Version40.class.getName();
+        case JDBC_41:
+        default:
+            return KylinJdbcFactory.Version41.class.getName();
+        }
+    }
+
+    @Override
+    public Meta createMeta(AvaticaConnection connection) {
+        return new KylinMeta((KylinConnection) connection);
+    }
+
+//    @Override
+//    protected Handler createHandler() {
+//        return new HandlerImpl() {
+//            @Override
+//            public void onConnectionInit(AvaticaConnection connection) throws SQLException {
+//                KylinConnection conn = (KylinConnection) connection;
+//                RemoteClient runner = ((KylinJdbcFactory) factory).newRemoteClient(conn);
+//                try {
+//                    runner.connect();
+//                    conn.setMetaProject(runner.getMetadata(conn.getProject()));
+//                    logger.debug("Connection inited.");
+//                } catch (ConnectionException e) {
+//                    logger.error(e.getLocalizedMessage(), e);
+//                    throw new SQLException(e.getLocalizedMessage());
+//                }
+//            }
+//
+//            public void onConnectionClose(AvaticaConnection connection) {
+//                logger.debug("Connection closed.");
+//            }
+//
+//            public void onStatementExecute(AvaticaStatement statement, ResultSink resultSink) {
+//                logger.debug("statement executed.");
+//            }
+//
+//            public void onStatementClose(AvaticaStatement statement) {
+//                logger.debug("statement closed.");
+//            }
+//        };
+//    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/IRemoteClient.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/IRemoteClient.java b/jdbc/src/main/java/org/apache/kylin/jdbc/IRemoteClient.java
new file mode 100644
index 0000000..b6a13e5
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/IRemoteClient.java
@@ -0,0 +1,56 @@
+/*
+ * 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.kylin.jdbc;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.calcite.avatica.AvaticaParameter;
+import org.apache.calcite.avatica.ColumnMetaData;
+import org.apache.kylin.jdbc.KylinMeta.KMetaProject;
+
+public interface IRemoteClient extends Closeable {
+
+    public static class QueryResult {
+        public final List<ColumnMetaData> columnMeta;
+        public final Iterable<Object> iterable;
+
+        public QueryResult(List<ColumnMetaData> columnMeta, Iterable<Object> iterable) {
+            this.columnMeta = columnMeta;
+            this.iterable = iterable;
+        }
+    }
+
+    /**
+     * Connect to Kylin restful service. IOException will be thrown if authentication failed.
+     */
+    public void connect() throws IOException;
+
+    /**
+     * Retrieve meta data of given project.
+     */
+    public KMetaProject retrieveMetaData(String project) throws IOException;
+
+    /**
+     * Execute query remotely and get back result.
+     */
+    public QueryResult executeQuery(String sql, List<AvaticaParameter> params, List<Object> paramValues) throws IOException;
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinClient.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinClient.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinClient.java
new file mode 100644
index 0000000..4b73ad6
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinClient.java
@@ -0,0 +1,373 @@
+package org.apache.kylin.jdbc;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.xml.bind.DatatypeConverter;
+
+import org.apache.calcite.avatica.AvaticaParameter;
+import org.apache.calcite.avatica.ColumnMetaData;
+import org.apache.calcite.avatica.ColumnMetaData.Rep;
+import org.apache.calcite.avatica.ColumnMetaData.ScalarType;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.commons.httpclient.protocol.Protocol;
+import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
+import org.apache.kylin.jdbc.KylinMeta.KMetaCatalog;
+import org.apache.kylin.jdbc.KylinMeta.KMetaColumn;
+import org.apache.kylin.jdbc.KylinMeta.KMetaProject;
+import org.apache.kylin.jdbc.KylinMeta.KMetaSchema;
+import org.apache.kylin.jdbc.KylinMeta.KMetaTable;
+import org.apache.kylin.jdbc.json.PreparedQueryRequest;
+import org.apache.kylin.jdbc.json.QueryRequest;
+import org.apache.kylin.jdbc.json.SQLResponseStub;
+import org.apache.kylin.jdbc.json.StatementParameter;
+import org.apache.kylin.jdbc.json.TableMetaStub;
+import org.apache.kylin.jdbc.json.TableMetaStub.ColumnMetaStub;
+import org.apache.kylin.jdbc.util.DefaultSslProtocolSocketFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class KylinClient implements IRemoteClient {
+
+    private static final Logger logger = LoggerFactory.getLogger(KylinClient.class);
+
+    private final KylinConnection conn;
+    private final Properties connProps;
+    private final HttpClient httpClient;
+    private final ObjectMapper jsonMapper;
+
+    public KylinClient(KylinConnection conn) {
+        this.conn = conn;
+        this.connProps = conn.getConnectionProperties();
+        this.httpClient = new HttpClient();
+        this.jsonMapper = new ObjectMapper();
+
+        // trust all certificates
+        if (isSSL()) {
+            Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) new DefaultSslProtocolSocketFactory(), 443));
+        }
+    }
+
+    private boolean isSSL() {
+        return Boolean.parseBoolean(connProps.getProperty("ssl", "false"));
+    }
+
+    private String baseUrl() {
+        return (isSSL() ? "https://" : "http://") + conn.getBaseUrl();
+    }
+
+    private void addHttpHeaders(HttpMethodBase method) {
+        method.addRequestHeader("Accept", "application/json, text/plain, */*");
+        method.addRequestHeader("Content-Type", "application/json");
+
+        String username = connProps.getProperty("user");
+        String password = connProps.getProperty("password");
+        String basicAuth = DatatypeConverter.printBase64Binary((username + ":" + password).getBytes());
+        method.addRequestHeader("Authorization", "Basic " + basicAuth);
+    }
+
+    @Override
+    public void connect() throws IOException {
+        PostMethod post = new PostMethod(baseUrl() + "/kylin/api/user/authentication");
+        addHttpHeaders(post);
+        StringRequestEntity requestEntity = new StringRequestEntity("{}", "application/json", "UTF-8");
+        post.setRequestEntity(requestEntity);
+
+        httpClient.executeMethod(post);
+
+        if (post.getStatusCode() != 200 && post.getStatusCode() != 201) {
+            throw asIOException(post);
+        }
+    }
+
+    @Override
+    public KMetaProject retrieveMetaData(String project) throws IOException {
+        assert conn.getProject().equals(project);
+
+        String url = baseUrl() + "/kylin/api/tables_and_columns?project=" + project;
+        GetMethod get = new GetMethod(url);
+        addHttpHeaders(get);
+
+        httpClient.executeMethod(get);
+
+        if (get.getStatusCode() != 200 && get.getStatusCode() != 201) {
+            throw asIOException(get);
+        }
+
+        List<TableMetaStub> tableMetaStubs = jsonMapper.readValue(get.getResponseBodyAsStream(), new TypeReference<List<TableMetaStub>>() {
+        });
+
+        List<KMetaTable> tables = convertMetaTables(tableMetaStubs);
+        List<KMetaSchema> schemas = convertMetaSchemas(tables);
+        List<KMetaCatalog> catalogs = convertMetaCatalogs(schemas);
+        return new KMetaProject(project, catalogs);
+    }
+
+    private List<KMetaCatalog> convertMetaCatalogs(List<KMetaSchema> schemas) {
+        Map<String, List<KMetaSchema>> catalogMap = new LinkedHashMap<String, List<KMetaSchema>>();
+        for (KMetaSchema schema : schemas) {
+            List<KMetaSchema> list = catalogMap.get(schema.tableCatalog);
+            if (list == null) {
+                list = new ArrayList<KMetaSchema>();
+                catalogMap.put(schema.tableCatalog, list);
+            }
+            list.add(schema);
+        }
+        
+        List<KMetaCatalog> result = new ArrayList<KMetaCatalog>();
+        for (List<KMetaSchema> catSchemas : catalogMap.values()) {
+            String catalog = catSchemas.get(0).tableCatalog;
+            result.add(new KMetaCatalog(catalog, catSchemas));
+        }
+        return result;
+    }
+
+    private List<KMetaSchema> convertMetaSchemas(List<KMetaTable> tables) {
+        Map<String, List<KMetaTable>> schemaMap = new LinkedHashMap<String, List<KMetaTable>>();
+        for (KMetaTable table : tables) {
+            String key = table.tableCat + "!!" + table.tableSchem;
+            List<KMetaTable> list = schemaMap.get(key);
+            if (list == null) {
+                list = new ArrayList<KMetaTable>();
+                schemaMap.put(key, list);
+            }
+            list.add(table);
+        }
+        
+        List<KMetaSchema> result = new ArrayList<KMetaSchema>();
+        for (List<KMetaTable> schemaTables : schemaMap.values()) {
+            String catalog = schemaTables.get(0).tableCat;
+            String schema = schemaTables.get(0).tableSchem;
+            result.add(new KMetaSchema(catalog, schema, schemaTables));
+        }
+        return result;
+    }
+
+    private List<KMetaTable> convertMetaTables(List<TableMetaStub> tableMetaStubs) {
+        List<KMetaTable> result = new ArrayList<KMetaTable>(tableMetaStubs.size());
+        for (TableMetaStub tableStub : tableMetaStubs) {
+            result.add(convertMetaTable(tableStub));
+        }
+        return result;
+    }
+
+    private KMetaTable convertMetaTable(TableMetaStub tableStub) {
+        List<KMetaColumn> columns = new ArrayList<KMetaColumn>(tableStub.getColumns().size());
+        for (ColumnMetaStub columnStub : tableStub.getColumns()) {
+            columns.add(convertMetaColumn(columnStub));
+        }
+        return new KMetaTable(tableStub.getTABLE_CAT(), tableStub.getTABLE_SCHEM(), tableStub.getTABLE_NAME(), tableStub.getTABLE_TYPE(), columns);
+    }
+
+    private KMetaColumn convertMetaColumn(ColumnMetaStub columnStub) {
+        return new KMetaColumn(columnStub.getTABLE_CAT(), columnStub.getTABLE_SCHEM(), columnStub.getTABLE_NAME(), columnStub.getCOLUMN_NAME(), columnStub.getDATA_TYPE(), columnStub.getTYPE_NAME(), columnStub.getCOLUMN_SIZE(), columnStub.getDECIMAL_DIGITS(), columnStub.getNUM_PREC_RADIX(), columnStub.getNULLABLE(), columnStub.getCHAR_OCTET_LENGTH(), columnStub.getORDINAL_POSITION(), columnStub.getIS_NULLABLE());
+    }
+
+    @Override
+    public QueryResult executeQuery(String sql, List<AvaticaParameter> params, List<Object> paramValues) throws IOException {
+
+        SQLResponseStub queryResp = executeKylinQuery(sql, convertParameters(params, paramValues));
+        if (queryResp.getIsException())
+            throw new IOException(queryResp.getExceptionMessage());
+
+        List<ColumnMetaData> metas = convertColumnMeta(queryResp);
+        List<Object> data = convertResultData(queryResp, metas);
+
+        return new QueryResult(metas, data);
+    }
+
+    private List<StatementParameter> convertParameters(List<AvaticaParameter> params, List<Object> paramValues) {
+        if (params == null || params.isEmpty())
+            return null;
+
+        assert params.size() == paramValues.size();
+
+        List<StatementParameter> result = new ArrayList<StatementParameter>();
+        for (Object v : paramValues) {
+            result.add(new StatementParameter(v.getClass().getCanonicalName(), String.valueOf(v)));
+        }
+        return result;
+    }
+
+    private SQLResponseStub executeKylinQuery(String sql, List<StatementParameter> params) throws IOException {
+        String url = baseUrl() + "/kylin/api/query";
+        String project = conn.getProject();
+
+        QueryRequest request = null;
+        if (null != params) {
+            request = new PreparedQueryRequest();
+            ((PreparedQueryRequest) request).setParams(params);
+            url += "/prestate"; // means prepared statement..
+        } else {
+            request = new QueryRequest();
+        }
+        request.setSql(sql);
+        request.setProject(project);
+
+        PostMethod post = new PostMethod(url);
+        addHttpHeaders(post);
+
+        String postBody = jsonMapper.writeValueAsString(request);
+        logger.debug("Post body:\n " + postBody);
+        StringRequestEntity requestEntity = new StringRequestEntity(postBody, "application/json", "UTF-8");
+        post.setRequestEntity(requestEntity);
+
+        httpClient.executeMethod(post);
+
+        if (post.getStatusCode() != 200 && post.getStatusCode() != 201) {
+            throw asIOException(post);
+        }
+
+        return jsonMapper.readValue(post.getResponseBodyAsStream(), SQLResponseStub.class);
+    }
+
+    private List<ColumnMetaData> convertColumnMeta(SQLResponseStub queryResp) {
+        List<ColumnMetaData> metas = new ArrayList<ColumnMetaData>();
+        for (int i = 0; i < queryResp.getColumnMetas().size(); i++) {
+            SQLResponseStub.ColumnMetaStub scm = queryResp.getColumnMetas().get(i);
+            ScalarType type = ColumnMetaData.scalar(scm.getColumnType(), scm.getColumnTypeName(), Rep.of(convertType(scm.getColumnType())));
+
+            ColumnMetaData meta = new ColumnMetaData(i, scm.isAutoIncrement(), scm.isCaseSensitive(), scm.isSearchable(), scm.isCurrency(), scm.getIsNullable(), scm.isSigned(), scm.getDisplaySize(), scm.getLabel(), scm.getName(), scm.getSchemaName(), scm.getPrecision(), scm.getScale(), scm.getTableName(), scm.getSchemaName(), type, scm.isReadOnly(), scm.isWritable(), scm.isWritable(), null);
+
+            metas.add(meta);
+        }
+
+        return metas;
+    }
+
+    private List<Object> convertResultData(SQLResponseStub queryResp, List<ColumnMetaData> metas) {
+        List<String[]> stringResults = queryResp.getResults();
+        List<Object> data = new ArrayList<Object>(stringResults.size());
+        for (String[] result : stringResults) {
+            Object[] row = new Object[result.length];
+
+            for (int i = 0; i < result.length; i++) {
+                ColumnMetaData meta = metas.get(i);
+                row[i] = wrapObject(result[i], meta.type.type);
+            }
+
+            data.add(row);
+        }
+        return (List<Object>) data;
+    }
+
+    private IOException asIOException(HttpMethodBase method) throws IOException {
+        return new IOException(method + " failed, error code " + method.getStatusCode() + " and response: " + method.getResponseBodyAsString());
+    }
+
+    @SuppressWarnings("rawtypes")
+    public static Class convertType(int sqlType) {
+        Class result = Object.class;
+
+        switch (sqlType) {
+        case Types.CHAR:
+        case Types.VARCHAR:
+        case Types.LONGVARCHAR:
+            result = String.class;
+            break;
+        case Types.NUMERIC:
+        case Types.DECIMAL:
+            result = BigDecimal.class;
+            break;
+        case Types.BIT:
+            result = Boolean.class;
+            break;
+        case Types.TINYINT:
+            result = Byte.class;
+            break;
+        case Types.SMALLINT:
+            result = Short.class;
+            break;
+        case Types.INTEGER:
+            result = Integer.class;
+            break;
+        case Types.BIGINT:
+            result = Long.class;
+            break;
+        case Types.REAL:
+        case Types.FLOAT:
+        case Types.DOUBLE:
+            result = Double.class;
+            break;
+        case Types.BINARY:
+        case Types.VARBINARY:
+        case Types.LONGVARBINARY:
+            result = Byte[].class;
+            break;
+        case Types.DATE:
+            result = Date.class;
+            break;
+        case Types.TIME:
+            result = Time.class;
+            break;
+        case Types.TIMESTAMP:
+            result = Timestamp.class;
+            break;
+        }
+
+        return result;
+    }
+
+    public static Object wrapObject(String value, int sqlType) {
+        if (null == value) {
+            return null;
+        }
+
+        switch (sqlType) {
+        case Types.CHAR:
+        case Types.VARCHAR:
+        case Types.LONGVARCHAR:
+            return value;
+        case Types.NUMERIC:
+        case Types.DECIMAL:
+            return new BigDecimal(value);
+        case Types.BIT:
+            return Boolean.parseBoolean(value);
+        case Types.TINYINT:
+            return Byte.valueOf(value);
+        case Types.SMALLINT:
+            return Short.valueOf(value);
+        case Types.INTEGER:
+            return Integer.parseInt(value);
+        case Types.BIGINT:
+            return Long.parseLong(value);
+        case Types.FLOAT:
+            return Float.parseFloat(value);
+        case Types.REAL:
+        case Types.DOUBLE:
+            return Double.parseDouble(value);
+        case Types.BINARY:
+        case Types.VARBINARY:
+        case Types.LONGVARBINARY:
+            return value.getBytes();
+        case Types.DATE:
+            return Date.valueOf(value);
+        case Types.TIME:
+            return Time.valueOf(value);
+        case Types.TIMESTAMP:
+            return Timestamp.valueOf(value);
+        }
+
+        return value;
+    }
+
+    @Override
+    public void close() throws IOException {
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinConnection.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinConnection.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinConnection.java
new file mode 100644
index 0000000..86b2ff8
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinConnection.java
@@ -0,0 +1,128 @@
+/*
+ * 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.kylin.jdbc;
+
+import java.io.IOException;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.calcite.avatica.AvaticaConnection;
+import org.apache.calcite.avatica.AvaticaParameter;
+import org.apache.calcite.avatica.AvaticaStatement;
+import org.apache.calcite.avatica.ColumnMetaData;
+import org.apache.calcite.avatica.Meta;
+import org.apache.calcite.avatica.Meta.CursorFactory;
+import org.apache.calcite.avatica.Meta.Signature;
+import org.apache.calcite.avatica.UnregisteredDriver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KylinConnection extends AvaticaConnection {
+
+    private static final Logger logger = LoggerFactory.getLogger(KylinConnection.class);
+
+    private final String baseUrl;
+    private final String project;
+    private final IRemoteClient remoteClient;
+
+    protected KylinConnection(UnregisteredDriver driver, KylinJdbcFactory factory, String url, Properties info) throws SQLException {
+        super(driver, factory, url, info);
+        
+        String odbcUrl = url;
+        odbcUrl = odbcUrl.replace(Driver.CONNECT_STRING_PREFIX + "//", "");
+        String[] temps = odbcUrl.split("/");
+
+        assert temps.length == 2;
+
+        this.baseUrl = temps[0];
+        this.project = temps[1];
+
+        logger.debug("Kylin base url " + this.baseUrl + ", project name " + this.project);
+        
+        this.remoteClient = factory.newRemoteClient(this);
+
+        try {
+            this.remoteClient.connect();
+        } catch (IOException e) {
+            throw new SQLException(e);
+        }
+    }
+    
+    String getBaseUrl() {
+        return baseUrl;
+    }
+    
+    String getProject() {
+        return project;
+    }
+    
+    Properties getConnectionProperties() {
+        return info;
+    }
+    
+    @Override
+    public AvaticaStatement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
+        return super.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
+    }
+
+    @Override
+    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
+        Meta.Signature sig = mockPreparedSignature(sql);
+        return factory().newPreparedStatement(this, null, sig, resultSetType, resultSetConcurrency, resultSetHoldability);
+    }
+    
+    // TODO add restful API to prepare SQL, get back expected ResultSetMetaData
+    Signature mockPreparedSignature(String sql) {
+        List<AvaticaParameter> params = new ArrayList<AvaticaParameter>();
+        int startIndex = 0;
+        while (sql.indexOf("?", startIndex) >= 0) {
+            AvaticaParameter param = new AvaticaParameter(false, 0, 0, 0, null, null, null);
+            params.add(param);
+            startIndex = sql.indexOf("?", startIndex) + 1;
+        }
+
+        ArrayList<ColumnMetaData> columns = new ArrayList<ColumnMetaData>();
+        Map<String, Object> internalParams = Collections.<String, Object>emptyMap();
+        
+        return new Meta.Signature(columns, sql, params, internalParams, CursorFactory.ARRAY);
+    }
+
+    private KylinJdbcFactory factory() {
+        return (KylinJdbcFactory) factory;
+    }
+
+    public IRemoteClient getRemoteClient() {
+        return remoteClient;
+    }
+
+    @Override
+    public void close() throws SQLException {
+        super.close();
+        try {
+            remoteClient.close();
+        } catch (IOException e) {
+            throw new SQLException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinConnectionImpl.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinConnectionImpl.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinConnectionImpl.java
deleted file mode 100644
index 887bd66..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinConnectionImpl.java
+++ /dev/null
@@ -1,158 +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.kylin.jdbc;
-
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Properties;
-
-import javax.xml.bind.DatatypeConverter;
-
-import net.hydromatic.avatica.AvaticaConnection;
-import net.hydromatic.avatica.AvaticaFactory;
-import net.hydromatic.avatica.AvaticaPreparedStatement;
-import net.hydromatic.avatica.AvaticaStatement;
-import net.hydromatic.avatica.Meta;
-import net.hydromatic.avatica.UnregisteredDriver;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import org.apache.kylin.jdbc.KylinPrepare.PrepareResult;
-
-/**
- * Kylin connection implementation
- * 
- * @author xduo
- * 
- */
-public abstract class KylinConnectionImpl extends AvaticaConnection {
-    private static final Logger logger = LoggerFactory.getLogger(KylinConnectionImpl.class);
-
-    private final String baseUrl;
-    private final String project;
-    private KylinMetaImpl.MetaProject metaProject;
-    public final List<AvaticaStatement> statements;
-    static final Trojan TROJAN = createTrojan();
-
-    protected KylinConnectionImpl(UnregisteredDriver driver, AvaticaFactory factory, String url, Properties info) {
-        super(driver, factory, url, info);
-
-        String odbcUrl = url;
-        odbcUrl = odbcUrl.replace(Driver.CONNECT_STRING_PREFIX + "//", "");
-        String[] temps = odbcUrl.split("/");
-
-        assert temps.length == 2;
-
-        this.baseUrl = temps[0];
-        this.project = temps[1];
-
-        logger.debug("Kylin base url " + this.baseUrl + ", project name " + this.project);
-
-        statements = new ArrayList<AvaticaStatement>();
-    }
-
-    @Override
-    protected Meta createMeta() {
-        return new KylinMetaImpl(this, (KylinJdbc41Factory) factory);
-    }
-
-    @Override
-    public AvaticaStatement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
-        AvaticaStatement statement = super.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
-        statements.add(statement);
-
-        return statement;
-    }
-
-    @Override
-    public PreparedStatement prepareStatement(String sql) throws SQLException {
-        PrepareResult pr = new KylinPrepareImpl().prepare(sql);
-        AvaticaPreparedStatement statement = ((KylinJdbc41Factory) factory).newPreparedStatement(this, pr, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, this.getHoldability());
-        statements.add(statement);
-
-        return statement;
-    }
-
-    // ~ kylin specified implements
-
-    public String getBasicAuthHeader() {
-        String username = this.info.getProperty("user");
-        String password = this.info.getProperty("password");
-
-        return DatatypeConverter.printBase64Binary((username + ":" + password).getBytes());
-    }
-
-    public String getConnectUrl() {
-        boolean isSsl = Boolean.parseBoolean((this.info.getProperty("ssl", "false")));
-        return (isSsl ? "https://" : "http://") + this.baseUrl + "/kylin/api/user/authentication";
-    }
-
-    public String getMetaProjectUrl(String project) {
-        assert project != null;
-        boolean isSsl = Boolean.parseBoolean((this.info.getProperty("ssl", "false")));
-        return (isSsl ? "https://" : "http://") + this.baseUrl + "/kylin/api/tables_and_columns?project=" + project;
-    }
-
-    public String getQueryUrl() {
-        boolean isSsl = Boolean.parseBoolean((this.info.getProperty("ssl", "false")));
-        return (isSsl ? "https://" : "http://") + this.baseUrl + "/kylin/api/query";
-    }
-
-    public String getProject() {
-        return this.project;
-    }
-
-    public Meta getMeta() {
-        return this.meta;
-    }
-
-    public AvaticaFactory getFactory() {
-        return this.factory;
-    }
-
-    public UnregisteredDriver getDriver() {
-        return this.driver;
-    }
-
-    public KylinMetaImpl.MetaProject getMetaProject() {
-        return metaProject;
-    }
-
-    public void setMetaProject(KylinMetaImpl.MetaProject metaProject) {
-        this.metaProject = metaProject;
-    }
-
-    @Override
-    public void close() throws SQLException {
-        super.close();
-
-        this.metaProject = null;
-        this.statements.clear();
-    }
-
-    @Override
-    public String toString() {
-        return "KylinConnectionImpl [baseUrl=" + baseUrl + ", project=" + project + ", metaProject=" + metaProject + "]";
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinEnumerator.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinEnumerator.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinEnumerator.java
deleted file mode 100644
index 04d9231..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinEnumerator.java
+++ /dev/null
@@ -1,87 +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.kylin.jdbc;
-
-import java.util.Collection;
-import java.util.Iterator;
-
-import net.hydromatic.linq4j.Enumerator;
-
-/**
- * Query results enumerator
- * 
- * @author xduo
- * 
- */
-public class KylinEnumerator<E> implements Enumerator<E> {
-
-    /**
-     * current row
-     */
-    private E current;
-
-    /**
-     * data collection
-     */
-    private Collection<E> dataCollection;
-
-    /**
-     * result iterator
-     */
-    private Iterator<E> cursor;
-
-    public KylinEnumerator(Collection<E> dataCollection) {
-        this.dataCollection = dataCollection;
-        this.cursor = this.dataCollection.iterator();
-
-        if (null == this.cursor) {
-            throw new RuntimeException("Cursor can't be null");
-        }
-    }
-
-    @Override
-    public E current() {
-        return current;
-    }
-
-    @Override
-    public boolean moveNext() {
-        if (!cursor.hasNext()) {
-            this.reset();
-
-            return false;
-        }
-
-        current = cursor.next();
-
-        return true;
-    }
-
-    @Override
-    public void reset() {
-        this.cursor = this.dataCollection.iterator();
-    }
-
-    @Override
-    public void close() {
-        this.cursor = null;
-        this.dataCollection = null;
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbc40Factory.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbc40Factory.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbc40Factory.java
deleted file mode 100644
index 235348b..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbc40Factory.java
+++ /dev/null
@@ -1,30 +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.kylin.jdbc;
-
-/**
- * @author xduo
- * 
- */
-public class KylinJdbc40Factory extends KylinJdbc41Factory {
-
-    public KylinJdbc40Factory() {
-        super(4, 0);
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbc41Factory.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbc41Factory.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbc41Factory.java
deleted file mode 100644
index 90ff2e2..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbc41Factory.java
+++ /dev/null
@@ -1,127 +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.kylin.jdbc;
-
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.util.List;
-import java.util.Properties;
-import java.util.TimeZone;
-
-import net.hydromatic.avatica.AvaticaConnection;
-import net.hydromatic.avatica.AvaticaDatabaseMetaData;
-import net.hydromatic.avatica.AvaticaFactory;
-import net.hydromatic.avatica.AvaticaPrepareResult;
-import net.hydromatic.avatica.AvaticaPreparedStatement;
-import net.hydromatic.avatica.AvaticaResultSet;
-import net.hydromatic.avatica.AvaticaResultSetMetaData;
-import net.hydromatic.avatica.AvaticaStatement;
-import net.hydromatic.avatica.ColumnMetaData;
-import net.hydromatic.avatica.UnregisteredDriver;
-
-import org.apache.kylin.jdbc.stub.KylinClient;
-import org.apache.kylin.jdbc.stub.RemoteClient;
-
-/**
- * Kylin JDBC factory.
- * 
- * @author xduo
- * 
- */
-public class KylinJdbc41Factory implements AvaticaFactory {
-    private final int major;
-    private final int minor;
-
-    /** Creates a JDBC factory. */
-    public KylinJdbc41Factory() {
-        this(4, 1);
-    }
-
-    /** Creates a JDBC factory with given major/minor version number. */
-    protected KylinJdbc41Factory(int major, int minor) {
-        this.major = major;
-        this.minor = minor;
-    }
-
-    public int getJdbcMajorVersion() {
-        return major;
-    }
-
-    public int getJdbcMinorVersion() {
-        return minor;
-    }
-
-    public AvaticaConnection newConnection(UnregisteredDriver driver, AvaticaFactory factory, String url, Properties info) {
-        return new KylinJdbc41Connection(driver, factory, url, info);
-    }
-
-    public AvaticaDatabaseMetaData newDatabaseMetaData(AvaticaConnection connection) {
-        return new AvaticaJdbc41DatabaseMetaData(connection);
-    }
-
-    public AvaticaStatement newStatement(AvaticaConnection connection, int resultSetType, int resultSetConcurrency, int resultSetHoldability) {
-        return new KylinJdbc41Statement(connection, resultSetType, resultSetConcurrency, resultSetHoldability);
-    }
-
-    public AvaticaPreparedStatement newPreparedStatement(AvaticaConnection connection, AvaticaPrepareResult prepareResult, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
-        return new KylinJdbc41PreparedStatement(connection, prepareResult, resultSetType, resultSetConcurrency, resultSetHoldability);
-    }
-
-    public AvaticaResultSet newResultSet(AvaticaStatement statement, AvaticaPrepareResult prepareResult, TimeZone timeZone) {
-        final ResultSetMetaData metaData = newResultSetMetaData(statement, prepareResult.getColumnList());
-        return new KylinResultSet(statement, prepareResult, metaData, timeZone);
-    }
-
-    public AvaticaResultSetMetaData newResultSetMetaData(AvaticaStatement statement, List<ColumnMetaData> columnMetaDataList) {
-        return new AvaticaResultSetMetaData(statement, null, columnMetaDataList);
-    }
-
-    // ~ kylin sepcified
-    public RemoteClient newRemoteClient(KylinConnectionImpl connection) {
-        return new KylinClient(connection);
-    }
-
-    /** Implementation of Connection for JDBC 4.1. */
-    private static class KylinJdbc41Connection extends KylinConnectionImpl {
-        KylinJdbc41Connection(UnregisteredDriver driver, AvaticaFactory factory, String url, Properties info) {
-            super(driver, (KylinJdbc41Factory) factory, url, info);
-        }
-    }
-
-    /** Implementation of Statement for JDBC 4.1. */
-    public static class KylinJdbc41Statement extends KylinStatementImpl {
-        public KylinJdbc41Statement(AvaticaConnection connection, int resultSetType, int resultSetConcurrency, int resultSetHoldability) {
-            super(connection, resultSetType, resultSetConcurrency, resultSetHoldability);
-        }
-    }
-
-    /** Implementation of PreparedStatement for JDBC 4.1. */
-    public static class KylinJdbc41PreparedStatement extends KylinPrepareStatementImpl {
-        KylinJdbc41PreparedStatement(AvaticaConnection connection, AvaticaPrepareResult sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
-            super(connection, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
-        }
-    }
-
-    /** Implementation of DatabaseMetaData for JDBC 4.1. */
-    private static class AvaticaJdbc41DatabaseMetaData extends AvaticaDatabaseMetaData {
-        AvaticaJdbc41DatabaseMetaData(AvaticaConnection connection) {
-            super(connection);
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbcFactory.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbcFactory.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbcFactory.java
new file mode 100644
index 0000000..030dbbb
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinJdbcFactory.java
@@ -0,0 +1,108 @@
+/*
+ * 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.kylin.jdbc;
+
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.Properties;
+import java.util.TimeZone;
+
+import org.apache.calcite.avatica.AvaticaConnection;
+import org.apache.calcite.avatica.AvaticaDatabaseMetaData;
+import org.apache.calcite.avatica.AvaticaFactory;
+import org.apache.calcite.avatica.AvaticaPreparedStatement;
+import org.apache.calcite.avatica.AvaticaResultSet;
+import org.apache.calcite.avatica.AvaticaResultSetMetaData;
+import org.apache.calcite.avatica.AvaticaStatement;
+import org.apache.calcite.avatica.Meta.Signature;
+import org.apache.calcite.avatica.Meta.StatementHandle;
+import org.apache.calcite.avatica.UnregisteredDriver;
+
+/**
+ * Kylin JDBC factory.
+ */
+public class KylinJdbcFactory implements AvaticaFactory {
+
+    public static class Version40 extends KylinJdbcFactory {
+        public Version40() {
+            super(4, 0);
+        }
+    }
+
+    public static class Version41 extends KylinJdbcFactory {
+        public Version41() {
+            super(4, 1);
+        }
+    }
+
+    final int major;
+    final int minor;
+
+    /** Creates a JDBC factory with given major/minor version number. */
+    protected KylinJdbcFactory(int major, int minor) {
+        this.major = major;
+        this.minor = minor;
+    }
+
+    @Override
+    public int getJdbcMajorVersion() {
+        return major;
+    }
+
+    @Override
+    public int getJdbcMinorVersion() {
+        return minor;
+    }
+
+    @Override
+    public AvaticaConnection newConnection(UnregisteredDriver driver, AvaticaFactory factory, String url, Properties info) throws SQLException {
+        return new KylinConnection(driver, (KylinJdbcFactory) factory, url, info);
+    }
+
+    @Override
+    public AvaticaDatabaseMetaData newDatabaseMetaData(AvaticaConnection connection) {
+        return new AvaticaDatabaseMetaData(connection) {
+        };
+    }
+
+    @Override
+    public AvaticaStatement newStatement(AvaticaConnection connection, StatementHandle h, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
+        return new KylinStatement((KylinConnection) connection, h, resultSetType, resultSetConcurrency, resultSetHoldability);
+    }
+
+    @Override
+    public AvaticaPreparedStatement newPreparedStatement(AvaticaConnection connection, StatementHandle h, Signature signature, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
+        return new KylinPreparedStatement((KylinConnection) connection, h, signature, resultSetType, resultSetConcurrency, resultSetHoldability);
+    }
+
+    @Override
+    public AvaticaResultSet newResultSet(AvaticaStatement statement, Signature signature, TimeZone timeZone, Iterable<Object> iterable) throws SQLException {
+        AvaticaResultSetMetaData resultSetMetaData = new AvaticaResultSetMetaData(statement, null, signature);
+        return new KylinResultSet(statement, signature, resultSetMetaData, timeZone, iterable);
+    }
+
+    @Override
+    public ResultSetMetaData newResultSetMetaData(AvaticaStatement statement, Signature signature) throws SQLException {
+        return new AvaticaResultSetMetaData(statement, null, signature);
+    }
+
+    public IRemoteClient newRemoteClient(KylinConnection conn) {
+        return new KylinClient(conn);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinMeta.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinMeta.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinMeta.java
new file mode 100644
index 0000000..72d7fb1
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinMeta.java
@@ -0,0 +1,341 @@
+/*
+ * 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.kylin.jdbc;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import org.apache.calcite.avatica.AvaticaUtils;
+import org.apache.calcite.avatica.ColumnMetaData;
+import org.apache.calcite.avatica.MetaImpl;
+
+/**
+ * Implementation of Avatica interface
+ */
+public class KylinMeta extends MetaImpl {
+
+    private KMetaProject metaProject;
+
+    public KylinMeta(KylinConnection connection) {
+        super(connection);
+    }
+
+    private KylinConnection connection() {
+        return (KylinConnection) connection;
+    }
+
+    // insert/update/delete go this path, ignorable for Kylin
+    @Override
+    public Signature prepare(StatementHandle h, String sql, int maxRowCount) {
+        return connection().mockPreparedSignature(sql);
+    }
+
+    // mimic from CalciteMetaImpl, real execution happens via callback in KylinResultSet.execute()
+    @Override
+    public MetaResultSet prepareAndExecute(StatementHandle h, String sql, int maxRowCount, PrepareCallback callback) {
+        final Signature signature;
+        try {
+            synchronized (callback.getMonitor()) {
+                callback.clear();
+                signature = prepare(h, sql, maxRowCount);
+                callback.assign(signature, null);
+            }
+            callback.execute();
+            return new MetaResultSet(h.id, false, signature, null);
+        } catch (SQLException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private KMetaProject getMetaProject() {
+        try {
+            if (metaProject == null) {
+                KylinConnection conn = connection();
+                metaProject = conn.getRemoteClient().retrieveMetaData(conn.getProject());
+            }
+            return metaProject;
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public MetaResultSet getTableTypes() {
+        return createResultSet(metaTableTypes, MetaTableType.class, "TABLE_TYPE");
+    }
+
+    @Override
+    public MetaResultSet getCatalogs() {
+        List<KMetaCatalog> catalogs = getMetaProject().catalogs;
+        return createResultSet(catalogs, MetaCatalog.class, "TABLE_CATALOG");
+    }
+
+    @Override
+    public MetaResultSet getSchemas(String catalog, Pat schemaPattern) {
+        List<KMetaSchema> schemas = getMetaProject().getSchemas(catalog, schemaPattern);
+        return createResultSet(schemas, MetaSchema.class, "TABLE_SCHEM", "TABLE_CATALOG");
+    }
+
+    @Override
+    public MetaResultSet getTables(String catalog, Pat schemaPattern, Pat tableNamePattern, List<String> typeList) {
+        List<KMetaTable> tables = getMetaProject().getTables(catalog, schemaPattern, tableNamePattern, typeList);
+        return createResultSet(tables, MetaTable.class, //
+                "TABLE_CAT", //
+                "TABLE_SCHEM", //
+                "TABLE_NAME", //
+                "TABLE_TYPE", //
+                "REMARKS", //
+                "TYPE_CAT", //
+                "TYPE_SCHEM", //
+                "TYPE_NAME", //
+                "SELF_REFERENCING_COL_NAME", //
+                "REF_GENERATION");
+    }
+
+    @Override
+    public MetaResultSet getColumns(String catalog, Pat schemaPattern, Pat tableNamePattern, Pat columnNamePattern) {
+        List<KMetaColumn> columns = getMetaProject().getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern);
+        return createResultSet(columns, MetaColumn.class, //
+                "TABLE_CAT", //
+                "TABLE_SCHEM", //
+                "TABLE_NAME", //
+                "COLUMN_NAME", //
+                "DATA_TYPE", //
+                "TYPE_NAME", //
+                "COLUMN_SIZE", //
+                "BUFFER_LENGTH", //
+                "DECIMAL_DIGITS", //
+                "NUM_PREC_RADIX", //
+                "NULLABLE", //
+                "REMARKS", //
+                "COLUMN_DEF", //
+                "SQL_DATA_TYPE", //
+                "SQL_DATETIME_SUB", //
+                "CHAR_OCTET_LENGTH", //
+                "ORDINAL_POSITION", //
+                "IS_NULLABLE", //
+                "SCOPE_CATALOG", //
+                "SCOPE_TABLE", //
+                "SOURCE_DATA_TYPE", //
+                "IS_AUTOINCREMENT", //
+                "IS_GENERATEDCOLUMN");
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    private MetaResultSet createResultSet(List iterable, Class clazz, String... names) {
+        final List<ColumnMetaData> columns = new ArrayList<ColumnMetaData>();
+        final List<Field> fields = new ArrayList<Field>();
+        final List<String> fieldNames = new ArrayList<String>();
+        for (String name : names) {
+            final int index = fields.size();
+            final String fieldName = AvaticaUtils.toCamelCase(name);
+            final Field field;
+            try {
+                field = clazz.getField(fieldName);
+            } catch (NoSuchFieldException e) {
+                throw new RuntimeException(e);
+            }
+            columns.add(columnMetaData(name, index, field.getType()));
+            fields.add(field);
+            fieldNames.add(fieldName);
+        }
+
+        CursorFactory cursorFactory = CursorFactory.record(clazz, fields, fieldNames);
+        Signature signature = new Signature(columns, "", null, Collections.<String, Object> emptyMap(), cursorFactory);
+        StatementHandle statementHandle = this.createStatement(null);
+
+        return new MetaResultSet(statementHandle.id, true, signature, iterable);
+    }
+
+    // ============================================================================
+
+    public static interface NamedWithChildren extends Named {
+        List<? extends NamedWithChildren> getChildren();
+    }
+
+    public static List<? extends NamedWithChildren> searchByPatterns(NamedWithChildren parent, Pat... patterns) {
+        assert patterns != null && patterns.length > 0;
+        
+        List<? extends NamedWithChildren> children = findChildren(parent, patterns[0]);
+        if (patterns.length == 1) {
+            return children;
+        } else {
+            List<NamedWithChildren> result = new ArrayList<NamedWithChildren>();
+            Pat[] subPatterns = Arrays.copyOfRange(patterns, 1, patterns.length);
+            for (NamedWithChildren c : children) {
+                result.addAll(searchByPatterns(c, subPatterns));
+            }
+            return result;
+        }
+    }
+
+    private static List<? extends NamedWithChildren> findChildren(NamedWithChildren parent, Pat pattern) {
+        if (null == pattern.s || pattern.s.equals("%")) {
+            return parent.getChildren();
+        }
+
+        List<NamedWithChildren> result = new ArrayList<NamedWithChildren>();
+        Pattern regex = likeToRegex(pattern);
+
+        for (NamedWithChildren c : parent.getChildren()) {
+            if (regex.matcher(c.getName()).matches()) {
+                result.add(c);
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Converts a LIKE-style pattern (where '%' represents a wild-card,
+     * escaped using '\') to a Java regex.
+     */
+    private static Pattern likeToRegex(Pat pattern) {
+        StringBuilder buf = new StringBuilder("^");
+        char[] charArray = pattern.s.toCharArray();
+        int slash = -2;
+        for (int i = 0; i < charArray.length; i++) {
+            char c = charArray[i];
+            if (slash == i - 1) {
+                buf.append('[').append(c).append(']');
+            } else {
+                switch (c) {
+                case '\\':
+                    slash = i;
+                    break;
+                case '%':
+                    buf.append(".*");
+                    break;
+                case '[':
+                    buf.append("\\[");
+                    break;
+                case ']':
+                    buf.append("\\]");
+                    break;
+                default:
+                    buf.append('[').append(c).append(']');
+                }
+            }
+        }
+        buf.append("$");
+
+        return Pattern.compile(buf.toString());
+    }
+
+    // ============================================================================
+
+    private static final List<MetaTableType> metaTableTypes = new ArrayList<MetaTableType>();
+    static {
+        metaTableTypes.add(new MetaTableType("TABLE"));
+    }
+
+    public static class KMetaProject implements NamedWithChildren {
+        public final String projectName;
+        public final List<KMetaCatalog> catalogs;
+
+        public KMetaProject(String projectName, List<KMetaCatalog> catalogs) {
+            this.projectName = projectName;
+            this.catalogs = catalogs;
+        }
+
+        @SuppressWarnings("unchecked")
+        public List<KMetaSchema> getSchemas(String catalog, Pat schemaPattern) {
+            return (List<KMetaSchema>) searchByPatterns(this, Pat.of(catalog), schemaPattern);
+        }
+
+        @SuppressWarnings("unchecked")
+        public List<KMetaTable> getTables(String catalog, Pat schemaPattern, Pat tableNamePattern, List<String> typeList) {
+            return (List<KMetaTable>) searchByPatterns(this, Pat.of(catalog), schemaPattern, tableNamePattern);
+        }
+        
+        @SuppressWarnings("unchecked")
+        public List<KMetaColumn> getColumns(String catalog, Pat schemaPattern, Pat tableNamePattern, Pat columnNamePattern) {
+            return (List<KMetaColumn>) searchByPatterns(this, Pat.of(catalog), schemaPattern, tableNamePattern, columnNamePattern);
+        }
+        
+        @Override
+        public String getName() {
+            return projectName;
+        }
+
+        @Override
+        public List<? extends NamedWithChildren> getChildren() {
+            return catalogs;
+        }
+    }
+
+    public static class KMetaCatalog extends MetaCatalog implements NamedWithChildren {
+        public final List<KMetaSchema> schemas;
+
+        public KMetaCatalog(String tableCatalog, List<KMetaSchema> schemas) {
+            super(tableCatalog);
+            this.schemas = schemas;
+        }
+
+        @Override
+        public List<? extends NamedWithChildren> getChildren() {
+            return schemas;
+        }
+    }
+
+    public static class KMetaSchema extends MetaSchema implements NamedWithChildren {
+        public final List<KMetaTable> tables;
+
+        public KMetaSchema(String tableCatalog, String tableSchem, List<KMetaTable> tables) {
+            super(tableCatalog, tableSchem);
+            this.tables = tables;
+        }
+
+        @Override
+        public List<? extends NamedWithChildren> getChildren() {
+            return tables;
+        }
+    }
+
+    public static class KMetaTable extends MetaTable implements NamedWithChildren {
+        public final List<KMetaColumn> columns;
+
+        public KMetaTable(String tableCat, String tableSchem, String tableName, String tableType, List<KMetaColumn> columns) {
+            super(tableCat, tableSchem, tableName, tableType);
+            this.columns = columns;
+        }
+
+        @Override
+        public List<? extends NamedWithChildren> getChildren() {
+            return columns;
+        }
+    }
+
+    public static class KMetaColumn extends MetaColumn implements NamedWithChildren {
+
+        public KMetaColumn(String tableCat, String tableSchem, String tableName, String columnName, int dataType, String typeName, int columnSize, Integer decimalDigits, int numPrecRadix, int nullable, int charOctetLength, int ordinalPosition, String isNullable) {
+            super(tableCat, tableSchem, tableName, columnName, dataType, typeName, columnSize, decimalDigits, numPrecRadix, nullable, charOctetLength, ordinalPosition, isNullable);
+        }
+
+        @Override
+        public List<NamedWithChildren> getChildren() {
+            return Collections.<NamedWithChildren> emptyList();
+        }
+    }
+}


[02/10] incubator-kylin git commit: KYLIN-780 Upgrade query module, all query test pass

Posted by li...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/test/java/org/apache/kylin/jdbc/DummyJdbc41Factory.java
----------------------------------------------------------------------
diff --git a/jdbc/src/test/java/org/apache/kylin/jdbc/DummyJdbc41Factory.java b/jdbc/src/test/java/org/apache/kylin/jdbc/DummyJdbc41Factory.java
deleted file mode 100644
index df0b49c..0000000
--- a/jdbc/src/test/java/org/apache/kylin/jdbc/DummyJdbc41Factory.java
+++ /dev/null
@@ -1,35 +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.kylin.jdbc;
-
-import org.apache.kylin.jdbc.stub.RemoteClient;
-
-/**
- * @author xduo
- * 
- */
-public class DummyJdbc41Factory extends KylinJdbc41Factory {
-
-    // ~ kylin sepcified
-    @Override
-    public RemoteClient newRemoteClient(KylinConnectionImpl connection) {
-        return new DummyClient(connection);
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/test/java/org/apache/kylin/jdbc/DummyJdbcFactory.java
----------------------------------------------------------------------
diff --git a/jdbc/src/test/java/org/apache/kylin/jdbc/DummyJdbcFactory.java b/jdbc/src/test/java/org/apache/kylin/jdbc/DummyJdbcFactory.java
new file mode 100644
index 0000000..578e594
--- /dev/null
+++ b/jdbc/src/test/java/org/apache/kylin/jdbc/DummyJdbcFactory.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.kylin.jdbc;
+
+import org.apache.kylin.jdbc.IRemoteClient;
+import org.apache.kylin.jdbc.KylinConnection;
+import org.apache.kylin.jdbc.KylinJdbcFactory;
+
+/**
+ */
+public class DummyJdbcFactory extends KylinJdbcFactory {
+    
+    public DummyJdbcFactory() {
+        super(4, 1);
+    }
+
+    @Override
+    public IRemoteClient newRemoteClient(KylinConnection conn) {
+        return new DummyClient(conn);
+    }
+
+    
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/metadata/src/main/java/org/apache/kylin/metadata/model/ColumnDesc.java
----------------------------------------------------------------------
diff --git a/metadata/src/main/java/org/apache/kylin/metadata/model/ColumnDesc.java b/metadata/src/main/java/org/apache/kylin/metadata/model/ColumnDesc.java
index 11564d2..6392eca 100644
--- a/metadata/src/main/java/org/apache/kylin/metadata/model/ColumnDesc.java
+++ b/metadata/src/main/java/org/apache/kylin/metadata/model/ColumnDesc.java
@@ -19,8 +19,6 @@
 package org.apache.kylin.metadata.model;
 
 import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import com.fasterxml.jackson.annotation.JsonAutoDetect;
 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
@@ -33,8 +31,6 @@ import com.fasterxml.jackson.annotation.JsonProperty;
 @JsonAutoDetect(fieldVisibility = Visibility.NONE, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
 public class ColumnDesc {
 
-    private static final Logger logger = LoggerFactory.getLogger(ColumnDesc.class);
-
     @JsonProperty("id")
     private String id;
     @JsonProperty("name")

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/metadata/src/main/java/org/apache/kylin/metadata/model/DataType.java
----------------------------------------------------------------------
diff --git a/metadata/src/main/java/org/apache/kylin/metadata/model/DataType.java b/metadata/src/main/java/org/apache/kylin/metadata/model/DataType.java
index 027b348..c1b1d23 100644
--- a/metadata/src/main/java/org/apache/kylin/metadata/model/DataType.java
+++ b/metadata/src/main/java/org/apache/kylin/metadata/model/DataType.java
@@ -1,304 +1,306 @@
-/*
- * 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.kylin.metadata.model;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * @author yangli9
- * 
- */
-public class DataType {
-
-    public static final String VALID_TYPES_STRING = "any|char|varchar|boolean|binary" //
-            + "|integer|tinyint|smallint|bigint|decimal|numeric|float|real|double" //
-            + "|date|time|datetime|timestamp|byte|int|short|long|string|hllc" //
-            + "|" + TblColRef.InnerDataTypeEnum.LITERAL.getDataType() //
-            + "|" + TblColRef.InnerDataTypeEnum.DERIVED.getDataType();
-
-    private static final Pattern TYPE_PATTERN = Pattern.compile(
-    // standard sql types, ref:
-    // http://www.w3schools.com/sql/sql_datatypes_general.asp
-            "(" + VALID_TYPES_STRING + ")" + "\\s*" //
-                    + "(?:" + "[(]" + "([\\d\\s,]+)" + "[)]" + ")?", Pattern.CASE_INSENSITIVE);
-
-    public static final Set<String> INTEGER_FAMILY = new HashSet<String>();
-    public static final Set<String> NUMBER_FAMILY = new HashSet<String>();
-    public static final Set<String> DATETIME_FAMILY = new HashSet<String>();
-    public static final Set<String> STRING_FAMILY = new HashSet<String>();
-    private static final Set<Integer> HLLC_PRECISIONS = new HashSet<Integer>();
-    private static final Map<String, String> LEGACY_TYPE_MAP = new HashMap<String, String>();
-    static {
-        INTEGER_FAMILY.add("tinyint");
-        INTEGER_FAMILY.add("smallint");
-        INTEGER_FAMILY.add("integer");
-        INTEGER_FAMILY.add("bigint");
-
-        NUMBER_FAMILY.addAll(INTEGER_FAMILY);
-        NUMBER_FAMILY.add("float");
-        NUMBER_FAMILY.add("double");
-        NUMBER_FAMILY.add("decimal");
-        NUMBER_FAMILY.add("real");
-        NUMBER_FAMILY.add("numeric");
-
-        DATETIME_FAMILY.add("date");
-        DATETIME_FAMILY.add("time");
-        DATETIME_FAMILY.add("datetime");
-        DATETIME_FAMILY.add("timestamp");
-
-        STRING_FAMILY.add("varchar");
-        STRING_FAMILY.add("char");
-
-        LEGACY_TYPE_MAP.put("byte", "tinyint");
-        LEGACY_TYPE_MAP.put("int", "integer");
-        LEGACY_TYPE_MAP.put("short", "smallint");
-        LEGACY_TYPE_MAP.put("long", "bigint");
-        LEGACY_TYPE_MAP.put("string", "varchar");
-        LEGACY_TYPE_MAP.put("hllc10", "hllc(10)");
-        LEGACY_TYPE_MAP.put("hllc12", "hllc(12)");
-        LEGACY_TYPE_MAP.put("hllc14", "hllc(14)");
-        LEGACY_TYPE_MAP.put("hllc15", "hllc(15)");
-        LEGACY_TYPE_MAP.put("hllc16", "hllc(16)");
-
-        for (int i = 10; i <= 16; i++)
-            HLLC_PRECISIONS.add(i);
-    }
-
-    private static final ConcurrentMap<DataType, DataType> CACHE = new ConcurrentHashMap<DataType, DataType>();
-
-    public static DataType getInstance(String type) {
-        if (type == null)
-            return null;
-
-        DataType dataType = new DataType(type);
-        DataType cached = CACHE.get(dataType);
-        if (cached == null) {
-            CACHE.put(dataType, dataType);
-            cached = dataType;
-        }
-        return cached;
-    }
-
-    // ============================================================================
-
-    private String name;
-    private int precision;
-    private int scale;
-
-    DataType(String datatype) {
-        parseDataType(datatype);
-    }
-
-    private void parseDataType(String datatype) {
-        datatype = datatype.trim().toLowerCase();
-        datatype = replaceLegacy(datatype);
-
-        Matcher m = TYPE_PATTERN.matcher(datatype);
-        if (m.matches() == false)
-            throw new IllegalArgumentException("bad data type -- " + datatype + ", does not match " + TYPE_PATTERN);
-
-        name = replaceLegacy(m.group(1));
-        precision = -1;
-        scale = -1;
-
-        String leftover = m.group(2);
-        if (leftover != null) {
-            String[] parts = leftover.split("\\s*,\\s*");
-            for (int i = 0; i < parts.length; i++) {
-                int n;
-                try {
-                    n = Integer.parseInt(parts[i]);
-                } catch (NumberFormatException e) {
-                    throw new IllegalArgumentException("bad data type -- " + datatype + ", precision/scale not numeric");
-                }
-                if (i == 0)
-                    precision = n;
-                else if (i == 1)
-                    scale = n;
-                else
-                    throw new IllegalArgumentException("bad data type -- " + datatype + ", too many precision/scale parts");
-            }
-        }
-
-        // FIXME 256 for unknown string precision
-        if ((name.equals("char") || name.equals("varchar")) && precision == -1) {
-            precision = 256; // to save memory at frontend, e.g. tableau will
-                             // allocate memory according to this
-        }
-
-        // FIXME (19,4) for unknown decimal precision
-        if ((name.equals("decimal") || name.equals("numeric")) && precision == -1) {
-            precision = 19;
-            scale = 4;
-        }
-
-        if (isHLLC() && HLLC_PRECISIONS.contains(precision) == false)
-            throw new IllegalArgumentException("HLLC precision must be one of " + HLLC_PRECISIONS);
-    }
-
-    private String replaceLegacy(String str) {
-        String replace = LEGACY_TYPE_MAP.get(str);
-        return replace == null ? str : replace;
-    }
-
-    public int getSpaceEstimate() {
-        if (isTinyInt()) {
-            return 1;
-        } else if (isSmallInt()) {
-            return 2;
-        } else if (isInt()) {
-            return 4;
-        } else if (isBigInt()) {
-            return 8;
-        } else if (isFloat()) {
-            return 4;
-        } else if (isDouble()) {
-            return 8;
-        } else if (isDecimal()) {
-            return 8;
-        } else if (isHLLC()) {
-            return 1 << precision;
-        }
-        throw new IllegalStateException("The return type : " + name + " is not recognized;");
-    }
-
-    public boolean isStringFamily() {
-        return STRING_FAMILY.contains(name);
-    }
-
-    public boolean isIntegerFamily() {
-        return INTEGER_FAMILY.contains(name);
-    }
-
-    public boolean isNumberFamily() {
-        return NUMBER_FAMILY.contains(name);
-    }
-
-    public boolean isDateTimeFamily() {
-        return DATETIME_FAMILY.contains(name);
-    }
-
-    public boolean isDate() {
-        return name.equals("date");
-    }
-
-    public boolean isTime() {
-        return name.equals("time");
-    }
-
-    public boolean isTimestamp() {
-        return name.equals("timestamp");
-    }
-
-    public boolean isDatetime() {
-        return name.equals("datetime");
-    }
-
-    public boolean isTinyInt() {
-        return name.equals("tinyint");
-    }
-
-    public boolean isSmallInt() {
-        return name.equals("smallint");
-    }
-
-    public boolean isInt() {
-        return name.equals("integer");
-    }
-
-    public boolean isBigInt() {
-        return name.equals("bigint");
-    }
-
-    public boolean isFloat() {
-        return name.equals("float");
-    }
-
-    public boolean isDouble() {
-        return name.equals("double");
-    }
-
-    public boolean isDecimal() {
-        return name.equals("decimal");
-    }
-
-    public boolean isHLLC() {
-        return name.equals("hllc");
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public int getPrecision() {
-        return precision;
-    }
-
-    public int getScale() {
-        return scale;
-    }
-
-    @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + ((name == null) ? 0 : name.hashCode());
-        result = prime * result + precision;
-        result = prime * result + scale;
-        return result;
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj)
-            return true;
-        if (obj == null)
-            return false;
-        if (getClass() != obj.getClass())
-            return false;
-        DataType other = (DataType) obj;
-        if (name == null) {
-            if (other.name != null)
-                return false;
-        } else if (!name.equals(other.name))
-            return false;
-        if (precision != other.precision)
-            return false;
-        if (scale != other.scale)
-            return false;
-        return true;
-    }
-
-    @Override
-    public String toString() {
-        if (precision < 0 && scale < 0)
-            return name;
-        else if (scale < 0)
-            return name + "(" + precision + ")";
-        else
-            return name + "(" + precision + "," + scale + ")";
-    }
-}
+/*
+ * 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.kylin.metadata.model;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author yangli9
+ * 
+ */
+public class DataType {
+    
+    public static final String VALID_TYPES_STRING = "any|char|varchar|boolean|binary" //
+            + "|integer|tinyint|smallint|bigint|decimal|numeric|float|real|double" //
+            + "|date|time|datetime|timestamp|byte|int|short|long|string|hllc" //
+            + "|" + TblColRef.InnerDataTypeEnum.LITERAL.getDataType() //
+            + "|" + TblColRef.InnerDataTypeEnum.DERIVED.getDataType();
+
+    private static final Pattern TYPE_PATTERN = Pattern.compile(
+    // standard sql types, ref:
+    // http://www.w3schools.com/sql/sql_datatypes_general.asp
+            "(" + VALID_TYPES_STRING + ")" + "\\s*" //
+                    + "(?:" + "[(]" + "([\\d\\s,]+)" + "[)]" + ")?", Pattern.CASE_INSENSITIVE);
+
+    public static final Set<String> INTEGER_FAMILY = new HashSet<String>();
+    public static final Set<String> NUMBER_FAMILY = new HashSet<String>();
+    public static final Set<String> DATETIME_FAMILY = new HashSet<String>();
+    public static final Set<String> STRING_FAMILY = new HashSet<String>();
+    private static final Set<Integer> HLLC_PRECISIONS = new HashSet<Integer>();
+    private static final Map<String, String> LEGACY_TYPE_MAP = new HashMap<String, String>();
+    static {
+        INTEGER_FAMILY.add("tinyint");
+        INTEGER_FAMILY.add("smallint");
+        INTEGER_FAMILY.add("integer");
+        INTEGER_FAMILY.add("bigint");
+
+        NUMBER_FAMILY.addAll(INTEGER_FAMILY);
+        NUMBER_FAMILY.add("float");
+        NUMBER_FAMILY.add("double");
+        NUMBER_FAMILY.add("decimal");
+        NUMBER_FAMILY.add("real");
+        NUMBER_FAMILY.add("numeric");
+
+        DATETIME_FAMILY.add("date");
+        DATETIME_FAMILY.add("time");
+        DATETIME_FAMILY.add("datetime");
+        DATETIME_FAMILY.add("timestamp");
+
+        STRING_FAMILY.add("varchar");
+        STRING_FAMILY.add("char");
+
+        LEGACY_TYPE_MAP.put("byte", "tinyint");
+        LEGACY_TYPE_MAP.put("int", "integer");
+        LEGACY_TYPE_MAP.put("short", "smallint");
+        LEGACY_TYPE_MAP.put("long", "bigint");
+        LEGACY_TYPE_MAP.put("string", "varchar");
+        LEGACY_TYPE_MAP.put("hllc10", "hllc(10)");
+        LEGACY_TYPE_MAP.put("hllc12", "hllc(12)");
+        LEGACY_TYPE_MAP.put("hllc14", "hllc(14)");
+        LEGACY_TYPE_MAP.put("hllc15", "hllc(15)");
+        LEGACY_TYPE_MAP.put("hllc16", "hllc(16)");
+
+        for (int i = 10; i <= 16; i++)
+            HLLC_PRECISIONS.add(i);
+    }
+
+    private static final ConcurrentMap<DataType, DataType> CACHE = new ConcurrentHashMap<DataType, DataType>();
+    
+    public static final DataType ANY = DataType.getInstance("any");
+
+    public static DataType getInstance(String type) {
+        if (type == null)
+            return null;
+
+        DataType dataType = new DataType(type);
+        DataType cached = CACHE.get(dataType);
+        if (cached == null) {
+            CACHE.put(dataType, dataType);
+            cached = dataType;
+        }
+        return cached;
+    }
+
+    // ============================================================================
+
+    private String name;
+    private int precision;
+    private int scale;
+
+    DataType(String datatype) {
+        parseDataType(datatype);
+    }
+
+    private void parseDataType(String datatype) {
+        datatype = datatype.trim().toLowerCase();
+        datatype = replaceLegacy(datatype);
+
+        Matcher m = TYPE_PATTERN.matcher(datatype);
+        if (m.matches() == false)
+            throw new IllegalArgumentException("bad data type -- " + datatype + ", does not match " + TYPE_PATTERN);
+
+        name = replaceLegacy(m.group(1));
+        precision = -1;
+        scale = -1;
+
+        String leftover = m.group(2);
+        if (leftover != null) {
+            String[] parts = leftover.split("\\s*,\\s*");
+            for (int i = 0; i < parts.length; i++) {
+                int n;
+                try {
+                    n = Integer.parseInt(parts[i]);
+                } catch (NumberFormatException e) {
+                    throw new IllegalArgumentException("bad data type -- " + datatype + ", precision/scale not numeric");
+                }
+                if (i == 0)
+                    precision = n;
+                else if (i == 1)
+                    scale = n;
+                else
+                    throw new IllegalArgumentException("bad data type -- " + datatype + ", too many precision/scale parts");
+            }
+        }
+
+        // FIXME 256 for unknown string precision
+        if ((name.equals("char") || name.equals("varchar")) && precision == -1) {
+            precision = 256; // to save memory at frontend, e.g. tableau will
+                             // allocate memory according to this
+        }
+
+        // FIXME (19,4) for unknown decimal precision
+        if ((name.equals("decimal") || name.equals("numeric")) && precision == -1) {
+            precision = 19;
+            scale = 4;
+        }
+
+        if (isHLLC() && HLLC_PRECISIONS.contains(precision) == false)
+            throw new IllegalArgumentException("HLLC precision must be one of " + HLLC_PRECISIONS);
+    }
+
+    private String replaceLegacy(String str) {
+        String replace = LEGACY_TYPE_MAP.get(str);
+        return replace == null ? str : replace;
+    }
+
+    public int getSpaceEstimate() {
+        if (isTinyInt()) {
+            return 1;
+        } else if (isSmallInt()) {
+            return 2;
+        } else if (isInt()) {
+            return 4;
+        } else if (isBigInt()) {
+            return 8;
+        } else if (isFloat()) {
+            return 4;
+        } else if (isDouble()) {
+            return 8;
+        } else if (isDecimal()) {
+            return 8;
+        } else if (isHLLC()) {
+            return 1 << precision;
+        }
+        throw new IllegalStateException("The return type : " + name + " is not recognized;");
+    }
+
+    public boolean isStringFamily() {
+        return STRING_FAMILY.contains(name);
+    }
+
+    public boolean isIntegerFamily() {
+        return INTEGER_FAMILY.contains(name);
+    }
+
+    public boolean isNumberFamily() {
+        return NUMBER_FAMILY.contains(name);
+    }
+
+    public boolean isDateTimeFamily() {
+        return DATETIME_FAMILY.contains(name);
+    }
+
+    public boolean isDate() {
+        return name.equals("date");
+    }
+
+    public boolean isTime() {
+        return name.equals("time");
+    }
+
+    public boolean isTimestamp() {
+        return name.equals("timestamp");
+    }
+
+    public boolean isDatetime() {
+        return name.equals("datetime");
+    }
+
+    public boolean isTinyInt() {
+        return name.equals("tinyint");
+    }
+
+    public boolean isSmallInt() {
+        return name.equals("smallint");
+    }
+
+    public boolean isInt() {
+        return name.equals("integer");
+    }
+
+    public boolean isBigInt() {
+        return name.equals("bigint");
+    }
+
+    public boolean isFloat() {
+        return name.equals("float");
+    }
+
+    public boolean isDouble() {
+        return name.equals("double");
+    }
+
+    public boolean isDecimal() {
+        return name.equals("decimal");
+    }
+
+    public boolean isHLLC() {
+        return name.equals("hllc");
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public int getPrecision() {
+        return precision;
+    }
+
+    public int getScale() {
+        return scale;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((name == null) ? 0 : name.hashCode());
+        result = prime * result + precision;
+        result = prime * result + scale;
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        DataType other = (DataType) obj;
+        if (name == null) {
+            if (other.name != null)
+                return false;
+        } else if (!name.equals(other.name))
+            return false;
+        if (precision != other.precision)
+            return false;
+        if (scale != other.scale)
+            return false;
+        return true;
+    }
+
+    @Override
+    public String toString() {
+        if (precision < 0 && scale < 0)
+            return name;
+        else if (scale < 0)
+            return name + "(" + precision + ")";
+        else
+            return name + "(" + precision + "," + scale + ")";
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/metadata/src/main/java/org/apache/kylin/metadata/model/FunctionDesc.java
----------------------------------------------------------------------
diff --git a/metadata/src/main/java/org/apache/kylin/metadata/model/FunctionDesc.java b/metadata/src/main/java/org/apache/kylin/metadata/model/FunctionDesc.java
index ab4d1cc..806874d 100644
--- a/metadata/src/main/java/org/apache/kylin/metadata/model/FunctionDesc.java
+++ b/metadata/src/main/java/org/apache/kylin/metadata/model/FunctionDesc.java
@@ -61,12 +61,13 @@ public class FunctionDesc {
     public boolean needRewrite() {
         return !isSum() && !isDimensionAsMetric();
     }
-    
+
     public ColumnDesc newFakeRewriteColumn(TableDesc sourceTable) {
         ColumnDesc fakeCol = new ColumnDesc();
         fakeCol.setName(getRewriteFieldName());
-        fakeCol.setDatatype(getSQLType());
-        fakeCol.setNullable(false);
+        fakeCol.setDatatype(getSQLType().toString());
+        if (isCount())
+            fakeCol.setNullable(false);
         fakeCol.init(sourceTable);
         return fakeCol;
     }
@@ -144,13 +145,13 @@ public class FunctionDesc {
         this.returnDataType = returnDataType;
     }
 
-    public String getSQLType() {
+    public DataType getSQLType() {
         if (isCountDistinct())
-            return "any";
+            return DataType.ANY;
         else if (isSum() || isMax() || isMin())
-            return parameter.getColRefs().get(0).getType().getName();
+            return parameter.getColRefs().get(0).getType();
         else
-            return returnType;
+            return returnDataType;
     }
 
     public String getReturnType() {

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 28357cd..1827ccf 100644
--- a/pom.xml
+++ b/pom.xml
@@ -85,9 +85,8 @@
         <!-- REST Service -->
         <spring.framework.version>3.1.2.RELEASE</spring.framework.version>
 
-        <!-- Optiq Version -->
-        <optiq.version>0.9.2-incubating</optiq.version>
-        <linq4j.version>0.4</linq4j.version>
+        <!-- Calcite Version -->
+        <calcite.version>1.0.0-incubating</calcite.version>
 
         <!-- Metrics Codahale Version -->
         <metrics.version>3.0.1</metrics.version>
@@ -101,7 +100,7 @@
         <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
         <sonar.language>java</sonar.language>
         <sonar.jacoco.excludes>
-            org/apache/kylin/**/tools/**:**/*CLI.java:net/hydromatic/optiq/**:org/eigenbase/sql2rel/**
+            org/apache/kylin/**/tools/**:**/*CLI.java
         </sonar.jacoco.excludes>
 
     </properties>
@@ -247,21 +246,21 @@
                 <version>${yarn.version}</version>
             </dependency>
 
-            <!-- optiq dependencies -->
+            <!-- Calcite dependencies -->
             <dependency>
                 <groupId>org.apache.calcite</groupId>
                 <artifactId>calcite-core</artifactId>
-                <version>${optiq.version}</version>
+                <version>${calcite.version}</version>
             </dependency>
             <dependency>
                 <groupId>org.apache.calcite</groupId>
                 <artifactId>calcite-avatica</artifactId>
-                <version>${optiq.version}</version>
+                <version>${calcite.version}</version>
             </dependency>
             <dependency>
-                <groupId>net.hydromatic</groupId>
-                <artifactId>linq4j</artifactId>
-                <version>${linq4j.version}</version>
+                <groupId>org.apache.calcite</groupId>
+                <artifactId>calcite-linq4j</artifactId>
+                <version>${calcite.version}</version>
             </dependency>
 
             <!-- Other dependencies -->

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/pom.xml
----------------------------------------------------------------------
diff --git a/query/pom.xml b/query/pom.xml
index 3881b4d..86fca54 100644
--- a/query/pom.xml
+++ b/query/pom.xml
@@ -23,7 +23,7 @@
     <artifactId>kylin-query</artifactId>
     <packaging>jar</packaging>
     <name>Kylin:Query</name>
-    <description>kylin query engine based on optiq</description>
+    <description>kylin query engine based on Calcite</description>
 
     <parent>
         <groupId>org.apache.kylin</groupId>
@@ -54,8 +54,8 @@
             <version>${project.parent.version}</version>
         </dependency>
         <dependency>
-            <groupId>net.hydromatic</groupId>
-            <artifactId>linq4j</artifactId>
+            <groupId>org.apache.calcite</groupId>
+            <artifactId>calcite-linq4j</artifactId>
         </dependency>
         <dependency>
             <groupId>log4j</groupId>

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/QueryCli.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/QueryCli.java b/query/src/main/java/org/apache/kylin/query/QueryCli.java
index fe02b2f..898cb22 100644
--- a/query/src/main/java/org/apache/kylin/query/QueryCli.java
+++ b/query/src/main/java/org/apache/kylin/query/QueryCli.java
@@ -25,13 +25,13 @@ import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.Statement;
 
+import org.apache.calcite.jdbc.Driver;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
 import org.apache.commons.cli.GnuParser;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
-
 import org.apache.kylin.common.KylinConfig;
 import org.apache.kylin.query.schema.OLAPSchemaFactory;
 
@@ -54,7 +54,7 @@ public class QueryCli {
         KylinConfig config = KylinConfig.createInstanceFromUri(commandLine.getOptionValue(OPTION_METADATA.getOpt()));
         String sql = commandLine.getOptionValue(OPTION_SQL.getOpt());
 
-        Class.forName("net.hydromatic.optiq.jdbc.Driver");
+        Class.forName(Driver.class.getName());
         File olapTmp = OLAPSchemaFactory.createTempOLAPJson(null, config);
 
         Connection conn = null;

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/enumerator/HiveEnumerator.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/enumerator/HiveEnumerator.java b/query/src/main/java/org/apache/kylin/query/enumerator/HiveEnumerator.java
index cad1624..b0113f9 100644
--- a/query/src/main/java/org/apache/kylin/query/enumerator/HiveEnumerator.java
+++ b/query/src/main/java/org/apache/kylin/query/enumerator/HiveEnumerator.java
@@ -25,15 +25,11 @@ import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.List;
 
-import net.hydromatic.linq4j.Enumerator;
-
+import org.apache.calcite.linq4j.Enumerator;
 import org.apache.kylin.query.relnode.OLAPContext;
 
 /**
  * Hive Query Result Enumerator
- * 
- * @author xjiang
- * 
  */
 public class HiveEnumerator implements Enumerator<Object[]> {
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/enumerator/LookupTableEnumerator.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/enumerator/LookupTableEnumerator.java b/query/src/main/java/org/apache/kylin/query/enumerator/LookupTableEnumerator.java
index 0618818..b19061b 100644
--- a/query/src/main/java/org/apache/kylin/query/enumerator/LookupTableEnumerator.java
+++ b/query/src/main/java/org/apache/kylin/query/enumerator/LookupTableEnumerator.java
@@ -1,105 +1,106 @@
-/*
- * 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.kylin.query.enumerator;
-
-import net.hydromatic.linq4j.Enumerator;
-import org.apache.kylin.cube.CubeInstance;
-import org.apache.kylin.cube.CubeManager;
-import org.apache.kylin.cube.model.DimensionDesc;
-import org.apache.kylin.dict.lookup.LookupStringTable;
-import org.apache.kylin.metadata.model.ColumnDesc;
-import org.apache.kylin.query.relnode.OLAPContext;
-import org.apache.kylin.query.schema.OLAPTable;
-import org.apache.kylin.storage.hybrid.HybridInstance;
-import org.apache.kylin.storage.tuple.Tuple;
-
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * @author yangli9
- */
-public class LookupTableEnumerator implements Enumerator<Object[]> {
-
-    private final Collection<String[]> allRows;
-    private final List<ColumnDesc> colDescs;
-    private final Object[] current;
-    private Iterator<String[]> iterator;
-
-    public LookupTableEnumerator(OLAPContext olapContext) {
-
-        //TODO: assuming LookupTableEnumerator is handled by a cube
-        CubeInstance cube = null;
-
-        if (olapContext.realization instanceof CubeInstance) {
-            cube = (CubeInstance) olapContext.realization;
-        } else if (olapContext.realization instanceof HybridInstance) {
-            cube = (CubeInstance) ((HybridInstance) olapContext.realization).getHistoryRealizationInstance();
-        }
-
-        String lookupTableName = olapContext.firstTableScan.getTableName();
-        DimensionDesc dim = cube.getDescriptor().findDimensionByTable(lookupTableName);
-        if (dim == null)
-            throw new IllegalStateException("No dimension with derived columns found for lookup table " + lookupTableName + ", cube desc " + cube.getDescriptor());
-
-        CubeManager cubeMgr = CubeManager.getInstance(cube.getConfig());
-        LookupStringTable table = cubeMgr.getLookupTable(cube.getLatestReadySegment(), dim);
-        this.allRows = table.getAllRows();
-
-        OLAPTable olapTable = (OLAPTable) olapContext.firstTableScan.getOlapTable();
-        this.colDescs = olapTable.getExposedColumns();
-        this.current = new Object[colDescs.size()];
-
-        reset();
-    }
-
-    @Override
-    public boolean moveNext() {
-        boolean hasNext = iterator.hasNext();
-        if (hasNext) {
-            String[] row = iterator.next();
-            for (int i = 0, n = colDescs.size(); i < n; i++) {
-                ColumnDesc colDesc = colDescs.get(i);
-                int colIdx = colDesc.getZeroBasedIndex();
-                if (colIdx >= 0) {
-                    current[i] = Tuple.convertOptiqCellValue(row[colIdx], colDesc.getType().getName());
-                } else {
-                    current[i] = null; // fake column
-                }
-            }
-        }
-        return hasNext;
-    }
-
-    @Override
-    public Object[] current() {
-        return current;
-    }
-
-    @Override
-    public void reset() {
-        this.iterator = allRows.iterator();
-    }
-
-    @Override
-    public void close() {
-    }
-
-}
+/*
+ * 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.kylin.query.enumerator;
+
+import org.apache.calcite.linq4j.Enumerator;
+import org.apache.kylin.cube.CubeInstance;
+import org.apache.kylin.cube.CubeManager;
+import org.apache.kylin.cube.model.DimensionDesc;
+import org.apache.kylin.dict.lookup.LookupStringTable;
+import org.apache.kylin.metadata.model.ColumnDesc;
+import org.apache.kylin.query.relnode.OLAPContext;
+import org.apache.kylin.query.schema.OLAPTable;
+import org.apache.kylin.storage.hybrid.HybridInstance;
+import org.apache.kylin.storage.tuple.Tuple;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ */
+public class LookupTableEnumerator implements Enumerator<Object[]> {
+
+    private final Collection<String[]> allRows;
+    private final List<ColumnDesc> colDescs;
+    private final Object[] current;
+    private Iterator<String[]> iterator;
+
+    public LookupTableEnumerator(OLAPContext olapContext) {
+
+        //TODO: assuming LookupTableEnumerator is handled by a cube
+        CubeInstance cube = null;
+
+        if (olapContext.realization instanceof CubeInstance) {
+            cube = (CubeInstance) olapContext.realization;
+        } else if (olapContext.realization instanceof HybridInstance) {
+            cube = (CubeInstance) ((HybridInstance) olapContext.realization).getHistoryRealizationInstance();
+        }
+
+        String lookupTableName = olapContext.firstTableScan.getTableName();
+        DimensionDesc dim = cube.getDescriptor().findDimensionByTable(lookupTableName);
+        if (dim == null)
+            throw new IllegalStateException("No dimension with derived columns found for lookup table " + lookupTableName + ", cube desc " + cube.getDescriptor());
+
+        CubeManager cubeMgr = CubeManager.getInstance(cube.getConfig());
+        LookupStringTable table = cubeMgr.getLookupTable(cube.getLatestReadySegment(), dim);
+        this.allRows = table.getAllRows();
+
+        OLAPTable olapTable = (OLAPTable) olapContext.firstTableScan.getOlapTable();
+        this.colDescs = olapTable.getExposedColumns();
+        this.current = new Object[colDescs.size()];
+
+        reset();
+    }
+
+    @Override
+    public boolean moveNext() {
+        boolean hasNext = iterator.hasNext();
+        if (hasNext) {
+            String[] row = iterator.next();
+            for (int i = 0, n = colDescs.size(); i < n; i++) {
+                ColumnDesc colDesc = colDescs.get(i);
+                int colIdx = colDesc.getZeroBasedIndex();
+                if (colIdx >= 0) {
+                    current[i] = Tuple.convertOptiqCellValue(row[colIdx], colDesc.getType().getName());
+                } else {
+                    current[i] = null; // fake column
+                }
+            }
+        }
+        return hasNext;
+    }
+
+    @Override
+    public Object[] current() {
+        // NOTE if without the copy, sql_lookup/query03.sql will yields messy result. Very weird coz other lookup queries are all good.
+        return Arrays.copyOf(current, current.length);
+    }
+
+    @Override
+    public void reset() {
+        this.iterator = allRows.iterator();
+    }
+
+    @Override
+    public void close() {
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/enumerator/OLAPEnumerator.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/enumerator/OLAPEnumerator.java b/query/src/main/java/org/apache/kylin/query/enumerator/OLAPEnumerator.java
index 768e7da..eaf668e 100644
--- a/query/src/main/java/org/apache/kylin/query/enumerator/OLAPEnumerator.java
+++ b/query/src/main/java/org/apache/kylin/query/enumerator/OLAPEnumerator.java
@@ -18,9 +18,12 @@
 
 package org.apache.kylin.query.enumerator;
 
-import net.hydromatic.linq4j.Enumerator;
-import net.hydromatic.optiq.DataContext;
-import net.hydromatic.optiq.jdbc.OptiqConnection;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.calcite.DataContext;
+import org.apache.calcite.jdbc.CalciteConnection;
+import org.apache.calcite.linq4j.Enumerator;
 import org.apache.kylin.metadata.filter.CompareTupleFilter;
 import org.apache.kylin.metadata.filter.TimeConditionLiteralsReplacer;
 import org.apache.kylin.metadata.filter.TupleFilter;
@@ -34,11 +37,7 @@ import org.apache.kylin.storage.hbase.coprocessor.DictCodeSystem;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.Map;
-import java.util.Properties;
-
 /**
- * @author xjiang
  */
 public class OLAPEnumerator implements Enumerator<Object[]> {
 
@@ -154,7 +153,7 @@ public class OLAPEnumerator implements Enumerator<Object[]> {
     }
 
     private void setConnectionProperties() {
-        OptiqConnection conn = (OptiqConnection) optiqContext.getQueryProvider();
+        CalciteConnection conn = (CalciteConnection) optiqContext.getQueryProvider();
         Properties connProps = conn.getProperties();
 
         String propThreshold = connProps.getProperty(OLAPQuery.PROP_SCAN_THRESHOLD);

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/enumerator/OLAPQuery.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/enumerator/OLAPQuery.java b/query/src/main/java/org/apache/kylin/query/enumerator/OLAPQuery.java
index db4e63c..27d8c94 100644
--- a/query/src/main/java/org/apache/kylin/query/enumerator/OLAPQuery.java
+++ b/query/src/main/java/org/apache/kylin/query/enumerator/OLAPQuery.java
@@ -18,17 +18,13 @@
 
 package org.apache.kylin.query.enumerator;
 
-import net.hydromatic.linq4j.AbstractEnumerable;
-import net.hydromatic.linq4j.Enumerable;
-import net.hydromatic.linq4j.Enumerator;
-import net.hydromatic.optiq.DataContext;
-
+import org.apache.calcite.DataContext;
+import org.apache.calcite.linq4j.AbstractEnumerable;
+import org.apache.calcite.linq4j.Enumerable;
+import org.apache.calcite.linq4j.Enumerator;
 import org.apache.kylin.query.relnode.OLAPContext;
 
 /**
- * 
- * @author xjiang
- * 
  */
 public class OLAPQuery extends AbstractEnumerable<Object[]> implements Enumerable<Object[]> {
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/optrule/OLAPAggregateRule.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/optrule/OLAPAggregateRule.java b/query/src/main/java/org/apache/kylin/query/optrule/OLAPAggregateRule.java
index 5c6b29b..b80a483 100644
--- a/query/src/main/java/org/apache/kylin/query/optrule/OLAPAggregateRule.java
+++ b/query/src/main/java/org/apache/kylin/query/optrule/OLAPAggregateRule.java
@@ -18,35 +18,31 @@
 
 package org.apache.kylin.query.optrule;
 
+import org.apache.calcite.plan.Convention;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.InvalidRelException;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.convert.ConverterRule;
+import org.apache.calcite.rel.logical.LogicalAggregate;
 import org.apache.kylin.query.relnode.OLAPRel;
-import org.eigenbase.rel.AggregateRel;
-import org.eigenbase.rel.InvalidRelException;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.convert.ConverterRule;
-import org.eigenbase.relopt.Convention;
-import org.eigenbase.relopt.RelTraitSet;
-
 import org.apache.kylin.query.relnode.OLAPAggregateRel;
 
 /**
- * 
- * @author xjiang
- * 
  */
 public class OLAPAggregateRule extends ConverterRule {
 
     public static final ConverterRule INSTANCE = new OLAPAggregateRule();
 
     public OLAPAggregateRule() {
-        super(AggregateRel.class, Convention.NONE, OLAPRel.CONVENTION, "OLAPAggregateRule");
+        super(LogicalAggregate.class, Convention.NONE, OLAPRel.CONVENTION, "OLAPAggregateRule");
     }
 
     @Override
     public RelNode convert(RelNode rel) {
-        AggregateRel agg = (AggregateRel) rel;
+        LogicalAggregate agg = (LogicalAggregate) rel;
         RelTraitSet traitSet = agg.getTraitSet().replace(OLAPRel.CONVENTION);
         try {
-            return new OLAPAggregateRel(agg.getCluster(), traitSet, convert(agg.getChild(), traitSet), agg.getGroupSet(), agg.getAggCallList());
+            return new OLAPAggregateRel(agg.getCluster(), traitSet, convert(agg.getInput(), traitSet), agg.getGroupSet(), agg.getAggCallList());
         } catch (InvalidRelException e) {
             throw new IllegalStateException("Can't create OLAPAggregateRel!", e);
         }

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/optrule/OLAPFilterRule.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/optrule/OLAPFilterRule.java b/query/src/main/java/org/apache/kylin/query/optrule/OLAPFilterRule.java
index 619e430..4c34f4e 100644
--- a/query/src/main/java/org/apache/kylin/query/optrule/OLAPFilterRule.java
+++ b/query/src/main/java/org/apache/kylin/query/optrule/OLAPFilterRule.java
@@ -18,33 +18,29 @@
 
 package org.apache.kylin.query.optrule;
 
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.logical.LogicalFilter;
 import org.apache.kylin.query.relnode.OLAPFilterRel;
 import org.apache.kylin.query.relnode.OLAPRel;
-import org.eigenbase.rel.FilterRel;
-import org.eigenbase.relopt.RelOptRule;
-import org.eigenbase.relopt.RelOptRuleCall;
-import org.eigenbase.relopt.RelTraitSet;
 
 /**
- * 
- * @author xjiang
- * 
  */
-
 public class OLAPFilterRule extends RelOptRule {
 
     public static final RelOptRule INSTANCE = new OLAPFilterRule();
 
     public OLAPFilterRule() {
-        super(operand(FilterRel.class, any()));
+        super(operand(LogicalFilter.class, any()));
     }
 
     @Override
     public void onMatch(RelOptRuleCall call) {
-        FilterRel filter = call.rel(0);
+        LogicalFilter filter = call.rel(0);
 
         RelTraitSet traitSet = filter.getTraitSet().replace(OLAPRel.CONVENTION);
-        OLAPFilterRel olapFilter = new OLAPFilterRel(filter.getCluster(), traitSet, convert(filter.getChild(), traitSet), filter.getCondition());
+        OLAPFilterRel olapFilter = new OLAPFilterRel(filter.getCluster(), traitSet, convert(filter.getInput(), traitSet), filter.getCondition());
         call.transformTo(olapFilter);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/optrule/OLAPJoinRule.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/optrule/OLAPJoinRule.java b/query/src/main/java/org/apache/kylin/query/optrule/OLAPJoinRule.java
index dd97933..10047e6 100644
--- a/query/src/main/java/org/apache/kylin/query/optrule/OLAPJoinRule.java
+++ b/query/src/main/java/org/apache/kylin/query/optrule/OLAPJoinRule.java
@@ -18,35 +18,32 @@
 
 package org.apache.kylin.query.optrule;
 
+import org.apache.calcite.plan.Convention;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.InvalidRelException;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.convert.ConverterRule;
+import org.apache.calcite.rel.core.JoinInfo;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.logical.LogicalJoin;
 import org.apache.kylin.query.relnode.OLAPFilterRel;
 import org.apache.kylin.query.relnode.OLAPJoinRel;
 import org.apache.kylin.query.relnode.OLAPRel;
-import org.eigenbase.rel.InvalidRelException;
-import org.eigenbase.rel.JoinInfo;
-import org.eigenbase.rel.JoinRel;
-import org.eigenbase.rel.JoinRelType;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.convert.ConverterRule;
-import org.eigenbase.relopt.Convention;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelTraitSet;
 
 /**
- * 
- * @author xjiang
- * 
  */
 public class OLAPJoinRule extends ConverterRule {
 
     public static final ConverterRule INSTANCE = new OLAPJoinRule();
 
     public OLAPJoinRule() {
-        super(JoinRel.class, Convention.NONE, OLAPRel.CONVENTION, "OLAPJoinRule");
+        super(LogicalJoin.class, Convention.NONE, OLAPRel.CONVENTION, "OLAPJoinRule");
     }
 
     @Override
     public RelNode convert(RelNode rel) {
-        JoinRel join = (JoinRel) rel;
+        LogicalJoin join = (LogicalJoin) rel;
         RelNode left = join.getInput(0);
         RelNode right = join.getInput(1);
 
@@ -68,8 +65,7 @@ public class OLAPJoinRule extends ConverterRule {
                     info.getEquiCondition(left, right, cluster.getRexBuilder()), //
                     info.leftKeys, info.rightKeys, join.getJoinType(), join.getVariablesStopped());
         } catch (InvalidRelException e) {
-            // Semantic error not possible. Must be a bug. Convert to
-            // internal error.
+            // Semantic error not possible. Must be a bug. Convert to internal error.
             throw new AssertionError(e);
             // LOGGER.fine(e.toString());
             // return null;

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/optrule/OLAPLimitRule.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/optrule/OLAPLimitRule.java b/query/src/main/java/org/apache/kylin/query/optrule/OLAPLimitRule.java
index 359b722..cde934e 100644
--- a/query/src/main/java/org/apache/kylin/query/optrule/OLAPLimitRule.java
+++ b/query/src/main/java/org/apache/kylin/query/optrule/OLAPLimitRule.java
@@ -18,35 +18,32 @@
 
 package org.apache.kylin.query.optrule;
 
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Sort;
 import org.apache.kylin.query.relnode.OLAPLimitRel;
 import org.apache.kylin.query.relnode.OLAPRel;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.SortRel;
-import org.eigenbase.relopt.RelOptRule;
-import org.eigenbase.relopt.RelOptRuleCall;
-import org.eigenbase.relopt.RelTraitSet;
 
 /**
- * 
- * @author xjiang
- * 
  */
 public class OLAPLimitRule extends RelOptRule {
 
     public static final RelOptRule INSTANCE = new OLAPLimitRule();
 
     public OLAPLimitRule() {
-        super(operand(SortRel.class, any()), "OLAPLimitRule");
+        super(operand(Sort.class, any()), "OLAPLimitRule");
     }
 
     @Override
     public void onMatch(RelOptRuleCall call) {
-        final SortRel sort = call.rel(0);
+        final Sort sort = call.rel(0);
         if (sort.offset == null && sort.fetch == null) {
             return;
         }
         final RelTraitSet traitSet = sort.getTraitSet().replace(OLAPRel.CONVENTION);
-        RelNode input = sort.getChild();
+        RelNode input = sort.getInput();
         if (!sort.getCollation().getFieldCollations().isEmpty()) {
             // Create a sort with the same sort key, but no offset or fetch.
             input = sort.copy(sort.getTraitSet(), input, sort.getCollation(), null, null);

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/optrule/OLAPProjectRule.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/optrule/OLAPProjectRule.java b/query/src/main/java/org/apache/kylin/query/optrule/OLAPProjectRule.java
index 7ab9dc3..4867162 100644
--- a/query/src/main/java/org/apache/kylin/query/optrule/OLAPProjectRule.java
+++ b/query/src/main/java/org/apache/kylin/query/optrule/OLAPProjectRule.java
@@ -18,34 +18,30 @@
 
 package org.apache.kylin.query.optrule;
 
-import org.eigenbase.rel.ProjectRel;
-import org.eigenbase.relopt.RelOptRule;
-import org.eigenbase.relopt.RelOptRuleCall;
-import org.eigenbase.relopt.RelTraitSet;
-
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.logical.LogicalProject;
 import org.apache.kylin.query.relnode.OLAPProjectRel;
 import org.apache.kylin.query.relnode.OLAPRel;
 
 /**
- * 
- * @author xjiang
- * 
  */
 public class OLAPProjectRule extends RelOptRule {
 
     public static final RelOptRule INSTANCE = new OLAPProjectRule();
 
     public OLAPProjectRule() {
-        super(operand(ProjectRel.class, any()));
+        super(operand(LogicalProject.class, any()));
     }
 
     @Override
     public void onMatch(RelOptRuleCall call) {
-        ProjectRel project = call.rel(0);
+        LogicalProject project = call.rel(0);
 
         RelTraitSet traitSet = project.getTraitSet().replace(OLAPRel.CONVENTION);
         OLAPProjectRel olapProj = new OLAPProjectRel(project.getCluster(), traitSet, //
-                convert(project.getChild(), traitSet), project.getProjects(), project.getRowType(), project.getFlags());
+                convert(project.getInput(), traitSet), project.getProjects(), project.getRowType(), project.getFlags());
         call.transformTo(olapProj);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/optrule/OLAPSortRule.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/optrule/OLAPSortRule.java b/query/src/main/java/org/apache/kylin/query/optrule/OLAPSortRule.java
index c9ea5f4..c020d63 100644
--- a/query/src/main/java/org/apache/kylin/query/optrule/OLAPSortRule.java
+++ b/query/src/main/java/org/apache/kylin/query/optrule/OLAPSortRule.java
@@ -18,35 +18,32 @@
 
 package org.apache.kylin.query.optrule;
 
+import org.apache.calcite.plan.Convention;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.convert.ConverterRule;
+import org.apache.calcite.rel.core.Sort;
 import org.apache.kylin.query.relnode.OLAPRel;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.SortRel;
-import org.eigenbase.rel.convert.ConverterRule;
-import org.eigenbase.relopt.Convention;
-import org.eigenbase.relopt.RelTraitSet;
-
 import org.apache.kylin.query.relnode.OLAPSortRel;
 
 /**
- * @author xjiang
- * 
  */
 public class OLAPSortRule extends ConverterRule {
 
     public static final OLAPSortRule INSTANCE = new OLAPSortRule();
 
     public OLAPSortRule() {
-        super(SortRel.class, Convention.NONE, OLAPRel.CONVENTION, "OLAPSortRule");
+        super(Sort.class, Convention.NONE, OLAPRel.CONVENTION, "OLAPSortRule");
     }
 
     @Override
     public RelNode convert(RelNode rel) {
-        final SortRel sort = (SortRel) rel;
+        final Sort sort = (Sort) rel;
         if (sort.offset != null || sort.fetch != null) {
             return null;
         }
         final RelTraitSet traitSet = sort.getTraitSet().replace(OLAPRel.CONVENTION);
-        final RelNode input = sort.getChild();
+        final RelNode input = sort.getInput();
         return new OLAPSortRel(rel.getCluster(), traitSet, convert(input, input.getTraitSet().replace(OLAPRel.CONVENTION)), sort.getCollation(), sort.offset, sort.fetch);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/optrule/OLAPToEnumerableConverterRule.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/optrule/OLAPToEnumerableConverterRule.java b/query/src/main/java/org/apache/kylin/query/optrule/OLAPToEnumerableConverterRule.java
index 4edee38..995e3e7 100644
--- a/query/src/main/java/org/apache/kylin/query/optrule/OLAPToEnumerableConverterRule.java
+++ b/query/src/main/java/org/apache/kylin/query/optrule/OLAPToEnumerableConverterRule.java
@@ -18,19 +18,14 @@
 
 package org.apache.kylin.query.optrule;
 
-import net.hydromatic.optiq.rules.java.EnumerableConvention;
-
+import org.apache.calcite.adapter.enumerable.EnumerableConvention;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.convert.ConverterRule;
 import org.apache.kylin.query.relnode.OLAPRel;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.rel.convert.ConverterRule;
-import org.eigenbase.relopt.RelTraitSet;
-
 import org.apache.kylin.query.relnode.OLAPToEnumerableConverter;
 
 /**
- * 
- * @author xjiang
- * 
  */
 public class OLAPToEnumerableConverterRule extends ConverterRule {
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
index 054a74e..b9adb4a 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
@@ -18,54 +18,54 @@
 
 package org.apache.kylin.query.relnode;
 
-import java.util.*;
-
-import net.hydromatic.optiq.AggregateFunction;
-import net.hydromatic.optiq.FunctionParameter;
-import net.hydromatic.optiq.impl.AggregateFunctionImpl;
-import net.hydromatic.optiq.rules.java.EnumerableConvention;
-import net.hydromatic.optiq.rules.java.EnumerableRel;
-import net.hydromatic.optiq.rules.java.EnumerableRelImplementor;
-import net.hydromatic.optiq.rules.java.JavaRules.EnumerableAggregateRel;
-
-import org.apache.kylin.query.sqlfunc.HLLDistinctCountAggFunc;
-import org.eigenbase.rel.AggregateCall;
-import org.eigenbase.rel.AggregateRelBase;
-import org.eigenbase.rel.Aggregation;
-import org.eigenbase.rel.InvalidRelException;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptCost;
-import org.eigenbase.relopt.RelOptPlanner;
-import org.eigenbase.relopt.RelTrait;
-import org.eigenbase.relopt.RelTraitSet;
-import org.eigenbase.reltype.RelDataType;
-import org.eigenbase.reltype.RelDataTypeFactory;
-import org.eigenbase.reltype.RelDataTypeField;
-import org.eigenbase.sql.SqlAggFunction;
-import org.eigenbase.sql.SqlIdentifier;
-import org.eigenbase.sql.fun.SqlStdOperatorTable;
-import org.eigenbase.sql.fun.SqlSumEmptyIsZeroAggFunction;
-import org.eigenbase.sql.parser.SqlParserPos;
-import org.eigenbase.sql.type.InferTypes;
-import org.eigenbase.sql.type.OperandTypes;
-import org.eigenbase.sql.type.ReturnTypes;
-import org.eigenbase.sql.type.SqlTypeFamily;
-import org.eigenbase.sql.validate.SqlUserDefinedAggFunction;
-import org.eigenbase.util.Util;
-
-import com.google.common.base.Preconditions;
-
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.calcite.adapter.enumerable.EnumerableAggregate;
+import org.apache.calcite.adapter.enumerable.EnumerableConvention;
+import org.apache.calcite.adapter.enumerable.EnumerableRel;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTrait;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.InvalidRelException;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.schema.AggregateFunction;
+import org.apache.calcite.schema.FunctionParameter;
+import org.apache.calcite.schema.impl.AggregateFunctionImpl;
+import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.InferTypes;
+import org.apache.calcite.sql.type.OperandTypes;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.calcite.sql.validate.SqlUserDefinedAggFunction;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.Util;
 import org.apache.kylin.metadata.model.ColumnDesc;
-import org.apache.kylin.metadata.model.TableDesc;
 import org.apache.kylin.metadata.model.FunctionDesc;
 import org.apache.kylin.metadata.model.ParameterDesc;
+import org.apache.kylin.metadata.model.TableDesc;
 import org.apache.kylin.metadata.model.TblColRef;
+import org.apache.kylin.query.sqlfunc.HLLDistinctCountAggFunc;
+
+import com.google.common.base.Preconditions;
 
 /**
- * @author xjiang
  */
-public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, EnumerableRel {
+public class OLAPAggregateRel extends Aggregate implements OLAPRel {
 
     private final static Map<String, String> AGGR_FUNC_MAP = new HashMap<String, String>();
 
@@ -98,16 +98,22 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
     private List<TblColRef> groups;
     private List<FunctionDesc> aggregations;
 
-    public OLAPAggregateRel(RelOptCluster cluster, RelTraitSet traits, RelNode child, BitSet groupSet, List<AggregateCall> aggCalls) throws InvalidRelException {
-        super(cluster, traits, child, groupSet, aggCalls);
+    public OLAPAggregateRel(RelOptCluster cluster, RelTraitSet traits, RelNode child, ImmutableBitSet groupSet, List<AggregateCall> aggCalls) throws InvalidRelException {
+        super(cluster, traits, child, false, groupSet, asList(groupSet), aggCalls);
         Preconditions.checkArgument(getConvention() == OLAPRel.CONVENTION);
         this.afterAggregate = false;
         this.rewriteAggCalls = aggCalls;
         this.rowType = getRowType();
     }
 
+    private static List<ImmutableBitSet> asList(ImmutableBitSet groupSet) {
+        ArrayList<ImmutableBitSet> l = new ArrayList<ImmutableBitSet>(1);
+        l.add(groupSet);
+        return l;
+    }
+
     @Override
-    public AggregateRelBase copy(RelTraitSet traitSet, RelNode input, BitSet groupSet, List<AggregateCall> aggCalls) {
+    public Aggregate copy(RelTraitSet traitSet, RelNode input, boolean indicator, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
         try {
             return new OLAPAggregateRel(getCluster(), traitSet, input, groupSet, aggCalls);
         } catch (InvalidRelException e) {
@@ -117,19 +123,13 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
 
     @Override
     public RelOptCost computeSelfCost(RelOptPlanner planner) {
-        double factor = .5;
-        for (AggregateCall aggCall : aggCalls) {
-            if ("$SUM0".equals(aggCall.getAggregation().getName())) {
-                factor = .2;
-            }
-        }
-        return super.computeSelfCost(planner).multiplyBy(factor);
+        return super.computeSelfCost(planner).multiplyBy(.05);
     }
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
 
-        implementor.visitChild(getChild(), this);
+        implementor.visitChild(getInput(), this);
 
         this.context = implementor.getContext();
         this.columnRowType = buildColumnRowType();
@@ -155,7 +155,7 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
         buildGroups();
         buildAggregations();
 
-        ColumnRowType inputColumnRowType = ((OLAPRel) getChild()).getColumnRowType();
+        ColumnRowType inputColumnRowType = ((OLAPRel) getInput()).getColumnRowType();
         List<TblColRef> columns = new ArrayList<TblColRef>(this.rowType.getFieldCount());
         columns.addAll(this.groups);
 
@@ -189,7 +189,7 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
     }
 
     private void buildGroups() {
-        ColumnRowType inputColumnRowType = ((OLAPRel) getChild()).getColumnRowType();
+        ColumnRowType inputColumnRowType = ((OLAPRel) getInput()).getColumnRowType();
         this.groups = new ArrayList<TblColRef>();
         for (int i = getGroupSet().nextSetBit(0); i >= 0; i = getGroupSet().nextSetBit(i + 1)) {
             Set<TblColRef> columns = inputColumnRowType.getSourceColumnsByIndex(i);
@@ -198,7 +198,7 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
     }
 
     private void buildAggregations() {
-        ColumnRowType inputColumnRowType = ((OLAPRel) getChild()).getColumnRowType();
+        ColumnRowType inputColumnRowType = ((OLAPRel) getInput()).getColumnRowType();
         this.aggregations = new ArrayList<FunctionDesc>();
         for (AggregateCall aggCall : this.rewriteAggCalls) {
             ParameterDesc parameter = null;
@@ -225,7 +225,7 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
     }
 
     private void translateAggregation() {
-        ColumnRowType inputColumnRowType = ((OLAPRel) getChild()).getColumnRowType();
+        ColumnRowType inputColumnRowType = ((OLAPRel) getInput()).getColumnRowType();
         for (int i = 0; i < this.aggregations.size(); i++) {
             FunctionDesc aggFunc = this.aggregations.get(i);
             context.aggregations.add(aggFunc);
@@ -249,14 +249,13 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
     }
 
     private void fillbackOptimizedColumn() {
-        // some aggcall will be optimized out in sub-query (e.g. tableau generated sql)
-        // we need to fill them back
-        RelDataType inputAggRow = getChild().getRowType();
+        // some aggcall will be optimized out in sub-query (e.g. tableau generated sql), we need to fill them back
+        RelDataType inputAggRow = getInput().getRowType();
         RelDataType outputAggRow = getRowType();
         if (inputAggRow.getFieldCount() != outputAggRow.getFieldCount()) {
             for (RelDataTypeField inputField : inputAggRow.getFieldList()) {
                 String inputFieldName = inputField.getName();
-                if (outputAggRow.getField(inputFieldName, true) == null) {
+                if (outputAggRow.getField(inputFieldName, true, false) == null) {
                     TblColRef column = this.columnRowType.getColumnByIndex(inputField.getIndex());
                     this.context.metricsColumns.add(column);
                 }
@@ -266,7 +265,7 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
 
     @Override
     public void implementRewrite(RewriteImplementor implementor) {
-        implementor.visitChild(this, getChild());
+        implementor.visitChild(this, getInput());
 
         // only rewrite the first aggregation
         if (!this.afterAggregate && RewriteImplementor.needRewrite(this.context)) {
@@ -283,7 +282,8 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
         }
 
         // rebuild rowType & columnRowType
-        this.rowType = this.deriveRowType();
+        //ClassUtil.updateFinalField(Aggregate.class, "aggCalls", this, rewriteAggCalls);
+        this.rowType = this.deriveRowType(); // this does not work coz super.aggCalls is final
         this.columnRowType = this.buildColumnRowType();
 
     }
@@ -293,34 +293,24 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
         // rebuild parameters
         List<Integer> newArgList = new ArrayList<Integer>(1);
         String fieldName = func.getRewriteFieldName();
-        RelDataTypeField field = getChild().getRowType().getField(fieldName, true);
+        RelDataTypeField field = getInput().getRowType().getField(fieldName, true, false);
         newArgList.add(field.getIndex());
 
         // rebuild function
         RelDataType fieldType = aggCall.getType();
-        Aggregation newAgg = aggCall.getAggregation();
+        SqlAggFunction newAgg = aggCall.getAggregation();
         if (func.isCountDistinct()) {
             newAgg = createHyperLogLogAggFunction(fieldType);
         } else if (func.isCount()) {
-            //newAgg = new SqlSumEmptyIsZeroAggFunction(fieldType);
             newAgg = SqlStdOperatorTable.SUM0;
         }
 
         // rebuild aggregate call
         AggregateCall newAggCall = new AggregateCall(newAgg, false, newArgList, fieldType, newAgg.getName());
-
-        // To make sure specified type matches the inferReturnType, or otherwise
-        // there will be assertion failure in optiq
-        // The problem is BIGINT != BIGINT NOT NULL
-        // Details see https://github.scm.corp.ebay.com/Kylin/Kylin/issues/323
-        SqlAggFunction aggFunction = (SqlAggFunction) newAggCall.getAggregation();
-        AggCallBinding callBinding = newAggCall.createBinding(this);
-        RelDataType inferReturnType = aggFunction.inferReturnType(callBinding);
-
-        return new AggregateCall(newAgg, false, newArgList, inferReturnType, newAgg.getName());
+        return newAggCall;
     }
 
-    private Aggregation createHyperLogLogAggFunction(RelDataType returnType) {
+    private SqlAggFunction createHyperLogLogAggFunction(RelDataType returnType) {
         RelDataTypeFactory typeFactory = getCluster().getTypeFactory();
         SqlIdentifier sqlIdentifier = new SqlIdentifier("HLL_COUNT", new SqlParserPos(1, 1));
         AggregateFunction aggFunction = AggregateFunctionImpl.create(HLLDistinctCountAggFunc.class);
@@ -335,16 +325,13 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
     }
 
     @Override
-    public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
-
-        EnumerableAggregateRel enumAggRel;
+    public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
         try {
-            enumAggRel = new EnumerableAggregateRel(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), getChild(), this.groupSet, rewriteAggCalls);
+            return new EnumerableAggregate(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
+                    sole(inputs), false, this.groupSet, this.groupSets, rewriteAggCalls);
         } catch (InvalidRelException e) {
-            throw new IllegalStateException("Can't create EnumerableAggregateRel!", e);
+            throw new IllegalStateException("Can't create EnumerableAggregate!", e);
         }
-
-        return enumAggRel.implement(implementor, pref);
     }
 
     @Override
@@ -359,7 +346,7 @@ public class OLAPAggregateRel extends AggregateRelBase implements OLAPRel, Enume
 
     @Override
     public boolean hasSubQuery() {
-        OLAPRel olapChild = (OLAPRel) getChild();
+        OLAPRel olapChild = (OLAPRel) getInput();
         return olapChild.hasSubQuery();
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/relnode/OLAPContext.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPContext.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPContext.java
index 6121c80..d8a94f8 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPContext.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPContext.java
@@ -18,6 +18,8 @@
 
 package org.apache.kylin.query.relnode;
 
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
 import org.apache.kylin.metadata.filter.TupleFilter;
 import org.apache.kylin.metadata.model.FunctionDesc;
 import org.apache.kylin.metadata.model.JoinDesc;
@@ -27,14 +29,10 @@ import org.apache.kylin.metadata.realization.SQLDigest;
 import org.apache.kylin.query.schema.OLAPSchema;
 import org.apache.kylin.storage.StorageContext;
 import org.apache.kylin.storage.tuple.TupleInfo;
-import org.eigenbase.reltype.RelDataType;
-import org.eigenbase.reltype.RelDataTypeField;
 
 import java.util.*;
 
 /**
- * @author xjiang
- * 
  */
 public class OLAPContext {
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/relnode/OLAPFilterRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPFilterRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPFilterRel.java
index 71cb2fa..d0f0e4e 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPFilterRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPFilterRel.java
@@ -18,36 +18,55 @@
 
 package org.apache.kylin.query.relnode;
 
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Sets;
-import net.hydromatic.optiq.rules.java.EnumerableConvention;
-import net.hydromatic.optiq.rules.java.EnumerableRel;
-import net.hydromatic.optiq.rules.java.EnumerableRelImplementor;
-import net.hydromatic.optiq.rules.java.JavaRules.EnumerableCalcRel;
-import net.hydromatic.optiq.runtime.SqlFunctions;
-import org.apache.kylin.metadata.filter.*;
-import org.apache.kylin.metadata.filter.TupleFilter.FilterOperatorEnum;
-import org.apache.kylin.metadata.model.TblColRef;
-import org.eigenbase.rel.FilterRelBase;
-import org.eigenbase.rel.RelCollation;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.relopt.*;
-import org.eigenbase.reltype.RelDataType;
-import org.eigenbase.rex.*;
-import org.eigenbase.sql.SqlKind;
-import org.eigenbase.sql.SqlOperator;
-import org.eigenbase.util.NlsString;
-
 import java.util.GregorianCalendar;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.calcite.adapter.enumerable.EnumerableCalc;
+import org.apache.calcite.adapter.enumerable.EnumerableConvention;
+import org.apache.calcite.adapter.enumerable.EnumerableRel;
+import org.apache.calcite.avatica.util.TimeUnitRange;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTrait;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexDynamicParam;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexLocalRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexProgram;
+import org.apache.calcite.rex.RexProgramBuilder;
+import org.apache.calcite.rex.RexVisitorImpl;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.util.NlsString;
+import org.apache.kylin.metadata.filter.CaseTupleFilter;
+import org.apache.kylin.metadata.filter.ColumnTupleFilter;
+import org.apache.kylin.metadata.filter.CompareTupleFilter;
+import org.apache.kylin.metadata.filter.ConstantTupleFilter;
+import org.apache.kylin.metadata.filter.DynamicTupleFilter;
+import org.apache.kylin.metadata.filter.ExtractTupleFilter;
+import org.apache.kylin.metadata.filter.LogicalTupleFilter;
+import org.apache.kylin.metadata.filter.TupleFilter;
+import org.apache.kylin.metadata.filter.TupleFilter.FilterOperatorEnum;
+import org.apache.kylin.metadata.model.TblColRef;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Sets;
+
 /**
- * @author xjiang
  */
-public class OLAPFilterRel extends FilterRelBase implements OLAPRel, EnumerableRel {
+public class OLAPFilterRel extends Filter implements OLAPRel {
 
     private static class TupleFilterVisitor extends RexVisitorImpl<TupleFilter> {
 
@@ -176,6 +195,7 @@ public class OLAPFilterRel extends FilterRelBase implements OLAPRel, EnumerableR
             return filter;
         }
 
+        @SuppressWarnings("unused")
         private String normToTwoDigits(int i) {
             if (i < 10)
                 return "0" + i;
@@ -193,9 +213,9 @@ public class OLAPFilterRel extends FilterRelBase implements OLAPRel, EnumerableR
                 GregorianCalendar g = (GregorianCalendar) literalValue;
                 //strValue = "" + g.get(Calendar.YEAR) + "-" + normToTwoDigits(g.get(Calendar.MONTH) + 1) + "-" + normToTwoDigits(g.get(Calendar.DAY_OF_MONTH));
                 strValue = Long.toString(g.getTimeInMillis());
-            } else if (literalValue instanceof SqlFunctions.TimeUnitRange) {
+            } else if (literalValue instanceof TimeUnitRange) {
                 // Extract(x from y) in where clause
-                strValue = ((SqlFunctions.TimeUnitRange) literalValue).name();
+                strValue = ((TimeUnitRange) literalValue).name();
             } else if (literalValue == null) {
                 strValue = null;
             } else {
@@ -229,13 +249,13 @@ public class OLAPFilterRel extends FilterRelBase implements OLAPRel, EnumerableR
     }
 
     @Override
-    public FilterRelBase copy(RelTraitSet traitSet, RelNode input, RexNode condition) {
+    public Filter copy(RelTraitSet traitSet, RelNode input, RexNode condition) {
         return new OLAPFilterRel(getCluster(), traitSet, input, condition);
     }
 
     @Override
     public void implementOLAP(OLAPImplementor implementor) {
-        implementor.visitChild(getChild(), this);
+        implementor.visitChild(getInput(), this);
 
         this.columnRowType = buildColumnRowType();
         this.context = implementor.getContext();
@@ -247,7 +267,7 @@ public class OLAPFilterRel extends FilterRelBase implements OLAPRel, EnumerableR
     }
 
     private ColumnRowType buildColumnRowType() {
-        OLAPRel olapChild = (OLAPRel) getChild();
+        OLAPRel olapChild = (OLAPRel) getInput();
         ColumnRowType inputColumnRowType = olapChild.getColumnRowType();
         return inputColumnRowType;
     }
@@ -263,7 +283,6 @@ public class OLAPFilterRel extends FilterRelBase implements OLAPRel, EnumerableR
         context.filterColumns = collectColumns(context.filter);
     }
 
-
     private Set<TblColRef> collectColumns(TupleFilter filter) {
         Set<TblColRef> ret = Sets.newHashSet();
         collectColumnsRecursively(filter, ret);
@@ -280,23 +299,22 @@ public class OLAPFilterRel extends FilterRelBase implements OLAPRel, EnumerableR
     }
 
     @Override
-    public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
+    public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
         // keep it for having clause
         RexBuilder rexBuilder = getCluster().getRexBuilder();
-        RelDataType inputRowType = getChild().getRowType();
+        RelDataType inputRowType = getInput().getRowType();
         RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
         programBuilder.addIdentity();
         programBuilder.addCondition(this.condition);
         RexProgram program = programBuilder.getProgram();
 
-        EnumerableCalcRel enumCalcRel = new EnumerableCalcRel(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), getChild(), this.rowType, program, ImmutableList.<RelCollation> of());
-
-        return enumCalcRel.implement(implementor, pref);
+        return new EnumerableCalc(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
+                sole(inputs), program, ImmutableList.<RelCollation> of());
     }
 
     @Override
     public void implementRewrite(RewriteImplementor implementor) {
-        implementor.visitChild(this, getChild());
+        implementor.visitChild(this, getInput());
 
         this.rowType = this.deriveRowType();
         this.columnRowType = buildColumnRowType();
@@ -314,7 +332,7 @@ public class OLAPFilterRel extends FilterRelBase implements OLAPRel, EnumerableR
 
     @Override
     public boolean hasSubQuery() {
-        OLAPRel olapChild = (OLAPRel) getChild();
+        OLAPRel olapChild = (OLAPRel) getInput();
         return olapChild.hasSubQuery();
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
index 4cfe133..3c57fdb 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java
@@ -25,42 +25,43 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import net.hydromatic.linq4j.expressions.Blocks;
-import net.hydromatic.linq4j.expressions.Expressions;
-import net.hydromatic.optiq.rules.java.EnumerableRelImplementor;
-import net.hydromatic.optiq.rules.java.JavaRules.EnumerableJoinRel;
-import net.hydromatic.optiq.rules.java.PhysType;
-import net.hydromatic.optiq.rules.java.PhysTypeImpl;
-
+import org.apache.calcite.adapter.enumerable.EnumerableConvention;
+import org.apache.calcite.adapter.enumerable.EnumerableJoin;
+import org.apache.calcite.adapter.enumerable.EnumerableRel;
+import org.apache.calcite.adapter.enumerable.EnumerableRelImplementor;
+import org.apache.calcite.adapter.enumerable.PhysType;
+import org.apache.calcite.adapter.enumerable.PhysTypeImpl;
+import org.apache.calcite.linq4j.tree.Blocks;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.linq4j.tree.MethodCallExpression;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.plan.RelTrait;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.InvalidRelException;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.JoinInfo;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory.FieldInfoBuilder;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rel.type.RelDataTypeFieldImpl;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.util.ImmutableIntList;
+import org.apache.kylin.metadata.model.JoinDesc;
+import org.apache.kylin.metadata.model.TblColRef;
 import org.apache.kylin.query.schema.OLAPTable;
-import org.eigenbase.rel.InvalidRelException;
-import org.eigenbase.rel.JoinInfo;
-import org.eigenbase.rel.JoinRelType;
-import org.eigenbase.rel.RelNode;
-import org.eigenbase.relopt.RelOptCluster;
-import org.eigenbase.relopt.RelOptCost;
-import org.eigenbase.relopt.RelOptPlanner;
-import org.eigenbase.relopt.RelOptTable;
-import org.eigenbase.relopt.RelTrait;
-import org.eigenbase.relopt.RelTraitSet;
-import org.eigenbase.reltype.RelDataType;
-import org.eigenbase.reltype.RelDataTypeFactory.FieldInfoBuilder;
-import org.eigenbase.reltype.RelDataTypeField;
-import org.eigenbase.reltype.RelDataTypeFieldImpl;
-import org.eigenbase.rex.RexCall;
-import org.eigenbase.rex.RexInputRef;
-import org.eigenbase.rex.RexNode;
-import org.eigenbase.sql.SqlKind;
-import org.eigenbase.util.ImmutableIntList;
 
 import com.google.common.base.Preconditions;
-import org.apache.kylin.metadata.model.JoinDesc;
-import org.apache.kylin.metadata.model.TblColRef;
 
 /**
- * @author xjiang
  */
-public class OLAPJoinRel extends EnumerableJoinRel implements OLAPRel {
+public class OLAPJoinRel extends EnumerableJoin implements OLAPRel {
 
     private final static String[] COLUMN_ARRAY_MARKER = new String[0];
 
@@ -79,7 +80,7 @@ public class OLAPJoinRel extends EnumerableJoinRel implements OLAPRel {
     }
 
     @Override
-    public EnumerableJoinRel copy(RelTraitSet traitSet, RexNode conditionExpr, RelNode left, RelNode right, //
+    public EnumerableJoin copy(RelTraitSet traitSet, RexNode conditionExpr, RelNode left, RelNode right, //
             JoinRelType joinType, boolean semiJoinDone) {
 
         final JoinInfo joinInfo = JoinInfo.of(left, right, condition);
@@ -213,20 +214,25 @@ public class OLAPJoinRel extends EnumerableJoinRel implements OLAPRel {
     }
 
     @Override
-    public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
-        Result result = null;
+    public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
         if (this.hasSubQuery) {
-            result = super.implement(implementor, pref);
+            try {
+                return new EnumerableJoin(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
+                        inputs.get(0), inputs.get(1), condition, leftKeys, rightKeys, joinType, variablesStopped);
+            } catch (InvalidRelException e) {
+                throw new IllegalStateException("Can't create EnumerableJoin!", e);
+            }
         } else {
-            // prepare return tuple info for execution
-            this.context.setReturnTupleInfo(rowType, columnRowType);
-            
-            PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());
-            RelOptTable factTable = context.firstTableScan.getTable();
-            result = implementor.result(physType, Blocks.toBlock(Expressions.call(factTable.getExpression(OLAPTable.class), "executeOLAPQuery", implementor.getRootExpression(), Expressions.constant(context.id))));
+            return this;
         }
+    }
 
-        return result;
+    @Override
+    public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
+        PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());
+        RelOptTable factTable = context.firstTableScan.getTable();
+        MethodCallExpression exprCall = Expressions.call(factTable.getExpression(OLAPTable.class), "executeIndexQuery", implementor.getRootExpression(), Expressions.constant(context.id));
+        return implementor.result(physType, Blocks.toBlock(exprCall));
     }
 
     @Override
@@ -247,7 +253,7 @@ public class OLAPJoinRel extends EnumerableJoinRel implements OLAPRel {
             List<RelDataTypeField> newFieldList = new LinkedList<RelDataTypeField>();
             for (Map.Entry<String, RelDataType> rewriteField : this.context.rewriteFields.entrySet()) {
                 String fieldName = rewriteField.getKey();
-                if (this.rowType.getField(fieldName, true) == null) {
+                if (this.rowType.getField(fieldName, true, false) == null) {
                     RelDataType fieldType = rewriteField.getValue();
                     RelDataTypeField newField = new RelDataTypeFieldImpl(fieldName, paramIndex++, fieldType);
                     newFieldList.add(newField);


[08/10] incubator-kylin git commit: KYLIN-780 Upgrade query module, all query test pass

Posted by li...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/atopcalcite/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
----------------------------------------------------------------------
diff --git a/atopcalcite/src/main/java/org/apache/calcite/runtime/SqlFunctions.java b/atopcalcite/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
new file mode 100644
index 0000000..3b1a2e0
--- /dev/null
+++ b/atopcalcite/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -0,0 +1,1261 @@
+/*
+ * OVERRIDE POINT:
+ * - divide(BigDecimal,BigDecimal), was `b0.divide(b1)`, now `b0.divide(b1, MathContext.DECIMAL64);` 
+ */
+
+/*
+ * 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.calcite.runtime;
+
+import org.apache.calcite.DataContext;
+import org.apache.calcite.avatica.util.ByteString;
+import org.apache.calcite.avatica.util.DateTimeUtils;
+import org.apache.calcite.linq4j.Enumerable;
+import org.apache.calcite.linq4j.Linq4j;
+import org.apache.calcite.linq4j.function.Deterministic;
+import org.apache.calcite.linq4j.function.Function1;
+import org.apache.calcite.linq4j.function.NonDeterministic;
+import org.apache.calcite.linq4j.tree.Primitive;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.math.MathContext;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.text.DecimalFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TimeZone;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.regex.Pattern;
+
+/**
+ * Helper methods to implement SQL functions in generated code.
+ *
+ * <p>Not present: and, or, not (builtin operators are better, because they
+ * use lazy evaluation. Implementations do not check for null values; the
+ * calling code must do that.</p>
+ *
+ * <p>Many of the functions do not check for null values. This is intentional.
+ * If null arguments are possible, the code-generation framework checks for
+ * nulls before calling the functions.</p>
+ */
+@SuppressWarnings("UnnecessaryUnboxing")
+@Deterministic
+public class SqlFunctions {
+  private static final DecimalFormat DOUBLE_FORMAT =
+      new DecimalFormat("0.0E0");
+
+  private static final TimeZone LOCAL_TZ = TimeZone.getDefault();
+
+  private static final Function1<List<Object>, Enumerable<Object>>
+  LIST_AS_ENUMERABLE =
+      new Function1<List<Object>, Enumerable<Object>>() {
+        public Enumerable<Object> apply(List<Object> list) {
+          return Linq4j.asEnumerable(list);
+        }
+      };
+
+  /** Holds, for each thread, a map from sequence name to sequence current
+   * value.
+   *
+   * <p>This is a straw man of an implementation whose main goal is to prove
+   * that sequences can be parsed, validated and planned. A real application
+   * will want persistent values for sequences, shared among threads. */
+  private static final ThreadLocal<Map<String, AtomicLong>> THREAD_SEQUENCES =
+      new ThreadLocal<Map<String, AtomicLong>>() {
+        @Override protected Map<String, AtomicLong> initialValue() {
+          return new HashMap<String, AtomicLong>();
+        }
+      };
+
+  private SqlFunctions() {
+  }
+
+  /** SQL SUBSTRING(string FROM ... FOR ...) function. */
+  public static String substring(String s, int from, int for_) {
+    return s.substring(from - 1, Math.min(from - 1 + for_, s.length()));
+  }
+
+  /** SQL SUBSTRING(string FROM ...) function. */
+  public static String substring(String s, int from) {
+    return s.substring(from - 1);
+  }
+
+  /** SQL UPPER(string) function. */
+  public static String upper(String s) {
+    return s.toUpperCase();
+  }
+
+  /** SQL LOWER(string) function. */
+  public static String lower(String s) {
+    return s.toLowerCase();
+  }
+
+  /** SQL INITCAP(string) function. */
+  public static String initcap(String s) {
+    // Assumes Alpha as [A-Za-z0-9]
+    // white space is treated as everything else.
+    final int len = s.length();
+    boolean start = true;
+    final StringBuilder newS = new StringBuilder();
+
+    for (int i = 0; i < len; i++) {
+      char curCh = s.charAt(i);
+      final int c = (int) curCh;
+      if (start) {  // curCh is whitespace or first character of word.
+        if (c > 47 && c < 58) { // 0-9
+          start = false;
+        } else if (c > 64 && c < 91) {  // A-Z
+          start = false;
+        } else if (c > 96 && c < 123) {  // a-z
+          start = false;
+          curCh = (char) (c - 32); // Uppercase this character
+        }
+        // else {} whitespace
+      } else {  // Inside of a word or white space after end of word.
+        if (c > 47 && c < 58) { // 0-9
+          // noop
+        } else if (c > 64 && c < 91) {  // A-Z
+          curCh = (char) (c + 32); // Lowercase this character
+        } else if (c > 96 && c < 123) {  // a-z
+          // noop
+        } else { // whitespace
+          start = true;
+        }
+      }
+      newS.append(curCh);
+    } // for each character in s
+    return newS.toString();
+  }
+
+  /** SQL CHARACTER_LENGTH(string) function. */
+  public static int charLength(String s) {
+    return s.length();
+  }
+
+  /** SQL {@code string || string} operator. */
+  public static String concat(String s0, String s1) {
+    return s0 + s1;
+  }
+
+  /** SQL {@code binary || binary} operator. */
+  public static ByteString concat(ByteString s0, ByteString s1) {
+    return s0.concat(s1);
+  }
+
+  /** SQL {@code RTRIM} function applied to string. */
+  public static String rtrim(String s) {
+    return trim_(s, false, true, ' ');
+  }
+
+  /** SQL {@code LTRIM} function. */
+  public static String ltrim(String s) {
+    return trim_(s, true, false, ' ');
+  }
+
+  /** SQL {@code TRIM(... seek FROM s)} function. */
+  public static String trim(boolean leading, boolean trailing, String seek,
+      String s) {
+    return trim_(s, leading, trailing, seek.charAt(0));
+  }
+
+  /** SQL {@code TRIM} function. */
+  private static String trim_(String s, boolean left, boolean right, char c) {
+    int j = s.length();
+    if (right) {
+      for (;;) {
+        if (j == 0) {
+          return "";
+        }
+        if (s.charAt(j - 1) != c) {
+          break;
+        }
+        --j;
+      }
+    }
+    int i = 0;
+    if (left) {
+      for (;;) {
+        if (i == j) {
+          return "";
+        }
+        if (s.charAt(i) != c) {
+          break;
+        }
+        ++i;
+      }
+    }
+    return s.substring(i, j);
+  }
+
+  /** SQL {@code TRIM} function applied to binary string. */
+  public static ByteString trim(ByteString s) {
+    return trim_(s, true, true);
+  }
+
+  /** Helper for CAST. */
+  public static ByteString rtrim(ByteString s) {
+    return trim_(s, false, true);
+  }
+
+  /** SQL {@code TRIM} function applied to binary string. */
+  private static ByteString trim_(ByteString s, boolean left, boolean right) {
+    int j = s.length();
+    if (right) {
+      for (;;) {
+        if (j == 0) {
+          return ByteString.EMPTY;
+        }
+        if (s.byteAt(j - 1) != 0) {
+          break;
+        }
+        --j;
+      }
+    }
+    int i = 0;
+    if (left) {
+      for (;;) {
+        if (i == j) {
+          return ByteString.EMPTY;
+        }
+        if (s.byteAt(i) != 0) {
+          break;
+        }
+        ++i;
+      }
+    }
+    return s.substring(i, j);
+  }
+
+  /** SQL {@code OVERLAY} function. */
+  public static String overlay(String s, String r, int start) {
+    if (s == null || r == null) {
+      return null;
+    }
+    return s.substring(0, start - 1)
+        + r
+        + s.substring(start - 1 + r.length());
+  }
+
+  /** SQL {@code OVERLAY} function. */
+  public static String overlay(String s, String r, int start, int length) {
+    if (s == null || r == null) {
+      return null;
+    }
+    return s.substring(0, start - 1)
+        + r
+        + s.substring(start - 1 + length);
+  }
+
+  /** SQL {@code OVERLAY} function applied to binary strings. */
+  public static ByteString overlay(ByteString s, ByteString r, int start) {
+    if (s == null || r == null) {
+      return null;
+    }
+    return s.substring(0, start - 1)
+           .concat(r)
+           .concat(s.substring(start - 1 + r.length()));
+  }
+
+  /** SQL {@code OVERLAY} function applied to binary strings. */
+  public static ByteString overlay(ByteString s, ByteString r, int start,
+      int length) {
+    if (s == null || r == null) {
+      return null;
+    }
+    return s.substring(0, start - 1)
+           .concat(r)
+           .concat(s.substring(start - 1 + length));
+  }
+
+  /** SQL {@code LIKE} function. */
+  public static boolean like(String s, String pattern) {
+    final String regex = Like.sqlToRegexLike(pattern, null);
+    return Pattern.matches(regex, s);
+  }
+
+  /** SQL {@code LIKE} function with escape. */
+  public static boolean like(String s, String pattern, String escape) {
+    final String regex = Like.sqlToRegexLike(pattern, escape);
+    return Pattern.matches(regex, s);
+  }
+
+  /** SQL {@code SIMILAR} function. */
+  public static boolean similar(String s, String pattern) {
+    final String regex = Like.sqlToRegexSimilar(pattern, null);
+    return Pattern.matches(regex, s);
+  }
+
+  /** SQL {@code SIMILAR} function with escape. */
+  public static boolean similar(String s, String pattern, String escape) {
+    final String regex = Like.sqlToRegexSimilar(pattern, escape);
+    return Pattern.matches(regex, s);
+  }
+
+  // =
+
+  /** SQL = operator applied to Object values (including String; neither
+   * side may be null). */
+  public static boolean eq(Object b0, Object b1) {
+    return b0.equals(b1);
+  }
+
+  /** SQL = operator applied to BigDecimal values (neither may be null). */
+  public static boolean eq(BigDecimal b0, BigDecimal b1) {
+    return b0.stripTrailingZeros().equals(b1.stripTrailingZeros());
+  }
+
+  // <>
+
+  /** SQL &lt;&gt; operator applied to Object values (including String;
+   * neither side may be null). */
+  public static boolean ne(Object b0, Object b1) {
+    return !b0.equals(b1);
+  }
+
+  /** SQL &lt;&gt; operator applied to BigDecimal values. */
+  public static boolean ne(BigDecimal b0, BigDecimal b1) {
+    return b0.compareTo(b1) != 0;
+  }
+
+  // <
+
+  /** SQL &lt; operator applied to boolean values. */
+  public static boolean lt(boolean b0, boolean b1) {
+    return compare(b0, b1) < 0;
+  }
+
+  /** SQL &lt; operator applied to String values. */
+  public static boolean lt(String b0, String b1) {
+    return b0.compareTo(b1) < 0;
+  }
+
+  /** SQL &lt; operator applied to ByteString values. */
+  public static boolean lt(ByteString b0, ByteString b1) {
+    return b0.compareTo(b1) < 0;
+  }
+
+  /** SQL &lt; operator applied to BigDecimal values. */
+  public static boolean lt(BigDecimal b0, BigDecimal b1) {
+    return b0.compareTo(b1) < 0;
+  }
+
+  // <=
+
+  /** SQL &le; operator applied to boolean values. */
+  public static boolean le(boolean b0, boolean b1) {
+    return compare(b0, b1) <= 0;
+  }
+
+  /** SQL &le; operator applied to String values. */
+  public static boolean le(String b0, String b1) {
+    return b0.compareTo(b1) <= 0;
+  }
+
+  /** SQL &le; operator applied to ByteString values. */
+  public static boolean le(ByteString b0, ByteString b1) {
+    return b0.compareTo(b1) <= 0;
+  }
+
+  /** SQL &le; operator applied to BigDecimal values. */
+  public static boolean le(BigDecimal b0, BigDecimal b1) {
+    return b0.compareTo(b1) <= 0;
+  }
+
+  // >
+
+  /** SQL &gt; operator applied to boolean values. */
+  public static boolean gt(boolean b0, boolean b1) {
+    return compare(b0, b1) > 0;
+  }
+
+  /** SQL &gt; operator applied to String values. */
+  public static boolean gt(String b0, String b1) {
+    return b0.compareTo(b1) > 0;
+  }
+
+  /** SQL &gt; operator applied to ByteString values. */
+  public static boolean gt(ByteString b0, ByteString b1) {
+    return b0.compareTo(b1) > 0;
+  }
+
+  /** SQL &gt; operator applied to BigDecimal values. */
+  public static boolean gt(BigDecimal b0, BigDecimal b1) {
+    return b0.compareTo(b1) > 0;
+  }
+
+  // >=
+
+  /** SQL &ge; operator applied to boolean values. */
+  public static boolean ge(boolean b0, boolean b1) {
+    return compare(b0, b1) >= 0;
+  }
+
+  /** SQL &ge; operator applied to String values. */
+  public static boolean ge(String b0, String b1) {
+    return b0.compareTo(b1) >= 0;
+  }
+
+  /** SQL &ge; operator applied to ByteString values. */
+  public static boolean ge(ByteString b0, ByteString b1) {
+    return b0.compareTo(b1) >= 0;
+  }
+
+  /** SQL &ge; operator applied to BigDecimal values. */
+  public static boolean ge(BigDecimal b0, BigDecimal b1) {
+    return b0.compareTo(b1) >= 0;
+  }
+
+  // +
+
+  /** SQL <code>+</code> operator applied to int values. */
+  public static int plus(int b0, int b1) {
+    return b0 + b1;
+  }
+
+  /** SQL <code>+</code> operator applied to int values; left side may be
+   * null. */
+  public static Integer plus(Integer b0, int b1) {
+    return b0 == null ? null : (b0 + b1);
+  }
+
+  /** SQL <code>+</code> operator applied to int values; right side may be
+   * null. */
+  public static Integer plus(int b0, Integer b1) {
+    return b1 == null ? null : (b0 + b1);
+  }
+
+  /** SQL <code>+</code> operator applied to nullable int values. */
+  public static Integer plus(Integer b0, Integer b1) {
+    return (b0 == null || b1 == null) ? null : (b0 + b1);
+  }
+
+  /** SQL <code>+</code> operator applied to nullable long and int values. */
+  public static Long plus(Long b0, Integer b1) {
+    return (b0 == null || b1 == null)
+        ? null
+        : (b0.longValue() + b1.longValue());
+  }
+
+  /** SQL <code>+</code> operator applied to nullable int and long values. */
+  public static Long plus(Integer b0, Long b1) {
+    return (b0 == null || b1 == null)
+        ? null
+        : (b0.longValue() + b1.longValue());
+  }
+
+  /** SQL <code>+</code> operator applied to BigDecimal values. */
+  public static BigDecimal plus(BigDecimal b0, BigDecimal b1) {
+    return (b0 == null || b1 == null) ? null : b0.add(b1);
+  }
+
+  // -
+
+  /** SQL <code>-</code> operator applied to int values. */
+  public static int minus(int b0, int b1) {
+    return b0 - b1;
+  }
+
+  /** SQL <code>-</code> operator applied to int values; left side may be
+   * null. */
+  public static Integer minus(Integer b0, int b1) {
+    return b0 == null ? null : (b0 - b1);
+  }
+
+  /** SQL <code>-</code> operator applied to int values; right side may be
+   * null. */
+  public static Integer minus(int b0, Integer b1) {
+    return b1 == null ? null : (b0 - b1);
+  }
+
+  /** SQL <code>-</code> operator applied to nullable int values. */
+  public static Integer minus(Integer b0, Integer b1) {
+    return (b0 == null || b1 == null) ? null : (b0 - b1);
+  }
+
+  /** SQL <code>-</code> operator applied to nullable long and int values. */
+  public static Long minus(Long b0, Integer b1) {
+    return (b0 == null || b1 == null)
+        ? null
+        : (b0.longValue() - b1.longValue());
+  }
+
+  /** SQL <code>-</code> operator applied to nullable int and long values. */
+  public static Long minus(Integer b0, Long b1) {
+    return (b0 == null || b1 == null)
+        ? null
+        : (b0.longValue() - b1.longValue());
+  }
+
+  /** SQL <code>-</code> operator applied to BigDecimal values. */
+  public static BigDecimal minus(BigDecimal b0, BigDecimal b1) {
+    return (b0 == null || b1 == null) ? null : b0.subtract(b1);
+  }
+
+  // /
+
+  /** SQL <code>/</code> operator applied to int values. */
+  public static int divide(int b0, int b1) {
+    return b0 / b1;
+  }
+
+  /** SQL <code>/</code> operator applied to int values; left side may be
+   * null. */
+  public static Integer divide(Integer b0, int b1) {
+    return b0 == null ? null : (b0 / b1);
+  }
+
+  /** SQL <code>/</code> operator applied to int values; right side may be
+   * null. */
+  public static Integer divide(int b0, Integer b1) {
+    return b1 == null ? null : (b0 / b1);
+  }
+
+  /** SQL <code>/</code> operator applied to nullable int values. */
+  public static Integer divide(Integer b0, Integer b1) {
+    return (b0 == null || b1 == null) ? null : (b0 / b1);
+  }
+
+  /** SQL <code>/</code> operator applied to nullable long and int values. */
+  public static Long divide(Long b0, Integer b1) {
+    return (b0 == null || b1 == null)
+        ? null
+        : (b0.longValue() / b1.longValue());
+  }
+
+  /** SQL <code>/</code> operator applied to nullable int and long values. */
+  public static Long divide(Integer b0, Long b1) {
+    return (b0 == null || b1 == null)
+        ? null
+        : (b0.longValue() / b1.longValue());
+  }
+
+  /** SQL <code>/</code> operator applied to BigDecimal values. */
+  public static BigDecimal divide(BigDecimal b0, BigDecimal b1) {
+    // OVERRIDE POINT
+    return (b0 == null || b1 == null) ? null : b0.divide(b1, MathContext.DECIMAL64);
+  }
+
+  // *
+
+  /** SQL <code>*</code> operator applied to int values. */
+  public static int multiply(int b0, int b1) {
+    return b0 * b1;
+  }
+
+  /** SQL <code>*</code> operator applied to int values; left side may be
+   * null. */
+  public static Integer multiply(Integer b0, int b1) {
+    return b0 == null ? null : (b0 * b1);
+  }
+
+  /** SQL <code>*</code> operator applied to int values; right side may be
+   * null. */
+  public static Integer multiply(int b0, Integer b1) {
+    return b1 == null ? null : (b0 * b1);
+  }
+
+  /** SQL <code>*</code> operator applied to nullable int values. */
+  public static Integer multiply(Integer b0, Integer b1) {
+    return (b0 == null || b1 == null) ? null : (b0 * b1);
+  }
+
+  /** SQL <code>*</code> operator applied to nullable long and int values. */
+  public static Long multiply(Long b0, Integer b1) {
+    return (b0 == null || b1 == null)
+        ? null
+        : (b0.longValue() * b1.longValue());
+  }
+
+  /** SQL <code>*</code> operator applied to nullable int and long values. */
+  public static Long multiply(Integer b0, Long b1) {
+    return (b0 == null || b1 == null)
+        ? null
+        : (b0.longValue() * b1.longValue());
+  }
+
+  /** SQL <code>*</code> operator applied to BigDecimal values. */
+  public static BigDecimal multiply(BigDecimal b0, BigDecimal b1) {
+    return (b0 == null || b1 == null) ? null : b0.multiply(b1);
+  }
+
+  // EXP
+
+  /** SQL <code>EXP</code> operator applied to double values. */
+  public static double exp(double b0) {
+    return Math.exp(b0);
+  }
+
+  public static double exp(long b0) {
+    return Math.exp(b0);
+  }
+
+  // POWER
+
+  /** SQL <code>POWER</code> operator applied to double values. */
+  public static double power(double b0, double b1) {
+    return Math.pow(b0, b1);
+  }
+
+  public static double power(long b0, long b1) {
+    return Math.pow(b0, b1);
+  }
+
+  public static double power(long b0, BigDecimal b1) {
+    return Math.pow(b0, b1.doubleValue());
+  }
+
+  // LN
+
+  /** SQL {@code LN(number)} function applied to double values. */
+  public static double ln(double d) {
+    return Math.log(d);
+  }
+
+  /** SQL {@code LN(number)} function applied to long values. */
+  public static double ln(long b0) {
+    return Math.log(b0);
+  }
+
+  /** SQL {@code LN(number)} function applied to BigDecimal values. */
+  public static double ln(BigDecimal d) {
+    return Math.log(d.doubleValue());
+  }
+
+  // LOG10
+
+  /** SQL <code>LOG10(numeric)</code> operator applied to double values. */
+  public static double log10(double b0) {
+    return Math.log10(b0);
+  }
+
+  /** SQL {@code LOG10(number)} function applied to long values. */
+  public static double log10(long b0) {
+    return Math.log10(b0);
+  }
+
+  /** SQL {@code LOG10(number)} function applied to BigDecimal values. */
+  public static double log10(BigDecimal d) {
+    return Math.log10(d.doubleValue());
+  }
+
+  // MOD
+
+  /** SQL <code>MOD</code> operator applied to byte values. */
+  public static byte mod(byte b0, byte b1) {
+    return (byte) (b0 % b1);
+  }
+
+  /** SQL <code>MOD</code> operator applied to short values. */
+  public static short mod(short b0, short b1) {
+    return (short) (b0 % b1);
+  }
+
+  /** SQL <code>MOD</code> operator applied to int values. */
+  public static int mod(int b0, int b1) {
+    return b0 % b1;
+  }
+
+  /** SQL <code>MOD</code> operator applied to long values. */
+  public static long mod(long b0, long b1) {
+    return b0 % b1;
+  }
+
+  // temporary
+  public static BigDecimal mod(BigDecimal b0, int b1) {
+    return mod(b0, BigDecimal.valueOf(b1));
+  }
+
+  // temporary
+  public static int mod(int b0, BigDecimal b1) {
+    return mod(b0, b1.intValue());
+  }
+
+  public static BigDecimal mod(BigDecimal b0, BigDecimal b1) {
+    final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
+    return bigDecimals[1];
+  }
+
+  // ABS
+
+  /** SQL <code>ABS</code> operator applied to byte values. */
+  public static byte abs(byte b0) {
+    return (byte) Math.abs(b0);
+  }
+
+  /** SQL <code>ABS</code> operator applied to short values. */
+  public static short abs(short b0) {
+    return (short) Math.abs(b0);
+  }
+
+  /** SQL <code>ABS</code> operator applied to int values. */
+  public static int abs(int b0) {
+    return Math.abs(b0);
+  }
+
+  /** SQL <code>ABS</code> operator applied to long values. */
+  public static long abs(long b0) {
+    return Math.abs(b0);
+  }
+
+  /** SQL <code>ABS</code> operator applied to float values. */
+  public static float abs(float b0) {
+    return Math.abs(b0);
+  }
+
+  /** SQL <code>ABS</code> operator applied to double values. */
+  public static double abs(double b0) {
+    return Math.abs(b0);
+  }
+
+  /** SQL <code>ABS</code> operator applied to BigDecimal values. */
+  public static BigDecimal abs(BigDecimal b0) {
+    return b0.abs();
+  }
+
+  // Helpers
+
+  /** Helper for implementing MIN. Somewhat similar to LEAST operator. */
+  public static <T extends Comparable<T>> T lesser(T b0, T b1) {
+    return b0 == null || b0.compareTo(b1) > 0 ? b1 : b0;
+  }
+
+  /** LEAST operator. */
+  public static <T extends Comparable<T>> T least(T b0, T b1) {
+    return b0 == null || b1 != null && b0.compareTo(b1) > 0 ? b1 : b0;
+  }
+
+  public static boolean greater(boolean b0, boolean b1) {
+    return b0 || b1;
+  }
+
+  public static boolean lesser(boolean b0, boolean b1) {
+    return b0 && b1;
+  }
+
+  public static byte greater(byte b0, byte b1) {
+    return b0 > b1 ? b0 : b1;
+  }
+
+  public static byte lesser(byte b0, byte b1) {
+    return b0 > b1 ? b1 : b0;
+  }
+
+  public static char greater(char b0, char b1) {
+    return b0 > b1 ? b0 : b1;
+  }
+
+  public static char lesser(char b0, char b1) {
+    return b0 > b1 ? b1 : b0;
+  }
+
+  public static short greater(short b0, short b1) {
+    return b0 > b1 ? b0 : b1;
+  }
+
+  public static short lesser(short b0, short b1) {
+    return b0 > b1 ? b1 : b0;
+  }
+
+  public static int greater(int b0, int b1) {
+    return b0 > b1 ? b0 : b1;
+  }
+
+  public static int lesser(int b0, int b1) {
+    return b0 > b1 ? b1 : b0;
+  }
+
+  public static long greater(long b0, long b1) {
+    return b0 > b1 ? b0 : b1;
+  }
+
+  public static long lesser(long b0, long b1) {
+    return b0 > b1 ? b1 : b0;
+  }
+
+  public static float greater(float b0, float b1) {
+    return b0 > b1 ? b0 : b1;
+  }
+
+  public static float lesser(float b0, float b1) {
+    return b0 > b1 ? b1 : b0;
+  }
+
+  public static double greater(double b0, double b1) {
+    return b0 > b1 ? b0 : b1;
+  }
+
+  public static double lesser(double b0, double b1) {
+    return b0 > b1 ? b1 : b0;
+  }
+
+  /** Helper for implementing MAX. Somewhat similar to GREATEST operator. */
+  public static <T extends Comparable<T>> T greater(T b0, T b1) {
+    return b0 == null || b0.compareTo(b1) < 0 ? b1 : b0;
+  }
+
+  /** GREATEST operator. */
+  public static <T extends Comparable<T>> T greatest(T b0, T b1) {
+    return b0 == null || b1 != null && b0.compareTo(b1) < 0 ? b1 : b0;
+  }
+
+  /** Boolean comparison. */
+  public static int compare(boolean x, boolean y) {
+    return x == y ? 0 : x ? 1 : -1;
+  }
+
+  /** CAST(FLOAT AS VARCHAR). */
+  public static String toString(float x) {
+    if (x == 0) {
+      return "0E0";
+    }
+    BigDecimal bigDecimal =
+        new BigDecimal(x, MathContext.DECIMAL32).stripTrailingZeros();
+    final String s = bigDecimal.toString();
+    return s.replaceAll("0*E", "E").replace("E+", "E");
+  }
+
+  /** CAST(DOUBLE AS VARCHAR). */
+  public static String toString(double x) {
+    if (x == 0) {
+      return "0E0";
+    }
+    BigDecimal bigDecimal =
+        new BigDecimal(x, MathContext.DECIMAL64).stripTrailingZeros();
+    final String s = bigDecimal.toString();
+    return s.replaceAll("0*E", "E").replace("E+", "E");
+  }
+
+  /** CAST(DECIMAL AS VARCHAR). */
+  public static String toString(BigDecimal x) {
+    final String s = x.toString();
+    if (s.startsWith("0")) {
+      // we want ".1" not "0.1"
+      return s.substring(1);
+    } else if (s.startsWith("-0")) {
+      // we want "-.1" not "-0.1"
+      return "-" + s.substring(2);
+    } else {
+      return s;
+    }
+  }
+
+  /** CAST(BOOLEAN AS VARCHAR). */
+  public static String toString(boolean x) {
+    // Boolean.toString returns lower case -- no good.
+    return x ? "TRUE" : "FALSE";
+  }
+
+  @NonDeterministic
+  private static Object cannotConvert(Object o, Class toType) {
+    throw new RuntimeException("Cannot convert " + o + " to " + toType);
+  }
+
+  /** CAST(VARCHAR AS BOOLEAN). */
+  public static boolean toBoolean(String s) {
+    s = trim_(s, true, true, ' ');
+    if (s.equalsIgnoreCase("TRUE")) {
+      return true;
+    } else if (s.equalsIgnoreCase("FALSE")) {
+      return false;
+    } else {
+      throw new RuntimeException("Invalid character for cast");
+    }
+  }
+
+  public static boolean toBoolean(Number number) {
+    return !number.equals(0);
+  }
+
+  public static boolean toBoolean(Object o) {
+    return o instanceof Boolean ? (Boolean) o
+        : o instanceof Number ? toBoolean((Number) o)
+        : o instanceof String ? toBoolean((String) o)
+        : (Boolean) cannotConvert(o, boolean.class);
+  }
+
+  // Don't need parseByte etc. - Byte.parseByte is sufficient.
+
+  public static byte toByte(Object o) {
+    return o instanceof Byte ? (Byte) o
+        : o instanceof Number ? toByte((Number) o)
+        : Byte.parseByte(o.toString());
+  }
+
+  public static byte toByte(Number number) {
+    return number.byteValue();
+  }
+
+  public static char toChar(String s) {
+    return s.charAt(0);
+  }
+
+  public static Character toCharBoxed(String s) {
+    return s.charAt(0);
+  }
+
+  public static short toShort(String s) {
+    return Short.parseShort(s.trim());
+  }
+
+  public static short toShort(Number number) {
+    return number.shortValue();
+  }
+
+  public static short toShort(Object o) {
+    return o instanceof Short ? (Short) o
+        : o instanceof Number ? toShort((Number) o)
+        : o instanceof String ? toShort((String) o)
+        : (Short) cannotConvert(o, short.class);
+  }
+
+  public static int toInt(java.util.Date v) {
+    return toInt(v, LOCAL_TZ);
+  }
+
+  public static int toInt(java.util.Date v, TimeZone timeZone) {
+    return (int) (toLong(v, timeZone)  / DateTimeUtils.MILLIS_PER_DAY);
+  }
+
+  public static Integer toIntOptional(java.util.Date v) {
+    return v == null ? null : toInt(v);
+  }
+
+  public static Integer toIntOptional(java.util.Date v, TimeZone timeZone) {
+    return v == null
+        ? null
+        : toInt(v, timeZone);
+  }
+
+  public static long toLong(Date v) {
+    return toLong(v, LOCAL_TZ);
+  }
+
+  public static int toInt(java.sql.Time v) {
+    return (int) (toLong(v) % DateTimeUtils.MILLIS_PER_DAY);
+  }
+
+  public static Integer toIntOptional(java.sql.Time v) {
+    return v == null ? null : toInt(v);
+  }
+
+  public static int toInt(String s) {
+    return Integer.parseInt(s.trim());
+  }
+
+  public static int toInt(Number number) {
+    return number.intValue();
+  }
+
+  public static int toInt(Object o) {
+    return o instanceof Integer ? (Integer) o
+        : o instanceof Number ? toInt((Number) o)
+        : o instanceof String ? toInt((String) o)
+        : (Integer) cannotConvert(o, int.class);
+  }
+
+  public static long toLong(Timestamp v) {
+    return toLong(v, LOCAL_TZ);
+  }
+
+  // mainly intended for java.sql.Timestamp but works for other dates also
+  public static long toLong(java.util.Date v, TimeZone timeZone) {
+    final long time = v.getTime();
+    return time + timeZone.getOffset(time);
+  }
+
+  // mainly intended for java.sql.Timestamp but works for other dates also
+  public static Long toLongOptional(java.util.Date v) {
+    return v == null ? null : toLong(v, LOCAL_TZ);
+  }
+
+  public static Long toLongOptional(Timestamp v, TimeZone timeZone) {
+    if (v == null) {
+      return null;
+    }
+    return toLong(v, LOCAL_TZ);
+  }
+
+  public static long toLong(String s) {
+    if (s.startsWith("199") && s.contains(":")) {
+      return Timestamp.valueOf(s).getTime();
+    }
+    return Long.parseLong(s.trim());
+  }
+
+  public static long toLong(Number number) {
+    return number.longValue();
+  }
+
+  public static long toLong(Object o) {
+    return o instanceof Long ? (Long) o
+        : o instanceof Number ? toLong((Number) o)
+        : o instanceof String ? toLong((String) o)
+        : (Long) cannotConvert(o, long.class);
+  }
+
+  public static float toFloat(String s) {
+    return Float.parseFloat(s.trim());
+  }
+
+  public static float toFloat(Number number) {
+    return number.floatValue();
+  }
+
+  public static float toFloat(Object o) {
+    return o instanceof Float ? (Float) o
+        : o instanceof Number ? toFloat((Number) o)
+            : o instanceof String ? toFloat((String) o)
+                : (Float) cannotConvert(o, float.class);
+  }
+
+  public static double toDouble(String s) {
+    return Double.parseDouble(s.trim());
+  }
+
+  public static double toDouble(Number number) {
+    return number.doubleValue();
+  }
+
+  public static double toDouble(Object o) {
+    return o instanceof Double ? (Double) o
+        : o instanceof Number ? toDouble((Number) o)
+        : o instanceof String ? toDouble((String) o)
+        : (Double) cannotConvert(o, double.class);
+  }
+
+  public static BigDecimal toBigDecimal(String s) {
+    return new BigDecimal(s.trim());
+  }
+
+  public static BigDecimal toBigDecimal(Number number) {
+    // There are some values of "long" that cannot be represented as "double".
+    // Not so "int". If it isn't a long, go straight to double.
+    return number instanceof BigDecimal ? (BigDecimal) number
+        : number instanceof BigInteger ? new BigDecimal((BigInteger) number)
+        : number instanceof Long ? new BigDecimal(number.longValue())
+        : new BigDecimal(number.doubleValue());
+  }
+
+  public static BigDecimal toBigDecimal(Object o) {
+    return o instanceof Number ? toBigDecimal((Number) o)
+        : toBigDecimal(o.toString());
+  }
+
+  // Don't need shortValueOf etc. - Short.valueOf is sufficient.
+
+  /** Helper for CAST(... AS VARCHAR(maxLength)). */
+  public static String truncate(String s, int maxLength) {
+    return s == null ? null
+        : s.length() > maxLength ? s.substring(0, maxLength)
+        : s;
+  }
+
+  /** Helper for CAST(... AS VARBINARY(maxLength)). */
+  public static ByteString truncate(ByteString s, int maxLength) {
+    return s == null ? null
+        : s.length() > maxLength ? s.substring(0, maxLength)
+        : s;
+  }
+
+  /** SQL {@code POSITION(seek IN string)} function. */
+  public static int position(String seek, String s) {
+    return s.indexOf(seek) + 1;
+  }
+
+  /** SQL {@code POSITION(seek IN string)} function. */
+  public static int position(ByteString seek, ByteString s) {
+    return s.indexOf(seek) + 1;
+  }
+
+  /** Helper for rounding. Truncate(12345, 1000) returns 12000. */
+  public static long round(long v, long x) {
+    return truncate(v + x / 2, x);
+  }
+
+  /** Helper for rounding. Truncate(12345, 1000) returns 12000. */
+  public static long truncate(long v, long x) {
+    long remainder = v % x;
+    if (remainder < 0) {
+      remainder += x;
+    }
+    return v - remainder;
+  }
+
+  /** Helper for rounding. Truncate(12345, 1000) returns 12000. */
+  public static int round(int v, int x) {
+    return truncate(v + x / 2, x);
+  }
+
+  /** Helper for rounding. Truncate(12345, 1000) returns 12000. */
+  public static int truncate(int v, int x) {
+    int remainder = v % x;
+    if (remainder < 0) {
+      remainder += x;
+    }
+    return v - remainder;
+  }
+
+  /** SQL {@code CURRENT_TIMESTAMP} function. */
+  @NonDeterministic
+  public static long currentTimestamp(DataContext root) {
+    // Cast required for JDK 1.6.
+    return (Long) DataContext.Variable.CURRENT_TIMESTAMP.get(root);
+  }
+
+  /** SQL {@code CURRENT_TIME} function. */
+  @NonDeterministic
+  public static int currentTime(DataContext root) {
+    int time = (int) (currentTimestamp(root) % DateTimeUtils.MILLIS_PER_DAY);
+    if (time < 0) {
+      time += DateTimeUtils.MILLIS_PER_DAY;
+    }
+    return time;
+  }
+
+  /** SQL {@code CURRENT_DATE} function. */
+  @NonDeterministic
+  public static int currentDate(DataContext root) {
+    final long timestamp = currentTimestamp(root);
+    int date = (int) (timestamp / DateTimeUtils.MILLIS_PER_DAY);
+    final int time = (int) (timestamp % DateTimeUtils.MILLIS_PER_DAY);
+    if (time < 0) {
+      --date;
+    }
+    return date;
+  }
+
+  /** SQL {@code LOCAL_TIMESTAMP} function. */
+  @NonDeterministic
+  public static long localTimestamp(DataContext root) {
+    // Cast required for JDK 1.6.
+    return (Long) DataContext.Variable.LOCAL_TIMESTAMP.get(root);
+  }
+
+  /** SQL {@code LOCAL_TIME} function. */
+  @NonDeterministic
+  public static int localTime(DataContext root) {
+    return (int) (localTimestamp(root) % DateTimeUtils.MILLIS_PER_DAY);
+  }
+
+  /** Helper for "array element reference". Caller has already ensured that
+   * array and index are not null. Index is 1-based, per SQL. */
+  public static Object arrayItem(List list, int item) {
+    if (item < 1 || item > list.size()) {
+      return null;
+    }
+    return list.get(item - 1);
+  }
+
+  /** Helper for "map element reference". Caller has already ensured that
+   * array and index are not null. Index is 1-based, per SQL. */
+  public static Object mapItem(Map map, Object item) {
+    return map.get(item);
+  }
+
+  /** Implements the {@code [ ... ]} operator on an object whose type is not
+   * known until runtime.
+   */
+  public static Object item(Object object, Object index) {
+    if (object instanceof Map) {
+      return ((Map) object).get(index);
+    }
+    if (object instanceof List && index instanceof Number) {
+      List list = (List) object;
+      return list.get(((Number) index).intValue());
+    }
+    return null;
+  }
+
+  /** NULL &rarr; FALSE, FALSE &rarr; FALSE, TRUE &rarr; TRUE. */
+  public static boolean isTrue(Boolean b) {
+    return b != null && b;
+  }
+
+  /** NULL &rarr; TRUE, FALSE &rarr; FALSE, TRUE &rarr; TRUE. */
+  public static boolean isNotFalse(Boolean b) {
+    return b == null || b;
+  }
+
+  /** NULL &rarr; NULL, FALSE &rarr; TRUE, TRUE &rarr; FALSE. */
+  public static Boolean not(Boolean b) {
+    return (b == null) ? null : !b;
+  }
+
+  /** Converts a JDBC array to a list. */
+  public static List arrayToList(final java.sql.Array a) {
+    if (a == null) {
+      return null;
+    }
+    try {
+      return Primitive.asList(a.getArray());
+    } catch (SQLException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  /** Support the {@code CURRENT VALUE OF sequence} operator. */
+  @NonDeterministic
+  public static long sequenceCurrentValue(String key) {
+    return getAtomicLong(key).get();
+  }
+
+  /** Support the {@code NEXT VALUE OF sequence} operator. */
+  @NonDeterministic
+  public static long sequenceNextValue(String key) {
+    return getAtomicLong(key).incrementAndGet();
+  }
+
+  private static AtomicLong getAtomicLong(String key) {
+    final Map<String, AtomicLong> map = THREAD_SEQUENCES.get();
+    AtomicLong atomic = map.get(key);
+    if (atomic == null) {
+      atomic = new AtomicLong();
+      map.put(key, atomic);
+    }
+    return atomic;
+  }
+
+  /** Support the SLICE function. */
+  public static List slice(List list) {
+    return list;
+  }
+
+  /** Support the ELEMENT function. */
+  public static Object element(List list) {
+    switch (list.size()) {
+    case 0:
+      return null;
+    case 1:
+      return list.get(0);
+    default:
+      throw new RuntimeException("more than one value");
+    }
+  }
+
+  /** Returns a lambda that converts a list to an enumerable. */
+  public static <E> Function1<List<E>, Enumerable<E>> listToEnumerable() {
+    //noinspection unchecked
+    return (Function1<List<E>, Enumerable<E>>) (Function1) LIST_AS_ENUMERABLE;
+  }
+
+}
+
+// End SqlFunctions.java


[09/10] incubator-kylin git commit: KYLIN-780 Upgrade query module, all query test pass

Posted by li...@apache.org.
KYLIN-780 Upgrade query module, all query test pass


Project: http://git-wip-us.apache.org/repos/asf/incubator-kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kylin/commit/22a0bc33
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kylin/tree/22a0bc33
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kylin/diff/22a0bc33

Branch: refs/heads/KYLIN-780-0.8
Commit: 22a0bc33432c84b366fd5285b43c7bec657ceaa8
Parents: 6331237
Author: Yang Li <li...@apache.org>
Authored: Tue Jun 16 07:53:53 2015 +0800
Committer: Li, Yang <ya...@ebay.com>
Committed: Fri Jul 3 16:37:25 2015 +0800

----------------------------------------------------------------------
 .../hydromatic/optiq/runtime/SqlFunctions.java  | 1705 ------
 .../adapter/enumerable/EnumerableJoin.java      |  187 +
 .../apache/calcite/runtime/SqlFunctions.java    | 1261 +++++
 .../calcite/sql2rel/SqlToRelConverter.java      | 4978 ++++++++++++++++++
 .../eigenbase/sql2rel/SqlToRelConverter.java    | 4795 -----------------
 jdbc/pom.xml                                    |    2 +-
 .../main/java/org/apache/kylin/jdbc/Driver.java |  287 +-
 .../org/apache/kylin/jdbc/IRemoteClient.java    |   56 +
 .../java/org/apache/kylin/jdbc/KylinClient.java |  373 ++
 .../org/apache/kylin/jdbc/KylinConnection.java  |  128 +
 .../apache/kylin/jdbc/KylinConnectionImpl.java  |  158 -
 .../org/apache/kylin/jdbc/KylinEnumerator.java  |   87 -
 .../apache/kylin/jdbc/KylinJdbc40Factory.java   |   30 -
 .../apache/kylin/jdbc/KylinJdbc41Factory.java   |  127 -
 .../org/apache/kylin/jdbc/KylinJdbcFactory.java |  108 +
 .../java/org/apache/kylin/jdbc/KylinMeta.java   |  341 ++
 .../org/apache/kylin/jdbc/KylinMetaImpl.java    |  827 ---
 .../org/apache/kylin/jdbc/KylinPrepare.java     |  103 -
 .../org/apache/kylin/jdbc/KylinPrepareImpl.java |   47 -
 .../kylin/jdbc/KylinPrepareStatementImpl.java   |  170 -
 .../kylin/jdbc/KylinPreparedStatement.java      |  100 +
 .../org/apache/kylin/jdbc/KylinResultSet.java   |  117 +-
 .../org/apache/kylin/jdbc/KylinStatement.java   |   31 +
 .../apache/kylin/jdbc/KylinStatementImpl.java   |   54 -
 .../kylin/jdbc/json/PreparedQueryRequest.java   |   33 +
 .../apache/kylin/jdbc/json/QueryRequest.java    |   40 +
 .../apache/kylin/jdbc/json/SQLResponseStub.java |  321 ++
 .../kylin/jdbc/json/StatementParameter.java     |   47 +
 .../apache/kylin/jdbc/json/TableMetaStub.java   |  345 ++
 .../kylin/jdbc/stub/ConnectionException.java    |   55 -
 .../org/apache/kylin/jdbc/stub/DataSet.java     |   55 -
 .../org/apache/kylin/jdbc/stub/KylinClient.java |  374 --
 .../kylin/jdbc/stub/KylinColumnMetaData.java    |   38 -
 .../apache/kylin/jdbc/stub/RemoteClient.java    |   59 -
 .../apache/kylin/jdbc/stub/SQLResponseStub.java |  323 --
 .../apache/kylin/jdbc/stub/TableMetaStub.java   |  347 --
 .../util/DefaultSslProtocolSocketFactory.java   |  298 +-
 .../org/apache/kylin/jdbc/util/SQLTypeMap.java  |  183 -
 .../java/org/apache/kylin/jdbc/DriverTest.java  |  339 +-
 .../java/org/apache/kylin/jdbc/DummyClient.java |  165 +-
 .../java/org/apache/kylin/jdbc/DummyDriver.java |   64 +-
 .../apache/kylin/jdbc/DummyJdbc41Factory.java   |   35 -
 .../org/apache/kylin/jdbc/DummyJdbcFactory.java |   39 +
 .../apache/kylin/metadata/model/ColumnDesc.java |    4 -
 .../apache/kylin/metadata/model/DataType.java   |  610 +--
 .../kylin/metadata/model/FunctionDesc.java      |   15 +-
 pom.xml                                         |   19 +-
 query/pom.xml                                   |    6 +-
 .../java/org/apache/kylin/query/QueryCli.java   |    4 +-
 .../kylin/query/enumerator/HiveEnumerator.java  |    6 +-
 .../query/enumerator/LookupTableEnumerator.java |  211 +-
 .../kylin/query/enumerator/OLAPEnumerator.java  |   15 +-
 .../kylin/query/enumerator/OLAPQuery.java       |   12 +-
 .../kylin/query/optrule/OLAPAggregateRule.java  |   22 +-
 .../kylin/query/optrule/OLAPFilterRule.java     |   18 +-
 .../kylin/query/optrule/OLAPJoinRule.java       |   28 +-
 .../kylin/query/optrule/OLAPLimitRule.java      |   19 +-
 .../kylin/query/optrule/OLAPProjectRule.java    |   18 +-
 .../kylin/query/optrule/OLAPSortRule.java       |   19 +-
 .../optrule/OLAPToEnumerableConverterRule.java  |   13 +-
 .../kylin/query/relnode/OLAPAggregateRel.java   |  155 +-
 .../apache/kylin/query/relnode/OLAPContext.java |    6 +-
 .../kylin/query/relnode/OLAPFilterRel.java      |   90 +-
 .../apache/kylin/query/relnode/OLAPJoinRel.java |   90 +-
 .../kylin/query/relnode/OLAPLimitRel.java       |   54 +-
 .../kylin/query/relnode/OLAPProjectRel.java     |  100 +-
 .../org/apache/kylin/query/relnode/OLAPRel.java |   62 +-
 .../apache/kylin/query/relnode/OLAPSortRel.java |   62 +-
 .../kylin/query/relnode/OLAPTableScan.java      |  103 +-
 .../relnode/OLAPToEnumerableConverter.java      |   55 +-
 .../apache/kylin/query/schema/OLAPSchema.java   |    6 +-
 .../kylin/query/schema/OLAPSchemaFactory.java   |   22 +-
 .../apache/kylin/query/schema/OLAPTable.java    |   52 +-
 .../apache/kylin/query/sqlfunc/QuarterBase.java |   38 -
 .../apache/kylin/query/sqlfunc/QuarterFunc.java |   34 -
 .../apache/kylin/query/test/KylinTestBase.java  |    2 +-
 .../kylin/rest/controller/QueryController.java  |   30 +-
 .../apache/kylin/rest/service/BasicService.java |   45 +-
 .../apache/kylin/rest/service/ModelService.java |   14 +-
 .../apache/kylin/rest/service/QueryService.java |   16 +-
 .../org/apache/kylin/rest/util/QueryUtil.java   |   22 +-
 81 files changed, 10030 insertions(+), 11299 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/atopcalcite/src/main/java/net/hydromatic/optiq/runtime/SqlFunctions.java
----------------------------------------------------------------------
diff --git a/atopcalcite/src/main/java/net/hydromatic/optiq/runtime/SqlFunctions.java b/atopcalcite/src/main/java/net/hydromatic/optiq/runtime/SqlFunctions.java
deleted file mode 100644
index db99c83..0000000
--- a/atopcalcite/src/main/java/net/hydromatic/optiq/runtime/SqlFunctions.java
+++ /dev/null
@@ -1,1705 +0,0 @@
-/*
- * OVERRIDE POINT:
- * - divide(BigDecimal,BigDecimal), was `b0.divide(b1)`, now `b0.divide(b1, MathContext.DECIMAL64);` 
- */
-
-/*
- * 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 net.hydromatic.optiq.runtime;
-
-import net.hydromatic.avatica.ByteString;
-
-import net.hydromatic.linq4j.Enumerable;
-import net.hydromatic.linq4j.Linq4j;
-import net.hydromatic.linq4j.expressions.Primitive;
-import net.hydromatic.linq4j.function.Deterministic;
-import net.hydromatic.linq4j.function.Function1;
-import net.hydromatic.linq4j.function.NonDeterministic;
-
-import net.hydromatic.optiq.DataContext;
-
-import org.eigenbase.util14.DateTimeUtil;
-
-import java.math.*;
-import java.sql.SQLException;
-import java.sql.Timestamp;
-import java.text.DecimalFormat;
-import java.util.*;
-import java.util.regex.Pattern;
-
-/**
- * Helper methods to implement SQL functions in generated code.
- *
- * <p>Not present: and, or, not (builtin operators are better, because they
- * use lazy evaluation. Implementations do not check for null values; the
- * calling code must do that.</p>
- *
- * <p>Many of the functions do not check for null values. This is intentional.
- * If null arguments are possible, the code-generation framework checks for
- * nulls before calling the functions.</p>
- */
-@SuppressWarnings("UnnecessaryUnboxing")
-@Deterministic
-public class SqlFunctions {
-  private static final DecimalFormat DOUBLE_FORMAT =
-      new DecimalFormat("0.0E0");
-
-  /** The julian date of the epoch, 1970-01-01. */
-  public static final int EPOCH_JULIAN = 2440588;
-
-  private static final TimeZone LOCAL_TZ = TimeZone.getDefault();
-
-  private static final Function1<List<Object>, Enumerable<Object>>
-  LIST_AS_ENUMERABLE =
-      new Function1<List<Object>, Enumerable<Object>>() {
-        public Enumerable<Object> apply(List<Object> list) {
-          return Linq4j.asEnumerable(list);
-        }
-      };
-
-  private SqlFunctions() {
-  }
-
-  /** SQL SUBSTRING(string FROM ... FOR ...) function. */
-  public static String substring(String s, int from, int for_) {
-    return s.substring(from - 1, Math.min(from - 1 + for_, s.length()));
-  }
-
-  /** SQL SUBSTRING(string FROM ...) function. */
-  public static String substring(String s, int from) {
-    return s.substring(from - 1);
-  }
-
-  /** SQL UPPER(string) function. */
-  public static String upper(String s) {
-    return s.toUpperCase();
-  }
-
-  /** SQL LOWER(string) function. */
-  public static String lower(String s) {
-    return s.toLowerCase();
-  }
-
-  /** SQL INITCAP(string) function. */
-  public static String initcap(String s) {
-    // Assumes Alpha as [A-Za-z0-9]
-    // white space is treated as everything else.
-    final int len = s.length();
-    boolean start = true;
-    final StringBuilder newS = new StringBuilder();
-
-    for (int i = 0; i < len; i++) {
-      char curCh = s.charAt(i);
-      final int c = (int) curCh;
-      if (start) {  // curCh is whitespace or first character of word.
-        if (c > 47 && c < 58) { // 0-9
-          start = false;
-        } else if (c > 64 && c < 91) {  // A-Z
-          start = false;
-        } else if (c > 96 && c < 123) {  // a-z
-          start = false;
-          curCh = (char) (c - 32); // Uppercase this character
-        }
-        // else {} whitespace
-      } else {  // Inside of a word or white space after end of word.
-        if (c > 47 && c < 58) { // 0-9
-          // noop
-        } else if (c > 64 && c < 91) {  // A-Z
-          curCh = (char) (c + 32); // Lowercase this character
-        } else if (c > 96 && c < 123) {  // a-z
-          // noop
-        } else { // whitespace
-          start = true;
-        }
-      }
-      newS.append(curCh);
-    } // for each character in s
-    return newS.toString();
-  }
-
-  /** SQL CHARACTER_LENGTH(string) function. */
-  public static int charLength(String s) {
-    return s.length();
-  }
-
-  /** SQL {@code string || string} operator. */
-  public static String concat(String s0, String s1) {
-    return s0 + s1;
-  }
-
-  /** SQL {@code binary || binary} operator. */
-  public static ByteString concat(ByteString s0, ByteString s1) {
-    return s0.concat(s1);
-  }
-
-  /** SQL {@code RTRIM} function applied to string. */
-  public static String rtrim(String s) {
-    return trim_(s, false, true, ' ');
-  }
-
-  /** SQL {@code LTRIM} function. */
-  public static String ltrim(String s) {
-    return trim_(s, true, false, ' ');
-  }
-
-  /** SQL {@code TRIM(... seek FROM s)} function. */
-  public static String trim(boolean leading, boolean trailing, String seek,
-      String s) {
-    return trim_(s, leading, trailing, seek.charAt(0));
-  }
-
-  /** SQL {@code TRIM} function. */
-  private static String trim_(String s, boolean left, boolean right, char c) {
-    int j = s.length();
-    if (right) {
-      for (;;) {
-        if (j == 0) {
-          return "";
-        }
-        if (s.charAt(j - 1) != c) {
-          break;
-        }
-        --j;
-      }
-    }
-    int i = 0;
-    if (left) {
-      for (;;) {
-        if (i == j) {
-          return "";
-        }
-        if (s.charAt(i) != c) {
-          break;
-        }
-        ++i;
-      }
-    }
-    return s.substring(i, j);
-  }
-
-  /** SQL {@code TRIM} function applied to binary string. */
-  public static ByteString trim(ByteString s) {
-    return trim_(s, true, true);
-  }
-
-  /** Helper for CAST. */
-  public static ByteString rtrim(ByteString s) {
-    return trim_(s, false, true);
-  }
-
-  /** SQL {@code TRIM} function applied to binary string. */
-  private static ByteString trim_(ByteString s, boolean left, boolean right) {
-    int j = s.length();
-    if (right) {
-      for (;;) {
-        if (j == 0) {
-          return ByteString.EMPTY;
-        }
-        if (s.byteAt(j - 1) != 0) {
-          break;
-        }
-        --j;
-      }
-    }
-    int i = 0;
-    if (left) {
-      for (;;) {
-        if (i == j) {
-          return ByteString.EMPTY;
-        }
-        if (s.byteAt(i) != 0) {
-          break;
-        }
-        ++i;
-      }
-    }
-    return s.substring(i, j);
-  }
-
-  /** SQL {@code OVERLAY} function. */
-  public static String overlay(String s, String r, int start) {
-    if (s == null || r == null) {
-      return null;
-    }
-    return s.substring(0, start - 1)
-        + r
-        + s.substring(start - 1 + r.length());
-  }
-
-  /** SQL {@code OVERLAY} function. */
-  public static String overlay(String s, String r, int start, int length) {
-    if (s == null || r == null) {
-      return null;
-    }
-    return s.substring(0, start - 1)
-        + r
-        + s.substring(start - 1 + length);
-  }
-
-  /** SQL {@code OVERLAY} function applied to binary strings. */
-  public static ByteString overlay(ByteString s, ByteString r, int start) {
-    if (s == null || r == null) {
-      return null;
-    }
-    return s.substring(0, start - 1)
-           .concat(r)
-           .concat(s.substring(start - 1 + r.length()));
-  }
-
-  /** SQL {@code OVERLAY} function applied to binary strings. */
-  public static ByteString overlay(ByteString s, ByteString r, int start,
-      int length) {
-    if (s == null || r == null) {
-      return null;
-    }
-    return s.substring(0, start - 1)
-           .concat(r)
-           .concat(s.substring(start - 1 + length));
-  }
-
-  /** SQL {@code LIKE} function. */
-  public static boolean like(String s, String pattern) {
-    final String regex = Like.sqlToRegexLike(pattern, null);
-    return Pattern.matches(regex, s);
-  }
-
-  /** SQL {@code LIKE} function with escape. */
-  public static boolean like(String s, String pattern, String escape) {
-    final String regex = Like.sqlToRegexLike(pattern, escape);
-    return Pattern.matches(regex, s);
-  }
-
-  /** SQL {@code SIMILAR} function. */
-  public static boolean similar(String s, String pattern) {
-    final String regex = Like.sqlToRegexSimilar(pattern, null);
-    return Pattern.matches(regex, s);
-  }
-
-  /** SQL {@code SIMILAR} function with escape. */
-  public static boolean similar(String s, String pattern, String escape) {
-    final String regex = Like.sqlToRegexSimilar(pattern, escape);
-    return Pattern.matches(regex, s);
-  }
-
-  // =
-
-  /** SQL = operator applied to Object values (including String; neither
-   * side may be null). */
-  public static boolean eq(Object b0, Object b1) {
-    return b0.equals(b1);
-  }
-
-  /** SQL = operator applied to BigDecimal values (neither may be null). */
-  public static boolean eq(BigDecimal b0, BigDecimal b1) {
-    return b0.stripTrailingZeros().equals(b1.stripTrailingZeros());
-  }
-
-  // <>
-
-  /** SQL &lt;&gt; operator applied to Object values (including String;
-   * neither side may be null). */
-  public static boolean ne(Object b0, Object b1) {
-    return !b0.equals(b1);
-  }
-
-  /** SQL &lt;&gt; operator applied to BigDecimal values. */
-  public static boolean ne(BigDecimal b0, BigDecimal b1) {
-    return b0.compareTo(b1) != 0;
-  }
-
-  // <
-
-  /** SQL &lt; operator applied to boolean values. */
-  public static boolean lt(boolean b0, boolean b1) {
-    return compare(b0, b1) < 0;
-  }
-
-  /** SQL &lt; operator applied to String values. */
-  public static boolean lt(String b0, String b1) {
-    return b0.compareTo(b1) < 0;
-  }
-
-  /** SQL &lt; operator applied to ByteString values. */
-  public static boolean lt(ByteString b0, ByteString b1) {
-    return b0.compareTo(b1) < 0;
-  }
-
-  /** SQL &lt; operator applied to BigDecimal values. */
-  public static boolean lt(BigDecimal b0, BigDecimal b1) {
-    return b0.compareTo(b1) < 0;
-  }
-
-  // <=
-
-  /** SQL &le; operator applied to boolean values. */
-  public static boolean le(boolean b0, boolean b1) {
-    return compare(b0, b1) <= 0;
-  }
-
-  /** SQL &le; operator applied to String values. */
-  public static boolean le(String b0, String b1) {
-    return b0.compareTo(b1) <= 0;
-  }
-
-  /** SQL &le; operator applied to ByteString values. */
-  public static boolean le(ByteString b0, ByteString b1) {
-    return b0.compareTo(b1) <= 0;
-  }
-
-  /** SQL &le; operator applied to BigDecimal values. */
-  public static boolean le(BigDecimal b0, BigDecimal b1) {
-    return b0.compareTo(b1) <= 0;
-  }
-
-  // >
-
-  /** SQL &gt; operator applied to boolean values. */
-  public static boolean gt(boolean b0, boolean b1) {
-    return compare(b0, b1) > 0;
-  }
-
-  /** SQL &gt; operator applied to String values. */
-  public static boolean gt(String b0, String b1) {
-    return b0.compareTo(b1) > 0;
-  }
-
-  /** SQL &gt; operator applied to ByteString values. */
-  public static boolean gt(ByteString b0, ByteString b1) {
-    return b0.compareTo(b1) > 0;
-  }
-
-  /** SQL &gt; operator applied to BigDecimal values. */
-  public static boolean gt(BigDecimal b0, BigDecimal b1) {
-    return b0.compareTo(b1) > 0;
-  }
-
-  // >=
-
-  /** SQL &ge; operator applied to boolean values. */
-  public static boolean ge(boolean b0, boolean b1) {
-    return compare(b0, b1) >= 0;
-  }
-
-  /** SQL &ge; operator applied to String values. */
-  public static boolean ge(String b0, String b1) {
-    return b0.compareTo(b1) >= 0;
-  }
-
-  /** SQL &ge; operator applied to ByteString values. */
-  public static boolean ge(ByteString b0, ByteString b1) {
-    return b0.compareTo(b1) >= 0;
-  }
-
-  /** SQL &ge; operator applied to BigDecimal values. */
-  public static boolean ge(BigDecimal b0, BigDecimal b1) {
-    return b0.compareTo(b1) >= 0;
-  }
-
-  // +
-
-  /** SQL <code>+</code> operator applied to int values. */
-  public static int plus(int b0, int b1) {
-    return b0 + b1;
-  }
-
-  /** SQL <code>+</code> operator applied to int values; left side may be
-   * null. */
-  public static Integer plus(Integer b0, int b1) {
-    return b0 == null ? null : (b0 + b1);
-  }
-
-  /** SQL <code>+</code> operator applied to int values; right side may be
-   * null. */
-  public static Integer plus(int b0, Integer b1) {
-    return b1 == null ? null : (b0 + b1);
-  }
-
-  /** SQL <code>+</code> operator applied to nullable int values. */
-  public static Integer plus(Integer b0, Integer b1) {
-    return (b0 == null || b1 == null) ? null : (b0 + b1);
-  }
-
-  /** SQL <code>+</code> operator applied to nullable long and int values. */
-  public static Long plus(Long b0, Integer b1) {
-    return (b0 == null || b1 == null)
-        ? null
-        : (b0.longValue() + b1.longValue());
-  }
-
-  /** SQL <code>+</code> operator applied to nullable int and long values. */
-  public static Long plus(Integer b0, Long b1) {
-    return (b0 == null || b1 == null)
-        ? null
-        : (b0.longValue() + b1.longValue());
-  }
-
-  /** SQL <code>+</code> operator applied to BigDecimal values. */
-  public static BigDecimal plus(BigDecimal b0, BigDecimal b1) {
-    return (b0 == null || b1 == null) ? null : b0.add(b1);
-  }
-
-  // -
-
-  /** SQL <code>-</code> operator applied to int values. */
-  public static int minus(int b0, int b1) {
-    return b0 - b1;
-  }
-
-  /** SQL <code>-</code> operator applied to int values; left side may be
-   * null. */
-  public static Integer minus(Integer b0, int b1) {
-    return b0 == null ? null : (b0 - b1);
-  }
-
-  /** SQL <code>-</code> operator applied to int values; right side may be
-   * null. */
-  public static Integer minus(int b0, Integer b1) {
-    return b1 == null ? null : (b0 - b1);
-  }
-
-  /** SQL <code>-</code> operator applied to nullable int values. */
-  public static Integer minus(Integer b0, Integer b1) {
-    return (b0 == null || b1 == null) ? null : (b0 - b1);
-  }
-
-  /** SQL <code>-</code> operator applied to nullable long and int values. */
-  public static Long minus(Long b0, Integer b1) {
-    return (b0 == null || b1 == null)
-        ? null
-        : (b0.longValue() - b1.longValue());
-  }
-
-  /** SQL <code>-</code> operator applied to nullable int and long values. */
-  public static Long minus(Integer b0, Long b1) {
-    return (b0 == null || b1 == null)
-        ? null
-        : (b0.longValue() - b1.longValue());
-  }
-
-  /** SQL <code>-</code> operator applied to BigDecimal values. */
-  public static BigDecimal minus(BigDecimal b0, BigDecimal b1) {
-    return (b0 == null || b1 == null) ? null : b0.subtract(b1);
-  }
-
-  // /
-
-  /** SQL <code>/</code> operator applied to int values. */
-  public static int divide(int b0, int b1) {
-    return b0 / b1;
-  }
-
-  /** SQL <code>/</code> operator applied to int values; left side may be
-   * null. */
-  public static Integer divide(Integer b0, int b1) {
-    return b0 == null ? null : (b0 / b1);
-  }
-
-  /** SQL <code>/</code> operator applied to int values; right side may be
-   * null. */
-  public static Integer divide(int b0, Integer b1) {
-    return b1 == null ? null : (b0 / b1);
-  }
-
-  /** SQL <code>/</code> operator applied to nullable int values. */
-  public static Integer divide(Integer b0, Integer b1) {
-    return (b0 == null || b1 == null) ? null : (b0 / b1);
-  }
-
-  /** SQL <code>/</code> operator applied to nullable long and int values. */
-  public static Long divide(Long b0, Integer b1) {
-    return (b0 == null || b1 == null)
-        ? null
-        : (b0.longValue() / b1.longValue());
-  }
-
-  /** SQL <code>/</code> operator applied to nullable int and long values. */
-  public static Long divide(Integer b0, Long b1) {
-    return (b0 == null || b1 == null)
-        ? null
-        : (b0.longValue() / b1.longValue());
-  }
-
-  /** SQL <code>/</code> operator applied to BigDecimal values. */
-  public static BigDecimal divide(BigDecimal b0, BigDecimal b1) {
-    // OVERRIDE POINT
-    return (b0 == null || b1 == null) ? null : b0.divide(b1, MathContext.DECIMAL64);
-  }
-
-  // *
-
-  /** SQL <code>*</code> operator applied to int values. */
-  public static int multiply(int b0, int b1) {
-    return b0 * b1;
-  }
-
-  /** SQL <code>*</code> operator applied to int values; left side may be
-   * null. */
-  public static Integer multiply(Integer b0, int b1) {
-    return b0 == null ? null : (b0 * b1);
-  }
-
-  /** SQL <code>*</code> operator applied to int values; right side may be
-   * null. */
-  public static Integer multiply(int b0, Integer b1) {
-    return b1 == null ? null : (b0 * b1);
-  }
-
-  /** SQL <code>*</code> operator applied to nullable int values. */
-  public static Integer multiply(Integer b0, Integer b1) {
-    return (b0 == null || b1 == null) ? null : (b0 * b1);
-  }
-
-  /** SQL <code>*</code> operator applied to nullable long and int values. */
-  public static Long multiply(Long b0, Integer b1) {
-    return (b0 == null || b1 == null)
-        ? null
-        : (b0.longValue() * b1.longValue());
-  }
-
-  /** SQL <code>*</code> operator applied to nullable int and long values. */
-  public static Long multiply(Integer b0, Long b1) {
-    return (b0 == null || b1 == null)
-        ? null
-        : (b0.longValue() * b1.longValue());
-  }
-
-  /** SQL <code>*</code> operator applied to BigDecimal values. */
-  public static BigDecimal multiply(BigDecimal b0, BigDecimal b1) {
-    return (b0 == null || b1 == null) ? null : b0.multiply(b1);
-  }
-
-  // EXP
-
-  /** SQL <code>EXP</code> operator applied to double values. */
-  public static double exp(double b0) {
-    return Math.exp(b0);
-  }
-
-  public static double exp(long b0) {
-    return Math.exp(b0);
-  }
-
-  // POWER
-
-  /** SQL <code>POWER</code> operator applied to double values. */
-  public static double power(double b0, double b1) {
-    return Math.pow(b0, b1);
-  }
-
-  public static double power(long b0, long b1) {
-    return Math.pow(b0, b1);
-  }
-
-  public static double power(long b0, BigDecimal b1) {
-    return Math.pow(b0, b1.doubleValue());
-  }
-
-  // LN
-
-  /** SQL {@code LN(number)} function applied to double values. */
-  public static double ln(double d) {
-    return Math.log(d);
-  }
-
-  /** SQL {@code LN(number)} function applied to long values. */
-  public static double ln(long b0) {
-    return Math.log(b0);
-  }
-
-  /** SQL {@code LN(number)} function applied to BigDecimal values. */
-  public static double ln(BigDecimal d) {
-    return Math.log(d.doubleValue());
-  }
-
-  // LOG10
-
-  /** SQL <code>LOG10(numeric)</code> operator applied to double values. */
-  public static double log10(double b0) {
-    return Math.log10(b0);
-  }
-
-  /** SQL {@code LOG10(number)} function applied to long values. */
-  public static double log10(long b0) {
-    return Math.log10(b0);
-  }
-
-  /** SQL {@code LOG10(number)} function applied to BigDecimal values. */
-  public static double log10(BigDecimal d) {
-    return Math.log10(d.doubleValue());
-  }
-
-  // MOD
-
-  /** SQL <code>MOD</code> operator applied to byte values. */
-  public static byte mod(byte b0, byte b1) {
-    return (byte) (b0 % b1);
-  }
-
-  /** SQL <code>MOD</code> operator applied to short values. */
-  public static short mod(short b0, short b1) {
-    return (short) (b0 % b1);
-  }
-
-  /** SQL <code>MOD</code> operator applied to int values. */
-  public static int mod(int b0, int b1) {
-    return b0 % b1;
-  }
-
-  /** SQL <code>MOD</code> operator applied to long values. */
-  public static long mod(long b0, long b1) {
-    return b0 % b1;
-  }
-
-  // temporary
-  public static BigDecimal mod(BigDecimal b0, int b1) {
-    return mod(b0, BigDecimal.valueOf(b1));
-  }
-
-  // temporary
-  public static int mod(int b0, BigDecimal b1) {
-    return mod(b0, b1.intValue());
-  }
-
-  public static BigDecimal mod(BigDecimal b0, BigDecimal b1) {
-    final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
-    return bigDecimals[1];
-  }
-
-  // ABS
-
-  /** SQL <code>ABS</code> operator applied to byte values. */
-  public static byte abs(byte b0) {
-    return (byte) Math.abs(b0);
-  }
-
-  /** SQL <code>ABS</code> operator applied to short values. */
-  public static short abs(short b0) {
-    return (short) Math.abs(b0);
-  }
-
-  /** SQL <code>ABS</code> operator applied to int values. */
-  public static int abs(int b0) {
-    return Math.abs(b0);
-  }
-
-  /** SQL <code>ABS</code> operator applied to long values. */
-  public static long abs(long b0) {
-    return Math.abs(b0);
-  }
-
-  /** SQL <code>ABS</code> operator applied to float values. */
-  public static float abs(float b0) {
-    return Math.abs(b0);
-  }
-
-  /** SQL <code>ABS</code> operator applied to double values. */
-  public static double abs(double b0) {
-    return Math.abs(b0);
-  }
-
-  /** SQL <code>ABS</code> operator applied to BigDecimal values. */
-  public static BigDecimal abs(BigDecimal b0) {
-    return b0.abs();
-  }
-
-  // Helpers
-
-  /** Helper for implementing MIN. Somewhat similar to LEAST operator. */
-  public static <T extends Comparable<T>> T lesser(T b0, T b1) {
-    return b0 == null || b0.compareTo(b1) > 0 ? b1 : b0;
-  }
-
-  /** LEAST operator. */
-  public static <T extends Comparable<T>> T least(T b0, T b1) {
-    return b0 == null || b1 != null && b0.compareTo(b1) > 0 ? b1 : b0;
-  }
-
-  public static boolean greater(boolean b0, boolean b1) {
-    return b0 || b1;
-  }
-
-  public static boolean lesser(boolean b0, boolean b1) {
-    return b0 && b1;
-  }
-
-  public static byte greater(byte b0, byte b1) {
-    return b0 > b1 ? b0 : b1;
-  }
-
-  public static byte lesser(byte b0, byte b1) {
-    return b0 > b1 ? b1 : b0;
-  }
-
-  public static char greater(char b0, char b1) {
-    return b0 > b1 ? b0 : b1;
-  }
-
-  public static char lesser(char b0, char b1) {
-    return b0 > b1 ? b1 : b0;
-  }
-
-  public static short greater(short b0, short b1) {
-    return b0 > b1 ? b0 : b1;
-  }
-
-  public static short lesser(short b0, short b1) {
-    return b0 > b1 ? b1 : b0;
-  }
-
-  public static int greater(int b0, int b1) {
-    return b0 > b1 ? b0 : b1;
-  }
-
-  public static int lesser(int b0, int b1) {
-    return b0 > b1 ? b1 : b0;
-  }
-
-  public static long greater(long b0, long b1) {
-    return b0 > b1 ? b0 : b1;
-  }
-
-  public static long lesser(long b0, long b1) {
-    return b0 > b1 ? b1 : b0;
-  }
-
-  public static float greater(float b0, float b1) {
-    return b0 > b1 ? b0 : b1;
-  }
-
-  public static float lesser(float b0, float b1) {
-    return b0 > b1 ? b1 : b0;
-  }
-
-  public static double greater(double b0, double b1) {
-    return b0 > b1 ? b0 : b1;
-  }
-
-  public static double lesser(double b0, double b1) {
-    return b0 > b1 ? b1 : b0;
-  }
-
-  /** Helper for implementing MAX. Somewhat similar to GREATEST operator. */
-  public static <T extends Comparable<T>> T greater(T b0, T b1) {
-    return b0 == null || b0.compareTo(b1) < 0 ? b1 : b0;
-  }
-
-  /** GREATEST operator. */
-  public static <T extends Comparable<T>> T greatest(T b0, T b1) {
-    return b0 == null || b1 != null && b0.compareTo(b1) < 0 ? b1 : b0;
-  }
-
-  /** Boolean comparison. */
-  public static int compare(boolean x, boolean y) {
-    return x == y ? 0 : x ? 1 : -1;
-  }
-
-  /** CAST(FLOAT AS VARCHAR). */
-  public static String toString(float x) {
-    if (x == 0) {
-      return "0E0";
-    }
-    BigDecimal bigDecimal =
-        new BigDecimal(x, MathContext.DECIMAL32).stripTrailingZeros();
-    final String s = bigDecimal.toString();
-    return s.replaceAll("0*E", "E").replace("E+", "E");
-  }
-
-  /** CAST(DOUBLE AS VARCHAR). */
-  public static String toString(double x) {
-    if (x == 0) {
-      return "0E0";
-    }
-    BigDecimal bigDecimal =
-        new BigDecimal(x, MathContext.DECIMAL64).stripTrailingZeros();
-    final String s = bigDecimal.toString();
-    return s.replaceAll("0*E", "E").replace("E+", "E");
-  }
-
-  /** CAST(DECIMAL AS VARCHAR). */
-  public static String toString(BigDecimal x) {
-    final String s = x.toString();
-    if (s.startsWith("0")) {
-      // we want ".1" not "0.1"
-      return s.substring(1);
-    } else if (s.startsWith("-0")) {
-      // we want "-.1" not "-0.1"
-      return "-" + s.substring(2);
-    } else {
-      return s;
-    }
-  }
-
-  /** CAST(BOOLEAN AS VARCHAR). */
-  public static String toString(boolean x) {
-    // Boolean.toString returns lower case -- no good.
-    return x ? "TRUE" : "FALSE";
-  }
-
-  @NonDeterministic
-  private static Object cannotConvert(Object o, Class toType) {
-    throw new RuntimeException("Cannot convert " + o + " to " + toType);
-  }
-
-  /** CAST(VARCHAR AS BOOLEAN). */
-  public static boolean toBoolean(String s) {
-    s = trim_(s, true, true, ' ');
-    if (s.equalsIgnoreCase("TRUE")) {
-      return true;
-    } else if (s.equalsIgnoreCase("FALSE")) {
-      return false;
-    } else {
-      throw new RuntimeException("Invalid character for cast");
-    }
-  }
-
-  public static boolean toBoolean(Number number) {
-    return !number.equals(0);
-  }
-
-  public static boolean toBoolean(Object o) {
-    return o instanceof Boolean ? (Boolean) o
-        : o instanceof Number ? toBoolean((Number) o)
-        : o instanceof String ? toBoolean((String) o)
-        : (Boolean) cannotConvert(o, boolean.class);
-  }
-
-  // Don't need parseByte etc. - Byte.parseByte is sufficient.
-
-  public static byte toByte(Object o) {
-    return o instanceof Byte ? (Byte) o
-        : o instanceof Number ? toByte((Number) o)
-        : Byte.parseByte(o.toString());
-  }
-
-  public static byte toByte(Number number) {
-    return number.byteValue();
-  }
-
-  public static char toChar(String s) {
-    return s.charAt(0);
-  }
-
-  public static Character toCharBoxed(String s) {
-    return s.charAt(0);
-  }
-
-  public static short toShort(String s) {
-    return Short.parseShort(s.trim());
-  }
-
-  public static short toShort(Number number) {
-    return number.shortValue();
-  }
-
-  public static short toShort(Object o) {
-    return o instanceof Short ? (Short) o
-        : o instanceof Number ? toShort((Number) o)
-        : o instanceof String ? toShort((String) o)
-        : (Short) cannotConvert(o, short.class);
-  }
-
-  public static int toInt(java.util.Date v) {
-    return toInt(v, LOCAL_TZ);
-  }
-
-  public static int toInt(java.util.Date v, TimeZone timeZone) {
-    return (int) (toLong(v, timeZone)  / DateTimeUtil.MILLIS_PER_DAY);
-  }
-
-  public static Integer toIntOptional(java.util.Date v) {
-    return v == null ? null : toInt(v);
-  }
-
-  public static Integer toIntOptional(java.util.Date v, TimeZone timeZone) {
-    return v == null
-        ? null
-        : toInt(v, timeZone);
-  }
-
-  public static long toLong(Date v) {
-    return toLong(v, LOCAL_TZ);
-  }
-
-  public static int toInt(java.sql.Time v) {
-    return (int) (toLong(v) % DateTimeUtil.MILLIS_PER_DAY);
-  }
-
-  public static Integer toIntOptional(java.sql.Time v) {
-    return v == null ? null : toInt(v);
-  }
-
-  public static int toInt(String s) {
-    return Integer.parseInt(s.trim());
-  }
-
-  public static int toInt(Number number) {
-    return number.intValue();
-  }
-
-  public static int toInt(Object o) {
-    return o instanceof Integer ? (Integer) o
-        : o instanceof Number ? toInt((Number) o)
-        : o instanceof String ? toInt((String) o)
-        : (Integer) cannotConvert(o, int.class);
-  }
-
-  public static long toLong(Timestamp v) {
-    return toLong(v, LOCAL_TZ);
-  }
-
-  // mainly intended for java.sql.Timestamp but works for other dates also
-  public static long toLong(java.util.Date v, TimeZone timeZone) {
-    final long time = v.getTime();
-    return time + timeZone.getOffset(time);
-  }
-
-  // mainly intended for java.sql.Timestamp but works for other dates also
-  public static Long toLongOptional(java.util.Date v) {
-    return v == null ? null : toLong(v, LOCAL_TZ);
-  }
-
-  public static Long toLongOptional(Timestamp v, TimeZone timeZone) {
-    if (v == null) {
-      return null;
-    }
-    return toLong(v, LOCAL_TZ);
-  }
-
-  public static long toLong(String s) {
-    if (s.startsWith("199") && s.contains(":")) {
-      return Timestamp.valueOf(s).getTime();
-    }
-    return Long.parseLong(s.trim());
-  }
-
-  public static long toLong(Number number) {
-    return number.longValue();
-  }
-
-  public static long toLong(Object o) {
-    return o instanceof Long ? (Long) o
-        : o instanceof Number ? toLong((Number) o)
-        : o instanceof String ? toLong((String) o)
-        : (Long) cannotConvert(o, long.class);
-  }
-
-  public static float toFloat(String s) {
-    return Float.parseFloat(s.trim());
-  }
-
-  public static float toFloat(Number number) {
-    return number.floatValue();
-  }
-
-  public static float toFloat(Object o) {
-    return o instanceof Float ? (Float) o
-        : o instanceof Number ? toFloat((Number) o)
-            : o instanceof String ? toFloat((String) o)
-                : (Float) cannotConvert(o, float.class);
-  }
-
-  public static double toDouble(String s) {
-    return Double.parseDouble(s.trim());
-  }
-
-  public static double toDouble(Number number) {
-    return number.doubleValue();
-  }
-
-  public static double toDouble(Object o) {
-    return o instanceof Double ? (Double) o
-        : o instanceof Number ? toDouble((Number) o)
-        : o instanceof String ? toDouble((String) o)
-        : (Double) cannotConvert(o, double.class);
-  }
-
-  public static BigDecimal toBigDecimal(String s) {
-    return new BigDecimal(s.trim());
-  }
-
-  public static BigDecimal toBigDecimal(Number number) {
-    // There are some values of "long" that cannot be represented as "double".
-    // Not so "int". If it isn't a long, go straight to double.
-    return number instanceof BigDecimal ? (BigDecimal) number
-        : number instanceof BigInteger ? new BigDecimal((BigInteger) number)
-        : number instanceof Long ? new BigDecimal(number.longValue())
-        : new BigDecimal(number.doubleValue());
-  }
-
-  public static BigDecimal toBigDecimal(Object o) {
-    return o instanceof Number ? toBigDecimal((Number) o)
-        : toBigDecimal(o.toString());
-  }
-
-  // Don't need shortValueOf etc. - Short.valueOf is sufficient.
-
-  /** Helper for CAST(... AS VARCHAR(maxLength)). */
-  public static String truncate(String s, int maxLength) {
-    return s == null ? null
-        : s.length() > maxLength ? s.substring(0, maxLength)
-        : s;
-  }
-
-  /** Helper for CAST(... AS VARBINARY(maxLength)). */
-  public static ByteString truncate(ByteString s, int maxLength) {
-    return s == null ? null
-        : s.length() > maxLength ? s.substring(0, maxLength)
-        : s;
-  }
-
-  /** SQL {@code POSITION(seek IN string)} function. */
-  public static int position(String seek, String s) {
-    return s.indexOf(seek) + 1;
-  }
-
-  /** SQL {@code POSITION(seek IN string)} function. */
-  public static int position(ByteString seek, ByteString s) {
-    return s.indexOf(seek) + 1;
-  }
-
-  /** Cheap, unsafe, long power. power(2, 3) returns 8. */
-  public static long powerX(long a, long b) {
-    long x = 1;
-    while (b > 0) {
-      x *= a;
-      --b;
-    }
-    return x;
-  }
-
-  /** Helper for rounding. Truncate(12345, 1000) returns 12000. */
-  public static long round(long v, long x) {
-    return truncate(v + x / 2, x);
-  }
-
-  /** Helper for rounding. Truncate(12345, 1000) returns 12000. */
-  public static long truncate(long v, long x) {
-    long remainder = v % x;
-    if (remainder < 0) {
-      remainder += x;
-    }
-    return v - remainder;
-  }
-
-  /** Helper for rounding. Truncate(12345, 1000) returns 12000. */
-  public static int round(int v, int x) {
-    return truncate(v + x / 2, x);
-  }
-
-  /** Helper for rounding. Truncate(12345, 1000) returns 12000. */
-  public static int truncate(int v, int x) {
-    int remainder = v % x;
-    if (remainder < 0) {
-      remainder += x;
-    }
-    return v - remainder;
-  }
-
-  /** Helper for CAST({timestamp} AS VARCHAR(n)). */
-  public static String unixTimestampToString(long timestamp) {
-    final StringBuilder buf = new StringBuilder(17);
-    int date = (int) (timestamp / DateTimeUtil.MILLIS_PER_DAY);
-    int time = (int) (timestamp % DateTimeUtil.MILLIS_PER_DAY);
-    if (time < 0) {
-      --date;
-      time += DateTimeUtil.MILLIS_PER_DAY;
-    }
-    unixDateToString(buf, date);
-    buf.append(' ');
-    unixTimeToString(buf, time);
-    return buf.toString();
-  }
-
-  /** Helper for CAST({timestamp} AS VARCHAR(n)). */
-  public static String unixTimeToString(int time) {
-    final StringBuilder buf = new StringBuilder(8);
-    unixTimeToString(buf, time);
-    return buf.toString();
-  }
-
-  private static void unixTimeToString(StringBuilder buf, int time) {
-    int h = time / 3600000;
-    int time2 = time % 3600000;
-    int m = time2 / 60000;
-    int time3 = time2 % 60000;
-    int s = time3 / 1000;
-    int ms = time3 % 1000;
-    int2(buf, h);
-    buf.append(':');
-    int2(buf, m);
-    buf.append(':');
-    int2(buf, s);
-  }
-
-  /** SQL {@code CURRENT_TIMESTAMP} function. */
-  @NonDeterministic
-  public static long currentTimestamp(DataContext root) {
-    // Cast required for JDK 1.6.
-    return (Long) DataContext.Variable.CURRENT_TIMESTAMP.get(root);
-  }
-
-  /** SQL {@code CURRENT_TIME} function. */
-  @NonDeterministic
-  public static int currentTime(DataContext root) {
-    int time = (int) (currentTimestamp(root) % DateTimeUtil.MILLIS_PER_DAY);
-    if (time < 0) {
-      time += DateTimeUtil.MILLIS_PER_DAY;
-    }
-    return time;
-  }
-
-  /** SQL {@code CURRENT_DATE} function. */
-  @NonDeterministic
-  public static int currentDate(DataContext root) {
-    final long timestamp = currentTimestamp(root);
-    int date = (int) (timestamp / DateTimeUtil.MILLIS_PER_DAY);
-    final int time = (int) (timestamp % DateTimeUtil.MILLIS_PER_DAY);
-    if (time < 0) {
-      --date;
-    }
-    return date;
-  }
-
-  /** SQL {@code LOCAL_TIMESTAMP} function. */
-  @NonDeterministic
-  public static long localTimestamp(DataContext root) {
-    // Cast required for JDK 1.6.
-    return (Long) DataContext.Variable.LOCAL_TIMESTAMP.get(root);
-  }
-
-  /** SQL {@code LOCAL_TIME} function. */
-  @NonDeterministic
-  public static int localTime(DataContext root) {
-    return (int) (localTimestamp(root) % DateTimeUtil.MILLIS_PER_DAY);
-  }
-
-  private static void int2(StringBuilder buf, int i) {
-    buf.append((char) ('0' + (i / 10) % 10));
-    buf.append((char) ('0' + i % 10));
-  }
-
-  private static void int4(StringBuilder buf, int i) {
-    buf.append((char) ('0' + (i / 1000) % 10));
-    buf.append((char) ('0' + (i / 100) % 10));
-    buf.append((char) ('0' + (i / 10) % 10));
-    buf.append((char) ('0' + i % 10));
-  }
-
-  public static int dateStringToUnixDate(String s) {
-    int hyphen1 = s.indexOf('-');
-    int y;
-    int m;
-    int d;
-    if (hyphen1 < 0) {
-      y = Integer.parseInt(s.trim());
-      m = 1;
-      d = 1;
-    } else {
-      y = Integer.parseInt(s.substring(0, hyphen1).trim());
-      final int hyphen2 = s.indexOf('-', hyphen1 + 1);
-      if (hyphen2 < 0) {
-        m = Integer.parseInt(s.substring(hyphen1 + 1).trim());
-        d = 1;
-      } else {
-        m = Integer.parseInt(s.substring(hyphen1 + 1, hyphen2).trim());
-        d = Integer.parseInt(s.substring(hyphen2 + 1).trim());
-      }
-    }
-    return ymdToUnixDate(y, m, d);
-  }
-
-  public static int timeStringToUnixDate(String v) {
-    return timeStringToUnixDate(v, 0);
-  }
-
-  public static int timeStringToUnixDate(String v, int start) {
-    final int colon1 = v.indexOf(':', start);
-    int hour;
-    int minute;
-    int second;
-    int milli;
-    if (colon1 < 0) {
-      hour = Integer.parseInt(v.trim());
-      minute = 1;
-      second = 1;
-      milli = 0;
-    } else {
-      hour = Integer.parseInt(v.substring(start, colon1).trim());
-      final int colon2 = v.indexOf(':', colon1 + 1);
-      if (colon2 < 0) {
-        minute = Integer.parseInt(v.substring(colon1 + 1).trim());
-        second = 1;
-        milli = 0;
-      } else {
-        minute = Integer.parseInt(v.substring(colon1 + 1, colon2).trim());
-        int dot = v.indexOf('.', colon2);
-        if (dot < 0) {
-          second = Integer.parseInt(v.substring(colon2 + 1).trim());
-          milli = 0;
-        } else {
-          second = Integer.parseInt(v.substring(colon2 + 1, dot).trim());
-          milli = Integer.parseInt(v.substring(dot + 1).trim());
-        }
-      }
-    }
-    return hour * (int) DateTimeUtil.MILLIS_PER_HOUR
-        + minute * (int) DateTimeUtil.MILLIS_PER_MINUTE
-        + second * (int) DateTimeUtil.MILLIS_PER_SECOND
-        + milli;
-  }
-
-  public static long timestampStringToUnixDate(String s) {
-    final long d;
-    final long t;
-    s = s.trim();
-    int space = s.indexOf(' ');
-    if (space >= 0) {
-      d = dateStringToUnixDate(s.substring(0, space));
-      t = timeStringToUnixDate(s, space + 1);
-    } else {
-      d = dateStringToUnixDate(s);
-      t = 0;
-    }
-    return d * DateTimeUtil.MILLIS_PER_DAY + t;
-  }
-
-  /** Helper for CAST({date} AS VARCHAR(n)). */
-  public static String unixDateToString(int date) {
-    final StringBuilder buf = new StringBuilder(10);
-    unixDateToString(buf, date);
-    return buf.toString();
-  }
-
-  private static void unixDateToString(StringBuilder buf, int date) {
-    julianToString(buf, date + EPOCH_JULIAN);
-  }
-
-  private static void julianToString(StringBuilder buf, int julian) {
-    // this shifts the epoch back to astronomical year -4800 instead of the
-    // start of the Christian era in year AD 1 of the proleptic Gregorian
-    // calendar.
-    int j = julian + 32044;
-    int g = j / 146097;
-    int dg = j % 146097;
-    int c = (dg / 36524 + 1) * 3 / 4;
-    int dc = dg - c * 36524;
-    int b = dc / 1461;
-    int db = dc % 1461;
-    int a = (db / 365 + 1) * 3 / 4;
-    int da = db - a * 365;
-
-    // integer number of full years elapsed since March 1, 4801 BC
-    int y = g * 400 + c * 100 + b * 4 + a;
-    // integer number of full months elapsed since the last March 1
-    int m = (da * 5 + 308) / 153 - 2;
-    // number of days elapsed since day 1 of the month
-    int d = da - (m + 4) * 153 / 5 + 122;
-    int year = y - 4800 + (m + 2) / 12;
-    int month = (m + 2) % 12 + 1;
-    int day = d + 1;
-    int4(buf, year);
-    buf.append('-');
-    int2(buf, month);
-    buf.append('-');
-    int2(buf, day);
-  }
-
-  public static long unixDateExtract(TimeUnitRange range, long date) {
-    return julianExtract(range, (int) date + EPOCH_JULIAN);
-  }
-
-  private static int julianExtract(TimeUnitRange range, int julian) {
-    // this shifts the epoch back to astronomical year -4800 instead of the
-    // start of the Christian era in year AD 1 of the proleptic Gregorian
-    // calendar.
-    int j = julian + 32044;
-    int g = j / 146097;
-    int dg = j % 146097;
-    int c = (dg / 36524 + 1) * 3 / 4;
-    int dc = dg - c * 36524;
-    int b = dc / 1461;
-    int db = dc % 1461;
-    int a = (db / 365 + 1) * 3 / 4;
-    int da = db - a * 365;
-
-    // integer number of full years elapsed since March 1, 4801 BC
-    int y = g * 400 + c * 100 + b * 4 + a;
-    // integer number of full months elapsed since the last March 1
-    int m = (da * 5 + 308) / 153 - 2;
-    // number of days elapsed since day 1 of the month
-    int d = da - (m + 4) * 153 / 5 + 122;
-    int year = y - 4800 + (m + 2) / 12;
-    int month = (m + 2) % 12 + 1;
-    int day = d + 1;
-    switch (range) {
-    case YEAR:
-      return year;
-    case MONTH:
-      return month;
-    case DAY:
-      return day;
-    default:
-      throw new AssertionError(range);
-    }
-  }
-
-  public static int ymdToUnixDate(int year, int month, int day) {
-    final int julian = ymdToJulian(year, month, day);
-    return julian - EPOCH_JULIAN;
-  }
-
-  public static int ymdToJulian(int year, int month, int day) {
-    int a = (14 - month) / 12;
-    int y = year + 4800 - a;
-    int m = month + 12 * a - 3;
-    int j = day + (153 * m + 2) / 5
-        + 365 * y
-        + y / 4
-        - y / 100
-        + y / 400
-        - 32045;
-    if (j < 2299161) {
-      j = day + (153 * m + 2) / 5 + 365 * y + y / 4 - 32083;
-    }
-    return j;
-  }
-
-  public static String intervalYearMonthToString(int v, TimeUnitRange range) {
-    final StringBuilder buf = new StringBuilder();
-    if (v >= 0) {
-      buf.append('+');
-    } else {
-      buf.append('-');
-      v = -v;
-    }
-    final int y;
-    final int m;
-    switch (range) {
-    case YEAR:
-      v = roundUp(v, 12);
-      y = v / 12;
-      buf.append(y);
-      break;
-    case YEAR_TO_MONTH:
-      y = v / 12;
-      buf.append(y);
-      buf.append('-');
-      m = v % 12;
-      number(buf, m, 2);
-      break;
-    case MONTH:
-      m = v;
-      buf.append(m);
-      break;
-    default:
-      throw new AssertionError(range);
-    }
-    return buf.toString();
-  }
-
-  private static StringBuilder number(StringBuilder buf, int v, int n) {
-    for (int k = digitCount(v); k < n; k++) {
-      buf.append('0');
-    }
-    return buf.append(v);
-  }
-
-  public static int digitCount(int v) {
-    for (int n = 1;; n++) {
-      v /= 10;
-      if (v == 0) {
-        return n;
-      }
-    }
-  }
-
-  public static String intervalDayTimeToString(long v, TimeUnitRange range,
-      int scale) {
-    final StringBuilder buf = new StringBuilder();
-    if (v >= 0) {
-      buf.append('+');
-    } else {
-      buf.append('-');
-      v = -v;
-    }
-    final long ms;
-    final long s;
-    final long m;
-    final long h;
-    final long d;
-    switch (range) {
-    case DAY_TO_SECOND:
-      v = roundUp(v, powerX(10, 3 - scale));
-      ms = v % 1000;
-      v /= 1000;
-      s = v % 60;
-      v /= 60;
-      m = v % 60;
-      v /= 60;
-      h = v % 24;
-      v /= 24;
-      d = v;
-      buf.append((int) d);
-      buf.append(' ');
-      number(buf, (int) h, 2);
-      buf.append(':');
-      number(buf, (int) m, 2);
-      buf.append(':');
-      number(buf, (int) s, 2);
-      fraction(buf, scale, ms);
-      break;
-    case DAY_TO_MINUTE:
-      v = roundUp(v, 1000 * 60);
-      v /= 1000;
-      v /= 60;
-      m = v % 60;
-      v /= 60;
-      h = v % 24;
-      v /= 24;
-      d = v;
-      buf.append((int) d);
-      buf.append(' ');
-      number(buf, (int) h, 2);
-      buf.append(':');
-      number(buf, (int) m, 2);
-      break;
-    case DAY_TO_HOUR:
-      v = roundUp(v, 1000 * 60 * 60);
-      v /= 1000;
-      v /= 60;
-      v /= 60;
-      h = v % 24;
-      v /= 24;
-      d = v;
-      buf.append((int) d);
-      buf.append(' ');
-      number(buf, (int) h, 2);
-      break;
-    case DAY:
-      v = roundUp(v, 1000 * 60 * 60 * 24);
-      d = v / (1000 * 60 * 60 * 24);
-      buf.append((int) d);
-      break;
-    case HOUR:
-      v = roundUp(v, 1000 * 60 * 60);
-      v /= 1000;
-      v /= 60;
-      v /= 60;
-      h = v;
-      buf.append((int) h);
-      break;
-    case HOUR_TO_MINUTE:
-      v = roundUp(v, 1000 * 60);
-      v /= 1000;
-      v /= 60;
-      m = v % 60;
-      v /= 60;
-      h = v;
-      buf.append((int) h);
-      buf.append(':');
-      number(buf, (int) m, 2);
-      break;
-    case HOUR_TO_SECOND:
-      v = roundUp(v, powerX(10, 3 - scale));
-      ms = v % 1000;
-      v /= 1000;
-      s = v % 60;
-      v /= 60;
-      m = v % 60;
-      v /= 60;
-      h = v;
-      buf.append((int) h);
-      buf.append(':');
-      number(buf, (int) m, 2);
-      buf.append(':');
-      number(buf, (int) s, 2);
-      fraction(buf, scale, ms);
-      break;
-    case MINUTE_TO_SECOND:
-      v = roundUp(v, powerX(10, 3 - scale));
-      ms = v % 1000;
-      v /= 1000;
-      s = v % 60;
-      v /= 60;
-      m = v;
-      buf.append((int) m);
-      buf.append(':');
-      number(buf, (int) s, 2);
-      fraction(buf, scale, ms);
-      break;
-    case MINUTE:
-      v = roundUp(v, 1000 * 60);
-      v /= 1000;
-      v /= 60;
-      m = v;
-      buf.append((int) m);
-      break;
-    case SECOND:
-      v = roundUp(v, powerX(10, 3 - scale));
-      ms = v % 1000;
-      v /= 1000;
-      s = v;
-      buf.append((int) s);
-      fraction(buf, scale, ms);
-      break;
-    default:
-      throw new AssertionError(range);
-    }
-    return buf.toString();
-  }
-
-  /**
-   * Rounds a dividend to the nearest divisor.
-   * For example roundUp(31, 10) yields 30; roundUp(37, 10) yields 40.
-   * @param dividend Number to be divided
-   * @param divisor Number to divide by
-   * @return Rounded dividend
-   */
-  private static long roundUp(long dividend, long divisor) {
-    long remainder = dividend % divisor;
-    dividend -= remainder;
-    if (remainder * 2 > divisor) {
-      dividend += divisor;
-    }
-    return dividend;
-  }
-
-  private static int roundUp(int dividend, int divisor) {
-    int remainder = dividend % divisor;
-    dividend -= remainder;
-    if (remainder * 2 > divisor) {
-      dividend += divisor;
-    }
-    return dividend;
-  }
-
-  private static void fraction(StringBuilder buf, int scale, long ms) {
-    if (scale > 0) {
-      buf.append('.');
-      long v1 = scale == 3 ? ms
-          : scale == 2 ? ms / 10
-          : scale == 1 ? ms / 100
-            : 0;
-      number(buf, (int) v1, scale);
-    }
-  }
-
-  /** Helper for "array element reference". Caller has already ensured that
-   * array and index are not null. Index is 1-based, per SQL. */
-  public static Object arrayItem(List list, int item) {
-    if (item < 1 || item > list.size()) {
-      return null;
-    }
-    return list.get(item - 1);
-  }
-
-  /** Helper for "map element reference". Caller has already ensured that
-   * array and index are not null. Index is 1-based, per SQL. */
-  public static Object mapItem(Map map, Object item) {
-    return map.get(item);
-  }
-
-  /** Implements the {@code [ ... ]} operator on an object whose type is not
-   * known until runtime.
-   */
-  public static Object item(Object object, Object index) {
-    if (object instanceof Map) {
-      return ((Map) object).get(index);
-    }
-    if (object instanceof List && index instanceof Number) {
-      List list = (List) object;
-      return list.get(((Number) index).intValue());
-    }
-    return null;
-  }
-
-  /** NULL &rarr; FALSE, FALSE &rarr; FALSE, TRUE &rarr; TRUE. */
-  public static boolean isTrue(Boolean b) {
-    return b != null && b;
-  }
-
-  /** NULL &rarr; TRUE, FALSE &rarr; FALSE, TRUE &rarr; TRUE. */
-  public static boolean isNotFalse(Boolean b) {
-    return b == null || b;
-  }
-
-  /** NULL &rarr; NULL, FALSE &rarr; TRUE, TRUE &rarr; FALSE. */
-  public static Boolean not(Boolean b) {
-    return (b == null) ? null : !b;
-  }
-
-  /** Converts a JDBC array to a list. */
-  public static List arrayToList(final java.sql.Array a) {
-    if (a == null) {
-      return null;
-    }
-    try {
-      return Primitive.asList(a.getArray());
-    } catch (SQLException e) {
-      throw new RuntimeException(e);
-    }
-  }
-
-  /** Support the SLICE function. */
-  public static List slice(List list) {
-    return list;
-  }
-
-  /** Support the ELEMENT function. */
-  public static Object element(List list) {
-    switch (list.size()) {
-    case 0:
-      return null;
-    case 1:
-      return list.get(0);
-    default:
-      throw new RuntimeException("more than one value");
-    }
-  }
-
-  /** Returns a lambda that converts a list to an enumerable. */
-  public static <E> Function1<List<E>, Enumerable<E>> listToEnumerable() {
-    //noinspection unchecked
-    return (Function1<List<E>, Enumerable<E>>) (Function1) LIST_AS_ENUMERABLE;
-  }
-
-  /** A range of time units. The first is more significant than the
-   * other (e.g. year-to-day) or the same as the other
-   * (e.g. month). */
-  public enum TimeUnitRange {
-    YEAR,
-    YEAR_TO_MONTH,
-    MONTH,
-    DAY,
-    DAY_TO_HOUR,
-    DAY_TO_MINUTE,
-    DAY_TO_SECOND,
-    HOUR,
-    HOUR_TO_MINUTE,
-    HOUR_TO_SECOND,
-    MINUTE,
-    MINUTE_TO_SECOND,
-    SECOND;
-
-    /** Whether this is in the YEAR-TO-MONTH family of intervals. */
-    public boolean monthly() {
-      return ordinal() <= MONTH.ordinal();
-    }
-  }
-}
-
-// End SqlFunctions.java

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/atopcalcite/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoin.java
----------------------------------------------------------------------
diff --git a/atopcalcite/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoin.java b/atopcalcite/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoin.java
new file mode 100644
index 0000000..918b4bf
--- /dev/null
+++ b/atopcalcite/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoin.java
@@ -0,0 +1,187 @@
+/*
+ * OVERRIDE POINT:
+ * - constructor was private instead of public 
+ */
+
+/*
+ * 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.calcite.adapter.enumerable;
+
+import org.apache.calcite.linq4j.tree.BlockBuilder;
+import org.apache.calcite.linq4j.tree.Expression;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.InvalidRelException;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelNodes;
+import org.apache.calcite.rel.core.JoinInfo;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rel.rules.EquiJoin;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.util.BuiltInMethod;
+import org.apache.calcite.util.ImmutableIntList;
+import org.apache.calcite.util.Util;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.Set;
+
+/** Implementation of {@link org.apache.calcite.rel.core.Join} in
+ * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */
+public class EnumerableJoin extends EquiJoin implements EnumerableRel {
+  public EnumerableJoin( // OVERRIDE POINT, the constructor was private
+      RelOptCluster cluster,
+      RelTraitSet traits,
+      RelNode left,
+      RelNode right,
+      RexNode condition,
+      ImmutableIntList leftKeys,
+      ImmutableIntList rightKeys,
+      JoinRelType joinType,
+      Set<String> variablesStopped)
+      throws InvalidRelException {
+    super(
+        cluster,
+        traits,
+        left,
+        right,
+        condition,
+        leftKeys,
+        rightKeys,
+        joinType,
+        variablesStopped);
+  }
+
+  @Override public EnumerableJoin copy(RelTraitSet traitSet, RexNode condition,
+      RelNode left, RelNode right, JoinRelType joinType,
+      boolean semiJoinDone) {
+    final JoinInfo joinInfo = JoinInfo.of(left, right, condition);
+    assert joinInfo.isEqui();
+    try {
+      return new EnumerableJoin(getCluster(), traitSet, left, right,
+          condition, joinInfo.leftKeys, joinInfo.rightKeys, joinType,
+          variablesStopped);
+    } catch (InvalidRelException e) {
+      // Semantic error not possible. Must be a bug. Convert to
+      // internal error.
+      throw new AssertionError(e);
+    }
+  }
+
+  @Override public RelOptCost computeSelfCost(RelOptPlanner planner) {
+    double rowCount = RelMetadataQuery.getRowCount(this);
+
+    // Joins can be flipped, and for many algorithms, both versions are viable
+    // and have the same cost. To make the results stable between versions of
+    // the planner, make one of the versions slightly more expensive.
+    switch (joinType) {
+    case RIGHT:
+      rowCount = addEpsilon(rowCount);
+      break;
+    default:
+      if (RelNodes.COMPARATOR.compare(left, right) > 0) {
+        rowCount = addEpsilon(rowCount);
+      }
+    }
+
+    // Cheaper if the smaller number of rows is coming from the LHS.
+    // Model this by adding L log L to the cost.
+    final double rightRowCount = right.getRows();
+    final double leftRowCount = left.getRows();
+    if (Double.isInfinite(leftRowCount)) {
+      rowCount = leftRowCount;
+    } else {
+      rowCount += Util.nLogN(leftRowCount);
+    }
+    if (Double.isInfinite(rightRowCount)) {
+      rowCount = rightRowCount;
+    } else {
+      rowCount += rightRowCount;
+    }
+    return planner.getCostFactory().makeCost(rowCount, 0, 0);
+  }
+
+  private double addEpsilon(double d) {
+    assert d >= 0d;
+    final double d0 = d;
+    if (d < 10) {
+      // For small d, adding 1 would change the value significantly.
+      d *= 1.001d;
+      if (d != d0) {
+        return d;
+      }
+    }
+    // For medium d, add 1. Keeps integral values integral.
+    ++d;
+    if (d != d0) {
+      return d;
+    }
+    // For large d, adding 1 might not change the value. Add .1%.
+    // If d is NaN, this still will probably not change the value. That's OK.
+    d *= 1.001d;
+    return d;
+  }
+
+  public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
+    BlockBuilder builder = new BlockBuilder();
+    final Result leftResult =
+        implementor.visitChild(this, 0, (EnumerableRel) left, pref);
+    Expression leftExpression =
+        builder.append(
+            "left", leftResult.block);
+    final Result rightResult =
+        implementor.visitChild(this, 1, (EnumerableRel) right, pref);
+    Expression rightExpression =
+        builder.append(
+            "right", rightResult.block);
+    final PhysType physType =
+        PhysTypeImpl.of(
+            implementor.getTypeFactory(), getRowType(), pref.preferArray());
+    final PhysType keyPhysType =
+        leftResult.physType.project(
+            leftKeys, JavaRowFormat.LIST);
+    return implementor.result(
+        physType,
+        builder.append(
+            Expressions.call(
+                leftExpression,
+                BuiltInMethod.JOIN.method,
+                Expressions.list(
+                    rightExpression,
+                    leftResult.physType.generateAccessor(leftKeys),
+                    rightResult.physType.generateAccessor(rightKeys),
+                    EnumUtils.joinSelector(joinType,
+                        physType,
+                        ImmutableList.of(
+                            leftResult.physType, rightResult.physType)))
+                    .append(
+                        Util.first(keyPhysType.comparer(),
+                            Expressions.constant(null)))
+                    .append(
+                        Expressions.constant(joinType.generatesNullsOnLeft()))
+                    .append(
+                        Expressions.constant(
+                            joinType.generatesNullsOnRight())))).toBlock());
+  }
+
+}
+
+// End EnumerableJoin.java


[07/10] incubator-kylin git commit: KYLIN-780 Upgrade query module, all query test pass

Posted by li...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
----------------------------------------------------------------------
diff --git a/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java b/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
new file mode 100644
index 0000000..2508629
--- /dev/null
+++ b/atopcalcite/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
@@ -0,0 +1,4978 @@
+/*
+ * OVERRIDE POINT:
+ * - getInSubqueryThreshold(), was `20`, now `Integer.MAX_VALUE`
+ * - isTrimUnusedFields(), override to false
+ * - AggConverter.visit(SqlCall), skip column reading for COUNT(COL), for https://jirap.corp.ebay.com/browse/KYLIN-104
+ */
+
+/*
+ * 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.calcite.sql2rel;
+
+import org.apache.calcite.linq4j.Ord;
+import org.apache.calcite.plan.Convention;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptQuery;
+import org.apache.calcite.plan.RelOptSamplingParameters;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.prepare.Prepare;
+import org.apache.calcite.prepare.RelOptTableImpl;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollationImpl;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelShuttle;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.core.Collect;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinInfo;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Sample;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rel.core.Uncollect;
+import org.apache.calcite.rel.logical.LogicalAggregate;
+import org.apache.calcite.rel.logical.LogicalCorrelate;
+import org.apache.calcite.rel.logical.LogicalIntersect;
+import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rel.logical.LogicalMinus;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rel.logical.LogicalTableFunctionScan;
+import org.apache.calcite.rel.logical.LogicalTableModify;
+import org.apache.calcite.rel.logical.LogicalTableScan;
+import org.apache.calcite.rel.logical.LogicalUnion;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rel.metadata.RelColumnMapping;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexCallBinding;
+import org.apache.calcite.rex.RexCorrelVariable;
+import org.apache.calcite.rex.RexDynamicParam;
+import org.apache.calcite.rex.RexFieldAccess;
+import org.apache.calcite.rex.RexFieldCollation;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexRangeRef;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.rex.RexVisitorImpl;
+import org.apache.calcite.rex.RexWindowBound;
+import org.apache.calcite.schema.ModifiableTable;
+import org.apache.calcite.schema.TranslatableTable;
+import org.apache.calcite.sql.JoinConditionType;
+import org.apache.calcite.sql.JoinType;
+import org.apache.calcite.sql.SemiJoinType;
+import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlDataTypeSpec;
+import org.apache.calcite.sql.SqlDelete;
+import org.apache.calcite.sql.SqlDynamicParam;
+import org.apache.calcite.sql.SqlExplainLevel;
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlInsert;
+import org.apache.calcite.sql.SqlIntervalQualifier;
+import org.apache.calcite.sql.SqlJoin;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlMerge;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlOperatorTable;
+import org.apache.calcite.sql.SqlSampleSpec;
+import org.apache.calcite.sql.SqlSelect;
+import org.apache.calcite.sql.SqlSelectKeyword;
+import org.apache.calcite.sql.SqlSetOperator;
+import org.apache.calcite.sql.SqlUpdate;
+import org.apache.calcite.sql.SqlUtil;
+import org.apache.calcite.sql.SqlWindow;
+import org.apache.calcite.sql.SqlWith;
+import org.apache.calcite.sql.SqlWithItem;
+import org.apache.calcite.sql.fun.SqlCountAggFunction;
+import org.apache.calcite.sql.fun.SqlInOperator;
+import org.apache.calcite.sql.fun.SqlRowOperator;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlReturnTypeInference;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.calcite.sql.type.TableFunctionReturnTypeInference;
+import org.apache.calcite.sql.util.SqlBasicVisitor;
+import org.apache.calcite.sql.util.SqlVisitor;
+import org.apache.calcite.sql.validate.AggregatingSelectScope;
+import org.apache.calcite.sql.validate.CollectNamespace;
+import org.apache.calcite.sql.validate.DelegatingScope;
+import org.apache.calcite.sql.validate.ListScope;
+import org.apache.calcite.sql.validate.ParameterScope;
+import org.apache.calcite.sql.validate.SelectScope;
+import org.apache.calcite.sql.validate.SqlMonotonicity;
+import org.apache.calcite.sql.validate.SqlQualified;
+import org.apache.calcite.sql.validate.SqlUserDefinedTableFunction;
+import org.apache.calcite.sql.validate.SqlUserDefinedTableMacro;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorImpl;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+import org.apache.calcite.sql.validate.SqlValidatorUtil;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.ImmutableIntList;
+import org.apache.calcite.util.NlsString;
+import org.apache.calcite.util.NumberUtil;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+import org.apache.calcite.util.mapping.Mappings;
+import org.apache.calcite.util.trace.CalciteTrace;
+import org.apache.calcite.sql.SqlNumericLiteral;
+
+import com.google.common.base.Function;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+import java.lang.reflect.Type;
+import java.math.BigDecimal;
+import java.util.AbstractList;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Stack;
+import java.util.TreeSet;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import static org.apache.calcite.sql.SqlUtil.stripAs;
+import static org.apache.calcite.util.Static.RESOURCE;
+
+/**
+ * Converts a SQL parse tree (consisting of
+ * {@link org.apache.calcite.sql.SqlNode} objects) into a relational algebra
+ * expression (consisting of {@link org.apache.calcite.rel.RelNode} objects).
+ *
+ * <p>The public entry points are: {@link #convertQuery},
+ * {@link #convertExpression(SqlNode)}.
+ */
+public class SqlToRelConverter {
+  //~ Static fields/initializers ---------------------------------------------
+
+  protected static final Logger SQL2REL_LOGGER =
+      CalciteTrace.getSqlToRelTracer();
+
+  private static final Function<SubQuery, SqlNode> FN =
+      new Function<SubQuery, SqlNode>() {
+        public SqlNode apply(SubQuery input) {
+          return input.node;
+        }
+      };
+  private static final BigDecimal TWO = BigDecimal.valueOf(2L);
+
+  //~ Instance fields --------------------------------------------------------
+
+  protected final SqlValidator validator;
+  protected final RexBuilder rexBuilder;
+  protected final Prepare.CatalogReader catalogReader;
+  protected final RelOptCluster cluster;
+  private DefaultValueFactory defaultValueFactory;
+  private SubqueryConverter subqueryConverter;
+  protected final List<RelNode> leaves = new ArrayList<RelNode>();
+  private final List<SqlDynamicParam> dynamicParamSqlNodes =
+      new ArrayList<SqlDynamicParam>();
+  private final SqlOperatorTable opTab;
+  private boolean shouldConvertTableAccess;
+  protected final RelDataTypeFactory typeFactory;
+  private final SqlNodeToRexConverter exprConverter;
+  private boolean decorrelationEnabled;
+  private boolean trimUnusedFields;
+  private boolean shouldCreateValuesRel;
+  private boolean isExplain;
+  private int nDynamicParamsInExplain;
+
+  /**
+   * Fields used in name resolution for correlated subqueries.
+   */
+  private final Map<String, DeferredLookup> mapCorrelToDeferred =
+      new HashMap<String, DeferredLookup>();
+  private int nextCorrel = 0;
+
+  private static final String CORREL_PREFIX = "$cor";
+
+  /**
+   * Stack of names of datasets requested by the <code>
+   * TABLE(SAMPLE(&lt;datasetName&gt;, &lt;query&gt;))</code> construct.
+   */
+  private final Stack<String> datasetStack = new Stack<String>();
+
+  /**
+   * Mapping of non-correlated subqueries that have been converted to their
+   * equivalent constants. Used to avoid re-evaluating the subquery if it's
+   * already been evaluated.
+   */
+  private final Map<SqlNode, RexNode> mapConvertedNonCorrSubqs =
+      new HashMap<SqlNode, RexNode>();
+
+  public final RelOptTable.ViewExpander viewExpander;
+
+  //~ Constructors -----------------------------------------------------------
+  /**
+   * Creates a converter.
+   *
+   * @param viewExpander    Preparing statement
+   * @param validator       Validator
+   * @param catalogReader   Schema
+   * @param planner         Planner
+   * @param rexBuilder      Rex builder
+   * @param convertletTable Expression converter
+   */
+  public SqlToRelConverter(
+      RelOptTable.ViewExpander viewExpander,
+      SqlValidator validator,
+      Prepare.CatalogReader catalogReader,
+      RelOptPlanner planner,
+      RexBuilder rexBuilder,
+      SqlRexConvertletTable convertletTable) {
+    this.viewExpander = viewExpander;
+    this.opTab =
+        (validator
+            == null) ? SqlStdOperatorTable.instance()
+            : validator.getOperatorTable();
+    this.validator = validator;
+    this.catalogReader = catalogReader;
+    this.defaultValueFactory = new NullDefaultValueFactory();
+    this.subqueryConverter = new NoOpSubqueryConverter();
+    this.rexBuilder = rexBuilder;
+    this.typeFactory = rexBuilder.getTypeFactory();
+    RelOptQuery query = new RelOptQuery(planner);
+    this.cluster = query.createCluster(typeFactory, rexBuilder);
+    this.shouldConvertTableAccess = true;
+    this.exprConverter =
+        new SqlNodeToRexConverterImpl(convertletTable);
+    decorrelationEnabled = true;
+    trimUnusedFields = false;
+    shouldCreateValuesRel = true;
+    isExplain = false;
+    nDynamicParamsInExplain = 0;
+  }
+
+  //~ Methods ----------------------------------------------------------------
+
+  /**
+   * @return the RelOptCluster in use.
+   */
+  public RelOptCluster getCluster() {
+    return cluster;
+  }
+
+  /**
+   * Returns the row-expression builder.
+   */
+  public RexBuilder getRexBuilder() {
+    return rexBuilder;
+  }
+
+  /**
+   * Returns the number of dynamic parameters encountered during translation;
+   * this must only be called after {@link #convertQuery}.
+   *
+   * @return number of dynamic parameters
+   */
+  public int getDynamicParamCount() {
+    return dynamicParamSqlNodes.size();
+  }
+
+  /**
+   * Returns the type inferred for a dynamic parameter.
+   *
+   * @param index 0-based index of dynamic parameter
+   * @return inferred type, never null
+   */
+  public RelDataType getDynamicParamType(int index) {
+    SqlNode sqlNode = dynamicParamSqlNodes.get(index);
+    if (sqlNode == null) {
+      throw Util.needToImplement("dynamic param type inference");
+    }
+    return validator.getValidatedNodeType(sqlNode);
+  }
+
+  /**
+   * Returns the current count of the number of dynamic parameters in an
+   * EXPLAIN PLAN statement.
+   *
+   * @param increment if true, increment the count
+   * @return the current count before the optional increment
+   */
+  public int getDynamicParamCountInExplain(boolean increment) {
+    int retVal = nDynamicParamsInExplain;
+    if (increment) {
+      ++nDynamicParamsInExplain;
+    }
+    return retVal;
+  }
+
+  /**
+   * @return mapping of non-correlated subqueries that have been converted to
+   * the constants that they evaluate to
+   */
+  public Map<SqlNode, RexNode> getMapConvertedNonCorrSubqs() {
+    return mapConvertedNonCorrSubqs;
+  }
+
+  /**
+   * Adds to the current map of non-correlated converted subqueries the
+   * elements from another map that contains non-correlated subqueries that
+   * have been converted by another SqlToRelConverter.
+   *
+   * @param alreadyConvertedNonCorrSubqs the other map
+   */
+  public void addConvertedNonCorrSubqs(
+      Map<SqlNode, RexNode> alreadyConvertedNonCorrSubqs) {
+    mapConvertedNonCorrSubqs.putAll(alreadyConvertedNonCorrSubqs);
+  }
+
+  /**
+   * Set a new DefaultValueFactory. To have any effect, this must be called
+   * before any convert method.
+   *
+   * @param factory new DefaultValueFactory
+   */
+  public void setDefaultValueFactory(DefaultValueFactory factory) {
+    defaultValueFactory = factory;
+  }
+
+  /**
+   * Sets a new SubqueryConverter. To have any effect, this must be called
+   * before any convert method.
+   *
+   * @param converter new SubqueryConverter
+   */
+  public void setSubqueryConverter(SubqueryConverter converter) {
+    subqueryConverter = converter;
+  }
+
+  /**
+   * Indicates that the current statement is part of an EXPLAIN PLAN statement
+   *
+   * @param nDynamicParams number of dynamic parameters in the statement
+   */
+  public void setIsExplain(int nDynamicParams) {
+    isExplain = true;
+    nDynamicParamsInExplain = nDynamicParams;
+  }
+
+  /**
+   * Controls whether table access references are converted to physical rels
+   * immediately. The optimizer doesn't like leaf rels to have
+   * {@link Convention#NONE}. However, if we are doing further conversion
+   * passes (e.g. {@link RelStructuredTypeFlattener}), then we may need to
+   * defer conversion. To have any effect, this must be called before any
+   * convert method.
+   *
+   * @param enabled true for immediate conversion (the default); false to
+   *                generate logical LogicalTableScan instances
+   */
+  public void enableTableAccessConversion(boolean enabled) {
+    shouldConvertTableAccess = enabled;
+  }
+
+  /**
+   * Controls whether instances of
+   * {@link org.apache.calcite.rel.logical.LogicalValues} are generated. These
+   * may not be supported by all physical implementations. To have any effect,
+   * this must be called before any convert method.
+   *
+   * @param enabled true to allow LogicalValues to be generated (the default);
+   *                false to force substitution of Project+OneRow instead
+   */
+  public void enableValuesRelCreation(boolean enabled) {
+    shouldCreateValuesRel = enabled;
+  }
+
+  private void checkConvertedType(SqlNode query, RelNode result) {
+    if (!query.isA(SqlKind.DML)) {
+      // Verify that conversion from SQL to relational algebra did
+      // not perturb any type information.  (We can't do this if the
+      // SQL statement is something like an INSERT which has no
+      // validator type information associated with its result,
+      // hence the namespace check above.)
+      RelDataType convertedRowType = result.getRowType();
+      if (!checkConvertedRowType(query, convertedRowType)) {
+        RelDataType validatedRowType =
+            validator.getValidatedNodeType(query);
+        validatedRowType = uniquifyFields(validatedRowType);
+        throw Util.newInternal("Conversion to relational algebra failed to "
+            + "preserve datatypes:\n"
+            + "validated type:\n"
+            + validatedRowType.getFullTypeString()
+            + "\nconverted type:\n"
+            + convertedRowType.getFullTypeString()
+            + "\nrel:\n"
+            + RelOptUtil.toString(result));
+      }
+    }
+  }
+
+  public RelNode flattenTypes(
+      RelNode rootRel,
+      boolean restructure) {
+    RelStructuredTypeFlattener typeFlattener =
+        new RelStructuredTypeFlattener(rexBuilder, createToRelContext());
+    return typeFlattener.rewrite(rootRel, restructure);
+  }
+
+  /**
+   * If subquery is correlated and decorrelation is enabled, performs
+   * decorrelation.
+   *
+   * @param query   Query
+   * @param rootRel Root relational expression
+   * @return New root relational expression after decorrelation
+   */
+  public RelNode decorrelate(SqlNode query, RelNode rootRel) {
+    if (!enableDecorrelation()) {
+      return rootRel;
+    }
+    final RelNode result = decorrelateQuery(rootRel);
+    if (result != rootRel) {
+      checkConvertedType(query, result);
+    }
+    return result;
+  }
+
+  /**
+   * Walks over a tree of relational expressions, replacing each
+   * {@link RelNode} with a 'slimmed down' relational expression that projects
+   * only the fields required by its consumer.
+   *
+   * <p>This may make things easier for the optimizer, by removing crud that
+   * would expand the search space, but is difficult for the optimizer itself
+   * to do it, because optimizer rules must preserve the number and type of
+   * fields. Hence, this transform that operates on the entire tree, similar
+   * to the {@link RelStructuredTypeFlattener type-flattening transform}.
+   *
+   * <p>Currently this functionality is disabled in farrago/luciddb; the
+   * default implementation of this method does nothing.
+   *
+   * @param rootRel Relational expression that is at the root of the tree
+   * @return Trimmed relational expression
+   */
+  public RelNode trimUnusedFields(RelNode rootRel) {
+    // Trim fields that are not used by their consumer.
+    if (isTrimUnusedFields()) {
+      final RelFieldTrimmer trimmer = newFieldTrimmer();
+      rootRel = trimmer.trim(rootRel);
+      boolean dumpPlan = SQL2REL_LOGGER.isLoggable(Level.FINE);
+      if (dumpPlan) {
+        SQL2REL_LOGGER.fine(
+            RelOptUtil.dumpPlan(
+                "Plan after trimming unused fields",
+                rootRel,
+                false,
+                SqlExplainLevel.EXPPLAN_ATTRIBUTES));
+      }
+    }
+    return rootRel;
+  }
+
+  /**
+   * Creates a RelFieldTrimmer.
+   *
+   * @return Field trimmer
+   */
+  protected RelFieldTrimmer newFieldTrimmer() {
+    return new RelFieldTrimmer(validator);
+  }
+
+  /**
+   * Converts an unvalidated query's parse tree into a relational expression.
+   *
+   * @param query           Query to convert
+   * @param needsValidation Whether to validate the query before converting;
+   *                        <code>false</code> if the query has already been
+   *                        validated.
+   * @param top             Whether the query is top-level, say if its result
+   *                        will become a JDBC result set; <code>false</code> if
+   *                        the query will be part of a view.
+   */
+  public RelNode convertQuery(
+      SqlNode query,
+      final boolean needsValidation,
+      final boolean top) {
+    if (needsValidation) {
+      query = validator.validate(query);
+    }
+
+    RelNode result = convertQueryRecursive(query, top, null);
+    checkConvertedType(query, result);
+
+    boolean dumpPlan = SQL2REL_LOGGER.isLoggable(Level.FINE);
+    if (dumpPlan) {
+      SQL2REL_LOGGER.fine(
+          RelOptUtil.dumpPlan(
+              "Plan after converting SqlNode to RelNode",
+              result,
+              false,
+              SqlExplainLevel.EXPPLAN_ATTRIBUTES));
+    }
+
+    return result;
+  }
+
+  protected boolean checkConvertedRowType(
+      SqlNode query,
+      RelDataType convertedRowType) {
+    RelDataType validatedRowType = validator.getValidatedNodeType(query);
+    validatedRowType = uniquifyFields(validatedRowType);
+
+    return RelOptUtil.equal(
+        "validated row type",
+        validatedRowType,
+        "converted row type",
+        convertedRowType,
+        false);
+  }
+
+  protected RelDataType uniquifyFields(RelDataType rowType) {
+    return validator.getTypeFactory().createStructType(
+        RelOptUtil.getFieldTypeList(rowType),
+        SqlValidatorUtil.uniquify(rowType.getFieldNames()));
+  }
+
+  /**
+   * Converts a SELECT statement's parse tree into a relational expression.
+   */
+  public RelNode convertSelect(SqlSelect select) {
+    final SqlValidatorScope selectScope = validator.getWhereScope(select);
+    final Blackboard bb = createBlackboard(selectScope, null);
+    convertSelectImpl(bb, select);
+    return bb.root;
+  }
+
+  /**
+   * Factory method for creating translation workspace.
+   */
+  protected Blackboard createBlackboard(
+      SqlValidatorScope scope,
+      Map<String, RexNode> nameToNodeMap) {
+    return new Blackboard(scope, nameToNodeMap);
+  }
+
+  /**
+   * Implementation of {@link #convertSelect(SqlSelect)}; derived class may
+   * override.
+   */
+  protected void convertSelectImpl(
+      final Blackboard bb,
+      SqlSelect select) {
+    convertFrom(
+        bb,
+        select.getFrom());
+    convertWhere(
+        bb,
+        select.getWhere());
+
+    List<SqlNode> orderExprList = new ArrayList<SqlNode>();
+    List<RelFieldCollation> collationList =
+        new ArrayList<RelFieldCollation>();
+    gatherOrderExprs(
+        bb,
+        select,
+        select.getOrderList(),
+        orderExprList,
+        collationList);
+    final RelCollation collation =
+        cluster.traitSetOf().canonize(RelCollationImpl.of(collationList));
+
+    if (validator.isAggregate(select)) {
+      convertAgg(
+          bb,
+          select,
+          orderExprList);
+    } else {
+      convertSelectList(
+          bb,
+          select,
+          orderExprList);
+    }
+
+    if (select.isDistinct()) {
+      distinctify(bb, true);
+    }
+    convertOrder(
+        select, bb, collation, orderExprList, select.getOffset(),
+        select.getFetch());
+    bb.setRoot(bb.root, true);
+  }
+
+  /**
+   * Having translated 'SELECT ... FROM ... [GROUP BY ...] [HAVING ...]', adds
+   * a relational expression to make the results unique.
+   *
+   * <p>If the SELECT clause contains duplicate expressions, adds
+   * {@link org.apache.calcite.rel.logical.LogicalProject}s so that we are
+   * grouping on the minimal set of keys. The performance gain isn't huge, but
+   * it is difficult to detect these duplicate expressions later.
+   *
+   * @param bb               Blackboard
+   * @param checkForDupExprs Check for duplicate expressions
+   */
+  private void distinctify(
+      Blackboard bb,
+      boolean checkForDupExprs) {
+    // Look for duplicate expressions in the project.
+    // Say we have 'select x, y, x, z'.
+    // Then dups will be {[2, 0]}
+    // and oldToNew will be {[0, 0], [1, 1], [2, 0], [3, 2]}
+    RelNode rel = bb.root;
+    if (checkForDupExprs && (rel instanceof LogicalProject)) {
+      LogicalProject project = (LogicalProject) rel;
+      final List<RexNode> projectExprs = project.getProjects();
+      List<Integer> origins = new ArrayList<Integer>();
+      int dupCount = 0;
+      for (int i = 0; i < projectExprs.size(); i++) {
+        int x = findExpr(projectExprs.get(i), projectExprs, i);
+        if (x >= 0) {
+          origins.add(x);
+          ++dupCount;
+        } else {
+          origins.add(i);
+        }
+      }
+      if (dupCount == 0) {
+        distinctify(bb, false);
+        return;
+      }
+
+      final Map<Integer, Integer> squished = Maps.newHashMap();
+      final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
+      final List<Pair<RexNode, String>> newProjects = Lists.newArrayList();
+      for (int i = 0; i < fields.size(); i++) {
+        if (origins.get(i) == i) {
+          squished.put(i, newProjects.size());
+          newProjects.add(RexInputRef.of2(i, fields));
+        }
+      }
+      rel =
+          new LogicalProject(
+              cluster,
+              rel,
+              Pair.left(newProjects),
+              Pair.right(newProjects),
+              LogicalProject.Flags.BOXED);
+
+      bb.root = rel;
+      distinctify(bb, false);
+      rel = bb.root;
+
+      // Create the expressions to reverse the mapping.
+      // Project($0, $1, $0, $2).
+      final List<Pair<RexNode, String>> undoProjects = Lists.newArrayList();
+      for (int i = 0; i < fields.size(); i++) {
+        final int origin = origins.get(i);
+        RelDataTypeField field = fields.get(i);
+        undoProjects.add(
+            Pair.of(
+                (RexNode) new RexInputRef(
+                    squished.get(origin), field.getType()),
+                field.getName()));
+      }
+
+      rel =
+          new LogicalProject(
+              cluster,
+              rel,
+              Pair.left(undoProjects),
+              Pair.right(undoProjects),
+              LogicalProject.Flags.BOXED);
+
+      bb.setRoot(
+          rel,
+          false);
+
+      return;
+    }
+
+    // Usual case: all of the expressions in the SELECT clause are
+    // different.
+    final ImmutableBitSet groupSet =
+        ImmutableBitSet.range(rel.getRowType().getFieldCount());
+    rel =
+        createAggregate(bb, false, groupSet, ImmutableList.of(groupSet),
+            ImmutableList.<AggregateCall>of());
+
+    bb.setRoot(
+        rel,
+        false);
+  }
+
+  private int findExpr(RexNode seek, List<RexNode> exprs, int count) {
+    for (int i = 0; i < count; i++) {
+      RexNode expr = exprs.get(i);
+      if (expr.toString().equals(seek.toString())) {
+        return i;
+      }
+    }
+    return -1;
+  }
+
+  /**
+   * Converts a query's ORDER BY clause, if any.
+   *
+   * @param select        Query
+   * @param bb            Blackboard
+   * @param collation     Collation list
+   * @param orderExprList Method populates this list with orderBy expressions
+   *                      not present in selectList
+   * @param offset        Expression for number of rows to discard before
+   *                      returning first row
+   * @param fetch         Expression for number of rows to fetch
+   */
+  protected void convertOrder(
+      SqlSelect select,
+      Blackboard bb,
+      RelCollation collation,
+      List<SqlNode> orderExprList,
+      SqlNode offset,
+      SqlNode fetch) {
+    if (select.getOrderList() == null) {
+      assert collation.getFieldCollations().isEmpty();
+      if (offset == null && fetch == null) {
+        return;
+      }
+    }
+
+    // Create a sorter using the previously constructed collations.
+    bb.setRoot(
+        new Sort(
+            cluster,
+            cluster.traitSetOf(Convention.NONE, collation),
+            bb.root,
+            collation,
+            offset == null ? null : convertExpression(offset),
+            fetch == null ? null : convertExpression(fetch)),
+        false);
+
+    // If extra expressions were added to the project list for sorting,
+    // add another project to remove them.
+    if (orderExprList.size() > 0) {
+      List<RexNode> exprs = new ArrayList<RexNode>();
+      final RelDataType rowType = bb.root.getRowType();
+      final int fieldCount =
+          rowType.getFieldCount() - orderExprList.size();
+      for (int i = 0; i < fieldCount; i++) {
+        exprs.add(rexBuilder.makeInputRef(bb.root, i));
+      }
+      bb.setRoot(
+          new LogicalProject(
+              cluster,
+              cluster.traitSetOf(RelCollationImpl.PRESERVE),
+              bb.root,
+              exprs,
+              cluster.getTypeFactory().createStructType(
+                  rowType.getFieldList().subList(0, fieldCount)),
+              Project.Flags.BOXED),
+          false);
+    }
+  }
+
+  /**
+   * Returns whether a given node contains a {@link SqlInOperator}.
+   *
+   * @param node a RexNode tree
+   */
+  private static boolean containsInOperator(
+      SqlNode node) {
+    try {
+      SqlVisitor<Void> visitor =
+          new SqlBasicVisitor<Void>() {
+            public Void visit(SqlCall call) {
+              if (call.getOperator() instanceof SqlInOperator) {
+                throw new Util.FoundOne(call);
+              }
+              return super.visit(call);
+            }
+          };
+      node.accept(visitor);
+      return false;
+    } catch (Util.FoundOne e) {
+      Util.swallow(e, null);
+      return true;
+    }
+  }
+
+  /**
+   * Push down all the NOT logical operators into any IN/NOT IN operators.
+   *
+   * @param sqlNode the root node from which to look for NOT operators
+   * @return the transformed SqlNode representation with NOT pushed down.
+   */
+  private static SqlNode pushDownNotForIn(SqlNode sqlNode) {
+    if ((sqlNode instanceof SqlCall) && containsInOperator(sqlNode)) {
+      SqlCall sqlCall = (SqlCall) sqlNode;
+      if ((sqlCall.getOperator() == SqlStdOperatorTable.AND)
+          || (sqlCall.getOperator() == SqlStdOperatorTable.OR)) {
+        SqlNode[] sqlOperands = ((SqlBasicCall) sqlCall).operands;
+        for (int i = 0; i < sqlOperands.length; i++) {
+          sqlOperands[i] = pushDownNotForIn(sqlOperands[i]);
+        }
+        return sqlNode;
+      } else if (sqlCall.getOperator() == SqlStdOperatorTable.NOT) {
+        SqlNode childNode = sqlCall.operand(0);
+        assert childNode instanceof SqlCall;
+        SqlBasicCall childSqlCall = (SqlBasicCall) childNode;
+        if (childSqlCall.getOperator() == SqlStdOperatorTable.AND) {
+          SqlNode[] andOperands = childSqlCall.getOperands();
+          SqlNode[] orOperands = new SqlNode[andOperands.length];
+          for (int i = 0; i < orOperands.length; i++) {
+            orOperands[i] =
+                SqlStdOperatorTable.NOT.createCall(
+                    SqlParserPos.ZERO,
+                    andOperands[i]);
+          }
+          for (int i = 0; i < orOperands.length; i++) {
+            orOperands[i] = pushDownNotForIn(orOperands[i]);
+          }
+          return SqlStdOperatorTable.OR.createCall(SqlParserPos.ZERO,
+              orOperands[0], orOperands[1]);
+        } else if (childSqlCall.getOperator() == SqlStdOperatorTable.OR) {
+          SqlNode[] orOperands = childSqlCall.getOperands();
+          SqlNode[] andOperands = new SqlNode[orOperands.length];
+          for (int i = 0; i < andOperands.length; i++) {
+            andOperands[i] =
+                SqlStdOperatorTable.NOT.createCall(
+                    SqlParserPos.ZERO,
+                    orOperands[i]);
+          }
+          for (int i = 0; i < andOperands.length; i++) {
+            andOperands[i] = pushDownNotForIn(andOperands[i]);
+          }
+          return SqlStdOperatorTable.AND.createCall(SqlParserPos.ZERO,
+              andOperands[0], andOperands[1]);
+        } else if (childSqlCall.getOperator() == SqlStdOperatorTable.NOT) {
+          SqlNode[] notOperands = childSqlCall.getOperands();
+          assert notOperands.length == 1;
+          return pushDownNotForIn(notOperands[0]);
+        } else if (childSqlCall.getOperator() instanceof SqlInOperator) {
+          SqlNode[] inOperands = childSqlCall.getOperands();
+          SqlInOperator inOp =
+              (SqlInOperator) childSqlCall.getOperator();
+          if (inOp.isNotIn()) {
+            return SqlStdOperatorTable.IN.createCall(
+                SqlParserPos.ZERO,
+                inOperands[0],
+                inOperands[1]);
+          } else {
+            return SqlStdOperatorTable.NOT_IN.createCall(
+                SqlParserPos.ZERO,
+                inOperands[0],
+                inOperands[1]);
+          }
+        } else {
+          // childSqlCall is "leaf" node in a logical expression tree
+          // (only considering AND, OR, NOT)
+          return sqlNode;
+        }
+      } else {
+        // sqlNode is "leaf" node in a logical expression tree
+        // (only considering AND, OR, NOT)
+        return sqlNode;
+      }
+    } else {
+      // tree rooted at sqlNode does not contain inOperator
+      return sqlNode;
+    }
+  }
+
+  /**
+   * Converts a WHERE clause.
+   *
+   * @param bb    Blackboard
+   * @param where WHERE clause, may be null
+   */
+  private void convertWhere(
+      final Blackboard bb,
+      final SqlNode where) {
+    if (where == null) {
+      return;
+    }
+    SqlNode newWhere = pushDownNotForIn(where);
+    replaceSubqueries(bb, newWhere, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
+    final RexNode convertedWhere = bb.convertExpression(newWhere);
+
+    // only allocate filter if the condition is not TRUE
+    if (!convertedWhere.isAlwaysTrue()) {
+      bb.setRoot(
+          RelOptUtil.createFilter(bb.root, convertedWhere),
+          false);
+    }
+  }
+
+  private void replaceSubqueries(
+      final Blackboard bb,
+      final SqlNode expr,
+      RelOptUtil.Logic logic) {
+    findSubqueries(bb, expr, logic, false);
+    for (SubQuery node : bb.subqueryList) {
+      substituteSubquery(bb, node);
+    }
+  }
+
+  private void substituteSubquery(Blackboard bb, SubQuery subQuery) {
+    final RexNode expr = subQuery.expr;
+    if (expr != null) {
+      // Already done.
+      return;
+    }
+
+    final SqlBasicCall call;
+    final RelNode rel;
+    final SqlNode query;
+    final Pair<RelNode, Boolean> converted;
+    switch (subQuery.node.getKind()) {
+    case CURSOR:
+      convertCursor(bb, subQuery);
+      return;
+
+    case MULTISET_QUERY_CONSTRUCTOR:
+    case MULTISET_VALUE_CONSTRUCTOR:
+      rel = convertMultisets(ImmutableList.of(subQuery.node), bb);
+      subQuery.expr = bb.register(rel, JoinRelType.INNER);
+      return;
+
+    case IN:
+      call = (SqlBasicCall) subQuery.node;
+      final SqlNode[] operands = call.getOperands();
+
+      SqlNode leftKeyNode = operands[0];
+      query = operands[1];
+
+      final List<RexNode> leftKeys;
+      switch (leftKeyNode.getKind()) {
+      case ROW:
+        leftKeys = Lists.newArrayList();
+        for (SqlNode sqlExpr : ((SqlBasicCall) leftKeyNode).getOperandList()) {
+          leftKeys.add(bb.convertExpression(sqlExpr));
+        }
+        break;
+      default:
+        leftKeys = ImmutableList.of(bb.convertExpression(leftKeyNode));
+      }
+
+      final boolean isNotIn = ((SqlInOperator) call.getOperator()).isNotIn();
+      if (query instanceof SqlNodeList) {
+        SqlNodeList valueList = (SqlNodeList) query;
+        if (!containsNullLiteral(valueList)
+            && valueList.size() < getInSubqueryThreshold()) {
+          // We're under the threshold, so convert to OR.
+          subQuery.expr =
+              convertInToOr(
+                  bb,
+                  leftKeys,
+                  valueList,
+                  isNotIn);
+          return;
+        }
+
+        // Otherwise, let convertExists translate
+        // values list into an inline table for the
+        // reference to Q below.
+      }
+
+      // Project out the search columns from the left side
+
+      //  Q1:
+      // "select from emp where emp.deptno in (select col1 from T)"
+      //
+      // is converted to
+      //
+      // "select from
+      //   emp inner join (select distinct col1 from T)) q
+      //   on emp.deptno = q.col1
+      //
+      // Q2:
+      // "select from emp where emp.deptno not in (Q)"
+      //
+      // is converted to
+      //
+      // "select from
+      //   emp left outer join (select distinct col1, TRUE from T) q
+      //   on emp.deptno = q.col1
+      //   where emp.deptno <> null
+      //         and q.indicator <> TRUE"
+      //
+      final boolean outerJoin = bb.subqueryNeedsOuterJoin
+          || isNotIn
+          || subQuery.logic == RelOptUtil.Logic.TRUE_FALSE_UNKNOWN;
+      converted =
+          convertExists(query, RelOptUtil.SubqueryType.IN, subQuery.logic,
+              outerJoin);
+      if (converted.right) {
+        // Generate
+        //    emp CROSS JOIN (SELECT COUNT(*) AS c,
+        //                       COUNT(deptno) AS ck FROM dept)
+        final RelDataType longType =
+            typeFactory.createSqlType(SqlTypeName.BIGINT);
+        final RelNode seek = converted.left.getInput(0); // fragile
+        final int keyCount = leftKeys.size();
+        final List<Integer> args = ImmutableIntList.range(0, keyCount);
+        LogicalAggregate aggregate =
+            new LogicalAggregate(cluster, seek, false, ImmutableBitSet.of(),
+                null,
+                ImmutableList.of(
+                    new AggregateCall(SqlStdOperatorTable.COUNT, false,
+                        ImmutableList.<Integer>of(), longType, null),
+                    new AggregateCall(SqlStdOperatorTable.COUNT, false,
+                        args, longType, null)));
+        LogicalJoin join =
+            new LogicalJoin(cluster, bb.root, aggregate,
+                rexBuilder.makeLiteral(true), JoinRelType.INNER,
+                ImmutableSet.<String>of());
+        bb.setRoot(join, false);
+      }
+      RexNode rex =
+          bb.register(converted.left,
+              outerJoin ? JoinRelType.LEFT : JoinRelType.INNER, leftKeys);
+
+      subQuery.expr = translateIn(subQuery, bb.root, rex);
+      if (isNotIn) {
+        subQuery.expr =
+            rexBuilder.makeCall(SqlStdOperatorTable.NOT, subQuery.expr);
+      }
+      return;
+
+    case EXISTS:
+      // "select from emp where exists (select a from T)"
+      //
+      // is converted to the following if the subquery is correlated:
+      //
+      // "select from emp left outer join (select AGG_TRUE() as indicator
+      // from T group by corr_var) q where q.indicator is true"
+      //
+      // If there is no correlation, the expression is replaced with a
+      // boolean indicating whether the subquery returned 0 or >= 1 row.
+      call = (SqlBasicCall) subQuery.node;
+      query = call.getOperands()[0];
+      converted = convertExists(query, RelOptUtil.SubqueryType.EXISTS,
+          subQuery.logic, true);
+      assert !converted.right;
+      if (convertNonCorrelatedSubQuery(subQuery, bb, converted.left, true)) {
+        return;
+      }
+      subQuery.expr = bb.register(converted.left, JoinRelType.LEFT);
+      return;
+
+    case SCALAR_QUERY:
+      // Convert the subquery.  If it's non-correlated, convert it
+      // to a constant expression.
+      call = (SqlBasicCall) subQuery.node;
+      query = call.getOperands()[0];
+      converted = convertExists(query, RelOptUtil.SubqueryType.SCALAR,
+          subQuery.logic, true);
+      assert !converted.right;
+      if (convertNonCorrelatedSubQuery(subQuery, bb, converted.left, false)) {
+        return;
+      }
+      rel = convertToSingleValueSubq(query, converted.left);
+      subQuery.expr = bb.register(rel, JoinRelType.LEFT);
+      return;
+
+    case SELECT:
+      // This is used when converting multiset queries:
+      //
+      // select * from unnest(select multiset[deptno] from emps);
+      //
+      converted = convertExists(subQuery.node, RelOptUtil.SubqueryType.SCALAR,
+          subQuery.logic, true);
+      assert !converted.right;
+      subQuery.expr = bb.register(converted.left, JoinRelType.LEFT);
+      return;
+
+    default:
+      throw Util.newInternal("unexpected kind of subquery :" + subQuery.node);
+    }
+  }
+
+  private RexNode translateIn(SubQuery subQuery, RelNode root,
+      final RexNode rex) {
+    switch (subQuery.logic) {
+    case TRUE:
+      return rexBuilder.makeLiteral(true);
+
+    case UNKNOWN_AS_FALSE:
+      assert rex instanceof RexRangeRef;
+      final int fieldCount = rex.getType().getFieldCount();
+      RexNode rexNode = rexBuilder.makeFieldAccess(rex, fieldCount - 1);
+      rexNode = rexBuilder.makeCall(SqlStdOperatorTable.IS_TRUE, rexNode);
+
+      // Then append the IS NOT NULL(leftKeysForIn).
+      //
+      // RexRangeRef contains the following fields:
+      //   leftKeysForIn,
+      //   rightKeysForIn (the original subquery select list),
+      //   nullIndicator
+      //
+      // The first two lists contain the same number of fields.
+      final int k = (fieldCount - 1) / 2;
+      for (int i = 0; i < k; i++) {
+        rexNode =
+            rexBuilder.makeCall(
+                SqlStdOperatorTable.AND,
+                rexNode,
+                rexBuilder.makeCall(
+                    SqlStdOperatorTable.IS_NOT_NULL,
+                    rexBuilder.makeFieldAccess(rex, i)));
+      }
+      return rexNode;
+
+    case TRUE_FALSE_UNKNOWN:
+    case UNKNOWN_AS_TRUE:
+      // select e.deptno,
+      //   case
+      //   when ct.c = 0 then false
+      //   when dt.i is not null then true
+      //   when e.deptno is null then null
+      //   when ct.ck < ct.c then null
+      //   else false
+      //   end
+      // from e
+      // cross join (select count(*) as c, count(deptno) as ck from v) as ct
+      // left join (select distinct deptno, true as i from v) as dt
+      //   on e.deptno = dt.deptno
+      final Join join = (Join) root;
+      final Project left = (Project) join.getLeft();
+      final RelNode leftLeft = ((Join) left.getInput(0)).getLeft();
+      final int leftLeftCount = leftLeft.getRowType().getFieldCount();
+      final RelDataType nullableBooleanType =
+          typeFactory.createTypeWithNullability(
+              typeFactory.createSqlType(SqlTypeName.BOOLEAN), true);
+      final RelDataType longType =
+          typeFactory.createSqlType(SqlTypeName.BIGINT);
+      final RexNode cRef = rexBuilder.makeInputRef(root, leftLeftCount);
+      final RexNode ckRef = rexBuilder.makeInputRef(root, leftLeftCount + 1);
+      final RexNode iRef =
+          rexBuilder.makeInputRef(root, root.getRowType().getFieldCount() - 1);
+
+      final RexLiteral zero =
+          rexBuilder.makeExactLiteral(BigDecimal.ZERO, longType);
+      final RexLiteral trueLiteral = rexBuilder.makeLiteral(true);
+      final RexLiteral falseLiteral = rexBuilder.makeLiteral(false);
+      final RexNode unknownLiteral =
+          rexBuilder.makeNullLiteral(SqlTypeName.BOOLEAN);
+
+      final ImmutableList.Builder<RexNode> args = ImmutableList.builder();
+      args.add(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, cRef, zero),
+          falseLiteral,
+          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, iRef),
+          trueLiteral);
+      final JoinInfo joinInfo = join.analyzeCondition();
+      for (int leftKey : joinInfo.leftKeys) {
+        final RexNode kRef = rexBuilder.makeInputRef(root, leftKey);
+        args.add(rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, kRef),
+            unknownLiteral);
+      }
+      args.add(rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN, ckRef, cRef),
+          unknownLiteral,
+          falseLiteral);
+
+      return rexBuilder.makeCall(
+          nullableBooleanType,
+          SqlStdOperatorTable.CASE,
+          args.build());
+
+    default:
+      throw new AssertionError(subQuery.logic);
+    }
+  }
+
+  private static boolean containsNullLiteral(SqlNodeList valueList) {
+    for (SqlNode node : valueList.getList()) {
+      if (node instanceof SqlLiteral) {
+        SqlLiteral lit = (SqlLiteral) node;
+        if (lit.getValue() == null) {
+          return true;
+        }
+      }
+    }
+    return false;
+  }
+
+  /**
+   * Determines if a subquery is non-correlated and if so, converts it to a
+   * constant.
+   *
+   * @param subQuery  the call that references the subquery
+   * @param bb        blackboard used to convert the subquery
+   * @param converted RelNode tree corresponding to the subquery
+   * @param isExists  true if the subquery is part of an EXISTS expression
+   * @return if the subquery can be converted to a constant
+   */
+  private boolean convertNonCorrelatedSubQuery(
+      SubQuery subQuery,
+      Blackboard bb,
+      RelNode converted,
+      boolean isExists) {
+    SqlCall call = (SqlBasicCall) subQuery.node;
+    if (subqueryConverter.canConvertSubquery()
+        && isSubQueryNonCorrelated(converted, bb)) {
+      // First check if the subquery has already been converted
+      // because it's a nested subquery.  If so, don't re-evaluate
+      // it again.
+      RexNode constExpr = mapConvertedNonCorrSubqs.get(call);
+      if (constExpr == null) {
+        constExpr =
+            subqueryConverter.convertSubquery(
+                call,
+                this,
+                isExists,
+                isExplain);
+      }
+      if (constExpr != null) {
+        subQuery.expr = constExpr;
+        mapConvertedNonCorrSubqs.put(call, constExpr);
+        return true;
+      }
+    }
+    return false;
+  }
+
+  /**
+   * Converts the RelNode tree for a select statement to a select that
+   * produces a single value.
+   *
+   * @param query the query
+   * @param plan   the original RelNode tree corresponding to the statement
+   * @return the converted RelNode tree
+   */
+  public RelNode convertToSingleValueSubq(
+      SqlNode query,
+      RelNode plan) {
+    // Check whether query is guaranteed to produce a single value.
+    if (query instanceof SqlSelect) {
+      SqlSelect select = (SqlSelect) query;
+      SqlNodeList selectList = select.getSelectList();
+      SqlNodeList groupList = select.getGroup();
+
+      if ((selectList.size() == 1)
+          && ((groupList == null) || (groupList.size() == 0))) {
+        SqlNode selectExpr = selectList.get(0);
+        if (selectExpr instanceof SqlCall) {
+          SqlCall selectExprCall = (SqlCall) selectExpr;
+          if (selectExprCall.getOperator()
+              instanceof SqlAggFunction) {
+            return plan;
+          }
+        }
+      }
+    }
+
+    // If not, project SingleValueAgg
+    return RelOptUtil.createSingleValueAggRel(
+        cluster,
+        plan);
+  }
+
+  /**
+   * Converts "x IN (1, 2, ...)" to "x=1 OR x=2 OR ...".
+   *
+   * @param leftKeys   LHS
+   * @param valuesList RHS
+   * @param isNotIn    is this a NOT IN operator
+   * @return converted expression
+   */
+  private RexNode convertInToOr(
+      final Blackboard bb,
+      final List<RexNode> leftKeys,
+      SqlNodeList valuesList,
+      boolean isNotIn) {
+    List<RexNode> comparisons = new ArrayList<RexNode>();
+    for (SqlNode rightVals : valuesList) {
+      RexNode rexComparison;
+      if (leftKeys.size() == 1) {
+        rexComparison =
+            rexBuilder.makeCall(
+                SqlStdOperatorTable.EQUALS,
+                leftKeys.get(0),
+                bb.convertExpression(rightVals));
+      } else {
+        assert rightVals instanceof SqlCall;
+        final SqlBasicCall call = (SqlBasicCall) rightVals;
+        assert (call.getOperator() instanceof SqlRowOperator)
+            && call.getOperands().length == leftKeys.size();
+        rexComparison =
+            RexUtil.composeConjunction(
+                rexBuilder,
+                Iterables.transform(
+                    Pair.zip(leftKeys, call.getOperandList()),
+                    new Function<Pair<RexNode, SqlNode>, RexNode>() {
+                      public RexNode apply(Pair<RexNode, SqlNode> pair) {
+                        return rexBuilder.makeCall(SqlStdOperatorTable.EQUALS,
+                            pair.left, bb.convertExpression(pair.right));
+                      }
+                    }),
+                false);
+      }
+      comparisons.add(rexComparison);
+    }
+
+    RexNode result =
+        RexUtil.composeDisjunction(rexBuilder, comparisons, true);
+    assert result != null;
+
+    if (isNotIn) {
+      result =
+          rexBuilder.makeCall(
+              SqlStdOperatorTable.NOT,
+              result);
+    }
+
+    return result;
+  }
+
+  /**
+   * Gets the list size threshold under which {@link #convertInToOr} is used.
+   * Lists of this size or greater will instead be converted to use a join
+   * against an inline table
+   * ({@link org.apache.calcite.rel.logical.LogicalValues}) rather than a
+   * predicate. A threshold of 0 forces usage of an inline table in all cases; a
+   * threshold of Integer.MAX_VALUE forces usage of OR in all cases
+   *
+   * @return threshold, default 20
+   */
+  protected int getInSubqueryThreshold() {
+    // OVERRIDE POINT
+    return Integer.MAX_VALUE; // was 20
+  }
+
+  /**
+   * Converts an EXISTS or IN predicate into a join. For EXISTS, the subquery
+   * produces an indicator variable, and the result is a relational expression
+   * which outer joins that indicator to the original query. After performing
+   * the outer join, the condition will be TRUE if the EXISTS condition holds,
+   * NULL otherwise.
+   *
+   * @param seek           A query, for example 'select * from emp' or
+   *                       'values (1,2,3)' or '('Foo', 34)'.
+   * @param subqueryType   Whether sub-query is IN, EXISTS or scalar
+   * @param logic Whether the answer needs to be in full 3-valued logic (TRUE,
+   *     FALSE, UNKNOWN) will be required, or whether we can accept an
+   *     approximation (say representing UNKNOWN as FALSE)
+   * @param needsOuterJoin Whether an outer join is needed
+   * @return join expression
+   * @pre extraExpr == null || extraName != null
+   */
+  private Pair<RelNode, Boolean> convertExists(
+      SqlNode seek,
+      RelOptUtil.SubqueryType subqueryType,
+      RelOptUtil.Logic logic,
+      boolean needsOuterJoin) {
+    final SqlValidatorScope seekScope =
+        (seek instanceof SqlSelect)
+            ? validator.getSelectScope((SqlSelect) seek)
+            : null;
+    final Blackboard seekBb = createBlackboard(seekScope, null);
+    RelNode seekRel = convertQueryOrInList(seekBb, seek);
+
+    return RelOptUtil.createExistsPlan(seekRel, subqueryType, logic,
+        needsOuterJoin);
+  }
+
+  private RelNode convertQueryOrInList(
+      Blackboard bb,
+      SqlNode seek) {
+    // NOTE: Once we start accepting single-row queries as row constructors,
+    // there will be an ambiguity here for a case like X IN ((SELECT Y FROM
+    // Z)).  The SQL standard resolves the ambiguity by saying that a lone
+    // select should be interpreted as a table expression, not a row
+    // expression.  The semantic difference is that a table expression can
+    // return multiple rows.
+    if (seek instanceof SqlNodeList) {
+      return convertRowValues(
+          bb,
+          seek,
+          ((SqlNodeList) seek).getList(),
+          false,
+          null);
+    } else {
+      return convertQueryRecursive(seek, false, null);
+    }
+  }
+
+  private RelNode convertRowValues(
+      Blackboard bb,
+      SqlNode rowList,
+      Collection<SqlNode> rows,
+      boolean allowLiteralsOnly,
+      RelDataType targetRowType) {
+    // NOTE jvs 30-Apr-2006: We combine all rows consisting entirely of
+    // literals into a single LogicalValues; this gives the optimizer a smaller
+    // input tree.  For everything else (computed expressions, row
+    // subqueries), we union each row in as a projection on top of a
+    // LogicalOneRow.
+
+    final ImmutableList.Builder<ImmutableList<RexLiteral>> tupleList =
+        ImmutableList.builder();
+    final RelDataType rowType;
+    if (targetRowType != null) {
+      rowType = targetRowType;
+    } else {
+      rowType =
+          SqlTypeUtil.promoteToRowType(
+              typeFactory,
+              validator.getValidatedNodeType(rowList),
+              null);
+    }
+
+    List<RelNode> unionInputs = new ArrayList<RelNode>();
+    for (SqlNode node : rows) {
+      SqlBasicCall call;
+      if (isRowConstructor(node)) {
+        call = (SqlBasicCall) node;
+        ImmutableList.Builder<RexLiteral> tuple = ImmutableList.builder();
+        for (Ord<SqlNode> operand : Ord.zip(call.operands)) {
+          RexLiteral rexLiteral =
+              convertLiteralInValuesList(
+                  operand.e,
+                  bb,
+                  rowType,
+                  operand.i);
+          if ((rexLiteral == null) && allowLiteralsOnly) {
+            return null;
+          }
+          if ((rexLiteral == null) || !shouldCreateValuesRel) {
+            // fallback to convertRowConstructor
+            tuple = null;
+            break;
+          }
+          tuple.add(rexLiteral);
+        }
+        if (tuple != null) {
+          tupleList.add(tuple.build());
+          continue;
+        }
+      } else {
+        RexLiteral rexLiteral =
+            convertLiteralInValuesList(
+                node,
+                bb,
+                rowType,
+                0);
+        if ((rexLiteral != null) && shouldCreateValuesRel) {
+          tupleList.add(ImmutableList.of(rexLiteral));
+          continue;
+        } else {
+          if ((rexLiteral == null) && allowLiteralsOnly) {
+            return null;
+          }
+        }
+
+        // convert "1" to "row(1)"
+        call =
+            (SqlBasicCall) SqlStdOperatorTable.ROW.createCall(
+                SqlParserPos.ZERO,
+                node);
+      }
+      unionInputs.add(convertRowConstructor(bb, call));
+    }
+    LogicalValues values =
+        new LogicalValues(
+            cluster,
+            rowType,
+            tupleList.build());
+    RelNode resultRel;
+    if (unionInputs.isEmpty()) {
+      resultRel = values;
+    } else {
+      if (!values.getTuples().isEmpty()) {
+        unionInputs.add(values);
+      }
+      LogicalUnion union =
+          new LogicalUnion(
+              cluster,
+              unionInputs,
+              true);
+      resultRel = union;
+    }
+    leaves.add(resultRel);
+    return resultRel;
+  }
+
+  private RexLiteral convertLiteralInValuesList(
+      SqlNode sqlNode,
+      Blackboard bb,
+      RelDataType rowType,
+      int iField) {
+    if (!(sqlNode instanceof SqlLiteral)) {
+      return null;
+    }
+    RelDataTypeField field = rowType.getFieldList().get(iField);
+    RelDataType type = field.getType();
+    if (type.isStruct()) {
+      // null literals for weird stuff like UDT's need
+      // special handling during type flattening, so
+      // don't use LogicalValues for those
+      return null;
+    }
+
+    RexNode literalExpr =
+        exprConverter.convertLiteral(
+            bb,
+            (SqlLiteral) sqlNode);
+
+    if (!(literalExpr instanceof RexLiteral)) {
+      assert literalExpr.isA(SqlKind.CAST);
+      RexNode child = ((RexCall) literalExpr).getOperands().get(0);
+      assert RexLiteral.isNullLiteral(child);
+
+      // NOTE jvs 22-Nov-2006:  we preserve type info
+      // in LogicalValues digest, so it's OK to lose it here
+      return (RexLiteral) child;
+    }
+
+    RexLiteral literal = (RexLiteral) literalExpr;
+
+    Comparable value = literal.getValue();
+
+    if (SqlTypeUtil.isExactNumeric(type)) {
+      BigDecimal roundedValue =
+          NumberUtil.rescaleBigDecimal(
+              (BigDecimal) value,
+              type.getScale());
+      return rexBuilder.makeExactLiteral(
+          roundedValue,
+          type);
+    }
+
+    if ((value instanceof NlsString)
+        && (type.getSqlTypeName() == SqlTypeName.CHAR)) {
+      // pad fixed character type
+      NlsString unpadded = (NlsString) value;
+      return rexBuilder.makeCharLiteral(
+          new NlsString(
+              Util.rpad(unpadded.getValue(), type.getPrecision()),
+              unpadded.getCharsetName(),
+              unpadded.getCollation()));
+    }
+    return literal;
+  }
+
+  private boolean isRowConstructor(SqlNode node) {
+    if (!(node.getKind() == SqlKind.ROW)) {
+      return false;
+    }
+    SqlCall call = (SqlCall) node;
+    return call.getOperator().getName().equalsIgnoreCase("row");
+  }
+
+  /**
+   * Builds a list of all <code>IN</code> or <code>EXISTS</code> operators
+   * inside SQL parse tree. Does not traverse inside queries.
+   *
+   * @param bb                           blackboard
+   * @param node                         the SQL parse tree
+   * @param logic Whether the answer needs to be in full 3-valued logic (TRUE,
+   *              FALSE, UNKNOWN) will be required, or whether we can accept
+   *              an approximation (say representing UNKNOWN as FALSE)
+   * @param registerOnlyScalarSubqueries if set to true and the parse tree
+   *                                     corresponds to a variation of a select
+   *                                     node, only register it if it's a scalar
+   *                                     subquery
+   */
+  private void findSubqueries(
+      Blackboard bb,
+      SqlNode node,
+      RelOptUtil.Logic logic,
+      boolean registerOnlyScalarSubqueries) {
+    final SqlKind kind = node.getKind();
+    switch (kind) {
+    case EXISTS:
+    case SELECT:
+    case MULTISET_QUERY_CONSTRUCTOR:
+    case MULTISET_VALUE_CONSTRUCTOR:
+    case CURSOR:
+    case SCALAR_QUERY:
+      if (!registerOnlyScalarSubqueries
+          || (kind == SqlKind.SCALAR_QUERY)) {
+        bb.registerSubquery(node, RelOptUtil.Logic.TRUE_FALSE);
+      }
+      return;
+    case IN:
+      if (((SqlCall) node).getOperator() == SqlStdOperatorTable.NOT_IN) {
+        logic = logic.negate();
+      }
+      break;
+    case NOT:
+      logic = logic.negate();
+      break;
+    }
+    if (node instanceof SqlCall) {
+      if (kind == SqlKind.OR
+          || kind == SqlKind.NOT) {
+        // It's always correct to outer join subquery with
+        // containing query; however, when predicates involve Or
+        // or NOT, outer join might be necessary.
+        bb.subqueryNeedsOuterJoin = true;
+      }
+      for (SqlNode operand : ((SqlCall) node).getOperandList()) {
+        if (operand != null) {
+          // In the case of an IN expression, locate scalar
+          // subqueries so we can convert them to constants
+          findSubqueries(
+              bb,
+              operand,
+              logic,
+              kind == SqlKind.IN || registerOnlyScalarSubqueries);
+        }
+      }
+    } else if (node instanceof SqlNodeList) {
+      for (SqlNode child : (SqlNodeList) node) {
+        findSubqueries(
+            bb,
+            child,
+            logic,
+            kind == SqlKind.IN || registerOnlyScalarSubqueries);
+      }
+    }
+
+    // Now that we've located any scalar subqueries inside the IN
+    // expression, register the IN expression itself.  We need to
+    // register the scalar subqueries first so they can be converted
+    // before the IN expression is converted.
+    if (kind == SqlKind.IN) {
+      if (logic == RelOptUtil.Logic.TRUE_FALSE_UNKNOWN
+          && !validator.getValidatedNodeType(node).isNullable()) {
+        logic = RelOptUtil.Logic.UNKNOWN_AS_FALSE;
+      }
+      // TODO: This conversion is only valid in the WHERE clause
+      if (logic == RelOptUtil.Logic.UNKNOWN_AS_FALSE
+          && !bb.subqueryNeedsOuterJoin) {
+        logic = RelOptUtil.Logic.TRUE;
+      }
+      bb.registerSubquery(node, logic);
+    }
+  }
+
+  /**
+   * Converts an expression from {@link SqlNode} to {@link RexNode} format.
+   *
+   * @param node Expression to translate
+   * @return Converted expression
+   */
+  public RexNode convertExpression(
+      SqlNode node) {
+    Map<String, RelDataType> nameToTypeMap = Collections.emptyMap();
+    Blackboard bb =
+        createBlackboard(
+            new ParameterScope((SqlValidatorImpl) validator, nameToTypeMap),
+            null);
+    return bb.convertExpression(node);
+  }
+
+  /**
+   * Converts an expression from {@link SqlNode} to {@link RexNode} format,
+   * mapping identifier references to predefined expressions.
+   *
+   * @param node          Expression to translate
+   * @param nameToNodeMap map from String to {@link RexNode}; when an
+   *                      {@link SqlIdentifier} is encountered, it is used as a
+   *                      key and translated to the corresponding value from
+   *                      this map
+   * @return Converted expression
+   */
+  public RexNode convertExpression(
+      SqlNode node,
+      Map<String, RexNode> nameToNodeMap) {
+    final Map<String, RelDataType> nameToTypeMap =
+        new HashMap<String, RelDataType>();
+    for (Map.Entry<String, RexNode> entry : nameToNodeMap.entrySet()) {
+      nameToTypeMap.put(entry.getKey(), entry.getValue().getType());
+    }
+    Blackboard bb =
+        createBlackboard(
+            new ParameterScope((SqlValidatorImpl) validator, nameToTypeMap),
+            nameToNodeMap);
+    return bb.convertExpression(node);
+  }
+
+  /**
+   * Converts a non-standard expression.
+   *
+   * <p>This method is an extension-point that derived classes can override. If
+   * this method returns a null result, the normal expression translation
+   * process will proceed. The default implementation always returns null.
+   *
+   * @param node Expression
+   * @param bb   Blackboard
+   * @return null to proceed with the usual expression translation process
+   */
+  protected RexNode convertExtendedExpression(
+      SqlNode node,
+      Blackboard bb) {
+    return null;
+  }
+
+  private RexNode convertOver(Blackboard bb, SqlNode node) {
+    SqlCall call = (SqlCall) node;
+    SqlCall aggCall = call.operand(0);
+    SqlNode windowOrRef = call.operand(1);
+    final SqlWindow window =
+        validator.resolveWindow(windowOrRef, bb.scope, true);
+    final SqlNodeList partitionList = window.getPartitionList();
+    final ImmutableList.Builder<RexNode> partitionKeys =
+        ImmutableList.builder();
+    for (SqlNode partition : partitionList) {
+      partitionKeys.add(bb.convertExpression(partition));
+    }
+    RexNode lowerBound = bb.convertExpression(window.getLowerBound());
+    RexNode upperBound = bb.convertExpression(window.getUpperBound());
+    SqlNodeList orderList = window.getOrderList();
+    if ((orderList.size() == 0) && !window.isRows()) {
+      // A logical range requires an ORDER BY clause. Use the implicit
+      // ordering of this relation. There must be one, otherwise it would
+      // have failed validation.
+      orderList = bb.scope.getOrderList();
+      if (orderList == null) {
+        throw new AssertionError(
+            "Relation should have sort key for implicit ORDER BY");
+      }
+    }
+    final ImmutableList.Builder<RexFieldCollation> orderKeys =
+        ImmutableList.builder();
+    final Set<SqlKind> flags = EnumSet.noneOf(SqlKind.class);
+    for (SqlNode order : orderList) {
+      flags.clear();
+      RexNode e = bb.convertSortExpression(order, flags);
+      orderKeys.add(new RexFieldCollation(e, flags));
+    }
+    try {
+      Util.permAssert(bb.window == null, "already in window agg mode");
+      bb.window = window;
+      RexNode rexAgg = exprConverter.convertCall(bb, aggCall);
+      rexAgg =
+          rexBuilder.ensureType(
+              validator.getValidatedNodeType(call), rexAgg, false);
+
+      // Walk over the tree and apply 'over' to all agg functions. This is
+      // necessary because the returned expression is not necessarily a call
+      // to an agg function. For example, AVG(x) becomes SUM(x) / COUNT(x).
+      final RexShuttle visitor =
+          new HistogramShuttle(
+              partitionKeys.build(), orderKeys.build(),
+              RexWindowBound.create(window.getLowerBound(), lowerBound),
+              RexWindowBound.create(window.getUpperBound(), upperBound),
+              window);
+      return rexAgg.accept(visitor);
+    } finally {
+      bb.window = null;
+    }
+  }
+
+  /**
+   * Converts a FROM clause into a relational expression.
+   *
+   * @param bb   Scope within which to resolve identifiers
+   * @param from FROM clause of a query. Examples include:
+   *
+   *             <ul>
+   *             <li>a single table ("SALES.EMP"),
+   *             <li>an aliased table ("EMP AS E"),
+   *             <li>a list of tables ("EMP, DEPT"),
+   *             <li>an ANSI Join expression ("EMP JOIN DEPT ON EMP.DEPTNO =
+   *             DEPT.DEPTNO"),
+   *             <li>a VALUES clause ("VALUES ('Fred', 20)"),
+   *             <li>a query ("(SELECT * FROM EMP WHERE GENDER = 'F')"),
+   *             <li>or any combination of the above.
+   *             </ul>
+   */
+  protected void convertFrom(
+      Blackboard bb,
+      SqlNode from) {
+    SqlCall call;
+    final SqlNode[] operands;
+    switch (from.getKind()) {
+    case AS:
+      operands = ((SqlBasicCall) from).getOperands();
+      convertFrom(bb, operands[0]);
+      return;
+
+    case WITH_ITEM:
+      convertFrom(bb, ((SqlWithItem) from).query);
+      return;
+
+    case WITH:
+      convertFrom(bb, ((SqlWith) from).body);
+      return;
+
+    case TABLESAMPLE:
+      operands = ((SqlBasicCall) from).getOperands();
+      SqlSampleSpec sampleSpec = SqlLiteral.sampleValue(operands[1]);
+      if (sampleSpec instanceof SqlSampleSpec.SqlSubstitutionSampleSpec) {
+        String sampleName =
+            ((SqlSampleSpec.SqlSubstitutionSampleSpec) sampleSpec)
+                .getName();
+        datasetStack.push(sampleName);
+        convertFrom(bb, operands[0]);
+        datasetStack.pop();
+      } else if (sampleSpec instanceof SqlSampleSpec.SqlTableSampleSpec) {
+        SqlSampleSpec.SqlTableSampleSpec tableSampleSpec =
+            (SqlSampleSpec.SqlTableSampleSpec) sampleSpec;
+        convertFrom(bb, operands[0]);
+        RelOptSamplingParameters params =
+            new RelOptSamplingParameters(
+                tableSampleSpec.isBernoulli(),
+                tableSampleSpec.getSamplePercentage(),
+                tableSampleSpec.isRepeatable(),
+                tableSampleSpec.getRepeatableSeed());
+        bb.setRoot(new Sample(cluster, bb.root, params), false);
+      } else {
+        throw Util.newInternal(
+            "unknown TABLESAMPLE type: " + sampleSpec);
+      }
+      return;
+
+    case IDENTIFIER:
+      final SqlValidatorNamespace fromNamespace =
+          validator.getNamespace(from).resolve();
+      if (fromNamespace.getNode() != null) {
+        convertFrom(bb, fromNamespace.getNode());
+        return;
+      }
+      final String datasetName =
+          datasetStack.isEmpty() ? null : datasetStack.peek();
+      boolean[] usedDataset = {false};
+      RelOptTable table =
+          SqlValidatorUtil.getRelOptTable(
+              fromNamespace,
+              catalogReader,
+              datasetName,
+              usedDataset);
+      final RelNode tableRel;
+      if (shouldConvertTableAccess) {
+        tableRel = toRel(table);
+      } else {
+        tableRel = new LogicalTableScan(cluster, table);
+      }
+      bb.setRoot(tableRel, true);
+      if (usedDataset[0]) {
+        bb.setDataset(datasetName);
+      }
+      return;
+
+    case JOIN:
+      final SqlJoin join = (SqlJoin) from;
+      final Blackboard fromBlackboard =
+          createBlackboard(validator.getJoinScope(from), null);
+      SqlNode left = join.getLeft();
+      SqlNode right = join.getRight();
+      final boolean isNatural = join.isNatural();
+      final JoinType joinType = join.getJoinType();
+      final Blackboard leftBlackboard =
+          createBlackboard(
+              Util.first(validator.getJoinScope(left),
+                  ((DelegatingScope) bb.scope).getParent()), null);
+      final Blackboard rightBlackboard =
+          createBlackboard(
+              Util.first(validator.getJoinScope(right),
+                  ((DelegatingScope) bb.scope).getParent()), null);
+      convertFrom(leftBlackboard, left);
+      RelNode leftRel = leftBlackboard.root;
+      convertFrom(rightBlackboard, right);
+      RelNode rightRel = rightBlackboard.root;
+      JoinRelType convertedJoinType = convertJoinType(joinType);
+      RexNode conditionExp;
+      if (isNatural) {
+        final List<String> columnList =
+            SqlValidatorUtil.deriveNaturalJoinColumnList(
+                validator.getNamespace(left).getRowType(),
+                validator.getNamespace(right).getRowType());
+        conditionExp = convertUsing(leftRel, rightRel, columnList);
+      } else {
+        conditionExp =
+            convertJoinCondition(
+                fromBlackboard,
+                join.getCondition(),
+                join.getConditionType(),
+                leftRel,
+                rightRel);
+      }
+
+      final RelNode joinRel =
+          createJoin(
+              fromBlackboard,
+              leftRel,
+              rightRel,
+              conditionExp,
+              convertedJoinType);
+      bb.setRoot(joinRel, false);
+      return;
+
+    case SELECT:
+    case INTERSECT:
+    case EXCEPT:
+    case UNION:
+      final RelNode rel = convertQueryRecursive(from, false, null);
+      bb.setRoot(rel, true);
+      return;
+
+    case VALUES:
+      convertValuesImpl(bb, (SqlCall) from, null);
+      return;
+
+    case UNNEST:
+      final SqlNode node = ((SqlCall) from).operand(0);
+      replaceSubqueries(bb, node, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
+      final RelNode childRel =
+          RelOptUtil.createProject(
+              (null != bb.root) ? bb.root : LogicalValues.createOneRow(cluster),
+              Collections.singletonList(bb.convertExpression(node)),
+              Collections.singletonList(validator.deriveAlias(node, 0)),
+              true);
+
+      Uncollect uncollect =
+          new Uncollect(cluster, cluster.traitSetOf(Convention.NONE),
+              childRel);
+      bb.setRoot(uncollect, true);
+      return;
+
+    case COLLECTION_TABLE:
+      call = (SqlCall) from;
+
+      // Dig out real call; TABLE() wrapper is just syntactic.
+      assert call.getOperandList().size() == 1;
+      call = call.operand(0);
+      convertCollectionTable(bb, call);
+      return;
+
+    default:
+      throw Util.newInternal("not a join operator " + from);
+    }
+  }
+
+  protected void convertCollectionTable(
+      Blackboard bb,
+      SqlCall call) {
+    final SqlOperator operator = call.getOperator();
+    if (operator == SqlStdOperatorTable.TABLESAMPLE) {
+      final String sampleName =
+          SqlLiteral.stringValue(call.operand(0));
+      datasetStack.push(sampleName);
+      SqlCall cursorCall = call.operand(1);
+      SqlNode query = cursorCall.operand(0);
+      RelNode converted = convertQuery(query, false, false);
+      bb.setRoot(converted, false);
+      datasetStack.pop();
+      return;
+    }
+    replaceSubqueries(bb, call, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
+
+    // Expand table macro if possible. It's more efficient than
+    // LogicalTableFunctionScan.
+    if (operator instanceof SqlUserDefinedTableMacro) {
+      final SqlUserDefinedTableMacro udf =
+          (SqlUserDefinedTableMacro) operator;
+      final TranslatableTable table = udf.getTable(typeFactory,
+        call.getOperandList());
+      final RelDataType rowType = table.getRowType(typeFactory);
+      RelOptTable relOptTable = RelOptTableImpl.create(null, rowType, table);
+      RelNode converted = toRel(relOptTable);
+      bb.setRoot(converted, true);
+      return;
+    }
+
+    Type elementType;
+    if (operator instanceof SqlUserDefinedTableFunction) {
+      SqlUserDefinedTableFunction udtf = (SqlUserDefinedTableFunction) operator;
+      elementType = udtf.getElementType(typeFactory, call.getOperandList());
+    } else {
+      elementType = null;
+    }
+
+    RexNode rexCall = bb.convertExpression(call);
+    final List<RelNode> inputs = bb.retrieveCursors();
+    Set<RelColumnMapping> columnMappings =
+        getColumnMappings(operator);
+    LogicalTableFunctionScan callRel =
+        new LogicalTableFunctionScan(
+            cluster,
+            inputs,
+            rexCall,
+            elementType,
+            validator.getValidatedNodeType(call),
+            columnMappings);
+    bb.setRoot(callRel, true);
+    afterTableFunction(bb, call, callRel);
+  }
+
+  protected void afterTableFunction(
+      SqlToRelConverter.Blackboard bb,
+      SqlCall call,
+      LogicalTableFunctionScan callRel) {
+  }
+
+  private Set<RelColumnMapping> getColumnMappings(SqlOperator op) {
+    SqlReturnTypeInference rti = op.getReturnTypeInference();
+    if (rti == null) {
+      return null;
+    }
+    if (rti instanceof TableFunctionReturnTypeInference) {
+      TableFunctionReturnTypeInference tfrti =
+          (TableFunctionReturnTypeInference) rti;
+      return tfrti.getColumnMappings();
+    } else {
+      return null;
+    }
+  }
+
+  protected RelNode createJoin(
+      Blackboard bb,
+      RelNode leftRel,
+      RelNode rightRel,
+      RexNode joinCond,
+      JoinRelType joinType) {
+    assert joinCond != null;
+
+    Set<String> correlatedVariables = RelOptUtil.getVariablesUsed(rightRel);
+    if (correlatedVariables.size() > 0) {
+      final ImmutableBitSet.Builder requiredColumns = ImmutableBitSet.builder();
+      final List<String> correlNames = Lists.newArrayList();
+
+      // All correlations must refer the same namespace since correlation
+      // produces exactly one correlation source.
+      // The same source might be referenced by different variables since
+      // DeferredLookups are not de-duplicated at create time.
+      SqlValidatorNamespace prevNs = null;
+
+      for (String correlName : correlatedVariables) {
+        DeferredLookup lookup = mapCorrelToDeferred.get(correlName);
+        RexFieldAccess fieldAccess = lookup.getFieldAccess(correlName);
+        String originalRelName = lookup.getOriginalRelName();
+        String originalFieldName = fieldAccess.getField().getName();
+
+        int[] nsIndexes = {-1};
+        final SqlValidatorScope[] ancestorScopes = {null};
+        SqlValidatorNamespace foundNs =
+            lookup.bb.scope.resolve(
+                ImmutableList.of(originalRelName),
+                ancestorScopes,
+                nsIndexes);
+
+        assert foundNs != null;
+        assert nsIndexes.length == 1;
+
+        int childNamespaceIndex = nsIndexes[0];
+
+        SqlValidatorScope ancestorScope = ancestorScopes[0];
+        boolean correlInCurrentScope = ancestorScope == bb.scope;
+
+        if (!correlInCurrentScope) {
+          continue;
+        }
+
+        if (prevNs == null) {
+          prevNs = foundNs;
+        } else {
+          assert prevNs == foundNs : "All correlation variables should resolve"
+              + " to the same namespace."
+              + " Prev ns=" + prevNs
+              + ", new ns=" + foundNs;
+        }
+
+        int namespaceOffset = 0;
+        if (childNamespaceIndex > 0) {
+          // If not the first child, need to figure out the width
+          // of output types from all the preceding namespaces
+          assert ancestorScope instanceof ListScope;
+          List<SqlValidatorNamespace> children =
+              ((ListScope) ancestorScope).getChildren();
+
+          for (int i = 0; i < childNamespaceIndex; i++) {
+            SqlValidatorNamespace child = children.get(i);
+            namespaceOffset +=
+                child.getRowType().getFieldCount();
+          }
+        }
+
+        RelDataTypeField field =
+            catalogReader.field(foundNs.getRowType(), originalFieldName);
+        int pos = namespaceOffset + field.getIndex();
+
+        assert field.getType()
+            == lookup.getFieldAccess(correlName).getField().getType();
+
+        assert pos != -1;
+
+        if (bb.mapRootRelToFieldProjection.containsKey(bb.root)) {
+          // bb.root is an aggregate and only projects group by
+          // keys.
+          Map<Integer, Integer> exprProjection =
+              bb.mapRootRelToFieldProjection.get(bb.root);
+
+          // subquery can reference group by keys projected from
+          // the root of the outer relation.
+          if (exprProjection.containsKey(pos)) {
+            pos = exprProjection.get(pos);
+          } else {
+            // correl not grouped
+            throw Util.newInternal(
+                "Identifier '" + originalRelName + "."
+                + originalFieldName + "' is not a group expr");
+          }
+        }
+
+        requiredColumns.set(pos);
+        correlNames.add(correlName);
+      }
+
+      if (!correlNames.isEmpty()) {
+        if (correlNames.size() > 1) {
+          // The same table was referenced more than once.
+          // So we deduplicate
+          RelShuttle dedup =
+              new DeduplicateCorrelateVariables(rexBuilder,
+                  correlNames.get(0),
+                  ImmutableSet.copyOf(Util.skip(correlNames)));
+          rightRel = rightRel.accept(dedup);
+        }
+        LogicalCorrelate corr = new LogicalCorrelate(
+            rightRel.getCluster(),
+            leftRel,
+            rightRel,
+            new CorrelationId(correlNames.get(0)),
+            requiredColumns.build(),
+            SemiJoinType.of(joinType));
+        if (!joinCond.isAlwaysTrue()) {
+          return RelOptUtil.createFilter(corr, joinCond);
+        }
+        return corr;
+      }
+    }
+
+    final List<RexNode> extraLeftExprs = new ArrayList<RexNode>();
+    final List<RexNode> extraRightExprs = new ArrayList<RexNode>();
+    final int leftCount = leftRel.getRowType().getFieldCount();
+    final int rightCount = rightRel.getRowType().getFieldCount();
+    if (!containsGet(joinCond)) {
+      joinCond = pushDownJoinConditions(
+          joinCond, leftCount, rightCount, extraLeftExprs, extraRightExprs);
+    }
+    if (!extraLeftExprs.isEmpty()) {
+      final List<RelDataTypeField> fields =
+          leftRel.getRowType().getFieldList();
+      leftRel = RelOptUtil.createProject(
+          leftRel,
+          new AbstractList<Pair<RexNode, String>>() {
+            @Override public int size() {
+              return leftCount + extraLeftExprs.size();
+            }
+
+            @Override public Pair<RexNode, String> get(int index) {
+              if (index < leftCount) {
+                RelDataTypeField field = fields.get(index);
+                return Pair.<RexNode, String>of(
+                    new RexInputRef(index, field.getType()),
+                    field.getName());
+              } else {
+                return Pair.<RexNode, String>of(
+                    extraLeftExprs.get(index - leftCount), null);
+              }
+            }
+          },
+          true);
+    }
+    if (!extraRightExprs.isEmpty()) {
+      final List<RelDataTypeField> fields =
+          rightRel.getRowType().getFieldList();
+      final int newLeftCount = leftCount + extraLeftExprs.size();
+      rightRel = RelOptUtil.createProject(
+          rightRel,
+          new AbstractList<Pair<RexNode, String>>() {
+            @Override public int size() {
+              return rightCount + extraRightExprs.size();
+            }
+
+            @Override public Pair<RexNode, String> get(int index) {
+              if (index < rightCount) {
+                RelDataTypeField field = fields.get(index);
+                return Pair.<RexNode, String>of(
+                    new RexInputRef(index, field.getType()),
+                    field.getName());
+              } else {
+                return Pair.of(
+                    RexUtil.shift(
+                        extraRightExprs.get(index - rightCount),
+                        -newLeftCount),
+                    null);
+              }
+            }
+          },
+          true);
+    }
+    RelNode join = createJoin(
+        leftRel,
+        rightRel,
+        joinCond,
+        joinType,
+        ImmutableSet.<String>of());
+    if (!extraLeftExprs.isEmpty() || !extraRightExprs.isEmpty()) {
+      Mappings.TargetMapping mapping =
+          Mappings.createShiftMapping(
+              leftCount + extraLeftExprs.size()
+                  + rightCount + extraRightExprs.size(),
+              0, 0, leftCount,
+              leftCount, leftCount + extraLeftExprs.size(), rightCount);
+      return RelOptUtil.project(join, mapping);
+    }
+    return join;
+  }
+
+  private static boolean containsGet(RexNode node) {
+    try {
+      node.accept(
+          new RexVisitorImpl<Void>(true) {
+            @Override public Void visitCall(RexCall call) {
+              if (call.getOperator() == RexBuilder.GET_OPERATOR) {
+                throw Util.FoundOne.NULL;
+              }
+              return super.visitCall(call);
+            }
+          });
+      return false;
+    } catch (Util.FoundOne e) {
+      return true;
+    }
+  }
+
+  /**
+   * Pushes down parts of a join condition. For example, given
+   * "emp JOIN dept ON emp.deptno + 1 = dept.deptno", adds a project above
+   * "emp" that computes the expression
+   * "emp.deptno + 1". The resulting join condition is a simple combination
+   * of AND, equals, and input fields.
+   */
+  private RexNode pushDownJoinConditions(
+      RexNode node,
+      int leftCount,
+      int rightCount,
+      List<RexNode> extraLeftExprs,
+      List<RexNode> extraRightExprs) {
+    switch (node.getKind()) {
+    case AND:
+    case OR:
+    case EQUALS:
+      RexCall call = (RexCall) node;
+      List<RexNode> list = new ArrayList<RexNode>();
+      List<RexNode> operands = Lists.newArrayList(call.getOperands());
+      for (int i = 0; i < operands.size(); i++) {
+        RexNode operand = operands.get(i);
+        final int left2 = leftCount + extraLeftExprs.size();
+        final int right2 = rightCount + extraRightExprs.size();
+        final RexNode e =
+            pushDownJoinConditions(
+                operand,
+                leftCount,
+                rightCount,
+                extraLeftExprs,
+                extraRightExprs);
+        final List<RexNode> remainingOperands = Util.skip(operands, i + 1);
+        final int left3 = leftCount + extraLeftExprs.size();
+        final int right3 = rightCount + extraRightExprs.size();
+        fix(remainingOperands, left2, left3);
+        fix(list, left2, left3);
+        list.add(e);
+      }
+      if (!list.equals(call.getOperands())) {
+        return call.clone(call.getType(), list);
+      }
+      return call;
+    case INPUT_REF:
+    case LITERAL:
+      return node;
+    default:
+      ImmutableBitSet bits = RelOptUtil.InputFinder.bits(node);
+      final int mid = leftCount + extraLeftExprs.size();
+      switch (Side.of(bits, mid)) {
+      case LEFT:
+        fix(extraRightExprs, mid, mid + 1);
+        extraLeftExprs.add(node);
+        return new RexInputRef(mid, node.getType());
+      case RIGHT:
+        final int index2 = mid + rightCount + extraRightExprs.size();
+        extraRightExprs.add(node);
+        return new RexInputRef(index2, node.getType());
+      case BOTH:
+      case EMPTY:
+      default:
+        return node;
+      }
+    }
+  }
+
+  private void fix(List<RexNode> operands, int before, int after) {
+    if (before == after) {
+      return;
+    }
+    for (int i = 0; i < operands.size(); i++) {
+      RexNode node = operands.get(i);
+      operands.set(i, RexUtil.shift(node, before, after - before));
+    }
+  }
+
+  /**
+   * Categorizes whether a bit set contains bits left and right of a
+   * line.
+   */
+  enum Side {
+    LEFT, RIGHT, BOTH, EMPTY;
+
+    static Side of(ImmutableBitSet bitSet, int middle) {
+      final int firstBit = bitSet.nextSetBit(0);
+      if (firstBit < 0) {
+        return EMPTY;
+      }
+      if (firstBit >= middle) {
+        return RIGHT;
+      }
+      if (bitSet.nextSetBit(middle) < 0) {
+        return LEFT;
+      }
+      return BOTH;
+    }
+  }
+
+  /**
+   * Determines whether a subquery is non-correlated. Note that a
+   * non-correlated subquery can contain correlated references, provided those
+   * references do not reference select statements that are parents of the
+   * subquery.
+   *
+   * @param subq the subquery
+   * @param bb   blackboard used while converting the subquery, i.e., the
+   *             blackboard of the parent query of this subquery
+   * @return true if the subquery is non-correlated.
+   */
+  private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
+    Set<String> correlatedVariables = RelOptUtil.getVariablesUsed(subq);
+    for (String correlName : correlatedVariables) {
+      DeferredLookup lookup = mapCorrelToDeferred.get(correlName);
+      String originalRelName = lookup.getOriginalRelName();
+
+      int[] nsIndexes = {-1};
+      final SqlValidatorScope[] ancestorScopes = {null};
+      SqlValidatorNamespace foundNs =
+          lookup.bb.scope.resolve(
+              ImmutableList.of(originalRelName),
+              ancestorScopes,
+              nsIndexes);
+
+      assert foundNs != null;
+      assert nsIndexes.length == 1;
+
+      SqlValidatorScope ancestorScope = ancestorScopes[0];
+
+      // If the correlated reference is in a scope that's "above" the
+      // subquery, then this is a correlated subquery.
+      SqlValidatorScope parentScope = bb.scope;
+      do {
+        if (ancestorScope == parentScope) {
+          return false;
+        }
+        if (parentScope instanceof DelegatingScope) {
+          parentScope = ((DelegatingScope) parentScope).getParent();
+        } else {
+          break;
+        }
+      } while (parentScope != null);
+    }
+    return true;
+  }
+
+  /**
+   * Returns a list of fields to be prefixed to each relational expression.
+   *
+   * @return List of system fields
+   */
+  protected List<RelDataTypeField> getSystemFields() {
+    return Collections.emptyList();
+  }
+
+  private RexNode convertJoinCondition(
+      Blackboard bb,
+      SqlNode condition,
+      JoinConditionType conditionType,
+      RelNode leftRel,
+      RelNode rightRel) {
+    if (condition == null) {
+      return rexBuilder.makeLiteral(true);
+    }
+    bb.setRoot(ImmutableList.of(leftRel, rightRel));
+    replaceSubqueries(bb, condition, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
+    switch (conditionType) {
+    case ON:
+      bb.setRoot(ImmutableList.of(leftRel, rightRel));
+      return bb.convertExpression(condition);
+    case USING:
+      SqlNodeList list = (SqlNodeList) condition;
+      List<String> nameList = new ArrayList<String>();
+      for (SqlNode columnName : list) {
+        final SqlIdentifier id = (SqlIdentifier) columnName;
+        String name = id.getSimple();
+        nameList.add(name);
+      }
+      return convertUsing(leftRel, rightRel, nameList);
+    default:
+      throw Util.unexpected(conditionType);
+    }
+  }
+
+  /**
+   * Returns an expression for matching columns of a USING clause or inferred
+   * from NATURAL JOIN. "a JOIN b USING (x, y)" becomes "a.x = b.x AND a.y =
+   * b.y". Returns null if the column list is empty.
+   *
+   * @param leftRel  Left input to the join
+   * @param rightRel Right input to the join
+   * @param nameList List of column names to join on
+   * @return Expression to match columns from name list, or true if name list
+   * is empty
+   */
+  private RexNode convertUsing(
+      RelNode leftRel,
+      RelNode rightRel,
+      List<String> nameList) {
+    final List<RexNode> list = Lists.newArrayList();
+    for (String name : nameList) {
+      final RelDataType leftRowType = leftRel.getRowType();
+      RelDataTypeField leftField = catalogReader.field(leftRowType, name);
+      RexNode left =
+          rexBuilder.makeInputRef(
+              leftField.getType(),
+              leftFiel

<TRUNCATED>

[04/10] incubator-kylin git commit: KYLIN-780 Upgrade query module, all query test pass

Posted by li...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinMetaImpl.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinMetaImpl.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinMetaImpl.java
deleted file mode 100644
index 3c14c9b..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinMetaImpl.java
+++ /dev/null
@@ -1,827 +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.kylin.jdbc;
-
-import java.lang.reflect.Field;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Types;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.regex.Pattern;
-
-import net.hydromatic.avatica.AvaticaPrepareResult;
-import net.hydromatic.avatica.AvaticaResultSet;
-import net.hydromatic.avatica.AvaticaStatement;
-import net.hydromatic.avatica.ColumnMetaData;
-import net.hydromatic.avatica.ColumnMetaData.Rep;
-import net.hydromatic.avatica.Cursor;
-import net.hydromatic.avatica.Meta;
-import net.hydromatic.linq4j.Enumerator;
-import net.hydromatic.optiq.runtime.EnumeratorCursor;
-
-import org.apache.kylin.jdbc.stub.DataSet;
-import org.apache.kylin.jdbc.stub.KylinColumnMetaData;
-import org.apache.kylin.jdbc.stub.RemoteClient;
-import org.eigenbase.sql.SqlJdbcFunctionCall;
-import org.eigenbase.sql.parser.SqlParser;
-import org.eigenbase.util.Util;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import org.apache.kylin.jdbc.util.SQLTypeMap;
-
-/**
- * Implementation of avatica interface
- *
- * @author xduo
- */
-public class KylinMetaImpl implements Meta {
-
-    private static final Logger logger = LoggerFactory.getLogger(KylinMetaImpl.class);
-
-    private final KylinConnectionImpl conn;
-
-    private final KylinJdbc41Factory factory;
-
-    /**
-     * @param conn
-     */
-    public KylinMetaImpl(KylinConnectionImpl conn, KylinJdbc41Factory factory) {
-        super();
-        this.conn = conn;
-        this.factory = factory;
-    }
-
-    private ResultSet mockEmptyResultSet() {
-        AvaticaResultSet resultSet = null;
-        try {
-            List<ColumnMetaData> columnMetas = new ArrayList<ColumnMetaData>();
-            List<Object[]> data = new ArrayList<Object[]>();
-            resultSet = this.conn.getFactory().newResultSet(this.conn.createStatement(), new KylinPrepare.PrepareResult(null, null, new KylinEnumerator<Object[]>(data), ColumnMetaData.struct(columnMetas)), this.conn.getTimeZone());
-            KylinConnectionImpl.TROJAN.execute(resultSet);
-        } catch (SQLException e) {
-            logger.error(e.getLocalizedMessage(), e);
-        }
-
-        return resultSet;
-    }
-
-    public String getSqlKeywords() {
-        return SqlParser.create("").getMetadata().getJdbcKeywords();
-    }
-
-    public String getNumericFunctions() {
-        return SqlJdbcFunctionCall.getNumericFunctions();
-    }
-
-    public String getStringFunctions() {
-        return SqlJdbcFunctionCall.getStringFunctions();
-    }
-
-    public String getSystemFunctions() {
-        return SqlJdbcFunctionCall.getSystemFunctions();
-    }
-
-    public String getTimeDateFunctions() {
-        return SqlJdbcFunctionCall.getTimeDateFunctions();
-    }
-
-    public ResultSet getTables(String catalog, Pat schemaPattern, Pat tableNamePattern, List<String> typeList) {
-        logger.debug("Get tables with conn " + conn);
-        MetaProject metaProject = conn.getMetaProject();
-
-        if (null != metaProject) {
-            final DataSet<MetaTable> tables = metaProject.getMetaTables(catalog, schemaPattern, tableNamePattern);
-            final NamedFieldGetter<MetaTable> tableGetter = new NamedFieldGetter<MetaTable>(MetaTable.class, tables.getMeta(), "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "TABLE_TYPE", "REMARKS", "TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME", "SELF_REFERENCING_COL_NAME", "REF_GENERATION");
-
-            AvaticaResultSet resultSet = null;
-            try {
-                resultSet = this.conn.getFactory().newResultSet(this.conn.createStatement(), new KylinPrepare.PrepareResult(null, null, null, tableGetter.structType) {
-                    @Override
-                    public Cursor createCursor() {
-                        return tableGetter.cursor(tables.getEnumerator());
-                    }
-                }, this.conn.getTimeZone());
-                KylinConnectionImpl.TROJAN.execute(resultSet);
-            } catch (SQLException e) {
-                logger.error(e.getLocalizedMessage(), e);
-            }
-
-            return resultSet;
-        } else {
-            return mockEmptyResultSet();
-        }
-    }
-
-    public ResultSet getColumns(String catalog, Pat schemaPattern, Pat tableNamePattern, Pat columnNamePattern) {
-        logger.debug("Get columns with conn " + conn);
-        MetaProject metaProject = conn.getMetaProject();
-
-        if (null != metaProject) {
-            final DataSet<MetaColumn> columns = metaProject.getMetaColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern);
-            final NamedFieldGetter<MetaColumn> columnGetter = new NamedFieldGetter<MetaColumn>(MetaColumn.class, columns.getMeta(), "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME", "COLUMN_SIZE", "BUFFER_LENGTH", "DECIMAL_DIGITS", "NUM_PREC_RADIX", "NULLABLE", "REMARKS", "COLUMN_DEF", "SQL_DATA_TYPE", "SQL_DATETIME_SUB", "CHAR_OCTET_LENGTH", "ORDINAL_POSITION", "IS_NULLABLE", "SCOPE_CATALOG", "SCOPE_TABLE", "SOURCE_DATA_TYPE", "IS_AUTOINCREMENT", "IS_GENERATEDCOLUMN");
-
-            AvaticaResultSet resultSet = null;
-            try {
-                resultSet = this.conn.getFactory().newResultSet(this.conn.createStatement(), new KylinPrepare.PrepareResult(null, null, null, columnGetter.structType) {
-                    @Override
-                    public Cursor createCursor() {
-                        return columnGetter.cursor(columns.getEnumerator());
-                    }
-                }, this.conn.getTimeZone());
-
-                KylinConnectionImpl.TROJAN.execute(resultSet);
-            } catch (SQLException e) {
-                logger.error(e.getLocalizedMessage(), e);
-            }
-
-            return resultSet;
-        } else {
-            return mockEmptyResultSet();
-        }
-    }
-
-    public ResultSet getSchemas(String catalog, Pat schemaPattern) {
-        logger.debug("Get schemas with conn " + conn);
-        MetaProject metaProject = conn.getMetaProject();
-
-        if (null != metaProject) {
-            final DataSet<MetaSchema> schemas = metaProject.getMetaSchemas(catalog, schemaPattern);
-            final NamedFieldGetter<MetaSchema> schemaGetter = new NamedFieldGetter<MetaSchema>(MetaSchema.class, schemas.getMeta(), "TABLE_SCHEM", "TABLE_CATALOG");
-
-            AvaticaResultSet resultSet = null;
-            try {
-                resultSet = this.conn.getFactory().newResultSet(this.conn.createStatement(), new KylinPrepare.PrepareResult(null, null, null, schemaGetter.structType) {
-                    @Override
-                    public Cursor createCursor() {
-                        return schemaGetter.cursor(schemas.getEnumerator());
-                    }
-                }, this.conn.getTimeZone());
-
-                KylinConnectionImpl.TROJAN.execute(resultSet);
-            } catch (SQLException e) {
-                logger.error(e.getLocalizedMessage(), e);
-            }
-
-            return resultSet;
-        } else {
-            return mockEmptyResultSet();
-        }
-    }
-
-    public ResultSet getCatalogs() {
-        MetaProject metaProject = conn.getMetaProject();
-
-        if (null != metaProject) {
-            final DataSet<MetaCatalog> catalogs = metaProject.getMetaCatalogs();
-            final NamedFieldGetter<MetaCatalog> catalogGetter = new NamedFieldGetter<MetaCatalog>(MetaCatalog.class, catalogs.getMeta(), "TABLE_CATALOG");
-
-            AvaticaResultSet resultSet = null;
-            try {
-                resultSet = this.conn.getFactory().newResultSet(this.conn.createStatement(), new KylinPrepare.PrepareResult(null, null, null, catalogGetter.structType) {
-                    @Override
-                    public Cursor createCursor() {
-                        return catalogGetter.cursor(catalogs.getEnumerator());
-                    }
-                }, this.conn.getTimeZone());
-
-                KylinConnectionImpl.TROJAN.execute(resultSet);
-            } catch (SQLException e) {
-                logger.error(e.getLocalizedMessage(), e);
-            }
-
-            return resultSet;
-        } else {
-            return mockEmptyResultSet();
-        }
-    }
-
-    public ResultSet getTableTypes() {
-        List<ColumnMetaData> tableTypeMeta = new ArrayList<ColumnMetaData>();
-        tableTypeMeta.add(ColumnMetaData.dummy(ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), false));
-        List<Object[]> data = new ArrayList<Object[]>();
-        Object[] row = new Object[1];
-        row[0] = "TABLE";
-        data.add(row);
-
-        AvaticaResultSet resultSet = null;
-        try {
-            resultSet = this.conn.getFactory().newResultSet(this.conn.createStatement(), new KylinPrepare.PrepareResult(null, null, new KylinEnumerator<Object[]>(data), ColumnMetaData.struct(tableTypeMeta)), this.conn.getTimeZone());
-            KylinConnectionImpl.TROJAN.execute(resultSet);
-        } catch (SQLException e) {
-            logger.error(e.getLocalizedMessage(), e);
-        }
-
-        return resultSet;
-    }
-
-    public ResultSet getProcedures(String catalog, Pat schemaPattern, Pat procedureNamePattern) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getProcedureColumns(String catalog, Pat schemaPattern, Pat procedureNamePattern, Pat columnNamePattern) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getColumnPrivileges(String catalog, String schema, String table, Pat columnNamePattern) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getTablePrivileges(String catalog, Pat schemaPattern, Pat tableNamePattern) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getVersionColumns(String catalog, String schema, String table) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getPrimaryKeys(String catalog, String schema, String table) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getImportedKeys(String catalog, String schema, String table) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getExportedKeys(String catalog, String schema, String table) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, String foreignCatalog, String foreignSchema, String foreignTable) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getTypeInfo() {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getUDTs(String catalog, Pat schemaPattern, Pat typeNamePattern, int[] types) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getSuperTypes(String catalog, Pat schemaPattern, Pat typeNamePattern) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getSuperTables(String catalog, Pat schemaPattern, Pat tableNamePattern) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getAttributes(String catalog, Pat schemaPattern, Pat typeNamePattern, Pat attributeNamePattern) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getClientInfoProperties() {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getFunctions(String catalog, Pat schemaPattern, Pat functionNamePattern) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getFunctionColumns(String catalog, Pat schemaPattern, Pat functionNamePattern, Pat columnNamePattern) {
-        return mockEmptyResultSet();
-    }
-
-    public ResultSet getPseudoColumns(String catalog, Pat schemaPattern, Pat tableNamePattern, Pat columnNamePattern) {
-        return mockEmptyResultSet();
-    }
-
-    public Cursor createCursor(AvaticaResultSet resultSet) {
-
-        if (!(resultSet instanceof KylinResultSet))
-            throw new IllegalStateException("resultSet is not KylinResultSet");
-
-        KylinPrepare.PrepareResult result = ((KylinResultSet) resultSet).getPrepareResult();
-
-        return result.createCursor();
-    }
-
-    /* 
-     * Client could request metadata after prepare
-     * 
-     * (non-Javadoc)
-     * @see net.hydromatic.avatica.Meta#prepare(net.hydromatic.avatica.AvaticaStatement, java.lang.String)
-     */
-    public AvaticaPrepareResult prepare(AvaticaStatement statement, String sql) {
-        RemoteClient client = factory.newRemoteClient(conn);
-        DataSet<Object[]> result = null;
-
-        try {
-            result = (DataSet<Object[]>) client.query(statement, sql);
-        } catch (Exception e) {
-            logger.error(e.getLocalizedMessage(), e);
-            throw new RuntimeException("Failed to query kylin server with exception " + e.getLocalizedMessage());
-        }
-
-        return new KylinPrepare.PrepareResult(sql, null, (Enumerator<Object[]>) result.getEnumerator(), ColumnMetaData.struct(result.getMeta()));
-    }
-
-    /**
-     * Tree node used by project tree-like structure
-     *
-     * @author xduo
-     */
-    interface Node {
-        /**
-         * Get the node name
-         *
-         * @return
-         */
-        public String getName();
-
-        /**
-         * Get direct children of the node.
-         *
-         * @return
-         */
-        public List<? extends Node> getChildren();
-
-        /**
-         * Search the subtree of the node with patterns. One pattern, one level.
-         *
-         * @param patterns
-         * @return
-         */
-        public List<? extends Node> searchByPatterns(Pat... patterns);
-    }
-
-    /**
-     * Abstract of the tree-like structure
-     *
-     * @author xduo
-     */
-    public static abstract class AbstractNode implements Node {
-
-        public List<? extends Node> searchByPatterns(Pat... patterns) {
-            if (patterns.length == 1) {
-                return findChildren(patterns[0]);
-            } else {
-                List<Node> children = new ArrayList<Node>();
-
-                for (Node child : this.findChildren(patterns[0])) {
-                    children.addAll(child.searchByPatterns(Arrays.copyOfRange(patterns, 1, patterns.length)));
-                }
-
-                return children;
-            }
-        }
-
-        private List<? extends Node> findChildren(Pat pattern) {
-            if (null == pattern.s || pattern.s.equals("%")) {
-                return this.getChildren();
-            }
-
-            List<Node> list = new ArrayList<Node>();
-
-            for (Node c : this.getChildren()) {
-                if (likeToRegex(pattern).matcher(c.getName()).matches()) {
-                    list.add(c);
-                }
-            }
-
-            return list;
-        }
-
-        ;
-
-        /**
-         * Converts a LIKE-style pattern (where '%' represents a wild-card,
-         * escaped using '\') to a Java regex.
-         */
-        private Pattern likeToRegex(Pat pattern) {
-            StringBuilder buf = new StringBuilder("^");
-            char[] charArray = pattern.s.toCharArray();
-            int slash = -2;
-            for (int i = 0; i < charArray.length; i++) {
-                char c = charArray[i];
-                if (slash == i - 1) {
-                    buf.append('[').append(c).append(']');
-                } else {
-                    switch (c) {
-                    case '\\':
-                        slash = i;
-                        break;
-                    case '%':
-                        buf.append(".*");
-                        break;
-                    case '[':
-                        buf.append("\\[");
-                        break;
-                    case ']':
-                        buf.append("\\]");
-                        break;
-                    default:
-                        buf.append('[').append(c).append(']');
-                    }
-                }
-            }
-            buf.append("$");
-
-            return Pattern.compile(buf.toString());
-        }
-    }
-
-    public static class MetaProject extends AbstractNode {
-        public final String project;
-        public final List<MetaCatalog> catalogs;
-
-        public MetaProject(String project, List<MetaCatalog> catalogs) {
-            super();
-            this.project = project;
-            this.catalogs = catalogs;
-        }
-
-        public DataSet<MetaCatalog> getMetaCatalogs() {
-            return new DataSet<MetaCatalog>(MetaCatalog.meta, new KylinEnumerator<MetaCatalog>(catalogs));
-        }
-
-        /**
-         * facade method to search schemas in current project.
-         *
-         * @param catalog
-         * @param schemaPattern
-         * @return
-         */
-        @SuppressWarnings("unchecked")
-        public DataSet<MetaSchema> getMetaSchemas(String catalog, Pat schemaPattern) {
-            List<? extends Node> metaSchemas = this.searchByPatterns(Pat.of(catalog), schemaPattern);
-
-            return new DataSet<MetaSchema>(MetaSchema.meta, new KylinEnumerator<MetaSchema>((Collection<MetaSchema>) metaSchemas));
-        }
-
-        /**
-         * facade method to search tables in current project
-         *
-         * @param catalog
-         * @param schemaPattern
-         * @param tableNamePattern
-         * @return
-         */
-        @SuppressWarnings("unchecked")
-        public DataSet<MetaTable> getMetaTables(String catalog, Pat schemaPattern, Pat tableNamePattern) {
-            logger.debug("getMetaTables with catalog:" + catalog + ", schema:" + schemaPattern.s + ", table:" + tableNamePattern.s);
-            List<? extends Node> tables = this.searchByPatterns(Pat.of(catalog), schemaPattern, tableNamePattern);
-
-            return new DataSet<MetaTable>(MetaTable.meta, new KylinEnumerator<MetaTable>((Collection<MetaTable>) tables));
-        }
-
-        /**
-         * facade method to search columns in current project
-         *
-         * @param catalog
-         * @param schemaPattern
-         * @param tableNamePattern
-         * @param columnNamePattern
-         * @return
-         */
-        @SuppressWarnings("unchecked")
-        public DataSet<MetaColumn> getMetaColumns(String catalog, Pat schemaPattern, Pat tableNamePattern, Pat columnNamePattern) {
-            logger.debug("getMetaColumns with catalog:" + catalog + ", schema:" + schemaPattern.s + ", table:" + tableNamePattern.s + ", column:" + columnNamePattern.s);
-            List<? extends Node> columns = this.searchByPatterns(Pat.of(catalog), schemaPattern, tableNamePattern, columnNamePattern);
-
-            return new DataSet<MetaColumn>(MetaColumn.meta, new KylinEnumerator<MetaColumn>((Collection<MetaColumn>) columns));
-        }
-
-        @Override
-        public String getName() {
-            return project;
-        }
-
-        @Override
-        public List<? extends Node> getChildren() {
-            return this.catalogs;
-        }
-    }
-
-    /**
-     * Metadata describing a catalog.
-     */
-    public static class MetaCatalog extends AbstractNode {
-        public static final List<ColumnMetaData> meta = new ArrayList<ColumnMetaData>();
-        public final String tableCatalog;
-        public final List<MetaSchema> schemas;
-
-        static {
-            meta.add(KylinColumnMetaData.dummy(0, "TABLE_CAT", "TABLE_CAT", ColumnMetaData.scalar(Types.VARCHAR, "varchar", Rep.STRING), true));
-        }
-
-        public MetaCatalog(String tableCatalog, List<MetaSchema> schemas) {
-            this.tableCatalog = tableCatalog;
-            this.schemas = schemas;
-        }
-
-        public String getName() {
-            return tableCatalog;
-        }
-
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + ((tableCatalog == null) ? 0 : tableCatalog.hashCode());
-            return result;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (this == obj)
-                return true;
-            if (obj == null)
-                return false;
-            if (getClass() != obj.getClass())
-                return false;
-            MetaCatalog other = (MetaCatalog) obj;
-            if (tableCatalog == null) {
-                if (other.tableCatalog != null)
-                    return false;
-            } else if (!tableCatalog.equals(other.tableCatalog))
-                return false;
-            return true;
-        }
-
-        @Override
-        public List<? extends Node> getChildren() {
-            return schemas;
-        }
-
-    }
-
-    /**
-     * Metadata describing a schema.
-     */
-    public static class MetaSchema extends AbstractNode {
-        public static final List<ColumnMetaData> meta = new ArrayList<ColumnMetaData>();
-        public final String tableCatalog;
-        public final String tableSchem;
-        public final List<MetaTable> tables;
-
-        static {
-            for (ColumnMetaData cmd : SQLTypeMap.schemaMetaTypeMapping.values()) {
-                meta.add(cmd);
-            }
-        }
-
-        public MetaSchema(String tableCatalog, String tableSchem, List<MetaTable> tables) {
-            this.tableCatalog = tableCatalog;
-            this.tableSchem = tableSchem;
-            this.tables = tables;
-        }
-
-        public String getName() {
-            return tableSchem;
-        }
-
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + ((tableCatalog == null) ? 0 : tableCatalog.hashCode());
-            result = prime * result + ((tableSchem == null) ? 0 : tableSchem.hashCode());
-            return result;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (this == obj)
-                return true;
-            if (obj == null)
-                return false;
-            if (getClass() != obj.getClass())
-                return false;
-            MetaSchema other = (MetaSchema) obj;
-            if (tableCatalog == null) {
-                if (other.tableCatalog != null)
-                    return false;
-            } else if (!tableCatalog.equals(other.tableCatalog))
-                return false;
-            if (tableSchem == null) {
-                if (other.tableSchem != null)
-                    return false;
-            } else if (!tableSchem.equals(other.tableSchem))
-                return false;
-            return true;
-        }
-
-        @Override
-        public List<MetaTable> getChildren() {
-            return this.tables;
-        }
-    }
-
-    /**
-     * Metadata describing a table type.
-     */
-    public static class MetaTableType {
-        public final String tableType;
-
-        public MetaTableType(String tableType) {
-            this.tableType = tableType;
-        }
-    }
-
-    /**
-     * Metadata describing a table.
-     */
-    public static class MetaTable extends AbstractNode {
-        public static final List<ColumnMetaData> meta = new ArrayList<ColumnMetaData>();
-        public final String tableCat;
-        public final String tableSchem;
-        public final String tableName;
-        public final String tableType;
-        public final String remarks;
-        public final String typeCat;
-        public final String typeSchem;
-        public final String typeName;
-        public final String selfReferencingColName;
-        public final String refGeneration;
-        public final List<MetaColumn> columns;
-
-        static {
-            for (ColumnMetaData cmd : SQLTypeMap.tableMetaTypeMapping.values()) {
-                meta.add(cmd);
-            }
-        }
-
-        public MetaTable(String tableCat, String tableSchem, String tableName, String tableType, String remarks, String typeCat, String typeSchem, String typeName, String selfReferencingColName, String refGeneration, List<MetaColumn> columns) {
-            this.tableCat = tableCat;
-            this.tableSchem = tableSchem;
-            this.tableName = tableName;
-            this.tableType = tableType;
-            this.remarks = remarks;
-            this.typeCat = typeCat;
-            this.typeSchem = typeSchem;
-            this.typeName = typeName;
-            this.selfReferencingColName = selfReferencingColName;
-            this.refGeneration = refGeneration;
-            this.columns = columns;
-        }
-
-        public String getName() {
-            return tableName;
-        }
-
-        @Override
-        public List<? extends Node> getChildren() {
-            return this.columns;
-        }
-    }
-
-    /**
-     * Metadata describing a column.
-     */
-    public static class MetaColumn implements Node {
-        public static final List<ColumnMetaData> meta = new ArrayList<ColumnMetaData>();
-        public final String tableCat;
-        public final String tableSchem;
-        public final String tableName;
-        public final String columnName;
-        public final int dataType;
-        public final String typeName;
-        public final int columnSize;
-        public final int bufferLength;
-        public final int decimalDigits;
-        public final int numPrecRadix;
-        public final int nullable;
-        public final String remarks;
-        public final String columnDef;
-        public final int sqlDataType;
-        public final int sqlDatetimeSub;
-        public final int charOctetLength;
-        public final int ordinalPosition;
-        public final String isNullable;
-        public final String scopeCatalog;
-        public final String scopeTable;
-        public final int sourceDataType;
-        public final String isAutoincrement;
-        public final String isGeneratedcolumn;
-
-        static {
-            for (ColumnMetaData cmd : SQLTypeMap.columnMetaTypeMapping.values()) {
-                meta.add(cmd);
-            }
-        }
-
-        public MetaColumn(String tableCat, String tableSchem, String tableName, String columnName, int dataType, String typeName, int columnSize, int bufferLength, int decimalDigits, int numPrecRadix, int nullable, String remarks, String columnDef, int sqlDataType, int sqlDatetimeSub, int charOctetLength, int ordinalPosition, String isNullable, String scopeCatalog, String scopeTable, int sourceDataType, String isAutoincrement, String isGeneratedcolumn) {
-            super();
-            this.tableCat = tableCat;
-            this.tableSchem = tableSchem;
-            this.tableName = tableName;
-            this.columnName = columnName;
-            this.dataType = dataType;
-            this.typeName = typeName;
-            this.columnSize = columnSize;
-            this.bufferLength = bufferLength;
-            this.decimalDigits = decimalDigits;
-            this.numPrecRadix = numPrecRadix;
-            this.nullable = nullable;
-            this.remarks = remarks;
-            this.columnDef = columnDef;
-            this.sqlDataType = sqlDataType;
-            this.sqlDatetimeSub = sqlDatetimeSub;
-            this.charOctetLength = charOctetLength;
-            this.ordinalPosition = ordinalPosition;
-            this.isNullable = isNullable;
-            this.scopeCatalog = scopeCatalog;
-            this.scopeTable = scopeTable;
-            this.sourceDataType = sourceDataType;
-            this.isAutoincrement = isAutoincrement;
-            this.isGeneratedcolumn = isGeneratedcolumn;
-        }
-
-        public String getName() {
-            return columnName;
-        }
-
-        @Override
-        public List<? extends Node> getChildren() {
-            return Collections.emptyList();
-        }
-
-        @Override
-        public List<? extends Node> searchByPatterns(Pat... patterns) {
-            return Collections.emptyList();
-        }
-    }
-
-    /**
-     * Accesses fields by name.
-     */
-    private static class NamedFieldGetter<T> {
-        private final List<Field> fields = new ArrayList<Field>();
-        private final ColumnMetaData.StructType structType;
-
-        public NamedFieldGetter(Class<T> clazz, List<ColumnMetaData> columns, String... names) {
-            init(clazz, names, fields);
-            structType = ColumnMetaData.struct(columns);
-        }
-
-        private void init(Class<T> clazz, String[] names, List<Field> fields) {
-            for (String name : names) {
-                final String fieldName = Util.toCamelCase(name);
-                final Field field;
-                try {
-                    field = clazz.getField(fieldName);
-                } catch (NoSuchFieldException e) {
-                    throw new RuntimeException(e);
-                }
-                fields.add(field);
-            }
-        }
-
-        Object get(Object o, int columnIndex) {
-            try {
-                return fields.get(columnIndex).get(o);
-            } catch (IllegalArgumentException e) {
-                throw new RuntimeException(e);
-            } catch (IllegalAccessException e) {
-                throw new RuntimeException(e);
-            }
-        }
-
-        public Cursor cursor(Enumerator<T> enumerator) {
-            // noinspection unchecked
-            return new EnumeratorCursor<T>(enumerator) {
-                protected Getter createGetter(final int ordinal) {
-                    return new Getter() {
-                        public Object getObject() {
-                            return get(current(), ordinal);
-                        }
-
-                        public boolean wasNull() {
-                            return getObject() == null;
-                        }
-                    };
-                }
-            };
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepare.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepare.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepare.java
deleted file mode 100644
index 0e4fb35..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepare.java
+++ /dev/null
@@ -1,103 +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.kylin.jdbc;
-
-import java.util.List;
-import java.util.Map;
-
-import net.hydromatic.avatica.AvaticaParameter;
-import net.hydromatic.avatica.AvaticaPrepareResult;
-import net.hydromatic.avatica.ColumnMetaData;
-import net.hydromatic.avatica.Cursor;
-import net.hydromatic.linq4j.Enumerator;
-import net.hydromatic.optiq.runtime.EnumeratorCursor;
-
-/**
- * Interface of kylin prepare statement implementation
- * 
- * @author xduo
- * 
- */
-public interface KylinPrepare {
-
-    PrepareResult prepare(String sql);
-
-    /**
-     * The result of preparing a query. It gives the Avatica driver framework
-     * the information it needs to create a prepared statement, or to execute a
-     * statement directly, without an explicit prepare step.
-     */
-    public static class PrepareResult implements AvaticaPrepareResult {
-        public final String sql; // for debug
-        public final ColumnMetaData.StructType structType;
-        public final Enumerator<Object[]> enumerator;
-        public final List<AvaticaParameter> parameterList;
-
-        public PrepareResult(String sql, List<AvaticaParameter> parameterList, Enumerator<Object[]> enumerator, ColumnMetaData.StructType structType) {
-            super();
-            this.sql = sql;
-            this.parameterList = parameterList;
-            this.enumerator = enumerator;
-            this.structType = structType;
-        }
-
-        public Cursor createCursor() {
-            return new EnumeratorCursor<Object[]>(enumerator) {
-                @Override
-                protected Getter createGetter(int ordinal) {
-                    return new ArrayEnumeratorGetter(ordinal);
-                }
-
-                /**
-                 * Row field accessor via index
-                 */
-                class ArrayEnumeratorGetter extends AbstractGetter {
-                    protected final int field;
-
-                    public ArrayEnumeratorGetter(int field) {
-                        this.field = field;
-                    }
-
-                    public Object getObject() {
-                        Object o = current()[field];
-                        wasNull[0] = o == null;
-                        return o;
-                    }
-                }
-            };
-        }
-
-        public List<ColumnMetaData> getColumnList() {
-            return structType.columns;
-        }
-
-        public List<AvaticaParameter> getParameterList() {
-            return parameterList;
-        }
-
-        public Map<String, Object> getInternalParameters() {
-            return null;
-        }
-
-        public String getSql() {
-            return sql;
-        }
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepareImpl.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepareImpl.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepareImpl.java
deleted file mode 100644
index 5aeeb42..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepareImpl.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.kylin.jdbc;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import net.hydromatic.avatica.AvaticaParameter;
-import net.hydromatic.avatica.ColumnMetaData;
-
-/**
- * @author xduo
- * 
- */
-public class KylinPrepareImpl implements KylinPrepare {
-
-    @Override
-    public PrepareResult prepare(String sql) {
-        List<AvaticaParameter> aps = new ArrayList<AvaticaParameter>();
-
-        int startIndex = 0;
-        while (sql.indexOf("?", startIndex) >= 0) {
-            AvaticaParameter ap = new AvaticaParameter(false, 0, 0, 0, null, null, null);
-            aps.add(ap);
-            startIndex = sql.indexOf("?", startIndex) + 1;
-        }
-
-        return new KylinPrepare.PrepareResult(sql, aps, null, ColumnMetaData.struct(new ArrayList<ColumnMetaData>()));
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepareStatementImpl.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepareStatementImpl.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepareStatementImpl.java
deleted file mode 100644
index a2818d8..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPrepareStatementImpl.java
+++ /dev/null
@@ -1,170 +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.kylin.jdbc;
-
-import java.io.InputStream;
-import java.io.Reader;
-import java.sql.NClob;
-import java.sql.ResultSet;
-import java.sql.RowId;
-import java.sql.SQLException;
-import java.sql.SQLXML;
-import java.util.Collections;
-import java.util.List;
-
-import net.hydromatic.avatica.AvaticaConnection;
-import net.hydromatic.avatica.AvaticaPrepareResult;
-import net.hydromatic.avatica.AvaticaPreparedStatement;
-import net.hydromatic.avatica.AvaticaResultSet;
-
-/**
- * Kylin prepare statement. <br>
- * Supported operations:
- * <ul>
- * <li>setString</li>
- * <li>setInt</li>
- * <li>setShort</li>
- * <li>setLong</li>
- * <li>setFloat</li>
- * <li>setDouble</li>
- * <li>setBoolean</li>
- * <li>setByte</li>
- * <li>setDate</li>
- * <li>setTime</li>
- * <li>setTimestamp</li>
- * </ul>
- * 
- * @author xduo
- * 
- */
-public abstract class KylinPrepareStatementImpl extends AvaticaPreparedStatement {
-
-    /**
-     * Before real query,
-     */
-    protected AvaticaPrepareResult prequeryResult;
-
-    protected KylinPrepareStatementImpl(AvaticaConnection connection, AvaticaPrepareResult prepareResult, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
-        super(connection, prepareResult, resultSetType, resultSetConcurrency, resultSetHoldability);
-
-        this.prequeryResult = prepareResult;
-    }
-
-    @Override
-    public ResultSet executeQuery() throws SQLException {
-        AvaticaPrepareResult queriedResult = ((KylinConnectionImpl) this.connection).getMeta().prepare(this, this.prequeryResult.getSql());
-
-        return executeQueryInternal(queriedResult);
-    }
-
-    @Override
-    protected void close_() {
-        if (!closed) {
-            closed = true;
-            final KylinConnectionImpl connection_ = (KylinConnectionImpl) connection;
-            connection_.statements.remove(this);
-            if (openResultSet != null) {
-                AvaticaResultSet c = openResultSet;
-                openResultSet = null;
-                c.close();
-            }
-            // If onStatementClose throws, this method will throw an
-            // exception (later
-            // converted to SQLException), but this statement still gets
-            // closed.
-            connection_.getDriver().handler.onStatementClose(this);
-        }
-    }
-
-    public List<Object> getParameterValues() {
-        return (List<Object>) Collections.unmodifiableList(super.getParameterValues());
-    }
-
-    public void setRowId(int parameterIndex, RowId x) throws SQLException {
-        getParameter(parameterIndex).setRowId(x);
-    }
-
-    public void setNString(int parameterIndex, String value) throws SQLException {
-        getParameter(parameterIndex).setNString(value);
-    }
-
-    public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException {
-        getParameter(parameterIndex).setNCharacterStream(value, length);
-    }
-
-    public void setNClob(int parameterIndex, NClob value) throws SQLException {
-        getParameter(parameterIndex).setNClob(value);
-    }
-
-    public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
-        getParameter(parameterIndex).setClob(reader, length);
-    }
-
-    public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
-        getParameter(parameterIndex).setBlob(inputStream, length);
-    }
-
-    public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
-        getParameter(parameterIndex).setNClob(reader, length);
-    }
-
-    public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
-        getParameter(parameterIndex).setSQLXML(xmlObject);
-    }
-
-    public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
-        getParameter(parameterIndex).setAsciiStream(x, length);
-    }
-
-    public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
-        getParameter(parameterIndex).setBinaryStream(x, length);
-    }
-
-    public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
-        getParameter(parameterIndex).setCharacterStream(reader, length);
-    }
-
-    public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
-        getParameter(parameterIndex).setAsciiStream(x);
-    }
-
-    public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
-        getParameter(parameterIndex).setBinaryStream(x);
-    }
-
-    public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
-        getParameter(parameterIndex).setCharacterStream(reader);
-    }
-
-    public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException {
-        getParameter(parameterIndex).setNCharacterStream(value);
-    }
-
-    public void setClob(int parameterIndex, Reader reader) throws SQLException {
-        getParameter(parameterIndex).setClob(reader);
-    }
-
-    public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
-        getParameter(parameterIndex).setBlob(inputStream);
-    }
-
-    public void setNClob(int parameterIndex, Reader reader) throws SQLException {
-        getParameter(parameterIndex).setNClob(reader);
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPreparedStatement.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPreparedStatement.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPreparedStatement.java
new file mode 100644
index 0000000..21cb162
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinPreparedStatement.java
@@ -0,0 +1,100 @@
+package org.apache.kylin.jdbc;
+
+import java.io.InputStream;
+import java.io.Reader;
+import java.sql.NClob;
+import java.sql.RowId;
+import java.sql.SQLException;
+import java.sql.SQLXML;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.calcite.avatica.AvaticaConnection;
+import org.apache.calcite.avatica.AvaticaPreparedStatement;
+import org.apache.calcite.avatica.Meta.Signature;
+import org.apache.calcite.avatica.Meta.StatementHandle;
+
+public class KylinPreparedStatement extends AvaticaPreparedStatement {
+
+    protected KylinPreparedStatement(AvaticaConnection connection, StatementHandle h, Signature signature, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
+        super(connection, h, signature, resultSetType, resultSetConcurrency, resultSetHoldability);
+    }
+    
+    protected List<Object> getParameterValues() {
+        return Arrays.asList(slots);
+    }
+    
+    // ============================================================================
+
+    public void setRowId(int parameterIndex, RowId x) throws SQLException {
+        getParameter(parameterIndex).setRowId(slots, parameterIndex, x);
+    }
+
+    public void setNString(int parameterIndex, String value) throws SQLException {
+        getParameter(parameterIndex).setNString(slots, parameterIndex, value);
+    }
+
+    public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException {
+        getParameter(parameterIndex).setNCharacterStream(slots, parameterIndex, value, length);
+    }
+
+    public void setNClob(int parameterIndex, NClob value) throws SQLException {
+        getParameter(parameterIndex).setNClob(slots, parameterIndex, value);
+    }
+
+    public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
+        getParameter(parameterIndex).setClob(slots, parameterIndex, reader, length);
+    }
+
+    public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
+        getParameter(parameterIndex).setBlob(slots, parameterIndex, inputStream, length);
+    }
+
+    public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
+        getParameter(parameterIndex).setNClob(slots, parameterIndex, reader, length);
+    }
+
+    public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
+        getParameter(parameterIndex).setSQLXML(slots, parameterIndex, xmlObject);
+    }
+
+    public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
+        getParameter(parameterIndex).setAsciiStream(slots, parameterIndex, x, length);
+    }
+
+    public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
+        getParameter(parameterIndex).setBinaryStream(slots, parameterIndex, x, length);
+    }
+
+    public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
+        getParameter(parameterIndex).setCharacterStream(slots, parameterIndex, reader, length);
+    }
+
+    public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
+        getParameter(parameterIndex).setAsciiStream(slots, parameterIndex, x);
+    }
+
+    public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
+        getParameter(parameterIndex).setBinaryStream(slots, parameterIndex, x);
+    }
+
+    public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
+        getParameter(parameterIndex).setCharacterStream(slots, parameterIndex, reader);
+    }
+
+    public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException {
+        getParameter(parameterIndex).setNCharacterStream(slots, parameterIndex, value);
+    }
+
+    public void setClob(int parameterIndex, Reader reader) throws SQLException {
+        getParameter(parameterIndex).setClob(slots, parameterIndex, reader);
+    }
+
+    public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
+        getParameter(parameterIndex).setBlob(slots, parameterIndex, inputStream);
+    }
+
+    public void setNClob(int parameterIndex, Reader reader) throws SQLException {
+        getParameter(parameterIndex).setNClob(slots, parameterIndex, reader);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinResultSet.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinResultSet.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinResultSet.java
index 28c3725..1229d10 100644
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinResultSet.java
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinResultSet.java
@@ -1,45 +1,72 @@
-/*
- * 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.kylin.jdbc;
-
-import java.sql.ResultSetMetaData;
-import java.util.TimeZone;
-
-import net.hydromatic.avatica.AvaticaPrepareResult;
-import net.hydromatic.avatica.AvaticaResultSet;
-import net.hydromatic.avatica.AvaticaStatement;
-
-import org.apache.kylin.jdbc.KylinPrepare.PrepareResult;
-
-/**
- * Kylin query result set
- * 
- * @author xduo
- * 
- */
-public class KylinResultSet extends AvaticaResultSet {
-
-    public KylinResultSet(AvaticaStatement statement, AvaticaPrepareResult prepareResult, ResultSetMetaData resultSetMetaData, TimeZone timeZone) {
-        super(statement, prepareResult, resultSetMetaData, timeZone);
-    }
-
-    public KylinPrepare.PrepareResult getPrepareResult() {
-        return (PrepareResult) prepareResult;
-    }
-}
+/*
+ * 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.kylin.jdbc;
+
+import java.io.IOException;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.TimeZone;
+
+import org.apache.calcite.avatica.AvaticaParameter;
+import org.apache.calcite.avatica.AvaticaResultSet;
+import org.apache.calcite.avatica.AvaticaStatement;
+import org.apache.calcite.avatica.Meta.Signature;
+import org.apache.calcite.avatica.MetaImpl;
+import org.apache.kylin.jdbc.IRemoteClient.QueryResult;
+
+public class KylinResultSet extends AvaticaResultSet {
+
+    public KylinResultSet(AvaticaStatement statement, Signature signature, ResultSetMetaData resultSetMetaData, TimeZone timeZone, Iterable<Object> iterable) {
+        super(statement, signature, resultSetMetaData, timeZone, iterable);
+    }
+
+    @Override
+    protected AvaticaResultSet execute() throws SQLException {
+        
+        // skip execution if result is already there (case of meta data lookup)
+        if (this.iterable != null) {
+            return super.execute();
+        }
+        
+        String sql = signature.sql;
+        List<AvaticaParameter> params = signature.parameters;
+        List<Object> paramValues = null;
+        if (params != null && params.size() > 0) {
+            paramValues = ((KylinPreparedStatement) statement).getParameterValues();
+        }
+        
+        IRemoteClient client = ((KylinConnection) statement.connection).getRemoteClient();
+        QueryResult result;
+        try {
+            result = client.executeQuery(sql, params, paramValues);
+        } catch (IOException e) {
+            throw new SQLException(e);
+        }
+        
+        columnMetaDataList.clear();
+        columnMetaDataList.addAll(result.columnMeta);
+        
+        cursor = MetaImpl.createCursor(signature.cursorFactory, result.iterable);
+        return super.execute2(cursor, columnMetaDataList);
+    }
+    
+    
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinStatement.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinStatement.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinStatement.java
new file mode 100644
index 0000000..6596389
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinStatement.java
@@ -0,0 +1,31 @@
+/*
+ * 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.kylin.jdbc;
+
+import org.apache.calcite.avatica.AvaticaConnection;
+import org.apache.calcite.avatica.AvaticaStatement;
+import org.apache.calcite.avatica.Meta.StatementHandle;
+
+public class KylinStatement extends AvaticaStatement {
+
+    protected KylinStatement(AvaticaConnection connection, StatementHandle h, int resultSetType, int resultSetConcurrency, int resultSetHoldability) {
+        super(connection, h, resultSetType, resultSetConcurrency, resultSetHoldability);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/KylinStatementImpl.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinStatementImpl.java b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinStatementImpl.java
deleted file mode 100644
index b380b00..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinStatementImpl.java
+++ /dev/null
@@ -1,54 +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.kylin.jdbc;
-
-import net.hydromatic.avatica.AvaticaConnection;
-import net.hydromatic.avatica.AvaticaResultSet;
-import net.hydromatic.avatica.AvaticaStatement;
-
-/**
- * Kylin statement implementation
- * 
- * @author xduo
- * 
- */
-public abstract class KylinStatementImpl extends AvaticaStatement {
-
-    protected KylinStatementImpl(AvaticaConnection connection, int resultSetType, int resultSetConcurrency, int resultSetHoldability) {
-        super(connection, resultSetType, resultSetConcurrency, resultSetHoldability);
-    }
-
-    @Override
-    protected void close_() {
-        if (!closed) {
-            closed = true;
-            final KylinConnectionImpl connection_ = (KylinConnectionImpl) connection;
-            connection_.statements.remove(this);
-            if (openResultSet != null) {
-                AvaticaResultSet c = openResultSet;
-                openResultSet = null;
-                c.close();
-            }
-            // If onStatementClose throws, this method will throw an exception
-            // (later
-            // converted to SQLException), but this statement still gets closed.
-            connection_.getDriver().handler.onStatementClose(this);
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/json/PreparedQueryRequest.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/json/PreparedQueryRequest.java b/jdbc/src/main/java/org/apache/kylin/jdbc/json/PreparedQueryRequest.java
new file mode 100644
index 0000000..1f37bfd
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/json/PreparedQueryRequest.java
@@ -0,0 +1,33 @@
+/*
+ * 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.kylin.jdbc.json;
+
+import java.util.List;
+
+public class PreparedQueryRequest extends QueryRequest {
+    private List<StatementParameter> params;
+
+    public List<StatementParameter> getParams() {
+        return params;
+    }
+
+    public void setParams(List<StatementParameter> params) {
+        this.params = params;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/json/QueryRequest.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/json/QueryRequest.java b/jdbc/src/main/java/org/apache/kylin/jdbc/json/QueryRequest.java
new file mode 100644
index 0000000..4878aba
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/json/QueryRequest.java
@@ -0,0 +1,40 @@
+/*
+ * 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.kylin.jdbc.json;
+
+public class QueryRequest {
+    private String sql;
+    private String project;
+
+    public String getSql() {
+        return sql;
+    }
+
+    public void setSql(String sql) {
+        this.sql = sql;
+    }
+
+    public String getProject() {
+        return project;
+    }
+
+    public void setProject(String project) {
+        this.project = project;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/json/SQLResponseStub.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/json/SQLResponseStub.java b/jdbc/src/main/java/org/apache/kylin/jdbc/json/SQLResponseStub.java
new file mode 100644
index 0000000..f34e48c
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/json/SQLResponseStub.java
@@ -0,0 +1,321 @@
+/*
+ * 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.kylin.jdbc.json;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ */
+public class SQLResponseStub implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    // private static final Logger logger =
+    // LoggerFactory.getLogger(SQLResponse.class);
+
+    // the data type for each column
+    private List<ColumnMetaStub> columnMetas;
+
+    // the results rows, each row contains several columns
+    private List<String[]> results;
+
+    private String cube;
+
+    // if not select query, only return affected row count
+    private int affectedRowCount;
+
+    // if isException, the detailed exception message
+    private String exceptionMessage;
+
+    private boolean isException;
+
+    private long duration;
+
+    private boolean isPartial = false;
+
+    private long totalScanCount;
+
+    private boolean hitCache = false;
+
+    public SQLResponseStub() {
+    }
+
+    public List<ColumnMetaStub> getColumnMetas() {
+        return columnMetas;
+    }
+
+    public void setColumnMetas(List<ColumnMetaStub> columnMetas) {
+        this.columnMetas = columnMetas;
+    }
+
+    public List<String[]> getResults() {
+        return results;
+    }
+
+    public void setResults(List<String[]> results) {
+        this.results = results;
+    }
+
+    public String getCube() {
+        return cube;
+    }
+
+    public void setCube(String cube) {
+        this.cube = cube;
+    }
+
+    public int getAffectedRowCount() {
+        return affectedRowCount;
+    }
+
+    public void setAffectedRowCount(int affectedRowCount) {
+        this.affectedRowCount = affectedRowCount;
+    }
+
+    public boolean getIsException() {
+        return isException;
+    }
+
+    public void setIsException(boolean isException) {
+        this.isException = isException;
+    }
+
+    public String getExceptionMessage() {
+        return exceptionMessage;
+    }
+
+    public void setExceptionMessage(String exceptionMessage) {
+        this.exceptionMessage = exceptionMessage;
+    }
+
+    public long getDuration() {
+        return duration;
+    }
+
+    public void setDuration(long duration) {
+        this.duration = duration;
+    }
+
+    public boolean isPartial() {
+        return isPartial;
+    }
+
+    public void setPartial(boolean isPartial) {
+        this.isPartial = isPartial;
+    }
+
+    public long getTotalScanCount() {
+        return totalScanCount;
+    }
+
+    public void setTotalScanCount(long totalScanCount) {
+        this.totalScanCount = totalScanCount;
+    }
+
+    public boolean isHitCache() {
+        return hitCache;
+    }
+
+    public void setHitCache(boolean hitCache) {
+        this.hitCache = hitCache;
+    }
+
+    public static long getSerialversionuid() {
+        return serialVersionUID;
+    }
+
+    public static class ColumnMetaStub {
+
+        private boolean isAutoIncrement;
+        private boolean isCaseSensitive;
+        private boolean isSearchable;
+        private boolean isCurrency;
+        private int isNullable;// 0:nonull, 1:nullable, 2: nullableunknown
+        private boolean isSigned;
+        private int displaySize;
+        private String label;// AS keyword
+        private String name;
+        private String schemaName;
+        private String catelogName;
+        private String tableName;
+        private int precision;
+        private int scale;
+        private int columnType;// as defined in java.sql.Types
+        private String columnTypeName;
+        private boolean isReadOnly;
+        private boolean isWritable;
+        private boolean isDefinitelyWritable;
+
+        public ColumnMetaStub() {
+        }
+
+        public boolean isAutoIncrement() {
+            return isAutoIncrement;
+        }
+
+        public void setAutoIncrement(boolean isAutoIncrement) {
+            this.isAutoIncrement = isAutoIncrement;
+        }
+
+        public boolean isCaseSensitive() {
+            return isCaseSensitive;
+        }
+
+        public void setCaseSensitive(boolean isCaseSensitive) {
+            this.isCaseSensitive = isCaseSensitive;
+        }
+
+        public boolean isSearchable() {
+            return isSearchable;
+        }
+
+        public void setSearchable(boolean isSearchable) {
+            this.isSearchable = isSearchable;
+        }
+
+        public boolean isCurrency() {
+            return isCurrency;
+        }
+
+        public void setCurrency(boolean isCurrency) {
+            this.isCurrency = isCurrency;
+        }
+
+        public int getIsNullable() {
+            return isNullable;
+        }
+
+        public void setIsNullable(int isNullable) {
+            this.isNullable = isNullable;
+        }
+
+        public boolean isSigned() {
+            return isSigned;
+        }
+
+        public void setSigned(boolean isSigned) {
+            this.isSigned = isSigned;
+        }
+
+        public int getDisplaySize() {
+            return displaySize;
+        }
+
+        public void setDisplaySize(int displaySize) {
+            this.displaySize = displaySize;
+        }
+
+        public String getLabel() {
+            return label;
+        }
+
+        public void setLabel(String label) {
+            this.label = label;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        public String getSchemaName() {
+            return schemaName;
+        }
+
+        public void setSchemaName(String schemaName) {
+            this.schemaName = schemaName;
+        }
+
+        public String getCatelogName() {
+            return catelogName;
+        }
+
+        public void setCatelogName(String catelogName) {
+            this.catelogName = catelogName;
+        }
+
+        public String getTableName() {
+            return tableName;
+        }
+
+        public void setTableName(String tableName) {
+            this.tableName = tableName;
+        }
+
+        public int getPrecision() {
+            return precision;
+        }
+
+        public void setPrecision(int precision) {
+            this.precision = precision;
+        }
+
+        public int getScale() {
+            return scale;
+        }
+
+        public void setScale(int scale) {
+            this.scale = scale;
+        }
+
+        public int getColumnType() {
+            return columnType;
+        }
+
+        public void setColumnType(int columnType) {
+            this.columnType = columnType;
+        }
+
+        public String getColumnTypeName() {
+            return columnTypeName;
+        }
+
+        public void setColumnTypeName(String columnTypeName) {
+            this.columnTypeName = columnTypeName;
+        }
+
+        public boolean isReadOnly() {
+            return isReadOnly;
+        }
+
+        public void setReadOnly(boolean isReadOnly) {
+            this.isReadOnly = isReadOnly;
+        }
+
+        public boolean isWritable() {
+            return isWritable;
+        }
+
+        public void setWritable(boolean isWritable) {
+            this.isWritable = isWritable;
+        }
+
+        public boolean isDefinitelyWritable() {
+            return isDefinitelyWritable;
+        }
+
+        public void setDefinitelyWritable(boolean isDefinitelyWritable) {
+            this.isDefinitelyWritable = isDefinitelyWritable;
+        }
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/json/StatementParameter.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/json/StatementParameter.java b/jdbc/src/main/java/org/apache/kylin/jdbc/json/StatementParameter.java
new file mode 100644
index 0000000..75cf422
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/json/StatementParameter.java
@@ -0,0 +1,47 @@
+/*
+ * 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.kylin.jdbc.json;
+
+public class StatementParameter {
+
+    private String className;
+    private String value;
+
+    public StatementParameter(String className, String value) {
+        this.className = className;
+        this.value = value;
+    }
+
+    public String getClassName() {
+        return className;
+    }
+
+    public void setClazz(String className) {
+        this.className = className;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/json/TableMetaStub.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/json/TableMetaStub.java b/jdbc/src/main/java/org/apache/kylin/jdbc/json/TableMetaStub.java
new file mode 100644
index 0000000..3d4b115
--- /dev/null
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/json/TableMetaStub.java
@@ -0,0 +1,345 @@
+/*
+ * 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.kylin.jdbc.json;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+/**
+ */
+public class TableMetaStub {
+
+    private static final long serialVersionUID = 1L;
+    private String TABLE_CAT;
+    private String TABLE_SCHEM;
+    private String TABLE_NAME;
+    private String TABLE_TYPE;
+    private String REMARKS;
+    private String TYPE_CAT;
+    private String TYPE_SCHEM;
+    private String TYPE_NAME;
+    private String SELF_REFERENCING_COL_NAME;
+    private String REF_GENERATION;
+    @JsonProperty("columns")
+    private List<ColumnMetaStub> columns = new ArrayList<ColumnMetaStub>();
+
+    public String getTABLE_CAT() {
+        return TABLE_CAT;
+    }
+
+    public void setTABLE_CAT(String tABLE_CAT) {
+        TABLE_CAT = tABLE_CAT;
+    }
+
+    public String getTABLE_SCHEM() {
+        return TABLE_SCHEM;
+    }
+
+    public void setTABLE_SCHEM(String tABLE_SCHEM) {
+        TABLE_SCHEM = tABLE_SCHEM;
+    }
+
+    public String getTABLE_NAME() {
+        return TABLE_NAME;
+    }
+
+    public void setTABLE_NAME(String tABLE_NAME) {
+        TABLE_NAME = tABLE_NAME;
+    }
+
+    public String getTABLE_TYPE() {
+        return TABLE_TYPE;
+    }
+
+    public void setTABLE_TYPE(String tABLE_TYPE) {
+        TABLE_TYPE = tABLE_TYPE;
+    }
+
+    public String getREMARKS() {
+        return REMARKS;
+    }
+
+    public void setREMARKS(String rEMARKS) {
+        REMARKS = rEMARKS;
+    }
+
+    public String getTYPE_CAT() {
+        return TYPE_CAT;
+    }
+
+    public void setTYPE_CAT(String tYPE_CAT) {
+        TYPE_CAT = tYPE_CAT;
+    }
+
+    public String getTYPE_SCHEM() {
+        return TYPE_SCHEM;
+    }
+
+    public void setTYPE_SCHEM(String tYPE_SCHEM) {
+        TYPE_SCHEM = tYPE_SCHEM;
+    }
+
+    public String getTYPE_NAME() {
+        return TYPE_NAME;
+    }
+
+    public void setTYPE_NAME(String tYPE_NAME) {
+        TYPE_NAME = tYPE_NAME;
+    }
+
+    public String getSELF_REFERENCING_COL_NAME() {
+        return SELF_REFERENCING_COL_NAME;
+    }
+
+    public void setSELF_REFERENCING_COL_NAME(String sELF_REFERENCING_COL_NAME) {
+        SELF_REFERENCING_COL_NAME = sELF_REFERENCING_COL_NAME;
+    }
+
+    public String getREF_GENERATION() {
+        return REF_GENERATION;
+    }
+
+    public void setREF_GENERATION(String rEF_GENERATION) {
+        REF_GENERATION = rEF_GENERATION;
+    }
+
+    public List<ColumnMetaStub> getColumns() {
+        return columns;
+    }
+
+    public void setColumns(List<ColumnMetaStub> columns) {
+        this.columns = columns;
+    }
+
+    public static long getSerialversionuid() {
+        return serialVersionUID;
+    }
+
+    public static class ColumnMetaStub {
+        private String TABLE_CAT;
+        private String TABLE_SCHEM;
+        private String TABLE_NAME;
+        private String COLUMN_NAME;
+        private int DATA_TYPE;
+        private String TYPE_NAME;
+        private int COLUMN_SIZE;
+        private int BUFFER_LENGTH;
+        private int DECIMAL_DIGITS;
+        private int NUM_PREC_RADIX;
+        private int NULLABLE;
+        private String REMARKS;
+        private String COLUMN_DEF;
+        private int SQL_DATA_TYPE;
+        private int SQL_DATETIME_SUB;
+        private int CHAR_OCTET_LENGTH;
+        private int ORDINAL_POSITION;
+        private String IS_NULLABLE;
+        private String SCOPE_CATLOG;
+        private String SCOPE_SCHEMA;
+        private String SCOPE_TABLE;
+        private short SOURCE_DATA_TYPE;
+        private String IS_AUTOINCREMENT;
+
+        public String getTABLE_CAT() {
+            return TABLE_CAT;
+        }
+
+        public void setTABLE_CAT(String tABLE_CAT) {
+            TABLE_CAT = tABLE_CAT;
+        }
+
+        public String getTABLE_SCHEM() {
+            return TABLE_SCHEM;
+        }
+
+        public void setTABLE_SCHEM(String tABLE_SCHEM) {
+            TABLE_SCHEM = tABLE_SCHEM;
+        }
+
+        public String getTABLE_NAME() {
+            return TABLE_NAME;
+        }
+
+        public void setTABLE_NAME(String tABLE_NAME) {
+            TABLE_NAME = tABLE_NAME;
+        }
+
+        public String getCOLUMN_NAME() {
+            return COLUMN_NAME;
+        }
+
+        public void setCOLUMN_NAME(String cOLUMN_NAME) {
+            COLUMN_NAME = cOLUMN_NAME;
+        }
+
+        public int getDATA_TYPE() {
+            return DATA_TYPE;
+        }
+
+        public void setDATA_TYPE(int dATA_TYPE) {
+            DATA_TYPE = dATA_TYPE;
+        }
+
+        public String getTYPE_NAME() {
+            return TYPE_NAME;
+        }
+
+        public void setTYPE_NAME(String tYPE_NAME) {
+            TYPE_NAME = tYPE_NAME;
+        }
+
+        public int getCOLUMN_SIZE() {
+            return COLUMN_SIZE;
+        }
+
+        public void setCOLUMN_SIZE(int cOLUMN_SIZE) {
+            COLUMN_SIZE = cOLUMN_SIZE;
+        }
+
+        public int getBUFFER_LENGTH() {
+            return BUFFER_LENGTH;
+        }
+
+        public void setBUFFER_LENGTH(int bUFFER_LENGTH) {
+            BUFFER_LENGTH = bUFFER_LENGTH;
+        }
+
+        public int getDECIMAL_DIGITS() {
+            return DECIMAL_DIGITS;
+        }
+
+        public void setDECIMAL_DIGITS(int dECIMAL_DIGITS) {
+            DECIMAL_DIGITS = dECIMAL_DIGITS;
+        }
+
+        public int getNUM_PREC_RADIX() {
+            return NUM_PREC_RADIX;
+        }
+
+        public void setNUM_PREC_RADIX(int nUM_PREC_RADIX) {
+            NUM_PREC_RADIX = nUM_PREC_RADIX;
+        }
+
+        public int getNULLABLE() {
+            return NULLABLE;
+        }
+
+        public void setNULLABLE(int nULLABLE) {
+            NULLABLE = nULLABLE;
+        }
+
+        public String getREMARKS() {
+            return REMARKS;
+        }
+
+        public void setREMARKS(String rEMARKS) {
+            REMARKS = rEMARKS;
+        }
+
+        public String getCOLUMN_DEF() {
+            return COLUMN_DEF;
+        }
+
+        public void setCOLUMN_DEF(String cOLUMN_DEF) {
+            COLUMN_DEF = cOLUMN_DEF;
+        }
+
+        public int getSQL_DATA_TYPE() {
+            return SQL_DATA_TYPE;
+        }
+
+        public void setSQL_DATA_TYPE(int sQL_DATA_TYPE) {
+            SQL_DATA_TYPE = sQL_DATA_TYPE;
+        }
+
+        public int getSQL_DATETIME_SUB() {
+            return SQL_DATETIME_SUB;
+        }
+
+        public void setSQL_DATETIME_SUB(int sQL_DATETIME_SUB) {
+            SQL_DATETIME_SUB = sQL_DATETIME_SUB;
+        }
+
+        public int getCHAR_OCTET_LENGTH() {
+            return CHAR_OCTET_LENGTH;
+        }
+
+        public void setCHAR_OCTET_LENGTH(int cHAR_OCTET_LENGTH) {
+            CHAR_OCTET_LENGTH = cHAR_OCTET_LENGTH;
+        }
+
+        public int getORDINAL_POSITION() {
+            return ORDINAL_POSITION;
+        }
+
+        public void setORDINAL_POSITION(int oRDINAL_POSITION) {
+            ORDINAL_POSITION = oRDINAL_POSITION;
+        }
+
+        public String getIS_NULLABLE() {
+            return IS_NULLABLE;
+        }
+
+        public void setIS_NULLABLE(String iS_NULLABLE) {
+            IS_NULLABLE = iS_NULLABLE;
+        }
+
+        public String getSCOPE_CATLOG() {
+            return SCOPE_CATLOG;
+        }
+
+        public void setSCOPE_CATLOG(String sCOPE_CATLOG) {
+            SCOPE_CATLOG = sCOPE_CATLOG;
+        }
+
+        public String getSCOPE_SCHEMA() {
+            return SCOPE_SCHEMA;
+        }
+
+        public void setSCOPE_SCHEMA(String sCOPE_SCHEMA) {
+            SCOPE_SCHEMA = sCOPE_SCHEMA;
+        }
+
+        public String getSCOPE_TABLE() {
+            return SCOPE_TABLE;
+        }
+
+        public void setSCOPE_TABLE(String sCOPE_TABLE) {
+            SCOPE_TABLE = sCOPE_TABLE;
+        }
+
+        public short getSOURCE_DATA_TYPE() {
+            return SOURCE_DATA_TYPE;
+        }
+
+        public void setSOURCE_DATA_TYPE(short sOURCE_DATA_TYPE) {
+            SOURCE_DATA_TYPE = sOURCE_DATA_TYPE;
+        }
+
+        public String getIS_AUTOINCREMENT() {
+            return IS_AUTOINCREMENT;
+        }
+
+        public void setIS_AUTOINCREMENT(String iS_AUTOINCREMENT) {
+            this.IS_AUTOINCREMENT = iS_AUTOINCREMENT;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/stub/ConnectionException.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/ConnectionException.java b/jdbc/src/main/java/org/apache/kylin/jdbc/stub/ConnectionException.java
deleted file mode 100644
index eeeafc6..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/ConnectionException.java
+++ /dev/null
@@ -1,55 +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.kylin.jdbc.stub;
-
-/**
- * @author xduo
- * 
- */
-public class ConnectionException extends Exception {
-
-    private static final long serialVersionUID = 1L;
-
-    public ConnectionException() {
-        super();
-    }
-
-    /**
-     * @param message
-     * @param cause
-     */
-    public ConnectionException(String message, Throwable cause) {
-        super(message, cause);
-    }
-
-    /**
-     * @param message
-     */
-    public ConnectionException(String message) {
-        super(message);
-    }
-
-    /**
-     * @param cause
-     */
-    public ConnectionException(Throwable cause) {
-        super(cause);
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/22a0bc33/jdbc/src/main/java/org/apache/kylin/jdbc/stub/DataSet.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/DataSet.java b/jdbc/src/main/java/org/apache/kylin/jdbc/stub/DataSet.java
deleted file mode 100644
index 3584b31..0000000
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/stub/DataSet.java
+++ /dev/null
@@ -1,55 +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.kylin.jdbc.stub;
-
-import java.util.List;
-
-import net.hydromatic.avatica.ColumnMetaData;
-import net.hydromatic.linq4j.Enumerator;
-
-/**
- * Data set wrapper.
- * 
- * @author xduo
- * 
- */
-public class DataSet<E> {
-
-    private final List<ColumnMetaData> meta;
-
-    private final Enumerator<E> enumerator;
-
-    /**
-     * @param meta
-     * @param enumerator
-     */
-    public DataSet(List<ColumnMetaData> meta, Enumerator<E> enumerator) {
-        this.meta = meta;
-        this.enumerator = enumerator;
-    }
-
-    public List<ColumnMetaData> getMeta() {
-        return meta;
-    }
-
-    public Enumerator<E> getEnumerator() {
-        return enumerator;
-    }
-
-}