Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic support for MERGE queries #28756

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
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;
Expand Down Expand Up @@ -62,6 +64,9 @@ public static SqlNode convert(final SQLStatement statement) {
if (statement instanceof InsertStatement) {
return new InsertStatementConverter().convert((InsertStatement) statement);
}
if (statement instanceof MergeStatement) {
return new MergeStatementConverter().convert((MergeStatement) statement);
}
throw new OptimizationSQLNodeConvertException(statement);
}
}
Original file line number Diff line number Diff line change
@@ -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> {
kanha-gupta marked this conversation as resolved.
Show resolved Hide resolved

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -111,7 +113,8 @@ private boolean isSupportedSQLCase(final InternalSQLParserTestParameter testPara
|| 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);
}
}
}
22 changes: 22 additions & 0 deletions test/it/optimizer/src/test/resources/converter/merge.xml
Original file line number Diff line number Diff line change
@@ -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 &quot;people_target&quot; USING &quot;people_source&quot; ON &quot;people_target&quot;.&quot;person_id&quot; = &quot;people_source&quot;.&quot;person_id&quot;" db-types="Oracle" sql-case-types="LITERAL" />
<test-cases sql-case-id="merge_into_table_using_subquery_alias" expected-sql="MERGE INTO &quot;bonuses&quot; &quot;D&quot; USING (SELECT &quot;employee_id&quot;, &quot;salary&quot;, &quot;department_id&quot; FROM &quot;employees&quot; WHERE &quot;department_id&quot; = 80) &quot;S&quot; ON &quot;D&quot;.&quot;employee_id&quot; = &quot;S&quot;.&quot;employee_id&quot;" db-types="Oracle" sql-case-types="LITERAL" />
</sql-node-converter-test-cases>