Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion pyiceberg/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,16 @@ def project(self, name: str, pred: BoundPredicate) -> UnboundPredicate | None:
return _truncate_number(name, pred, self.transform(field_type))
elif isinstance(field_type, (BinaryType, StringType)):
if isinstance(pred, BoundLiteralPredicate):
return _truncate_array(name, pred, self.transform(field_type))
if isinstance(pred, BoundNotStartsWith):
literal_width = len(pred.literal.value)
if literal_width < self.width:
return pred.as_unbound(name, pred.literal.value)
elif literal_width == self.width:
return NotEqualTo(name, pred.literal.value)
else:
return None
else:
return _truncate_array(name, pred, self.transform(field_type))

def strict_project(self, name: str, pred: BoundPredicate) -> UnboundPredicate | None:
field_type = pred.term.ref().field.field_type
Expand Down
17 changes: 15 additions & 2 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,9 +1026,22 @@ def test_projection_truncate_string_starts_with(bound_reference_str: BoundRefere


def test_projection_truncate_string_not_starts_with(bound_reference_str: BoundReference) -> None:
# literal_width (5) > truncate width (2): no inclusive projection possible (unsafe)
assert TruncateTransform(2).project("name", BoundNotStartsWith(term=bound_reference_str, literal=literal("hello"))) is None


def test_projection_truncate_string_not_starts_with_shorter_literal(bound_reference_str: BoundReference) -> None:
# literal_width (2) == truncate width (2): project to !=
assert TruncateTransform(2).project(
"name", BoundNotStartsWith(term=bound_reference_str, literal=literal("he"))
) == NotEqualTo(term="name", literal=literal("he"))


def test_projection_truncate_string_not_starts_with_original_literal(bound_reference_str: BoundReference) -> None:
# literal_width (1) < truncate width (2): keep original literal
assert TruncateTransform(2).project(
"name", BoundNotStartsWith(term=bound_reference_str, literal=literal("hello"))
) == NotStartsWith(term="name", literal=literal("he"))
"name", BoundNotStartsWith(term=bound_reference_str, literal=literal("h"))
) == NotStartsWith(term="name", literal=literal("h"))


def _test_projection(lhs: UnboundPredicate | None, rhs: UnboundPredicate | None) -> None:
Expand Down