From 93465fa3218d710dbcd283a1a5ca77ddc8635f0f Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Mon, 6 Jan 2025 19:42:37 +0800 Subject: [PATCH] Support show create table, show columns, show index statement bind --- .../dal/filter/ShowFilterSegmentBinder.java | 52 ++++++++++++++++++ .../dal/ShowColumnsStatementBinder.java | 54 +++++++++++++++++++ .../dal/ShowCreateTableStatementBinder.java | 51 ++++++++++++++++++ .../dal/ShowIndexStatementBinder.java | 52 ++++++++++++++++++ .../engine/type/DALStatementBindEngine.java | 15 ++++++ .../test/resources/cases/dal/show-columns.xml | 38 +++++++++++++ .../resources/cases/dal/show-create-table.xml | 38 +++++++++++++ .../test/resources/cases/dal/show-index.xml | 48 +++++++++++++++++ .../test/resources/sqls/dal/show-columns.xml | 22 ++++++++ .../resources/sqls/dal/show-create-table.xml | 22 ++++++++ .../test/resources/sqls/dal/show-index.xml | 23 ++++++++ .../impl/ShowCreateTableStatementAssert.java | 4 ++ 12 files changed, 419 insertions(+) create mode 100644 infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dal/filter/ShowFilterSegmentBinder.java create mode 100644 infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ShowColumnsStatementBinder.java create mode 100644 infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ShowCreateTableStatementBinder.java create mode 100644 infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ShowIndexStatementBinder.java create mode 100644 test/it/binder/src/test/resources/cases/dal/show-columns.xml create mode 100644 test/it/binder/src/test/resources/cases/dal/show-create-table.xml create mode 100644 test/it/binder/src/test/resources/cases/dal/show-index.xml create mode 100644 test/it/binder/src/test/resources/sqls/dal/show-columns.xml create mode 100644 test/it/binder/src/test/resources/sqls/dal/show-create-table.xml create mode 100644 test/it/binder/src/test/resources/sqls/dal/show-index.xml diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dal/filter/ShowFilterSegmentBinder.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dal/filter/ShowFilterSegmentBinder.java new file mode 100644 index 0000000000000..6e94a2d167cbc --- /dev/null +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dal/filter/ShowFilterSegmentBinder.java @@ -0,0 +1,52 @@ +/* + * 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.segment.dal.filter; + +import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString; +import com.google.common.collect.Multimap; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.TableSegmentBinderContext; +import org.apache.shardingsphere.infra.binder.engine.segment.dml.predicate.WhereSegmentBinder; +import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; +import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.ShowFilterSegment; + +/** + * Show filter segment binder. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ShowFilterSegmentBinder { + + /** + * Bind show filter segment. + * + * @param segment show filter segment + * @param binderContext SQL statement binder context + * @param tableBinderContexts table binder contexts + * @param outerTableBinderContexts outer table binder contexts + * @return bound show filter segment + */ + public static ShowFilterSegment bind(final ShowFilterSegment segment, final SQLStatementBinderContext binderContext, + final Multimap tableBinderContexts, + final Multimap outerTableBinderContexts) { + ShowFilterSegment result = new ShowFilterSegment(segment.getStartIndex(), segment.getStopIndex()); + segment.getLike().ifPresent(result::setLike); + segment.getWhere().ifPresent(optional -> result.setWhere(WhereSegmentBinder.bind(optional, binderContext, tableBinderContexts, outerTableBinderContexts))); + return result; + } +} diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ShowColumnsStatementBinder.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ShowColumnsStatementBinder.java new file mode 100644 index 0000000000000..3860e3e10d74f --- /dev/null +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ShowColumnsStatementBinder.java @@ -0,0 +1,54 @@ +/* + * 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.dal; + +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.dal.filter.ShowFilterSegmentBinder; +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.dal.ShowColumnsStatement; + +/** + * Show columns statement binder. + */ +public final class ShowColumnsStatementBinder implements SQLStatementBinder { + + @Override + public ShowColumnsStatement bind(final ShowColumnsStatement sqlStatement, final SQLStatementBinderContext binderContext) { + ShowColumnsStatement result = copy(sqlStatement); + Multimap tableBinderContexts = LinkedHashMultimap.create(); + result.setTable(SimpleTableSegmentBinder.bind(sqlStatement.getTable(), binderContext, tableBinderContexts)); + sqlStatement.getFromDatabase().ifPresent(result::setFromDatabase); + sqlStatement.getFilter().ifPresent(optional -> result.setFilter(ShowFilterSegmentBinder.bind(optional, binderContext, tableBinderContexts, LinkedHashMultimap.create()))); + return result; + } + + @SneakyThrows(ReflectiveOperationException.class) + private static ShowColumnsStatement copy(final ShowColumnsStatement sqlStatement) { + ShowColumnsStatement 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/statement/dal/ShowCreateTableStatementBinder.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ShowCreateTableStatementBinder.java new file mode 100644 index 0000000000000..d3fabf6be508a --- /dev/null +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ShowCreateTableStatementBinder.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.dal; + +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.dal.ShowCreateTableStatement; + +/** + * Show create table statement binder. + */ +public final class ShowCreateTableStatementBinder implements SQLStatementBinder { + + @Override + public ShowCreateTableStatement bind(final ShowCreateTableStatement sqlStatement, final SQLStatementBinderContext binderContext) { + ShowCreateTableStatement result = copy(sqlStatement); + Multimap tableBinderContexts = LinkedHashMultimap.create(); + result.setTable(SimpleTableSegmentBinder.bind(sqlStatement.getTable(), binderContext, tableBinderContexts)); + return result; + } + + @SneakyThrows(ReflectiveOperationException.class) + private static ShowCreateTableStatement copy(final ShowCreateTableStatement sqlStatement) { + ShowCreateTableStatement 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/statement/dal/ShowIndexStatementBinder.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ShowIndexStatementBinder.java new file mode 100644 index 0000000000000..3c165e9152918 --- /dev/null +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ShowIndexStatementBinder.java @@ -0,0 +1,52 @@ +/* + * 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.dal; + +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.dal.ShowIndexStatement; + +/** + * Show index statement binder. + */ +public final class ShowIndexStatementBinder implements SQLStatementBinder { + + @Override + public ShowIndexStatement bind(final ShowIndexStatement sqlStatement, final SQLStatementBinderContext binderContext) { + ShowIndexStatement result = copy(sqlStatement); + Multimap tableBinderContexts = LinkedHashMultimap.create(); + result.setTable(SimpleTableSegmentBinder.bind(sqlStatement.getTable(), binderContext, tableBinderContexts)); + sqlStatement.getFromDatabase().ifPresent(result::setFromDatabase); + return result; + } + + @SneakyThrows(ReflectiveOperationException.class) + private static ShowIndexStatement copy(final ShowIndexStatement sqlStatement) { + ShowIndexStatement 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/DALStatementBindEngine.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java index 2ab63622ff8a7..e2f2cbc6815a4 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java @@ -20,10 +20,16 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; import org.apache.shardingsphere.infra.binder.engine.statement.dal.OptimizeTableStatementBinder; +import org.apache.shardingsphere.infra.binder.engine.statement.dal.ShowColumnsStatementBinder; +import org.apache.shardingsphere.infra.binder.engine.statement.dal.ShowCreateTableStatementBinder; +import org.apache.shardingsphere.infra.binder.engine.statement.dal.ShowIndexStatementBinder; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DALStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.OptimizeTableStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ShowColumnsStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ShowCreateTableStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ShowIndexStatement; /** * DAL statement bind engine. @@ -48,6 +54,15 @@ public DALStatement bind(final DALStatement statement) { if (statement instanceof OptimizeTableStatement) { return new OptimizeTableStatementBinder().bind((OptimizeTableStatement) statement, binderContext); } + if (statement instanceof ShowCreateTableStatement) { + return new ShowCreateTableStatementBinder().bind((ShowCreateTableStatement) statement, binderContext); + } + if (statement instanceof ShowColumnsStatement) { + return new ShowColumnsStatementBinder().bind((ShowColumnsStatement) statement, binderContext); + } + if (statement instanceof ShowIndexStatement) { + return new ShowIndexStatementBinder().bind((ShowIndexStatement) statement, binderContext); + } return statement; } } diff --git a/test/it/binder/src/test/resources/cases/dal/show-columns.xml b/test/it/binder/src/test/resources/cases/dal/show-columns.xml new file mode 100644 index 0000000000000..a76feaf30bdee --- /dev/null +++ b/test/it/binder/src/test/resources/cases/dal/show-columns.xml @@ -0,0 +1,38 @@ + + + + + + + + + + +
+
+ + + + + + + + +
+
+
diff --git a/test/it/binder/src/test/resources/cases/dal/show-create-table.xml b/test/it/binder/src/test/resources/cases/dal/show-create-table.xml new file mode 100644 index 0000000000000..c7224906a8d9d --- /dev/null +++ b/test/it/binder/src/test/resources/cases/dal/show-create-table.xml @@ -0,0 +1,38 @@ + + + + + + + + + + +
+
+ + + + + + + + +
+
+
diff --git a/test/it/binder/src/test/resources/cases/dal/show-index.xml b/test/it/binder/src/test/resources/cases/dal/show-index.xml new file mode 100644 index 0000000000000..cf2a6e13ecba0 --- /dev/null +++ b/test/it/binder/src/test/resources/cases/dal/show-index.xml @@ -0,0 +1,48 @@ + + + + + + + + + + +
+
+ + + + + + + +
+ +
+ + + + + + + + +
+
+
diff --git a/test/it/binder/src/test/resources/sqls/dal/show-columns.xml b/test/it/binder/src/test/resources/sqls/dal/show-columns.xml new file mode 100644 index 0000000000000..5847f2c264688 --- /dev/null +++ b/test/it/binder/src/test/resources/sqls/dal/show-columns.xml @@ -0,0 +1,22 @@ + + + + + + + diff --git a/test/it/binder/src/test/resources/sqls/dal/show-create-table.xml b/test/it/binder/src/test/resources/sqls/dal/show-create-table.xml new file mode 100644 index 0000000000000..45249ab533731 --- /dev/null +++ b/test/it/binder/src/test/resources/sqls/dal/show-create-table.xml @@ -0,0 +1,22 @@ + + + + + + + diff --git a/test/it/binder/src/test/resources/sqls/dal/show-index.xml b/test/it/binder/src/test/resources/sqls/dal/show-index.xml new file mode 100644 index 0000000000000..a0f09bf87c3f1 --- /dev/null +++ b/test/it/binder/src/test/resources/sqls/dal/show-index.xml @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/statement/dal/impl/ShowCreateTableStatementAssert.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/statement/dal/impl/ShowCreateTableStatementAssert.java index 901aff442396a..bbd9ea0e0e9c4 100644 --- a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/statement/dal/impl/ShowCreateTableStatementAssert.java +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/statement/dal/impl/ShowCreateTableStatementAssert.java @@ -21,6 +21,7 @@ import lombok.NoArgsConstructor; import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ShowCreateTableStatement; import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext; +import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.table.TableAssert; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.dal.ShowCreateTableStatementTestCase; /** @@ -37,5 +38,8 @@ public final class ShowCreateTableStatementAssert { * @param expected expected show create table statement test case */ public static void assertIs(final SQLCaseAssertContext assertContext, final ShowCreateTableStatement actual, final ShowCreateTableStatementTestCase expected) { + if (null != actual.getTable()) { + TableAssert.assertIs(assertContext, actual.getTable(), expected.getTable()); + } } }