Repository: phoenix
Updated Branches:
  refs/heads/calcite 52ba5d24d -> 69a2f2437


PHOENIX-2678 Replace calcite default RelOptCostFactory


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

Branch: refs/heads/calcite
Commit: 69a2f2437bf0d6513d3f6c46ec34094279f0f411
Parents: 52ba5d2
Author: maryannxue <maryann....@gmail.com>
Authored: Sat Feb 13 22:53:03 2016 -0500
Committer: maryannxue <maryann....@gmail.com>
Committed: Sat Feb 13 22:53:03 2016 -0500

----------------------------------------------------------------------
 .../org/apache/phoenix/calcite/CalciteIT.java   |   8 +-
 .../apache/phoenix/calcite/CalciteIndexIT.java  |   2 +-
 .../calcite/jdbc/PhoenixPrepareImpl.java        |   2 +
 .../phoenix/calcite/plan/PhoenixCost.java       | 227 +++++++++++++++++++
 .../calcite/rel/PhoenixAbstractAggregate.java   |   2 +-
 .../calcite/rel/PhoenixAbstractSort.java        |   2 +-
 .../calcite/rel/PhoenixClientAggregate.java     |   3 +-
 .../phoenix/calcite/rel/PhoenixClientJoin.java  |   4 +-
 .../calcite/rel/PhoenixClientProject.java       |   3 +-
 .../calcite/rel/PhoenixClientSemiJoin.java      |   4 +-
 .../phoenix/calcite/rel/PhoenixClientSort.java  |   3 +-
 .../calcite/rel/PhoenixCompactClientSort.java   |   3 +-
 .../phoenix/calcite/rel/PhoenixCorrelate.java   |  21 +-
 .../phoenix/calcite/rel/PhoenixFilter.java      |   4 +-
 .../phoenix/calcite/rel/PhoenixLimit.java       |   4 +-
 .../calcite/rel/PhoenixMergeSortUnion.java      |   4 +-
 .../apache/phoenix/calcite/rel/PhoenixRel.java  |   7 -
 .../calcite/rel/PhoenixServerAggregate.java     |   3 +-
 .../phoenix/calcite/rel/PhoenixServerJoin.java  |   4 +-
 .../calcite/rel/PhoenixServerProject.java       |   3 +-
 .../calcite/rel/PhoenixServerSemiJoin.java      |   4 +-
 .../phoenix/calcite/rel/PhoenixServerSort.java  |   3 +-
 .../phoenix/calcite/rel/PhoenixTableScan.java   |   6 +-
 .../phoenix/calcite/rel/PhoenixUncollect.java   |  15 +-
 .../phoenix/calcite/rel/PhoenixUnion.java       |   4 +-
 .../phoenix/calcite/rel/PhoenixValues.java      |   5 +-
 .../calcite/rules/PhoenixConverterRules.java    |   3 +-
 27 files changed, 293 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java
index df6ac81..820c2d4 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java
@@ -293,17 +293,17 @@ public class CalciteIT extends BaseCalciteIT {
         
         start(false, 1000f).sql("select t1.entity_id, t2.a_string, 
t3.organization_id from aTable t1 join aTable t2 on t1.entity_id = t2.entity_id 
and t1.organization_id = t2.organization_id join atable t3 on t1.entity_id = 
t3.entity_id and t1.organization_id = t3.organization_id")
                 .explainIs("PhoenixToEnumerableConverter\n" +
-                           "  PhoenixClientProject(ENTITY_ID=[$1], 
A_STRING=[$6], ORGANIZATION_ID=[$2])\n" +
-                           "    PhoenixClientJoin(condition=[AND(=($1, $5), 
=($0, $4))], joinType=[inner])\n" +
+                           "  PhoenixClientProject(ENTITY_ID=[$1], 
A_STRING=[$4], ORGANIZATION_ID=[$5])\n" +
+                           "    PhoenixClientJoin(condition=[AND(=($1, $6), 
=($0, $5))], joinType=[inner])\n" +
                            "      PhoenixClientJoin(condition=[AND(=($1, $3), 
=($0, $2))], joinType=[inner])\n" +
                            "        PhoenixServerSort(sort0=[$1], sort1=[$0], 
dir0=[ASC], dir1=[ASC])\n" +
                            "          
PhoenixServerProject(ORGANIZATION_ID=[$0], ENTITY_ID=[$1])\n" +
                            "            PhoenixTableScan(table=[[phoenix, 
ATABLE]])\n" +
                            "        PhoenixServerSort(sort0=[$1], sort1=[$0], 
dir0=[ASC], dir1=[ASC])\n" +
-                           "          
PhoenixServerProject(ORGANIZATION_ID=[$0], ENTITY_ID=[$1])\n" +
+                           "          
PhoenixServerProject(ORGANIZATION_ID=[$0], ENTITY_ID=[$1], A_STRING=[$2])\n" +
                            "            PhoenixTableScan(table=[[phoenix, 
ATABLE]])\n" +
                            "      PhoenixServerSort(sort0=[$1], sort1=[$0], 
dir0=[ASC], dir1=[ASC])\n" +
-                           "        PhoenixServerProject(ORGANIZATION_ID=[$0], 
ENTITY_ID=[$1], A_STRING=[$2])\n" +
+                           "        PhoenixServerProject(ORGANIZATION_ID=[$0], 
ENTITY_ID=[$1])\n" +
                            "          PhoenixTableScan(table=[[phoenix, 
ATABLE]])\n")
                 .resultIs(false, new Object[][] {
                           {"00A123122312312", "a", "00D300000000XHP"},

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIndexIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIndexIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIndexIT.java
index d1aea74..1b2423b 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIndexIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIndexIT.java
@@ -168,7 +168,7 @@ public class CalciteIndexIT extends BaseCalciteIT {
                            "    PhoenixTableScan(table=[[phoenix, 
IDXSALTED_SALTED_TEST_TABLE]], filter=[>(CAST($0):INTEGER, 4)])\n")
                 .resultIs(false, new Object[][]{{999L}})
                 .close();
-        start(true, 1f).sql("select * from " + SALTED_TABLE_NAME + " where 
col1 <= 5 order by col1")
+        start(true, 0.001f).sql("select * from " + SALTED_TABLE_NAME + " where 
col1 <= 5 order by col1")
                 .explainIs("PhoenixToEnumerableConverter\n" +
                            "  PhoenixServerProject(MYPK0=[$1], MYPK1=[$2], 
COL0=[$3], COL1=[CAST($0):INTEGER])\n" +
                            "    PhoenixTableScan(table=[[phoenix, 
IDXSALTED_SALTED_TEST_TABLE]], filter=[<=(CAST($0):INTEGER, 5)], 
scanOrder=[FORWARD])\n")

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
index ae09b42..b5968f7 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
@@ -27,6 +27,7 @@ import org.apache.calcite.util.Pair;
 import org.apache.phoenix.calcite.PhoenixSchema;
 import org.apache.phoenix.calcite.parse.SqlCreateView;
 import org.apache.phoenix.calcite.parser.PhoenixParserImpl;
+import org.apache.phoenix.calcite.plan.PhoenixCost;
 import org.apache.phoenix.calcite.rel.PhoenixRel;
 import org.apache.phoenix.calcite.rel.PhoenixServerProject;
 import org.apache.phoenix.calcite.rel.PhoenixTemporarySort;
@@ -77,6 +78,7 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl {
             final CalcitePrepare.Context prepareContext,
             org.apache.calcite.plan.Context externalContext,
             RelOptCostFactory costFactory) {
+        costFactory = PhoenixCost.FACTORY;
         RelOptPlanner planner = super.createPlanner(prepareContext, 
externalContext, costFactory);
         
         planner.removeRule(EnumerableRules.ENUMERABLE_SEMI_JOIN_RULE);

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/plan/PhoenixCost.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/plan/PhoenixCost.java 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/plan/PhoenixCost.java
new file mode 100644
index 0000000..3bbd403
--- /dev/null
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/plan/PhoenixCost.java
@@ -0,0 +1,227 @@
+package org.apache.phoenix.calcite.plan;
+
+import java.util.Objects;
+
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptCostFactory;
+import org.apache.calcite.plan.RelOptUtil;
+
+/**
+ * Implementation of Calcite {@link org.apache.calcite.plan.RelOptCost}
+ * for Phoenix.
+ */
+public class PhoenixCost implements RelOptCost, Comparable<RelOptCost> {
+    //~ Static fields/initializers 
---------------------------------------------
+
+    static final PhoenixCost INFINITY =
+            new PhoenixCost(
+                    Double.POSITIVE_INFINITY,
+                    Double.POSITIVE_INFINITY,
+                    Double.POSITIVE_INFINITY) {
+        public String toString() {
+            return "{inf}";
+        }
+    };
+
+    static final PhoenixCost HUGE =
+            new PhoenixCost(
+                    Double.MAX_VALUE,
+                    Double.MAX_VALUE,
+                    Double.MAX_VALUE) {
+        public String toString() {
+            return "{huge}";
+        }
+    };
+
+    static final PhoenixCost ZERO =
+            new PhoenixCost(0.0, 0.0, 0.0) {
+        public String toString() {
+            return "{zero}";
+        }
+    };
+
+    static final PhoenixCost TINY =
+            new PhoenixCost(1.0, 1.0, 1.0) {
+        public String toString() {
+            return "{tiny}";
+        }
+    };
+
+    public static final RelOptCostFactory FACTORY = new Factory();
+
+    //~ Instance fields 
--------------------------------------------------------
+
+    final double cpu;
+    final double io;
+    final double rowCount;
+
+    //~ Constructors 
-----------------------------------------------------------
+
+    PhoenixCost(double rowCount, double cpu, double io) {
+        this.rowCount = rowCount;
+        this.cpu = cpu;
+        this.io = io;
+    }
+
+    //~ Methods 
----------------------------------------------------------------
+
+    @Override
+    public double getRows() {
+        return rowCount;
+    }
+
+    @Override
+    public double getCpu() {
+        return cpu;
+    }
+
+    @Override
+    public double getIo() {
+        return io;
+    }
+
+    @Override
+    public boolean isInfinite() {
+        return (this == INFINITY)
+                || (this.rowCount == Double.POSITIVE_INFINITY)
+                || (this.cpu == Double.POSITIVE_INFINITY)
+                || (this.io == Double.POSITIVE_INFINITY);
+    }
+
+    @Override
+    public boolean isLe(RelOptCost other) {
+        return this.compareTo(other) <= 0;
+    }
+
+    public boolean isLt(RelOptCost other) {
+        return this.compareTo(other) < 0;
+    }
+
+    @Override
+    public int compareTo(RelOptCost other) {
+        if (this == other) {
+            return 0;
+        }
+        
+        PhoenixCost that = (PhoenixCost) other;
+        if (this.rowCount != that.rowCount) {
+            return this.rowCount < that.rowCount ? -1 : 1;
+        }
+        
+        double thisSum = this.cpu + this.io;
+        double thatSum = that.cpu + that.io;
+        return thisSum == thatSum ? 0 : (thisSum < thatSum ? -1 : 1);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(rowCount, cpu, io);
+    }
+
+    @Override
+    public boolean equals(RelOptCost other) {
+        return (other instanceof PhoenixCost)
+                && this.compareTo(other) == 0;
+    }
+
+    public boolean isEqWithEpsilon(RelOptCost other) {
+        if (!(other instanceof PhoenixCost)) {
+            return false;
+        }
+        PhoenixCost that = (PhoenixCost) other;
+        return (this == that)
+                || ((Math.abs(this.rowCount - that.rowCount) < 
RelOptUtil.EPSILON)
+                        && (Math.abs(this.cpu - that.cpu) < RelOptUtil.EPSILON)
+                        && (Math.abs(this.io - that.io) < RelOptUtil.EPSILON));
+    }
+
+    public RelOptCost minus(RelOptCost other) {
+        if (this == INFINITY) {
+            return this;
+        }
+        PhoenixCost that = (PhoenixCost) other;
+        return new PhoenixCost(
+                this.rowCount - that.rowCount,
+                this.cpu - that.cpu,
+                this.io - that.io);
+    }
+
+    public RelOptCost multiplyBy(double factor) {
+        if (this == INFINITY) {
+            return this;
+        }
+        return new PhoenixCost(rowCount * factor, cpu * factor, io * factor);
+    }
+
+    public double divideBy(RelOptCost cost) {
+        // Compute the geometric average of the ratios of all of the factors
+        // which are non-zero and finite.
+        PhoenixCost that = (PhoenixCost) cost;
+        double d = 1;
+        double n = 0;
+        if ((this.rowCount != 0)
+                && !Double.isInfinite(this.rowCount)
+                && (that.rowCount != 0)
+                && !Double.isInfinite(that.rowCount)) {
+            d *= this.rowCount / that.rowCount;
+            ++n;
+        }
+        if ((this.cpu != 0)
+                && !Double.isInfinite(this.cpu)
+                && (that.cpu != 0)
+                && !Double.isInfinite(that.cpu)) {
+            d *= this.cpu / that.cpu;
+            ++n;
+        }
+        if ((this.io != 0)
+                && !Double.isInfinite(this.io)
+                && (that.io != 0)
+                && !Double.isInfinite(that.io)) {
+            d *= this.io / that.io;
+            ++n;
+        }
+        if (n == 0) {
+            return 1.0;
+        }
+        return Math.pow(d, 1 / n);
+    }
+
+    public RelOptCost plus(RelOptCost other) {
+        PhoenixCost that = (PhoenixCost) other;
+        if ((this == INFINITY) || (that == INFINITY)) {
+            return INFINITY;
+        }
+        return new PhoenixCost(
+                this.rowCount + that.rowCount,
+                this.cpu + that.cpu,
+                this.io + that.io);
+    }
+
+    public String toString() {
+        return "{" + rowCount + " rows, " + cpu + " cpu, " + io + " io}";
+    }
+
+    /** Implementation of {@link org.apache.calcite.plan.RelOptCostFactory}
+     * that creates {@link org.apache.phoenix.calcite.plan.PhoenixCost}s. */
+    private static class Factory implements RelOptCostFactory {
+      public RelOptCost makeCost(double dRows, double dCpu, double dIo) {
+        return new PhoenixCost(dRows, dCpu, dIo);
+      }
+
+      public RelOptCost makeHugeCost() {
+        return PhoenixCost.HUGE;
+      }
+
+      public RelOptCost makeInfiniteCost() {
+        return PhoenixCost.INFINITY;
+      }
+
+      public RelOptCost makeTinyCost() {
+        return PhoenixCost.TINY;
+      }
+
+      public RelOptCost makeZeroCost() {
+        return PhoenixCost.ZERO;
+      }
+    }
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractAggregate.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractAggregate.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractAggregate.java
index de9f0c2..26d64dc 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractAggregate.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractAggregate.java
@@ -118,7 +118,7 @@ abstract public class PhoenixAbstractAggregate extends 
Aggregate implements Phoe
             multiplier += 0.0125f;
           }
         }
-        return planner.getCostFactory().makeCost(rowCount * multiplier * 
bytesPerRow, 0, 0);
+        return planner.getCostFactory().makeCost(0, rowCount * multiplier * 
bytesPerRow, 0);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractSort.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractSort.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractSort.java
index b8c0136..4719426 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractSort.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractSort.java
@@ -41,7 +41,7 @@ abstract public class PhoenixAbstractSort extends Sort 
implements PhoenixRel {
         double rowCount = mq.getRowCount(this);
         double bytesPerRow = mq.getAverageRowSize(this);
         return planner.getCostFactory().makeCost(
-                Util.nLogN(rowCount) * bytesPerRow, rowCount, 0);
+                0, Util.nLogN(rowCount) * bytesPerRow, 0);
     }
 
     protected static OrderBy getOrderBy(RelCollation collation, Implementor 
implementor, TupleProjector tupleProjector) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientAggregate.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientAggregate.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientAggregate.java
index 5aaff2f..9bf633e 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientAggregate.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientAggregate.java
@@ -53,8 +53,7 @@ public class PhoenixClientAggregate extends 
PhoenixAbstractAggregate {
         if (!getInput().getConvention().satisfies(PhoenixConvention.CLIENT))
             return planner.getCostFactory().makeInfiniteCost();
         
-        return super.computeSelfCost(planner, mq)
-                .multiplyBy(PHOENIX_FACTOR);
+        return super.computeSelfCost(planner, mq);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientJoin.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientJoin.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientJoin.java
index 2df5f74..1593b9d 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientJoin.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientJoin.java
@@ -103,9 +103,9 @@ public class PhoenixClientJoin extends PhoenixAbstractJoin {
                 rowCount += rightRowCount;
             }
         }            
-        RelOptCost cost = planner.getCostFactory().makeCost(rowCount, 0, 0);
+        RelOptCost cost = planner.getCostFactory().makeCost(0, rowCount, 0);
 
-        return cost.multiplyBy(SERVER_FACTOR).multiplyBy(PHOENIX_FACTOR);
+        return cost.multiplyBy(SERVER_FACTOR);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientProject.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientProject.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientProject.java
index 799513d..09eff65 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientProject.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientProject.java
@@ -63,8 +63,7 @@ public class PhoenixClientProject extends 
PhoenixAbstractProject {
         if (!getInput().getConvention().satisfies(PhoenixConvention.GENERIC))
             return planner.getCostFactory().makeInfiniteCost();
         
-        return super.computeSelfCost(planner, mq)
-                .multiplyBy(PHOENIX_FACTOR);
+        return super.computeSelfCost(planner, mq);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSemiJoin.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSemiJoin.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSemiJoin.java
index 31dfc3d..08da3fb 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSemiJoin.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSemiJoin.java
@@ -87,9 +87,9 @@ public class PhoenixClientSemiJoin extends 
PhoenixAbstractSemiJoin implements
                 rowCount += rightRowCount;
             }
         }            
-        RelOptCost cost = planner.getCostFactory().makeCost(rowCount, 0, 0);
+        RelOptCost cost = planner.getCostFactory().makeCost(0, rowCount, 0);
 
-        return cost.multiplyBy(SERVER_FACTOR).multiplyBy(PHOENIX_FACTOR);
+        return cost.multiplyBy(SERVER_FACTOR);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSort.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSort.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSort.java
index 59fb639..80061c8 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSort.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSort.java
@@ -48,8 +48,7 @@ public class PhoenixClientSort extends PhoenixAbstractSort {
         if (!getInput().getConvention().satisfies(PhoenixConvention.CLIENT))
             return planner.getCostFactory().makeInfiniteCost();
         
-        return super.computeSelfCost(planner, mq)
-                .multiplyBy(PHOENIX_FACTOR);
+        return super.computeSelfCost(planner, mq);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCompactClientSort.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCompactClientSort.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCompactClientSort.java
index 8d68a57..be4c6c1 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCompactClientSort.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCompactClientSort.java
@@ -43,8 +43,7 @@ public class PhoenixCompactClientSort extends 
PhoenixAbstractSort {
             return planner.getCostFactory().makeInfiniteCost();
         
         return super.computeSelfCost(planner, mq)
-                .multiplyBy(SERVER_FACTOR)
-                .multiplyBy(PHOENIX_FACTOR);
+                .multiplyBy(SERVER_FACTOR);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCorrelate.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCorrelate.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCorrelate.java
index 496dfdb..b27a4be 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCorrelate.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCorrelate.java
@@ -65,8 +65,25 @@ public class PhoenixCorrelate extends Correlate implements 
PhoenixRel {
         if (!getLeft().getConvention().satisfies(PhoenixConvention.GENERIC)
                 || 
!getRight().getConvention().satisfies(PhoenixConvention.GENERIC))
             return planner.getCostFactory().makeInfiniteCost();
-        
-        return super.computeSelfCost(planner, mq).multiplyBy(PHOENIX_FACTOR);
+
+        double rowCount = mq.getRowCount(this);
+
+        final double rightRowCount = right.estimateRowCount(mq);
+        final double leftRowCount = left.estimateRowCount(mq);
+        if (Double.isInfinite(leftRowCount) || 
Double.isInfinite(rightRowCount)) {
+            return planner.getCostFactory().makeInfiniteCost();
+        }
+
+        Double restartCount = mq.getRowCount(getLeft());
+        // RelMetadataQuery.getCumulativeCost(getRight()); does not work for
+        // RelSubset, so we ask planner to cost-estimate right relation
+        RelOptCost rightCost = planner.getCost(getRight(), mq);
+        RelOptCost rescanCost =
+                rightCost.multiplyBy(Math.max(1.0, restartCount - 1));
+
+        return planner.getCostFactory().makeCost(0,
+                rowCount /* generate results */ + leftRowCount /* scan left 
results */,
+                0).plus(rescanCost);
     }
     
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixFilter.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixFilter.java 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixFilter.java
index 0a0ab8e..d3367af 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixFilter.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixFilter.java
@@ -57,7 +57,9 @@ public class PhoenixFilter extends Filter implements 
PhoenixRel {
         if (!getInput().getConvention().satisfies(PhoenixConvention.GENERIC))
             return planner.getCostFactory().makeInfiniteCost();
         
-        return super.computeSelfCost(planner, mq).multiplyBy(PHOENIX_FACTOR);
+        double rows = mq.getRowCount(this);
+        double inputRows = mq.getRowCount(getInput());
+        return planner.getCostFactory().makeCost(0, rows + inputRows, 0);
     }
 
     public QueryPlan implement(Implementor implementor) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixLimit.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixLimit.java 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixLimit.java
index 3edcf60..44f8229 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixLimit.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixLimit.java
@@ -69,9 +69,7 @@ public class PhoenixLimit extends SingleRel implements 
PhoenixRel {
             return planner.getCostFactory().makeInfiniteCost();
         
         double rowCount = mq.getRowCount(this);
-        return planner.getCostFactory()
-                .makeCost(rowCount, 0, 0)
-                .multiplyBy(PHOENIX_FACTOR);
+        return planner.getCostFactory().makeCost(0, rowCount, 0);
     }
     
     @Override 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java
index 9695662..395a164 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java
@@ -59,9 +59,9 @@ public class PhoenixMergeSortUnion extends Union implements 
PhoenixRel {
             }
         }
         
+        double rowCount = mq.getRowCount(this);
         double mergeSortFactor = 1.1;
-        return super.computeSelfCost(planner, mq)
-                .multiplyBy(PHOENIX_FACTOR).multiplyBy(mergeSortFactor);
+        return planner.getCostFactory().makeCost(0, rowCount * 
mergeSortFactor, 0);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixRel.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixRel.java 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixRel.java
index f6b0e97..cff8f70 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixRel.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixRel.java
@@ -34,13 +34,6 @@ public interface PhoenixRel extends RelNode {
   /** For test purpose */
   String ROW_COUNT_FACTOR = "phoenix.calcite.metadata.rowcount.factor";
 
-  /** Relative cost of Phoenix versus Enumerable convention.
-   *
-   * <p>Multiply by the value (which is less than unity), and you will get a 
cheaper cost.
-   * Phoenix is cheaper.
-   */
-  double PHOENIX_FACTOR = 0.0001;
-
   /** Relative cost of server plan versus client plan.
    *
    * <p>Multiply by the value (which is less than unity), and you will get a 
cheaper cost.

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerAggregate.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerAggregate.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerAggregate.java
index 7ea2581..f7d02f8 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerAggregate.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerAggregate.java
@@ -49,8 +49,7 @@ public class PhoenixServerAggregate extends 
PhoenixAbstractAggregate {
             return planner.getCostFactory().makeInfiniteCost();
         
         return super.computeSelfCost(planner, mq)
-                .multiplyBy(SERVER_FACTOR)
-                .multiplyBy(PHOENIX_FACTOR);
+                .multiplyBy(SERVER_FACTOR);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerJoin.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerJoin.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerJoin.java
index 2f09a9d..39a3e9d 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerJoin.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerJoin.java
@@ -99,9 +99,9 @@ public class PhoenixServerJoin extends PhoenixAbstractJoin {
             }
         }            
         
-        RelOptCost cost = planner.getCostFactory().makeCost(rowCount, 0, 0);
+        RelOptCost cost = planner.getCostFactory().makeCost(0, rowCount, 0);
 
-        return cost.multiplyBy(SERVER_FACTOR).multiplyBy(PHOENIX_FACTOR);
+        return cost.multiplyBy(SERVER_FACTOR);
     }
     
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerProject.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerProject.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerProject.java
index ba0854b..ed90c5c 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerProject.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerProject.java
@@ -53,8 +53,7 @@ public class PhoenixServerProject extends 
PhoenixAbstractProject {
             return planner.getCostFactory().makeInfiniteCost();
         
         return super.computeSelfCost(planner, mq)
-                .multiplyBy(SERVER_FACTOR)
-                .multiplyBy(PHOENIX_FACTOR);
+                .multiplyBy(SERVER_FACTOR);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSemiJoin.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSemiJoin.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSemiJoin.java
index 74c3e4d..50b4ba1 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSemiJoin.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSemiJoin.java
@@ -86,9 +86,9 @@ public class PhoenixServerSemiJoin extends 
PhoenixAbstractSemiJoin {
             }
         }            
         
-        RelOptCost cost = planner.getCostFactory().makeCost(rowCount, 0, 0);
+        RelOptCost cost = planner.getCostFactory().makeCost(0, rowCount, 0);
 
-        return cost.multiplyBy(SERVER_FACTOR).multiplyBy(PHOENIX_FACTOR);
+        return cost.multiplyBy(SERVER_FACTOR);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSort.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSort.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSort.java
index 3053cca..104c3da 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSort.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSort.java
@@ -44,8 +44,7 @@ public class PhoenixServerSort extends PhoenixAbstractSort {
             return planner.getCostFactory().makeInfiniteCost();
         
         return super.computeSelfCost(planner, mq)
-                .multiplyBy(SERVER_FACTOR)
-                .multiplyBy(PHOENIX_FACTOR);
+                .multiplyBy(SERVER_FACTOR);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableScan.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableScan.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableScan.java
index 0841006..522cc7b 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableScan.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableScan.java
@@ -259,10 +259,8 @@ public class PhoenixTableScan extends TableScan implements 
PhoenixRel {
                 byteCount = addEpsilon(byteCount);
             }
         }
-        return planner.getCostFactory()
-                .makeCost(byteCount, byteCount + 1, 0)
-                .multiplyBy(SERVER_FACTOR)
-                .multiplyBy(PHOENIX_FACTOR);
+        return planner.getCostFactory().makeCost(0, byteCount, 0)
+                .multiplyBy(SERVER_FACTOR);
     }
     
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java
index 0d64868..cf090c3 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java
@@ -20,29 +20,30 @@ import org.apache.phoenix.schema.types.PDataType;
 
 public class PhoenixUncollect extends Uncollect implements PhoenixRel {
     
-    public static PhoenixUncollect create(RelNode input) {
+    public static PhoenixUncollect create(RelNode input, boolean 
withOrdinality) {
         RelOptCluster cluster = input.getCluster();
         RelTraitSet traits = cluster.traitSetOf(PhoenixConvention.CLIENT);
-        return new PhoenixUncollect(cluster, traits, input);
+        return new PhoenixUncollect(cluster, traits, input, withOrdinality);
     }
 
     private PhoenixUncollect(RelOptCluster cluster, RelTraitSet traitSet,
-            RelNode child) {
-        super(cluster, traitSet, child);
+            RelNode child, boolean withOrdinality) {
+        super(cluster, traitSet, child, withOrdinality);
     }
 
     @Override
     public PhoenixUncollect copy(RelTraitSet traitSet,
         RelNode newInput) {
-        return create(newInput);
+        return create(newInput, withOrdinality);
     }
 
     @Override
     public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery 
mq) {
         if (!getInput().getConvention().satisfies(PhoenixConvention.GENERIC))
             return planner.getCostFactory().makeInfiniteCost();
-        
-        return super.computeSelfCost(planner, mq).multiplyBy(PHOENIX_FACTOR);
+                
+        double rowCount = mq.getRowCount(this);
+        return planner.getCostFactory().makeCost(0, rowCount, 0);
     }
     
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUnion.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUnion.java 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUnion.java
index 70bc71c..b0978ea 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUnion.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUnion.java
@@ -47,8 +47,8 @@ public class PhoenixUnion extends Union implements PhoenixRel 
{
             }
         }
         
-        return super.computeSelfCost(planner, mq)
-                .multiplyBy(PHOENIX_FACTOR);
+        double rowCount = mq.getRowCount(this);
+        return planner.getCostFactory().makeCost(0, rowCount, 0);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixValues.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixValues.java 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixValues.java
index 9af3f52..5422583 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixValues.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixValues.java
@@ -101,8 +101,9 @@ public class PhoenixValues extends Values implements 
PhoenixRel {
     }
 
     @Override
-    public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery 
mq) {
-        return super.computeSelfCost(planner, mq).multiplyBy(PHOENIX_FACTOR);
+    public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery 
mq) {        
+        double rowCount = mq.getRowCount(this);
+        return planner.getCostFactory().makeCost(0, rowCount, 0);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/69a2f243/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
index 81dbe21..396974d 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
@@ -680,7 +680,8 @@ public class PhoenixConverterRules {
             return PhoenixUncollect.create(
                 convert(
                         uncollect.getInput(), 
-                        
uncollect.getInput().getTraitSet().replace(PhoenixConvention.GENERIC)));
+                        
uncollect.getInput().getTraitSet().replace(PhoenixConvention.GENERIC)),
+                uncollect.withOrdinality);
         }
     }
 

Reply via email to