Skip to content

Commit

Permalink
Support create view, alter view, drop view sql bind logic (#34167)
Browse files Browse the repository at this point in the history
* Support create view, alter view, drop view sql bind logic

* Support create view, alter view, drop view sql bind logic

* Support create view, alter view, drop view sql bind logic

* update release note

* fix rewrite it
  • Loading branch information
strongduanmu authored Dec 26, 2024
1 parent e95517c commit 7f8fa57
Show file tree
Hide file tree
Showing 24 changed files with 442 additions and 18 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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)
1. SQL Binder: Support create view, alter view, drop view sql bind logic - [#34167](https://github.com/apache/shardingsphere/pull/34167)

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
@Override
public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final AlterViewStatementContext sqlStatementContext) {
AlterViewStatement alterViewStatement = sqlStatementContext.getSqlStatement();
Optional<SelectStatement> selectStatement = alterViewStatement.getSelectStatement();
Optional<SelectStatement> selectStatement = alterViewStatement.getSelect();
String originView = alterViewStatement.getView().getTableName().getIdentifier().getValue();
selectStatement.ifPresent(optional -> checkAlterViewShardingTables(rule, optional, originView));
alterViewStatement.getRenameView().ifPresent(optional -> checkBroadcastShardingView(rule, originView, optional.getTableName().getIdentifier().getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public AlterViewStatementContext(final AlterViewStatement sqlStatement) {
super(sqlStatement);
Collection<SimpleTableSegment> tables = new LinkedList<>();
tables.add(sqlStatement.getView());
Optional<SelectStatement> selectStatement = sqlStatement.getSelectStatement();
Optional<SelectStatement> selectStatement = sqlStatement.getSelect();
selectStatement.ifPresent(optional -> {
TableExtractor extractor = new TableExtractor();
extractor.extractTablesFromSelect(optional);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterTableStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterViewStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateViewStatement;
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.value.identifier.IdentifierValue;
Expand Down Expand Up @@ -129,7 +131,8 @@ private static IdentifierValue getSchemaName(final SimpleTableSegment segment, f
}

private static void checkTableExists(final SQLStatementBinderContext binderContext, final ShardingSphereSchema schema, final String schemaName, final String tableName) {
if (binderContext.getSqlStatement() instanceof CreateTableStatement) {
// TODO refactor table exists check with spi @duanzhengqiang
if (binderContext.getSqlStatement() instanceof CreateTableStatement && isCreateTable(((CreateTableStatement) binderContext.getSqlStatement()).getTable(), tableName)) {
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate()
|| ((CreateTableStatement) binderContext.getSqlStatement()).isIfNotExists() || !schema.containsTable(tableName), () -> new TableExistsException(tableName));
return;
Expand All @@ -146,6 +149,14 @@ private static void checkTableExists(final SQLStatementBinderContext binderConte
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate() || !schema.containsTable(tableName), () -> new TableExistsException(tableName));
return;
}
if (binderContext.getSqlStatement() instanceof CreateViewStatement && isCreateTable(((CreateViewStatement) binderContext.getSqlStatement()).getView(), tableName)) {
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate() || !schema.containsTable(tableName), () -> new TableExistsException(tableName));
return;
}
if (binderContext.getSqlStatement() instanceof AlterViewStatement && isRenameView((AlterViewStatement) binderContext.getSqlStatement(), tableName)) {
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate() || !schema.containsTable(tableName), () -> new TableExistsException(tableName));
return;
}
if ("DUAL".equalsIgnoreCase(tableName)) {
return;
}
Expand All @@ -158,6 +169,10 @@ private static void checkTableExists(final SQLStatementBinderContext binderConte
ShardingSpherePreconditions.checkState(schema.containsTable(tableName), () -> new TableNotFoundException(tableName));
}

private static boolean isCreateTable(final SimpleTableSegment simpleTableSegment, final String tableName) {
return simpleTableSegment.getTableName().getIdentifier().getValue().equalsIgnoreCase(tableName);
}

private static boolean isRenameTable(final AlterTableStatement alterTableStatement, final String tableName) {
return alterTableStatement.getRenameTable().isPresent() && alterTableStatement.getRenameTable().get().getTableName().getIdentifier().getValue().equalsIgnoreCase(tableName);
}
Expand All @@ -171,6 +186,10 @@ private static boolean isRenameTable(final RenameTableStatement renameTableState
return false;
}

private static boolean isRenameView(final AlterViewStatement alterViewStatement, final String tableName) {
return alterViewStatement.getRenameView().isPresent() && alterViewStatement.getRenameView().get().getTableName().getIdentifier().getValue().equalsIgnoreCase(tableName);
}

private static SimpleTableSegmentBinderContext createSimpleTableBinderContext(final SimpleTableSegment segment, final ShardingSphereSchema schema, final IdentifierValue databaseName,
final IdentifierValue schemaName, final SQLStatementBinderContext binderContext) {
IdentifierValue tableName = segment.getTableName().getIdentifier();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* Alter index statement binder.
*/
public class AlterIndexStatementBinder implements SQLStatementBinder<AlterIndexStatement> {
public final class AlterIndexStatementBinder implements SQLStatementBinder<AlterIndexStatement> {

@Override
public AlterIndexStatement bind(final AlterIndexStatement sqlStatement, final SQLStatementBinderContext binderContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 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.infra.binder.engine.statement.dml.SelectStatementBinder;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterViewStatement;

/**
* Alter view statement binder.
*/
public final class AlterViewStatementBinder implements SQLStatementBinder<AlterViewStatement> {

@Override
public AlterViewStatement bind(final AlterViewStatement sqlStatement, final SQLStatementBinderContext binderContext) {
AlterViewStatement result = copy(sqlStatement);
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create();
result.setView(SimpleTableSegmentBinder.bind(sqlStatement.getView(), binderContext, tableBinderContexts));
sqlStatement.getSelect().ifPresent(optional -> result.setSelect(new SelectStatementBinder().bind(optional, binderContext)));
sqlStatement.getRenameView().ifPresent(optional -> result.setRenameView(SimpleTableSegmentBinder.bind(optional, binderContext, tableBinderContexts)));
return result;
}

@SneakyThrows(ReflectiveOperationException.class)
private static AlterViewStatement copy(final AlterViewStatement sqlStatement) {
AlterViewStatement result = sqlStatement.getClass().getDeclaredConstructor().newInstance();
sqlStatement.getViewDefinition().ifPresent(result::setViewDefinition);
sqlStatement.getConstraintDefinition().ifPresent(result::setConstraintDefinition);
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
@@ -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.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.infra.binder.engine.statement.dml.SelectStatementBinder;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateViewStatement;

/**
* Create view statement binder.
*/
public final class CreateViewStatementBinder implements SQLStatementBinder<CreateViewStatement> {

@Override
public CreateViewStatement bind(final CreateViewStatement sqlStatement, final SQLStatementBinderContext binderContext) {
CreateViewStatement result = copy(sqlStatement);
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create();
result.setView(SimpleTableSegmentBinder.bind(sqlStatement.getView(), binderContext, tableBinderContexts));
result.setSelect(new SelectStatementBinder().bind(sqlStatement.getSelect(), binderContext));
return result;
}

@SneakyThrows(ReflectiveOperationException.class)
private static CreateViewStatement copy(final CreateViewStatement sqlStatement) {
CreateViewStatement result = sqlStatement.getClass().getDeclaredConstructor().newInstance();
result.setViewDefinition(sqlStatement.getViewDefinition());
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 @@ -30,7 +30,7 @@
/**
* Drop index statement binder.
*/
public class DropIndexStatementBinder implements SQLStatementBinder<DropIndexStatement> {
public final class DropIndexStatementBinder implements SQLStatementBinder<DropIndexStatement> {

@Override
public DropIndexStatement bind(final DropIndexStatement sqlStatement, final SQLStatementBinderContext binderContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.DropViewStatement;

/**
* Drop view statement binder.
*/
public final class DropViewStatementBinder implements SQLStatementBinder<DropViewStatement> {

@Override
public DropViewStatement bind(final DropViewStatement sqlStatement, final SQLStatementBinderContext binderContext) {
DropViewStatement result = copy(sqlStatement);
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create();
sqlStatement.getViews().forEach(each -> result.getViews().add(SimpleTableSegmentBinder.bind(each, binderContext, tableBinderContexts)));
return result;
}

@SneakyThrows(ReflectiveOperationException.class)
private static DropViewStatement copy(final DropViewStatement sqlStatement) {
DropViewStatement result = sqlStatement.getClass().getDeclaredConstructor().newInstance();
result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments());
result.getCommentSegments().addAll(sqlStatement.getCommentSegments());
result.getVariableNames().addAll(sqlStatement.getVariableNames());
return result;
}
}
Loading

0 comments on commit 7f8fa57

Please sign in to comment.