SinBex commented on code in PR #25578:
URL: https://github.com/apache/flink/pull/25578#discussion_r1897888785


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/processor/AdaptiveJoinProcessor.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * 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.flink.table.planner.plan.nodes.exec.processor;
+
+import org.apache.flink.configuration.JobManagerOptions;
+import org.apache.flink.table.api.TableConfig;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.config.OptimizerConfigOptions;
+import org.apache.flink.table.planner.plan.nodes.exec.AdaptiveJoinExecNode;
+import org.apache.flink.table.planner.plan.nodes.exec.ExecEdge;
+import org.apache.flink.table.planner.plan.nodes.exec.ExecNode;
+import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeGraph;
+import org.apache.flink.table.planner.plan.nodes.exec.InputProperty;
+import 
org.apache.flink.table.planner.plan.nodes.exec.InputProperty.DistributionType;
+import 
org.apache.flink.table.planner.plan.nodes.exec.batch.BatchExecAdaptiveJoin;
+import org.apache.flink.table.planner.plan.nodes.exec.batch.BatchExecExchange;
+import org.apache.flink.table.planner.plan.nodes.exec.stream.StreamExecNode;
+import 
org.apache.flink.table.planner.plan.nodes.exec.visitor.AbstractExecNodeExactlyOnceVisitor;
+import org.apache.flink.table.planner.plan.utils.OperatorType;
+import org.apache.flink.table.planner.utils.TableConfigUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static 
org.apache.flink.table.planner.plan.nodes.exec.InputProperty.DistributionType.KEEP_INPUT_AS_IS;
+
+/**
+ * A {@link ExecNodeGraphProcessor} which replace the qualified join nodes 
into adaptive broadcast
+ * join nodes.
+ */
+public class AdaptiveJoinProcessor implements ExecNodeGraphProcessor {
+
+    @Override
+    public ExecNodeGraph process(ExecNodeGraph execGraph, ProcessorContext 
context) {
+        if (execGraph.getRootNodes().get(0) instanceof StreamExecNode) {
+            throw new TableException("StreamExecNode is not supported yet");
+        }
+        if (!isAdaptiveJoinEnabled(context)) {
+            return execGraph;
+        }
+
+        AbstractExecNodeExactlyOnceVisitor visitor =
+                new AbstractExecNodeExactlyOnceVisitor() {
+                    @Override
+                    protected void visitNode(ExecNode<?> node) {
+                        visitInputs(node);
+                        if 
(checkKeepInputAsIsExisted(node.getInputProperties())) {
+                            return;
+                        }
+                        for (int i = 0; i < node.getInputEdges().size(); ++i) {
+                            ExecEdge edge = node.getInputEdges().get(i);
+                            ExecNode<?> newNode = 
replaceAdaptiveJoinNode(edge.getSource());
+                            node.replaceInputEdge(
+                                    i,
+                                    ExecEdge.builder()
+                                            .source(newNode)
+                                            .target(node)
+                                            .shuffle(edge.getShuffle())
+                                            
.exchangeMode(edge.getExchangeMode())
+                                            .build());
+                        }
+                    }
+                };
+
+        List<ExecNode<?>> newRootNodes =
+                execGraph.getRootNodes().stream()
+                        .map(
+                                node -> {
+                                    node = replaceAdaptiveJoinNode(node);
+                                    node.accept(visitor);
+                                    return node;
+                                })
+                        .collect(Collectors.toList());
+
+        return new ExecNodeGraph(execGraph.getFlinkVersion(), newRootNodes);
+    }
+
+    private ExecNode<?> replaceAdaptiveJoinNode(ExecNode<?> node) {
+        if (!(checkAllInputShuffleIsHash(node))
+                || isUpstreamNodeKeepInputAsIs(node.getInputEdges())) {
+            return node;
+        }
+        ExecNode<?> newNode = node;
+        if (node instanceof AdaptiveJoinExecNode
+                && ((AdaptiveJoinExecNode) 
node).canBeTransformedToAdaptiveJoin()) {
+            BatchExecAdaptiveJoin adaptiveJoin = ((AdaptiveJoinExecNode) 
node).toAdaptiveJoinNode();
+            replaceInputEdge(adaptiveJoin, node);
+            newNode = adaptiveJoin;
+        }
+
+        return newNode;
+    }
+
+    private boolean checkKeepInputAsIsExisted(List<InputProperty> 
inputProperties) {
+        return inputProperties.stream()
+                .anyMatch(
+                        inputProperty ->
+                                
inputProperty.getRequiredDistribution().getType()
+                                        == KEEP_INPUT_AS_IS);
+    }
+
+    private boolean isUpstreamNodeKeepInputAsIs(List<ExecEdge> inputEdges) {
+        return inputEdges.stream()
+                .filter(execEdge -> execEdge.getSource() instanceof 
BatchExecExchange)

Review Comment:
   Because KEEP_INPUT_AS_IS is achieved by inserting BatchExecExchange, so we 
should skip a BatchExecExchange to get the real 'KEEP_INPUT_AS_IS' node.



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