This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 5dd0f90  should throw exception while the SQL clause unsupported in 
encrypt rule (#14239)
5dd0f90 is described below

commit 5dd0f90089ed07f526bc9fc4b48e61deea1231d7
Author: cheese8 <[email protected]>
AuthorDate: Fri Dec 24 14:05:40 2021 +0800

    should throw exception while the SQL clause unsupported in encrypt rule 
(#14239)
    
    * should throw exception while the SQL clause unsupported in encrypt rule
    
    * fix checkstyle err
    
    * improve
    
    * support rewrite right value for sotable operator
    
    * improve
    
    * 1、support to encrypt sortableOperator 2、check encrypt valuues on sortable 
operator
    
    * improve style
    
    * improve
---
 .../rewrite/condition/EncryptCondition.java        |  7 +++++
 .../rewrite/condition/EncryptConditionEngine.java  | 33 ++++++++++++++++------
 .../condition/impl/EncryptEqualCondition.java      |  6 +++-
 .../rewrite/condition/impl/EncryptInCondition.java |  6 +++-
 .../impl/EncryptPredicateParameterRewriter.java    | 13 ++++++++-
 .../rewrite/impl/EncryptEqualConditionTest.java    |  2 +-
 .../rewrite/impl/EncryptInConditionTest.java       |  2 +-
 7 files changed, 55 insertions(+), 14 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptCondition.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptCondition.java
index 6b8ddc9..228f804 100644
--- 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptCondition.java
+++ 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptCondition.java
@@ -33,6 +33,13 @@ public interface EncryptCondition {
     String getColumnName();
     
     /**
+     * Is sortable.
+     *
+     * @return sortable
+     */
+    boolean isSortable();
+    
+    /**
      * Get table name.
      * 
      * @return table name
diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java
index 23f12a94..53eb835 100644
--- 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java
+++ 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java
@@ -43,6 +43,7 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.util.ColumnExtractor;
 import 
org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionExtractUtil;
 import org.apache.shardingsphere.sql.parser.sql.common.util.WhereExtractUtil;
 
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -116,11 +117,7 @@ public final class EncryptConditionEngine {
     
     private Optional<EncryptCondition> createEncryptCondition(final 
ExpressionSegment expression, final String tableName) {
         if (expression instanceof BinaryOperationExpression) {
-            String operator = ((BinaryOperationExpression) 
expression).getOperator();
-            if (!LOGICAL_OPERATOR.contains(operator)) {
-                ExpressionSegment rightValue = ((BinaryOperationExpression) 
expression).getRight();
-                return isSupportedOperator(operator) ? 
createCompareEncryptCondition(tableName, (BinaryOperationExpression) 
expression, rightValue) : Optional.empty();
-            }
+            return createEncryptCondition((BinaryOperationExpression) 
expression, tableName);
         }
         if (expression instanceof InExpression) {
             return createInEncryptCondition(tableName, (InExpression) 
expression, ((InExpression) expression).getRight());
@@ -131,6 +128,17 @@ public final class EncryptConditionEngine {
         return Optional.empty();
     }
     
+    private Optional<EncryptCondition> createEncryptCondition(final 
BinaryOperationExpression expression, final String tableName) {
+        String operator = expression.getOperator();
+        if (!LOGICAL_OPERATOR.contains(operator)) {
+            if (isSupportedOperator(operator)) {
+                return createCompareEncryptCondition(tableName, expression, 
operator, expression.getRight());
+            }
+            throw new ShardingSphereException("The SQL clause '%s' is 
unsupported in encrypt rule.", operator);
+        }
+        return Optional.empty();
+    }
+    
     private Collection<WhereSegment> getWhereSegments(final 
SQLStatementContext<?> sqlStatementContext) {
         Collection<WhereSegment> result = new LinkedList<>();
         if (sqlStatementContext instanceof WhereAvailable) {
@@ -157,12 +165,13 @@ public final class EncryptConditionEngine {
         return new ColumnProjection(owner, segment.getIdentifier().getValue(), 
null);
     }
     
-    private static Optional<EncryptCondition> 
createCompareEncryptCondition(final String tableName, final 
BinaryOperationExpression expression, final ExpressionSegment 
compareRightValue) {
+    private Optional<EncryptCondition> createCompareEncryptCondition(final 
String tableName, final BinaryOperationExpression expression, final String 
operator, 
+                                                                     final 
ExpressionSegment compareRightValue) {
         if (!(expression.getLeft() instanceof ColumnSegment)) {
             return Optional.empty();
         }
         return (compareRightValue instanceof SimpleExpressionSegment && 
!(compareRightValue instanceof SubqueryExpressionSegment))
-                ? Optional.of(new EncryptEqualCondition(((ColumnSegment) 
expression.getLeft()).getIdentifier().getValue(), tableName, 
compareRightValue.getStartIndex(),
+                ? Optional.of(new EncryptEqualCondition(((ColumnSegment) 
expression.getLeft()).getIdentifier().getValue(), isSortableOperator(operator), 
tableName, compareRightValue.getStartIndex(),
                 expression.getStopIndex(), compareRightValue))
                 : Optional.empty();
     }
@@ -180,11 +189,17 @@ public final class EncryptConditionEngine {
         if (expressionSegments.isEmpty()) {
             return Optional.empty();
         }
-        return Optional.of(new EncryptInCondition(((ColumnSegment) 
inExpression.getLeft()).getIdentifier().getValue(),
+        return Optional.of(new EncryptInCondition(((ColumnSegment) 
inExpression.getLeft()).getIdentifier().getValue(), isSortableOperator("IN"),
                 tableName, inRightValue.getStartIndex(), 
inRightValue.getStopIndex(), expressionSegments));
     }
     
     private boolean isSupportedOperator(final String operator) {
-        return "=".equals(operator) || "<>".equals(operator) || 
"!=".equals(operator);
+        Collection<String> operators = Arrays.asList("=", "<>", "!=", ">", 
"<", ">=", "<=");
+        return operators.contains(operator);
+    }
+    
+    private static boolean isSortableOperator(final String operator) {
+        Collection<String> operators = Arrays.asList(">", "<", ">=", "<=");
+        return operators.contains(operator);
     }
 }
diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptEqualCondition.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptEqualCondition.java
index 8f6741a..7de8568 100644
--- 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptEqualCondition.java
+++ 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptEqualCondition.java
@@ -41,6 +41,8 @@ public final class EncryptEqualCondition implements 
EncryptCondition {
     
     private final String columnName;
     
+    private final boolean isSortable;
+    
     private final String tableName;
 
     private final int startIndex;
@@ -51,8 +53,10 @@ public final class EncryptEqualCondition implements 
EncryptCondition {
     
     private final Map<Integer, Object> positionValueMap = new 
LinkedHashMap<>();
     
-    public EncryptEqualCondition(final String columnName, final String 
tableName, final int startIndex, final int stopIndex, final ExpressionSegment 
expressionSegment) {
+    public EncryptEqualCondition(final String columnName, final boolean 
isSortable, final String tableName, final int startIndex, final int stopIndex, 
+                                 final ExpressionSegment expressionSegment) {
         this.columnName = columnName;
+        this.isSortable = isSortable;
         this.tableName = tableName;
         this.startIndex = startIndex;
         this.stopIndex = stopIndex;
diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptInCondition.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptInCondition.java
index a9f47b7..5deb8c7 100644
--- 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptInCondition.java
+++ 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptInCondition.java
@@ -41,6 +41,8 @@ public final class EncryptInCondition implements 
EncryptCondition {
     
     private final String columnName;
     
+    private final boolean isSortable;
+    
     private final String tableName;
 
     private final int startIndex;
@@ -51,8 +53,10 @@ public final class EncryptInCondition implements 
EncryptCondition {
     
     private final Map<Integer, Object> positionValueMap = new 
LinkedHashMap<>();
     
-    public EncryptInCondition(final String columnName, final String tableName, 
final int startIndex, final int stopIndex, final List<ExpressionSegment> 
expressionSegments) {
+    public EncryptInCondition(final String columnName, final boolean 
isSortable, final String tableName, final int startIndex, final int stopIndex, 
+                              final List<ExpressionSegment> 
expressionSegments) {
         this.columnName = columnName;
+        this.isSortable = isSortable;
         this.tableName = tableName;
         this.startIndex = startIndex;
         this.stopIndex = stopIndex;
diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptPredicateParameterRewriter.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptPredicateParameterRewriter.java
index c25115b..512ef5b 100644
--- 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptPredicateParameterRewriter.java
+++ 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptPredicateParameterRewriter.java
@@ -23,6 +23,7 @@ import 
org.apache.shardingsphere.encrypt.rewrite.condition.EncryptConditionEngin
 import 
org.apache.shardingsphere.encrypt.rewrite.parameter.EncryptParameterRewriter;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import 
org.apache.shardingsphere.infra.binder.statement.dml.util.DMLStatementContextHelper;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import 
org.apache.shardingsphere.infra.rewrite.parameter.builder.ParameterBuilder;
 import 
org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.StandardParameterBuilder;
@@ -64,9 +65,19 @@ public final class EncryptPredicateParameterRewriter extends 
EncryptParameterRew
     private List<Object> getEncryptedValues(final String schemaName, final 
EncryptCondition encryptCondition, final List<Object> originalValues) {
         String tableName = encryptCondition.getTableName();
         String columnName = encryptCondition.getColumnName();
-        return getEncryptRule().findAssistedQueryColumn(tableName, 
columnName).isPresent()
+        List<Object> result = 
getEncryptRule().findAssistedQueryColumn(tableName, columnName).isPresent()
                 ? getEncryptRule().getEncryptAssistedQueryValues(schemaName, 
tableName, columnName, originalValues) 
                         : getEncryptRule().getEncryptValues(schemaName, 
tableName, columnName, originalValues);
+        checkSortable(encryptCondition, result);
+        return result;
+    }
+    
+    private void checkSortable(final EncryptCondition encryptCondition, final 
List<Object> values) {
+        values.stream().forEach(each -> {
+            if (encryptCondition.isSortable() && !(each instanceof Number)) {
+                throw new ShardingSphereException("The SQL clause is 
unsupported in encrypt rule as not sortable encrypted values.");
+            }
+        });
     }
     
     private void encryptParameters(final ParameterBuilder parameterBuilder, 
final Map<Integer, Integer> positionIndexes, final List<Object> encryptValues) {
diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptEqualConditionTest.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptEqualConditionTest.java
index 3c23e30..58d5ab8 100644
--- 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptEqualConditionTest.java
+++ 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptEqualConditionTest.java
@@ -31,7 +31,7 @@ public final class EncryptEqualConditionTest {
     
     @Test
     public void assertGetConditionValues() {
-        List<Object> actual = new EncryptEqualCondition("col", null, 0, 0, new 
LiteralExpressionSegment(0, 0, 1)).getValues(Collections.emptyList());
+        List<Object> actual = new EncryptEqualCondition("col", false, null, 0, 
0, new LiteralExpressionSegment(0, 0, 1)).getValues(Collections.emptyList());
         assertThat(actual.size(), is(1));
         assertThat(actual.get(0), is(1));
     }
diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptInConditionTest.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptInConditionTest.java
index aea5979..2787a8c 100644
--- 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptInConditionTest.java
+++ 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptInConditionTest.java
@@ -33,7 +33,7 @@ public final class EncryptInConditionTest {
     @Test
     public void assertGetConditionValues() {
         List<Object> actual = new EncryptInCondition(
-                "col", null, 0, 0, Arrays.asList(new 
LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 
2))).getValues(Collections.emptyList());
+                "col", false, null, 0, 0, Arrays.asList(new 
LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 
2))).getValues(Collections.emptyList());
         assertThat(actual.size(), is(2));
         assertThat(actual.get(0), is(1));
         assertThat(actual.get(1), is(2));

Reply via email to