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



##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/WhereExtractUtil.java
##########
@@ -47,14 +51,38 @@
             return Collections.emptyList();
         }
         TableSegment tableSegment = selectStatement.getFrom();
-        Collection<WhereSegment> result = new LinkedList<>();
-        if (tableSegment instanceof JoinTableSegment && null != 
((JoinTableSegment) tableSegment).getCondition()) {
-            ExpressionSegment expressionSegment = ((JoinTableSegment) 
tableSegment).getCondition();
-            WhereSegment whereSegment = new 
WhereSegment(expressionSegment.getStartIndex(), 
expressionSegment.getStopIndex(), expressionSegment);
-            result.add(whereSegment);
+        Collection<WhereSegment> result = new HashSet<>();
+        
+        if (!(tableSegment instanceof JoinTableSegment) || null == 
((JoinTableSegment) tableSegment).getCondition()) {
+            return Collections.emptyList();
+        }
+        
+        JoinTableSegment joinTableSegment = (JoinTableSegment) tableSegment;
+        ExpressionSegment leftCondition = joinTableSegment.getCondition();

Review comment:
       @cheese8 Can we optimize this logic by recursive JoinTableSegment?

##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/ColumnExtractor.java
##########
@@ -54,4 +59,35 @@
         }
         return Optional.empty();
     }
+    
+    /**
+     * Get left and right value if either value of expression is column 
segment.
+     *
+     * @param expression expression segment
+     * @return column segment collection
+     */
+    public static Collection<ColumnSegment> extractAll(final ExpressionSegment 
expression) {
+        if (expression instanceof BinaryOperationExpression) {
+            BinaryOperationExpression boExpression = 
(BinaryOperationExpression) expression;
+            Collection<ColumnSegment> columns = new ArrayList<>();
+            if (Objects.nonNull(boExpression.getLeft()) && 
boExpression.getLeft() instanceof ColumnSegment) {

Review comment:
       @cheese8 `Objects.nonNull(boExpression.getLeft())` is useless, can we 
remove it?

##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/ColumnExtractor.java
##########
@@ -54,4 +59,35 @@
         }
         return Optional.empty();
     }
+    
+    /**
+     * Get left and right value if either value of expression is column 
segment.
+     *
+     * @param expression expression segment
+     * @return column segment collection
+     */
+    public static Collection<ColumnSegment> extractAll(final ExpressionSegment 
expression) {
+        if (expression instanceof BinaryOperationExpression) {
+            BinaryOperationExpression boExpression = 
(BinaryOperationExpression) expression;
+            Collection<ColumnSegment> columns = new ArrayList<>();
+            if (Objects.nonNull(boExpression.getLeft()) && 
boExpression.getLeft() instanceof ColumnSegment) {
+                columns.add((ColumnSegment) boExpression.getLeft());
+            }
+            if (Objects.nonNull(boExpression.getRight()) && 
boExpression.getRight() instanceof ColumnSegment) {
+                columns.add((ColumnSegment) boExpression.getRight());
+            }
+            return columns;
+        }
+        if (expression instanceof InExpression && 
Objects.nonNull(((InExpression) expression).getLeft()) 
+                && ((InExpression) expression).getLeft() instanceof 
ColumnSegment) {
+            ColumnSegment column = (ColumnSegment) ((InExpression) 
expression).getLeft();
+            return Arrays.asList(column);
+        }
+        if (expression instanceof BetweenExpression && 
Objects.nonNull(((BetweenExpression) expression).getLeft()) 
+                && ((BetweenExpression) expression).getLeft() instanceof 
ColumnSegment) {
+            ColumnSegment column = (ColumnSegment) ((BetweenExpression) 
expression).getLeft();
+            return Arrays.asList(column);
+        }
+        return Collections.emptyList();

Review comment:
       @cheese8 Maybe add a param `result` to store the result is better.
   
   ```java
   Collection<ColumnSegment> result = new LinkedList<>();
   ```




-- 
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