You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2025/06/13 23:33:13 UTC

(shardingsphere) branch master updated: Merge CreateProcedureStatementContext and TableAvailableSQLStatementContext (#35703)

This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 320dd2c067c Merge CreateProcedureStatementContext and TableAvailableSQLStatementContext (#35703)
320dd2c067c is described below

commit 320dd2c067ca2bc962acc87ee04a97ed10b4797b
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Jun 14 07:33:08 2025 +0800

    Merge CreateProcedureStatementContext and TableAvailableSQLStatementContext (#35703)
---
 .../ShardingCreateProcedureSupportedChecker.java   | 10 ++---
 ...hardingCreateProcedureSupportedCheckerTest.java | 26 ++++++-----
 .../statement/SQLStatementContextFactory.java      |  5 ++-
 .../type/ddl/CreateProcedureStatementContext.java  | 52 ----------------------
 .../ddl/CreateProcedureStatementContextTest.java   | 35 ---------------
 5 files changed, 24 insertions(+), 104 deletions(-)

diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedChecker.java
index d07ce6c6d8b..7283abc214f 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedChecker.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedChecker.java
@@ -18,7 +18,7 @@
 package org.apache.shardingsphere.sharding.checker.sql.ddl;
 
 import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CreateProcedureStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.TableAvailableSQLStatementContext;
 import org.apache.shardingsphere.infra.checker.SupportedSQLChecker;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
@@ -35,16 +35,16 @@ import java.util.Optional;
 /**
  * Create procedure supported checker for sharding.
  */
-public final class ShardingCreateProcedureSupportedChecker implements SupportedSQLChecker<CreateProcedureStatementContext, ShardingRule> {
+public final class ShardingCreateProcedureSupportedChecker implements SupportedSQLChecker<TableAvailableSQLStatementContext, ShardingRule> {
     
     @Override
     public boolean isCheck(final SQLStatementContext sqlStatementContext) {
-        return sqlStatementContext instanceof CreateProcedureStatementContext;
+        return sqlStatementContext.getSqlStatement() instanceof CreateProcedureStatement;
     }
     
     @Override
-    public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final CreateProcedureStatementContext sqlStatementContext) {
-        CreateProcedureStatement createProcedureStatement = sqlStatementContext.getSqlStatement();
+    public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final TableAvailableSQLStatementContext sqlStatementContext) {
+        CreateProcedureStatement createProcedureStatement = (CreateProcedureStatement) sqlStatementContext.getSqlStatement();
         Optional<RoutineBodySegment> routineBodySegment = createProcedureStatement.getRoutineBody();
         if (!routineBodySegment.isPresent()) {
             return;
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedCheckerTest.java
index 6c466c80f32..e274518d6c6 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedCheckerTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedCheckerTest.java
@@ -17,7 +17,7 @@
 
 package org.apache.shardingsphere.sharding.checker.sql.ddl;
 
-import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CreateProcedureStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.TableAvailableSQLStatementContext;
 import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
 import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -37,6 +37,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 
+import java.util.Arrays;
 import java.util.Optional;
 
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -54,9 +55,11 @@ class ShardingCreateProcedureSupportedCheckerTest {
     @Test
     void assertCheck() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("bar_tbl"))));
+        SimpleTableSegment fromTable = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("bar_tbl")));
+        selectStatement.setFrom(fromTable);
         CreateTableStatement createTableStatement = mock(CreateTableStatement.class);
-        when(createTableStatement.getTable()).thenReturn(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl"))));
+        SimpleTableSegment table = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")));
+        when(createTableStatement.getTable()).thenReturn(table);
         ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
         validStatementSegment.setSqlStatement(createTableStatement);
         ValidStatementSegment selectValidStatementSegment = new ValidStatementSegment(0, 0);
@@ -70,21 +73,22 @@ class ShardingCreateProcedureSupportedCheckerTest {
         ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
         when(schema.containsTable("bar_tbl")).thenReturn(true);
         when(rule.isShardingTable("bar_tbl")).thenReturn(false);
-        CreateProcedureStatementContext sqlStatementContext = new CreateProcedureStatementContext(mock(), sqlStatement);
+        TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, Arrays.asList(fromTable, table));
         assertDoesNotThrow(() -> new ShardingCreateProcedureSupportedChecker().check(rule, database, schema, sqlStatementContext));
     }
     
     @Test
     void assertCheckWithShardingTable() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl"))));
+        SimpleTableSegment fromTable = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")));
+        selectStatement.setFrom(fromTable);
         ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
         validStatementSegment.setSqlStatement(selectStatement);
         RoutineBodySegment routineBody = new RoutineBodySegment(0, 0);
         routineBody.getValidStatements().add(validStatementSegment);
         CreateProcedureStatement sqlStatement = new CreateProcedureStatement();
         sqlStatement.setRoutineBody(routineBody);
-        CreateProcedureStatementContext sqlStatementContext = new CreateProcedureStatementContext(mock(), sqlStatement);
+        TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, fromTable);
         ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
         when(rule.isShardingTable("foo_tbl")).thenReturn(true);
         assertThrows(UnsupportedShardingOperationException.class, () -> new ShardingCreateProcedureSupportedChecker().check(rule, database, mock(), sqlStatementContext));
@@ -93,14 +97,15 @@ class ShardingCreateProcedureSupportedCheckerTest {
     @Test
     void assertCheckWithNoSuchTable() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl"))));
+        SimpleTableSegment fromTable = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")));
+        selectStatement.setFrom(fromTable);
         ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
         validStatementSegment.setSqlStatement(selectStatement);
         RoutineBodySegment routineBody = new RoutineBodySegment(0, 0);
         routineBody.getValidStatements().add(validStatementSegment);
         CreateProcedureStatement sqlStatement = mock(CreateProcedureStatement.class);
         when(sqlStatement.getRoutineBody()).thenReturn(Optional.of(routineBody));
-        CreateProcedureStatementContext sqlStatementContext = new CreateProcedureStatementContext(mock(), sqlStatement);
+        TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, fromTable);
         ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
         assertThrows(NoSuchTableException.class, () -> new ShardingCreateProcedureSupportedChecker().check(rule, database, mock(), sqlStatementContext));
     }
@@ -108,14 +113,15 @@ class ShardingCreateProcedureSupportedCheckerTest {
     @Test
     void assertCheckWithTableExists() {
         CreateTableStatement createTableStatement = new CreateTableStatement();
-        createTableStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl"))));
+        SimpleTableSegment fromTable = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")));
+        createTableStatement.setTable(fromTable);
         ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
         validStatementSegment.setSqlStatement(createTableStatement);
         RoutineBodySegment routineBody = new RoutineBodySegment(0, 0);
         routineBody.getValidStatements().add(validStatementSegment);
         CreateProcedureStatement sqlStatement = mock(CreateProcedureStatement.class);
         when(sqlStatement.getRoutineBody()).thenReturn(Optional.of(routineBody));
-        CreateProcedureStatementContext sqlStatementContext = new CreateProcedureStatementContext(mock(), sqlStatement);
+        TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, fromTable);
         ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
         ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
         when(schema.containsTable("foo_tbl")).thenReturn(true);
diff --git a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactory.java b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactory.java
index 8ba016d86c3..4d632d29a67 100644
--- a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactory.java
+++ b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactory.java
@@ -30,7 +30,6 @@ import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.AlterTa
 import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.AlterViewStatementContext;
 import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CloseStatementContext;
 import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CreateIndexStatementContext;
-import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CreateProcedureStatementContext;
 import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CreateTableStatementContext;
 import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CreateViewStatementContext;
 import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CursorStatementContext;
@@ -48,6 +47,7 @@ import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
 import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
 import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.AnalyzeTableStatement;
@@ -200,7 +200,8 @@ public final class SQLStatementContextFactory {
             return new TableAvailableSQLStatementContext(databaseType, sqlStatement, ((CreateFunctionStatement) sqlStatement).getTables());
         }
         if (sqlStatement instanceof CreateProcedureStatement) {
-            return new CreateProcedureStatementContext(databaseType, (CreateProcedureStatement) sqlStatement);
+            return new TableAvailableSQLStatementContext(databaseType, sqlStatement,
+                    ((CreateProcedureStatement) sqlStatement).getRoutineBody().map(optional -> new TableExtractor().extractExistTableFromRoutineBody(optional)).orElseGet(Collections::emptyList));
         }
         if (sqlStatement instanceof CreateViewStatement) {
             return new CreateViewStatementContext(metaData, databaseType, params, (CreateViewStatement) sqlStatement, currentDatabaseName);
diff --git a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateProcedureStatementContext.java b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateProcedureStatementContext.java
deleted file mode 100644
index c7bcbc71ed0..00000000000
--- a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateProcedureStatementContext.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * 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.context.statement.type.ddl;
-
-import lombok.Getter;
-import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
-import org.apache.shardingsphere.infra.binder.context.statement.CommonSQLStatementContext;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateProcedureStatement;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Optional;
-
-/**
- * Create procedure statement context.
- */
-@Getter
-public final class CreateProcedureStatementContext extends CommonSQLStatementContext {
-    
-    private final TablesContext tablesContext;
-    
-    public CreateProcedureStatementContext(final DatabaseType databaseType, final CreateProcedureStatement sqlStatement) {
-        super(databaseType, sqlStatement);
-        Optional<RoutineBodySegment> routineBodySegment = sqlStatement.getRoutineBody();
-        Collection<SimpleTableSegment> tables = routineBodySegment.map(optional -> new TableExtractor().extractExistTableFromRoutineBody(optional)).orElseGet(Collections::emptyList);
-        tablesContext = new TablesContext(tables);
-    }
-    
-    @Override
-    public CreateProcedureStatement getSqlStatement() {
-        return (CreateProcedureStatement) super.getSqlStatement();
-    }
-}
diff --git a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateProcedureStatementContextTest.java b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateProcedureStatementContextTest.java
deleted file mode 100644
index a29961e7c84..00000000000
--- a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateProcedureStatementContextTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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.context.statement.type.ddl;
-
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateProcedureStatement;
-import org.junit.jupiter.api.Test;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-
-class CreateProcedureStatementContextTest {
-    
-    @Test
-    void assertNewInstance() {
-        CreateProcedureStatement sqlStatement = mock(CreateProcedureStatement.class);
-        CreateProcedureStatementContext actual = new CreateProcedureStatementContext(mock(), sqlStatement);
-        assertThat(actual.getSqlStatement(), is(sqlStatement));
-    }
-}