raoraoxiong commented on code in PR #28998:
URL: https://github.com/apache/flink/pull/28998#discussion_r3925848008


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/utils/PythonCallCseResult.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.calcite.rex.RexCall;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/** Encapsulates the result of Python UDF call Common Sub-expression 
Elimination (CSE). */
+@Internal
+public class PythonCallCseResult {
+
+    /**
+     * The deduplicated Python UDF calls to be evaluated, in execution order.
+     *
+     * <p>Nested call trees are flattened so that a sub-expression shared 
between calls appears
+     * exactly once. For example {@code SELECT udf1(x), udf2(udf1(x))} yields 
{@code [udf1(x),
+     * udf2(<ref to udf1(x)>)]}.
+     */
+    private final List<RexCall> deduplicatedCalls;
+
+    /**
+     * For each projection entry, the position in {@link #deduplicatedCalls} 
holding its result.
+     *
+     * <p>Flattening appends intermediate sub-expressions that must not be 
emitted, and post-order
+     * can leave a projected result before the end of the list, so this is not 
necessarily the
+     * identity.
+     */
+    private final int[] outputIndices;
+
+    /**
+     * Maps a call to the position in {@link #deduplicatedCalls} where its 
result is computed.
+     *
+     * <p>Derived from {@link #deduplicatedCalls}: since the list is already 
deduplicated, the first
+     * occurrence of a structurally equal call is the one that computes it.
+     */
+    private final Map<RexCall, Integer> refMap;
+
+    public PythonCallCseResult(List<RexCall> deduplicatedCalls, int[] 
outputIndices) {
+        this.deduplicatedCalls = deduplicatedCalls;
+        this.outputIndices = outputIndices;
+        this.refMap = buildRefMap(deduplicatedCalls);
+    }
+
+    public List<RexCall> getDeduplicatedCalls() {
+        return deduplicatedCalls;
+    }
+
+    public int[] getOutputIndices() {
+        return outputIndices;
+    }
+
+    public Map<RexCall, Integer> getRefMap() {
+        return refMap;
+    }
+
+    /**
+     * Indexes the deduplicated calls by structural equality, keeping the 
first occurrence so that a
+     * parent references its own child rather than a later structurally equal 
duplicate.
+     */
+    private static Map<RexCall, Integer> buildRefMap(List<RexCall> 
deduplicatedCalls) {

Review Comment:
   **Non-deterministic calls could be referenced.** Fixed. The guard was in the 
wrong
   place: `callToIndex` checks determinism and correctly keeps two `Nondet(s)` 
entries
   apart, but `refMap` indexed every entry unconditionally. Not flattening
   non-deterministic children was not sufficient — the child never becomes an 
entry of
   the evaluated list, yet it was still looked up in `refMap` and found the
   structurally equal top-level entry to point at. `refMap` now only indexes
   deterministic calls. Added an IT case for this shape, which fails without 
the fix.



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