Skip to content

Commit 7f8fa57

Browse files
authored
Support create view, alter view, drop view sql bind logic (#34167)
* 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
1 parent e95517c commit 7f8fa57

File tree

24 files changed

+442
-18
lines changed

24 files changed

+442
-18
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
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)
4545
1. SQL Binder: Support copy statement sql bind and add bind test case - [#34159](https://github.com/apache/shardingsphere/pull/34159)
4646
1. SQL Binder: Support truncate table sql bind and add test case - [#34162](https://github.com/apache/shardingsphere/pull/34162)
47+
1. SQL Binder: Support create view, alter view, drop view sql bind logic - [#34167](https://github.com/apache/shardingsphere/pull/34167)
4748

4849
### Bug Fixes
4950

features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterViewSupportedChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
4747
@Override
4848
public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final AlterViewStatementContext sqlStatementContext) {
4949
AlterViewStatement alterViewStatement = sqlStatementContext.getSqlStatement();
50-
Optional<SelectStatement> selectStatement = alterViewStatement.getSelectStatement();
50+
Optional<SelectStatement> selectStatement = alterViewStatement.getSelect();
5151
String originView = alterViewStatement.getView().getTableName().getIdentifier().getValue();
5252
selectStatement.ifPresent(optional -> checkAlterViewShardingTables(rule, optional, originView));
5353
alterViewStatement.getRenameView().ifPresent(optional -> checkBroadcastShardingView(rule, originView, optional.getTableName().getIdentifier().getValue()));

infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/AlterViewStatementContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public AlterViewStatementContext(final AlterViewStatement sqlStatement) {
4242
super(sqlStatement);
4343
Collection<SimpleTableSegment> tables = new LinkedList<>();
4444
tables.add(sqlStatement.getView());
45-
Optional<SelectStatement> selectStatement = sqlStatement.getSelectStatement();
45+
Optional<SelectStatement> selectStatement = sqlStatement.getSelect();
4646
selectStatement.ifPresent(optional -> {
4747
TableExtractor extractor = new TableExtractor();
4848
extractor.extractTablesFromSelect(optional);

infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/from/type/SimpleTableSegmentBinder.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@
5151
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
5252
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
5353
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterTableStatement;
54+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterViewStatement;
5455
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement;
56+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateViewStatement;
5557
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
5658
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.RenameTableStatement;
5759
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
@@ -129,7 +131,8 @@ private static IdentifierValue getSchemaName(final SimpleTableSegment segment, f
129131
}
130132

131133
private static void checkTableExists(final SQLStatementBinderContext binderContext, final ShardingSphereSchema schema, final String schemaName, final String tableName) {
132-
if (binderContext.getSqlStatement() instanceof CreateTableStatement) {
134+
// TODO refactor table exists check with spi @duanzhengqiang
135+
if (binderContext.getSqlStatement() instanceof CreateTableStatement && isCreateTable(((CreateTableStatement) binderContext.getSqlStatement()).getTable(), tableName)) {
133136
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate()
134137
|| ((CreateTableStatement) binderContext.getSqlStatement()).isIfNotExists() || !schema.containsTable(tableName), () -> new TableExistsException(tableName));
135138
return;
@@ -146,6 +149,14 @@ private static void checkTableExists(final SQLStatementBinderContext binderConte
146149
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate() || !schema.containsTable(tableName), () -> new TableExistsException(tableName));
147150
return;
148151
}
152+
if (binderContext.getSqlStatement() instanceof CreateViewStatement && isCreateTable(((CreateViewStatement) binderContext.getSqlStatement()).getView(), tableName)) {
153+
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate() || !schema.containsTable(tableName), () -> new TableExistsException(tableName));
154+
return;
155+
}
156+
if (binderContext.getSqlStatement() instanceof AlterViewStatement && isRenameView((AlterViewStatement) binderContext.getSqlStatement(), tableName)) {
157+
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate() || !schema.containsTable(tableName), () -> new TableExistsException(tableName));
158+
return;
159+
}
149160
if ("DUAL".equalsIgnoreCase(tableName)) {
150161
return;
151162
}
@@ -158,6 +169,10 @@ private static void checkTableExists(final SQLStatementBinderContext binderConte
158169
ShardingSpherePreconditions.checkState(schema.containsTable(tableName), () -> new TableNotFoundException(tableName));
159170
}
160171

172+
private static boolean isCreateTable(final SimpleTableSegment simpleTableSegment, final String tableName) {
173+
return simpleTableSegment.getTableName().getIdentifier().getValue().equalsIgnoreCase(tableName);
174+
}
175+
161176
private static boolean isRenameTable(final AlterTableStatement alterTableStatement, final String tableName) {
162177
return alterTableStatement.getRenameTable().isPresent() && alterTableStatement.getRenameTable().get().getTableName().getIdentifier().getValue().equalsIgnoreCase(tableName);
163178
}
@@ -171,6 +186,10 @@ private static boolean isRenameTable(final RenameTableStatement renameTableState
171186
return false;
172187
}
173188

189+
private static boolean isRenameView(final AlterViewStatement alterViewStatement, final String tableName) {
190+
return alterViewStatement.getRenameView().isPresent() && alterViewStatement.getRenameView().get().getTableName().getIdentifier().getValue().equalsIgnoreCase(tableName);
191+
}
192+
174193
private static SimpleTableSegmentBinderContext createSimpleTableBinderContext(final SimpleTableSegment segment, final ShardingSphereSchema schema, final IdentifierValue databaseName,
175194
final IdentifierValue schemaName, final SQLStatementBinderContext binderContext) {
176195
IdentifierValue tableName = segment.getTableName().getIdentifier();

infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/AlterIndexStatementBinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/**
3131
* Alter index statement binder.
3232
*/
33-
public class AlterIndexStatementBinder implements SQLStatementBinder<AlterIndexStatement> {
33+
public final class AlterIndexStatementBinder implements SQLStatementBinder<AlterIndexStatement> {
3434

3535
@Override
3636
public AlterIndexStatement bind(final AlterIndexStatement sqlStatement, final SQLStatementBinderContext binderContext) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.infra.binder.engine.statement.ddl;
19+
20+
import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString;
21+
import com.google.common.collect.LinkedHashMultimap;
22+
import com.google.common.collect.Multimap;
23+
import lombok.SneakyThrows;
24+
import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.TableSegmentBinderContext;
25+
import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.type.SimpleTableSegmentBinder;
26+
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinder;
27+
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
28+
import org.apache.shardingsphere.infra.binder.engine.statement.dml.SelectStatementBinder;
29+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterViewStatement;
30+
31+
/**
32+
* Alter view statement binder.
33+
*/
34+
public final class AlterViewStatementBinder implements SQLStatementBinder<AlterViewStatement> {
35+
36+
@Override
37+
public AlterViewStatement bind(final AlterViewStatement sqlStatement, final SQLStatementBinderContext binderContext) {
38+
AlterViewStatement result = copy(sqlStatement);
39+
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create();
40+
result.setView(SimpleTableSegmentBinder.bind(sqlStatement.getView(), binderContext, tableBinderContexts));
41+
sqlStatement.getSelect().ifPresent(optional -> result.setSelect(new SelectStatementBinder().bind(optional, binderContext)));
42+
sqlStatement.getRenameView().ifPresent(optional -> result.setRenameView(SimpleTableSegmentBinder.bind(optional, binderContext, tableBinderContexts)));
43+
return result;
44+
}
45+
46+
@SneakyThrows(ReflectiveOperationException.class)
47+
private static AlterViewStatement copy(final AlterViewStatement sqlStatement) {
48+
AlterViewStatement result = sqlStatement.getClass().getDeclaredConstructor().newInstance();
49+
sqlStatement.getViewDefinition().ifPresent(result::setViewDefinition);
50+
sqlStatement.getConstraintDefinition().ifPresent(result::setConstraintDefinition);
51+
result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments());
52+
result.getCommentSegments().addAll(sqlStatement.getCommentSegments());
53+
result.getVariableNames().addAll(sqlStatement.getVariableNames());
54+
return result;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.infra.binder.engine.statement.ddl;
19+
20+
import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString;
21+
import com.google.common.collect.LinkedHashMultimap;
22+
import com.google.common.collect.Multimap;
23+
import lombok.SneakyThrows;
24+
import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.TableSegmentBinderContext;
25+
import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.type.SimpleTableSegmentBinder;
26+
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinder;
27+
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
28+
import org.apache.shardingsphere.infra.binder.engine.statement.dml.SelectStatementBinder;
29+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateViewStatement;
30+
31+
/**
32+
* Create view statement binder.
33+
*/
34+
public final class CreateViewStatementBinder implements SQLStatementBinder<CreateViewStatement> {
35+
36+
@Override
37+
public CreateViewStatement bind(final CreateViewStatement sqlStatement, final SQLStatementBinderContext binderContext) {
38+
CreateViewStatement result = copy(sqlStatement);
39+
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create();
40+
result.setView(SimpleTableSegmentBinder.bind(sqlStatement.getView(), binderContext, tableBinderContexts));
41+
result.setSelect(new SelectStatementBinder().bind(sqlStatement.getSelect(), binderContext));
42+
return result;
43+
}
44+
45+
@SneakyThrows(ReflectiveOperationException.class)
46+
private static CreateViewStatement copy(final CreateViewStatement sqlStatement) {
47+
CreateViewStatement result = sqlStatement.getClass().getDeclaredConstructor().newInstance();
48+
result.setViewDefinition(sqlStatement.getViewDefinition());
49+
result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments());
50+
result.getCommentSegments().addAll(sqlStatement.getCommentSegments());
51+
result.getVariableNames().addAll(sqlStatement.getVariableNames());
52+
return result;
53+
}
54+
}

infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/DropIndexStatementBinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/**
3131
* Drop index statement binder.
3232
*/
33-
public class DropIndexStatementBinder implements SQLStatementBinder<DropIndexStatement> {
33+
public final class DropIndexStatementBinder implements SQLStatementBinder<DropIndexStatement> {
3434

3535
@Override
3636
public DropIndexStatement bind(final DropIndexStatement sqlStatement, final SQLStatementBinderContext binderContext) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.infra.binder.engine.statement.ddl;
19+
20+
import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString;
21+
import com.google.common.collect.LinkedHashMultimap;
22+
import com.google.common.collect.Multimap;
23+
import lombok.SneakyThrows;
24+
import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.TableSegmentBinderContext;
25+
import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.type.SimpleTableSegmentBinder;
26+
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinder;
27+
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
28+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropViewStatement;
29+
30+
/**
31+
* Drop view statement binder.
32+
*/
33+
public final class DropViewStatementBinder implements SQLStatementBinder<DropViewStatement> {
34+
35+
@Override
36+
public DropViewStatement bind(final DropViewStatement sqlStatement, final SQLStatementBinderContext binderContext) {
37+
DropViewStatement result = copy(sqlStatement);
38+
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create();
39+
sqlStatement.getViews().forEach(each -> result.getViews().add(SimpleTableSegmentBinder.bind(each, binderContext, tableBinderContexts)));
40+
return result;
41+
}
42+
43+
@SneakyThrows(ReflectiveOperationException.class)
44+
private static DropViewStatement copy(final DropViewStatement sqlStatement) {
45+
DropViewStatement result = sqlStatement.getClass().getDeclaredConstructor().newInstance();
46+
result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments());
47+
result.getCommentSegments().addAll(sqlStatement.getCommentSegments());
48+
result.getVariableNames().addAll(sqlStatement.getVariableNames());
49+
return result;
50+
}
51+
}

0 commit comments

Comments
 (0)