Repository: phoenix
Updated Branches:
  refs/heads/calcite 53dab808a -> f2d95da77


Adjust cost of Project


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

Branch: refs/heads/calcite
Commit: cedc1c502b177824a476622294d8699f5637e327
Parents: 53dab80
Author: maryannxue <wei....@intel.com>
Authored: Tue Jun 2 15:14:43 2015 -0400
Committer: maryannxue <wei....@intel.com>
Committed: Tue Jun 2 15:14:43 2015 -0400

----------------------------------------------------------------------
 .../org/apache/phoenix/calcite/CalciteTest.java |  4 ++--
 .../apache/phoenix/calcite/PhoenixSchema.java   |  3 +++
 .../calcite/metadata/PhoenixRelMdRowCount.java  |  7 +-----
 .../calcite/rel/PhoenixAbstractProject.java     | 12 ++++++++++
 .../phoenix/calcite/rel/PhoenixClientJoin.java  | 24 ++++++++++++--------
 .../phoenix/calcite/rel/PhoenixServerJoin.java  | 21 +++++++++--------
 6 files changed, 44 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/cedc1c50/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteTest.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteTest.java 
b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteTest.java
index acb02f3..89006ed 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteTest.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteTest.java
@@ -623,8 +623,8 @@ public class CalciteTest extends BaseClientManagedTimeIT {
         
         start().sql("SELECT item.\"item_id\", item.name, supp.\"supplier_id\", 
supp.name FROM " + JOIN_ITEM_TABLE_FULL_NAME + " item JOIN " + 
JOIN_SUPPLIER_TABLE_FULL_NAME + " supp ON item.\"supplier_id\" = 
supp.\"supplier_id\" limit 3")
                 .explainIs("PhoenixToEnumerableConverter\n" +
-                           "  PhoenixClientProject(item_id=[$0], NAME=[$1], 
supplier_id=[$3], NAME0=[$4])\n" +
-                           "    PhoenixLimit(fetch=[3])\n" +
+                           "  PhoenixLimit(fetch=[3])\n" +
+                           "    PhoenixServerProject(item_id=[$0], NAME=[$1], 
supplier_id=[$3], NAME0=[$4])\n" +
                            "      PhoenixServerJoin(condition=[=($2, $3)], 
joinType=[inner])\n" +
                            "        PhoenixServerProject(item_id=[$0], 
NAME=[$1], supplier_id=[$5])\n" +
                            "          PhoenixTableScan(table=[[phoenix, Join, 
ItemTable]])\n" +

http://git-wip-us.apache.org/repos/asf/phoenix/blob/cedc1c50/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java
index 589f61d..5fb407b 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java
@@ -82,11 +82,14 @@ public class PhoenixSchema implements Schema {
             properties.setProperty(entry.getKey(), 
String.valueOf(entry.getValue()));
         }
         try {
+            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
             final Connection connection =
                 DriverManager.getConnection(url, properties);
             final PhoenixConnection phoenixConnection =
                 connection.unwrap(PhoenixConnection.class);
             return new PhoenixSchema(null, phoenixConnection);
+        } catch (ClassNotFoundException e) {
+            throw new RuntimeException(e);
         } catch (SQLException e) {
             throw new RuntimeException(e);
         }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/cedc1c50/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdRowCount.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdRowCount.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdRowCount.java
index a9b5274..23108b2 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdRowCount.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdRowCount.java
@@ -7,7 +7,6 @@ import org.apache.calcite.rel.metadata.RelMetadataQuery;
 import org.apache.calcite.util.BuiltInMethod;
 import org.apache.calcite.util.ImmutableBitSet;
 import org.apache.phoenix.calcite.rel.PhoenixAbstractAggregate;
-import org.apache.phoenix.calcite.rel.PhoenixAbstractSort;
 import org.apache.phoenix.calcite.rel.PhoenixLimit;
 
 public class PhoenixRelMdRowCount {
@@ -34,11 +33,7 @@ public class PhoenixRelMdRowCount {
         }
     }
     
-    public Double getRowCount(PhoenixAbstractSort rel) {
-        return rel.getRows();
-      }
-    
     public Double getRowCount(PhoenixLimit rel) {
         return rel.getRows();
-      }
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/cedc1c50/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractProject.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractProject.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractProject.java
index 9950605..893dcd1 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractProject.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractProject.java
@@ -3,9 +3,12 @@ package org.apache.phoenix.calcite.rel;
 import java.util.List;
 
 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.RelNode;
 import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rex.RexNode;
 import org.apache.phoenix.calcite.CalciteUtils;
@@ -25,6 +28,15 @@ abstract public class PhoenixAbstractProject extends Project 
implements PhoenixR
         super(cluster, traits, input, projects, rowType);
         assert getConvention() == PhoenixRel.CONVENTION;
     }
+
+    @Override
+    public RelOptCost computeSelfCost(RelOptPlanner planner) {
+        // This is to minimize the weight of cost of Project so that it
+        // does not affect more important decisions like join algorithms.
+        double rowCount = RelMetadataQuery.getRowCount(getInput());
+        double rows = 2 * rowCount / (rowCount + 1);
+        return planner.getCostFactory().makeCost(rows, 0, 0);
+    }
     
     protected TupleProjector project(Implementor implementor) {        
         List<Expression> exprs = Lists.newArrayList();

http://git-wip-us.apache.org/repos/asf/phoenix/blob/cedc1c50/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 2acd11f..7bc0b5e 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
@@ -13,8 +13,8 @@ import org.apache.calcite.rel.RelCollation;
 import org.apache.calcite.rel.RelCollationTraitDef;
 import org.apache.calcite.rel.RelCollations;
 import org.apache.calcite.rel.RelFieldCollation;
-import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.RelFieldCollation.Direction;
+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.metadata.RelMetadataQuery;
@@ -26,11 +26,11 @@ import 
org.apache.phoenix.calcite.metadata.PhoenixRelMdCollation;
 import org.apache.phoenix.compile.ColumnResolver;
 import org.apache.phoenix.compile.FromCompiler;
 import org.apache.phoenix.compile.JoinCompiler;
+import org.apache.phoenix.compile.OrderByCompiler.OrderBy;
 import org.apache.phoenix.compile.QueryPlan;
 import org.apache.phoenix.compile.RowProjector;
 import org.apache.phoenix.compile.SequenceManager;
 import org.apache.phoenix.compile.StatementContext;
-import org.apache.phoenix.compile.OrderByCompiler.OrderBy;
 import org.apache.phoenix.execute.ClientScanPlan;
 import org.apache.phoenix.execute.SortMergeJoinPlan;
 import org.apache.phoenix.expression.Expression;
@@ -102,16 +102,20 @@ public class PhoenixClientJoin extends 
PhoenixAbstractJoin {
 
     @Override
     public RelOptCost computeSelfCost(RelOptPlanner planner) {
-        double rowCount = RelMetadataQuery.getRowCount(this);
-        
-        for (RelNode input : getInputs()) {
-            double inputRowCount = RelMetadataQuery.getRowCount(input);
-            if (Double.isInfinite(inputRowCount)) {
-                rowCount = inputRowCount;
+        double rowCount = RelMetadataQuery.getRowCount(this);        
+
+        double leftRowCount = RelMetadataQuery.getRowCount(getLeft());
+        if (Double.isInfinite(leftRowCount)) {
+            rowCount = leftRowCount;
+        } else {
+            rowCount += leftRowCount;
+            double rightRowCount = RelMetadataQuery.getRowCount(getRight());
+            if (Double.isInfinite(rightRowCount)) {
+                rowCount = rightRowCount;
             } else {
-                rowCount += inputRowCount;
+                rowCount += rightRowCount;
             }
-        }
+        }            
         RelOptCost cost = planner.getCostFactory().makeCost(rowCount, 0, 0);
 
         return cost.multiplyBy(PHOENIX_FACTOR);

http://git-wip-us.apache.org/repos/asf/phoenix/blob/cedc1c50/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 f73527a..32ec08b 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
@@ -74,17 +74,20 @@ public class PhoenixServerJoin extends PhoenixAbstractJoin {
         //TODO return infinite cost if RHS size exceeds memory limit.
         
         double rowCount = RelMetadataQuery.getRowCount(this);
-        
-        for (RelNode input : getInputs()) {
-            double inputRowCount = RelMetadataQuery.getRowCount(input);
-            if (Double.isInfinite(inputRowCount)) {
-                rowCount = inputRowCount;
-            } else if (input == getLeft()) {
-                rowCount += inputRowCount;
+
+        double leftRowCount = RelMetadataQuery.getRowCount(getLeft());
+        if (Double.isInfinite(leftRowCount)) {
+            rowCount = leftRowCount;
+        } else {
+            rowCount += leftRowCount;
+            double rightRowCount = RelMetadataQuery.getRowCount(getRight());
+            if (Double.isInfinite(rightRowCount)) {
+                rowCount = rightRowCount;
             } else {
-                rowCount += Util.nLogN(inputRowCount);
+                rowCount += Util.nLogN(rightRowCount);
             }
-        }
+        }            
+        
         RelOptCost cost = planner.getCostFactory().makeCost(rowCount, 0, 0);
 
         return cost.multiplyBy(SERVER_FACTOR).multiplyBy(PHOENIX_FACTOR);

Reply via email to