This is an automated email from the ASF dual-hosted git repository.

stoty pushed a commit to branch 5.1
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/5.1 by this push:
     new 42737e0551 PHOENIX-6669 RVC where optimization scan merge creates 
incorrect results
42737e0551 is described below

commit 42737e05517b461577314c8d48d88b7762371d85
Author: Gokcen Iskender <gokc...@gmail.com>
AuthorDate: Mon Mar 21 12:27:16 2022 -0700

    PHOENIX-6669 RVC where optimization scan merge creates incorrect results
    
    Signed-off-by: Gokcen Iskender <gokc...@gmail.com>
---
 .../apache/phoenix/end2end/SkipScanQueryIT.java    | 32 ++++++++++++++++++++++
 .../apache/phoenix/expression/BaseExpression.java  | 14 +++++++++-
 .../apache/phoenix/compile/WhereOptimizerTest.java |  4 +--
 3 files changed, 47 insertions(+), 3 deletions(-)

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 68abb0df7a..45a1a56d79 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
@@ -966,6 +966,38 @@ public class SkipScanQueryIT extends 
ParallelStatsDisabledIT {
 
     }
 
+    @Test
+    public void testPHOENIX6669() throws SQLException {
+        String tableName = generateUniqueName();
+        String ddl = "CREATE TABLE IF NOT EXISTS "+ tableName + " (\n" +
+                "    PK1 VARCHAR NOT NULL,\n" +
+                "    PK2 BIGINT NOT NULL,\n" +
+                "    PK3 BIGINT NOT NULL,\n" +
+                "    PK4 VARCHAR NOT NULL,\n" +
+                "    COL1 BIGINT,\n" +
+                "    COL2 INTEGER,\n" +
+                "    COL3 VARCHAR,\n" +
+                "    COL4 VARCHAR,    CONSTRAINT PK PRIMARY KEY\n" +
+                "    (\n" +
+                "        PK1,\n" +
+                "        PK2,\n" +
+                "        PK3,\n" +
+                "        PK4\n" +
+                "    )\n" +
+                ")";
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            conn.createStatement().execute(ddl);
+            conn.createStatement().execute("UPSERT INTO " + tableName + " 
(PK1, PK4, COL1, PK2, COL2, PK3, COL3, COL4)" +
+                    "            VALUES ('xx', 'xid1', 0, 7, 7, 7, 'INSERT', 
null)");
+            conn.commit();
+
+            ResultSet rs = conn.createStatement().executeQuery("select PK2 
from "+  tableName
+                    +  " where (PK1 = 'xx') and (PK1, PK2, PK3) > ('xx', 5, 2) 
"
+                    +  " and (PK1, PK2, PK3) <= ('xx', 5, 2)");
+            assertFalse(rs.next());
+        }
+    }
+
     @Test
     public void testKeyRangesContainsAllValues() throws Exception {
         String tableName = generateUniqueName();
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/expression/BaseExpression.java 
b/phoenix-core/src/main/java/org/apache/phoenix/expression/BaseExpression.java
index a9e11c0d1d..cf601dc0d8 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/expression/BaseExpression.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/expression/BaseExpression.java
@@ -226,9 +226,21 @@ public abstract class BaseExpression implements Expression 
{
         if (iterator == null) {
             iterator = visitor.defaultIterator(this);
         }
-        List<T> l = Collections.emptyList();
+
+        // PHOENIX-6669 Sort RVCs together and first so that where optimizer 
intersectrages work correctly
+        List<Expression> children = new ArrayList<>();
         while (iterator.hasNext()) {
             Expression child = iterator.next();
+            if (child != null && child.getChildren() != null && 
child.getChildren().size() > 1 &&
+                    child.getChildren().get(1) instanceof 
RowValueConstructorExpression) {
+                children.add(0, child);
+            } else {
+                children.add(child);
+            }
+        }
+
+        List<T> l = Collections.emptyList();
+        for (Expression child : children) {
             T t = child.accept(visitor);
             if (t != null) {
                 if (l.isEmpty()) {
diff --git 
a/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereOptimizerTest.java 
b/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereOptimizerTest.java
index 4b0dbb3274..d50310821b 100644
--- 
a/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereOptimizerTest.java
+++ 
b/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereOptimizerTest.java
@@ -3317,8 +3317,8 @@ public class WhereOptimizerTest extends 
BaseConnectionlessQueryTest {
             assertTrue(filterList.getFilters().get(1) instanceof 
RowKeyComparisonFilter);
             rowKeyComparisonFilter =(RowKeyComparisonFilter) 
filterList.getFilters().get(1);
             assertTrue(rowKeyComparisonFilter.toString().equals(
-                    "((TO_INTEGER(PK3), PK4) < (TO_INTEGER(TO_INTEGER(3)), 4) 
AND "+
-                    "(PK5, TO_INTEGER(PK6), PK7) < (5, 
TO_INTEGER(TO_INTEGER(6)), 7))"));
+                    "((PK5, TO_INTEGER(PK6), PK7) < (5, 
TO_INTEGER(TO_INTEGER(6)), 7) AND " +
+                            "(TO_INTEGER(PK3), PK4) < 
(TO_INTEGER(TO_INTEGER(3)), 4))"));
         }
     }
 

Reply via email to