Repository: phoenix
Updated Branches:
  refs/heads/calcite 216010dfd -> ca91c5d9a


Add epsilon to make cost stable between data table plans and index table plans


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

Branch: refs/heads/calcite
Commit: ca91c5d9aadffc0947f908f91992f0b0276d5a9b
Parents: 216010d
Author: maryannxue <wei....@intel.com>
Authored: Thu Jul 23 10:12:19 2015 -0400
Committer: maryannxue <wei....@intel.com>
Committed: Thu Jul 23 10:12:19 2015 -0400

----------------------------------------------------------------------
 .../org/apache/phoenix/calcite/CalciteTest.java | 15 +++++-----
 .../phoenix/calcite/rel/PhoenixTableScan.java   | 31 ++++++++++++++++++--
 2 files changed, 36 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/ca91c5d9/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 08d272f..1d11c20 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
@@ -255,12 +255,19 @@ public class CalciteTest extends BaseClientManagedTimeIT {
         ensureTableCreated(url, ATABLE_NAME);
         initATableValues(getOrganizationId(), null, url);
         initJoinTableValues(url, null, null);
+        createIndices(
+                "CREATE INDEX IDX1 ON aTable (a_string) INCLUDE (b_string, 
x_integer)",
+                "CREATE INDEX IDX2 ON aTable (b_string) INCLUDE (a_string, 
y_integer)",
+                "CREATE INDEX IDX_FULL ON aTable (b_string) INCLUDE (a_string, 
a_integer, a_date, a_time, a_timestamp, x_decimal, x_long, x_integer, 
y_integer, a_byte, a_short, a_float, a_double, a_unsigned_float, 
a_unsigned_double)");
         final Connection connection = DriverManager.getConnection(url);
         connection.createStatement().execute("UPDATE STATISTICS ATABLE");
         connection.createStatement().execute("UPDATE STATISTICS " + 
JOIN_CUSTOMER_TABLE_FULL_NAME);
         connection.createStatement().execute("UPDATE STATISTICS " + 
JOIN_ITEM_TABLE_FULL_NAME);
         connection.createStatement().execute("UPDATE STATISTICS " + 
JOIN_SUPPLIER_TABLE_FULL_NAME);
         connection.createStatement().execute("UPDATE STATISTICS " + 
JOIN_ORDER_TABLE_FULL_NAME);
+        connection.createStatement().execute("UPDATE STATISTICS IDX1");
+        connection.createStatement().execute("UPDATE STATISTICS IDX2");
+        connection.createStatement().execute("UPDATE STATISTICS IDX_FULL");
         connection.close();
     }
     
@@ -851,14 +858,6 @@ public class CalciteTest extends BaseClientManagedTimeIT {
     }
     
     @Test public void testIndex() {
-        try {
-            createIndices(
-                    "CREATE INDEX IDX1 ON aTable (a_string) INCLUDE (b_string, 
x_integer)",
-                    "CREATE INDEX IDX2 ON aTable (b_string) INCLUDE (a_string, 
y_integer)",
-                    "CREATE INDEX IDX_FULL ON aTable (b_string) INCLUDE 
(a_string, a_integer, a_date, a_time, a_timestamp, x_decimal, x_long, 
x_integer, y_integer, a_byte, a_short, a_float, a_double, a_unsigned_float, 
a_unsigned_double)");
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
         final Start start = start(getConnectionProps(true), false);
         start.sql("select x_integer from aTable")
             .explainIs("PhoenixToEnumerableConverter\n" +

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ca91c5d9/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 2e11c09..b8e97ed 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
@@ -120,14 +120,20 @@ public class PhoenixTableScan extends TableScan 
implements PhoenixRel {
     @Override
     public RelOptCost computeSelfCost(RelOptPlanner planner) {
         double rowCount = super.getRows();
+        Double filteredRowCount = null;
         if (scanRanges != null) {
             if (scanRanges.isPointLookup()) {
-                rowCount = 1;
+                filteredRowCount = 1.0;
             } else if (scanRanges.getPkColumnSpan() > 0) {
                 // TODO
-                rowCount = rowCount * RelMetadataQuery.getSelectivity(this, 
filter);
+                filteredRowCount = rowCount * 
RelMetadataQuery.getSelectivity(this, filter);
             }
         }
+        if (filteredRowCount != null) {
+            rowCount = filteredRowCount;
+        } else if (table.unwrap(PhoenixTable.class).getTable().getParentName() 
!= null){
+            rowCount = addEpsilon(rowCount);
+        }
         int fieldCount = this.table.getRowType().getFieldCount();
         return planner.getCostFactory()
                 .makeCost(rowCount * 2 * fieldCount / (fieldCount + 1), 
rowCount + 1, 0)
@@ -199,4 +205,25 @@ public class PhoenixTableScan extends TableScan implements 
PhoenixRel {
             scan.addFamily(family.getName().getBytes());
         }
     }
+
+    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;
+    }
 }

Reply via email to