Skip to content
Permalink
Browse files
IGNITE-16630 Fix JDBC tests related to MERGE statement - Fixes #710.
Signed-off-by: zstan <stanilovsky@gmail.com>
  • Loading branch information
zstan committed Mar 11, 2022
1 parent b5b3e71 commit 71ec36cbccd8c461e3c5f8294000e8a0e56c3924
Showing 1 changed file with 25 additions and 136 deletions.
@@ -247,64 +247,46 @@ public void testBatchParseException() throws Exception {
}
}

@Disabled("https://issues.apache.org/jira/browse/IGNITE-16489")
@Test
public void testBatchMerge() throws SQLException {
final int batchSize = 7;

for (int idx = 0, i = 0; i < batchSize; ++i, idx += i) {
stmt.addBatch("merge into Person (id, firstName, lastName, age) values "
+ generateValues(idx, i + 1));
}

int[] updCnts = stmt.executeBatch();
final int batchSize = 5;

assertEquals(batchSize, updCnts.length, "Invalid update counts size");

for (int i = 0; i < batchSize; ++i) {
assertEquals(i + 1, updCnts[i], "Invalid update count");
try (Statement statement = conn.createStatement()) {
statement.executeUpdate("CREATE TABLE Src(id INT PRIMARY KEY, firstName VARCHAR, lastName VARCHAR, age INT)");
}
}

@Disabled("https://issues.apache.org/jira/browse/IGNITE-16489")
@Test
public void testBatchMergeParseException() throws Exception {
final int batchSize = 7;

final int failedIdx = 5;
try {
for (int idx = 0, i = 0; i < batchSize; ++i, idx += i) {
stmt.addBatch("INSERT INTO Person (id, firstName, lastName, age) values "
+ generateValues(idx, i + 1));

for (int idx = 0, i = 0; i < failedIdx; ++i, idx += i) {
stmt.addBatch("merge into Person (id, firstName, lastName, age) values "
+ generateValues(idx, i + 1));
}
stmt.addBatch("INSERT INTO Src (id, firstName, lastName, age) values "
+ generateValues(idx + 1, i + 1));
}

stmt.addBatch("merge into Person (id, firstName, lastName, age) values (4444, 'FAIL', 1, 1, 1)");
stmt.executeBatch();

stmt.addBatch("merge into Person (id, firstName, lastName, age) values "
+ generateValues(100, 7));
String sql = "MERGE INTO Person dst USING Src src ON dst.id = src.id "
+ "WHEN MATCHED THEN UPDATE SET firstName = src.firstName "
+ "WHEN NOT MATCHED THEN INSERT (id, firstName, lastName) VALUES (src.id, src.firstName, src.lastName)";

try {
stmt.executeBatch();
stmt.addBatch(sql);

fail("BatchUpdateException must be thrown");
} catch (BatchUpdateException e) {
int[] updCnts = e.getUpdateCounts();
stmt.addBatch("INSERT INTO Person (id, firstName, lastName, age) values " + valuesRow(100));

assertEquals(batchSize, updCnts.length, "Invalid update counts size");
stmt.addBatch(sql);

for (int i = 0; i < batchSize; ++i) {
assertEquals(i != failedIdx ? i + 1 : Statement.EXECUTE_FAILED, updCnts[i],
"Invalid update count: " + i);
}
int[] updCnts = stmt.executeBatch();

if (!e.getMessage().contains("Value conversion failed")) {
log.error("Invalid exception: ", e);
assertEquals(3, updCnts.length, "Invalid update counts size");

fail();
// result size is equal to mathematical progression:
assertEquals((1 + batchSize) * batchSize / 2, updCnts[0], "Invalid update counts");
assertEquals((1 + batchSize) * batchSize / 2, updCnts[2], "Invalid update counts");
} finally {
try (Statement statement = conn.createStatement()) {
statement.executeUpdate("DROP TABLE Src;");
}

assertEquals(SqlStateCode.INTERNAL_ERROR, e.getSQLState(), "Invalid SQL state.");
assertEquals(IgniteQueryErrorCode.UNKNOWN, e.getErrorCode(), "Invalid error code.");
}
}

@@ -471,99 +453,6 @@ public void testBatchExceptionPrepared() throws Exception {
}
}

@Disabled("https://issues.apache.org/jira/browse/IGNITE-16489")
@Test
public void testBatchMergePrepared() throws SQLException {
final int batchSize = 10;

pstmt = conn.prepareStatement("merge into Person(_key, id, firstName, lastName, age) values "
+ "(?, ?, ?, ?, ?)");

for (int i = 0; i < batchSize; ++i) {
int paramCnt = 1;

pstmt.setString(paramCnt++, "p" + i);
pstmt.setInt(paramCnt++, i);
pstmt.setString(paramCnt++, "Name" + i);
pstmt.setString(paramCnt++, "Lastname" + i);
pstmt.setInt(paramCnt++, 20 + i);

pstmt.addBatch();
}

int[] updCnts = pstmt.executeBatch();

assertEquals(batchSize, updCnts.length, "Invalid update counts size");

for (int i = 0; i < batchSize; ++i) {
assertEquals(1, updCnts[i], "Invalid update count");
}
}

@Disabled("https://issues.apache.org/jira/browse/IGNITE-16489")
@Test
public void testBatchMergeExceptionPrepared() throws Exception {
final int batchSize = 7;

final int failedIdx = 5;

pstmt = conn.prepareStatement("merge into Person(_key, id, firstName, lastName, age) values "
+ "(?, ?, ?, ?, ?)");

for (int i = 0; i < failedIdx; ++i) {
int paramCnt = 1;

pstmt.setString(paramCnt++, "p" + i);
pstmt.setInt(paramCnt++, i);
pstmt.setString(paramCnt++, "Name" + i);
pstmt.setString(paramCnt++, "Lastname" + i);
pstmt.setInt(paramCnt++, 20 + i);

pstmt.addBatch();
}

int paramCnt = 1;
pstmt.setString(paramCnt++, "p" + failedIdx);
pstmt.setString(paramCnt++, "FAIL");
pstmt.setString(paramCnt++, "Name" + failedIdx);
pstmt.setString(paramCnt++, "Lastname" + failedIdx);
pstmt.setInt(paramCnt++, 20 + failedIdx);

pstmt.addBatch();

paramCnt = 1;
pstmt.setString(paramCnt++, "p" + failedIdx + 1);
pstmt.setInt(paramCnt++, failedIdx + 1);
pstmt.setString(paramCnt++, "Name" + failedIdx + 1);
pstmt.setString(paramCnt++, "Lastname" + failedIdx + 1);
pstmt.setInt(paramCnt++, 20 + failedIdx + 1);

pstmt.addBatch();

try {
int[] res = pstmt.executeBatch();

fail("BatchUpdateException must be thrown res=" + Arrays.toString(res));
} catch (BatchUpdateException e) {
int[] updCnts = e.getUpdateCounts();

assertEquals(batchSize, updCnts.length, "Invalid update counts size");

for (int i = 0; i < batchSize; ++i) {
assertEquals(i != failedIdx ? 1 : Statement.EXECUTE_FAILED, updCnts[i], "Invalid update count");
}

if (!e.getMessage().contains("Value conversion failed")) {
log.error("Invalid exception: ", e);

fail();
}

assertEquals(SqlStateCode.INTERNAL_ERROR, e.getSQLState(), "Invalid SQL state.");
assertEquals(IgniteQueryErrorCode.UNKNOWN, e.getErrorCode(), "Invalid error code.");
}
}

/**
* Populates table 'Person' with entities.
*

0 comments on commit 71ec36c

Please sign in to comment.