[FLINK-38579] [table-planner] Fix incorrect UB drop for filter on non-upsert key
Reopens #27371 (auto-closed by stale-bot)
What is the purpose
Linked issue: close https://issues.apache.org/jira/browse/FLINK-38579
This pull request fixes incorrect changelog mode inference when filters or non-equi join conditions are pushed down on non-upsert key columns. Without this fix, Flink incorrectly drops UPDATE_BEFORE events in these scenarios, leading to phantom rows in the output.
Problem: When a filter like c < 2 (where c is a non-upsert key column) is pushed down to a changelog source, and a row with c=1 is updated to c=2, the old row c=1 matches the filter but the new row c=2 doesn't. The planner was incorrectly allowing DropUpdateBefore optimization, which caused the UPDATE_BEFORE event to be lost. Without the UPDATE_BEFORE, downstream operators couldn't retract the old c=1 row, leaving it incorrectly in the result.
Solution: The fix prevents ONLY_UPDATE_AFTER and DELETE_BY_KEY changelog mode when filters or non-equi join conditions reference non-upsert key columns, ensuring UPDATE_BEFORE events are preserved for correct retraction semantics.
Brief change log
- Added
referencesNonUpsertKeyColumns()helper to check if RexNodes reference non-upsert key columns - Added
hasNonUpsertKeyFilterPushedDown()to detect filters on non-upsert keys in TableSourceScan - Added
hasNonUpsertKeyNonEquiCondition()to detect non-equi join conditions on non-upsert keys, with precise left/right input analysis - Modified
SatisfyUpdateKindTraitVisitorto rejectONLY_UPDATE_AFTERfor TableSourceScan when filter references non-upsert keys - Modified
SatisfyUpdateKindTraitVisitorto rejectONLY_UPDATE_AFTERfor StreamPhysicalJoin when non-equi condition references non-upsert keys - Added comprehensive IT tests covering filter pushdown and join scenarios
Verifying this change
This change added tests and can be verified as follows:
- Added
ChangelogSourceITCase.testFilterPushedDownOnNonUpsertKey()- Tests filter pushed down on non-upsert key column with UPDATE_BEFORE preservation (7 parameterized configurations, 6 pass, 1 skipped for incompatible CDC duplicate + MiniBatch) - Added
ChangelogSourceITCase.testJoinWithNonEquivConditionOnNonUpsertKey()- Tests non-equi join condition on left side non-upsert key column - Added
ChangelogSourceITCase.testJoinWithNonEquivConditionOnRightNonUpsertKey()- Tests non-equi join condition on right side non-upsert key column to validate left/right split logic - All tests verify that without the fix, wrong results occur (phantom rows), and with the fix, correct empty results are produced
- Tests use changelog data with INSERT, UPDATE_BEFORE, UPDATE_AFTER events to simulate real CDC scenarios
Note:
The CDC duplicate + MiniBatch configuration correctly fails to generate an execution plan for filter pushdown with this fix. In this case, the filter is evaluated at the source, where ChangelogNormalize enforces ONLY_UPDATE_AFTER to deduplicate updates, while filtering on a non-upsert key requires UPDATE_BEFORE for correctness. These requirements conflict at the source level, so the planner correctly rejects this configuration.
Join non-equi conditions are evaluated downstream in the join operator, so they do not conflict with ChangelogNormalize.
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 (only affects plan optimization phase)
- 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 (bug fix)
- If yes, how is the feature documented? not applicable
CI report:
- b8be8b9b3517f40f28e28bbb5e0899ba02fca574 Azure: SUCCESS
Bot commands
The @flinkbot bot supports the following commands:@flinkbot run azurere-run the last Azure build
@flinkbot run azure
@lincoln-lil @xuyangzhong Appreciate if you can take a look 🙏
Sorry this sat so long, finally got back to it. Thanks @xuyangzhong and @featzhang for the detailed review.
Rebased on master and pushed an update, PTAL 🙏
I noticed a gap of my own related to multi-joins: a non-equi predicate can sit in an ON clause rather than the post-join filter and still gets evaluated at runtime, so dropping UB there leaks a phantom row. Added a guard for it, skipping the common join-key equalities since those are already handled by the join-key logic.
This is the same area as #27372 (upsert-key enrichment via equi-join equivalences) and they complement each other: that PR's equivalences feed straight into these guards, and the multijoin common-key skip here is really a local version of the same idea. After equivalence-metadata route @xuyangzhong suggested in #27372 , these guards could just consume that in a follow-up and the special-case drops out. Keeping them independent for now.
@xuyangzhong @featzhang @gustavodemorais Rebased on current master; all review comments are addressed
@gustavodemorais, your FLINK-40841 now handles the regular non-equi join at runtime, so I dropped this PR's UPDATE_BEFORE guard for StreamPhysicalJoin, in a separate commit. Only its DELETE_BY_KEY guard stays.
What's left is still broken on master: filters on non-upsert-key columns pushed into the source, in lookup joins, or in multi-join conditions leave phantom rows. Also the corresponding ChangelogSourceITCase tests fail on master without this fix.
Friendly ping for a review 🙏
Thanks @fresh-borzoni for picking this back up 🙏 This is a tricky bug. :)
Left a few small comments.
@polyzos Thank you for the review, addressed, PTAL 🙏
Thanks @fresh-borzoni, everything seems addressed 🙏 ran the full build plus the targeted and regression test suites locally, without any failures and checked the changes. From a correctness standpoint this looks ready. 👍
@lincoln-lil 👋 Can you help take a look at this or if you can point us to a person to help merge this? I have already reviewed and tested all the work, but we need some help to push this
@polyzos thanks for the fix! Pls rebase and update the pr. @xuyangzhong could you follow up check the final changes and handle the merge?
@xuyangzhong @lincoln-lil updated, PTAL
@xuyangzhong Thank you for the review, addressed, PTAL 🙏