This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 7c0b3bcd2ac Add test cases on SQLStatementConverter (#37341)
7c0b3bcd2ac is described below
commit 7c0b3bcd2ac448a013745a7d70e2cbabad3aed6a
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Dec 11 14:05:57 2025 +0800
Add test cases on SQLStatementConverter (#37341)
* Add DeleteStatementConverterTest
* Add test cases on SQLStatementConverter
---
.../statement/delete/DeleteStatementConverter.java | 9 +-
.../delete/DeleteStatementConverterTest.java | 221 +++++++++++++++++++++
.../explain/ExplainStatementConverterTest.java | 120 +++++++++++
.../insert/InsertStatementConverterTest.java | 109 ++++++++++
.../merge/MergeStatementConverterTest.java | 108 ++++++++++
.../select/SelectStatementConverterTest.java | 127 ++++++++++++
.../update/UpdateStatementConverterTest.java | 112 +++++++++++
7 files changed, 799 insertions(+), 7 deletions(-)
diff --git
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/delete/DeleteStatementConverter.java
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/delete/DeleteStatementConverter.java
index ede35a6b4c1..dfbf09dbe07 100644
---
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/delete/DeleteStatementConverter.java
+++
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/delete/DeleteStatementConverter.java
@@ -62,11 +62,7 @@ public final class DeleteStatementConverter implements
SQLStatementConverter<Del
}
private SqlNode convertDelete(final DeleteStatement deleteStatement) {
- if (deleteStatement.getTable() instanceof DeleteMultiTableSegment) {
- return convertDeleteMultipleTable(deleteStatement);
- } else {
- return convertDeleteSingleTable(deleteStatement);
- }
+ return deleteStatement.getTable() instanceof DeleteMultiTableSegment ?
convertDeleteMultipleTable(deleteStatement) :
convertDeleteSingleTable(deleteStatement);
}
private SqlNode convertDeleteMultipleTable(final DeleteStatement
deleteStatement) {
@@ -74,8 +70,7 @@ public final class DeleteStatementConverter implements
SQLStatementConverter<Del
SqlNodeList targetTables = convertTargetTables(multiTableSegment);
SqlNodeList targetTableAliases =
convertTargetTableAliases(multiTableSegment);
SqlNode condition =
deleteStatement.getWhere().flatMap(WhereConverter::convert).orElse(null);
- SqlDelete sqlDelete = new SqlDelete(SqlParserPos.ZERO,
targetTables.get(0), condition, null,
- targetTableAliases.isEmpty() ? null : (SqlIdentifier)
targetTableAliases.get(0));
+ SqlDelete sqlDelete = new SqlDelete(SqlParserPos.ZERO,
targetTables.get(0), condition, null, targetTableAliases.isEmpty() ? null :
(SqlIdentifier) targetTableAliases.get(0));
return deleteStatement.getWith().flatMap(optional ->
WithConverter.convert(optional, sqlDelete)).orElse(sqlDelete);
}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/delete/DeleteStatementConverterTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/delete/DeleteStatementConverterTest.java
new file mode 100644
index 00000000000..76651ef7d33
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/delete/DeleteStatementConverterTest.java
@@ -0,0 +1,221 @@
+/*
+ * 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.sql.ast.converter.statement.delete;
+
+import org.apache.calcite.sql.SqlDelete;
+import org.apache.calcite.sql.SqlDynamicParam;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlOrderBy;
+import org.apache.calcite.sql.SqlWith;
+import
org.apache.shardingsphere.database.connector.core.metadata.database.enums.NullsOrderType;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.statement.core.enums.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.complex.CommonTableExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.OrderBySegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ColumnOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.OrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.LimitSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.AliasSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.DeleteMultiTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.JoinTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.DeleteStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.Test;
+import org.mockito.stubbing.Answer;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.spy;
+
+class DeleteStatementConverterTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Test
+ void assertConvertSingleTableWithoutOrderByAndLimit() {
+ DeleteStatement deleteStatement = new DeleteStatement(databaseType);
+ deleteStatement.setTable(createSimpleTableSegment("t_order"));
+ SqlDelete actual = (SqlDelete) new
DeleteStatementConverter().convert(deleteStatement);
+ assertThat(((SqlIdentifier) actual.getTargetTable()).getSimple(),
is("t_order"));
+ assertNull(actual.getCondition());
+ assertNull(actual.getAlias());
+ }
+
+ @Test
+ void assertConvertSingleTableWithAliasAndOrderBy() {
+ DeleteStatement deleteStatement = new DeleteStatement(databaseType);
+ deleteStatement.setTable(createSimpleTableSegmentWithAlias("t_order",
"do"));
+ deleteStatement.setWhere(new WhereSegment(0, 0, new
ParameterMarkerExpressionSegment(0, 0, 0)));
+ deleteStatement.setOrderBy(createOrderBySegment());
+ SqlOrderBy actual = (SqlOrderBy) new
DeleteStatementConverter().convert(deleteStatement);
+ assertThat(actual.query, isA(SqlDelete.class));
+ SqlDelete sqlDelete = (SqlDelete) actual.query;
+ assertThat(((SqlIdentifier) sqlDelete.getTargetTable()).getSimple(),
is("t_order"));
+ assertNotNull(sqlDelete.getAlias());
+ assertThat(sqlDelete.getAlias().toString(), is("do"));
+ assertThat(actual.orderList.size(), is(1));
+ assertThat(sqlDelete.getCondition(), isA(SqlDynamicParam.class));
+ }
+
+ @Test
+ void assertConvertSingleTableWithLimit() {
+ DeleteStatement deleteStatement = new DeleteStatement(databaseType);
+ deleteStatement.setTable(createSimpleTableSegment("t_order"));
+ LimitSegment limit = new LimitSegment(0, 0, new
NumberLiteralLimitValueSegment(0, 0, 1L), new
ParameterMarkerLimitValueSegment(0, 0, 0));
+ deleteStatement.setLimit(limit);
+ SqlOrderBy actual = (SqlOrderBy) new
DeleteStatementConverter().convert(deleteStatement);
+ assertThat(actual.offset, isA(SqlLiteral.class));
+ assertNotNull(actual.offset);
+ assertThat(actual.offset.toString(), is("1"));
+ assertThat(actual.fetch, isA(SqlDynamicParam.class));
+ assertNotNull(actual.fetch);
+ assertThat(((SqlDynamicParam) actual.fetch).getIndex(), is(0));
+ }
+
+ @Test
+ void assertConvertMultiTableWithAliasesAndWith() {
+ DeleteMultiTableSegment multiTableSegment = new
DeleteMultiTableSegment();
+ multiTableSegment.setRelationTable(createJoinTableSegment());
+ List<SimpleTableSegment> actualDeleteTables = new LinkedList<>();
+ actualDeleteTables.add(createSimpleTableSegment("l"));
+ SimpleTableSegment tableWithOwner = createSimpleTableSegment("r");
+ tableWithOwner.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("schema")));
+ actualDeleteTables.add(tableWithOwner);
+ actualDeleteTables.add(createSimpleTableSegment("DUAL"));
+ multiTableSegment.setActualDeleteTables(actualDeleteTables);
+ DeleteStatement deleteStatement = new DeleteStatement(databaseType);
+ deleteStatement.setTable(multiTableSegment);
+ deleteStatement.setWhere(new WhereSegment(0, 0, new
ParameterMarkerExpressionSegment(0, 0, 0)));
+ deleteStatement.setWith(createWithSegment());
+ SqlWith actual = (SqlWith) new
DeleteStatementConverter().convert(deleteStatement);
+ SqlDelete sqlDelete = (SqlDelete) actual.body;
+ assertThat(((SqlIdentifier) sqlDelete.getTargetTable()).getSimple(),
is("left_table"));
+ assertNotNull(sqlDelete.getAlias());
+ assertThat(sqlDelete.getAlias().toString(), is("l"));
+ assertThat(sqlDelete.getCondition(), isA(SqlDynamicParam.class));
+ assertNotNull(sqlDelete.getCondition());
+ assertThat(((SqlDynamicParam) sqlDelete.getCondition()).getIndex(),
is(0));
+ }
+
+ @Test
+ void assertConvertMultiTableWithoutAliasMapping() {
+ DeleteMultiTableSegment multiTableSegment = new
DeleteMultiTableSegment();
+ SimpleTableSegment relationTable =
createSimpleTableSegment("target_table");
+ multiTableSegment.setRelationTable(relationTable);
+
multiTableSegment.setActualDeleteTables(Collections.singletonList(createSimpleTableSegment("target_table")));
+ DeleteStatement deleteStatement = new DeleteStatement(databaseType);
+ deleteStatement.setTable(multiTableSegment);
+ SqlDelete actual = (SqlDelete) new
DeleteStatementConverter().convert(deleteStatement);
+ assertThat(((SqlIdentifier) actual.getTargetTable()).getSimple(),
is("target_table"));
+ assertNotNull(actual.getAlias());
+ assertThat(actual.getAlias().toString(), is("target_table"));
+ }
+
+ @Test
+ void assertConvertSingleTableWithWithSegment() {
+ DeleteStatement deleteStatement = new DeleteStatement(databaseType);
+ deleteStatement.setTable(createSimpleTableSegment("t_order"));
+ deleteStatement.setWhere(new WhereSegment(0, 0, new
ParameterMarkerExpressionSegment(0, 0, 0)));
+ deleteStatement.setWith(createWithSegment());
+ SqlWith actual = (SqlWith) new
DeleteStatementConverter().convert(deleteStatement);
+ SqlDelete sqlDelete = (SqlDelete) actual.body;
+ assertThat(((SqlIdentifier) sqlDelete.getTargetTable()).getSimple(),
is("t_order"));
+ assertNull(sqlDelete.getAlias());
+ assertThat(sqlDelete.getCondition(), isA(SqlDynamicParam.class));
+ }
+
+ @Test
+ void assertConvertMultiTableAliasEmptyBranch() {
+ DeleteMultiTableSegment multiTableSegment = new
DeleteMultiTableSegment();
+ List<SimpleTableSegment> tables = spy(new LinkedList<>());
+ tables.add(createSimpleTableSegment("target_table"));
+ doAnswer((Answer<Void>) invocation ->
null).when(tables).forEach(any());
+ multiTableSegment.setActualDeleteTables(tables);
+
multiTableSegment.setRelationTable(createSimpleTableSegmentWithAlias("target_table",
"t"));
+ DeleteStatement deleteStatement = new DeleteStatement(databaseType);
+ deleteStatement.setTable(multiTableSegment);
+ SqlDelete actual = (SqlDelete) new
DeleteStatementConverter().convert(deleteStatement);
+ assertNull(actual.getAlias());
+ }
+
+ @Test
+ void assertConvertSingleTableWithoutTargetTable() {
+ DeleteStatement deleteStatement = new DeleteStatement(databaseType);
+ deleteStatement.setTable(createSimpleTableSegment("DUAL"));
+ assertThrows(IllegalStateException.class, () -> new
DeleteStatementConverter().convert(deleteStatement));
+ }
+
+ private SimpleTableSegment createSimpleTableSegment(final String
tableName) {
+ return new SimpleTableSegment(new TableNameSegment(0, 0, new
IdentifierValue(tableName)));
+ }
+
+ private SimpleTableSegment createSimpleTableSegmentWithAlias(final String
tableName, final String alias) {
+ SimpleTableSegment result = createSimpleTableSegment(tableName);
+ result.setAlias(new AliasSegment(0, 0, new IdentifierValue(alias)));
+ return result;
+ }
+
+ private OrderBySegment createOrderBySegment() {
+ ColumnSegment column = new ColumnSegment(0, 0, new
IdentifierValue("order_col"));
+ Collection<OrderByItemSegment> orderByItems =
Collections.singleton(new ColumnOrderByItemSegment(column, OrderDirection.ASC,
NullsOrderType.FIRST));
+ return new OrderBySegment(0, 0, orderByItems);
+ }
+
+ private WithSegment createWithSegment() {
+ SelectStatement selectStatement = new SelectStatement(databaseType);
+ ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
+ projectionsSegment.getProjections().add(new
ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("col"))));
+ selectStatement.setProjections(projectionsSegment);
+ CommonTableExpressionSegment cte = new CommonTableExpressionSegment(
+ 0, 0, new AliasSegment(0, 0, new IdentifierValue("cte")), new
SubquerySegment(0, 0, selectStatement, "subquery"));
+ return new WithSegment(0, 0, Collections.singleton(cte), false);
+ }
+
+ private JoinTableSegment createJoinTableSegment() {
+ JoinTableSegment result = new JoinTableSegment();
+ result.setLeft(createSimpleTableSegmentWithAlias("left_table", "l"));
+ result.setRight(createSimpleTableSegmentWithAlias("right_table", "r"));
+ return result;
+ }
+}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/explain/ExplainStatementConverterTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/explain/ExplainStatementConverterTest.java
new file mode 100644
index 00000000000..da677f62202
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/explain/ExplainStatementConverterTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.sql.ast.converter.statement.explain;
+
+import org.apache.calcite.sql.SqlExplain;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.InsertValuesSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.SetAssignmentSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.ExplainStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.DeleteStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.InsertStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.UpdateStatement;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.isA;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class ExplainStatementConverterTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Test
+ void assertConvertSelectStatement() {
+ ExplainStatement explainStatement = new ExplainStatement(databaseType,
createSelectStatement());
+ SqlNode actual = new
ExplainStatementConverter().convert(explainStatement);
+ assertThat(((SqlExplain) actual).getExplicandum(), isA(SqlNode.class));
+ }
+
+ @Test
+ void assertConvertDeleteStatement() {
+ ExplainStatement explainStatement = new ExplainStatement(databaseType,
createDeleteStatement());
+ SqlNode actual = new
ExplainStatementConverter().convert(explainStatement);
+ assertThat(((SqlExplain) actual).getExplicandum(), isA(SqlNode.class));
+ }
+
+ @Test
+ void assertConvertUpdateStatement() {
+ ExplainStatement explainStatement = new ExplainStatement(databaseType,
createUpdateStatement());
+ SqlNode actual = new
ExplainStatementConverter().convert(explainStatement);
+ assertThat(((SqlExplain) actual).getExplicandum(), isA(SqlNode.class));
+ }
+
+ @Test
+ void assertConvertInsertStatement() {
+ ExplainStatement explainStatement = new ExplainStatement(databaseType,
createInsertStatement());
+ SqlNode actual = new
ExplainStatementConverter().convert(explainStatement);
+ assertThat(((SqlExplain) actual).getExplicandum(), isA(SqlNode.class));
+ }
+
+ @Test
+ void assertConvertUnsupportedSQLStatementThrowsException() {
+ ExplainStatement explainStatement = new ExplainStatement(databaseType,
new SQLStatement(databaseType) {
+ });
+ assertThrows(IllegalStateException.class, () -> new
ExplainStatementConverter().convert(explainStatement));
+ }
+
+ private SelectStatement createSelectStatement() {
+ SelectStatement result = new SelectStatement(databaseType);
+ result.setProjections(createProjectionsSegment());
+ result.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new
IdentifierValue("t_select"))));
+ return result;
+ }
+
+ private DeleteStatement createDeleteStatement() {
+ DeleteStatement result = new DeleteStatement(databaseType);
+ result.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new
IdentifierValue("t_delete"))));
+ return result;
+ }
+
+ private UpdateStatement createUpdateStatement() {
+ UpdateStatement result = new UpdateStatement(databaseType);
+ result.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new
IdentifierValue("t_update"))));
+ result.setSetAssignment(new SetAssignmentSegment(0, 0,
Collections.singleton(new ColumnAssignmentSegment(0, 0,
+ Collections.singletonList(new ColumnSegment(0, 0, new
IdentifierValue("col"))), new ParameterMarkerExpressionSegment(0, 0, 0)))));
+ return result;
+ }
+
+ private InsertStatement createInsertStatement() {
+ InsertStatement result = new InsertStatement(databaseType);
+ result.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new
IdentifierValue("t_insert"))));
+ result.getValues().add(new InsertValuesSegment(0, 0,
Collections.singletonList(new ParameterMarkerExpressionSegment(0, 0, 0))));
+ return result;
+ }
+
+ private ProjectionsSegment createProjectionsSegment() {
+ ProjectionsSegment result = new ProjectionsSegment(0, 0);
+ result.getProjections().add(new ColumnProjectionSegment(new
ColumnSegment(0, 0, new IdentifierValue("col"))));
+ return result;
+ }
+}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/insert/InsertStatementConverterTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/insert/InsertStatementConverterTest.java
new file mode 100644
index 00000000000..ad993a1286c
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/insert/InsertStatementConverterTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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.sql.ast.converter.statement.insert;
+
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlInsert;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.InsertValuesSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.SetAssignmentSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.InsertColumnsSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.InsertStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.isA;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class InsertStatementConverterTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Test
+ void assertConvertWithInsertSelect() {
+ InsertStatement insertStatement = new InsertStatement(databaseType);
+ insertStatement.setTable(new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("t_insert_select"))));
+ SelectStatement selectStatement = new SelectStatement(databaseType);
+ selectStatement.setProjections(createProjectionsSegment());
+ insertStatement.setInsertSelect(new SubquerySegment(0, 0,
selectStatement, "select"));
+ SqlInsert actual = (SqlInsert) new
InsertStatementConverter().convert(insertStatement);
+ assertThat(actual.getSource(), isA(SqlNode.class));
+ }
+
+ @Test
+ void assertConvertWithSetAssignmentColumns() {
+ InsertStatement insertStatement = new InsertStatement(databaseType);
+ insertStatement.setTable(new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("t_insert_set"))));
+ SetAssignmentSegment setAssignment = createSetAssignmentSegment();
+ insertStatement.setSetAssignment(setAssignment);
+ insertStatement.setInsertColumns(new InsertColumnsSegment(0, 0,
Collections.emptyList()));
+ SqlInsert actual = (SqlInsert) new
InsertStatementConverter().convert(insertStatement);
+ assertThat(actual.getTargetTable(), isA(SqlNode.class));
+ assertThat(actual.getSource(), instanceOf(SqlBasicCall.class));
+ assertThat(actual.getTargetColumnList(),
instanceOf(SqlNodeList.class));
+ }
+
+ @Test
+ void assertConvertWithValuesOnly() {
+ InsertStatement insertStatement = new InsertStatement(databaseType);
+ insertStatement.setTable(new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("t_insert_values"))));
+ insertStatement.getValues().add(new InsertValuesSegment(0, 0,
Collections.singletonList(new ParameterMarkerExpressionSegment(0, 0, 0))));
+ SqlInsert actual = (SqlInsert) new
InsertStatementConverter().convert(insertStatement);
+ assertThat(actual.getSource(), instanceOf(SqlBasicCall.class));
+ assertNull(actual.getTargetColumnList());
+ }
+
+ @Test
+ void assertConvertWithExplicitColumns() {
+ InsertStatement insertStatement = new InsertStatement(databaseType);
+ insertStatement.setTable(new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("t_insert_columns"))));
+ InsertColumnsSegment insertColumnsSegment = new
InsertColumnsSegment(0, 0, Collections.singletonList(new ColumnSegment(0, 0,
new IdentifierValue("col"))));
+ insertStatement.setInsertColumns(insertColumnsSegment);
+ insertStatement.getValues().add(new InsertValuesSegment(0, 0,
Collections.singletonList(new ParameterMarkerExpressionSegment(0, 0, 0))));
+ SqlInsert actual = (SqlInsert) new
InsertStatementConverter().convert(insertStatement);
+ assertThat(actual.getTargetColumnList(),
instanceOf(SqlNodeList.class));
+ }
+
+ private ProjectionsSegment createProjectionsSegment() {
+ ProjectionsSegment result = new ProjectionsSegment(0, 0);
+ result.getProjections().add(new ColumnProjectionSegment(new
ColumnSegment(0, 0, new IdentifierValue("col"))));
+ return result;
+ }
+
+ private SetAssignmentSegment createSetAssignmentSegment() {
+ ColumnAssignmentSegment columnAssignmentSegment = new
ColumnAssignmentSegment(0, 0, Collections.singletonList(new ColumnSegment(0, 0,
new IdentifierValue("col"))),
+ new ParameterMarkerExpressionSegment(0, 0, 0));
+ return new SetAssignmentSegment(0, 0,
Collections.singleton(columnAssignmentSegment));
+ }
+}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/merge/MergeStatementConverterTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/merge/MergeStatementConverterTest.java
new file mode 100644
index 00000000000..efd59e3fec7
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/merge/MergeStatementConverterTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.sql.ast.converter.statement.merge;
+
+import org.apache.calcite.sql.SqlMerge;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlUpdate;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.SetAssignmentSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionWithParamsSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.AliasSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.MergeStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.UpdateStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.isA;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class MergeStatementConverterTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Test
+ void assertConvertWithUpdate() {
+ MergeStatement mergeStatement =
createMergeStatement(createSimpleTableSegment("target_table"),
createSimpleTableSegment("source_table"));
+ mergeStatement.setUpdate(createUpdateStatement());
+ SqlMerge actual = (SqlMerge) new
MergeStatementConverter().convert(mergeStatement);
+ assertThat(actual.getCondition(), isA(SqlNode.class));
+ assertThat(actual.getUpdateCall(), isA(SqlUpdate.class));
+ }
+
+ @Test
+ void assertConvertWithoutUpdate() {
+ MergeStatement mergeStatement =
createMergeStatement(createSimpleTableSegment("target_table"),
createSimpleTableSegment("source_table"));
+ SqlMerge actual = (SqlMerge) new
MergeStatementConverter().convert(mergeStatement);
+ assertNull(actual.getUpdateCall());
+ }
+
+ @Test
+ void assertConvertUpdateWithEmptyTable() {
+ MergeStatement mergeStatement =
createMergeStatement(createSimpleTableSegment("target_table"),
createSimpleTableSegment("source_table"));
+ UpdateStatement updateStatement = createUpdateStatement();
+ updateStatement.setTable(createSimpleTableSegment("DUAL"));
+ mergeStatement.setUpdate(updateStatement);
+ SqlMerge actual = (SqlMerge) new
MergeStatementConverter().convert(mergeStatement);
+ assertNotNull(actual.getUpdateCall());
+ assertThat(actual.getUpdateCall().getTargetTable(),
is(SqlNodeList.EMPTY));
+ }
+
+ private MergeStatement createMergeStatement(final SimpleTableSegment
target, final SimpleTableSegment source) {
+ MergeStatement result = new MergeStatement(databaseType);
+ result.setTarget(target);
+ result.setSource(source);
+ result.setExpression(new ExpressionWithParamsSegment(0, 0, new
ParameterMarkerExpressionSegment(0, 0, 0)));
+ return result;
+ }
+
+ private UpdateStatement createUpdateStatement() {
+ UpdateStatement result = new UpdateStatement(databaseType);
+ SimpleTableSegment table = createSimpleTableSegment("t_update");
+ table.setAlias(new AliasSegment(0, 0, new IdentifierValue("u")));
+ result.setTable(table);
+ SetAssignmentSegment setAssignment = createSetAssignmentSegment();
+ result.setSetAssignment(setAssignment);
+ result.setWhere(new WhereSegment(0, 0, new
ParameterMarkerExpressionSegment(0, 0, 0)));
+ return result;
+ }
+
+ private SetAssignmentSegment createSetAssignmentSegment() {
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("col"));
+ ColumnAssignmentSegment columnAssignmentSegment = new
ColumnAssignmentSegment(0, 0, Collections.singletonList(columnSegment),
+ new ParameterMarkerExpressionSegment(0, 0, 0));
+ return new SetAssignmentSegment(0, 0,
Collections.singleton(columnAssignmentSegment));
+ }
+
+ private SimpleTableSegment createSimpleTableSegment(final String
tableName) {
+ return new SimpleTableSegment(new TableNameSegment(0, 0, new
IdentifierValue(tableName)));
+ }
+}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/select/SelectStatementConverterTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/select/SelectStatementConverterTest.java
new file mode 100644
index 00000000000..8dbfaa50ab0
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/select/SelectStatementConverterTest.java
@@ -0,0 +1,127 @@
+/*
+ * 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.sql.ast.converter.statement.select;
+
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOrderBy;
+import org.apache.calcite.sql.SqlSelect;
+import
org.apache.shardingsphere.database.connector.core.metadata.database.enums.NullsOrderType;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.sql.parser.statement.core.enums.CombineType;
+import
org.apache.shardingsphere.sql.parser.statement.core.enums.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.combine.CombineSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.LimitSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.AliasSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WindowItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WindowSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.OrderBySegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ColumnOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.isA;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class SelectStatementConverterTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Test
+ void assertConvertWithCombineAndLimit() {
+ SelectStatement left = createBaseSelect(true, true);
+ SelectStatement right = createBaseSelect(false, false);
+ CombineSegment combineSegment = new CombineSegment(0, 0, new
SubquerySegment(0, 0, left, "left"), CombineType.UNION, new SubquerySegment(0,
0, right, "right"));
+ SelectStatement selectStatement = createBaseSelect(true, true);
+ selectStatement.setCombine(combineSegment);
+ LimitSegment limit = new LimitSegment(0, 0, new
NumberLiteralLimitValueSegment(0, 0, 1L), new
ParameterMarkerLimitValueSegment(0, 0, 0));
+ selectStatement.setLimit(limit);
+ SqlOrderBy actual = (SqlOrderBy) new
SelectStatementConverter().convert(selectStatement);
+ assertThat(actual.offset, instanceOf(SqlNode.class));
+ assertThat(actual.fetch, instanceOf(SqlNode.class));
+ assertThat(actual.query, instanceOf(SqlBasicCall.class));
+ }
+
+ @Test
+ void assertConvertWithoutLimitButWithOrderByAndWindow() {
+ SelectStatement selectStatement = createBaseSelect(false, false);
+ selectStatement.setOrderBy(createOrderBySegment());
+ selectStatement.setWindow(createWindowSegment());
+ SqlOrderBy actual = (SqlOrderBy) new
SelectStatementConverter().convert(selectStatement);
+ assertNull(actual.offset);
+ assertThat(((SqlSelect) actual.query).getWindowList(),
isA(SqlNode.class));
+ }
+
+ private SelectStatement createBaseSelect(final boolean withWithSegment,
final boolean distinct) {
+ SelectStatement result = new SelectStatement(databaseType);
+ result.setProjections(createProjectionsSegment());
+ result.getProjections().setDistinctRow(distinct);
+ SimpleTableSegment fromTable = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("t_select")));
+ fromTable.setAlias(new AliasSegment(0, 0, new IdentifierValue("t")));
+ result.setFrom(fromTable);
+ result.setWhere(new WhereSegment(0, 0, new
ParameterMarkerExpressionSegment(0, 0, 0)));
+ if (withWithSegment) {
+ result.setWith(createWithSegment());
+ }
+ return result;
+ }
+
+ private WithSegment createWithSegment() {
+ SelectStatement selectStatement = new SelectStatement(databaseType);
+ selectStatement.setProjections(createProjectionsSegment());
+ return new WithSegment(0, 0, Collections.emptyList(), false);
+ }
+
+ private WindowSegment createWindowSegment() {
+ WindowItemSegment windowItemSegment = new WindowItemSegment(0, 0);
+ windowItemSegment.setWindowName(new IdentifierValue("win"));
+
windowItemSegment.setPartitionListSegments(Collections.singletonList(new
ParameterMarkerExpressionSegment(0, 0, 0)));
+ windowItemSegment.setOrderBySegment(createOrderBySegment());
+ WindowSegment result = new WindowSegment(0, 0);
+ result.getItemSegments().add(windowItemSegment);
+ return result;
+ }
+
+ private ProjectionsSegment createProjectionsSegment() {
+ ProjectionsSegment result = new ProjectionsSegment(0, 0);
+ result.getProjections().add(new ColumnProjectionSegment(new
ColumnSegment(0, 0, new IdentifierValue("col"))));
+ return result;
+ }
+
+ private OrderBySegment createOrderBySegment() {
+ ColumnOrderByItemSegment orderByItemSegment = new
ColumnOrderByItemSegment(new ColumnSegment(0, 0, new IdentifierValue("col")),
OrderDirection.ASC, NullsOrderType.FIRST);
+ return new OrderBySegment(0, 0,
Collections.singleton(orderByItemSegment));
+ }
+}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/update/UpdateStatementConverterTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/update/UpdateStatementConverterTest.java
new file mode 100644
index 00000000000..885fd17005f
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/statement/update/UpdateStatementConverterTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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.sql.ast.converter.statement.update;
+
+import org.apache.calcite.sql.SqlDynamicParam;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOrderBy;
+import org.apache.calcite.sql.SqlUpdate;
+import
org.apache.shardingsphere.database.connector.core.metadata.database.enums.NullsOrderType;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.statement.core.enums.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.SetAssignmentSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.OrderBySegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ColumnOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.LimitSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.AliasSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.UpdateStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class UpdateStatementConverterTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Test
+ void assertConvertWithLimitAndAlias() {
+ UpdateStatement updateStatement = createUpdateStatement(true);
+ LimitSegment limit = new LimitSegment(0, 0, new
NumberLiteralLimitValueSegment(0, 0, 1L), new
ParameterMarkerLimitValueSegment(0, 0, 0));
+ updateStatement.setLimit(limit);
+ SqlOrderBy actual = (SqlOrderBy) new
UpdateStatementConverter().convert(updateStatement);
+ assertThat(actual.offset, instanceOf(SqlNode.class));
+ assertThat(actual.fetch, instanceOf(SqlDynamicParam.class));
+ SqlUpdate sqlUpdate = (SqlUpdate) actual.query;
+ assertThat(sqlUpdate.getAlias(), instanceOf(SqlIdentifier.class));
+ }
+
+ @Test
+ void assertConvertWithoutLimitButWithOrderBy() {
+ UpdateStatement updateStatement = createUpdateStatement(false);
+ updateStatement.setOrderBy(createOrderBySegment());
+ SqlOrderBy actual = (SqlOrderBy) new
UpdateStatementConverter().convert(updateStatement);
+ assertNull(actual.offset);
+ SqlUpdate sqlUpdate = (SqlUpdate) actual.query;
+ assertNull(sqlUpdate.getAlias());
+ }
+
+ @Test
+ void assertConvertWithLimitWithoutOffsetAndRowCount() {
+ UpdateStatement updateStatement = createUpdateStatement(true);
+ LimitSegment limit = new LimitSegment(0, 0, null, null);
+ updateStatement.setLimit(limit);
+ SqlOrderBy actual = (SqlOrderBy) new
UpdateStatementConverter().convert(updateStatement);
+ assertNull(actual.offset);
+ assertNull(actual.fetch);
+ }
+
+ private UpdateStatement createUpdateStatement(final boolean withAlias) {
+ UpdateStatement result = new UpdateStatement(databaseType);
+ SimpleTableSegment tableSegment = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("t_update")));
+ if (withAlias) {
+ tableSegment.setAlias(new AliasSegment(0, 0, new
IdentifierValue("u")));
+ }
+ result.setTable(tableSegment);
+ SetAssignmentSegment setAssignment = createSetAssignmentSegment();
+ result.setSetAssignment(setAssignment);
+ result.setWhere(new WhereSegment(0, 0, new
ParameterMarkerExpressionSegment(0, 0, 0)));
+ return result;
+ }
+
+ private SetAssignmentSegment createSetAssignmentSegment() {
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("col"));
+ ColumnAssignmentSegment columnAssignmentSegment = new
ColumnAssignmentSegment(0, 0, Collections.singletonList(columnSegment),
+ new ParameterMarkerExpressionSegment(0, 0, 0));
+ return new SetAssignmentSegment(0, 0,
Collections.singleton(columnAssignmentSegment));
+ }
+
+ private OrderBySegment createOrderBySegment() {
+ ColumnOrderByItemSegment orderByItemSegment = new
ColumnOrderByItemSegment(new ColumnSegment(0, 0, new IdentifierValue("col")),
OrderDirection.ASC, NullsOrderType.FIRST);
+ return new OrderBySegment(0, 0,
Collections.singleton(orderByItemSegment));
+ }
+}