This is an automated email from the ASF dual-hosted git repository. chengzhang 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 5be4af425ff Support truncate table sql bind and add test case (#34162) 5be4af425ff is described below commit 5be4af425ffe569b68dfb825178961f0dff62580 Author: Zhengqiang Duan <duanzhengqi...@apache.org> AuthorDate: Thu Dec 26 16:37:50 2024 +0800 Support truncate table sql bind and add test case (#34162) * add test case for cursor statement bind * Support truncate table sql bind and add test case for truncate table * update release note --- RELEASE-NOTES.md | 1 + .../statement/ddl/TruncateStatementBinder.java | 51 ++++++++++++ .../binder/engine/type/DDLStatementBindEngine.java | 5 ++ .../binder/src/test/resources/cases/ddl/cursor.xml | 93 ++++++++++++++++++++++ .../test/resources/cases/ddl/truncate-table.xml | 28 +++++++ .../src/test/resources/sqls/ddl/truncate-table.xml | 21 +++++ .../binder/src/test/resources/sqls/dml/cursor.xml | 21 +++++ 7 files changed, 220 insertions(+) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 02663ad74b9..92a9855feda 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -43,6 +43,7 @@ 1. SQL Binder: Support alter table, drop table sql bind and add test case - [#34154](https://github.com/apache/shardingsphere/pull/34154) 1. SQL Binder: Support rename table statement sql bind and split segment bind to ddl and dml package - [#34158](https://github.com/apache/shardingsphere/pull/34158) 1. SQL Binder: Support copy statement sql bind and add bind test case - [#34159](https://github.com/apache/shardingsphere/pull/34159) +1. SQL Binder: Support truncate table sql bind and add test case - [#34162](https://github.com/apache/shardingsphere/pull/34162) ### Bug Fixes diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/TruncateStatementBinder.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/TruncateStatementBinder.java new file mode 100644 index 00000000000..f9a9969f71c --- /dev/null +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/TruncateStatementBinder.java @@ -0,0 +1,51 @@ +/* + * 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.infra.binder.engine.statement.ddl; + +import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString; +import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.Multimap; +import lombok.SneakyThrows; +import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.TableSegmentBinderContext; +import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.type.SimpleTableSegmentBinder; +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.ddl.TruncateStatement; + +/** + * Truncate statement binder. + */ +public final class TruncateStatementBinder implements SQLStatementBinder<TruncateStatement> { + + @Override + public TruncateStatement bind(final TruncateStatement sqlStatement, final SQLStatementBinderContext binderContext) { + TruncateStatement result = copy(sqlStatement); + Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create(); + sqlStatement.getTables().forEach(each -> result.getTables().add(SimpleTableSegmentBinder.bind(each, binderContext, tableBinderContexts))); + return result; + } + + @SneakyThrows(ReflectiveOperationException.class) + private static TruncateStatement copy(final TruncateStatement sqlStatement) { + TruncateStatement result = sqlStatement.getClass().getDeclaredConstructor().newInstance(); + result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments()); + result.getCommentSegments().addAll(sqlStatement.getCommentSegments()); + result.getVariableNames().addAll(sqlStatement.getVariableNames()); + return result; + } +} diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DDLStatementBindEngine.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DDLStatementBindEngine.java index 6f610e8610d..07de9be627a 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DDLStatementBindEngine.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DDLStatementBindEngine.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.binder.engine.statement.ddl.CreateTableSt import org.apache.shardingsphere.infra.binder.engine.statement.ddl.CursorStatementBinder; import org.apache.shardingsphere.infra.binder.engine.statement.ddl.DropTableStatementBinder; import org.apache.shardingsphere.infra.binder.engine.statement.ddl.RenameTableStatementBinder; +import org.apache.shardingsphere.infra.binder.engine.statement.ddl.TruncateStatementBinder; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterTableStatement; @@ -34,6 +35,7 @@ import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CursorS import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.RenameTableStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.TruncateStatement; /** * DDL statement bind engine. @@ -73,6 +75,9 @@ public final class DDLStatementBindEngine { if (statement instanceof CreateIndexStatement) { return new CreateIndexStatementBinder().bind((CreateIndexStatement) statement, binderContext); } + if (statement instanceof TruncateStatement) { + return new TruncateStatementBinder().bind((TruncateStatement) statement, binderContext); + } return statement; } } diff --git a/test/it/binder/src/test/resources/cases/ddl/cursor.xml b/test/it/binder/src/test/resources/cases/ddl/cursor.xml new file mode 100644 index 00000000000..0772e747d00 --- /dev/null +++ b/test/it/binder/src/test/resources/cases/ddl/cursor.xml @@ -0,0 +1,93 @@ +<?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> + <cursor sql-case-id="create_cursor"> + <cursor-name name="t_order_cursor" start-index="7" stop-index="20" /> + <select> + <projections start-index="33" stop-index="33"> + <shorthand-projection start-index="33" stop-index="33"> + <actual-projections> + <column-projection name="order_id" start-index="0" stop-index="0" start-delimiter=""" end-delimiter="""> + <owner name="o" start-index="0" stop-index="0" /> + <column-bound> + <original-database name="foo_db_1" /> + <original-schema name="public" /> + <original-table name="t_order" /> + <original-column name="order_id" start-delimiter=""" end-delimiter=""" /> + </column-bound> + </column-projection> + <column-projection name="user_id" start-index="0" stop-index="0" start-delimiter=""" end-delimiter="""> + <owner name="o" start-index="0" stop-index="0" /> + <column-bound> + <original-database name="foo_db_1" /> + <original-schema name="public" /> + <original-table name="t_order" /> + <original-column name="user_id" start-delimiter=""" end-delimiter=""" /> + </column-bound> + </column-projection> + <column-projection name="status" start-index="0" stop-index="0" start-delimiter=""" end-delimiter="""> + <owner name="o" start-index="0" stop-index="0" /> + <column-bound> + <original-database name="foo_db_1" /> + <original-schema name="public" /> + <original-table name="t_order" /> + <original-column name="status" start-delimiter=""" end-delimiter=""" /> + </column-bound> + </column-projection> + <column-projection name="merchant_id" start-index="0" stop-index="0" start-delimiter=""" end-delimiter="""> + <owner name="o" start-index="0" stop-index="0" /> + <column-bound> + <original-database name="foo_db_1" /> + <original-schema name="public" /> + <original-table name="t_order" /> + <original-column name="merchant_id" start-delimiter=""" end-delimiter=""" /> + </column-bound> + </column-projection> + <column-projection name="remark" start-index="0" stop-index="0" start-delimiter=""" end-delimiter="""> + <owner name="o" start-index="0" stop-index="0" /> + <column-bound> + <original-database name="foo_db_1" /> + <original-schema name="public" /> + <original-table name="t_order" /> + <original-column name="remark" start-delimiter=""" end-delimiter=""" /> + </column-bound> + </column-projection> + <column-projection name="creation_date" start-index="0" stop-index="0" start-delimiter=""" end-delimiter="""> + <owner name="o" start-index="0" stop-index="0" /> + <column-bound> + <original-database name="foo_db_1" /> + <original-schema name="public" /> + <original-table name="t_order" /> + <original-column name="creation_date" start-delimiter=""" end-delimiter=""" /> + </column-bound> + </column-projection> + </actual-projections> + </shorthand-projection> + </projections> + <from> + <simple-table name="t_order" start-index="40" stop-index="48" alias="o"> + <table-bound> + <original-database name="foo_db_1" /> + <original-schema name="public" /> + </table-bound> + </simple-table> + </from> + </select> + </cursor> +</sql-parser-test-cases> diff --git a/test/it/binder/src/test/resources/cases/ddl/truncate-table.xml b/test/it/binder/src/test/resources/cases/ddl/truncate-table.xml new file mode 100644 index 00000000000..41759a90373 --- /dev/null +++ b/test/it/binder/src/test/resources/cases/ddl/truncate-table.xml @@ -0,0 +1,28 @@ +<?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> + <truncate sql-case-id="truncate_table"> + <table name="t_order" start-index="15" stop-index="21"> + <table-bound> + <original-database name="foo_db_1" /> + <original-schema name="foo_db_1" /> + </table-bound> + </table> + </truncate> +</sql-parser-test-cases> diff --git a/test/it/binder/src/test/resources/sqls/ddl/truncate-table.xml b/test/it/binder/src/test/resources/sqls/ddl/truncate-table.xml new file mode 100644 index 00000000000..0b49b1b2362 --- /dev/null +++ b/test/it/binder/src/test/resources/sqls/ddl/truncate-table.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-cases> + <sql-case id="truncate_table" value="TRUNCATE TABLE t_order" db-types="MySQL,Doris" /> +</sql-cases> diff --git a/test/it/binder/src/test/resources/sqls/dml/cursor.xml b/test/it/binder/src/test/resources/sqls/dml/cursor.xml new file mode 100644 index 00000000000..d952df8cc2f --- /dev/null +++ b/test/it/binder/src/test/resources/sqls/dml/cursor.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-cases> + <sql-case id="create_cursor" value="CURSOR t_order_cursor FOR SELECT * FROM t_order o" db-types="openGauss"/> +</sql-cases>