flink icon indicating copy to clipboard operation
flink copied to clipboard

[FLINK-38579] [table-planner] Fix incorrect UB drop for filter on non-upsert key

Open fresh-borzoni opened this issue 7 months ago • 4 comments

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 SatisfyUpdateKindTraitVisitor to reject ONLY_UPDATE_AFTER for TableSourceScan when filter references non-upsert keys
  • Modified SatisfyUpdateKindTraitVisitor to reject ONLY_UPDATE_AFTER for 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

fresh-borzoni avatar May 02 '26 09:05 fresh-borzoni

CI report:

  • b8be8b9b3517f40f28e28bbb5e0899ba02fca574 Azure: SUCCESS
Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run azure re-run the last Azure build

flinkbot avatar May 02 '26 09:05 flinkbot

@flinkbot run azure

fresh-borzoni avatar May 02 '26 13:05 fresh-borzoni

@lincoln-lil @xuyangzhong Appreciate if you can take a look 🙏

fresh-borzoni avatar May 02 '26 17:05 fresh-borzoni

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.

fresh-borzoni avatar Jun 08 '26 14:06 fresh-borzoni

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

fresh-borzoni avatar Oct 28 '26 11:10 fresh-borzoni

Thanks @fresh-borzoni for picking this back up 🙏 This is a tricky bug. :)

Left a few small comments.

polyzos avatar Oct 29 '26 10:10 polyzos

@polyzos Thank you for the review, addressed, PTAL 🙏

fresh-borzoni avatar Oct 29 '26 20:10 fresh-borzoni

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

polyzos avatar Oct 30 '26 14:10 polyzos

@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 avatar Oct 30 '26 14:10 polyzos

@polyzos thanks for the fix! Pls rebase and update the pr. @xuyangzhong could you follow up check the final changes and handle the merge?

lincoln-lil avatar Nov 09 '26 06:11 lincoln-lil

@xuyangzhong @lincoln-lil updated, PTAL

fresh-borzoni avatar Nov 12 '26 01:11 fresh-borzoni

@xuyangzhong Thank you for the review, addressed, PTAL 🙏

fresh-borzoni avatar Nov 13 '26 12:11 fresh-borzoni