Repository navigation
[Kernel]Introduce flatmap utility method for Closable Iterator - #5388
Merged
Merged
Conversation
…ableIterators This PR adds a reusable utility method for flattening nested CloseableIterator structures. The method properly manages resource cleanup by closing both the outer iterator and all inner iterators. Key features: - Flattens CloseableIterator<CloseableIterator<T>> into CloseableIterator<T> - Properly closes inner iterators when exhausted - Closes both current inner and outer iterators on close() - Handles empty iterators correctly - Comprehensive unit tests covering all edge cases This utility can be used in various places where nested iterator structures need to be flattened, avoiding reimplementation of common iterator patterns and reducing potential bugs.
huan233usc
requested review from
allisonport-db,
raveeram-db and
scottsand-db
October 23, 2025 18:17
- Fix typo in comment: 'will null' -> 'with null' - Fix double period in JavaDoc - Add exception handling in hasNext() to ensure cleanup on error - Update test assertions to use === for consistency - Add test case for exception handling during iteration - Verify that outer iterator is closed when exception occurs
raveeram-db
reviewed
Oct 23, 2025
| * @param <T> The type of elements in the inner iterators | ||
| * @return A new {@link CloseableIterator} that flattens all nested iterators | ||
| */ | ||
| public static <T> CloseableIterator<T> flatMap( |
Collaborator
There was a problem hiding this comment.
Semantics wise, isn't this more of a flatten API than flatMap @huan233usc ?
Collaborator
Author
There was a problem hiding this comment.
yes flatten is a better naming
| if (!hasNext()) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
| return currentInnerIterator.next(); |
Collaborator
There was a problem hiding this comment.
Can this throw? currentInnerIterator.next(); and are callers expected to handle this?
| } | ||
|
|
||
| /** | ||
| * Flattens a nested {@link CloseableIterator} structure into a single flat iterator. This method |
Collaborator
There was a problem hiding this comment.
perhaps a note that Callers should call close even if the iterator is not exhausted so the innerIterators are closed
…ocumentation - Rename flatMap() to flatten() for more accurate semantics - Add important note in JavaDoc about calling close() even if iterator is not fully consumed - Remove unnecessary 'throws IOException' from close() method - Update all test case names from 'flatMap' to 'flatten' Addresses reviewer feedback: 1. Method name should be 'flatten' not 'flatMap' (semantics) 2. Callers need to know they must call close() for resource cleanup 3. Exception handling in next() is correct as-is
1 of 5 tasks
raveeram-db
approved these changes
Oct 23, 2025
allisonport-db
approved these changes
Oct 23, 2025
allisonport-db
left a comment
Contributor
There was a problem hiding this comment.
LGTM thanks for doing this
01001101CK
pushed a commit
to 01001101CK/delta
that referenced
this pull request
Nov 17, 2025
…-io#5388) <!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md 2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP] Your PR title ...'. 3. Be sure to keep the PR description updated to reflect all changes. 4. Please write your PR title to summarize what this PR proposes. 5. If possible, provide a concise example to reproduce the issue for a faster review. 6. If applicable, include the corresponding issue number in the PR title and link it in the body. --> #### Which Delta project/connector is this regarding? <!-- Please add the component selected below to the beginning of the pull request title For example: [Spark] Title of my pull request --> - [ ] Spark - [ ] Standalone - [ ] Flink - [x] Kernel - [ ] Other (fill in here) ## Description <!-- - Describe what this PR changes. - Describe why we need the change. If this PR resolves an issue be sure to include "Resolves #XXX" to correctly link and close the issue upon merge. --> Adds a `Utils.flatMap` utility method for flattening nested `CloseableIterator<CloseableIterator<T>>` structures into `CloseableIterator<T>`. It will be helpful for use case like delta-io#5290 ## How was this patch tested? <!-- If tests were added, say they were added here. Please make sure to test the changes thoroughly including negative and positive cases if possible. If the changes were tested in any way other than unit tests, please clarify how you tested step by step (ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future). If the changes were not tested, please explain why. --> Unit tests ## Does this PR introduce _any_ user-facing changes? <!-- If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible. If possible, please also clarify if this is a user-facing change compared to the released Delta Lake versions or within the unreleased branches such as master. If no, write 'No'. --> No
huashi-st
pushed a commit
to huashi-st/delta
that referenced
this pull request
Apr 24, 2026
…-io#5388) <!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md 2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP] Your PR title ...'. 3. Be sure to keep the PR description updated to reflect all changes. 4. Please write your PR title to summarize what this PR proposes. 5. If possible, provide a concise example to reproduce the issue for a faster review. 6. If applicable, include the corresponding issue number in the PR title and link it in the body. --> #### Which Delta project/connector is this regarding? <!-- Please add the component selected below to the beginning of the pull request title For example: [Spark] Title of my pull request --> - [ ] Spark - [ ] Standalone - [ ] Flink - [x] Kernel - [ ] Other (fill in here) ## Description <!-- - Describe what this PR changes. - Describe why we need the change. If this PR resolves an issue be sure to include "Resolves #XXX" to correctly link and close the issue upon merge. --> Adds a `Utils.flatMap` utility method for flattening nested `CloseableIterator<CloseableIterator<T>>` structures into `CloseableIterator<T>`. It will be helpful for use case like delta-io#5290 ## How was this patch tested? <!-- If tests were added, say they were added here. Please make sure to test the changes thoroughly including negative and positive cases if possible. If the changes were tested in any way other than unit tests, please clarify how you tested step by step (ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future). If the changes were not tested, please explain why. --> Unit tests ## Does this PR introduce _any_ user-facing changes? <!-- If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible. If possible, please also clarify if this is a user-facing change compared to the released Delta Lake versions or within the unreleased branches such as master. If no, write 'No'. --> No
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which Delta project/connector is this regarding?
Description
Adds a
Utils.flatMaputility method for flattening nestedCloseableIterator<CloseableIterator<T>>structures intoCloseableIterator<T>.It will be helpful for use case like #5290
How was this patch tested?
Unit tests
Does this PR introduce any user-facing changes?
No