gortiz commented on code in PR #13733: URL: https://github.com/apache/pinot/pull/13733#discussion_r1752092256
########## 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> { Review Comment: It is similar to the other visitor I've converted. The fact that the implementation uses a visitor is an implementation detail we don't need to leak into the caller. Imagine the case you mention. Would you be able to know if you can reuse the same visitor in two different calls? Why would you need to call builder.build() in your code? What is easier to read? ```java Builder builder = ...; PlanNode planNode = ...; RelNode relNode = PlanNodeToRelConverter.convert(builder, planNode); ``` or ```java Builder builder = ...; PlanNode planNode = ...; PlanNodeToRelConverter visitor = new PlanNodeToRelConverter.convert(builder, planNode); planNode.visit(visitor, null); RelNode relNode = builder.build(); ``` Also... the `convert` method has no javadoc but in case we have to add it, it is simpler to add it there. I love visitors (although I would love even more to be able to use sealed classes) but they are a poor man switch. Switches are great tools to use in your functions, but in Java we offer methods to be called, not switches. -- 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