raoraoxiong opened a new pull request, #28638:
URL: https://github.com/apache/flink/pull/28638
## What is the purpose of the change
This pull request implements Common Sub-expression Elimination (CSE) for
Python UDFs in Flink SQL. When a query contains identical deterministic Python
UDF calls (e.g., `SELECT udf1(x), udf1(x)` or `SELECT udf1(x), udf2(udf1(x))`),
the current implementation sends each call independently to the Python worker,
resulting in redundant cross-process (JVM ↔ Python Worker) communication and
computation. This PR deduplicates such calls so that each unique expression is
computed only once, and duplicate positions reference the pre-computed result.
## Brief change log
**Phase 1: Top-level projection deduplication (Commit 1)**
- Modify `CommonExecPythonCalc` to deduplicate identical deterministic
Python UDF calls in the projection list at the ExecNode level
- Append a ref-reuse projection operator to expand deduplicated results back
to the expected output schema
- Add `ProjectionCodeGenerator.generateProjectionOperator` for the expansion
projection
**Phase 2: Full-tree CSE and condition-projection deduplication (Commit 2)**
- Add `PythonCallDeduplicator` and `PythonCallCseResult` for structural
deduplication with nested sub-expression flattening and reference maps
- Add `PythonFunctionInfo.ResultRef` for referencing pre-computed UDF
results by index
- Extend protobuf `Input` message with `refIndex` field
- Modify Python worker (`operations.py`) to support sequential execution
with result references
- Add `RemoteCalcConditionProjectionCseRule` optimizer rule to deduplicate
Python UDF calls shared between WHERE conditions and SELECT projections
- Register the new rule in stream and batch rule sets
## Verifying this change
This change added tests and can be verified as follows:
- Added `CommonExecPythonCalcRefReuseTest` (Java unit test) to verify
deduplication logic, ref-map resolution, and detail name generation with
parameterized test cases
- Added `PythonCalcConditionCseTest` (Scala plan test) to verify the
optimizer rule produces correct plans for condition-projection CSE scenarios
- Added `test_scalar_function_cse.py` (Python unit test) to verify the
sequential execution codegen path (with refIndex) and the traditional lambda
path (without refIndex)
- Added integration tests in `test_udf.py` to verify end-to-end correctness
of CSE with real Python UDFs
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): no
- The public API, i.e., is any changed class annotated with
`@Public(Evolving)`: no
- The serializers: no
- The runtime per-record code paths (performance sensitive): no
- Anything that affects deployment or recovery: JobManager (and its
components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
- The S3 file system connector: no
## Documentation
- Does this pull request introduce a new feature? yes
- If yes, how is the feature documented? JavaDocs
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes — Claude-4.6-Opus
<!--
Generated-by: Claude-4.6-Opus
-->
--
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]