You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ja...@apache.org on 2018/03/09 23:21:56 UTC

[3/4] phoenix git commit: PHOENIX-1267 Set scan.setSmall(true) when appropriate (Abhishek Singh Chouhan)

PHOENIX-1267 Set scan.setSmall(true) when appropriate (Abhishek Singh Chouhan)


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

Branch: refs/heads/master
Commit: abfc1ff4d37d67eb4666c4c7db24cb22f041768c
Parents: 45f856a
Author: James Taylor <jt...@salesforce.com>
Authored: Thu Mar 8 14:38:28 2018 -0800
Committer: James Taylor <jt...@salesforce.com>
Committed: Fri Mar 9 15:19:20 2018 -0800

----------------------------------------------------------------------
 .../apache/phoenix/execute/BaseQueryPlan.java   | 16 +++++++++---
 .../org/apache/phoenix/query/QueryServices.java |  1 +
 .../phoenix/query/QueryServicesOptions.java     |  1 +
 .../phoenix/compile/QueryCompilerTest.java      | 26 ++++++++++++++++++++
 4 files changed, 40 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/abfc1ff4/phoenix-core/src/main/java/org/apache/phoenix/execute/BaseQueryPlan.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/execute/BaseQueryPlan.java b/phoenix-core/src/main/java/org/apache/phoenix/execute/BaseQueryPlan.java
index 0bc606e..b152030 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/execute/BaseQueryPlan.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/execute/BaseQueryPlan.java
@@ -63,6 +63,8 @@ import org.apache.phoenix.parse.HintNode.Hint;
 import org.apache.phoenix.parse.ParseNodeFactory;
 import org.apache.phoenix.parse.TableName;
 import org.apache.phoenix.query.QueryConstants;
+import org.apache.phoenix.query.QueryServices;
+import org.apache.phoenix.query.QueryServicesOptions;
 import org.apache.phoenix.schema.KeyValueSchema;
 import org.apache.phoenix.schema.PColumn;
 import org.apache.phoenix.schema.PName;
@@ -235,12 +237,14 @@ public abstract class BaseQueryPlan implements QueryPlan {
              scan = context.getScan();
          }
          
+         ScanRanges scanRanges = context.getScanRanges();
+
 		/*
 		 * For aggregate queries, we still need to let the AggregationPlan to
 		 * proceed so that we can give proper aggregates even if there are no
 		 * row to be scanned.
 		 */
-        if (context.getScanRanges() == ScanRanges.NOTHING && !getStatement().isAggregate()) {
+        if (scanRanges == ScanRanges.NOTHING && !getStatement().isAggregate()) {
         return getWrappedIterator(caches, ResultIterator.EMPTY_ITERATOR);
         }
         
@@ -267,11 +271,15 @@ public abstract class BaseQueryPlan implements QueryPlan {
             }
         }
         
-        if (statement.getHint().hasHint(Hint.SMALL)) {
+
+        PhoenixConnection connection = context.getConnection();
+        final int smallScanThreshold = connection.getQueryServices().getProps().getInt(QueryServices.SMALL_SCAN_THRESHOLD_ATTRIB,
+          QueryServicesOptions.DEFAULT_SMALL_SCAN_THRESHOLD);
+
+        if (statement.getHint().hasHint(Hint.SMALL) || (scanRanges.isPointLookup() && scanRanges.getPointLookupCount() < smallScanThreshold)) {
             scan.setSmall(true);
         }
         
-        PhoenixConnection connection = context.getConnection();
 
         // set read consistency
         if (table.getType() != PTableType.SYSTEM) {
@@ -280,7 +288,7 @@ public abstract class BaseQueryPlan implements QueryPlan {
         // TODO fix this in PHOENIX-2415 Support ROW_TIMESTAMP with transactional tables
         if (!table.isTransactional()) {
 	                // Get the time range of row_timestamp column
-	        TimeRange rowTimestampRange = context.getScanRanges().getRowTimestampRange();
+	        TimeRange rowTimestampRange = scanRanges.getRowTimestampRange();
 	        // Get the already existing time range on the scan.
 	        TimeRange scanTimeRange = scan.getTimeRange();
 	        Long scn = connection.getSCN();

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abfc1ff4/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
index 0979e36..0b18aaa 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
@@ -300,6 +300,7 @@ public interface QueryServices extends SQLCloseable {
 
     // Whether to enable cost-based-decision in the query optimizer
     public static final String COST_BASED_OPTIMIZER_ENABLED = "phoenix.costbased.optimizer.enabled";
+    public static final String SMALL_SCAN_THRESHOLD_ATTRIB = "phoenix.query.smallScanThreshold";
 
     /**
      * Get executor service used for parallel scans

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abfc1ff4/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
index 8dadbb0..961ab9f 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
@@ -327,6 +327,7 @@ public class QueryServicesOptions {
 
     //default update cache frequency
     public static final int DEFAULT_UPDATE_CACHE_FREQUENCY = 0;
+    public static final int DEFAULT_SMALL_SCAN_THRESHOLD = 100;
 
     @SuppressWarnings("serial")
     public static final Set<String> DEFAULT_QUERY_SERVER_SKIP_WORDS = new HashSet<String>() {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abfc1ff4/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
index 9386bd2..2b5a8cb 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
@@ -4483,6 +4483,32 @@ public class QueryCompilerTest extends BaseConnectionlessQueryTest {
     }
 
     @Test
+    public void testSmallScanForPointLookups() throws SQLException {
+        Properties props = PropertiesUtil.deepCopy(new Properties());
+        createTestTable(getUrl(), "CREATE TABLE FOO(\n" +
+                      "                a VARCHAR NOT NULL,\n" +
+                      "                b VARCHAR NOT NULL,\n" +
+                      "                c VARCHAR,\n" +
+                      "                CONSTRAINT pk PRIMARY KEY (a, b DESC, c)\n" +
+                      "              )");
+
+        props.put(QueryServices.SMALL_SCAN_THRESHOLD_ATTRIB, "2");
+        try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+            String query = "select * from foo where a = 'a' and b = 'b' and c in ('x','y','z')";
+            PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
+            QueryPlan plan = stmt.optimizeQuery(query);
+            plan.iterator();
+            //Fail since we have 3 rows in pointLookup
+            assertFalse(plan.getContext().getScan().isSmall());
+            query = "select * from foo where a = 'a' and b = 'b' and c = 'c'";
+            plan = stmt.compileQuery(query);
+            plan.iterator();
+            //Should be small scan, query is for single row pointLookup
+            assertTrue(plan.getContext().getScan().isSmall());
+        }
+    }
+
+    @Test
     public void testLocalIndexPruningInSortMergeJoin() throws SQLException {
         verifyLocalIndexPruningWithMultipleTables("SELECT /*+ USE_SORT_MERGE_JOIN*/ *\n" +
                 "FROM T1 JOIN T2 ON T1.A = T2.A\n" +