|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * Copyright The original authors |
| 5 | + * |
| 6 | + * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + */ |
| 8 | +package dev.hardwood.internal.reader; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.UncheckedIOException; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import dev.hardwood.internal.thrift.ColumnIndexReader; |
| 15 | +import dev.hardwood.internal.thrift.OffsetIndexReader; |
| 16 | +import dev.hardwood.internal.thrift.ThriftCompactReader; |
| 17 | +import dev.hardwood.metadata.ColumnIndex; |
| 18 | +import dev.hardwood.metadata.OffsetIndex; |
| 19 | +import dev.hardwood.metadata.PageLocation; |
| 20 | +import dev.hardwood.metadata.RowGroup; |
| 21 | +import dev.hardwood.reader.FilterPredicate; |
| 22 | +import dev.hardwood.reader.FilterPredicate.And; |
| 23 | +import dev.hardwood.reader.FilterPredicate.BinaryColumnPredicate; |
| 24 | +import dev.hardwood.reader.FilterPredicate.BooleanColumnPredicate; |
| 25 | +import dev.hardwood.reader.FilterPredicate.DoubleColumnPredicate; |
| 26 | +import dev.hardwood.reader.FilterPredicate.FloatColumnPredicate; |
| 27 | +import dev.hardwood.reader.FilterPredicate.IntColumnPredicate; |
| 28 | +import dev.hardwood.reader.FilterPredicate.LongColumnPredicate; |
| 29 | +import dev.hardwood.reader.FilterPredicate.Not; |
| 30 | +import dev.hardwood.reader.FilterPredicate.Or; |
| 31 | +import dev.hardwood.schema.FileSchema; |
| 32 | + |
| 33 | +/// Evaluates a [FilterPredicate] against per-page statistics from the Column Index |
| 34 | +/// to produce [RowRanges] representing rows that might match. |
| 35 | +/// |
| 36 | +/// This is the page-level equivalent of [RowGroupFilterEvaluator]. While that class |
| 37 | +/// decides whether an entire row group can be skipped, this class determines which |
| 38 | +/// pages within a surviving row group can be skipped. |
| 39 | +public class PageFilterEvaluator { |
| 40 | + |
| 41 | + /// Computes the row ranges within a row group that might match the given predicate, |
| 42 | + /// based on per-page min/max statistics from the Column Index. |
| 43 | + /// |
| 44 | + /// Returns `RowRanges.all()` when the Column Index is absent or the predicate |
| 45 | + /// cannot be evaluated at the page level (conservative fallback). |
| 46 | + /// |
| 47 | + /// @param predicate the filter predicate to evaluate |
| 48 | + /// @param rowGroup the row group to evaluate against |
| 49 | + /// @param schema the file schema for column resolution |
| 50 | + /// @param indexBuffers pre-fetched index buffers for the row group |
| 51 | + /// @return row ranges that might contain matching rows |
| 52 | + public static RowRanges computeMatchingRows(FilterPredicate predicate, RowGroup rowGroup, |
| 53 | + FileSchema schema, RowGroupIndexBuffers indexBuffers) { |
| 54 | + long rowCount = rowGroup.numRows(); |
| 55 | + return evaluate(predicate, rowGroup, schema, indexBuffers, rowCount); |
| 56 | + } |
| 57 | + |
| 58 | + private static RowRanges evaluate(FilterPredicate predicate, RowGroup rowGroup, |
| 59 | + FileSchema schema, RowGroupIndexBuffers indexBuffers, long rowCount) { |
| 60 | + return switch (predicate) { |
| 61 | + case IntColumnPredicate p -> evaluateLeaf(p.column(), rowGroup, schema, indexBuffers, rowCount, |
| 62 | + (ci, i) -> { |
| 63 | + int min = StatisticsDecoder.decodeInt(ci.minValues().get(i)); |
| 64 | + int max = StatisticsDecoder.decodeInt(ci.maxValues().get(i)); |
| 65 | + return RowGroupFilterEvaluator.canDrop(p.op(), p.value(), min, max); |
| 66 | + }); |
| 67 | + case LongColumnPredicate p -> evaluateLeaf(p.column(), rowGroup, schema, indexBuffers, rowCount, |
| 68 | + (ci, i) -> { |
| 69 | + long min = StatisticsDecoder.decodeLong(ci.minValues().get(i)); |
| 70 | + long max = StatisticsDecoder.decodeLong(ci.maxValues().get(i)); |
| 71 | + return RowGroupFilterEvaluator.canDrop(p.op(), p.value(), min, max); |
| 72 | + }); |
| 73 | + case FloatColumnPredicate p -> evaluateLeaf(p.column(), rowGroup, schema, indexBuffers, rowCount, |
| 74 | + (ci, i) -> { |
| 75 | + float min = StatisticsDecoder.decodeFloat(ci.minValues().get(i)); |
| 76 | + float max = StatisticsDecoder.decodeFloat(ci.maxValues().get(i)); |
| 77 | + return RowGroupFilterEvaluator.canDropFloat(p.op(), p.value(), min, max); |
| 78 | + }); |
| 79 | + case DoubleColumnPredicate p -> evaluateLeaf(p.column(), rowGroup, schema, indexBuffers, rowCount, |
| 80 | + (ci, i) -> { |
| 81 | + double min = StatisticsDecoder.decodeDouble(ci.minValues().get(i)); |
| 82 | + double max = StatisticsDecoder.decodeDouble(ci.maxValues().get(i)); |
| 83 | + return RowGroupFilterEvaluator.canDropDouble(p.op(), p.value(), min, max); |
| 84 | + }); |
| 85 | + case BooleanColumnPredicate p -> evaluateLeaf(p.column(), rowGroup, schema, indexBuffers, rowCount, |
| 86 | + (ci, i) -> { |
| 87 | + int min = StatisticsDecoder.decodeBoolean(ci.minValues().get(i)) ? 1 : 0; |
| 88 | + int max = StatisticsDecoder.decodeBoolean(ci.maxValues().get(i)) ? 1 : 0; |
| 89 | + int value = p.value() ? 1 : 0; |
| 90 | + return RowGroupFilterEvaluator.canDrop(p.op(), value, min, max); |
| 91 | + }); |
| 92 | + case BinaryColumnPredicate p -> evaluateLeaf(p.column(), rowGroup, schema, indexBuffers, rowCount, |
| 93 | + (ci, i) -> { |
| 94 | + byte[] min = ci.minValues().get(i); |
| 95 | + byte[] max = ci.maxValues().get(i); |
| 96 | + int cmpMin = StatisticsDecoder.compareBinary(p.value(), min); |
| 97 | + int cmpMax = StatisticsDecoder.compareBinary(p.value(), max); |
| 98 | + return RowGroupFilterEvaluator.canDropCompared(p.op(), cmpMin, cmpMax, |
| 99 | + StatisticsDecoder.compareBinary(min, max)); |
| 100 | + }); |
| 101 | + case And a -> { |
| 102 | + RowRanges result = RowRanges.all(rowCount); |
| 103 | + for (FilterPredicate child : a.filters()) { |
| 104 | + result = result.intersect(evaluate(child, rowGroup, schema, indexBuffers, rowCount)); |
| 105 | + } |
| 106 | + yield result; |
| 107 | + } |
| 108 | + case Or o -> { |
| 109 | + RowRanges result = null; |
| 110 | + for (FilterPredicate child : o.filters()) { |
| 111 | + RowRanges childRanges = evaluate(child, rowGroup, schema, indexBuffers, rowCount); |
| 112 | + result = (result == null) ? childRanges : result.union(childRanges); |
| 113 | + } |
| 114 | + yield (result != null) ? result : RowRanges.all(rowCount); |
| 115 | + } |
| 116 | + case Not ignored -> RowRanges.all(rowCount); |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + /// Evaluates a leaf predicate against the Column Index for a single column, |
| 121 | + /// producing RowRanges for pages that cannot be dropped. |
| 122 | + private static RowRanges evaluateLeaf(String columnName, RowGroup rowGroup, |
| 123 | + FileSchema schema, RowGroupIndexBuffers indexBuffers, long rowCount, |
| 124 | + PageCanDropTest canDropTest) { |
| 125 | + |
| 126 | + int columnIndex = RowGroupFilterEvaluator.resolveColumnIndex(columnName, rowGroup, schema); |
| 127 | + if (columnIndex < 0) { |
| 128 | + return RowRanges.all(rowCount); |
| 129 | + } |
| 130 | + |
| 131 | + ColumnIndexBuffers colBuffers = indexBuffers.forColumn(columnIndex); |
| 132 | + if (colBuffers == null || colBuffers.columnIndex() == null || colBuffers.offsetIndex() == null) { |
| 133 | + return RowRanges.all(rowCount); |
| 134 | + } |
| 135 | + |
| 136 | + ColumnIndex columnIdx; |
| 137 | + OffsetIndex offsetIdx; |
| 138 | + try { |
| 139 | + columnIdx = ColumnIndexReader.read(new ThriftCompactReader(colBuffers.columnIndex())); |
| 140 | + offsetIdx = OffsetIndexReader.read(new ThriftCompactReader(colBuffers.offsetIndex())); |
| 141 | + } |
| 142 | + catch (IOException e) { |
| 143 | + throw new UncheckedIOException("Failed to parse Column/Offset Index for column '" + columnName + "'", e); |
| 144 | + } |
| 145 | + |
| 146 | + return evaluatePages(columnIdx, offsetIdx, rowCount, canDropTest); |
| 147 | + } |
| 148 | + |
| 149 | + /// Evaluates a keep bitmap for pages using pre-parsed Column Index and Offset Index. |
| 150 | + static RowRanges evaluatePages(ColumnIndex columnIdx, OffsetIndex offsetIdx, |
| 151 | + long rowCount, PageCanDropTest canDropTest) { |
| 152 | + List<PageLocation> pages = offsetIdx.pageLocations(); |
| 153 | + int pageCount = pages.size(); |
| 154 | + boolean[] keep = new boolean[pageCount]; |
| 155 | + |
| 156 | + for (int i = 0; i < pageCount; i++) { |
| 157 | + // Null-only pages cannot match any value predicate |
| 158 | + if (columnIdx.nullPages().get(i)) { |
| 159 | + continue; |
| 160 | + } |
| 161 | + // Keep the page if it cannot be dropped |
| 162 | + keep[i] = !canDropTest.canDrop(columnIdx, i); |
| 163 | + } |
| 164 | + |
| 165 | + return RowRanges.fromPages(pages, keep, rowCount); |
| 166 | + } |
| 167 | + |
| 168 | + /// Functional interface for testing whether a page can be dropped based on its |
| 169 | + /// Column Index min/max values. |
| 170 | + @FunctionalInterface |
| 171 | + interface PageCanDropTest { |
| 172 | + boolean canDrop(ColumnIndex columnIndex, int pageIndex); |
| 173 | + } |
| 174 | +} |
0 commit comments