Dennis-Mircea opened a new pull request, #28091:
URL: https://github.com/apache/flink/pull/28091
<!--
*Thank you very much for contributing to Apache Flink - we are happy that
you want to help us improve Flink. To help the community review your
contribution in the best possible way, please go through the checklist below,
which will get the contribution into a shape in which it can be best reviewed.*
*Please understand that we do not do this to make contributions to Flink a
hassle. In order to uphold a high standard of quality for code contributions,
while at the same time managing a large number of contributions, we need
contributors to prepare the contributions well, and give reviewers enough
contextual information for the review. Please also understand that
contributions that do not follow this guide will take longer to review and thus
typically be picked up with lower priority by the community.*
## Contribution Checklist
- Make sure that the pull request corresponds to a [JIRA
issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are
made for typos in JavaDoc or documentation files, which need no JIRA issue.
- Name the pull request in the form "[FLINK-XXXX] [component] Title of the
pull request", where *FLINK-XXXX* should be replaced by the actual issue
number. Skip *component* if you are unsure about which is the best component.
Typo fixes that have no associated JIRA issue should be named following
this pattern: `[hotfix] [docs] Fix typo in event time introduction` or
`[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
- Fill out the template below to describe the changes contributed by the
pull request. That will give reviewers the context they need to do the review.
- Make sure that the change passes the automated tests, i.e., `mvn clean
verify` passes. You can set up Azure Pipelines CI to do that following [this
guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
- Each pull request should address only one issue, not mix up code from
multiple issues.
- Each commit in the pull request has a meaningful commit message
(including the JIRA id)
- Once all items of the checklist are addressed, remove the above text and
this checklist, leaving only the filled out template below.
**(The sections below can be removed for hotfixes of typos)**
-->
## What is the purpose of the change
This pull request resolves
[FLINK-14621](https://issues.apache.org/jira/browse/FLINK-14621) by stopping
the planner from generating a `WatermarkAssigner` operator when no downstream
operator depends on watermarks at runtime.
Until now, every `WATERMARK FOR …` declaration in DDL produced a runtime
`WatermarkAssigner` regardless of whether anything downstream actually consumed
the watermark (e.g. window aggregates, event-time interval / temporal joins,
event-time temporal sort, `CURRENT_WATERMARK()`). The assigner is harmless but
wasteful: it adds a per-record operator on the hot path and complicates the
plan. After this change the assigner is dropped from the physical plan whenever
it is provably unused, while remaining present on every plan that does need it.
The remove pass is implemented as a HEP rule that runs in the
`PHYSICAL_REWRITE` phase. The rule walks the subtree from the sink towards the
sources, drops every redundant `StreamPhysicalWatermarkAssigner`, and rewrites
the operators on the path between the former assigner and the sink so that the
time-indicator (`*ROWTIME*`) marker on the watermark column is demoted back to
a plain `TIMESTAMP` where appropriate.
## Brief change log
- **New rule `RedundantWatermarkAssignerRemoveRule`**
(`flink-table-planner`, package `plan.rules.physical.stream`):
- Anchored on `StreamPhysicalSink`. Walks the subtree from the sink and
drops `StreamPhysicalWatermarkAssigner` nodes whose watermarks are not consumed
by any operator on the path back to the sink.
- Treats the following as watermark consumers:
- any `StreamPhysicalRel` that returns `requireWatermark() == true`
(window aggregates, event-time interval/temporal joins, event-time
match-recognize, PTFs, …);
- any rel hosting a `CURRENT_WATERMARK(…)` SQL call in its expressions
(the function reads watermarks at runtime through the operator context);
- `StreamPhysicalTemporalSort` whose primary sort key is a rowtime
time-indicator (its runtime operator is `RowTimeSortOperator`, which fires on
watermarks even though it does not formally implement `requireWatermark`).
- When the assigner is dropped, parent `StreamPhysicalCalc` nodes are
rebuilt through `RexTimeIndicatorMaterializer` so that input refs/calls that
pointed at the now-demoted column are retyped; pass-through rels (Exchange,
Union, ChangelogNormalize, Sink, …) are recreated via `node.copy(traits,
[newInput])`.
- The rule conservatively bails out when an event-time `*ROWTIME*`
time-indicator survives in the sink's row type (`sinkConsumesRowtime`). That
signals the sink consumes a rowtime attribute as a watermark-bearing column
(e.g. for forwarding to an external system, or because the sink table itself
was declared with `WATERMARK FOR …`). In that case the watermark must remain
available at runtime.
- **Extracted `RexTimeIndicatorMaterializer`** out of
`RelTimeIndicatorConverter` into a top-level public class so it can be reused
by the new rule.
- **Wired the rule into the optimizer**: added a HEP program `remove
redundant watermarks` after `PHYSICAL_REWRITE` in `FlinkStreamProgram`, and a
new `REMOVE_REDUNDANT_WATERMARK_RULES` set in `FlinkStreamRuleSets`.
- **`MiniBatchIntervalInferRule`**: removed the now-unreachable
`MiniBatchMode.ProcTime` branch from the `StreamPhysicalWatermarkAssigner` case
(any surviving assigner is necessarily rowtime-driven), reformatted the class
JavaDoc into a proper ordered list, and ported the rule to Java to drop the
last Scala source under this package.
- **Tests**:
- New `RedundantWatermarkAssignerRemoveRuleTest` (8 cases): simple SELECT,
Calc chain, Tumble window, event-time interval join, mixed UNION,
sink-consumes-rowtime guard, and `CURRENT_WATERMARK` in projection / filter.
- `MiniBatchIntervalInferTest.testRedundantWatermarkDefinition`,
`WindowTableFunctionTest.testProctimeWindowTVFWithMiniBatch`,
`AggregateTest.testAggWithMiniBatch` were updated to anchor a sink (so the new
rule fires) and their goldens regenerated to reflect the post-FLINK-14621 plan
(assigner removed; mini-batch `ProcTime` assigner sits directly above the table
source scan).
- Goldens regenerated for `DuplicateChangesInferRuleTest`,
`DeltaJoinTest`, `NonDeterministicDagTest`, and the seven
`Python*GroupWindowAggregate` / `Python*OverAggregate` JSON plan tests where
the redundant assigner was previously printed.
## Verifying this change
This change added tests and can be verified as follows:
- New `RedundantWatermarkAssignerRemoveRuleTest` covers the rule's positive
cases (assigner removed under simple Calc, Calc chain, mixed UNION) and
negative cases (assigner kept for window aggregations, event-time interval
joins, sinks that consume a rowtime column, and queries that reference
`CURRENT_WATERMARK`).
- All updated goldens were re-generated and the diffs reviewed; they are
uniformly "redundant `WatermarkAssigner` removed; affected `*ROWTIME*` columns
demoted to `TIMESTAMP`".
- The full `flink-table-planner` test suite (10 685 tests) passes locally
with no failures introduced by this change. Targeted runs over `*MiniBatch*`,
`*Watermark*` and the `CalcITCase#testCurrentWatermark*` integration tests pass.
- `./mvnw spotless:apply` and `./mvnw checkstyle:check -T1C` are clean for
the touched module.
## 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): yes - the
change *removes* a per-record operator (`WatermarkAssigner`) from plans that
don't need it; existing plans that still need watermarks are unaffected.
- 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? no - it is a planner
optimization with no user-visible API or configuration change. No documentation
updates required.
- If yes, how is the feature documented? not applicable
---
##### Was generative AI tooling used to co-author this PR?
<!--
If generative AI tooling has been used in the process of authoring this PR,
please
change the checkbox below to `[X]` followed by the name of the tool, and
uncomment the
"Generated-by" line. See the ASF Generative Tooling Guidance for details:
https://www.apache.org/legal/generative-tooling.html
You are responsible for the quality and correctness of every change in this
PR
regardless of the tooling used. Low-effort AI-generated PRs will be closed.
See
AGENTS.md for the full guidance.
-->
- [X] Yes (please specify the tool below)
<!--
Generated-by: [GitHub Copilot]
-->
--
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]