提交 33a96ecc authored 作者: Owner's avatar Owner

Added extra test case for multiple updates

上级 7400c0ac
...@@ -106,7 +106,7 @@ public class MergeUsing extends Merge { ...@@ -106,7 +106,7 @@ public class MergeUsing extends Merge {
} }
} }
if(sourceKeysRemembered.containsKey(sourceKeyValuesList)){ if(sourceKeysRemembered.containsKey(sourceKeyValuesList)){
throw DbException.get(ErrorCode.DUPLICATE_KEY_1, "Merge using ON column expression, duplicates values found:keys" throw DbException.get(ErrorCode.DUPLICATE_KEY_1, "Merge using ON column expression, duplicate values found:keys"
+Arrays.asList(sourceKeys).toString()+":values:"+sourceKeyValuesList.toString() +Arrays.asList(sourceKeys).toString()+":values:"+sourceKeyValuesList.toString()
+":from:"+sourceTableFilter.getTable()+":alias:"+sourceTableFilter.getTableAlias()+":current row number:"+countInputRows +":from:"+sourceTableFilter.getTable()+":alias:"+sourceTableFilter.getTableAlias()+":current row number:"+countInputRows
+":conflicting row number:"+sourceKeysRemembered.get(sourceKeyValuesList)); +":conflicting row number:"+sourceKeysRemembered.get(sourceKeyValuesList));
...@@ -167,27 +167,27 @@ public class MergeUsing extends Merge { ...@@ -167,27 +167,27 @@ public class MergeUsing extends Merge {
sourceTableFilter.set(sourceRow); sourceTableFilter.set(sourceRow);
// try and perform an update // try and perform an update
int count = 0; int rowUpdateCount = 0;
System.out.println("onConditions="+onCondition.toString()); System.out.println("onConditions="+onCondition.toString());
if(updateCommand!=null){ if(updateCommand!=null){
System.out.println("updatePlanSQL="+updateCommand.getPlanSQL()); System.out.println("updatePlanSQL="+updateCommand.getPlanSQL());
count += updateCommand.update(); rowUpdateCount += updateCommand.update();
System.out.println("update.count="+count); System.out.println("update.count="+rowUpdateCount);
} }
if(deleteCommand!=null && count==0){ if(deleteCommand!=null && rowUpdateCount==0){
System.out.println("deleteCommand="+deleteCommand.getPlanSQL()); System.out.println("deleteCommand="+deleteCommand.getPlanSQL());
count += deleteCommand.update(); rowUpdateCount += deleteCommand.update();
System.out.println("delete.count="+count); System.out.println("delete.count="+rowUpdateCount);
} }
// if either updates do nothing, try an insert // if either updates do nothing, try an insert
if (count == 0) { if (rowUpdateCount == 0) {
count+=addRowByCommandInsert(session,newTargetRow); rowUpdateCount+=addRowByCommandInsert(session,newTargetRow);
//addRowByAPIInsert(session,newTargetRow); //addRowByAPIInsert(session,newTargetRow);
} else if (count != 1) { } else if (rowUpdateCount != 1) {
throw DbException.get(ErrorCode.DUPLICATE_KEY_1, targetTable.getSQL()); throw DbException.get(ErrorCode.DUPLICATE_KEY_1, "Duplicate key updated "+rowUpdateCount+" rows at once, only 1 expected:"+targetTable.getSQL());
} }
countUpdatedRows+=count; countUpdatedRows+=rowUpdateCount;
} }
......
...@@ -123,8 +123,19 @@ public class TestMergeUsing extends TestBase { ...@@ -123,8 +123,19 @@ public class TestMergeUsing extends TestBase {
GATHER_ORDERED_RESULTS_SQL, GATHER_ORDERED_RESULTS_SQL,
"SELECT 1 AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(1,1)", "SELECT 1 AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(1,1)",
3, 3,
"Unique index or primary key violation: \"Merge using ON column expression, duplicates values found:keys[ID]:values:[1]:from:PUBLIC.SOURCE:alias:SOURCE:current row number:2:conflicting row number:1" "Unique index or primary key violation: \"Merge using ON column expression, duplicate values found:keys[ID]:values:[1]:from:PUBLIC.SOURCE:alias:SOURCE:current row number:2:conflicting row number:1"
); );
// Duplicate key updated 3 rows at once, only 1 expected
testMergeUsingException(
"CREATE TABLE PARENT AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"+
"CREATE TABLE SOURCE AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );",
"MERGE INTO PARENT USING SOURCE ON (PARENT.ID = SOURCE.ID) WHEN MATCHED THEN UPDATE SET PARENT.NAME = SOURCE.NAME||SOURCE.ID WHERE PARENT.ID = 2 DELETE WHERE PARENT.ID = 1 WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (SOURCE.ID, SOURCE.NAME)",
GATHER_ORDERED_RESULTS_SQL,
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)",
3,
"Duplicate key updated 3 rows at once, only 1 expected"
);
} }
/** /**
...@@ -145,30 +156,34 @@ public class TestMergeUsing extends TestBase { ...@@ -145,30 +156,34 @@ public class TestMergeUsing extends TestBase {
ResultSet rs; ResultSet rs;
int rowCountUpdate; int rowCountUpdate;
stat = conn.createStatement(); try{
stat.execute(setupSQL); stat = conn.createStatement();
stat.execute(setupSQL);
prep = conn.prepareStatement(statementUnderTest);
rowCountUpdate = prep.executeUpdate(); prep = conn.prepareStatement(statementUnderTest);
rowCountUpdate = prep.executeUpdate();
// compare actual results from SQL resultsset with expected results - by diffing (aka set MINUS operation)
rs = stat.executeQuery("( "+gatherResultsSQL+" ) MINUS ( "+expectedResultsSQL+" )"); // compare actual results from SQL resultsset with expected results - by diffing (aka set MINUS operation)
rs = stat.executeQuery("( "+gatherResultsSQL+" ) MINUS ( "+expectedResultsSQL+" )");
int rowCount = 0;
StringBuffer diffBuffer = new StringBuffer(""); int rowCount = 0;
while (rs.next()) { StringBuffer diffBuffer = new StringBuffer("");
rowCount++; while (rs.next()) {
diffBuffer.append("|"); rowCount++;
System.out.println("rs.getMetaData().getColumnCount()="+rs.getMetaData().getColumnCount()); diffBuffer.append("|");
for(int ndx = 1; ndx <= rs.getMetaData().getColumnCount(); ndx++){ System.out.println("rs.getMetaData().getColumnCount()="+rs.getMetaData().getColumnCount());
diffBuffer.append(rs.getObject(ndx)); for(int ndx = 1; ndx <= rs.getMetaData().getColumnCount(); ndx++){
diffBuffer.append("|\n"); diffBuffer.append(rs.getObject(ndx));
diffBuffer.append("|\n");
}
} }
assertEquals("Differences between expected and actual output found:"+diffBuffer,0,rowCount);
assertEquals("Expected update counts differ",expectedRowUpdateCount,rowCountUpdate);
}
finally{
conn.close();
deleteDb("mergeUsingQueries");
} }
assertEquals("Differences between expected and actual output found:"+diffBuffer,0,rowCount);
assertEquals("Expected update counts differ",expectedRowUpdateCount,rowCountUpdate);
conn.close();
deleteDb("mergeUsingQueries");
} }
/** /**
* Run a test case of the merge using syntax * Run a test case of the merge using syntax
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论