|
25 | 25 | import org.junit.Before; |
26 | 26 | import org.junit.Test; |
27 | 27 |
|
28 | | -import java.sql.Connection; |
29 | | -import java.sql.ResultSet; |
30 | | -import java.sql.Statement; |
31 | | -import java.sql.Timestamp; |
| 28 | +import java.sql.*; |
32 | 29 | import java.util.Map; |
33 | 30 |
|
| 31 | +import static com.alipay.oceanbase.rpc.ObGlobal.OB_VERSION_4_3_4_0; |
34 | 32 | import static com.alipay.oceanbase.rpc.mutation.MutationFactory.colVal; |
35 | 33 | import static org.junit.Assert.assertEquals; |
36 | 34 |
|
@@ -574,4 +572,92 @@ public void test_ttl_query_with_global_index() throws Exception { |
574 | 572 | } |
575 | 573 | } |
576 | 574 |
|
| 575 | + @Test |
| 576 | + public void test_for_auto_split() throws Exception { |
| 577 | + if (ObGlobal.OB_VERSION < OB_VERSION_4_3_4_0) { |
| 578 | + return; |
| 579 | + } |
| 580 | + String tableName = "test_auto_split_global_index"; |
| 581 | + String creteTableSql = "create table if not exists `test_auto_split_global_index` (" + |
| 582 | + " `c1` int," + |
| 583 | + " `c2` varchar(128), " + |
| 584 | + " `c3` varchar(128)," + |
| 585 | + " primary key(`c1`)," + |
| 586 | + " key `g_idx` (`c2`) global)" + |
| 587 | + " partition by range() size ('128MB');"; |
| 588 | + executeSQL(creteTableSql); |
| 589 | + try { |
| 590 | + String c1 = "c1"; |
| 591 | + String c2 = "c2"; |
| 592 | + String c3 = "c3"; |
| 593 | + int rowCnt = 10; |
| 594 | + // prepare data |
| 595 | + for (int i = 0; i < rowCnt; i++) { |
| 596 | + client.insert(tableName).setRowKey(colVal(c1, i)) |
| 597 | + .addMutateColVal(colVal(c2, c2+"_"+i), colVal(c3, c3+"_"+i)) |
| 598 | + .execute(); |
| 599 | + } |
| 600 | + QueryResultSet resultSet; |
| 601 | + int scanCnt= 0; |
| 602 | + // query with primary index |
| 603 | + resultSet = client.query(tableName) |
| 604 | + .setScanRangeColumns("c1") |
| 605 | + .addScanRange(new Object[] { 0 }, new Object[] { rowCnt + 1 }).execute(); |
| 606 | + while(resultSet.next()) { |
| 607 | + Map<String, Object> res = resultSet.getRow(); |
| 608 | + Assert.assertEquals(res.get(c1), scanCnt); |
| 609 | + Assert.assertEquals(res.get(c2), c2+"_"+scanCnt); |
| 610 | + Assert.assertEquals(res.get(c3), c3+"_"+scanCnt); |
| 611 | + scanCnt++; |
| 612 | + } |
| 613 | + Assert.assertEquals(rowCnt, scanCnt); |
| 614 | + |
| 615 | + // query with global index without lookup table |
| 616 | + scanCnt = 0; |
| 617 | + resultSet = client.query(tableName) |
| 618 | + .indexName("g_idx") |
| 619 | + .setScanRangeColumns(c2) |
| 620 | + .select(c1,c2) |
| 621 | + .addScanRange(new Object[] { "c2_0" }, new Object[] { "c2_9" }).execute(); |
| 622 | + while(resultSet.next()) { |
| 623 | + Map<String, Object> res = resultSet.getRow(); |
| 624 | + Assert.assertEquals(res.get(c1), scanCnt); |
| 625 | + Assert.assertEquals(res.get(c2), c2+"_"+scanCnt); |
| 626 | + scanCnt++; |
| 627 | + } |
| 628 | + Assert.assertEquals(rowCnt, scanCnt); |
| 629 | + |
| 630 | + // query with gloabl index with lookup table |
| 631 | + scanCnt = 0; |
| 632 | + resultSet = client.query(tableName) |
| 633 | + .indexName("g_idx") |
| 634 | + .setScanRangeColumns("c2") |
| 635 | + .addScanRange(new Object[] { "c2_0" }, new Object[] { "c2_9" }).execute(); |
| 636 | + while(resultSet.next()) { |
| 637 | + Map<String, Object> res = resultSet.getRow(); |
| 638 | + Assert.assertEquals(res.get(c1), scanCnt); |
| 639 | + Assert.assertEquals(res.get(c2), c2+"_"+scanCnt); |
| 640 | + Assert.assertEquals(res.get(c3), c3+"_"+scanCnt); |
| 641 | + scanCnt++; |
| 642 | + } |
| 643 | + Assert.assertEquals(rowCnt, scanCnt); |
| 644 | + } catch (Exception e) { |
| 645 | + e.printStackTrace(); |
| 646 | + } finally { |
| 647 | + dropTable(tableName); |
| 648 | + } |
| 649 | + } |
| 650 | + |
| 651 | + private void executeSQL(String createSQL) throws SQLException { |
| 652 | + Connection connection = ObTableClientTestUtil.getConnection(); |
| 653 | + Statement statement = connection.createStatement(); |
| 654 | + statement.execute(createSQL); |
| 655 | + } |
| 656 | + |
| 657 | + private void dropTable(String tableName) throws SQLException { |
| 658 | + // use sql to drop table |
| 659 | + Connection connection = ObTableClientTestUtil.getConnection(); |
| 660 | + Statement statement = connection.createStatement(); |
| 661 | + statement.execute("drop table " + tableName); |
| 662 | + } |
577 | 663 | } |
0 commit comments