PHOENIX-4692 ArrayIndexOutOfBoundsException in ScanRanges.intersectScan

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

Branch: refs/heads/4.x-HBase-1.4
Commit: 28b9de0da01b61e61c749ed433ddb995596b3e45
Parents: 58415e2
Author: maryannxue <maryann....@gmail.com>
Authored: Fri May 18 19:46:29 2018 -0700
Committer: maryannxue <maryann....@gmail.com>
Committed: Fri May 18 19:46:29 2018 -0700

----------------------------------------------------------------------
 .../apache/phoenix/end2end/SkipScanQueryIT.java | 21 ++++++++++++++++++++
 .../apache/phoenix/compile/WhereCompiler.java   | 12 +++++------
 .../apache/phoenix/execute/BaseQueryPlan.java   |  2 +-
 .../apache/phoenix/execute/HashJoinPlan.java    |  5 ++++-
 4 files changed, 32 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/28b9de0d/phoenix-core/src/it/java/org/apache/phoenix/end2end/SkipScanQueryIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SkipScanQueryIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SkipScanQueryIT.java
index d98bbe2..fb0b568 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SkipScanQueryIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SkipScanQueryIT.java
@@ -563,4 +563,25 @@ public class SkipScanQueryIT extends 
ParallelStatsDisabledIT {
             assertFalse(rs.next());
         }
     }
+
+    @Test
+    public void testSkipScanJoinOptimization() throws Exception {
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            String tableName = generateUniqueName();
+            String viewName = generateUniqueName();
+            String idxName = "IDX_" + tableName;
+            conn.setAutoCommit(true);
+            conn.createStatement().execute(
+                    "create table " + tableName + " (PK1 INTEGER NOT NULL, PK2 
INTEGER NOT NULL, " +
+                            " ID1 INTEGER, ID2 INTEGER CONSTRAINT PK PRIMARY 
KEY(PK1 , PK2))SALT_BUCKETS = 4");
+            conn.createStatement().execute("upsert into " + tableName + " 
values (1,1,1,1)");
+            conn.createStatement().execute("upsert into " + tableName + " 
values (2,2,2,2)");
+            conn.createStatement().execute("upsert into " + tableName + " 
values (2,3,1,2)");
+            conn.createStatement().execute("create view " + viewName + " as 
select * from " +
+                    tableName + " where PK1 in (1,2)");
+            conn.createStatement().execute("create index " + idxName + " on " 
+ viewName + " (ID1)");
+            ResultSet rs = conn.createStatement().executeQuery("select /*+ 
INDEX(" + viewName + " " + idxName + ") */ * from " + viewName + " where ID1 = 
1 ");
+            assertTrue(rs.next());
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/28b9de0d/phoenix-core/src/main/java/org/apache/phoenix/compile/WhereCompiler.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/compile/WhereCompiler.java 
b/phoenix-core/src/main/java/org/apache/phoenix/compile/WhereCompiler.java
index 2cf5857..832b1f0 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/WhereCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/WhereCompiler.java
@@ -105,9 +105,9 @@ public class WhereCompiler {
      * @throws AmbiguousColumnException if an unaliased column name is 
ambiguous across multiple tables
      */
     public static Expression compile(StatementContext context, 
FilterableStatement statement, ParseNode viewWhere, Set<SubqueryParseNode> 
subqueryNodes) throws SQLException {
-        return compile(context, statement, viewWhere, 
Collections.<Expression>emptyList(), false, subqueryNodes);
+        return compile(context, statement, viewWhere, 
Collections.<Expression>emptyList(), subqueryNodes);
     }
-    
+
     /**
      * Optimize scan ranges by applying dynamically generated filter 
expressions.
      * @param context the shared context during query compilation
@@ -118,7 +118,7 @@ public class WhereCompiler {
      * @throws ColumnNotFoundException if column name could not be resolved
      * @throws AmbiguousColumnException if an unaliased column name is 
ambiguous across multiple tables
      */    
-    public static Expression compile(StatementContext context, 
FilterableStatement statement, ParseNode viewWhere, List<Expression> 
dynamicFilters, boolean hashJoinOptimization, Set<SubqueryParseNode> 
subqueryNodes) throws SQLException {
+    public static Expression compile(StatementContext context, 
FilterableStatement statement, ParseNode viewWhere, List<Expression> 
dynamicFilters, Set<SubqueryParseNode> subqueryNodes) throws SQLException {
         ParseNode where = statement.getWhere();
         if (subqueryNodes != null) { // if the subqueryNodes passed in is 
null, we assume there will be no sub-queries in the WHERE clause.
             SubqueryParseNodeVisitor subqueryVisitor = new 
SubqueryParseNodeVisitor(context, subqueryNodes);
@@ -156,7 +156,7 @@ public class WhereCompiler {
         if (context.getCurrentTable().getTable().getType() != 
PTableType.PROJECTED && context.getCurrentTable().getTable().getType() != 
PTableType.SUBQUERY) {
             expression = WhereOptimizer.pushKeyExpressionsToScan(context, 
statement, expression, extractedNodes);
         }
-        setScanFilter(context, statement, expression, 
whereCompiler.disambiguateWithFamily, hashJoinOptimization);
+        setScanFilter(context, statement, expression, 
whereCompiler.disambiguateWithFamily);
 
         return expression;
     }
@@ -249,12 +249,12 @@ public class WhereCompiler {
      * @param context the shared context during query compilation
      * @param whereClause the final where clause expression.
      */
-    private static void setScanFilter(StatementContext context, 
FilterableStatement statement, Expression whereClause, boolean 
disambiguateWithFamily, boolean hashJoinOptimization) {
+    private static void setScanFilter(StatementContext context, 
FilterableStatement statement, Expression whereClause, boolean 
disambiguateWithFamily) {
         Scan scan = context.getScan();
 
         if (LiteralExpression.isBooleanFalseOrNull(whereClause)) {
             context.setScanRanges(ScanRanges.NOTHING);
-        } else if (whereClause != null && 
!ExpressionUtil.evaluatesToTrue(whereClause) && !hashJoinOptimization) {
+        } else if (whereClause != null && 
!ExpressionUtil.evaluatesToTrue(whereClause)) {
             Filter filter = null;
             final Counter counter = new Counter();
             whereClause.accept(new KeyValueExpressionVisitor() {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/28b9de0d/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 8a8c822..25980b9 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
@@ -261,7 +261,7 @@ public abstract class BaseQueryPlan implements QueryPlan {
         PTable table = tableRef.getTable();
         
         if (dynamicFilter != null) {
-            WhereCompiler.compile(context, statement, null, 
Collections.singletonList(dynamicFilter), false, null);            
+            WhereCompiler.compile(context, statement, null, 
Collections.singletonList(dynamicFilter), null);
         }
         
         if (OrderBy.REV_ROW_KEY_ORDER_BY.equals(orderBy)) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/28b9de0d/phoenix-core/src/main/java/org/apache/phoenix/execute/HashJoinPlan.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/execute/HashJoinPlan.java 
b/phoenix-core/src/main/java/org/apache/phoenix/execute/HashJoinPlan.java
index 6ade42e..5b433b3 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/execute/HashJoinPlan.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/execute/HashJoinPlan.java
@@ -216,6 +216,9 @@ public class HashJoinPlan extends DelegateQueryPlan {
         boolean hasKeyRangeExpressions = keyRangeExpressions != null && 
!keyRangeExpressions.isEmpty();
         if (recompileWhereClause || hasKeyRangeExpressions) {
             StatementContext context = delegate.getContext();
+            // Since we are going to compile the WHERE conditions all over 
again, we will clear
+            // the old filter, otherwise there would be conflicts and would 
cause PHOENIX-4692.
+            context.getScan().setFilter(null);
             PTable table = context.getCurrentTable().getTable();
             ParseNode viewWhere = table.getViewStatement() == null ? null : 
new SQLParser(table.getViewStatement()).parseQuery().getWhere();
             
context.setResolver(FromCompiler.getResolverForQuery((SelectStatement) 
(delegate.getStatement()), delegate.getContext().getConnection()));
@@ -223,7 +226,7 @@ public class HashJoinPlan extends DelegateQueryPlan {
                 postFilter = WhereCompiler.compile(delegate.getContext(), 
delegate.getStatement(), viewWhere, null);
             }
             if (hasKeyRangeExpressions) {
-                WhereCompiler.compile(delegate.getContext(), 
delegate.getStatement(), viewWhere, keyRangeExpressions, true, null);
+                WhereCompiler.compile(delegate.getContext(), 
delegate.getStatement(), viewWhere, keyRangeExpressions, null);
             }
         }
 

Reply via email to