Repository navigation
Row Lineage: Add metadata column support for _row_id #4375
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
Merged
Merged
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Metadata columns that are not part of a table schema, but can be selected in a scan.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| from types import MappingProxyType | ||
|
|
||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.types import LongType, NestedField | ||
|
|
||
| ROW_ID = NestedField( | ||
| field_id=2147483540, | ||
| name="_row_id", | ||
| field_type=LongType(), | ||
| required=False, | ||
| doc="Implicit row ID that is automatically assigned", | ||
| ) | ||
| LAST_UPDATED_SEQUENCE_NUMBER = NestedField( | ||
| field_id=2147483539, | ||
| name="_last_updated_sequence_number", | ||
| field_type=LongType(), | ||
| required=False, | ||
| doc="Sequence number when the row was last updated", | ||
| ) | ||
|
|
||
| METADATA_COLUMNS: Mapping[str, NestedField] = MappingProxyType( | ||
| {field.name: field for field in (ROW_ID, LAST_UPDATED_SEQUENCE_NUMBER)} | ||
| ) | ||
|
|
||
|
|
||
| def _find_metadata_column(name: str, case_sensitive: bool) -> NestedField | None: | ||
| if case_sensitive: | ||
| return METADATA_COLUMNS.get(name) | ||
| return next((field for field in METADATA_COLUMNS.values() if field.name.lower() == name.lower()), None) | ||
|
|
||
|
|
||
| def _has_field(schema: Schema, name: str, case_sensitive: bool) -> bool: | ||
| try: | ||
| schema.find_field(name, case_sensitive=case_sensitive) | ||
| return True | ||
| except ValueError: | ||
| return False | ||
|
|
||
|
|
||
| def split_selected_fields( | ||
| schema: Schema, selected_fields: tuple[str, ...], case_sensitive: bool | ||
| ) -> tuple[tuple[str, ...], tuple[NestedField, ...]]: | ||
| """Split selected names into schema columns and metadata columns. | ||
|
|
||
| A name that resolves in the schema is a schema column, even when it matches a metadata column name. | ||
| """ | ||
| schema_names: list[str] = [] | ||
| metadata_fields: list[NestedField] = [] | ||
| for name in selected_fields: | ||
| if name != "*" and not _has_field(schema, name, case_sensitive): | ||
| if (field := _find_metadata_column(name, case_sensitive)) is not None: | ||
| if field not in metadata_fields: | ||
| metadata_fields.append(field) | ||
| continue | ||
| schema_names.append(name) | ||
| return tuple(schema_names), tuple(metadata_fields) | ||
|
|
||
|
|
||
| def project_schema(schema: Schema, selected_fields: tuple[str, ...], case_sensitive: bool, format_version: int) -> Schema: | ||
| """Prune the schema to the selected names, and append any selected metadata columns.""" | ||
| schema_names, metadata_fields = split_selected_fields(schema, selected_fields, case_sensitive) | ||
|
|
||
| if metadata_fields and format_version < 3: | ||
| names = ", ".join(field.name for field in metadata_fields) | ||
| raise ValueError( | ||
| f"Cannot select {names}: row lineage requires format version 3, but the table has format version {format_version}" | ||
| ) | ||
|
|
||
| projected = schema if "*" in schema_names else schema.select(*schema_names, case_sensitive=case_sensitive) | ||
| if not metadata_fields: | ||
| return projected | ||
|
|
||
| return Schema( | ||
| *projected.fields, | ||
| *metadata_fields, | ||
| schema_id=projected.schema_id, | ||
| identifier_field_ids=projected.identifier_field_ids, | ||
| ) |
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.
Nit:
positionsis always set whenrow_id_index is not None, so thepositions is not Nonecheck is redundant.