You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "boyjoy1127 (via GitHub)" <gi...@apache.org> on 2023/08/30 03:38:03 UTC
[GitHub] [shardingsphere] boyjoy1127 opened a new pull request, #28314: Add insert statement converter.
boyjoy1127 opened a new pull request, #28314:
URL: https://github.com/apache/shardingsphere/pull/28314
Ref #27376.
Changes proposed in this pull request:
- add insert statement converter.
---
Before committing this PR, I'm sure that I have checked the following options:
- [X] My code follows the [code of conduct](https://shardingsphere.apache.org/community/en/involved/conduct/code/) of this project.
- [X] I have self-reviewed the commit code.
- [X] I have (or in comment I request) added corresponding labels for the pull request.
- [X] I have passed maven check locally : `./mvnw clean install -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e`.
- [X] I have made corresponding changes to the documentation.
- [X] I have added corresponding unit tests for my changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] strongduanmu commented on a diff in pull request #28314: Add insert statement converter.
Posted by "strongduanmu (via GitHub)" <gi...@apache.org>.
strongduanmu commented on code in PR #28314:
URL: https://github.com/apache/shardingsphere/pull/28314#discussion_r1310968807
##########
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.size() == 0) {
Review Comment:
Please put 0 on the left condition.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] strongduanmu merged pull request #28314: Add insert statement converter.
Posted by "strongduanmu (via GitHub)" <gi...@apache.org>.
strongduanmu merged PR #28314:
URL: https://github.com/apache/shardingsphere/pull/28314
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org