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 a06037336d9 Merge to dd2b6da (#35775)
a06037336d9 is described below
commit a06037336d930efe0fa278db5417bc03428d5fd0
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jun 21 16:45:13 2025 +0800
Merge to dd2b6da (#35775)
* Add ConstraintSQLStatementAttribute
* Add ConstraintSQLStatementAttribute
* Add ConstraintSQLStatementAttribute
* Add ConstraintSQLStatementAttribute
---
.../impl/ShardingConstraintTokenGenerator.java | 17 +++++------
.../token/ShardingTokenGenerateBuilderTest.java | 2 ++
.../impl/ShardingConstraintTokenGeneratorTest.java | 35 +++++++++++-----------
.../metadata/generator/PipelineDDLDecorator.java | 14 ++++-----
.../type/ConstraintSQLStatementAttribute.java} | 7 +++--
.../core/statement/ddl/AlterTableStatement.java | 27 +++++++++++------
.../core/statement/ddl/CreateTableStatement.java | 23 +++++++++-----
7 files changed, 72 insertions(+), 53 deletions(-)
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingConstraintTokenGenerator.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingConstraintTokenGenerator.java
index a2e4dc32406..b6587284b75 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingConstraintTokenGenerator.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingConstraintTokenGenerator.java
@@ -20,12 +20,12 @@ package
org.apache.shardingsphere.sharding.rewrite.token.generator.impl;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.available.ConstraintAvailableSQLStatement;
import
org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.CollectionSQLTokenGenerator;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.SQLToken;
import org.apache.shardingsphere.sharding.rewrite.token.pojo.ConstraintToken;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.constraint.ConstraintSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.ConstraintSQLStatementAttribute;
import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import java.util.Collection;
@@ -42,20 +42,17 @@ public final class ShardingConstraintTokenGenerator
implements CollectionSQLToke
@Override
public boolean isGenerateSQLToken(final SQLStatementContext
sqlStatementContext) {
- return sqlStatementContext.getSqlStatement() instanceof
ConstraintAvailableSQLStatement
- && !((ConstraintAvailableSQLStatement)
sqlStatementContext.getSqlStatement()).getConstraints().isEmpty();
+ return
sqlStatementContext.getSqlStatement().getAttributes().findAttribute(ConstraintSQLStatementAttribute.class).map(optional
-> !optional.getConstraints().isEmpty()).orElse(false);
}
@Override
public Collection<SQLToken> generateSQLTokens(final SQLStatementContext
sqlStatementContext) {
Collection<SQLToken> result = new LinkedList<>();
- if (sqlStatementContext.getSqlStatement() instanceof
ConstraintAvailableSQLStatement) {
- for (ConstraintSegment each : ((ConstraintAvailableSQLStatement)
sqlStatementContext.getSqlStatement()).getConstraints()) {
- IdentifierValue constraintIdentifier = each.getIdentifier();
- // TODO make sure can remove null check here? @duanzhengqiang
- if (null != constraintIdentifier) {
- result.add(new ConstraintToken(each.getStartIndex(),
each.getStopIndex(), constraintIdentifier, sqlStatementContext, rule));
- }
+ for (ConstraintSegment each :
sqlStatementContext.getSqlStatement().getAttributes().getAttribute(ConstraintSQLStatementAttribute.class).getConstraints())
{
+ IdentifierValue constraintIdentifier = each.getIdentifier();
+ // TODO make sure can remove null check here? @duanzhengqiang
+ if (null != constraintIdentifier) {
+ result.add(new ConstraintToken(each.getStartIndex(),
each.getStopIndex(), constraintIdentifier, sqlStatementContext, rule));
}
}
return result;
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingTokenGenerateBuilderTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingTokenGenerateBuilderTest.java
index 2448df85c53..f6f25c453c0 100644
---
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingTokenGenerateBuilderTest.java
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingTokenGenerateBuilderTest.java
@@ -26,6 +26,7 @@ import
org.apache.shardingsphere.sharding.rewrite.token.generator.impl.ShardingD
import
org.apache.shardingsphere.sharding.rewrite.token.generator.impl.ShardingRemoveTokenGenerator;
import
org.apache.shardingsphere.sharding.rewrite.token.generator.impl.ShardingTableTokenGenerator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.internal.configuration.plugins.Plugins;
@@ -57,6 +58,7 @@ class ShardingTokenGenerateBuilderTest {
when(routeContext.containsTableSharding()).thenReturn(true);
SelectStatementContext sqlStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
when(sqlStatementContext.getProjectionsContext().getAggregationProjections().isEmpty()).thenReturn(false);
+
when(sqlStatementContext.getSqlStatement().getAttributes()).thenReturn(new
SQLStatementAttributes());
ShardingTokenGenerateBuilder shardingTokenGenerateBuilder = new
ShardingTokenGenerateBuilder(shardingRule, routeContext, sqlStatementContext);
Collection<SQLTokenGenerator> sqlTokenGenerators =
shardingTokenGenerateBuilder.getSQLTokenGenerators();
assertThat(sqlTokenGenerators.size(), is(4));
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingConstraintTokenGeneratorTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingConstraintTokenGeneratorTest.java
index e739df8c8f0..3032a3140e8 100644
---
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingConstraintTokenGeneratorTest.java
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingConstraintTokenGeneratorTest.java
@@ -23,6 +23,8 @@ import
org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.SQLToken;
import org.apache.shardingsphere.sharding.rewrite.token.pojo.ConstraintToken;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.constraint.ConstraintSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.ConstraintSQLStatementAttribute;
import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import org.junit.jupiter.api.Test;
@@ -42,39 +44,38 @@ class ShardingConstraintTokenGeneratorTest {
private final ShardingConstraintTokenGenerator generator = new
ShardingConstraintTokenGenerator(mock(ShardingRule.class));
@Test
- void assertIsNotGenerateSQLTokenWithNotConstraintAvailable() {
-
assertFalse(generator.isGenerateSQLToken(mock(SQLStatementContext.class)));
+ void assertIsNotGenerateSQLTokenWithoutConstraintSQLStatementAttribute() {
+ SQLStatementContext sqlStatementContext =
mock(SQLStatementContext.class, RETURNS_DEEP_STUBS);
+
when(sqlStatementContext.getSqlStatement().getAttributes()).thenReturn(new
SQLStatementAttributes());
+ assertFalse(generator.isGenerateSQLToken(sqlStatementContext));
}
@Test
void assertIsNotGenerateSQLTokenWithEmptyConstraint() {
- AlterTableStatementContext alterTableStatementContext =
mock(AlterTableStatementContext.class, RETURNS_DEEP_STUBS);
-
when(alterTableStatementContext.getSqlStatement().getConstraints().isEmpty()).thenReturn(true);
- assertFalse(generator.isGenerateSQLToken(alterTableStatementContext));
+ AlterTableStatementContext sqlStatementContext =
mock(AlterTableStatementContext.class, RETURNS_DEEP_STUBS);
+ ConstraintSQLStatementAttribute constraintAttribute =
mock(ConstraintSQLStatementAttribute.class);
+
when(constraintAttribute.getConstraints()).thenReturn(Collections.emptyList());
+
when(sqlStatementContext.getSqlStatement().getAttributes()).thenReturn(new
SQLStatementAttributes(constraintAttribute));
+ assertFalse(generator.isGenerateSQLToken(sqlStatementContext));
}
@Test
void assertIsGenerateSQLToken() {
- AlterTableStatementContext alterTableStatementContext =
mock(AlterTableStatementContext.class, RETURNS_DEEP_STUBS);
- assertTrue(generator.isGenerateSQLToken(alterTableStatementContext));
- }
-
- @Test
- void assertGenerateSQLTokensWithNotConstraintAvailable() {
- Collection<SQLToken> actual =
generator.generateSQLTokens(mock(SQLStatementContext.class));
- assertTrue(actual.isEmpty());
+ assertTrue(generator.isGenerateSQLToken(mockSQLStatementContext()));
}
@Test
void assertGenerateSQLTokens() {
- Collection<SQLToken> actual =
generator.generateSQLTokens(mockAlterTableStatementContext());
+ Collection<SQLToken> actual =
generator.generateSQLTokens(mockSQLStatementContext());
assertThat(actual.size(), is(1));
assertConstraintToken((ConstraintToken) actual.iterator().next());
}
- private AlterTableStatementContext mockAlterTableStatementContext() {
- AlterTableStatementContext result =
mock(AlterTableStatementContext.class, RETURNS_DEEP_STUBS);
-
when(result.getSqlStatement().getConstraints()).thenReturn(Collections.singleton(new
ConstraintSegment(1, 3, mock(IdentifierValue.class))));
+ private SQLStatementContext mockSQLStatementContext() {
+ SQLStatementContext result = mock(SQLStatementContext.class,
RETURNS_DEEP_STUBS);
+ ConstraintSQLStatementAttribute constraintAttribute =
mock(ConstraintSQLStatementAttribute.class);
+
when(constraintAttribute.getConstraints()).thenReturn(Collections.singleton(new
ConstraintSegment(1, 3, mock(IdentifierValue.class))));
+ when(result.getSqlStatement().getAttributes()).thenReturn(new
SQLStatementAttributes(constraintAttribute));
return result;
}
diff --git
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/generator/PipelineDDLDecorator.java
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/generator/PipelineDDLDecorator.java
index a2c4b311e55..d115d67dd9a 100644
---
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/generator/PipelineDDLDecorator.java
+++
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/generator/PipelineDDLDecorator.java
@@ -19,12 +19,11 @@ package
org.apache.shardingsphere.data.pipeline.core.metadata.generator;
import com.google.common.base.Strings;
import lombok.AllArgsConstructor;
+import
org.apache.shardingsphere.infra.binder.context.available.IndexContextAvailable;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.AlterTableStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CreateIndexStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.CreateTableStatementContext;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.available.ConstraintAvailableSQLStatement;
-import
org.apache.shardingsphere.infra.binder.context.available.IndexContextAvailable;
import org.apache.shardingsphere.infra.binder.engine.SQLBindEngine;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.hint.HintValueContext;
@@ -36,6 +35,8 @@ import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.constrain
import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexSegment;
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.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.ConstraintSQLStatementAttribute;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CommentStatement;
import java.util.Collections;
@@ -110,17 +111,16 @@ public final class PipelineDDLDecorator {
}
TableNameSegment tableNameSegment =
sqlStatementContext.getTablesContext().getSimpleTables().iterator().next().getTableName();
if
(!tableNameSegment.getIdentifier().getValue().equals(targetTableName)) {
+ SQLStatementAttributes attributes =
sqlStatementContext.getSqlStatement().getAttributes();
if (sqlStatementContext instanceof IndexContextAvailable) {
for (IndexSegment each : ((IndexContextAvailable)
sqlStatementContext).getIndexes()) {
String logicIndexName =
IndexMetaDataUtils.getLogicIndexName(each.getIndexName().getIdentifier().getValue(),
tableNameSegment.getIdentifier().getValue());
replaceMap.put(each.getIndexName(), logicIndexName);
}
}
- if (sqlStatementContext.getSqlStatement() instanceof
ConstraintAvailableSQLStatement) {
- for (ConstraintSegment each :
((ConstraintAvailableSQLStatement)
sqlStatementContext.getSqlStatement()).getConstraints()) {
- String logicConstraint =
IndexMetaDataUtils.getLogicIndexName(each.getIdentifier().getValue(),
tableNameSegment.getIdentifier().getValue());
- replaceMap.put(each, logicConstraint);
- }
+ for (ConstraintSegment each :
attributes.findAttribute(ConstraintSQLStatementAttribute.class).map(ConstraintSQLStatementAttribute::getConstraints).orElse(Collections.emptyList()))
{
+ String logicConstraint =
IndexMetaDataUtils.getLogicIndexName(each.getIdentifier().getValue(),
tableNameSegment.getIdentifier().getValue());
+ replaceMap.put(each, logicConstraint);
}
}
}
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/available/ConstraintAvailableSQLStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/type/ConstraintSQLStatementAttribute.java
similarity index 82%
rename from
parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/available/ConstraintAvailableSQLStatement.java
rename to
parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/type/ConstraintSQLStatementAttribute.java
index c2d65d591d5..e7131289888 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/available/ConstraintAvailableSQLStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/type/ConstraintSQLStatementAttribute.java
@@ -15,16 +15,17 @@
* limitations under the License.
*/
-package
org.apache.shardingsphere.sql.parser.statement.core.statement.available;
+package
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.constraint.ConstraintSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttribute;
import java.util.Collection;
/**
- * Constraint available SQL statement.
+ * Constraint SQL statement attribute.
*/
-public interface ConstraintAvailableSQLStatement {
+public interface ConstraintSQLStatementAttribute extends SQLStatementAttribute
{
/**
* Get constraint segments.
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/AlterTableStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/AlterTableStatement.java
index 1471c592e2b..7e87e0991b2 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/AlterTableStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/AlterTableStatement.java
@@ -37,7 +37,8 @@ import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.table.Con
import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.table.LockTableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.available.ConstraintAvailableSQLStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.ConstraintSQLStatementAttribute;
import java.util.Collection;
import java.util.LinkedList;
@@ -48,7 +49,7 @@ import java.util.Optional;
*/
@Getter
@Setter
-public final class AlterTableStatement extends AbstractSQLStatement implements
DDLStatement, ConstraintAvailableSQLStatement {
+public final class AlterTableStatement extends AbstractSQLStatement implements
DDLStatement {
private SimpleTableSegment table;
@@ -130,13 +131,21 @@ public final class AlterTableStatement extends
AbstractSQLStatement implements D
}
@Override
- public Collection<ConstraintSegment> getConstraints() {
- Collection<ConstraintSegment> result = new LinkedList<>();
- for (AddConstraintDefinitionSegment each : addConstraintDefinitions) {
-
each.getConstraintDefinition().getConstraintName().ifPresent(result::add);
+ public SQLStatementAttributes getAttributes() {
+ return new SQLStatementAttributes(new
CreateTableConstraintSQLStatementAttribute());
+ }
+
+ private class CreateTableConstraintSQLStatementAttribute implements
ConstraintSQLStatementAttribute {
+
+ @Override
+ public Collection<ConstraintSegment> getConstraints() {
+ Collection<ConstraintSegment> result = new LinkedList<>();
+ for (AddConstraintDefinitionSegment each :
addConstraintDefinitions) {
+
each.getConstraintDefinition().getConstraintName().ifPresent(result::add);
+ }
+
validateConstraintDefinitions.stream().map(ValidateConstraintDefinitionSegment::getConstraintName).forEach(result::add);
+
dropConstraintDefinitions.stream().map(DropConstraintDefinitionSegment::getConstraintName).forEach(result::add);
+ return result;
}
-
validateConstraintDefinitions.stream().map(ValidateConstraintDefinitionSegment::getConstraintName).forEach(result::add);
-
dropConstraintDefinitions.stream().map(DropConstraintDefinitionSegment::getConstraintName).forEach(result::add);
- return result;
}
}
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/CreateTableStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/CreateTableStatement.java
index b7700c2d71d..6d84c8ce4c9 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/CreateTableStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/CreateTableStatement.java
@@ -26,7 +26,8 @@ import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.table.Cre
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.available.ConstraintAvailableSQLStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.ConstraintSQLStatementAttribute;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
import java.util.Collection;
@@ -39,7 +40,7 @@ import java.util.Optional;
*/
@Getter
@Setter
-public final class CreateTableStatement extends AbstractSQLStatement
implements DDLStatement, ConstraintAvailableSQLStatement {
+public final class CreateTableStatement extends AbstractSQLStatement
implements DDLStatement {
private SimpleTableSegment table;
@@ -85,11 +86,19 @@ public final class CreateTableStatement extends
AbstractSQLStatement implements
}
@Override
- public Collection<ConstraintSegment> getConstraints() {
- Collection<ConstraintSegment> result = new LinkedList<>();
- for (ConstraintDefinitionSegment each : constraintDefinitions) {
- each.getConstraintName().ifPresent(result::add);
+ public SQLStatementAttributes getAttributes() {
+ return new SQLStatementAttributes(new
CreateTableConstraintSQLStatementAttribute());
+ }
+
+ private class CreateTableConstraintSQLStatementAttribute implements
ConstraintSQLStatementAttribute {
+
+ @Override
+ public Collection<ConstraintSegment> getConstraints() {
+ Collection<ConstraintSegment> result = new LinkedList<>();
+ for (ConstraintDefinitionSegment each : constraintDefinitions) {
+ each.getConstraintName().ifPresent(result::add);
+ }
+ return result;
}
- return result;
}
}