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 02b4eaa033e Merge LoadDataStatementContext and TableAvailableSQLStatementContext (#35706) 02b4eaa033e is described below commit 02b4eaa033e629cf58a2b04151955c8eab185c22 Author: Liang Zhang <zhangli...@apache.org> AuthorDate: Sat Jun 14 14:23:59 2025 +0800 Merge LoadDataStatementContext and TableAvailableSQLStatementContext (#35706) --- .../sql/dml/ShardingLoadDataSupportedChecker.java | 11 +++--- .../dml/ShardingLoadDataSupportedCheckerTest.java | 13 ++++--- .../statement/SQLStatementContextFactory.java | 5 --- .../type/dml/LoadDataStatementContext.java | 44 ---------------------- .../binder/mysql/MySQLSQLStatementExtractor.java | 6 ++- 5 files changed, 19 insertions(+), 60 deletions(-) diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedChecker.java index e7dd586dd4d..ee3d775b244 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedChecker.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedChecker.java @@ -19,28 +19,29 @@ package org.apache.shardingsphere.sharding.checker.sql.dml; import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; -import org.apache.shardingsphere.infra.binder.context.statement.type.dml.LoadDataStatementContext; +import org.apache.shardingsphere.infra.binder.context.statement.TableAvailableSQLStatementContext; import org.apache.shardingsphere.infra.checker.SupportedSQLChecker; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.LoadDataStatement; /** * Load data supported checker for sharding. */ @HighFrequencyInvocation -public final class ShardingLoadDataSupportedChecker implements SupportedSQLChecker<LoadDataStatementContext, ShardingRule> { +public final class ShardingLoadDataSupportedChecker implements SupportedSQLChecker<TableAvailableSQLStatementContext, ShardingRule> { @Override public boolean isCheck(final SQLStatementContext sqlStatementContext) { - return sqlStatementContext instanceof LoadDataStatementContext; + return sqlStatementContext.getSqlStatement() instanceof LoadDataStatement; } @Override - public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final LoadDataStatementContext sqlStatementContext) { - String tableName = sqlStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue(); + public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final TableAvailableSQLStatementContext sqlStatementContext) { + String tableName = ((LoadDataStatement) sqlStatementContext.getSqlStatement()).getTable().getTableName().getIdentifier().getValue(); ShardingSpherePreconditions.checkState(!rule.isShardingTable(tableName), () -> new UnsupportedShardingOperationException("LOAD DATA", tableName)); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java index d1b5729440f..9715a64686b 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.sharding.checker.sql.dml; -import org.apache.shardingsphere.infra.binder.context.statement.type.dml.LoadDataStatementContext; +import org.apache.shardingsphere.infra.binder.context.statement.TableAvailableSQLStatementContext; import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException; import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; @@ -43,15 +43,18 @@ class ShardingLoadDataSupportedCheckerTest { @Test void assertCheckWithSingleTable() { LoadDataStatement sqlStatement = mock(LoadDataStatement.class); - when(sqlStatement.getTable()).thenReturn(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")))); - assertDoesNotThrow(() -> new ShardingLoadDataSupportedChecker().check(rule, mock(), mock(), new LoadDataStatementContext(mock(), sqlStatement))); + SimpleTableSegment table = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl"))); + when(sqlStatement.getTable()).thenReturn(table); + assertDoesNotThrow(() -> new ShardingLoadDataSupportedChecker().check(rule, mock(), mock(), new TableAvailableSQLStatementContext(mock(), sqlStatement, table))); } @Test void assertCheckWithShardingTable() { LoadDataStatement sqlStatement = mock(LoadDataStatement.class); - when(sqlStatement.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(sqlStatement.getTable()).thenReturn(table); when(rule.isShardingTable("foo_tbl")).thenReturn(true); - assertThrows(UnsupportedShardingOperationException.class, () -> new ShardingLoadDataSupportedChecker().check(rule, mock(), mock(), new LoadDataStatementContext(mock(), sqlStatement))); + assertThrows(UnsupportedShardingOperationException.class, + () -> new ShardingLoadDataSupportedChecker().check(rule, mock(), mock(), new TableAvailableSQLStatementContext(mock(), sqlStatement, table))); } } 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 a9196cf323d..669e3a63615 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 @@ -39,7 +39,6 @@ import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.FetchSt import org.apache.shardingsphere.infra.binder.context.statement.type.ddl.MoveStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.type.dml.DeleteStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.type.dml.InsertStatementContext; -import org.apache.shardingsphere.infra.binder.context.statement.type.dml.LoadDataStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.type.dml.LoadXMLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.type.dml.SelectStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.type.dml.UpdateStatementContext; @@ -85,7 +84,6 @@ import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DMLStat import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DoStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.LoadDataStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.LoadXMLStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.MergeStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement; @@ -151,9 +149,6 @@ public final class SQLStatementContextFactory { if (sqlStatement instanceof InsertStatement) { return new InsertStatementContext(databaseType, (InsertStatement) sqlStatement, params, metaData, currentDatabaseName); } - if (sqlStatement instanceof LoadDataStatement) { - return new LoadDataStatementContext(databaseType, (LoadDataStatement) sqlStatement); - } if (sqlStatement instanceof LoadXMLStatement) { return new LoadXMLStatementContext(databaseType, (LoadXMLStatement) sqlStatement); } diff --git a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/dml/LoadDataStatementContext.java b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/dml/LoadDataStatementContext.java deleted file mode 100644 index e82b64ae679..00000000000 --- a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/dml/LoadDataStatementContext.java +++ /dev/null @@ -1,44 +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.dml; - -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.binder.context.type.TableAvailable; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.LoadDataStatement; - -/** - * Load data statement context. - */ -@Getter -public final class LoadDataStatementContext extends CommonSQLStatementContext implements TableAvailable { - - private final TablesContext tablesContext; - - public LoadDataStatementContext(final DatabaseType databaseType, final LoadDataStatement sqlStatement) { - super(databaseType, sqlStatement); - tablesContext = new TablesContext(sqlStatement.getTable()); - } - - @Override - public LoadDataStatement getSqlStatement() { - return (LoadDataStatement) super.getSqlStatement(); - } -} diff --git a/infra/binder/dialect/mysql/src/main/java/org/apache/shardingsphere/infra/binder/mysql/MySQLSQLStatementExtractor.java b/infra/binder/dialect/mysql/src/main/java/org/apache/shardingsphere/infra/binder/mysql/MySQLSQLStatementExtractor.java index 68ae317f646..f4d7c809807 100644 --- a/infra/binder/dialect/mysql/src/main/java/org/apache/shardingsphere/infra/binder/mysql/MySQLSQLStatementExtractor.java +++ b/infra/binder/dialect/mysql/src/main/java/org/apache/shardingsphere/infra/binder/mysql/MySQLSQLStatementExtractor.java @@ -20,9 +20,10 @@ package org.apache.shardingsphere.infra.binder.mysql; import org.apache.shardingsphere.infra.binder.context.extractor.DialectSQLStatementExtractor; 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.mysql.dal.MySQLDescribeStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.OptimizeTableStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ShowCreateTableStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.LoadDataStatement; +import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLDescribeStatement; import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLFlushStatement; import java.util.Collection; @@ -47,6 +48,9 @@ public final class MySQLSQLStatementExtractor implements DialectSQLStatementExtr if (sqlStatement instanceof MySQLDescribeStatement) { return Collections.singletonList(((MySQLDescribeStatement) sqlStatement).getTable()); } + if (sqlStatement instanceof LoadDataStatement) { + return Collections.singletonList(((LoadDataStatement) sqlStatement).getTable()); + } return Collections.emptyList(); }