Skip to content

[Kernel]Introduce flatmap utility method for Closable Iterator - #5388

Merged
huan233usc merged 5 commits into
delta-io:masterfrom
huan233usc:flatmap-utility-method
Oct 23, 2025
Merged

huan233usc merged 5 commits into
delta-io:masterfrom
huan233usc:flatmap-utility-method

Conversation

@huan233usc

Copy link
Copy Markdown
Collaborator

Which Delta project/connector is this regarding?

  • Spark
  • Standalone
  • Flink
  • Kernel
  • Other (fill in here)

Description

Adds a Utils.flatMap utility method for flattening nested CloseableIterator<CloseableIterator<T>> structures into CloseableIterator<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

…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.
- 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
* @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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Semantics wise, isn't this more of a flatten API than flatMap @huan233usc ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yes flatten is a better naming

if (!hasNext()) {
throw new NoSuchElementException();
}
return currentInnerIterator.next();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

@allisonport-db allisonport-db 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.

LGTM thanks for doing this

@huan233usc
huan233usc merged commit 151e6a4 into delta-io:master Oct 23, 2025
27 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants