antonkw commented on code in PR #4177:
URL: https://github.com/apache/calcite/pull/4177#discussion_r1949458362


##########
core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java:
##########
@@ -456,7 +458,10 @@ public Result visit(Filter e) {
               builder.context.toSql(null, e.getCondition())));
       return builder.result();
     } else {
-      final Result x = visitInput(e, 0, Clause.WHERE);
+      Result x = visitInput(e, 0, Clause.WHERE);
+      if (e.getCondition().getKind() == NOT || e.getCondition().getKind() == 
EXISTS) {

Review Comment:
   @suibianwanwank 
   Can you please elaborate on what you suspected?
   
   I added the predicate you mentioned:
   ```java
     @Test void testAntiJoinWithComplexInput() {
       final String sql = "SELECT * FROM "
           + "(select * from ("
           + "select e1.\"product_id\" FROM \"foodmart\".\"product\" e1 "
           + "LEFT JOIN \"foodmart\".\"product\" e3 "
           + "on e1.\"product_id\" = e3.\"product_id\""
           + ")"
           + ") selected where not exists\n"
           + "(select 1 from \"foodmart\".\"product\" e2 "
           + "where e2.\"product_id\" = selected.\"product_id\" and 
e2.\"product_id\" > 10)";
       final String expected =
           "SELECT *\nFROM (SELECT \"product\".\"product_id\"\nFROM 
\"foodmart\".\"product\"\n"
               + "LEFT JOIN \"foodmart\".\"product\" AS \"product0\" "
               + "ON \"product\".\"product_id\" = \"product0\".\"product_id\") 
AS \"t\"\n"
               + "WHERE NOT EXISTS ("
               + "SELECT *\nFROM \"foodmart\".\"product\"\n"
               + "WHERE \"product_id\" = \"t\".\"product_id\" AND 
\"product_id\" > 10"
               + ")";
       sql(sql).ok(expected);
     }
   ```
   
   Without escaping mess:
   ```sql
   SELECT * FROM 
     foodmart.product 
   WHERE 
     product_id = t.product_id AND product_id > 10
   ```
   
   Seems good to me.
   
   >  How about resetting on parseCorrelTable?
   
   Feels unsafe to me. Particularly due to fact that such aliases are not 
legitimate in all cases(why we should represent and maintain state that isn't 
reflecting possible query). 
   I can try your suggestion to see what tests think about it :)



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