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 cdb84fb5482 Add insert statement converter. (#28314)
cdb84fb5482 is described below
commit cdb84fb54825d220f5191f4b1449d1ccb9d55816
Author: boyjoy1127 <[email protected]>
AuthorDate: Thu Aug 31 11:58:34 2023 +0800
Add insert statement converter. (#28314)
* feat: add insert converter.
* feat: add insert converter.
* feat: add insert converter.
---
.../compiler/converter/SQLNodeConverterEngine.java | 5 +
.../statement/insert/InsertStatementConverter.java | 108 +++++++++++++++++++++
.../converter/SQLNodeConverterEngineIT.java | 5 +-
.../src/test/resources/converter/insert.xml | 21 ++++
.../main/resources/sql/supported/dml/insert.xml | 2 +-
5 files changed, 139 insertions(+), 2 deletions(-)
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 db559acb36f..eb8a934fa9f 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
@@ -23,10 +23,12 @@ import org.apache.calcite.sql.SqlNode;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ExplainStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
+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.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.update.UpdateStatementConverter;
import
org.apache.shardingsphere.sqlfederation.exception.OptimizationSQLNodeConvertException;
import
org.apache.shardingsphere.sqlfederation.compiler.converter.statement.select.SelectStatementConverter;
@@ -57,6 +59,9 @@ public final class SQLNodeConverterEngine {
if (statement instanceof UpdateStatement) {
return new UpdateStatementConverter().convert((UpdateStatement)
statement);
}
+ if (statement instanceof InsertStatement) {
+ return new InsertStatementConverter().convert((InsertStatement)
statement);
+ }
throw new OptimizationSQLNodeConvertException(statement);
}
}
diff --git
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/statement/insert/InsertStatementConverter.java
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/statement/insert/InsertStatementConverter.java
new file mode 100644
index 00000000000..2b717e03087
--- /dev/null
+++
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/statement/insert/InsertStatementConverter.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.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.calcite.sql.SqlSelect;
+import org.apache.calcite.sql.SqlValuesOperator;
+import org.apache.calcite.sql.fun.SqlRowOperator;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
+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.dialect.handler.dml.SelectStatementHandler;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.ExpressionConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.impl.ColumnConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.from.TableConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.groupby.GroupByConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.groupby.HavingConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.projection.DistinctConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.projection.ProjectionsConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.where.WhereConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.window.WindowConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.statement.SQLStatementConverter;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Insert statement converter.
+ */
+public final class InsertStatementConverter implements
SQLStatementConverter<InsertStatement, SqlNode> {
+
+ @Override
+ public SqlNode convert(final InsertStatement insertStatement) {
+ return convertInsert(insertStatement);
+ }
+
+ private SqlInsert convertInsert(final InsertStatement insertStatement) {
+ SqlNode table = new
TableConverter().convert(insertStatement.getTable()).orElseThrow(IllegalStateException::new);
+ SqlParserPos position = SqlParserPos.ZERO;
+ SqlNodeList keywords = new SqlNodeList(position);
+ SqlNode source;
+ if (insertStatement.getInsertSelect().isPresent()) {
+ source = convertSelect(insertStatement.getInsertSelect().get());
+ } else {
+ source = convertValues(insertStatement.getValues());
+ }
+ SqlNodeList columnList = convertColumn(insertStatement.getColumns());
+ return new SqlInsert(SqlParserPos.ZERO, keywords, table, source,
columnList);
+ }
+
+ private SqlNode convertSelect(final SubquerySegment subquerySegment) {
+ SelectStatement selectStatement = subquerySegment.getSelect();
+ SqlNodeList distinct = new
DistinctConverter().convert(selectStatement.getProjections()).orElse(null);
+ SqlNodeList projection = new
ProjectionsConverter().convert(selectStatement.getProjections()).orElseThrow(IllegalStateException::new);
+ SqlNode from = new
TableConverter().convert(selectStatement.getFrom()).orElse(null);
+ SqlNode where = selectStatement.getWhere().flatMap(optional -> new
WhereConverter().convert(optional)).orElse(null);
+ SqlNodeList groupBy = selectStatement.getGroupBy().flatMap(optional ->
new GroupByConverter().convert(optional)).orElse(null);
+ SqlNode having = selectStatement.getHaving().flatMap(optional -> new
HavingConverter().convert(optional)).orElse(null);
+ SqlNodeList window =
SelectStatementHandler.getWindowSegment(selectStatement).flatMap(new
WindowConverter()::convert).orElse(SqlNodeList.EMPTY);
+ return new SqlSelect(SqlParserPos.ZERO, distinct, projection, from,
where, groupBy, having, window, null, null, null, null, SqlNodeList.EMPTY);
+ }
+
+ private SqlNode convertValues(final Collection<InsertValuesSegment>
insertValuesSegments) {
+ List<SqlNode> values = new ArrayList<>();
+ for (InsertValuesSegment each : insertValuesSegments) {
+ values.add(convertExpression(each.getValues().get(0)));
+ }
+ List<SqlNode> operands = new ArrayList<>();
+ operands.add(new SqlBasicCall(new SqlRowOperator("ROW"), values,
SqlParserPos.ZERO));
+ return new SqlBasicCall(new SqlValuesOperator(), operands,
SqlParserPos.ZERO);
+ }
+
+ private SqlNodeList convertColumn(final Collection<ColumnSegment>
columnSegments) {
+ List<SqlNode> columns = columnSegments.stream().map(each -> new
ColumnConverter().convert(each).orElseThrow(IllegalStateException::new)).collect(Collectors.toList());
+ if (columns.isEmpty()) {
+ return null;
+ }
+ return new SqlNodeList(columns, SqlParserPos.ZERO);
+ }
+
+ private SqlNode convertExpression(final ExpressionSegment
expressionSegment) {
+ return new
ExpressionConverter().convert(expressionSegment).orElseThrow(IllegalStateException::new);
+ }
+}
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 0211da24818..fc1233355d9 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
@@ -64,6 +64,8 @@ class SQLNodeConverterEngineIT {
private static final String UPDATE_STATEMENT_PREFIX = "UPDATE";
+ private static final String INSERT_STATEMENT_PREFIX = "INSERT";
+
@ParameterizedTest(name = "{0} ({1}) -> {2}")
@ArgumentsSource(TestCaseArgumentsProvider.class)
void assertConvert(final String sqlCaseId, final SQLCaseType sqlCaseType,
final String databaseType) {
@@ -108,7 +110,8 @@ class SQLNodeConverterEngineIT {
return
testParam.getSqlCaseId().toUpperCase().startsWith(SELECT_STATEMENT_PREFIX)
||
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(UPDATE_STATEMENT_PREFIX)
+ ||
testParam.getSqlCaseId().toUpperCase().startsWith(INSERT_STATEMENT_PREFIX);
}
}
}
diff --git a/test/it/optimizer/src/test/resources/converter/insert.xml
b/test/it/optimizer/src/test/resources/converter/insert.xml
new file mode 100644
index 00000000000..7ed3870d811
--- /dev/null
+++ b/test/it/optimizer/src/test/resources/converter/insert.xml
@@ -0,0 +1,21 @@
+<?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="insert_into_values" expected-sql="INSERT INTO
`test_nested` VALUES (1)" db-types="MySQL" sql-case-types="LITERAL" />
+</sql-node-converter-test-cases>
diff --git a/test/it/parser/src/main/resources/sql/supported/dml/insert.xml
b/test/it/parser/src/main/resources/sql/supported/dml/insert.xml
index ee36c40dc8b..7ec25d4c3d7 100644
--- a/test/it/parser/src/main/resources/sql/supported/dml/insert.xml
+++ b/test/it/parser/src/main/resources/sql/supported/dml/insert.xml
@@ -17,7 +17,7 @@
-->
<sql-cases>
- <sql-case id="insert_into_values" value="insert into test_nested
values(1)" db-types="MySQL" />
+ <sql-case id="insert_into_values" value="INSERT INTO test_nested
VALUES(1)" db-types="MySQL" />
<sql-case id="insert_with_all_placeholders" value="INSERT INTO t_order
(order_id, user_id, status) VALUES (?, ?, ?)" />
<sql-case id="insert_with_historical_type_cast_syntax" value="INSERT INTO
t_order (order_id, user_id, status) VALUES (?, ?::int4, ?::text)"
db-types="PostgreSQL,openGauss" />
<sql-case id="insert_with_now_function" value="INSERT INTO t_order_item
(item_id, order_id, user_id, status, creation_date) VALUES (?, ?, ?, 'insert',
now())" db-types="MySQL" />