vvysotskyi commented on a change in pull request #1441: DRILL-6703: Query with 
complex expressions in lateral and unnest fails with CannotPlanException
URL: https://github.com/apache/drill/pull/1441#discussion_r212394250
 
 

 ##########
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ComplexUnnestVisitor.java
 ##########
 @@ -0,0 +1,195 @@
+/*
+ * 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.drill.exec.planner.sql.handlers;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelShuttleImpl;
+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.LogicalCorrelate;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexFieldAccess;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.drill.exec.planner.logical.DrillRelFactories;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Visitor that moves non-{@link RexFieldAccess} rex node from project below 
{@link Uncollect}
+ * to the left side of the {@link Correlate}.
+ */
+public class ComplexUnnestVisitor extends RelShuttleImpl {
+  private static final String COMPLEX_FIELD_NAME = "$COMPLEX_FIELD_NAME";
+
+  private final Map<CorrelationId, RelNode> leftInputs = new HashMap<>();
+  private final Map<CorrelationId, CorrelationId> updatedCorrelationIds = new 
HashMap<>();
+
+  private ComplexUnnestVisitor() {
+  }
+
+  @Override
+  public RelNode visit(LogicalCorrelate correlate) {
+    RelNode left = correlate.getLeft().accept(this);
+    leftInputs.put(correlate.getCorrelationId(), left);
+
+    RelNode right = correlate.getRight().accept(this);
+    // if right input wasn't changed or left input wasn't changed
+    // after rewriting right input, no need to create Correlate with new 
CorrelationId
+    if (correlate.getRight() == right
+        || left == leftInputs.get(correlate.getCorrelationId())) {
+      if (correlate.getLeft() == left) {
+        return correlate;
+      }
+      // changed only inputs, but CorrelationId left the same
+      return correlate.copy(correlate.getTraitSet(), Arrays.asList(left, 
right));
+    }
+
+    Correlate newCorrelate = correlate.copy(correlate.getTraitSet(),
+        leftInputs.get(correlate.getCorrelationId()), right,
+        updatedCorrelationIds.get(correlate.getCorrelationId()),
+        ImmutableBitSet.of(left.getRowType().getFieldCount()), 
correlate.getJoinType());
+
+    RelBuilder builder = 
DrillRelFactories.LOGICAL_BUILDER.create(correlate.getCluster(), null);
+    builder.push(newCorrelate);
+
+    List<RexNode> topProjectExpressions = 
left.getRowType().getFieldList().stream()
+        .map(field -> builder.getRexBuilder().makeInputRef(left, 
field.getIndex()))
+        .collect(Collectors.toList());
+
+    switch (correlate.getJoinType()) {
+      case LEFT:
+      case INNER:
+        // adds field from the right input of correlate to the top project
+        topProjectExpressions.add(
+            builder.getRexBuilder().makeInputRef(newCorrelate, 
topProjectExpressions.size() + 1));
+        // fall through
+      case ANTI:
+      case SEMI:
+        builder.project(topProjectExpressions, 
correlate.getRowType().getFieldNames());
+    }
+    return builder.build();
+  }
+
+  @Override
+  public RelNode visit(RelNode other) {
+    if (other instanceof Uncollect) {
+      return visit((Uncollect) other);
+    }
+    return super.visit(other);
+  }
+
+  public RelNode visit(Uncollect uncollect) {
+    RelBuilder builder = 
DrillRelFactories.LOGICAL_BUILDER.create(uncollect.getCluster(), null);
+    RexBuilder rexBuilder = builder.getRexBuilder();
+
+    assert uncollect.getInput() instanceof Project : "Uncollect should have 
Project input";
 
 Review comment:
   In the case, if at this point we have something that differs from Project, 
then something went wrong.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to