You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by go...@apache.org on 2022/03/22 16:19:05 UTC

[phoenix] branch master updated: PHOENIX-6669 RVC where optimization scan merge creates incorrect results

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

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


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

commit f91f8ad896d53c85fd2765f0225163ec886c53f5
Author: Gokcen Iskender <go...@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 <go...@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 68abb0d..45a1a56 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
@@ -967,6 +967,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();
         String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + "(" +
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 a9e11c0..cf601dc 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 82b4e6a..a451afe 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
@@ -3031,8 +3031,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))"));
         }
     }
 }