From ac06e508233f1bd8ab84377f5e24cc83de24c94e Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Thu, 8 Oct 2026 16:07:38 -0700 Subject: [PATCH 1/3] PHOENIX-7894 Set EXPECTED_UPPER_REGION_KEY on local-index scans The local-index stale region check falls back to the scan stop row when EXPECTED_UPPER_REGION_KEY is absent. Reversed scans are swapped on the client, so the stop row is the region start key and every region looks stale. Set the attribute to the region end key in setLocalIndexAttributes so the check is direction-independent. When the check passes, setupLocalIndexScan must also accept the swapped boundaries. It took the key prefix from the start row, which is the region end key for a reversed scan, so the scan returned no rows. Take the prefix from the region start key in both directions. For a reversed scan, put the stop row suffix in the start row. Co-Authored-By: Claude Opus 5.5 --- .../org/apache/phoenix/util/ScanUtil.java | 38 +++-- .../BaseScannerRegionObserver.java | 9 +- .../LocalIndexScanBoundaryTest.java | 152 ++++++++++++++++++ .../org/apache/phoenix/util/ScanUtilTest.java | 58 +++++++ 4 files changed, 243 insertions(+), 14 deletions(-) create mode 100644 phoenix-core/src/test/java/org/apache/phoenix/coprocessor/LocalIndexScanBoundaryTest.java diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/util/ScanUtil.java b/phoenix-core-client/src/main/java/org/apache/phoenix/util/ScanUtil.java index e66a93d0541..96e50515d59 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/util/ScanUtil.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/util/ScanUtil.java @@ -21,6 +21,7 @@ import static org.apache.phoenix.compile.OrderByCompiler.OrderBy.REV_ROW_KEY_ORDER_BY; import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.CDC_DATA_TABLE_DEF; import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.CUSTOM_ANNOTATIONS; +import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.EXPECTED_UPPER_REGION_KEY; import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.SCAN_ACTUAL_START_ROW; import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.SCAN_START_ROW_SUFFIX; import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.SCAN_STOP_ROW_SUFFIX; @@ -912,20 +913,34 @@ public static void setupReverseScan(Scan scan) { } /** - * prefix region start key to the start row/stop row suffix and set as scan boundaries. + * prefix region start key to the start row/stop row suffix and set as scan boundaries. A reversed + * scan arrives with its boundaries already swapped by the client. For that scan, the stop row + * holds the region start key, and the stop row suffix gives the new start row. */ public static void setupLocalIndexScan(Scan scan) { - byte[] prefix = - scan.getStartRow().length == 0 ? new byte[scan.getStopRow().length] : scan.getStartRow(); - int prefixLength = - scan.getStartRow().length == 0 ? scan.getStopRow().length : scan.getStartRow().length; - if (scan.getAttribute(SCAN_START_ROW_SUFFIX) != null) { - scan.withStartRow( - ScanRanges.prefixKey(scan.getAttribute(SCAN_START_ROW_SUFFIX), 0, prefix, prefixLength)); + boolean reversed = scan.isReversed(); + byte[] regionStartKey = reversed ? scan.getStopRow() : scan.getStartRow(); + byte[] regionEndKey = reversed ? scan.getStartRow() : scan.getStopRow(); + byte[] prefix = regionStartKey.length == 0 ? new byte[regionEndKey.length] : regionStartKey; + int prefixLength = regionStartKey.length == 0 ? regionEndKey.length : regionStartKey.length; + byte[] lowerSuffix = scan.getAttribute(SCAN_START_ROW_SUFFIX); + byte[] upperSuffix = scan.getAttribute(SCAN_STOP_ROW_SUFFIX); + if (reversed) { + if (upperSuffix != null) { + scan.withStartRow(ScanRanges.prefixKey(upperSuffix, 0, prefix, prefixLength), + scan.includeStartRow()); + } + if (lowerSuffix != null) { + scan.withStopRow(ScanRanges.prefixKey(lowerSuffix, 0, prefix, prefixLength), + scan.includeStopRow()); + } + return; + } + if (lowerSuffix != null) { + scan.withStartRow(ScanRanges.prefixKey(lowerSuffix, 0, prefix, prefixLength)); } - if (scan.getAttribute(SCAN_STOP_ROW_SUFFIX) != null) { - scan.withStopRow( - ScanRanges.prefixKey(scan.getAttribute(SCAN_STOP_ROW_SUFFIX), 0, prefix, prefixLength)); + if (upperSuffix != null) { + scan.withStopRow(ScanRanges.prefixKey(upperSuffix, 0, prefix, prefixLength)); } } @@ -948,6 +963,7 @@ public static void setLocalIndexAttributes(Scan newScan, int keyOffset, byte[] r byte[] regionEndKey, byte[] startRowSuffix, byte[] stopRowSuffix) { if (ScanUtil.isLocalIndex(newScan)) { newScan.setAttribute(SCAN_ACTUAL_START_ROW, regionStartKey); + newScan.setAttribute(EXPECTED_UPPER_REGION_KEY, regionEndKey); newScan.withStartRow(regionStartKey); newScan.withStopRow(regionEndKey); if (keyOffset > 0) { diff --git a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/BaseScannerRegionObserver.java b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/BaseScannerRegionObserver.java index 9cb73a0b1c4..095466af14d 100644 --- a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/BaseScannerRegionObserver.java +++ b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/BaseScannerRegionObserver.java @@ -67,6 +67,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting; + abstract public class BaseScannerRegionObserver implements RegionObserver { private static final Logger LOGGER = LoggerFactory.getLogger(BaseScannerRegionObserver.class); @@ -79,8 +81,8 @@ public String toString() { return this.getClass().getName(); } - private static void throwIfScanOutOfRegion(Scan scan, Region region) - throws DoNotRetryIOException { + @VisibleForTesting + static void throwIfScanOutOfRegion(Scan scan, Region region) throws DoNotRetryIOException { boolean isLocalIndex = ScanUtil.isLocalIndex(scan); byte[] lowerInclusiveScanKey = scan.getStartRow(); byte[] upperExclusiveScanKey = scan.getStopRow(); @@ -90,7 +92,8 @@ private static void throwIfScanOutOfRegion(Scan scan, Region region) if (isLocalIndex) { // For local indexes we have to abort any scan that was open during a split. // We detect that condition as follows: - // 1. The scanner's stop row has to always match the region's end key. + // 1. The EXPECTED_UPPER_REGION_KEY attribute has to always match the region's end key. + // Older clients do not set the attribute. For them, the scan's stop row is the fallback. // 2. Phoenix sets the SCAN_ACTUAL_START_ROW attribute to the scan's original start row // We cannot directly compare that with the region's start key, but can enforce that // the original start row still falls within the new region. diff --git a/phoenix-core/src/test/java/org/apache/phoenix/coprocessor/LocalIndexScanBoundaryTest.java b/phoenix-core/src/test/java/org/apache/phoenix/coprocessor/LocalIndexScanBoundaryTest.java new file mode 100644 index 00000000000..09b9321ebb0 --- /dev/null +++ b/phoenix-core/src/test/java/org/apache/phoenix/coprocessor/LocalIndexScanBoundaryTest.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.phoenix.coprocessor; + +import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.EXPECTED_UPPER_REGION_KEY; +import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.LOCAL_INDEX; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.hadoop.hbase.DoNotRetryIOException; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.RegionInfoBuilder; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.regionserver.Region; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.schema.StaleRegionBoundaryCacheException; +import org.apache.phoenix.schema.types.PDataType; +import org.apache.phoenix.util.ScanUtil; +import org.junit.Test; + +/** + * Tests the server-side boundary check and the boundary rewrite for local index scans. Each scan is + * prepared as the client prepares it, including the client-side swap for reversed scans. + */ +public class LocalIndexScanBoundaryTest { + + private static final byte[] EMPTY = HConstants.EMPTY_BYTE_ARRAY; + private static final byte[] E = Bytes.toBytes("e"); + private static final byte[] I = Bytes.toBytes("i"); + private static final byte[] O = Bytes.toBytes("o"); + private static final byte[] LOWER_SUFFIX = new byte[] { 0, 1 }; + private static final byte[] UPPER_SUFFIX = new byte[] { 0, 2 }; + + private static Region region(byte[] startKey, byte[] endKey) { + Region region = mock(Region.class); + when(region.getRegionInfo()).thenReturn(RegionInfoBuilder.newBuilder(TableName.valueOf("T")) + .setStartKey(startKey).setEndKey(endKey).build()); + return region; + } + + private static Scan clientScan(byte[] regionStartKey, byte[] regionEndKey, boolean reversed) { + Scan scan = new Scan(); + scan.setAttribute(LOCAL_INDEX, PDataType.TRUE_BYTES); + ScanUtil.setLocalIndexAttributes(scan, 0, regionStartKey, regionEndKey, LOWER_SUFFIX, + UPPER_SUFFIX); + if (reversed) { + ScanUtil.setReversed(scan); + ScanUtil.setupReverseScan(scan); + } + return scan; + } + + private static byte[] concat(byte[] prefix, byte[] suffix) { + return Bytes.add(prefix, suffix); + } + + private static void assertStale(Scan scan, Region region) { + try { + BaseScannerRegionObserver.throwIfScanOutOfRegion(scan, region); + fail("Expected a stale region boundary failure"); + } catch (DoNotRetryIOException e) { + assertTrue(e.getCause() instanceof StaleRegionBoundaryCacheException); + } + } + + private static void assertScanRows(Scan scan, byte[] prefix, boolean reversed) { + if (reversed) { + assertTrue(scan.isReversed()); + assertArrayEquals(concat(prefix, UPPER_SUFFIX), scan.getStartRow()); + assertArrayEquals(concat(prefix, LOWER_SUFFIX), scan.getStopRow()); + assertFalse(scan.includeStartRow()); + assertTrue(scan.includeStopRow()); + } else { + assertFalse(scan.isReversed()); + assertArrayEquals(concat(prefix, LOWER_SUFFIX), scan.getStartRow()); + assertArrayEquals(concat(prefix, UPPER_SUFFIX), scan.getStopRow()); + assertTrue(scan.includeStartRow()); + assertFalse(scan.includeStopRow()); + } + } + + @Test + public void testMatchingRegionPassesInBothDirections() throws Exception { + for (boolean reversed : new boolean[] { false, true }) { + // First region: the prefix is zero bytes with the length of the region end key. + Scan scan = clientScan(EMPTY, E, reversed); + BaseScannerRegionObserver.throwIfScanOutOfRegion(scan, region(EMPTY, E)); + assertScanRows(scan, new byte[E.length], reversed); + + scan = clientScan(E, I, reversed); + BaseScannerRegionObserver.throwIfScanOutOfRegion(scan, region(E, I)); + assertScanRows(scan, E, reversed); + + // Last region: the end key is open. + scan = clientScan(O, EMPTY, reversed); + BaseScannerRegionObserver.throwIfScanOutOfRegion(scan, region(O, EMPTY)); + assertScanRows(scan, O, reversed); + } + } + + @Test + public void testSplitIsDetectedInBothDirections() { + for (boolean reversed : new boolean[] { false, true }) { + // The client still sees region [e, o), but the region split at i. + assertStale(clientScan(E, O, reversed), region(E, I)); + assertStale(clientScan(E, O, reversed), region(I, O)); + // The last region split at o. + assertStale(clientScan(I, EMPTY, reversed), region(I, O)); + assertStale(clientScan(I, EMPTY, reversed), region(O, EMPTY)); + } + } + + @Test + public void testMergeIsDetectedInBothDirections() { + for (boolean reversed : new boolean[] { false, true }) { + // The client still sees region [e, i), but regions [e, i) and [i, o) merged. + assertStale(clientScan(E, I, reversed), region(E, O)); + } + } + + @Test + public void testForwardScanWithoutExpectedUpperRegionKey() throws Exception { + // Older clients do not set the attribute. The forward scan falls back to the stop row. + Scan scan = clientScan(E, I, false); + scan.setAttribute(EXPECTED_UPPER_REGION_KEY, null); + BaseScannerRegionObserver.throwIfScanOutOfRegion(scan, region(E, I)); + assertScanRows(scan, E, false); + scan = clientScan(E, I, false); + scan.setAttribute(EXPECTED_UPPER_REGION_KEY, null); + assertStale(scan, region(E, O)); + } +} diff --git a/phoenix-core/src/test/java/org/apache/phoenix/util/ScanUtilTest.java b/phoenix-core/src/test/java/org/apache/phoenix/util/ScanUtilTest.java index 6bba98113ff..d372ddd92b9 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/util/ScanUtilTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/util/ScanUtilTest.java @@ -17,6 +17,11 @@ */ package org.apache.phoenix.util; +import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.EXPECTED_UPPER_REGION_KEY; +import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.LOCAL_INDEX; +import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.SCAN_ACTUAL_START_ROW; +import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.SCAN_START_ROW_SUFFIX; +import static org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants.SCAN_STOP_ROW_SUFFIX; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -24,6 +29,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; +import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; import org.apache.phoenix.compile.StatementContext; @@ -549,6 +555,58 @@ public void testRowSizeScansDoNotEnableIndexReadRepair() { assertNull( indexRebuildScan.getAttribute(BaseScannerRegionObserverConstants.CHECK_VERIFY_COLUMN)); } + + private static Scan newLocalIndexScan() { + Scan scan = new Scan(); + scan.setAttribute(LOCAL_INDEX, PDataType.TRUE_BYTES); + return scan; + } + + @Test + public void testSetLocalIndexAttributesSetsExpectedUpperRegionKey() { + Scan scan = newLocalIndexScan(); + ScanUtil.setLocalIndexAttributes(scan, 0, Bytes.toBytes("e"), Bytes.toBytes("i"), + new byte[] { 0, 1 }, new byte[] { 0, 2 }); + assertArrayEquals(Bytes.toBytes("i"), scan.getAttribute(EXPECTED_UPPER_REGION_KEY)); + assertArrayEquals(Bytes.toBytes("e"), scan.getAttribute(SCAN_ACTUAL_START_ROW)); + assertArrayEquals(Bytes.toBytes("e"), scan.getStartRow()); + assertArrayEquals(Bytes.toBytes("i"), scan.getStopRow()); + } + + @Test + public void testExpectedUpperRegionKeyForLastOpenEndedRegion() { + for (boolean reversed : new boolean[] { false, true }) { + Scan scan = newLocalIndexScan(); + ScanUtil.setLocalIndexAttributes(scan, 0, Bytes.toBytes("o"), HConstants.EMPTY_END_ROW, + new byte[] { 0, 1 }, new byte[] { 0, 2 }); + if (reversed) { + ScanUtil.setReversed(scan); + ScanUtil.setupReverseScan(scan); + } + byte[] expectedUpper = scan.getAttribute(EXPECTED_UPPER_REGION_KEY); + assertNotNull(expectedUpper); + assertArrayEquals(HConstants.EMPTY_END_ROW, expectedUpper); + } + } + + @Test + public void testExpectedUpperRegionKeyWithKeyOffset() { + Scan scan = newLocalIndexScan(); + ScanUtil.setLocalIndexAttributes(scan, 1, Bytes.toBytes("e"), Bytes.toBytes("i"), + new byte[] { 'e', 0, 1 }, new byte[] { 'e', 0, 2 }); + assertArrayEquals(Bytes.toBytes("i"), scan.getAttribute(EXPECTED_UPPER_REGION_KEY)); + assertArrayEquals(new byte[] { 0, 1 }, scan.getAttribute(SCAN_START_ROW_SUFFIX)); + assertArrayEquals(new byte[] { 0, 2 }, scan.getAttribute(SCAN_STOP_ROW_SUFFIX)); + } + + @Test + public void testSetLocalIndexAttributesNoopForNonLocalIndexScan() { + Scan scan = new Scan(); + ScanUtil.setLocalIndexAttributes(scan, 0, Bytes.toBytes("e"), Bytes.toBytes("i"), + new byte[] { 0, 1 }, new byte[] { 0, 2 }); + assertNull(scan.getAttribute(EXPECTED_UPPER_REGION_KEY)); + assertNull(scan.getAttribute(SCAN_ACTUAL_START_ROW)); + } } } From d7313141825a441f1d3bd5901a12056ba3c25a52 Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Thu, 8 Oct 2026 17:47:29 -0700 Subject: [PATCH 2/3] Address review feedback --- .../BaseScannerRegionObserver.java | 5 +++-- .../LocalIndexScanBoundaryTest.java | 21 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/BaseScannerRegionObserver.java b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/BaseScannerRegionObserver.java index 095466af14d..de6180e0416 100644 --- a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/BaseScannerRegionObserver.java +++ b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/BaseScannerRegionObserver.java @@ -93,13 +93,14 @@ static void throwIfScanOutOfRegion(Scan scan, Region region) throws DoNotRetryIO // For local indexes we have to abort any scan that was open during a split. // We detect that condition as follows: // 1. The EXPECTED_UPPER_REGION_KEY attribute has to always match the region's end key. - // Older clients do not set the attribute. For them, the scan's stop row is the fallback. + // Older clients do not set the attribute. For them, the region end key is the fallback. + // A reversed scan has its boundaries swapped, so the end key is in the start row. // 2. Phoenix sets the SCAN_ACTUAL_START_ROW attribute to the scan's original start row // We cannot directly compare that with the region's start key, but can enforce that // the original start row still falls within the new region. byte[] expectedUpperRegionKey = scan.getAttribute(BaseScannerRegionObserverConstants.EXPECTED_UPPER_REGION_KEY) == null - ? scan.getStopRow() + ? (scan.isReversed() ? scan.getStartRow() : scan.getStopRow()) : scan.getAttribute(BaseScannerRegionObserverConstants.EXPECTED_UPPER_REGION_KEY); byte[] actualStartRow = diff --git a/phoenix-core/src/test/java/org/apache/phoenix/coprocessor/LocalIndexScanBoundaryTest.java b/phoenix-core/src/test/java/org/apache/phoenix/coprocessor/LocalIndexScanBoundaryTest.java index 09b9321ebb0..14a0d186113 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/coprocessor/LocalIndexScanBoundaryTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/coprocessor/LocalIndexScanBoundaryTest.java @@ -139,14 +139,17 @@ public void testMergeIsDetectedInBothDirections() { } @Test - public void testForwardScanWithoutExpectedUpperRegionKey() throws Exception { - // Older clients do not set the attribute. The forward scan falls back to the stop row. - Scan scan = clientScan(E, I, false); - scan.setAttribute(EXPECTED_UPPER_REGION_KEY, null); - BaseScannerRegionObserver.throwIfScanOutOfRegion(scan, region(E, I)); - assertScanRows(scan, E, false); - scan = clientScan(E, I, false); - scan.setAttribute(EXPECTED_UPPER_REGION_KEY, null); - assertStale(scan, region(E, O)); + public void testScanWithoutExpectedUpperRegionKey() throws Exception { + // Older clients do not set the attribute. The check falls back to the region end key in the + // scan, which is the stop row for a forward scan and the start row for a reversed scan. + for (boolean reversed : new boolean[] { false, true }) { + Scan scan = clientScan(E, I, reversed); + scan.setAttribute(EXPECTED_UPPER_REGION_KEY, null); + BaseScannerRegionObserver.throwIfScanOutOfRegion(scan, region(E, I)); + assertScanRows(scan, E, reversed); + scan = clientScan(E, I, reversed); + scan.setAttribute(EXPECTED_UPPER_REGION_KEY, null); + assertStale(scan, region(E, O)); + } } } From 12a06a138802be2ba15cf7f3cf8c43e079c76f67 Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Thu, 8 Oct 2026 22:57:24 -0700 Subject: [PATCH 3/3] Address another finding --- .../org/apache/phoenix/util/ScanUtil.java | 5 +++ .../org/apache/phoenix/util/ScanUtilTest.java | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/util/ScanUtil.java b/phoenix-core-client/src/main/java/org/apache/phoenix/util/ScanUtil.java index 96e50515d59..b6cfee9851d 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/util/ScanUtil.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/util/ScanUtil.java @@ -1611,8 +1611,13 @@ public static void setScanAttributesForPhoenixTTL(Scan scan, PTable table, byte[] actualStartRow = scan.getAttribute(SCAN_ACTUAL_START_ROW) != null ? scan.getAttribute(SCAN_ACTUAL_START_ROW) : HConstants.EMPTY_BYTE_ARRAY; + // The region end key is not known here. Keep the one that the region scan already has. + byte[] expectedUpperRegionKey = scan.getAttribute(EXPECTED_UPPER_REGION_KEY); ScanUtil.setLocalIndexAttributes(scan, 0, actualStartRow, HConstants.EMPTY_BYTE_ARRAY, scan.getStartRow(), scan.getStopRow()); + if (expectedUpperRegionKey != null) { + scan.setAttribute(EXPECTED_UPPER_REGION_KEY, expectedUpperRegionKey); + } } } } diff --git a/phoenix-core/src/test/java/org/apache/phoenix/util/ScanUtilTest.java b/phoenix-core/src/test/java/org/apache/phoenix/util/ScanUtilTest.java index d372ddd92b9..a21e7c5b316 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/util/ScanUtilTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/util/ScanUtilTest.java @@ -25,19 +25,29 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.List; +import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; import org.apache.phoenix.compile.StatementContext; import org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants; +import org.apache.phoenix.jdbc.PhoenixConnection; +import org.apache.phoenix.query.ConnectionQueryServices; import org.apache.phoenix.query.KeyRange; import org.apache.phoenix.query.KeyRange.Bound; import org.apache.phoenix.query.QueryConstants; +import org.apache.phoenix.schema.LiteralTTLExpression; import org.apache.phoenix.schema.PDatum; +import org.apache.phoenix.schema.PName; +import org.apache.phoenix.schema.PTable; +import org.apache.phoenix.schema.PTableType; import org.apache.phoenix.schema.RowKeySchema; import org.apache.phoenix.schema.RowKeySchema.RowKeySchemaBuilder; import org.apache.phoenix.schema.SortOrder; @@ -599,6 +609,35 @@ public void testExpectedUpperRegionKeyWithKeyOffset() { assertArrayEquals(new byte[] { 0, 2 }, scan.getAttribute(SCAN_STOP_ROW_SUFFIX)); } + @Test + public void testPhoenixTTLRewrapKeepsExpectedUpperRegionKey() throws Exception { + PName name = mock(PName.class); + when(name.getString()).thenReturn("T"); + PName schemaName = mock(PName.class); + when(schemaName.getString()).thenReturn("S"); + PTable table = mock(PTable.class); + when(table.isStrictTTL()).thenReturn(true); + when(table.getType()).thenReturn(PTableType.TABLE); + when(table.getSchemaName()).thenReturn(schemaName); + when(table.getTableName()).thenReturn(name); + when(table.getName()).thenReturn(name); + when(table.getColumnFamilies()).thenReturn(Collections.emptyList()); + when(table.getEncodingScheme()) + .thenReturn(PTable.QualifierEncodingScheme.NON_ENCODED_QUALIFIERS); + when(table.getCompiledTTLExpression(org.mockito.ArgumentMatchers.any())) + .thenReturn(new LiteralTTLExpression(100)); + ConnectionQueryServices services = mock(ConnectionQueryServices.class); + when(services.getConfiguration()).thenReturn(new Configuration(false)); + PhoenixConnection connection = mock(PhoenixConnection.class); + when(connection.getQueryServices()).thenReturn(services); + + Scan scan = newLocalIndexScan(); + ScanUtil.setLocalIndexAttributes(scan, 0, Bytes.toBytes("e"), Bytes.toBytes("i"), + new byte[] { 0, 1 }, new byte[] { 0, 2 }); + ScanUtil.setScanAttributesForPhoenixTTL(scan, table, connection); + assertArrayEquals(Bytes.toBytes("i"), scan.getAttribute(EXPECTED_UPPER_REGION_KEY)); + } + @Test public void testSetLocalIndexAttributesNoopForNonLocalIndexScan() { Scan scan = new Scan();