silundong commented on code in PR #4691:
URL: https://github.com/apache/calcite/pull/4691#discussion_r2633191002


##########
core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java:
##########
@@ -634,7 +634,23 @@ public TrimResult trimFields(
     RelOptUtil.InputFinder inputFinder =
         new RelOptUtil.InputFinder(inputExtraFields, fieldsUsed);
     conditionExpr.accept(inputFinder);
-    final ImmutableBitSet inputFieldsUsed = inputFinder.build();
+
+    // Collect all the SubQueries in the filter condition.
+    List<RexSubQuery> subQueries = RexUtil.SubQueryCollector.collect(filter);
+    // Get all the correlationIds present in the SubQueries
+    Set<CorrelationId> correlationIds = 
RelOptUtil.getVariablesUsed(subQueries);
+    ImmutableBitSet requiredColumns = ImmutableBitSet.of();
+    if (!correlationIds.isEmpty()) {
+      // Correlation columns are also needed by SubQueries, so add them to 
inputFieldsUsed.
+      requiredColumns = 
RelOptUtil.correlationColumns(correlationIds.iterator().next(), filter);

Review Comment:
   > variablesSet should include cor0 and cor1.
   
   In my opinion, this plan is correct. `cor1` represents a variable from the 
input of LogicalFilter, so it is added to variablesSet. `cor0` represents the 
variable from the left side of LogicalCorrelate, which LogicalCorrelate has 
already recorded. `cor0` and `cor1` belong to different scopes. Adding `cor0` 
to the variablesSet of LogicalFilter would cause errors in removing subqueries 
and decorrelation.



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