diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/from/type/SimpleTableSegmentBinder.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/from/type/SimpleTableSegmentBinder.java index eb57d97f82824..d4ea769c4c9ea 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/from/type/SimpleTableSegmentBinder.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/from/type/SimpleTableSegmentBinder.java @@ -80,8 +80,10 @@ public static SimpleTableSegment bind(final SimpleTableSegment segment, final SQ IdentifierValue tableName = segment.getTableName().getIdentifier(); ShardingSphereSchema schema = binderContext.getMetaData().getDatabase(databaseName.getValue()).getSchema(schemaName.getValue()); checkTableExists(binderContext, schema, schemaName.getValue(), tableName.getValue()); - tableBinderContexts.put(new CaseInsensitiveString(segment.getAliasName().orElseGet(tableName::getValue)), - createSimpleTableBinderContext(segment, schema, databaseName, schemaName, binderContext)); + if (null != tableBinderContexts) { + tableBinderContexts.put(new CaseInsensitiveString(segment.getAliasName().orElseGet(tableName::getValue)), + createSimpleTableBinderContext(segment, schema, databaseName, schemaName, binderContext)); + } TableNameSegment tableNameSegment = new TableNameSegment(segment.getTableName().getStartIndex(), segment.getTableName().getStopIndex(), tableName); tableNameSegment.setTableBoundInfo(new TableSegmentBoundInfo(databaseName, schemaName)); SimpleTableSegment result = new SimpleTableSegment(tableNameSegment); diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/AlterIndexStatementBinder.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/AlterIndexStatementBinder.java new file mode 100644 index 0000000000000..983d881060b61 --- /dev/null +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/AlterIndexStatementBinder.java @@ -0,0 +1,56 @@ +/* + * 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 lombok.SneakyThrows; +import org.apache.shardingsphere.infra.binder.engine.segment.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.AlterIndexStatement; +import org.apache.shardingsphere.sql.parser.statement.sqlserver.ddl.SQLServerAlterIndexStatement; + +/** + * Alter index statement binder. + */ +public class AlterIndexStatementBinder implements SQLStatementBinder { + + @Override + public AlterIndexStatement bind(final AlterIndexStatement sqlStatement, final SQLStatementBinderContext binderContext) { + if (sqlStatement instanceof SQLServerAlterIndexStatement) { + SQLServerAlterIndexStatement source = (SQLServerAlterIndexStatement) sqlStatement; + if (!source.getSimpleTable().isPresent()) { + return sqlStatement; + } + SQLServerAlterIndexStatement result = copy(source); + result.setSimpleTable(SimpleTableSegmentBinder.bind(source.getSimpleTable().get(), binderContext, null)); + return result; + } + return sqlStatement; + } + + @SneakyThrows(ReflectiveOperationException.class) + private static SQLServerAlterIndexStatement copy(final SQLServerAlterIndexStatement sqlStatement) { + SQLServerAlterIndexStatement result = sqlStatement.getClass().getDeclaredConstructor().newInstance(); + sqlStatement.getIndex().ifPresent(result::setIndex); + sqlStatement.getSimpleTable().ifPresent(result::setSimpleTable); + 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 6665fc27ec50e..d5168ac3a8aa9 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 @@ -19,11 +19,13 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; +import org.apache.shardingsphere.infra.binder.engine.statement.ddl.AlterIndexStatementBinder; import org.apache.shardingsphere.infra.binder.engine.statement.ddl.CreateIndexStatementBinder; import org.apache.shardingsphere.infra.binder.engine.statement.ddl.CreateTableStatementBinder; import org.apache.shardingsphere.infra.binder.engine.statement.ddl.CursorStatementBinder; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterIndexStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateIndexStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CursorStatement; @@ -58,6 +60,9 @@ public DDLStatement bind(final DDLStatement statement) { if (statement instanceof CreateIndexStatement) { return new CreateIndexStatementBinder().bind((CreateIndexStatement) statement, binderContext); } + if (statement instanceof AlterIndexStatement) { + return new AlterIndexStatementBinder().bind((AlterIndexStatement) statement, binderContext); + } return statement; } } diff --git a/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/dialect/sqlserver/SQLServerBinderIT.java b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/dialect/sqlserver/SQLServerBinderIT.java new file mode 100644 index 0000000000000..ed9847cb74574 --- /dev/null +++ b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/dialect/sqlserver/SQLServerBinderIT.java @@ -0,0 +1,25 @@ +/* + * 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.test.it.sql.binder.dialect.sqlserver; + +import org.apache.shardingsphere.test.it.sql.binder.SQLBinderIT; +import org.apache.shardingsphere.test.it.sql.binder.SQLBinderITSettings; + +@SQLBinderITSettings("SQLServer") +class SQLServerBinderIT extends SQLBinderIT { +} diff --git a/test/it/binder/src/test/resources/cases/ddl/alter-index.xml b/test/it/binder/src/test/resources/cases/ddl/alter-index.xml new file mode 100644 index 0000000000000..c6768222d1007 --- /dev/null +++ b/test/it/binder/src/test/resources/cases/ddl/alter-index.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + +
+
+
diff --git a/test/it/binder/src/test/resources/sqls/ddl/alter-index.xml b/test/it/binder/src/test/resources/sqls/ddl/alter-index.xml new file mode 100644 index 0000000000000..852ee567ac66e --- /dev/null +++ b/test/it/binder/src/test/resources/sqls/ddl/alter-index.xml @@ -0,0 +1,21 @@ + + + + + +