gortiz commented on code in PR #13733:
URL: https://github.com/apache/pinot/pull/13733#discussion_r1752074374


##########
pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/PlanNodeToRelConverter.java:
##########
@@ -0,0 +1,468 @@
+/**
+ * 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.pinot.query.planner.logical;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelDistribution;
+import org.apache.calcite.rel.RelDistributions;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.SetOp;
+import org.apache.calcite.rel.core.Window;
+import org.apache.calcite.rel.logical.LogicalIntersect;
+import org.apache.calcite.rel.logical.LogicalMinus;
+import org.apache.calcite.rel.logical.LogicalSort;
+import org.apache.calcite.rel.logical.LogicalUnion;
+import org.apache.calcite.rel.logical.LogicalWindow;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexWindowBound;
+import org.apache.calcite.rex.RexWindowBounds;
+import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.pinot.common.utils.DatabaseUtils;
+import org.apache.pinot.core.operator.ExplainAttributeBuilder;
+import org.apache.pinot.core.plan.PinotExplainedRelNode;
+import org.apache.pinot.query.planner.plannode.AggregateNode;
+import org.apache.pinot.query.planner.plannode.ExchangeNode;
+import org.apache.pinot.query.planner.plannode.ExplainedNode;
+import org.apache.pinot.query.planner.plannode.FilterNode;
+import org.apache.pinot.query.planner.plannode.JoinNode;
+import org.apache.pinot.query.planner.plannode.MailboxReceiveNode;
+import org.apache.pinot.query.planner.plannode.MailboxSendNode;
+import org.apache.pinot.query.planner.plannode.PlanNode;
+import org.apache.pinot.query.planner.plannode.PlanNodeVisitor;
+import org.apache.pinot.query.planner.plannode.ProjectNode;
+import org.apache.pinot.query.planner.plannode.SetOpNode;
+import org.apache.pinot.query.planner.plannode.SortNode;
+import org.apache.pinot.query.planner.plannode.TableScanNode;
+import org.apache.pinot.query.planner.plannode.ValueNode;
+import org.apache.pinot.query.planner.plannode.WindowNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Converts a {@link PlanNode} into a {@link RelNode}.
+ *
+ * This class is used to convert serialized plan nodes into RelNodes so they 
can be used when explain with
+ * implementation is requested. Therefore some nodes may be transformed in a 
way that loses information that is
+ * required to create an actual executable plan but not necessary in order to 
describe the plan.
+ */
+public final class PlanNodeToRelConverter {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PlanNodeToRelConverter.class);
+
+  private PlanNodeToRelConverter() {
+  }
+
+  public static RelNode convert(RelBuilder builder, PlanNode planNode) {
+    ConverterVisitor visitor = new ConverterVisitor(builder);
+    planNode.visit(visitor, null);
+
+    return visitor.build();
+  }
+
+  private static class ConverterVisitor implements PlanNodeVisitor<Void, Void> 
{
+    private final RelBuilder _builder;
+
+    public ConverterVisitor(RelBuilder builder) {
+      _builder = builder;
+    }
+
+    private void visitChildren(PlanNode node) {
+      node.getInputs().forEach(input -> input.visit(this, null));
+    }
+
+    @Override
+    public Void visitAggregate(AggregateNode node, Void context) {
+      visitChildren(node);
+
+      try {
+        int[] groupKeyArr = 
node.getGroupKeys().stream().mapToInt(Integer::intValue).toArray();
+        RelBuilder.GroupKey groupKey = _builder.groupKey(groupKeyArr);
+
+        List<RelBuilder.AggCall> aggCalls =
+            node.getAggCalls().stream().map(functionCall -> 
RexExpressionUtils.toAggCall(_builder, functionCall))
+                .collect(Collectors.toList());
+
+        _builder.aggregate(groupKey, aggCalls);
+      } catch (RuntimeException e) {
+        LOGGER.warn("Failed to convert aggregate node: {}", node, e);
+        _builder.push(new PinotExplainedRelNode(_builder.getCluster(), 
"UnknownAggregate", Collections.emptyMap(),
+            node.getDataSchema(), readAlreadyPushedChildren(node)));
+      }

Review Comment:
   Given the builder is quite complex, some subtle conditions may produce this 
error. Given the class javadoc explicitly that the generated tree should be 
used in explains because semantics may be lost, I think it is fair to return 
this synthetic node. In the end what we are trying to give users is something 
like:
   
   ```
   Execution Plan
   LogicalSort(offset=[0], fetch=[10])
     PinotLogicalSortExchange(distribution=[hash], collation=[[]], 
isSortOnSender=[false], isSortOnReceiver=[false])
       UnknownAggregate
         LogicalJoin(condition=[=($3, $5)], joinType=[inner])
           ...
   ```
   
   Instead of an error. Being optimistic, I think this will be more useful than 
just having a blind error.



-- 
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: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to