Yash-cor commented on code in PR #34384:
URL: https://github.com/apache/shardingsphere/pull/34384#discussion_r1924987094


##########
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/with/WithSegmentBinder.java:
##########
@@ -71,18 +74,26 @@ public static WithSegment bind(final WithSegment segment, 
final SQLStatementBind
     }
     
     private static SimpleTableSegmentBinderContext 
createWithTableBinderContext(final CommonTableExpressionSegment 
commonTableExpressionSegment) {
-        return new 
SimpleTableSegmentBinderContext(commonTableExpressionSegment.getSubquery().getSelect().getProjections().getProjections());
+        if (commonTableExpressionSegment.getColumns().isEmpty()) {
+            return new 
SimpleTableSegmentBinderContext(commonTableExpressionSegment.getSubquery().getSelect().getProjections().getProjections());
+        } else {
+            Collection<ProjectionSegment> projectionSegments = new 
LinkedList<>();
+            commonTableExpressionSegment.getColumns().forEach(each -> 
projectionSegments.add(new ColumnProjectionSegment(each)));

Review Comment:
   Hello @strongduanmu while changing the logic I found Error in the way 
WithSegment is handling Recursive segment of with clause.
   
   - Recursive segment allows user to use the same common table expression ( 
cte  ) name in cte's  subquery
   Example - 
       ```
        WITH RECURSIVE cte  AS
       (
          SELECT 1 as (n)
          UNION ALL
          SELECT n + 1 FROM cte WHERE n < 5
       )
      SELECT * FROM cte;
      ```
   
      or similarly can be written as
   
      ```
      WITH RECURSIVE cte (n) AS
      (
         SELECT 1
         UNION ALL
         SELECT n + 1 FROM cte WHERE n < 5
      )
      SELECT * FROM cte;
      ```
   
   -  The WITH clause must begin with WITH RECURSIVE if any CTE in the WITH 
clause refers to itself. (If no CTE refers to itself, RECURSIVE is permitted 
but not required.) We have to include a check for it.
   
   - The recursive CTE subquery has two parts, separated by UNION ALL or UNION 
DISTINCT. Thus, a recursive CTE consists of a non-recursive SELECT part 
followed by a recursive SELECT part. Should i include a check for it as this 
check happens when the query hits the MySQL database. 
   
   
   
   



##########
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/with/WithSegmentBinder.java:
##########
@@ -71,18 +74,26 @@ public static WithSegment bind(final WithSegment segment, 
final SQLStatementBind
     }
     
     private static SimpleTableSegmentBinderContext 
createWithTableBinderContext(final CommonTableExpressionSegment 
commonTableExpressionSegment) {
-        return new 
SimpleTableSegmentBinderContext(commonTableExpressionSegment.getSubquery().getSelect().getProjections().getProjections());
+        if (commonTableExpressionSegment.getColumns().isEmpty()) {
+            return new 
SimpleTableSegmentBinderContext(commonTableExpressionSegment.getSubquery().getSelect().getProjections().getProjections());
+        } else {
+            Collection<ProjectionSegment> projectionSegments = new 
LinkedList<>();
+            commonTableExpressionSegment.getColumns().forEach(each -> 
projectionSegments.add(new ColumnProjectionSegment(each)));

Review Comment:
   > Can you move this logic to CommonTableExpressionSegmentBinder?
   
   Along with this should we also move the bindWithColumns to 
CommonTableExpressionBinder.



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