Skip to content

Commit bf76666

Browse files
authored
Merge pull request #165 from GroundWu/fix_bugs_autoinc_batch
add test case for has auto-increment column in batch operation
2 parents 23ca287 + 7190c11 commit bf76666

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/test/java/com/alipay/oceanbase/rpc/ObTableClientAutoIncTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
import com.alipay.oceanbase.rpc.exception.ObTableException;
2323
import com.alipay.oceanbase.rpc.filter.ObCompareOp;
2424
import com.alipay.oceanbase.rpc.filter.ObTableValueFilter;
25+
import com.alipay.oceanbase.rpc.mutation.BatchOperation;
26+
import com.alipay.oceanbase.rpc.mutation.Insert;
27+
import com.alipay.oceanbase.rpc.mutation.Replace;
28+
import com.alipay.oceanbase.rpc.mutation.Row;
29+
import com.alipay.oceanbase.rpc.mutation.result.BatchOperationResult;
2530
import com.alipay.oceanbase.rpc.mutation.result.MutationResult;
2631
import com.alipay.oceanbase.rpc.property.Property;
2732
import com.alipay.oceanbase.rpc.protocol.payload.ResultCodes;
@@ -89,6 +94,13 @@ private void dropTable(String tableName) throws SQLException {
8994
statement.execute("drop table " + tableName);
9095
}
9196

97+
private void deleteTable(String tableName) throws SQLException {
98+
// use sql to drop table
99+
Connection connection = ObTableClientTestUtil.getConnection();
100+
Statement statement = connection.createStatement();
101+
statement.execute("delete from " + tableName);
102+
}
103+
92104
@Test
93105
// Test auto increment on rowkey
94106
public void testAutoIncrementRowkey() throws Exception {
@@ -564,4 +576,69 @@ public void testAutoColumnRowKey() throws Exception {
564576
} finally {
565577
}
566578
}
579+
580+
@Test
581+
public void test_autoinc_in_batch() throws Exception {
582+
// auto increment is rowKey
583+
test_autoinc_in_batch_inner("test_auto_increment_rk_batch", false, false);
584+
test_autoinc_in_batch_inner("test_auto_increment_rk_batch_ttl", false, false);
585+
test_autoinc_in_batch_inner("test_auto_increment_rk_batch", false, true);
586+
test_autoinc_in_batch_inner("test_auto_increment_rk_batch_ttl", false, true);
587+
// auto increment is not rowKey
588+
test_autoinc_in_batch_inner("test_auto_increment_batch", true, false);
589+
test_autoinc_in_batch_inner("test_auto_increment_batch_ttl", true, false);
590+
test_autoinc_in_batch_inner("test_auto_increment_batch", true, true);
591+
test_autoinc_in_batch_inner("test_auto_increment_batch_ttl", true, true);
592+
}
593+
594+
public void test_autoinc_in_batch_inner(String tableName, boolean useAutoinc, boolean useReplace) throws Exception {
595+
try {
596+
int batchSize = 10;
597+
BatchOperation batchOperation = client.batchOperation(tableName);
598+
for (int i = 0; i < batchSize; i++) {
599+
Long c1_val = Long.valueOf(i + 100);
600+
Long c2_val = useAutoinc ? null : Long.valueOf(i + 1000);
601+
String c3_val = String.format("col_%d", i);
602+
if (useReplace) {
603+
Replace replace = new Replace();
604+
replace.setRowKey(row(colVal("c1", c1_val)));
605+
replace.addMutateRow(row(colVal("c2", c2_val), colVal("c3", c3_val)));
606+
batchOperation.addOperation(replace);
607+
} else {
608+
Insert insert = new Insert();
609+
insert.setRowKey(row(colVal("c1", c1_val)));
610+
insert.addMutateRow(row(colVal("c2", c2_val), colVal("c3", c3_val)));
611+
batchOperation.addOperation(insert);
612+
}
613+
614+
}
615+
BatchOperationResult batchOperationResult = batchOperation.execute();
616+
for (int i = 0; i < batchSize; i++) {
617+
Assert.assertEquals(1, batchOperationResult.get(i).getAffectedRows());
618+
}
619+
// check result
620+
TableQuery tableQuery = client.query(tableName);
621+
tableQuery.select("c1", "c2", "c3");
622+
tableQuery.addScanRange(new Object[]{100L}, new Object[]{Long.valueOf(100 + batchSize - 1)});
623+
QueryResultSet result = tableQuery.execute();
624+
int i = 0;
625+
Long c2_val = -1L;
626+
while (result.next()) {
627+
Row row = result.getResultRow();
628+
Assert.assertEquals(Long.valueOf(i + 100), row.get("c1"));
629+
if (useAutoinc) {
630+
Assert.assertTrue(c2_val < (Long)row.get("c2"));
631+
c2_val = (Long)row.get("c2");
632+
} else {
633+
Assert.assertEquals(Long.valueOf(i + 1000), row.get("c2"));
634+
}
635+
Assert.assertEquals(String.format("col_%d", i), row.get("c3"));
636+
i++;
637+
}
638+
Assert.assertEquals(batchSize, i);
639+
} finally {
640+
deleteTable(tableName);
641+
}
642+
}
643+
567644
}

src/test/resources/ci.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,33 @@ CREATE TABLE IF NOT EXISTS `audit_test` (
597597
PRIMARY KEY (`c1`)
598598
);
599599

600+
CREATE TABLE IF NOT EXISTS `test_auto_increment_rk_batch` (
601+
`c1` bigint(20) NOT NULL AUTO_INCREMENT,
602+
`c2` bigint(20) NOT NULL,
603+
`c3` varchar(20) DEFAULT "hello",
604+
PRIMARY KEY (`c1`)
605+
);
606+
607+
CREATE TABLE IF NOT EXISTS `test_auto_increment_rk_batch_ttl` (
608+
`c1` bigint(20) NOT NULL AUTO_INCREMENT,
609+
`c2` bigint(20) NOT NULL,
610+
`c3` varchar(20) DEFAULT "hello",
611+
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
612+
PRIMARY KEY (`c1`)
613+
) TTL(update_time + INTERVAL 300 SECOND);
614+
615+
CREATE TABLE IF NOT EXISTS `test_auto_increment_batch` (
616+
`c1` bigint(20) NOT NULL,
617+
`c2` bigint(20) NOT NULL AUTO_INCREMENT,
618+
`c3` varchar(20) DEFAULT "hello",
619+
PRIMARY KEY (`c1`)
620+
);
621+
622+
CREATE TABLE IF NOT EXISTS `test_auto_increment_batch_ttl` (
623+
`c1` bigint(20) NOT NULL,
624+
`c2` bigint(20) NOT NULL AUTO_INCREMENT,
625+
`c3` varchar(20) DEFAULT "hello",
626+
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
627+
PRIMARY KEY (`c1`)
628+
) TTL(update_time + INTERVAL 300 SECOND);
600629
alter system set kv_hotkey_throttle_threshold = 50;

0 commit comments

Comments
 (0)