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 2ddab2d487a Merge CreateFunctionStatementContext and
TableAvailableSQLStatementContext (#35702)
2ddab2d487a is described below
commit 2ddab2d487ae7c99c52882f83a96269ae5c2af8b
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Jun 13 20:48:44 2025 +0800
Merge CreateFunctionStatementContext and TableAvailableSQLStatementContext
(#35702)
* Merge CreateFunctionStatementContext and TableAvailableSQLStatementContext
* Merge CreateFunctionStatementContext and TableAvailableSQLStatementContext
---
.../ShardingCreateFunctionSupportedChecker.java | 10 ++---
...ShardingCreateFunctionSupportedCheckerTest.java | 22 +++++----
.../statement/SQLStatementContextFactory.java | 3 +-
.../type/ddl/CreateFunctionStatementContext.java | 52 ----------------------
.../ddl/CreateFunctionStatementContextTest.java | 35 ---------------
.../statement/ddl/CreateFunctionStatement.java | 13 ++++++
6 files changed, 32 insertions(+), 103 deletions(-)
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedChecker.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedChecker.java
index 7255d1d5688..dd7d763258f 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedChecker.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedChecker.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.CreateFunctionStatementContext;
+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 function supported checker for sharding.
*/
-public final class ShardingCreateFunctionSupportedChecker implements
SupportedSQLChecker<CreateFunctionStatementContext, ShardingRule> {
+public final class ShardingCreateFunctionSupportedChecker implements
SupportedSQLChecker<TableAvailableSQLStatementContext, ShardingRule> {
@Override
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext instanceof CreateFunctionStatementContext;
+ return sqlStatementContext.getSqlStatement() instanceof
CreateFunctionStatement;
}
@Override
- public void check(final ShardingRule rule, final ShardingSphereDatabase
database, final ShardingSphereSchema currentSchema, final
CreateFunctionStatementContext sqlStatementContext) {
- CreateFunctionStatement createFunctionStatement =
sqlStatementContext.getSqlStatement();
+ public void check(final ShardingRule rule, final ShardingSphereDatabase
database, final ShardingSphereSchema currentSchema, final
TableAvailableSQLStatementContext sqlStatementContext) {
+ CreateFunctionStatement createFunctionStatement =
(CreateFunctionStatement) sqlStatementContext.getSqlStatement();
Optional<RoutineBodySegment> routineBodySegment =
createFunctionStatement.getRoutineBody();
if (!routineBodySegment.isPresent()) {
return;
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java
index 77f8e3e10fc..cd75e5116ad 100644
---
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.sharding.checker.sql.ddl;
-import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CreateFunctionStatementContext;
+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;
@@ -54,7 +54,8 @@ class ShardingCreateFunctionSupportedCheckerTest {
@Test
void assertCheckCreateFunction() {
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"))));
ValidStatementSegment validStatementSegment = new
ValidStatementSegment(0, 0);
@@ -66,7 +67,7 @@ class ShardingCreateFunctionSupportedCheckerTest {
routineBody.getValidStatements().add(selectValidStatementSegment);
CreateFunctionStatement sqlStatement =
mock(CreateFunctionStatement.class);
when(sqlStatement.getRoutineBody()).thenReturn(Optional.of(routineBody));
- CreateFunctionStatementContext sqlStatementContext = new
CreateFunctionStatementContext(mock(), sqlStatement);
+ TableAvailableSQLStatementContext sqlStatementContext = new
TableAvailableSQLStatementContext(mock(), sqlStatement, fromTable);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.containsTable("bar_tbl")).thenReturn(true);
@@ -76,14 +77,15 @@ class ShardingCreateFunctionSupportedCheckerTest {
@Test
void assertCheckCreateFunctionWithShardingTable() {
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);
CreateFunctionStatement sqlStatement =
mock(CreateFunctionStatement.class);
when(sqlStatement.getRoutineBody()).thenReturn(Optional.of(routineBody));
- CreateFunctionStatementContext sqlStatementContext = new
CreateFunctionStatementContext(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
ShardingCreateFunctionSupportedChecker().check(rule, database, mock(),
sqlStatementContext));
@@ -92,14 +94,15 @@ class ShardingCreateFunctionSupportedCheckerTest {
@Test
void assertCheckCreateFunctionWithNoSuchTable() {
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);
CreateFunctionStatement sqlStatement =
mock(CreateFunctionStatement.class);
when(sqlStatement.getRoutineBody()).thenReturn(Optional.of(routineBody));
- CreateFunctionStatementContext sqlStatementContext = new
CreateFunctionStatementContext(mock(), sqlStatement);
+ TableAvailableSQLStatementContext sqlStatementContext = new
TableAvailableSQLStatementContext(mock(), sqlStatement, fromTable);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
assertThrows(NoSuchTableException.class, () -> new
ShardingCreateFunctionSupportedChecker().check(rule, database, mock(),
sqlStatementContext));
}
@@ -107,14 +110,15 @@ class ShardingCreateFunctionSupportedCheckerTest {
@Test
void assertCheckCreateFunctionWithTableExists() {
CreateTableStatement createTableStatement = new CreateTableStatement();
- createTableStatement.setTable(new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("foo_tbl"))));
+ SimpleTableSegment table = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("foo_tbl")));
+ createTableStatement.setTable(table);
ValidStatementSegment validStatementSegment = new
ValidStatementSegment(0, 0);
validStatementSegment.setSqlStatement(createTableStatement);
RoutineBodySegment routineBody = new RoutineBodySegment(0, 0);
routineBody.getValidStatements().add(validStatementSegment);
CreateFunctionStatement sqlStatement =
mock(CreateFunctionStatement.class);
when(sqlStatement.getRoutineBody()).thenReturn(Optional.of(routineBody));
- CreateFunctionStatementContext sqlStatementContext = new
CreateFunctionStatementContext(mock(), sqlStatement);
+ TableAvailableSQLStatementContext sqlStatementContext = new
TableAvailableSQLStatementContext(mock(), sqlStatement, table);
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 c8a40bab4af..8ba016d86c3 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
@@ -29,7 +29,6 @@ import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.AlterIn
import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.AlterTableStatementContext;
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.CreateFunctionStatementContext;
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;
@@ -198,7 +197,7 @@ public final class SQLStatementContextFactory {
return new TableAvailableSQLStatementContext(databaseType,
sqlStatement, ((TruncateStatement) sqlStatement).getTables());
}
if (sqlStatement instanceof CreateFunctionStatement) {
- return new CreateFunctionStatementContext(databaseType,
(CreateFunctionStatement) sqlStatement);
+ return new TableAvailableSQLStatementContext(databaseType,
sqlStatement, ((CreateFunctionStatement) sqlStatement).getTables());
}
if (sqlStatement instanceof CreateProcedureStatement) {
return new CreateProcedureStatementContext(databaseType,
(CreateProcedureStatement) sqlStatement);
diff --git
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateFunctionStatementContext.java
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateFunctionStatementContext.java
deleted file mode 100644
index 6734c9e3cf3..00000000000
---
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateFunctionStatementContext.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.CreateFunctionStatement;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Optional;
-
-/**
- * Create function statement context.
- */
-@Getter
-public final class CreateFunctionStatementContext extends
CommonSQLStatementContext {
-
- private final TablesContext tablesContext;
-
- public CreateFunctionStatementContext(final DatabaseType databaseType,
final CreateFunctionStatement 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 CreateFunctionStatement getSqlStatement() {
- return (CreateFunctionStatement) super.getSqlStatement();
- }
-}
diff --git
a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateFunctionStatementContextTest.java
b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateFunctionStatementContextTest.java
deleted file mode 100644
index 73b4ca7c415..00000000000
---
a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/CreateFunctionStatementContextTest.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.CreateFunctionStatement;
-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 CreateFunctionStatementContextTest {
-
- @Test
- void assertMySQLNewInstance() {
- CreateFunctionStatement sqlStatement =
mock(CreateFunctionStatement.class);
- CreateFunctionStatementContext actual = new
CreateFunctionStatementContext(mock(), sqlStatement);
- assertThat(actual.getSqlStatement(), is(sqlStatement));
- }
-}
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/CreateFunctionStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/CreateFunctionStatement.java
index f4686386866..ada403f1819 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/CreateFunctionStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/CreateFunctionStatement.java
@@ -19,12 +19,16 @@ package
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl;
import lombok.Getter;
import lombok.Setter;
+import
org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.FunctionNameSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import java.util.Optional;
@@ -58,4 +62,13 @@ public class CreateFunctionStatement extends
AbstractSQLStatement implements DDL
public Optional<RoutineBodySegment> getRoutineBody() {
return Optional.ofNullable(routineBody);
}
+
+ /**
+ * Get tables.
+ *
+ * @return tables
+ */
+ public Collection<SimpleTableSegment> getTables() {
+ return getRoutineBody().map(optional -> new
TableExtractor().extractExistTableFromRoutineBody(optional)).orElseGet(Collections::emptyList);
+ }
}