strongduanmu commented on a change in pull request #12504:
URL: https://github.com/apache/shardingsphere/pull/12504#discussion_r716301816



##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptPredicateColumnTokenGenerator.java
##########
@@ -56,57 +59,82 @@
     
     @Override
     protected boolean isGenerateSQLTokenForEncrypt(final SQLStatementContext 
sqlStatementContext) {
+        return 
isGenerateSQLTokenForEncryptOnWhereAvailable(sqlStatementContext) || 
isGenerateSQLTokenForEncryptOnJoinSegments(sqlStatementContext);
+    }
+    
+    private boolean isGenerateSQLTokenForEncryptOnWhereAvailable(final 
SQLStatementContext sqlStatementContext) {
         return sqlStatementContext instanceof WhereAvailable && 
((WhereAvailable) sqlStatementContext).getWhere().isPresent();
     }
     
+    private boolean isGenerateSQLTokenForEncryptOnJoinSegments(final 
SQLStatementContext sqlStatementContext) {
+        return sqlStatementContext instanceof SelectStatementContext && 
!WhereExtractUtil.getJoinWhereSegments((SelectStatement) 
sqlStatementContext.getSqlStatement()).isEmpty();
+    }
+    
     @Override
     public Collection<SubstitutableColumnNameToken> generateSQLTokens(final 
SQLStatementContext sqlStatementContext) {
-        Preconditions.checkState(((WhereAvailable) 
sqlStatementContext).getWhere().isPresent());
         Collection<SubstitutableColumnNameToken> result = new 
LinkedHashSet<>();
-        ExpressionSegment expression = ((WhereAvailable) 
sqlStatementContext).getWhere().get().getExpr();
-        Collection<AndPredicate> andPredicates = 
ExpressionExtractUtil.getAndPredicates(expression);
-        Map<String, String> columnTableNames = 
getColumnTableNames(sqlStatementContext, andPredicates);
-        for (AndPredicate each : andPredicates) {
-            result.addAll(generateSQLTokens(each.getPredicates(), 
columnTableNames));
+        Collection<AndPredicate> andPredicates = new LinkedHashSet<>();
+        if (isGenerateSQLTokenForEncryptOnWhereAvailable(sqlStatementContext)) 
{
+            ExpressionSegment expression = ((WhereAvailable) 
sqlStatementContext).getWhere().get().getExpr();
+            
andPredicates.addAll(ExpressionExtractUtil.getAndPredicates(expression));
         }
+        Optional<Collection<WhereSegment>> whereSegments = Optional.empty();
+        if (sqlStatementContext instanceof SelectStatementContext) {
+            whereSegments = 
Optional.of(WhereExtractUtil.getJoinWhereSegments((SelectStatement) 
sqlStatementContext.getSqlStatement()));

Review comment:
       Why add `Optional` here?

##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptPredicateColumnTokenGenerator.java
##########
@@ -56,57 +59,82 @@
     
     @Override
     protected boolean isGenerateSQLTokenForEncrypt(final SQLStatementContext 
sqlStatementContext) {
+        return 
isGenerateSQLTokenForEncryptOnWhereAvailable(sqlStatementContext) || 
isGenerateSQLTokenForEncryptOnJoinSegments(sqlStatementContext);
+    }
+    
+    private boolean isGenerateSQLTokenForEncryptOnWhereAvailable(final 
SQLStatementContext sqlStatementContext) {
         return sqlStatementContext instanceof WhereAvailable && 
((WhereAvailable) sqlStatementContext).getWhere().isPresent();
     }
     
+    private boolean isGenerateSQLTokenForEncryptOnJoinSegments(final 
SQLStatementContext sqlStatementContext) {
+        return sqlStatementContext instanceof SelectStatementContext && 
!WhereExtractUtil.getJoinWhereSegments((SelectStatement) 
sqlStatementContext.getSqlStatement()).isEmpty();

Review comment:
       @cheese8 Can it be replaced by the `isContainsJoinQuery` method?

##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
##########
@@ -63,10 +65,16 @@ protected boolean isGenerateSQLTokenForEncrypt(final 
SQLStatementContext sqlStat
     @Override
     public Collection<SubstitutableColumnNameToken> generateSQLTokens(final 
SelectStatementContext selectStatementContext) {
         ProjectionsSegment projectionsSegment = 
selectStatementContext.getSqlStatement().getProjections();
-        // TODO process multiple tables
-        String tableName = 
selectStatementContext.getAllTables().iterator().next().getTableName().getIdentifier().getValue();
-        return getEncryptRule().findEncryptTable(tableName).map(
-            encryptTable -> generateSQLTokens(projectionsSegment, tableName, 
selectStatementContext, encryptTable)).orElseGet(Collections::emptyList);
+        Collection<SubstitutableColumnNameToken> result = new HashSet<>();
+        Collection<String> tableNames = 
selectStatementContext.getTablesContext().getTableNames();
+        for (String each : tableNames) {
+            Optional<EncryptTable> encryptTable = 
getEncryptRule().findEncryptTable(each);
+            if (encryptTable.isPresent()) {
+                Collection<SubstitutableColumnNameToken> sqlTokens = 
generateSQLTokens(projectionsSegment, each, selectStatementContext, 
encryptTable.get());

Review comment:
       @cheese8 `result.add(generateSQLTokens(projectionsSegment, each, 
selectStatementContext, encryptTable.get()))` is better.

##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptPredicateColumnTokenGenerator.java
##########
@@ -56,57 +59,82 @@
     
     @Override
     protected boolean isGenerateSQLTokenForEncrypt(final SQLStatementContext 
sqlStatementContext) {
+        return 
isGenerateSQLTokenForEncryptOnWhereAvailable(sqlStatementContext) || 
isGenerateSQLTokenForEncryptOnJoinSegments(sqlStatementContext);
+    }
+    
+    private boolean isGenerateSQLTokenForEncryptOnWhereAvailable(final 
SQLStatementContext sqlStatementContext) {
         return sqlStatementContext instanceof WhereAvailable && 
((WhereAvailable) sqlStatementContext).getWhere().isPresent();
     }
     
+    private boolean isGenerateSQLTokenForEncryptOnJoinSegments(final 
SQLStatementContext sqlStatementContext) {
+        return sqlStatementContext instanceof SelectStatementContext && 
!WhereExtractUtil.getJoinWhereSegments((SelectStatement) 
sqlStatementContext.getSqlStatement()).isEmpty();
+    }
+    
     @Override
     public Collection<SubstitutableColumnNameToken> generateSQLTokens(final 
SQLStatementContext sqlStatementContext) {
-        Preconditions.checkState(((WhereAvailable) 
sqlStatementContext).getWhere().isPresent());
         Collection<SubstitutableColumnNameToken> result = new 
LinkedHashSet<>();
-        ExpressionSegment expression = ((WhereAvailable) 
sqlStatementContext).getWhere().get().getExpr();
-        Collection<AndPredicate> andPredicates = 
ExpressionExtractUtil.getAndPredicates(expression);
-        Map<String, String> columnTableNames = 
getColumnTableNames(sqlStatementContext, andPredicates);
-        for (AndPredicate each : andPredicates) {
-            result.addAll(generateSQLTokens(each.getPredicates(), 
columnTableNames));
+        Collection<AndPredicate> andPredicates = new LinkedHashSet<>();
+        if (isGenerateSQLTokenForEncryptOnWhereAvailable(sqlStatementContext)) 
{
+            ExpressionSegment expression = ((WhereAvailable) 
sqlStatementContext).getWhere().get().getExpr();
+            
andPredicates.addAll(ExpressionExtractUtil.getAndPredicates(expression));
         }
+        Optional<Collection<WhereSegment>> whereSegments = Optional.empty();
+        if (sqlStatementContext instanceof SelectStatementContext) {
+            whereSegments = 
Optional.of(WhereExtractUtil.getJoinWhereSegments((SelectStatement) 
sqlStatementContext.getSqlStatement()));
+            whereSegments.get().forEach(each -> 
andPredicates.addAll(ExpressionExtractUtil.getAndPredicates(each.getExpr())));
+        }
+        Map<String, String> columnTableNames = 
getColumnTableNames(sqlStatementContext, andPredicates, whereSegments);
+        andPredicates.forEach(each -> 
result.addAll(generateSQLTokens(each.getPredicates(), columnTableNames)));

Review comment:
       @cheese8 Can we use stream api to replace foreach?

##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/predicate/WhereSegment.java
##########
@@ -29,6 +31,7 @@
 @RequiredArgsConstructor
 @Getter
 @Setter
+@EqualsAndHashCode

Review comment:
       @cheese8 Why add this annotation?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to