Copilot commented on code in PR #2668:
URL: https://github.com/apache/phoenix/pull/2668#discussion_r4517418465
##########
phoenix-core-server/src/main/java/org/apache/phoenix/iterate/NonAggregateRegionScannerFactory.java:
##########
@@ -599,6 +618,15 @@ public boolean isFilterDone() {
return tuple == null;
}
+ // Get the next tuple. When the build becomes complete, size the chunk
to the cached rows.
+ private void nextTuple() throws SQLException {
+ tuple = iterator.next();
+ if (!isChunkSized && !isDummy(tuple)) {
+ chunk.resize(iterator.getByteSize());
+ isChunkSized = true;
+ }
+ }
Review Comment:
`nextTuple()` calls `isDummy(tuple)` even when `iterator.next()` may return
`null` (end of scan). If `isDummy` doesn’t accept null, this will throw an NPE
during scan exhaustion. Make the condition null-safe (e.g., treat `null` as
“build complete” and resize once), such as checking `tuple == null` before
calling `isDummy(tuple)`.
##########
phoenix-core/src/it/java/org/apache/phoenix/end2end/ServerPagingIT.java:
##########
@@ -749,6 +756,106 @@ public void testBloomFilterDefaults() throws Exception {
}
}
+ private static final int TOPN_ROW_COUNT = 1000;
+ private static final int TOPN_PREAD_MAX_BYTES = 4096;
+
+ private static int topNSortValue(int id) {
+ // 37 and the row count have no common factor, so each row gets a
different value.
+ return (id * 37) % TOPN_ROW_COUNT;
+ }
+
+ private static List<Integer> expectedTopNIds(int offset, int limit) {
+ Integer[] idsBySortValue = new Integer[TOPN_ROW_COUNT];
+ for (int id = 0; id < TOPN_ROW_COUNT; id++) {
+ idsBySortValue[topNSortValue(id)] = id;
+ }
+ return Arrays.asList(idsBySortValue).subList(offset, offset + limit);
+ }
+
+ /**
+ * Run a topN query on a global index. The index table has a small pread
limit for its store
+ * scanners. The index scan goes above this limit while the server builds
the topN rows at open.
+ * Thus the open RPC is a heartbeat. The method checks the rows and the RPC
count for each region.
+ */
+ private void assertTopNOnIndexWithPreadLimit(boolean uncovered, String
indexSplitPoint,
+ int offset, int expectedRpcsPerRegion) throws Exception {
+ String dataTableName = generateUniqueName();
+ String indexName = generateUniqueName();
+ Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+ // A long page keeps the time limit out of the test. Only the pread limit
pages the scan.
+ props.put(QueryServices.PHOENIX_SERVER_PAGE_SIZE_MS,
Long.toString(TimeUnit.HOURS.toMillis(1)));
+ try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+ conn.createStatement().execute("CREATE TABLE " + dataTableName
+ + " (ID INTEGER NOT NULL PRIMARY KEY, V1 VARCHAR, V2 VARCHAR, V3
INTEGER)");
+ conn.createStatement()
+ .execute("CREATE " + (uncovered ? "UNCOVERED " : "") + "INDEX " +
indexName + " ON "
+ + dataTableName + " (V1)" + (uncovered ? "" : " INCLUDE (V2, V3)") +
" \""
+ + StoreScanner.STORESCANNER_PREAD_MAX_BYTES + "\"=" +
TOPN_PREAD_MAX_BYTES
+ + (indexSplitPoint == null ? "" : " SPLIT ON ('" + indexSplitPoint +
"')"));
+ TableDescriptor td =
conn.unwrap(PhoenixConnection.class).getQueryServices()
+ .getTableDescriptor(Bytes.toBytes(indexName));
+ assertEquals(Integer.toString(TOPN_PREAD_MAX_BYTES),
+ td.getValue(StoreScanner.STORESCANNER_PREAD_MAX_BYTES));
+ PreparedStatement upsert =
+ conn.prepareStatement("UPSERT INTO " + dataTableName + " VALUES (?, ?,
?, ?)");
+ for (int id = 0; id < TOPN_ROW_COUNT; id++) {
+ upsert.setInt(1, id);
+ upsert.setString(2, String.format("v%04d", id));
+ upsert.setString(3, "x" + id);
+ upsert.setInt(4, topNSortValue(id));
+ upsert.executeUpdate();
+ if (id % 100 == 99) {
+ conn.commit();
+ }
+ }
+ conn.commit();
+ getUtility().flush(TableName.valueOf(indexName));
+
+ int limit = 5;
+ String sql =
+ "SELECT /*+ INDEX(" + dataTableName + " " + indexName + ") */ ID, V3
FROM " + dataTableName
+ + " WHERE V1 >= 'v' ORDER BY V3 LIMIT " + limit + (offset > 0 ? "
OFFSET " + offset : "");
+ assertNotNull(
+ assertPlan(conn,
sql).tableContains(indexName).attributes().getServerSortedBy());
+ try (ResultSet rs = conn.createStatement().executeQuery(sql)) {
+ for (int id : expectedTopNIds(offset, limit)) {
+ assertTrue(rs.next());
+ assertEquals(id, rs.getInt(1));
+ assertEquals(topNSortValue(id), rs.getInt(2));
+ }
+ assertFalse(rs.next());
+ // With an offset, the client does not read all rows from each region.
A scanner reports
+ // its metrics only at the end or at close, so close the result set
first.
+ rs.close();
+ long numRpcs =
+ getMetricValue(PhoenixRuntime.getRequestReadMetricInfo(rs),
MetricType.COUNT_RPC_CALLS);
+ int numRegions =
getUtility().getAdmin().getRegions(TableName.valueOf(indexName)).size();
+ assertEquals(indexSplitPoint == null ? 1 : 2, numRegions);
+ assertEquals(expectedRpcsPerRegion * numRegions, numRpcs);
+ }
Review Comment:
This uses `rs` after explicitly closing it
(`PhoenixRuntime.getRequestReadMetricInfo(rs)`), which is brittle and may break
if metric extraction ever requires an open ResultSet. Prefer capturing the
metric info object before closing (or restructure the try-with-resources scope
so the ResultSet is closed before computing metrics, without accessing the
closed `rs`).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]