Skip to content

allow string transforms in add_field() #1883

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pyiceberg/table/update/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Optional,
Set,
Tuple,
Union,
)

from pyiceberg.expressions import (
Expand All @@ -47,7 +48,7 @@
UpdatesAndRequirements,
UpdateTableMetadata,
)
from pyiceberg.transforms import IdentityTransform, TimeTransform, Transform, VoidTransform
from pyiceberg.transforms import IdentityTransform, TimeTransform, Transform, VoidTransform, parse_transform

if TYPE_CHECKING:
from pyiceberg.table import Transaction
Expand Down Expand Up @@ -85,12 +86,16 @@ def __init__(self, transaction: Transaction, case_sensitive: bool = True) -> Non
def add_field(
self,
source_column_name: str,
transform: Transform[Any, Any],
transform: Union[str, Transform[Any, Any]], # Aceptamos strings o Transform
Copy link
Contributor

Choose a reason for hiding this comment

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

😉

Suggested change
transform: Union[str, Transform[Any, Any]], # Aceptamos strings o Transform
transform: Union[str, Transform[Any, Any]], # We accept strings or Transform

partition_field_name: Optional[str] = None,
) -> UpdateSpec:

if isinstance(transform, str):
transform = parse_transform(transform)

ref = Reference(source_column_name)
bound_ref = ref.bind(self._transaction.table_metadata.schema(), self._case_sensitive)
# verify transform can actually bind it

output_type = bound_ref.field.field_type
if not transform.can_transform(output_type):
raise ValueError(f"{transform} cannot transform {output_type} values from {bound_ref.field.name}")
Expand Down Expand Up @@ -128,6 +133,7 @@ def add_field(
self._adds.append(new_field)
return self


def add_identity(self, source_column_name: str) -> UpdateSpec:
Comment on lines +136 to 137
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to revert this to make the linter happy:

Suggested change
def add_identity(self, source_column_name: str) -> UpdateSpec:
def add_identity(self, source_column_name: str) -> UpdateSpec:

return self.add_field(source_column_name, IdentityTransform(), None)

Expand Down
Loading