Skip to content

Commit b04cbb8

Browse files
author
Peter Toth
committed
address review finding
1 parent 1bffbc2 commit b04cbb8

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileReader.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,8 @@ public ColumnIndexStore getColumnIndexStore(int blockIndex) {
14921492
/**
14931493
* Computes the {@link RowRanges} within the given row group that may pass the configured filter
14941494
* (set via {@link ParquetReadOptions} or {@link ParquetInputFormat#setFilterPredicate}). If no
1495-
* filter is configured, returns a {@link RowRanges} covering all rows in the row group.
1495+
* filter is configured, returns a {@link RowRanges} covering all rows in the row group. If the
1496+
* row group has no rows, returns {@link RowRanges#EMPTY}.
14961497
*
14971498
* <p>This computation is metadata-only: it consults each filter-referenced column's column
14981499
* index from the file footer; no column data is read from disk. The result can be passed to
@@ -1501,10 +1502,20 @@ public ColumnIndexStore getColumnIndexStore(int blockIndex) {
15011502
*
15021503
* @param blockIndex the row group (block) index
15031504
* @return row ranges within the block that may pass the configured filter
1505+
* @throws IllegalArgumentException if {@code blockIndex} is out of range
15041506
*/
15051507
public RowRanges getRowRanges(int blockIndex) {
1508+
if (blockIndex < 0 || blockIndex >= blocks.size()) {
1509+
throw new IllegalArgumentException(String.format(
1510+
"Invalid block index %s, the valid block index range are: [%s, %s]",
1511+
blockIndex, 0, blocks.size() - 1));
1512+
}
1513+
long rowCount = blocks.get(blockIndex).getRowCount();
1514+
if (rowCount == 0L) {
1515+
return RowRanges.EMPTY;
1516+
}
15061517
if (!FilterCompat.isFilteringRequired(options.getRecordFilter())) {
1507-
return RowRanges.createSingle(blocks.get(blockIndex).getRowCount());
1518+
return RowRanges.createSingle(rowCount);
15081519
}
15091520
RowRanges rowRanges = blockRowRanges.get(blockIndex);
15101521
if (rowRanges == null) {

parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderRowRanges.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import static org.apache.parquet.hadoop.ParquetFileWriter.Mode.OVERWRITE;
2222
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertThrows;
2324
import static org.junit.Assert.assertTrue;
2425

2526
import java.io.File;
@@ -93,4 +94,13 @@ public void getRowRangesWithoutFilterCoversAllRows() throws IOException {
9394
assertTrue(ranges.isOverlapping(0L, block.getRowCount() - 1));
9495
}
9596
}
97+
98+
@Test
99+
public void getRowRangesRejectsOutOfRangeBlockIndex() throws IOException {
100+
try (ParquetFileReader reader = openReader()) {
101+
int blockCount = reader.getRowGroups().size();
102+
assertThrows(IllegalArgumentException.class, () -> reader.getRowRanges(-1));
103+
assertThrows(IllegalArgumentException.class, () -> reader.getRowRanges(blockCount));
104+
}
105+
}
96106
}

0 commit comments

Comments
 (0)