Saurabh Rai created PHOENIX-8076:
------------------------------------

             Summary: OFFSET is ignored when ORDER BY is opposite to the row 
key sort order (reverse scan with SERVER OFFSET)
                 Key: PHOENIX-8076
                 URL: https://issues.apache.org/jira/browse/PHOENIX-8076
             Project: Phoenix
          Issue Type: Bug
    Affects Versions: 5.3.2, 5.3.1, 5.2.2, 5.3.0, 5.2.1, 5.2.0
            Reporter: Saurabh Rai
            Assignee: Saurabh Rai


For a table that is not salted and has no local index, Phoenix pushes OFFSET to 
the region server ({quote}SERVER OFFSET n{quote}). When the query's ORDER BY 
runs opposite to the row key order, this becomes a reverse range scan, and the 
server ignores the offset: it returns the first LIMIT rows. As a result, every 
page of an OFFSET-paged query returns the same rows, and no error is raised.

The offset works correctly for forward scans and for salted tables, where it is 
applied on the client after the merge sort ({{CLIENT OFFSET n}}).

h3. Steps to reproduce
{code:sql}
CREATE TABLE T (K1 VARCHAR NOT NULL, SEQ INTEGER NOT NULL, V VARCHAR
    CONSTRAINT PK PRIMARY KEY (K1, SEQ DESC));
-- upsert SEQ = 1..20 for K1 = 'a'
UPSERT INTO T VALUES ('a', 1, 'v1');
-- ...
UPSERT INTO T VALUES ('a', 20, 'v20');

-- ORDER BY ASC on a DESC row key column => REVERSE range scan with SERVER 
OFFSET
SELECT SEQ FROM T WHERE K1 = 'a' ORDER BY SEQ LIMIT 5 OFFSET 0;   -- 1..5
SELECT SEQ FROM T WHERE K1 = 'a' ORDER BY SEQ LIMIT 5 OFFSET 5;   -- expected 
6..10, actual 1..5

-- Same query in row key order (forward scan) is correct
SELECT SEQ FROM T WHERE K1 = 'a' ORDER BY SEQ DESC LIMIT 5 OFFSET 5;   -- 15..11
{code}
The EXPLAIN plan for the failing query shows a REVERSE range scan with {{SERVER 
OFFSET 5}}.

h3. Root cause
{{NonAggregateRegionScannerFactory.getOffsetScanner()}} checks whether the scan 
is a continuation of one that already returned rows, for example a scan 
reopened after a region move. If it is, the offset has already been applied and 
must not be applied again. The check compares the scan's start row with the 
{{_ScanActualStartRow}} attribute:
{code:java}
final byte[] initStartRowKey = scan.getStartRow().length > 0
  ? scan.getStartRow()
  : (scan.isReversed() ? region.getRegionInfo().getEndKey() : 
region.getRegionInfo().getStartKey());
byte[] prevScanStartRowKey = 
scan.getAttribute(BaseScannerRegionObserverConstants.SCAN_ACTUAL_START_ROW);
if (prevScanStartRowKey != null && Bytes.compareTo(prevScanStartRowKey, 
initStartRowKey) != 0
    && Bytes.compareTo(ByteUtil.concat(prevScanStartRowKey, 
ByteUtil.ZERO_BYTE), initStartRowKey) != 0) {
  iterator.setRowCountToOffset();
}
{code}
For a reverse scan, the two values hold opposite ends of the range:
* {{ScanRanges.intersectScan()}} sets {{_ScanActualStartRow}} to the *lower* 
bound of the scan range.
* {{TableResultIterator.initScanner()}} then calls 
{{ScanUtil.setupReverseScan()}}, which swaps the start and stop rows. On the 
server, {{scan.getStartRow()}} is therefore the *upper* bound.

So the comparison fails on the first RPC of every reverse range scan. 
{{setRowCountToOffset()}} then marks the offset as already consumed, and no 
rows are skipped.

The check was introduced by PHOENIX-7106. It is present in 5.2.0 through 5.2.2, 
5.3.0 through 5.3.2, master, and the 5.1 branch; 5.1.3 does not have it. 
PHOENIX-8010 added the null guard to the same condition, but does not address 
this. {{QueryWithOffsetIT}} has no reverse-scan case.

h3. Possible fix
For reversed scans, the continuation check must compare values in the same 
orientation. A resumed reverse scan moves the *upper* bound, and the original 
upper bound is not currently stored in any scan attribute. One option is for 
the client to record it in a new attribute, which the server compares against 
when {{scan.isReversed()}}. Servers must still handle older clients that don't 
send the new attribute.

Tests to add to {{QueryWithOffsetIT}}:
* a DESC row key column with ORDER BY ASC
* an ASC row key column with ORDER BY DESC

h3. Workarounds
* Query in row key order, so it runs as a forward scan.
* Use keyset pagination ({{WHERE SEQ > ? ... LIMIT n}}) instead of OFFSET.
* Apply the offset in the application ({{LIMIT offset + n}}, then skip rows).



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to