提交 176dc52c authored 作者: Owner's avatar Owner

Added match-select query for ROWID checking

上级 73baaefd
...@@ -1126,7 +1126,7 @@ public class Parser { ...@@ -1126,7 +1126,7 @@ public class Parser {
private MergeUsing parseMergeUsing(Merge oldCommand, int start) { private MergeUsing parseMergeUsing(Merge oldCommand, int start) {
MergeUsing command = new MergeUsing(oldCommand); MergeUsing command = new MergeUsing(oldCommand);
currentPrepared = command; currentPrepared = command;
if (readIf("(")) { if (readIf("(")) {
/* a select query is supplied */ /* a select query is supplied */
if (isSelect()) { if (isSelect()) {
...@@ -1162,7 +1162,7 @@ public class Parser { ...@@ -1162,7 +1162,7 @@ public class Parser {
Expression condition = readExpression(); Expression condition = readExpression();
command.setOnCondition(condition); command.setOnCondition(condition);
read(")"); read(")");
if(readIfAll("WHEN","MATCHED","THEN")){ if(readIfAll("WHEN","MATCHED","THEN")){
int startMatched = lastParseIndex; int startMatched = lastParseIndex;
if (readIf("UPDATE")){ if (readIf("UPDATE")){
...@@ -1192,6 +1192,33 @@ public class Parser { ...@@ -1192,6 +1192,33 @@ public class Parser {
} }
setSQL(command, "MERGE", start); setSQL(command, "MERGE", start);
// build and prepare the targetMatchQuery ready to test each rows existence in the target table (using source row to match)
StringBuffer targetMatchQuerySQL = new StringBuffer("SELECT _ROWID_ FROM "+command.getTargetTable().getName());
if(command.getTargetTableFilter().getTableAlias()!=null){
targetMatchQuerySQL.append(" AS "+command.getTargetTableFilter().getTableAlias());
}
targetMatchQuerySQL.append(" WHERE "+command.getOnCondition().getSQL());
// Select preparedTargetMatchQuery = new Select(session);
// preparedTargetMatchQuery.addTableFilter(command.getTargetTableFilter(), true/*isTop*/);
// preparedTargetMatchQuery.addTableFilter(command.getSourceTableFilter(), false/*isTop - not top table scan*/);
// preparedTargetMatchQuery.setSQL(targetMatchQuerySQL.toString());
// ArrayList<Expression> selectList = New.arrayList();
// //Database db = session == null ? null : session.getDatabase();
// selectList.add(new ExpressionColumn(session.getDatabase(), command.getTargetTableFilter().getTable().getRowIdColumn()));
// preparedTargetMatchQuery.setExpressions(selectList);
// preparedTargetMatchQuery.init();
command.setTargetMatchQuery((Select)parse(targetMatchQuerySQL.toString()));
// Select command = new Select(session);
// currentSelect = command;
// TableFilter filter = parseValuesTable(0);
// ArrayList<Expression> list = New.arrayList();
// list.add(new Wildcard(null, null));
// command.setExpressions(list);
// command.addTableFilter(filter, true);
// command.init();
return command; return command;
} }
......
...@@ -168,6 +168,10 @@ public class Select extends Query { ...@@ -168,6 +168,10 @@ public class Select extends Query {
} }
} }
public Expression getCondition() {
return condition;
}
private LazyResult queryGroupSorted(int columnCount, ResultTarget result) { private LazyResult queryGroupSorted(int columnCount, ResultTarget result) {
LazyResultGroupSorted lazyResult = new LazyResultGroupSorted(expressionArray, columnCount); LazyResultGroupSorted lazyResult = new LazyResultGroupSorted(expressionArray, columnCount);
if (result == null) { if (result == null) {
......
...@@ -182,7 +182,17 @@ public class TestMergeUsing extends TestBase { ...@@ -182,7 +182,17 @@ public class TestMergeUsing extends TestBase {
"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)", "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, 3,
"No references to source columns found in ON clause" "No references to source columns found in ON clause"
); );
// Insert does not insert correct values with respect to ON condition (inserts ID value above 100, instead)
testMergeUsingException(
"CREATE TABLE PARENT AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(4,4) );"+
"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+100, SOURCE.NAME)",
GATHER_ORDERED_RESULTS_SQL,
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(4,4)",
1,
"Expected to find key after row inserted, but none found. Insert does not match ON condition."
);
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论