-
Notifications
You must be signed in to change notification settings - Fork 151
Add Iceberg incremental read via CHANGES AT(VERSION) END(VERSION) #4262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sfc-gh-igarish
wants to merge
2
commits into
main
Choose a base branch
from
igarish/iceberg-incremental-read-changes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -248,6 +248,81 @@ def _extract_time_travel_from_options(options: dict) -> dict: | |
| return result | ||
|
|
||
|
|
||
| def _get_reader_option(options: dict, *keys: str): | ||
| """Case-insensitive lookup for a reader option key.""" | ||
| for key in keys: | ||
| for option_key, value in options.items(): | ||
| if option_key.upper() == key.upper(): | ||
| return value | ||
| return None | ||
|
|
||
|
|
||
| def _extract_iceberg_changes_from_options(options: dict) -> dict: | ||
| """Extract Spark Iceberg incremental-read options from a reader dict. | ||
|
|
||
| Maps ``start-snapshot-id`` / ``end-snapshot-id`` (and underscore | ||
| variants) to internal ``start_snapshot_id`` / ``end_snapshot_id`` | ||
| kwargs consumed by :meth:`Session.table`. | ||
| """ | ||
| start = _get_reader_option(options, "start-snapshot-id", "start_snapshot_id") | ||
| end = _get_reader_option(options, "end-snapshot-id", "end_snapshot_id") | ||
| if start is None and end is None: | ||
| return {} | ||
| if start is None: | ||
| raise ValueError( | ||
| "Iceberg incremental read requires 'start-snapshot-id'; " | ||
| "'end-snapshot-id' cannot be used alone." | ||
| ) | ||
| try: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this part looks to be the same logic as |
||
| start_id = int(start) | ||
| except (TypeError, ValueError): | ||
| raise ValueError( | ||
| "'start-snapshot-id' must be a 64-bit integer Iceberg snapshot id, " | ||
| f"got {start!r}." | ||
| ) from None | ||
| end_id = None | ||
| if end is not None: | ||
| try: | ||
| end_id = int(end) | ||
| except (TypeError, ValueError): | ||
| raise ValueError( | ||
| "'end-snapshot-id' must be a 64-bit integer Iceberg snapshot id, " | ||
| f"got {end!r}." | ||
| ) from None | ||
| return {"start_snapshot_id": start_id, "end_snapshot_id": end_id} | ||
|
|
||
|
|
||
| def _reader_options_conflict_with_incremental_read(options: dict) -> list[str]: | ||
| """Return reader option keys that cannot coexist with incremental read.""" | ||
| incremental_keys = { | ||
| "start-snapshot-id", | ||
| "start_snapshot_id", | ||
| "end-snapshot-id", | ||
| "end_snapshot_id", | ||
| } | ||
| if not any( | ||
| k.upper().replace("_", "-") in {x.replace("_", "-") for x in incremental_keys} | ||
| for k in options | ||
| ): | ||
| return [] | ||
| blocked = [] | ||
| for key in options: | ||
| upper = key.upper() | ||
| if upper in incremental_keys or upper.replace("_", "-") in { | ||
| x.replace("_", "-") for x in incremental_keys | ||
| }: | ||
| continue | ||
| if upper in _TIME_TRAVEL_OPTIONS_PARAMS_MAP or upper in ( | ||
| "SNAPSHOT-ID", | ||
| "SNAPSHOT_ID", | ||
| "AS-OF-TIMESTAMP", | ||
| "VERSION_TAG", | ||
| "VERSION-TAG", | ||
| ): | ||
| blocked.append(key) | ||
| return blocked | ||
|
|
||
|
|
||
| class DataFrameReader: | ||
| """Provides methods to load data in various supported formats from a Snowflake | ||
| stage to a :class:`DataFrame`. The paths provided to the DataFrameReader must refer | ||
|
|
@@ -671,11 +746,29 @@ def table( | |
| # still pass them without us advertising the surface. | ||
| version = kwargs.pop("version", None) | ||
| version_tag = kwargs.pop("version_tag", None) | ||
| start_snapshot_id = kwargs.pop("start_snapshot_id", None) | ||
| end_snapshot_id = kwargs.pop("end_snapshot_id", None) | ||
| if kwargs: | ||
| raise TypeError( | ||
| f"table() got unexpected keyword arguments: {sorted(kwargs)}" | ||
| ) | ||
|
|
||
| changes_from_options = _extract_iceberg_changes_from_options(self._cur_options) | ||
| if changes_from_options: | ||
| conflicting = _reader_options_conflict_with_incremental_read( | ||
| self._cur_options | ||
| ) | ||
| if conflicting: | ||
| raise ValueError( | ||
| "Cannot combine Iceberg incremental read " | ||
| "('start-snapshot-id' / 'end-snapshot-id') with time travel " | ||
| f"options on the same read; found {conflicting!r}." | ||
| ) | ||
| if start_snapshot_id is None: | ||
| start_snapshot_id = changes_from_options["start_snapshot_id"] | ||
| if end_snapshot_id is None: | ||
| end_snapshot_id = changes_from_options.get("end_snapshot_id") | ||
|
|
||
| # AST. | ||
| stmt = None | ||
| if _emit_ast and self._ast is not None: | ||
|
|
@@ -697,6 +790,17 @@ def table( | |
| ast.stream.value = stream | ||
|
|
||
| if ( | ||
| start_snapshot_id is not None | ||
| or end_snapshot_id is not None | ||
| or changes_from_options | ||
| ): | ||
| table = self._session.table( | ||
| name, | ||
| _emit_ast=False, | ||
| start_snapshot_id=start_snapshot_id, | ||
| end_snapshot_id=end_snapshot_id, | ||
| ) | ||
| elif ( | ||
| time_travel_mode is not None | ||
| or version is not None | ||
| or version_tag is not None | ||
|
|
||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a question about this part of code:
It looks like after the first try block, it is impossible for
snapshot_idto be a bool value here, can you tell me what is the scenario this check trying to guard?