Repository: phoenix
Updated Branches:
  refs/heads/4.x-HBase-1.3 913a85576 -> a2a30443b


PHOENIX-4830 fix order by primary key desc

add unit test


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

Branch: refs/heads/4.x-HBase-1.3
Commit: 59f8d0fd1a110786251dbf79c7bc743d1569b54c
Parents: 913a855
Author: Xu Cang <xc...@salesforce.com>
Authored: Fri Aug 10 00:45:17 2018 -0700
Committer: Thomas D'Silva <tdsi...@apache.org>
Committed: Sat Aug 11 11:43:14 2018 -0700

----------------------------------------------------------------------
 .../org/apache/phoenix/end2end/OrderByIT.java   | 46 ++++++++++++++++++++
 .../phoenix/iterate/TableResultIterator.java    | 10 ++++-
 .../java/org/apache/phoenix/util/ScanUtil.java  | 19 ++++++++
 3 files changed, 73 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/59f8d0fd/phoenix-core/src/it/java/org/apache/phoenix/end2end/OrderByIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/OrderByIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/OrderByIT.java
index 578a3af..d7bbc05 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/OrderByIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/OrderByIT.java
@@ -656,6 +656,52 @@ public class OrderByIT extends ParallelStatsDisabledIT {
     }
 
     @Test
+    public void testOrderByDescOnPkWithSubQuery() throws Exception {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String tableName = generateUniqueName();
+        String ddl = "create table " + tableName + " (id bigint not null 
primary key, a bigint)";
+        conn.createStatement().execute(ddl);
+
+        conn.createStatement().execute("upsert into " + tableName + " values 
(1, 11)");
+        conn.createStatement().execute("upsert into " + tableName + " values 
(2, 22)");
+        conn.createStatement().execute("upsert into " + tableName + " values 
(3, 33)");
+        conn.createStatement().execute("upsert into " + tableName + " values 
(4,44)");
+
+        conn.commit();
+
+        ResultSet rs;
+        PhoenixStatement stmt = 
conn.createStatement().unwrap(PhoenixStatement.class);
+        rs = stmt.executeQuery("select id from " + tableName + " where id in 
(select id from "
+            + tableName + ") order by id desc");
+        assertTrue(rs.next());
+        assertEquals("4", rs.getString(1));
+    }
+
+    @Test
+    public void testOrderByAscOnPkWithSubQuery() throws Exception {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String tableName = generateUniqueName();
+        String ddl = "create table " + tableName + " (id bigint not null 
primary key, a bigint)";
+        conn.createStatement().execute(ddl);
+
+        conn.createStatement().execute("upsert into " + tableName + " values 
(1, 11)");
+        conn.createStatement().execute("upsert into " + tableName + " values 
(2, 22)");
+        conn.createStatement().execute("upsert into " + tableName + " values 
(3, 33)");
+        conn.createStatement().execute("upsert into " + tableName + " values 
(4,44)");
+
+        conn.commit();
+
+        ResultSet rs;
+        PhoenixStatement stmt = 
conn.createStatement().unwrap(PhoenixStatement.class);
+        rs = stmt.executeQuery("select id from " + tableName + " where id in 
(select id from "
+            + tableName + ") order by id");
+        assertTrue(rs.next());
+        assertEquals("1", rs.getString(1));
+    }
+
+    @Test
     public void testNullsLastWithDesc() throws Exception {
         Connection conn=null;
         try {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/59f8d0fd/phoenix-core/src/main/java/org/apache/phoenix/iterate/TableResultIterator.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/iterate/TableResultIterator.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/iterate/TableResultIterator.java
index 06f612a..9a80d9f 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/iterate/TableResultIterator.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/iterate/TableResultIterator.java
@@ -17,6 +17,7 @@
  */
 package org.apache.phoenix.iterate;
 
+import static 
org.apache.phoenix.coprocessor.BaseScannerRegionObserver.REVERSE_SCAN;
 import static 
org.apache.phoenix.coprocessor.BaseScannerRegionObserver.SCAN_ACTUAL_START_ROW;
 import static 
org.apache.phoenix.coprocessor.BaseScannerRegionObserver.SCAN_START_ROW_SUFFIX;
 import static 
org.apache.phoenix.iterate.TableResultIterator.RenewLeaseStatus.CLOSED;
@@ -53,6 +54,7 @@ import org.apache.phoenix.query.QueryConstants;
 import org.apache.phoenix.schema.PTable;
 import org.apache.phoenix.schema.StaleRegionBoundaryCacheException;
 import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PBoolean;
 import org.apache.phoenix.util.ByteUtil;
 import org.apache.phoenix.util.Closeables;
 import org.apache.phoenix.util.EnvironmentEdgeManager;
@@ -246,8 +248,12 @@ public class TableResultIterator implements ResultIterator 
{
             ResultIterator delegate = this.scanIterator;
             if (delegate == UNINITIALIZED_SCANNER) {
                 try {
-                    this.scanIterator =
-                            new 
ScanningResultIterator(htable.getScanner(scan), scan, scanMetricsHolder);
+                    if (scan.getAttribute(REVERSE_SCAN) != null
+                        && 
(boolean)(PBoolean.INSTANCE.toObject(scan.getAttribute(REVERSE_SCAN)))) {
+                        ScanUtil.prepareStopRowForReverseScan(scan);
+                    }
+                    this.scanIterator = new 
ScanningResultIterator(htable.getScanner(scan), scan,
+                        scanMetricsHolder);
                 } catch (IOException e) {
                     Closeables.closeQuietly(htable);
                     throw ServerUtil.parseServerException(e);

http://git-wip-us.apache.org/repos/asf/phoenix/blob/59f8d0fd/phoenix-core/src/main/java/org/apache/phoenix/util/ScanUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/ScanUtil.java 
b/phoenix-core/src/main/java/org/apache/phoenix/util/ScanUtil.java
index 62ecebd..2dd46a6 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/ScanUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/ScanUtil.java
@@ -656,6 +656,25 @@ public class ScanUtil {
     }
 
     /**
+     * HBase scan stopRow is exclusive.So we have to append trailing 0 to 
achieve inclusiveness.
+     * for reverse scan, need to append trailing F to stopRow.
+     * @param scan
+     */
+    public static void prepareStopRowForReverseScan(Scan scan) {
+        byte[] stopRow = scan.getStopRow();
+        if (stopRow == null) {
+            return;
+        }
+        byte[] newStopRow = new byte[stopRow.length + 1];
+        int i = 0;
+        for (byte nsr : stopRow) {
+            newStopRow[i++] = nsr;
+        }
+        newStopRow[i] = QueryConstants.DESC_SEPARATOR_BYTE;
+        scan.setStopRow(newStopRow);
+    }
+
+    /**
      * Start key and stop key of the original scan from client are regions 
start and end keys so
      * prefix scan start/stop key to the start row/stop row suffix and set 
them as scan boundaries.
      * @param scan

Reply via email to