phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From la...@apache.org
Subject phoenix git commit: PHOENIX-4964 ORDER BY should use a LOCAL index even if the query is not fully covered.
Date Fri, 12 Oct 2018 05:47:32 GMT
Repository: phoenix
Updated Branches:
  refs/heads/4.x-HBase-1.3 dd3e55f10 -> ed183eff0


PHOENIX-4964 ORDER BY should use a LOCAL index even if the query is not fully covered.


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

Branch: refs/heads/4.x-HBase-1.3
Commit: ed183eff0003c3be5d0f2a0b581a1f20f3b1857b
Parents: dd3e55f
Author: Lars Hofhansl <larsh@apache.org>
Authored: Thu Oct 11 22:49:07 2018 -0700
Committer: Lars Hofhansl <larsh@apache.org>
Committed: Thu Oct 11 22:49:07 2018 -0700

----------------------------------------------------------------------
 .../phoenix/end2end/index/LocalIndexIT.java     | 59 ++++++++++++++++++++
 .../apache/phoenix/optimize/QueryOptimizer.java |  9 ++-
 2 files changed, 66 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/ed183eff/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java
index e260969..5a59c81 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java
@@ -266,6 +266,65 @@ public class LocalIndexIT extends BaseLocalIndexIT {
         }
         indexTable.close();
     }
+
+    @Test
+    public void testLocalIndexUsedForUncoveredOrderBy() throws Exception {
+        String tableName = schemaName + "." + generateUniqueName();
+        String indexName = "IDX_" + generateUniqueName();
+        TableName physicalTableName = SchemaUtil.getPhysicalTableName(tableName.getBytes(),
isNamespaceMapped);
+        String indexPhysicalTableName = physicalTableName.getNameAsString();
+
+        createBaseTable(tableName, null, "('e','i','o')");
+        try (Connection conn1 = getConnection()) {
+            conn1.createStatement().execute("UPSERT INTO " + tableName + " values('b',1,2,4,'z')");
+            conn1.createStatement().execute("UPSERT INTO " + tableName + " values('f',1,2,3,'a')");
+            conn1.createStatement().execute("UPSERT INTO " + tableName + " values('j',2,4,2,'a')");
+            conn1.createStatement().execute("UPSERT INTO " + tableName + " values('q',3,1,1,'c')");
+            conn1.commit();
+            conn1.createStatement().execute("CREATE LOCAL INDEX " + indexName + " ON " +
tableName + "(v1)");
+
+            String query = "SELECT * FROM " + tableName +" ORDER BY V1";
+            ResultSet rs = conn1.createStatement().executeQuery("EXPLAIN "+ query);
+
+            HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
+            int numRegions = admin.getTableRegions(physicalTableName).size();
+
+            assertEquals(
+                "CLIENT PARALLEL " + numRegions + "-WAY RANGE SCAN OVER "
+                        + indexPhysicalTableName + " [1]\n"
+                                + "    SERVER FILTER BY FIRST KEY ONLY\n"
+                                + "CLIENT MERGE SORT",
+                        QueryUtil.getExplainPlan(rs));
+
+            rs = conn1.createStatement().executeQuery(query);
+            String v = "";
+            while(rs.next()) {
+                String next = rs.getString("v1");
+                assertTrue(v.compareTo(next) <= 0);
+                v = next;
+            }
+            rs.close();
+
+            query = "SELECT * FROM " + tableName +" ORDER BY V1 DESC NULLS LAST";
+            rs = conn1.createStatement().executeQuery("EXPLAIN "+ query);
+            assertEquals(
+                "CLIENT PARALLEL " + numRegions + "-WAY REVERSE RANGE SCAN OVER "
+                        + indexPhysicalTableName + " [1]\n"
+                                + "    SERVER FILTER BY FIRST KEY ONLY\n"
+                                + "CLIENT MERGE SORT",
+                        QueryUtil.getExplainPlan(rs));
+
+            rs = conn1.createStatement().executeQuery(query);
+            v = "zz";
+            while(rs.next()) {
+                String next = rs.getString("v1");
+                assertTrue(v.compareTo(next) >= 0);
+                v = next;
+            }
+            rs.close();
+
+        }
+    }
     
     @Test
     public void testLocalIndexScanJoinColumnsFromDataTable() throws Exception {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ed183eff/phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java b/phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java
index 6d668cc..4595a36 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java
@@ -322,13 +322,18 @@ public class QueryOptimizer {
                 QueryCompiler compiler = new QueryCompiler(statement, indexSelect, resolver,
targetColumns, parallelIteratorFactory, dataPlan.getContext().getSequenceManager(), isProjected,
true, dataPlans);
                 
                 QueryPlan plan = compiler.compile();
+
+                boolean optimizedOrderBy = plan.getOrderBy().getOrderByExpressions().isEmpty()
&&
+                        !dataPlan.getOrderBy().getOrderByExpressions().isEmpty();
+
                 // If query doesn't have where clause and some of columns to project are
missing
                 // in the index then we need to get missing columns from main table for each
row in
                 // local index. It's like full scan of both local index and data table which
is inefficient.
                 // Then we don't use the index. If all the columns to project are present
in the index 
-                // then we can use the index even the query doesn't have where clause. 
+                // then we can use the index even the query doesn't have where clause.
+                // We'll use the index anyway if it allowed us to optimize an ORDER BY clause
away.
                 if (index.getIndexType() == IndexType.LOCAL && indexSelect.getWhere()
== null
-                        && !plan.getContext().getDataColumns().isEmpty()) {
+                        && !plan.getContext().getDataColumns().isEmpty() &&
!optimizedOrderBy) {
                     return null;
                 }
                 indexTableRef = plan.getTableRef();


Mime
View raw message