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 c4e58167651 Add WithSQLStatementAttribute (#35776)
c4e58167651 is described below
commit c4e581676512858ad3711c224cabeec766a68118
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jun 21 17:55:20 2025 +0800
Add WithSQLStatementAttribute (#35776)
* Add WithSQLStatementAttribute
* Add WithSQLStatementAttribute
* Add WithSQLStatementAttribute
* Add WithSQLStatementAttribute
* Add WithSQLStatementAttribute
* Add WithSQLStatementAttribute
* Add WithSQLStatementAttribute
---
.../sql/with/EncryptWithClauseSupportedChecker.java | 4 ++--
.../with/EncryptWithClauseSupportedCheckerTest.java | 12 +++++++++---
.../ShardingSQLRewriteContextDecoratorTest.java | 6 +++++-
.../statement/type/dml/SelectStatementContext.java | 2 +-
.../type/WithSQLStatementAttribute.java} | 21 +++++++++++++--------
.../core/statement/dml/DeleteStatement.java | 18 ++++++++++++++----
.../core/statement/dml/InsertStatement.java | 18 ++++++++++++++----
.../core/statement/dml/MergeStatement.java | 7 +++++++
.../core/statement/dml/SelectStatement.java | 18 ++++++++++++++----
.../core/statement/dml/UpdateStatement.java | 18 ++++++++++++++----
10 files changed, 93 insertions(+), 31 deletions(-)
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/with/EncryptWithClauseSupportedChecker.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/with/EncryptWithClauseSupportedChecker.java
index 7bd2eb939ee..1757d2541a9 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/with/EncryptWithClauseSupportedChecker.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/with/EncryptWithClauseSupportedChecker.java
@@ -26,7 +26,7 @@ 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.sql.parser.statement.core.statement.available.WithAvailableSQLStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.WithSQLStatementAttribute;
/**
* With clause supported checker for encrypt.
@@ -36,7 +36,7 @@ public final class EncryptWithClauseSupportedChecker
implements SupportedSQLChec
@Override
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext.getSqlStatement() instanceof
WithAvailableSQLStatement && ((WithAvailableSQLStatement)
sqlStatementContext.getSqlStatement()).getWith().isPresent();
+ return
sqlStatementContext.getSqlStatement().getAttributes().findAttribute(WithSQLStatementAttribute.class).map(WithSQLStatementAttribute::containsWith).orElse(false);
}
@Override
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/with/EncryptWithClauseSupportedCheckerTest.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/with/EncryptWithClauseSupportedCheckerTest.java
index b4583baf78a..a083cddea90 100644
---
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/with/EncryptWithClauseSupportedCheckerTest.java
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/with/EncryptWithClauseSupportedCheckerTest.java
@@ -21,6 +21,8 @@ import
org.apache.shardingsphere.encrypt.exception.syntax.UnsupportedEncryptSQLE
import
org.apache.shardingsphere.encrypt.rewrite.token.generator.fixture.EncryptGeneratorFixtureBuilder;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.dml.SelectStatementContext;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.WithSQLStatementAttribute;
import org.junit.jupiter.api.Test;
import java.util.Collections;
@@ -38,13 +40,17 @@ class EncryptWithClauseSupportedCheckerTest {
@Test
void assertIsCheck() {
SelectStatementContext sqlStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
-
when(sqlStatementContext.getSqlStatement().getWith().isPresent()).thenReturn(true);
+ WithSQLStatementAttribute withAttribute =
mock(WithSQLStatementAttribute.class);
+ when(withAttribute.containsWith()).thenReturn(true);
+
when(sqlStatementContext.getSqlStatement().getAttributes()).thenReturn(new
SQLStatementAttributes(withAttribute));
assertTrue(new
EncryptWithClauseSupportedChecker().isCheck(sqlStatementContext));
}
@Test
- void assertIsCheckWithoutWithAvailable() {
- assertFalse(new
EncryptWithClauseSupportedChecker().isCheck(mock(SQLStatementContext.class)));
+ void assertIsCheckWithoutWith() {
+ SelectStatementContext sqlStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+
when(sqlStatementContext.getSqlStatement().getAttributes()).thenReturn(new
SQLStatementAttributes());
+ assertFalse(new
EncryptWithClauseSupportedChecker().isCheck(sqlStatementContext));
}
@Test
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
index c45fc4706f4..b8a2af3ae46 100644
---
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
@@ -24,6 +24,7 @@ import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
import org.junit.jupiter.api.Test;
import java.util.Collections;
@@ -41,7 +42,9 @@ class ShardingSQLRewriteContextDecoratorTest {
SQLRewriteContext sqlRewriteContext = mock(SQLRewriteContext.class);
when(sqlRewriteContext.getDatabase()).thenReturn(mock(ShardingSphereDatabase.class));
when(sqlRewriteContext.getParameters()).thenReturn(Collections.singletonList(new
Object()));
-
when(sqlRewriteContext.getSqlStatementContext()).thenReturn(mock(SQLStatementContext.class,
RETURNS_DEEP_STUBS));
+ SQLStatementContext sqlStatementContext =
mock(SQLStatementContext.class, RETURNS_DEEP_STUBS);
+
when(sqlStatementContext.getSqlStatement().getAttributes()).thenReturn(new
SQLStatementAttributes());
+
when(sqlRewriteContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
new
ShardingSQLRewriteContextDecorator().decorate(mock(ShardingRule.class),
mock(ConfigurationProperties.class), sqlRewriteContext,
mock(RouteContext.class));
assertTrue(sqlRewriteContext.getSqlTokens().isEmpty());
}
@@ -51,6 +54,7 @@ class ShardingSQLRewriteContextDecoratorTest {
SQLRewriteContext sqlRewriteContext = mock(SQLRewriteContext.class);
InsertStatementContext insertStatementContext =
mock(InsertStatementContext.class, RETURNS_DEEP_STUBS);
when(insertStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("t_order"));
+
when(insertStatementContext.getSqlStatement().getAttributes()).thenReturn(new
SQLStatementAttributes());
when(sqlRewriteContext.getSqlStatementContext()).thenReturn(insertStatementContext);
ShardingRule shardingRule = mock(ShardingRule.class);
when(shardingRule.findShardingTable("t_order")).thenReturn(Optional.empty());
diff --git
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/dml/SelectStatementContext.java
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/dml/SelectStatementContext.java
index 4d1ed650aea..c536a3eb057 100644
---
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/dml/SelectStatementContext.java
+++
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/dml/SelectStatementContext.java
@@ -20,6 +20,7 @@ package
org.apache.shardingsphere.infra.binder.context.statement.type.dml;
import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.Setter;
+import
org.apache.shardingsphere.infra.binder.context.available.WhereContextAvailable;
import org.apache.shardingsphere.infra.binder.context.aware.ParameterAware;
import
org.apache.shardingsphere.infra.binder.context.segment.select.groupby.GroupByContext;
import
org.apache.shardingsphere.infra.binder.context.segment.select.groupby.engine.GroupByContextEngine;
@@ -38,7 +39,6 @@ import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.SubqueryProjection;
import
org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import
org.apache.shardingsphere.infra.binder.context.available.WhereContextAvailable;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import
org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.NoDatabaseSelectedException;
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/available/WithAvailableSQLStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/type/WithSQLStatementAttribute.java
similarity index 67%
rename from
parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/available/WithAvailableSQLStatement.java
rename to
parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/type/WithSQLStatementAttribute.java
index 786e8bd1c3a..16748b1af7f 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/available/WithAvailableSQLStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/type/WithSQLStatementAttribute.java
@@ -15,21 +15,26 @@
* 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 lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
-
-import java.util.Optional;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttribute;
/**
- * With available SQL statement.
+ * With SQL statement attribute.
*/
-public interface WithAvailableSQLStatement {
+@RequiredArgsConstructor
+public final class WithSQLStatementAttribute implements SQLStatementAttribute {
+
+ private final WithSegment with;
/**
- * Get with segment.
+ * Whether to contain with.
*
- * @return with segment
+ * @return contains with or not
*/
- Optional<WithSegment> getWith();
+ public boolean containsWith() {
+ return null != with;
+ }
}
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/DeleteStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/DeleteStatement.java
index 9f6dc5a8f08..0c88133b922 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/DeleteStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/DeleteStatement.java
@@ -27,7 +27,8 @@ import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.Outpu
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.available.WithAvailableSQLStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.WithSQLStatementAttribute;
import java.util.Optional;
@@ -36,7 +37,7 @@ import java.util.Optional;
*/
@Getter
@Setter
-public final class DeleteStatement extends AbstractSQLStatement implements
DMLStatement, WithAvailableSQLStatement {
+public final class DeleteStatement extends AbstractSQLStatement implements
DMLStatement {
private TableSegment table;
@@ -79,6 +80,15 @@ public final class DeleteStatement extends
AbstractSQLStatement implements DMLSt
return Optional.ofNullable(limit);
}
+ /**
+ * Get with.
+ *
+ * @return with
+ */
+ public Optional<WithSegment> getWith() {
+ return Optional.ofNullable(with);
+ }
+
/**
* Get returning.
*
@@ -98,7 +108,7 @@ public final class DeleteStatement extends
AbstractSQLStatement implements DMLSt
}
@Override
- public Optional<WithSegment> getWith() {
- return Optional.ofNullable(with);
+ public SQLStatementAttributes getAttributes() {
+ return new SQLStatementAttributes(new WithSQLStatementAttribute(with));
}
}
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/InsertStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/InsertStatement.java
index f5a460ba11e..9f7d5cb4e54 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/InsertStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/InsertStatement.java
@@ -37,7 +37,8 @@ import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.Outpu
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
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.WithAvailableSQLStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.WithSQLStatementAttribute;
import java.util.Collection;
import java.util.Collections;
@@ -49,7 +50,7 @@ import java.util.Optional;
*/
@Getter
@Setter
-public final class InsertStatement extends AbstractSQLStatement implements
DMLStatement, WithAvailableSQLStatement {
+public final class InsertStatement extends AbstractSQLStatement implements
DMLStatement {
private SimpleTableSegment table;
@@ -139,6 +140,15 @@ public final class InsertStatement extends
AbstractSQLStatement implements DMLSt
return Optional.ofNullable(setAssignment);
}
+ /**
+ * Get with.
+ *
+ * @return with
+ */
+ public Optional<WithSegment> getWith() {
+ return Optional.ofNullable(with);
+ }
+
/**
* Get output.
*
@@ -221,7 +231,7 @@ public final class InsertStatement extends
AbstractSQLStatement implements DMLSt
}
@Override
- public Optional<WithSegment> getWith() {
- return Optional.ofNullable(with);
+ public SQLStatementAttributes getAttributes() {
+ return new SQLStatementAttributes(new WithSQLStatementAttribute(with));
}
}
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/MergeStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/MergeStatement.java
index 1a053e46069..039fa92fe10 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/MergeStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/MergeStatement.java
@@ -28,6 +28,8 @@ import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.Outpu
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.WithSQLStatementAttribute;
import java.util.Collection;
import java.util.LinkedList;
@@ -115,4 +117,9 @@ public final class MergeStatement extends
AbstractSQLStatement implements DMLSta
public Optional<OptionHintSegment> getOptionHint() {
return Optional.ofNullable(optionHint);
}
+
+ @Override
+ public SQLStatementAttributes getAttributes() {
+ return new SQLStatementAttributes(new WithSQLStatementAttribute(with));
+ }
}
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/SelectStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/SelectStatement.java
index 8bb27303eec..8a7991941a6 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/SelectStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/SelectStatement.java
@@ -34,7 +34,8 @@ import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.Windo
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.available.WithAvailableSQLStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.WithSQLStatementAttribute;
import java.util.Optional;
@@ -43,7 +44,7 @@ import java.util.Optional;
*/
@Getter
@Setter
-public final class SelectStatement extends AbstractSQLStatement implements
DMLStatement, WithAvailableSQLStatement {
+public final class SelectStatement extends AbstractSQLStatement implements
DMLStatement {
private ProjectionsSegment projections;
@@ -129,6 +130,15 @@ public final class SelectStatement extends
AbstractSQLStatement implements DMLSt
return Optional.ofNullable(combine);
}
+ /**
+ * Get with.
+ *
+ * @return with
+ */
+ public Optional<WithSegment> getWith() {
+ return Optional.ofNullable(with);
+ }
+
/**
* Get subquery type.
*
@@ -193,7 +203,7 @@ public final class SelectStatement extends
AbstractSQLStatement implements DMLSt
}
@Override
- public Optional<WithSegment> getWith() {
- return Optional.ofNullable(with);
+ public SQLStatementAttributes getAttributes() {
+ return new SQLStatementAttributes(new WithSQLStatementAttribute(with));
}
}
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/UpdateStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/UpdateStatement.java
index 0acc94975eb..6a2a8b44268 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/UpdateStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dml/UpdateStatement.java
@@ -30,7 +30,8 @@ import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.Outpu
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.available.WithAvailableSQLStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.WithSQLStatementAttribute;
import java.util.Optional;
@@ -39,7 +40,7 @@ import java.util.Optional;
*/
@Getter
@Setter
-public final class UpdateStatement extends AbstractSQLStatement implements
DMLStatement, WithAvailableSQLStatement {
+public final class UpdateStatement extends AbstractSQLStatement implements
DMLStatement {
private TableSegment table;
@@ -137,6 +138,15 @@ public final class UpdateStatement extends
AbstractSQLStatement implements DMLSt
return Optional.ofNullable(deleteWhere);
}
+ /**
+ * Get with.
+ *
+ * @return with
+ */
+ public Optional<WithSegment> getWith() {
+ return Optional.ofNullable(with);
+ }
+
/**
* Get returning.
*
@@ -156,7 +166,7 @@ public final class UpdateStatement extends
AbstractSQLStatement implements DMLSt
}
@Override
- public Optional<WithSegment> getWith() {
- return Optional.ofNullable(with);
+ public SQLStatementAttributes getAttributes() {
+ return new SQLStatementAttributes(new WithSQLStatementAttribute(with));
}
}