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

Add more sql binder test case for insert statement #34149

Merged
merged 3 commits into from
Dec 25, 2024
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 @@ -53,7 +53,9 @@ private static CreateTableStatement copy(final CreateTableStatement sqlStatement
result.getColumns().addAll(sqlStatement.getColumns());
sqlStatement.getLikeTable().ifPresent(result::setLikeTable);
sqlStatement.getCreateTableOption().ifPresent(result::setCreateTableOption);
result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments());
result.getCommentSegments().addAll(sqlStatement.getCommentSegments());
result.getVariableNames().addAll(sqlStatement.getVariableNames());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private static CursorStatement copy(final CursorStatement sqlStatement) {
result.setCursorName(sqlStatement.getCursorName());
result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments());
result.getCommentSegments().addAll(sqlStatement.getCommentSegments());
result.getVariableNames().addAll(sqlStatement.getVariableNames());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.binder.engine.segment.from.TableSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.segment.from.context.TableSegmentBinderContext;
import org.apache.shardingsphere.infra.binder.engine.segment.order.OrderBySegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.segment.predicate.WhereSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.segment.with.WithSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinder;
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
Expand All @@ -37,20 +39,22 @@ public final class DeleteStatementBinder implements SQLStatementBinder<DeleteSta
public DeleteStatement bind(final DeleteStatement sqlStatement, final SQLStatementBinderContext binderContext) {
DeleteStatement result = copy(sqlStatement);
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create();
sqlStatement.getWithSegment().ifPresent(optional -> result.setWithSegment(WithSegmentBinder.bind(optional, binderContext, binderContext.getExternalTableBinderContexts())));
result.setTable(TableSegmentBinder.bind(sqlStatement.getTable(), binderContext, tableBinderContexts, LinkedHashMultimap.create()));
sqlStatement.getWhere().ifPresent(optional -> result.setWhere(WhereSegmentBinder.bind(optional, binderContext, tableBinderContexts, LinkedHashMultimap.create())));
sqlStatement.getOrderBy().ifPresent(optional -> result.setOrderBy(
OrderBySegmentBinder.bind(optional, binderContext, LinkedHashMultimap.create(), tableBinderContexts, LinkedHashMultimap.create())));
return result;
}

@SneakyThrows(ReflectiveOperationException.class)
private DeleteStatement copy(final DeleteStatement sqlStatement) {
DeleteStatement result = sqlStatement.getClass().getDeclaredConstructor().newInstance();
sqlStatement.getOrderBy().ifPresent(result::setOrderBy);
sqlStatement.getLimit().ifPresent(result::setLimit);
sqlStatement.getWithSegment().ifPresent(result::setWithSegment);
sqlStatement.getOutputSegment().ifPresent(result::setOutputSegment);
result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments());
result.getCommentSegments().addAll(sqlStatement.getCommentSegments());
result.getVariableNames().addAll(sqlStatement.getVariableNames());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shardingsphere.infra.binder.engine.segment.expression.type.SubquerySegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.segment.from.context.TableSegmentBinderContext;
import org.apache.shardingsphere.infra.binder.engine.segment.from.type.SimpleTableSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.segment.with.WithSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinder;
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
Expand All @@ -44,6 +45,7 @@ public final class InsertStatementBinder implements SQLStatementBinder<InsertSta
public InsertStatement bind(final InsertStatement sqlStatement, final SQLStatementBinderContext binderContext) {
InsertStatement result = copy(sqlStatement);
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create();
sqlStatement.getWithSegment().ifPresent(optional -> result.setWithSegment(WithSegmentBinder.bind(optional, binderContext, binderContext.getExternalTableBinderContexts())));
sqlStatement.getTable().ifPresent(optional -> result.setTable(SimpleTableSegmentBinder.bind(optional, binderContext, tableBinderContexts)));
if (sqlStatement.getInsertColumns().isPresent() && !sqlStatement.getInsertColumns().get().getColumns().isEmpty()) {
result.setInsertColumns(InsertColumnsSegmentBinder.bind(sqlStatement.getInsertColumns().get(), binderContext, tableBinderContexts));
Expand All @@ -61,14 +63,14 @@ private InsertStatement copy(final InsertStatement sqlStatement) {
result.getValues().addAll(sqlStatement.getValues());
sqlStatement.getSetAssignment().ifPresent(result::setSetAssignment);
sqlStatement.getOnDuplicateKeyColumns().ifPresent(result::setOnDuplicateKeyColumns);
sqlStatement.getWithSegment().ifPresent(result::setWithSegment);
sqlStatement.getOutputSegment().ifPresent(result::setOutputSegment);
sqlStatement.getMultiTableInsertType().ifPresent(result::setMultiTableInsertType);
sqlStatement.getMultiTableInsertIntoSegment().ifPresent(result::setMultiTableInsertIntoSegment);
sqlStatement.getMultiTableConditionalIntoSegment().ifPresent(result::setMultiTableConditionalIntoSegment);
sqlStatement.getReturningSegment().ifPresent(result::setReturningSegment);
result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments());
result.getCommentSegments().addAll(sqlStatement.getCommentSegments());
result.getVariableNames().addAll(sqlStatement.getVariableNames());
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.infra.binder.engine.segment.assign.AssignmentSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.segment.from.TableSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.segment.from.context.TableSegmentBinderContext;
import org.apache.shardingsphere.infra.binder.engine.segment.order.OrderBySegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.segment.predicate.WhereSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.segment.with.WithSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinder;
Expand All @@ -44,16 +45,18 @@ public UpdateStatement bind(final UpdateStatement sqlStatement, final SQLStateme
sqlStatement.getFrom().ifPresent(optional -> result.setFrom(TableSegmentBinder.bind(optional, binderContext, tableBinderContexts, LinkedHashMultimap.create())));
sqlStatement.getAssignmentSegment().ifPresent(optional -> result.setSetAssignment(AssignmentSegmentBinder.bind(optional, binderContext, tableBinderContexts, LinkedHashMultimap.create())));
sqlStatement.getWhere().ifPresent(optional -> result.setWhere(WhereSegmentBinder.bind(optional, binderContext, tableBinderContexts, LinkedHashMultimap.create())));
sqlStatement.getOrderBy().ifPresent(optional -> result.setOrderBy(
OrderBySegmentBinder.bind(optional, binderContext, LinkedHashMultimap.create(), tableBinderContexts, LinkedHashMultimap.create())));
return result;
}

@SneakyThrows(ReflectiveOperationException.class)
private UpdateStatement copy(final UpdateStatement sqlStatement) {
UpdateStatement result = sqlStatement.getClass().getDeclaredConstructor().newInstance();
sqlStatement.getOrderBy().ifPresent(result::setOrderBy);
sqlStatement.getLimit().ifPresent(result::setLimit);
result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments());
result.getCommentSegments().addAll(sqlStatement.getCommentSegments());
result.getVariableNames().addAll(sqlStatement.getVariableNames());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<original-database name="foo_db_1" />
<original-schema name="public" />
<original-table name="t_order" />
<original-column name="user_id" />
<original-column name="user_id" start-delimiter="&quot;" end-delimiter="&quot;" />
</column-bound>
</column>
</create-index>
Expand Down
188 changes: 188 additions & 0 deletions test/it/binder/src/test/resources/cases/dml/insert.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?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-parser-test-cases>
<insert sql-case-id="insert_with_columns">
<table name="t_order" start-index="12" stop-index="18">
<table-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
</table-bound>
</table>
<columns start-index="20" stop-index="82">
<column name="order_id" start-index="21" stop-index="28">
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="order_id" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
<column name="user_id" start-index="31" stop-index="37">
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="user_id" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
<column name="status" start-index="40" stop-index="45">
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="status" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
<column name="merchant_id" start-index="48" stop-index="58">
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="merchant_id" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
<column name="remark" start-index="61" stop-index="66">
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="remark" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
<column name="creation_date" start-index="69" stop-index="81">
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="creation_date" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
</columns>
<values>
<value>
<assignment-value>
<literal-expression value="1" start-index="92" stop-index="92" />
</assignment-value>
<assignment-value>
<literal-expression value="1" start-index="95" stop-index="95" />
</assignment-value>
<assignment-value>
<literal-expression value="OK" start-index="98" stop-index="101" />
</assignment-value>
<assignment-value>
<literal-expression value="1" start-index="104" stop-index="104" />
</assignment-value>
<assignment-value>
<literal-expression value="TEST" start-index="107" stop-index="112" />
</assignment-value>
<assignment-value>
<literal-expression value="2024-12-25" start-index="115" stop-index="126" />
</assignment-value>
</value>
</values>
</insert>

<insert sql-case-id="insert_without_columns">
<table name="t_order" start-index="12" stop-index="18">
<table-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
</table-bound>
</table>
<columns start-index="19" stop-index="19" />
<derived-columns start-index="0" stop-index="0">
<column name="order_id" start-index="0" stop-index="0" start-delimiter="`" end-delimiter="`" >
<owner name="t_order" start-index="0" stop-index="0" />
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="order_id" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
<column name="user_id" start-index="0" stop-index="0" start-delimiter="`" end-delimiter="`" >
<owner name="t_order" start-index="0" stop-index="0" />
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="user_id" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
<column name="status" start-index="0" stop-index="0" start-delimiter="`" end-delimiter="`" >
<owner name="t_order" start-index="0" stop-index="0" />
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="status" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
<column name="merchant_id" start-index="0" stop-index="0" start-delimiter="`" end-delimiter="`" >
<owner name="t_order" start-index="0" stop-index="0" />
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="merchant_id" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
<column name="remark" start-index="0" stop-index="0" start-delimiter="`" end-delimiter="`" >
<owner name="t_order" start-index="0" stop-index="0" />
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="remark" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
<column name="creation_date" start-index="0" stop-index="0" start-delimiter="`" end-delimiter="`" >
<owner name="t_order" start-index="0" stop-index="0" />
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order" />
<original-column name="creation_date" start-delimiter="`" end-delimiter="`" />
</column-bound>
</column>
</derived-columns>
<values>
<value>
<assignment-value>
<literal-expression value="1" start-index="28" stop-index="28" />
</assignment-value>
<assignment-value>
<literal-expression value="1" start-index="31" stop-index="31" />
</assignment-value>
<assignment-value>
<literal-expression value="OK" start-index="34" stop-index="37" />
</assignment-value>
<assignment-value>
<literal-expression value="1" start-index="40" stop-index="40" />
</assignment-value>
<assignment-value>
<literal-expression value="TEST" start-index="43" stop-index="48" />
</assignment-value>
<assignment-value>
<literal-expression value="2024-12-25" start-index="51" stop-index="62" />
</assignment-value>
</value>
</values>
</insert>
</sql-parser-test-cases>
22 changes: 22 additions & 0 deletions test/it/binder/src/test/resources/sqls/dml/insert.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-cases>
<sql-case id="insert_with_columns" value="INSERT INTO t_order (order_id, user_id, status, merchant_id, remark, creation_date) VALUES (1, 1, 'OK', 1, 'TEST', '2024-12-25')" db-types="MySQL"/>
<sql-case id="insert_without_columns" value="INSERT INTO t_order VALUES (1, 1, 'OK', 1, 'TEST', '2024-12-25')" db-types="MySQL"/>
</sql-cases>
Loading
Loading