raoraoxiong commented on code in PR #28998: URL: https://github.com/apache/flink/pull/28998#discussion_r3925822437
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/utils/PythonCallDeduplicator.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.utils; + +import org.apache.flink.annotation.Internal; +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.Collections; +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 { + + /** + * Flattens the given Python UDF call trees and deduplicates the resulting calls. + * + * <p>Flattening all trees into a single list enables cross-subtree reuse: e.g. in {@code SELECT + * udf1(x), udf2(udf1(x))}, the inner {@code udf1(x)} is evaluated only once and {@code udf2} + * receives its result by reference. + */ + public static PythonCallCseResult deduplicate(List<RexCall> pythonRexCalls) { Review Comment: **Flattening without any sharing.** Agreed, and thanks for pointing at the cost rather than just the shape — the per-record result list is the part that matters. Deduplication is what proves sharing, so if it did not merge any entry the original call trees are now kept and the worker nests them as before. One thing worth noting: my first attempt compared the deduplicated size against the number of projections, which would have skipped `SELECT Det(Det(s)), Det(s)` — a case that does share. Comparing against the flattened size instead keeps that working. -- 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]
