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


##########
core/src/main/java/org/apache/calcite/rel/rules/UnnestDecorrelateRule.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexCorrelVariable;
+import org.apache.calcite.rex.RexFieldAccess;
+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.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static java.util.Objects.requireNonNull;
+
+/** 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) {
+      final RexFieldAccess fa = (RexFieldAccess) projected;
+      final ArrayList<RelDataTypeField> fieldsAccessed = new ArrayList<>();
+      RexNode referenceExpr = fa.getReferenceExpr();
+      fieldsAccessed.add(fa.getField());

Review Comment:
   Why do we need this line of code? Can `while (true)` handle it if set:
   ```
   RexNode referenceExpr = fa
   ```



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