This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 3e03fd6ccc4 Basic support for MERGE queries (#28756)
3e03fd6ccc4 is described below
commit 3e03fd6ccc49e071f88f78e0a1a60c0163ef87f2
Author: kanha gupta <[email protected]>
AuthorDate: Sun Oct 15 06:19:45 2023 +0530
Basic support for MERGE queries (#28756)
* Basic support for MERGE queries
* error resolve
* java doc add
---
.../compiler/converter/SQLNodeConverterEngine.java | 5 +++
.../statement/merge/MergeStatementConverter.java | 40 ++++++++++++++++++++++
.../converter/SQLNodeConverterEngineIT.java | 5 ++-
.../src/test/resources/converter/merge.xml | 22 ++++++++++++
4 files changed, 71 insertions(+), 1 deletion(-)
diff --git
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/SQLNodeConverterEngine.java
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/SQLNodeConverterEngine.java
index eb8a934fa9f..bdd58388aa3 100644
---
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/SQLNodeConverterEngine.java
+++
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/SQLNodeConverterEngine.java
@@ -26,9 +26,11 @@ import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteState
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.MergeStatement;
import
org.apache.shardingsphere.sqlfederation.compiler.converter.statement.delete.DeleteStatementConverter;
import
org.apache.shardingsphere.sqlfederation.compiler.converter.statement.explain.ExplainStatementConverter;
import
org.apache.shardingsphere.sqlfederation.compiler.converter.statement.insert.InsertStatementConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.statement.merge.MergeStatementConverter;
import
org.apache.shardingsphere.sqlfederation.compiler.converter.statement.update.UpdateStatementConverter;
import
org.apache.shardingsphere.sqlfederation.exception.OptimizationSQLNodeConvertException;
import
org.apache.shardingsphere.sqlfederation.compiler.converter.statement.select.SelectStatementConverter;
@@ -62,6 +64,9 @@ public final class SQLNodeConverterEngine {
if (statement instanceof InsertStatement) {
return new InsertStatementConverter().convert((InsertStatement)
statement);
}
+ if (statement instanceof MergeStatement) {
+ return new MergeStatementConverter().convert((MergeStatement)
statement);
+ }
throw new OptimizationSQLNodeConvertException(statement);
}
}
diff --git
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/statement/merge/MergeStatementConverter.java
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/statement/merge/MergeStatementConverter.java
new file mode 100644
index 00000000000..d19b2c26adf
--- /dev/null
+++
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/statement/merge/MergeStatementConverter.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.sqlfederation.compiler.converter.statement.merge;
+
+import org.apache.calcite.sql.SqlMerge;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.MergeStatement;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.ExpressionConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.from.TableConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.statement.SQLStatementConverter;
+
+/**
+ * Merge statement converter.
+ */
+public final class MergeStatementConverter implements
SQLStatementConverter<MergeStatement, SqlNode> {
+
+ @Override
+ public SqlNode convert(final MergeStatement mergeStatement) {
+ SqlNode targetTable = new
TableConverter().convert(mergeStatement.getTarget()).orElseThrow(IllegalStateException::new);
+ SqlNode condition = new
ExpressionConverter().convert(mergeStatement.getExpression().getExpr()).get();
+ SqlNode sourceTable = new
TableConverter().convert(mergeStatement.getSource()).orElseThrow(IllegalStateException::new);
+ return new SqlMerge(SqlParserPos.ZERO, targetTable, condition,
sourceTable, null, null, null, null);
+ }
+}
diff --git
a/test/it/optimizer/src/test/java/org/apache/shardingsphere/test/it/optimizer/converter/SQLNodeConverterEngineIT.java
b/test/it/optimizer/src/test/java/org/apache/shardingsphere/test/it/optimizer/converter/SQLNodeConverterEngineIT.java
index 708fb470cc2..55a6b0f89e7 100644
---
a/test/it/optimizer/src/test/java/org/apache/shardingsphere/test/it/optimizer/converter/SQLNodeConverterEngineIT.java
+++
b/test/it/optimizer/src/test/java/org/apache/shardingsphere/test/it/optimizer/converter/SQLNodeConverterEngineIT.java
@@ -66,6 +66,8 @@ class SQLNodeConverterEngineIT {
private static final String INSERT_STATEMENT_PREFIX = "INSERT";
+ private static final String MERGE_STATEMENT_PREFIX = "MERGE";
+
@ParameterizedTest(name = "{0} ({1}) -> {2}")
@ArgumentsSource(TestCaseArgumentsProvider.class)
void assertConvert(final String sqlCaseId, final SQLCaseType sqlCaseType,
final String databaseType) {
@@ -111,7 +113,8 @@ class SQLNodeConverterEngineIT {
||
testParam.getSqlCaseId().toUpperCase().startsWith(DELETE_STATEMENT_PREFIX)
||
testParam.getSqlCaseId().toUpperCase().startsWith(EXPLAIN_STATEMENT_PREFIX)
||
testParam.getSqlCaseId().toUpperCase().startsWith(UPDATE_STATEMENT_PREFIX)
- ||
testParam.getSqlCaseId().toUpperCase().startsWith(INSERT_STATEMENT_PREFIX);
+ ||
testParam.getSqlCaseId().toUpperCase().startsWith(INSERT_STATEMENT_PREFIX)
+ ||
testParam.getSqlCaseId().toUpperCase().startsWith(MERGE_STATEMENT_PREFIX);
}
}
}
diff --git a/test/it/optimizer/src/test/resources/converter/merge.xml
b/test/it/optimizer/src/test/resources/converter/merge.xml
new file mode 100644
index 00000000000..b2d0ca14c7e
--- /dev/null
+++ b/test/it/optimizer/src/test/resources/converter/merge.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<sql-node-converter-test-cases>
+ <test-cases sql-case-id="merge_into_table_using_table" expected-sql="MERGE
INTO "people_target" USING "people_source" ON
"people_target"."person_id" =
"people_source"."person_id"" db-types="Oracle"
sql-case-types="LITERAL" />
+ <test-cases sql-case-id="merge_into_table_using_subquery_alias"
expected-sql="MERGE INTO "bonuses" "D" USING (SELECT
"employee_id", "salary", "department_id" FROM
"employees" WHERE "department_id" = 80) "S" ON
"D"."employee_id" = "S"."employee_id""
db-types="Oracle" sql-case-types="LITERAL" />
+</sql-node-converter-test-cases>