|
25 | 25 | import org.apache.paimon.data.serializer.InternalRowSerializer; |
26 | 26 | import org.apache.paimon.fs.Path; |
27 | 27 | import org.apache.paimon.fs.local.LocalFileIO; |
| 28 | +import org.apache.paimon.predicate.Predicate; |
| 29 | +import org.apache.paimon.predicate.PredicateBuilder; |
28 | 30 | import org.apache.paimon.reader.RecordReader; |
29 | 31 | import org.apache.paimon.table.FormatTable; |
30 | 32 | import org.apache.paimon.table.sink.BatchTableCommit; |
|
40 | 42 | import org.junit.jupiter.api.Test; |
41 | 43 | import org.junit.jupiter.api.io.TempDir; |
42 | 44 |
|
| 45 | +import javax.annotation.Nullable; |
| 46 | + |
43 | 47 | import java.util.ArrayList; |
44 | 48 | import java.util.Arrays; |
45 | 49 | import java.util.Collections; |
@@ -186,6 +190,53 @@ void testInvalidCompressionPropagatedFromWriter() { |
186 | 190 | .hasMessageContaining("Mosaic format only supports zstd"); |
187 | 191 | } |
188 | 192 |
|
| 193 | + @Test |
| 194 | + void testFilterOnDataColumnAfterPartitionColumn() throws Exception { |
| 195 | + RowType rowType = partitionFirstRowType(); |
| 196 | + FormatTable table = writePartitionFirstTable(rowType, "a,b"); |
| 197 | + PredicateBuilder builder = new PredicateBuilder(rowType); |
| 198 | + |
| 199 | + assertThat(readAll(table, rowType, builder.equal(1, 1))) |
| 200 | + .extracting(row -> row.getInt(1)) |
| 201 | + .contains(1); |
| 202 | + assertThat(readAll(table, rowType, builder.equal(2, 101))) |
| 203 | + .extracting(row -> row.getInt(2)) |
| 204 | + .contains(101); |
| 205 | + assertThat(readAll(table, rowType, builder.greaterOrEqual(1, 105))).isEmpty(); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + void testFilterOnColumnWithoutStatsAfterPartitionColumn() throws Exception { |
| 210 | + RowType rowType = partitionFirstRowType(); |
| 211 | + FormatTable table = writePartitionFirstTable(rowType, "b"); |
| 212 | + PredicateBuilder builder = new PredicateBuilder(rowType); |
| 213 | + |
| 214 | + assertThat(readAll(table, rowType, builder.equal(1, 1))) |
| 215 | + .extracting(row -> row.getInt(1)) |
| 216 | + .contains(1); |
| 217 | + } |
| 218 | + |
| 219 | + private static RowType partitionFirstRowType() { |
| 220 | + return RowType.builder() |
| 221 | + .field("dt", DataTypes.STRING()) |
| 222 | + .field("a", DataTypes.INT()) |
| 223 | + .field("b", DataTypes.INT()) |
| 224 | + .build(); |
| 225 | + } |
| 226 | + |
| 227 | + private FormatTable writePartitionFirstTable(RowType rowType, String statsColumns) |
| 228 | + throws Exception { |
| 229 | + Map<String, String> options = new HashMap<>(); |
| 230 | + options.put(MosaicFileFormat.STATS_COLUMNS.key(), statsColumns); |
| 231 | + FormatTable table = buildFormatTable(rowType, Collections.singletonList("dt"), options); |
| 232 | + List<InternalRow> rows = new ArrayList<>(); |
| 233 | + for (int i = 1; i <= 10; i++) { |
| 234 | + rows.add(GenericRow.of(BinaryString.fromString("p"), i, 100 + i)); |
| 235 | + } |
| 236 | + writeAll(table, rows); |
| 237 | + return table; |
| 238 | + } |
| 239 | + |
189 | 240 | private FormatTable buildFormatTable( |
190 | 241 | RowType rowType, List<String> partitionKeys, Map<String, String> options) { |
191 | 242 | String location = new Path(tempPath.toUri()).toString(); |
@@ -218,7 +269,15 @@ private static void writeAll(FormatTable table, List<InternalRow> rows) throws E |
218 | 269 | } |
219 | 270 |
|
220 | 271 | private static List<InternalRow> readAll(FormatTable table, RowType rowType) throws Exception { |
| 272 | + return readAll(table, rowType, null); |
| 273 | + } |
| 274 | + |
| 275 | + private static List<InternalRow> readAll( |
| 276 | + FormatTable table, RowType rowType, @Nullable Predicate filter) throws Exception { |
221 | 277 | ReadBuilder readBuilder = table.newReadBuilder(); |
| 278 | + if (filter != null) { |
| 279 | + readBuilder.withFilter(filter); |
| 280 | + } |
222 | 281 | List<Split> splits = readBuilder.newScan().plan().splits(); |
223 | 282 | assertThat(splits).isNotEmpty(); |
224 | 283 |
|
|
0 commit comments