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 cd360cc78a6 Add test cases on
EncryptInsertSelectProjectionSupportedChecker (#33646)
cd360cc78a6 is described below
commit cd360cc78a61163a63dd44b9cdf536f34f50be9d
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Nov 13 23:03:47 2024 +0800
Add test cases on EncryptInsertSelectProjectionSupportedChecker (#33646)
* Add test cases on EncryptInsertSelectProjectionSupportedChecker
* Add test cases on EncryptInsertSelectProjectionSupportedChecker
---
.../EncryptOrderByItemSupportedChecker.java | 8 +--
...ryptInsertSelectProjectionSupportedChecker.java | 10 ++--
.../EncryptProjectionRewriteSupportedChecker.java | 16 +++---
.../EncryptSelectProjectionSupportedChecker.java | 10 ++--
...InsertSelectProjectionSupportedCheckerTest.java | 66 ++++++++++++++++++++++
5 files changed, 88 insertions(+), 22 deletions(-)
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedChecker.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedChecker.java
index edc84aa1c8b..b53b9b352de 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedChecker.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedChecker.java
@@ -60,10 +60,10 @@ public final class EncryptOrderByItemSupportedChecker
implements SupportedSQLChe
}
@Override
- public void check(final EncryptRule encryptRule, final
ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext) {
+ public void check(final EncryptRule rule, final ShardingSphereSchema
schema, final SelectStatementContext sqlStatementContext) {
for (OrderByItem each : getOrderByItems(sqlStatementContext)) {
if (each.getSegment() instanceof ColumnOrderByItemSegment) {
- checkColumnOrderByItem(encryptRule, schema,
sqlStatementContext, ((ColumnOrderByItemSegment)
each.getSegment()).getColumn());
+ checkColumnOrderByItem(rule, schema, sqlStatementContext,
((ColumnOrderByItemSegment) each.getSegment()).getColumn());
}
}
}
@@ -79,10 +79,10 @@ public final class EncryptOrderByItemSupportedChecker
implements SupportedSQLChe
return result;
}
- private void checkColumnOrderByItem(final EncryptRule encryptRule, final
ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext,
final ColumnSegment columnSegment) {
+ private void checkColumnOrderByItem(final EncryptRule rule, final
ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext,
final ColumnSegment columnSegment) {
Map<String, String> columnTableNames =
sqlStatementContext.getTablesContext().findTableNames(Collections.singleton(columnSegment),
schema);
String tableName =
columnTableNames.getOrDefault(columnSegment.getExpression(), "");
- Optional<EncryptTable> encryptTable =
encryptRule.findEncryptTable(tableName);
+ Optional<EncryptTable> encryptTable = rule.findEncryptTable(tableName);
String columnName = columnSegment.getIdentifier().getValue();
ShardingSpherePreconditions.checkState(!encryptTable.isPresent() ||
!encryptTable.get().isEncryptColumn(columnName), () -> new
UnsupportedEncryptSQLException("ORDER BY"));
}
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedChecker.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedChecker.java
index 64ef9467eea..d8eb389ea2d 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedChecker.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedChecker.java
@@ -38,15 +38,15 @@ public final class
EncryptInsertSelectProjectionSupportedChecker implements Supp
}
@Override
- public void check(final EncryptRule encryptRule, final
ShardingSphereSchema schema, final InsertStatementContext sqlStatementContext) {
- checkSelect(encryptRule,
sqlStatementContext.getInsertSelectContext().getSelectStatementContext());
+ public void check(final EncryptRule rule, final ShardingSphereSchema
schema, final InsertStatementContext sqlStatementContext) {
+ checkSelect(rule,
sqlStatementContext.getInsertSelectContext().getSelectStatementContext());
for (SelectStatementContext each :
sqlStatementContext.getInsertSelectContext().getSelectStatementContext().getSubqueryContexts().values())
{
- checkSelect(encryptRule, each);
+ checkSelect(rule, each);
}
}
- private void checkSelect(final EncryptRule encryptRule, final
SelectStatementContext selectStatementContext) {
-
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptProjectionInCombineSegment(encryptRule,
selectStatementContext);
+ private void checkSelect(final EncryptRule rule, final
SelectStatementContext selectStatementContext) {
+
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptProjectionInCombineSegment(rule,
selectStatementContext);
for (ProjectionSegment each :
selectStatementContext.getSqlStatement().getProjections().getProjections()) {
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptShorthandExpandWithSubqueryStatement(selectStatementContext,
each);
}
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptProjectionRewriteSupportedChecker.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptProjectionRewriteSupportedChecker.java
index 0118ada8034..d1ab2ce8d07 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptProjectionRewriteSupportedChecker.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptProjectionRewriteSupportedChecker.java
@@ -45,15 +45,15 @@ public final class EncryptProjectionRewriteSupportedChecker
{
/**
* Check not contain encrypt projection in combine segment.
*
- * @param encryptRule encrypt rule
+ * @param rule encrypt rule
* @param selectStatementContext select statement context
*/
- public static void checkNotContainEncryptProjectionInCombineSegment(final
EncryptRule encryptRule, final SelectStatementContext selectStatementContext) {
-
ShardingSpherePreconditions.checkState(!containsEncryptProjectionInCombineSegment(encryptRule,
selectStatementContext),
+ public static void checkNotContainEncryptProjectionInCombineSegment(final
EncryptRule rule, final SelectStatementContext selectStatementContext) {
+
ShardingSpherePreconditions.checkState(!containsEncryptProjectionInCombineSegment(rule,
selectStatementContext),
() -> new UnsupportedSQLOperationException("Can not support
encrypt projection in combine statement."));
}
- private static boolean containsEncryptProjectionInCombineSegment(final
EncryptRule encryptRule, final SelectStatementContext selectStatementContext) {
+ private static boolean containsEncryptProjectionInCombineSegment(final
EncryptRule rule, final SelectStatementContext selectStatementContext) {
if
(!selectStatementContext.getSqlStatement().getCombine().isPresent()) {
return false;
}
@@ -62,18 +62,18 @@ public final class EncryptProjectionRewriteSupportedChecker
{
List<Projection> rightProjections =
selectStatementContext.getSubqueryContexts().get(combineSegment.getRight().getStartIndex()).getProjectionsContext().getExpandProjections();
ShardingSpherePreconditions.checkState(leftProjections.size() ==
rightProjections.size(), () -> new UnsupportedSQLOperationException("Column
projections must be same for combine statement"));
for (int i = 0; i < leftProjections.size(); i++) {
- if (containsEncryptProjectionInCombineSegment(encryptRule,
leftProjections.get(i), rightProjections.get(i))) {
+ if (containsEncryptProjectionInCombineSegment(rule,
leftProjections.get(i), rightProjections.get(i))) {
return true;
}
}
return false;
}
- private static boolean containsEncryptProjectionInCombineSegment(final
EncryptRule encryptRule, final Projection leftProjection, final Projection
rightProjection) {
+ private static boolean containsEncryptProjectionInCombineSegment(final
EncryptRule rule, final Projection leftProjection, final Projection
rightProjection) {
ColumnSegmentBoundInfo leftColumnInfo =
getColumnSegmentBoundInfo(leftProjection);
- EncryptAlgorithm leftColumnEncryptor =
encryptRule.findQueryEncryptor(leftColumnInfo.getOriginalTable().getValue(),
leftColumnInfo.getOriginalColumn().getValue()).orElse(null);
+ EncryptAlgorithm leftColumnEncryptor =
rule.findQueryEncryptor(leftColumnInfo.getOriginalTable().getValue(),
leftColumnInfo.getOriginalColumn().getValue()).orElse(null);
ColumnSegmentBoundInfo rightColumnInfo =
getColumnSegmentBoundInfo(rightProjection);
- EncryptAlgorithm rightColumnEncryptor =
encryptRule.findQueryEncryptor(rightColumnInfo.getOriginalTable().getValue(),
rightColumnInfo.getOriginalColumn().getValue()).orElse(null);
+ EncryptAlgorithm rightColumnEncryptor =
rule.findQueryEncryptor(rightColumnInfo.getOriginalTable().getValue(),
rightColumnInfo.getOriginalColumn().getValue()).orElse(null);
return null != leftColumnEncryptor || null != rightColumnEncryptor;
}
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedChecker.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedChecker.java
index c646db3bec1..f8c6b9fc792 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedChecker.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedChecker.java
@@ -37,15 +37,15 @@ public final class EncryptSelectProjectionSupportedChecker
implements SupportedS
}
@Override
- public void check(final EncryptRule encryptRule, final
ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext) {
- checkSelect(encryptRule, sqlStatementContext);
+ public void check(final EncryptRule rule, final ShardingSphereSchema
schema, final SelectStatementContext sqlStatementContext) {
+ checkSelect(rule, sqlStatementContext);
for (SelectStatementContext each :
sqlStatementContext.getSubqueryContexts().values()) {
- checkSelect(encryptRule, each);
+ checkSelect(rule, each);
}
}
- private void checkSelect(final EncryptRule encryptRule, final
SelectStatementContext selectStatementContext) {
-
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptProjectionInCombineSegment(encryptRule,
selectStatementContext);
+ private void checkSelect(final EncryptRule rule, final
SelectStatementContext selectStatementContext) {
+
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptProjectionInCombineSegment(rule,
selectStatementContext);
for (ProjectionSegment each :
selectStatementContext.getSqlStatement().getProjections().getProjections()) {
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptShorthandExpandWithSubqueryStatement(selectStatementContext,
each);
}
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedCheckerTest.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedCheckerTest.java
new file mode 100644
index 00000000000..493b0cabd03
--- /dev/null
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedCheckerTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.encrypt.checker.sql.projection;
+
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import
org.apache.shardingsphere.infra.binder.context.segment.insert.values.InsertSelectContext;
+import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
+import
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
+import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class EncryptInsertSelectProjectionSupportedCheckerTest {
+
+ @Test
+ void assertIsCheckWithNotInsertStatementContext() {
+ assertFalse(new
EncryptInsertSelectProjectionSupportedChecker().isCheck(mock(SQLStatementContext.class)));
+ }
+
+ @Test
+ void assertIsCheckWithoutInsertSelect() {
+ InsertStatementContext sqlStatementContext =
mock(InsertStatementContext.class);
+ when(sqlStatementContext.getInsertSelectContext()).thenReturn(null);
+ assertFalse(new
EncryptInsertSelectProjectionSupportedChecker().isCheck(sqlStatementContext));
+ }
+
+ @Test
+ void assertIsCheckWithInsertSelect() {
+ InsertStatementContext sqlStatementContext =
mock(InsertStatementContext.class);
+
when(sqlStatementContext.getInsertSelectContext()).thenReturn(mock(InsertSelectContext.class));
+ assertTrue(new
EncryptInsertSelectProjectionSupportedChecker().isCheck(sqlStatementContext));
+ }
+
+ @Test
+ void assertCheckSuccess() {
+ InsertStatementContext sqlStatementContext =
mock(InsertStatementContext.class);
+
when(sqlStatementContext.getInsertSelectContext()).thenReturn(mock(InsertSelectContext.class,
RETURNS_DEEP_STUBS));
+
when(sqlStatementContext.getInsertSelectContext().getSelectStatementContext().getSubqueryContexts().values())
+
.thenReturn(Collections.singletonList(mock(SelectStatementContext.class,
RETURNS_DEEP_STUBS)));
+ assertDoesNotThrow(() -> new
EncryptInsertSelectProjectionSupportedChecker().check(mock(EncryptRule.class),
mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
+}