dssysolyatin commented on code in PR #4709:
URL: https://github.com/apache/calcite/pull/4709#discussion_r2667444706


##########
core/src/main/java/org/apache/calcite/rel/rules/UnnestDecorrelateRule.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Correlate;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Uncollect;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexCorrelVariable;
+import org.apache.calcite.rex.RexFieldAccess;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import org.immutables.value.Value;
+
+import java.util.List;
+
+/** Convert representations of a projected Unnest that use LogicalCorrelate 
into
+ * simple Unnest representations.
+ *
+ * <p>Original plan:
+ * LogicalProject // only uses rightmost columns of correlate, outerProject
+ *   LogicalCorrelate(correlation=[$cor0], joinType=[inner], 
requiredColumns=[{...}])
+ *     LeftSubquery
+ *     LogicalProject (optional; innerProject)
+ *       Uncollect
+ *         LogicalProject(COL=[$cor0.ARRAY])
+ *           LogicalValues(tuples=[[{ 0 }]])
+ *
+ * <p>is converted to
+ *
+ * <p>Resulting plan:
+ * LogicalProject
+ *   LogicalProject (optional)
+ *     Uncollect
+ *       LogicalProject
+ *         LeftSubquery
+ */
[email protected]
+public class UnnestDecorrelateRule extends 
RelRule<UnnestDecorrelateRule.Config>
+    implements TransformationRule {
+
+  protected UnnestDecorrelateRule(UnnestDecorrelateRule.Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    Project outerProject = call.rel(0);
+    Correlate cor = call.rel(1);
+    CorrelationId corId = cor.getCorrelationId();
+
+    RelNode left = call.rel(2);
+    int leftCount = left.getRowType().getFieldCount();
+    ImmutableBitSet used = 
RelOptUtil.InputFinder.bits(outerProject.getProjects(), null);
+    int firstUsed = used.nextSetBit(0);
+    if (firstUsed != -1 && firstUsed < leftCount) {
+      return;
+    }
+
+    int uncollectIndex = 3;
+    Project innerProject = null;
+    if (call.rel(uncollectIndex) instanceof Project) {
+      innerProject = call.rel(3);
+      uncollectIndex = 4;
+    }
+
+    Uncollect uncollect = call.rel(uncollectIndex);
+    Project project = call.rel(uncollectIndex + 1);
+
+    List<RexNode> projects = project.getProjects();
+    if (projects.size() != 1) {
+      return;
+    }
+
+    RexNode projected = projects.get(0);
+    if (projected instanceof RexFieldAccess) {

Review Comment:
   Nit: I would suggest to use recursion in this place instead of while. Java 
does not support tail recursion optiomization, but recursion should not be deep:
   
   ```
   private boolean processReferenceExpr(RexNode referenceExpr, CorrelationId 
corId,
                                           List<RelDataTypeField> 
fieldsAccessed) {
         if (referenceExpr instanceof RexCorrelVariable) {
             RexCorrelVariable cv = (RexCorrelVariable) referenceExpr;
             // Wrong variable accessed; bail out
             // Otherwise continue converting
             return cv.id == corId;
         } else if (referenceExpr instanceof RexFieldAccess) {
             // Notice that we are peeling off fields in reverse order, 
starting with the last
             RexFieldAccess fieldAccess = (RexFieldAccess) referenceExpr;
             fieldsAccessed.add(fieldAccess.getField());
             return processReferenceExpr(fieldAccess.getReferenceExpr(), corId, 
fieldsAccessed);
         } else {
             return false;
         }
     }
     
     if (!processReferenceExpr(....)) {
        return;
     }
   ```



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