raoraoxiong commented on code in PR #28998: URL: https://github.com/apache/flink/pull/28998#discussion_r3920874092
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/common/PythonCallDeduplicator.java: ########## @@ -0,0 +1,128 @@ +/* + * 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.common; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.table.planner.plan.utils.PythonUtil; +import org.apache.flink.table.planner.utils.ShortcutUtils; + +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexNode; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +/** + * Utility for Python UDF Common Sub-expression Elimination (CSE) of nested calls. + * + * <p>Nested Python UDF call trees are flattened in post-order and deduplicated by structural + * equivalence, so that a sub-expression shared between calls is evaluated only once by the Python + * worker. Duplicates between whole projection entries are already removed in the planner by {@code + * RemoteCalcProjectionCseRule}, so this only concerns sub-expressions. + */ +@Internal +public class PythonCallDeduplicator { + + /** + * Recursively collects all deterministic Python UDF calls from a call tree in DFS post-order. + * + * <p>Post-order ensures child results are computed before parents that reference them via + * refIndex. Non-deterministic children are NOT flattened to prevent incorrect sharing. + */ + @VisibleForTesting + static List<RexCall> collectAllPythonUdfCalls(RexCall root) { Review Comment: `PythonCallDeduplicator` and `PythonCallCseResult` now live in `plan/nodes/exec/utils`, next to `CommonPythonUtil`, along with the invariant test. -- 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]
