Skip to content

Commit 1ddf167

Browse files
palashcPalash Chauhan
and
Palash Chauhan
authored
PHOENIX-7599: Fix count of rows scanned metric for uncovered indexes (#2139)
Co-authored-by: Palash Chauhan <[email protected]>
1 parent a3ac251 commit 1ddf167

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/UncoveredIndexRegionScanner.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,14 @@ protected Scan prepareDataTableScan(Collection<byte[]> dataRowKeys,
220220
protected boolean scanIndexTableRows(List<Cell> result,
221221
final long startTime,
222222
final byte[] actualStartKey,
223-
final int offset) throws IOException {
223+
final int offset,
224+
ScannerContext scannerContext) throws IOException {
224225
boolean hasMore = false;
225226
if (actualStartKey != null) {
226227
do {
227-
hasMore = innerScanner.nextRaw(result);
228+
hasMore = scannerContext != null
229+
? innerScanner.nextRaw(result, scannerContext)
230+
: innerScanner.nextRaw(result);
228231
if (result.isEmpty()) {
229232
return hasMore;
230233
}
@@ -249,7 +252,9 @@ protected boolean scanIndexTableRows(List<Cell> result,
249252
do {
250253
List<Cell> row = new ArrayList<Cell>();
251254
if (result.isEmpty()) {
252-
hasMore = innerScanner.nextRaw(row);
255+
hasMore = scannerContext != null
256+
? innerScanner.nextRaw(row, scannerContext)
257+
: innerScanner.nextRaw(row);
253258
} else {
254259
row.addAll(result);
255260
result.clear();
@@ -289,8 +294,9 @@ protected boolean scanIndexTableRows(List<Cell> result,
289294
}
290295

291296
protected boolean scanIndexTableRows(List<Cell> result,
292-
final long startTime) throws IOException {
293-
return scanIndexTableRows(result, startTime, null, 0);
297+
final long startTime,
298+
ScannerContext scannerContext) throws IOException {
299+
return scanIndexTableRows(result, startTime, null, 0, scannerContext);
294300
}
295301

296302
private boolean verifyIndexRowAndRepairIfNecessary(Result dataRow, byte[] indexRowKey,
@@ -381,8 +387,8 @@ protected boolean getNextCoveredIndexRow(List<Cell> result) throws IOException {
381387
}
382388
}
383389

384-
public boolean next(List<Cell> result, ScannerContext scannerContext) throws IOException {
385-
return next(result);
390+
public boolean next(List<Cell> result) throws IOException {
391+
return next(result, null);
386392
}
387393

388394
/**
@@ -399,7 +405,7 @@ public boolean next(List<Cell> result, ScannerContext scannerContext) throws IOE
399405
* @throws IOException
400406
*/
401407
@Override
402-
public boolean next(List<Cell> result) throws IOException {
408+
public boolean next(List<Cell> result, ScannerContext scannerContext) throws IOException {
403409
long startTime = EnvironmentEdgeManager.currentTimeMillis();
404410
boolean hasMore;
405411
region.startRegionOperation();
@@ -416,7 +422,7 @@ public boolean next(List<Cell> result) throws IOException {
416422
state = State.SCANNING_INDEX;
417423
}
418424
if (state == State.SCANNING_INDEX) {
419-
hasMore = scanIndexTableRows(result, startTime);
425+
hasMore = scanIndexTableRows(result, startTime, scannerContext);
420426
if (isDummy(result)) {
421427
updateDummyWithPrevRowKey(result, initStartRowKey, includeInitStartRowKey,
422428
scan);

phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/UncoveredLocalIndexRegionScanner.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,19 @@ protected void scanDataTableRows(long startTime)
120120

121121
@Override
122122
protected boolean scanIndexTableRows(List<Cell> result,
123-
final long startTime) throws IOException {
124-
return scanIndexTableRows(result, startTime, actualStartKey, offset);
123+
final long startTime,
124+
ScannerContext scannerContext) throws IOException {
125+
return scanIndexTableRows(result, startTime, actualStartKey, offset, scannerContext);
125126
}
126127

127128
@Override
128-
public boolean next(List<Cell> result) throws IOException {
129-
boolean hasMore = super.next(result);
129+
public boolean next(List<Cell> result, ScannerContext scannerContext) throws IOException {
130+
boolean hasMore = super.next(result, scannerContext);
130131
ServerIndexUtil.wrapResultUsingOffset(result, offset);
131132
return hasMore;
132133
}
133134

134-
public boolean next(List<Cell> result, ScannerContext scannerContext) throws IOException {
135-
return next(result);
135+
public boolean next(List<Cell> result) throws IOException {
136+
return next(result, null);
136137
}
137138
}

phoenix-core/src/it/java/org/apache/phoenix/monitoring/CountRowsScannedIT.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,26 @@ public void testQueryIndex() throws Exception {
215215
Assert.assertEquals(indexName, stmt.getQueryPlan().getTableRef().getTable().getTableName().toString());
216216
}
217217

218+
@Test
219+
public void testQueryUncoveredIndex() throws Exception {
220+
Connection conn = DriverManager.getConnection(getUrl());
221+
String tableName = generateUniqueName();
222+
String indexName = generateUniqueName();
223+
PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
224+
stmt.execute("CREATE TABLE " + tableName
225+
+ " (A UNSIGNED_LONG NOT NULL PRIMARY KEY, Z UNSIGNED_LONG)");
226+
stmt.execute("CREATE UNCOVERED INDEX " + indexName + " ON " + tableName + "(Z)");
227+
for (int i = 1; i <= 100; i++) {
228+
String sql = String.format("UPSERT INTO %s VALUES (%d, %d)", tableName, i, i);
229+
stmt.execute(sql);
230+
}
231+
conn.commit();
232+
String selectQuery = "SELECT A FROM " + tableName + " WHERE Z > 34 AND Z < 63";
233+
long count = countRowsScannedFromSql(stmt, selectQuery);
234+
assertEquals(28, count);
235+
Assert.assertEquals(indexName, stmt.getQueryPlan().getTableRef().getTable().getTableName().toString());
236+
}
237+
218238
@Test
219239
public void testJoin() throws Exception {
220240
Connection conn = DriverManager.getConnection(getUrl());

0 commit comments

Comments
 (0)