Skip to content

[Iceberg CDC] Add Snapshot Changelog Scanner#38836

Merged
ahmedabu98 merged 4 commits into
apache:masterfrom
ahmedabu98:iceberg_changelogscanner
Jul 9, 2026
Merged

[Iceberg CDC] Add Snapshot Changelog Scanner#38836
ahmedabu98 merged 4 commits into
apache:masterfrom
ahmedabu98:iceberg_changelogscanner

Conversation

@ahmedabu98

@ahmedabu98 ahmedabu98 commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Adds a DoFn that scans each incoming snapshot and plans batches of changelog scan tasks. Each batch is routed to the appropriate downstream path depending on its contents and size. There's a lot of details in the java doc, but to summarize:

  • Unidirectional changes, i.e. inserts only or deletes only, are routed to a fast path that can be read and emitted directly.
  • Small bidirectional groups are routed to a local in-memory resolver.
  • Large bidirectional groups are routed to the distributed resolution path where rows are grouped by primary key then passed to a resolver

We perform a few optimizations to limit the amount of data we need to resolve:

  • non-overwrite snapshots can bypass update resolution
  • if partition specs are derived from PK fields, we narrow down to per-partition analysis
  • non-overlapping opposing tasks can bypass update resolution

Note: "update resolution" is logic that compares inserts and deletes to check if an update has occurred.

Part of #38831


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request adds a new ChangelogScanner to the Iceberg IO connector in Apache Beam. The scanner is designed to efficiently process Iceberg snapshots by categorizing changelog tasks into different paths based on their complexity and size. By implementing smart routing and leveraging partition and file-level metadata, the change significantly reduces the data volume requiring a costly CoGroupByKey shuffle, improving overall performance for CDC workloads.

Highlights

  • New Changelog Scanner: Introduced a new ChangelogScanner DoFn that processes Iceberg snapshots to plan changelog scan tasks.
  • Intelligent Task Routing: Implemented logic to route tasks based on complexity: unidirectional changes bypass shuffling, small bidirectional groups are resolved in-memory, and large bidirectional groups are routed to a distributed resolution path.
  • Partition Optimization: Added optimization to narrow down change analysis to individual partitions when partition specs are derived from primary key fields.
  • File-Level Overlap Analysis: Added file-level metadata analysis to identify non-overlapping opposing tasks, allowing them to bypass the expensive CoGroupByKey shuffle.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@ahmedabu98 ahmedabu98 marked this pull request as draft June 5, 2026 19:40
@ahmedabu98

Copy link
Copy Markdown
Contributor Author

Need to merge #38821 first

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces Iceberg CDC support by adding ChangelogDescriptor, ChangelogScanner, and corresponding unit tests. The ChangelogScanner optimizes data processing by routing changelog tasks into unidirectional, small bidirectional, or large bidirectional paths. The review feedback highlights a critical issue where instance variables in the DoFn could leak state or cause a NullPointerException, suggests an early return optimization when primary key fields are empty, and recommends removing unused helper methods in both the scanner and its test suite.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +261 to +273
boolean metricsAvailable = true;
MetricsConfig metricsConfig = MetricsConfig.forTable(table);
Collection<String> pkFields = table.schema().identifierFieldNames();
for (String field : pkFields) {
MetricsModes.MetricsMode mode = metricsConfig.columnMode(field);
if (!(mode instanceof MetricsModes.Full) && !(mode instanceof MetricsModes.Truncate)) {
metricsAvailable = false;
break;
}
}
if (metricsAvailable) {
scan = scan.includeColumnStats(pkFields);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If pkFields is empty, metricsAvailable remains true, which leads to calling scan.includeColumnStats(Collections.emptyList()). If there are no primary keys, we cannot perform file-level overlap analysis anyway. Returning early when pkFields is empty avoids unnecessary overhead.

    Collection<String> pkFields = table.schema().identifierFieldNames();
    if (pkFields.isEmpty()) {
      return scan;
    }
    boolean metricsAvailable = true;
    MetricsConfig metricsConfig = MetricsConfig.forTable(table);
    for (String field : pkFields) {
      MetricsModes.MetricsMode mode = metricsConfig.columnMode(field);
      if (!(mode instanceof MetricsModes.Full) && !(mode instanceof MetricsModes.Truncate)) {
        metricsAvailable = false;
        break;
      }
    }
    if (metricsAvailable) {
      scan = scan.includeColumnStats(pkFields);
    }

@ahmedabu98 ahmedabu98 marked this pull request as ready for review June 25, 2026 18:41

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces Iceberg CDC and changelog scanning capabilities to Apache Beam, adding a new ChangelogScanner DoFn to route changelog tasks based on complexity, alongside supporting classes like ChangelogDescriptor and unit tests. The review feedback highlights several critical improvements: refactoring the DoFn to be stateless to prevent concurrency issues, adding defensive checks for missing primary keys, avoiding unnecessary SerializableTable copying, optimizing file analysis to bypass execution when column stats are missing, replacing inefficient multi-pass stream operations with a single-pass loop, and removing dead code.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +601 to +634
StructLike globalInsertLower =
insertTasks.stream()
.filter(t -> t.overlaps)
.map(t -> t.lowerId)
.min(idComp)
.orElseThrow();
StructLike globalInsertUpper =
insertTasks.stream()
.filter(t -> t.overlaps)
.map(t -> t.upperId)
.max(idComp)
.orElseThrow();
StructLike globalDeleteLower =
deleteTasks.stream()
.filter(t -> t.overlaps)
.map(t -> t.lowerId)
.min(idComp)
.orElseThrow();
StructLike globalDeleteUpper =
deleteTasks.stream()
.filter(t -> t.overlaps)
.map(t -> t.upperId)
.max(idComp)
.orElseThrow();

overlapLower =
idComp.compare(globalInsertLower, globalDeleteLower) > 0
? globalInsertLower
: globalDeleteLower;
overlapUpper =
idComp.compare(globalInsertUpper, globalDeleteUpper) < 0
? globalInsertUpper
: globalDeleteUpper;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using multiple stream operations to find the global minimum and maximum bounds of overlapping tasks results in 4 separate passes over insertTasks and deleteTasks. This can be done much more efficiently in a single pass using a simple loop, which also avoids the overhead of stream creation.

    StructLike globalInsertLower = null;
    StructLike globalInsertUpper = null;
    for (TaskAndBounds t : insertTasks) {
      if (t.overlaps) {
        if (globalInsertLower == null || idComp.compare(t.lowerId, globalInsertLower) < 0) {
          globalInsertLower = t.lowerId;
        }
        if (globalInsertUpper == null || idComp.compare(t.upperId, globalInsertUpper) > 0) {
          globalInsertUpper = t.upperId;
        }
      }
    }

    StructLike globalDeleteLower = null;
    StructLike globalDeleteUpper = null;
    for (TaskAndBounds t : deleteTasks) {
      if (t.overlaps) {
        if (globalDeleteLower == null || idComp.compare(t.lowerId, globalDeleteLower) < 0) {
          globalDeleteLower = t.lowerId;
        }
        if (globalDeleteUpper == null || idComp.compare(t.upperId, globalDeleteUpper) > 0) {
          globalDeleteUpper = t.upperId;
        }
      }
    }

    if (globalInsertLower == null || globalDeleteLower == null) {
      throw new IllegalStateException("Expected at least one overlapping task in bidirectional list");
    }

    overlapLower =
        idComp.compare(globalInsertLower, globalDeleteLower) > 0
            ? globalInsertLower
            : globalDeleteLower;
    overlapUpper =
        idComp.compare(globalInsertUpper, globalDeleteUpper) < 0
            ? globalInsertUpper
            : globalDeleteUpper;

Comment on lines +926 to +928
static String name(String path) {
return Iterables.getLast(Splitter.on("-").split(path));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The name method is unused/dead code in this class and its tests. Removing it keeps the codebase clean and allows removing the unused Splitter import.

@github-actions

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @kennknowles for label java.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Reminder, please take a look at this pr: @kennknowles

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Assigning new set of reviewers because Pr has gone too long without review. If you would like to opt out of this review, comment assign to next reviewer:

R: @Abacn for label java.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

@talatuyarer talatuyarer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see any issue

throw new IllegalStateException("Unknown ChangelogScanTask type: " + task.getClass());
}
}
} catch (TaskAndBounds.NoBoundMetricsException e) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One task without PK bound metrics silently poisons the whole group which means merge-on-read tables lose the entire file-overlap optimization. Do you think we can handle this per Task ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we can, since we don't know where that task overlaps. It could overlap with any other task in the group without us knowing. There could be update records in those overlaps, so it would be risky to let them continue going down the fast path.

I think the safest thing is to disregard the optimization. Otherwise we could miss out on update records. Lmk if I'm missing something though!

@ahmedabu98 ahmedabu98 merged commit dac4722 into apache:master Jul 9, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants