|
81 | 81 | import static org.apache.paimon.CoreOptions.ROW_TRACKING_ENABLED; |
82 | 82 | import static org.apache.paimon.CoreOptions.TARGET_FILE_SIZE; |
83 | 83 | import static org.apache.paimon.data.DataFormatTestUtil.internalRowToString; |
| 84 | +import static org.apache.paimon.globalindex.btree.BTreeIndexOptions.BTREE_INDEX_COMPRESSION; |
84 | 85 | import static org.apache.paimon.table.SimpleTableTestBase.getResult; |
85 | 86 | import static org.assertj.core.api.Assertions.assertThat; |
86 | 87 |
|
@@ -414,6 +415,7 @@ public void testBtreeIndexWrite() throws Exception { |
414 | 415 | testBtreeIndexWriteString(); |
415 | 416 | testBtreeIndexWriteInt(); |
416 | 417 | testBtreeIndexWriteBigInt(); |
| 418 | + testBtreeIndexWriteLarge(); |
417 | 419 | } |
418 | 420 |
|
419 | 421 | private void testBtreeIndexWriteString() throws Exception { |
@@ -498,6 +500,75 @@ private <T> void testBtreeIndexWriteGeneric( |
498 | 500 | assertThat(result).containsOnly("v2"); |
499 | 501 | } |
500 | 502 |
|
| 503 | + private void testBtreeIndexWriteLarge() throws Exception { |
| 504 | + // create table |
| 505 | + RowType rowType = |
| 506 | + RowType.of( |
| 507 | + new DataType[] {DataTypes.STRING(), DataTypes.STRING()}, |
| 508 | + new String[] {"k", "v"}); |
| 509 | + Options options = new Options(); |
| 510 | + Path tablePath = new Path(warehouse.toString() + "/default.db/test_btree_index_large"); |
| 511 | + options.set(PATH, tablePath.toString()); |
| 512 | + options.set(ROW_TRACKING_ENABLED, true); |
| 513 | + options.set(DATA_EVOLUTION_ENABLED, true); |
| 514 | + options.set(GLOBAL_INDEX_ENABLED, true); |
| 515 | + options.set(BTREE_INDEX_COMPRESSION, "zstd"); |
| 516 | + TableSchema tableSchema = |
| 517 | + SchemaUtils.forceCommit( |
| 518 | + new SchemaManager(LocalFileIO.create(), tablePath), |
| 519 | + new Schema( |
| 520 | + rowType.getFields(), |
| 521 | + Collections.emptyList(), |
| 522 | + Collections.emptyList(), |
| 523 | + options.toMap(), |
| 524 | + "")); |
| 525 | + AppendOnlyFileStoreTable table = |
| 526 | + new AppendOnlyFileStoreTable( |
| 527 | + FileIOFinder.find(tablePath), |
| 528 | + tablePath, |
| 529 | + tableSchema, |
| 530 | + CatalogEnvironment.empty()); |
| 531 | + |
| 532 | + // write data |
| 533 | + BatchWriteBuilder writeBuilder = table.newBatchWriteBuilder(); |
| 534 | + try (BatchTableWrite write = writeBuilder.newWrite(); |
| 535 | + BatchTableCommit commit = writeBuilder.newCommit()) { |
| 536 | + for (int i = 0; i < 2000; i++) { |
| 537 | + write.write( |
| 538 | + GenericRow.of( |
| 539 | + BinaryString.fromString("k" + i), |
| 540 | + BinaryString.fromString("v" + i))); |
| 541 | + } |
| 542 | + commit.commit(write.prepareCommit()); |
| 543 | + } |
| 544 | + |
| 545 | + // build index |
| 546 | + BTreeGlobalIndexBuilder builder = |
| 547 | + new BTreeGlobalIndexBuilder(table).withIndexType("btree").withIndexField("k"); |
| 548 | + try (BatchTableCommit commit = writeBuilder.newCommit()) { |
| 549 | + commit.commit(builder.build(builder.scan(), IOManager.create(warehouse.toString()))); |
| 550 | + } |
| 551 | + |
| 552 | + // assert index |
| 553 | + List<IndexManifestEntry> indexEntries = |
| 554 | + table.indexManifestFileReader().read(table.latestSnapshot().get().indexManifest); |
| 555 | + assertThat(indexEntries) |
| 556 | + .singleElement() |
| 557 | + .matches(entry -> entry.indexFile().rowCount() == 2000); |
| 558 | + |
| 559 | + // read index |
| 560 | + PredicateBuilder predicateBuilder = new PredicateBuilder(table.rowType()); |
| 561 | + ReadBuilder readBuilder = |
| 562 | + table.newReadBuilder() |
| 563 | + .withFilter(predicateBuilder.equal(0, BinaryString.fromString("k2"))); |
| 564 | + List<String> result = new ArrayList<>(); |
| 565 | + readBuilder |
| 566 | + .newRead() |
| 567 | + .createReader(readBuilder.newScan().plan()) |
| 568 | + .forEachRemaining(r -> result.add(r.getString(1).toString())); |
| 569 | + assertThat(result).containsOnly("v2"); |
| 570 | + } |
| 571 | + |
501 | 572 | // Helper method from TableTestBase |
502 | 573 | protected Identifier identifier(String tableName) { |
503 | 574 | return new Identifier(database, tableName); |
|
0 commit comments