Skip to content
Merged
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
9 changes: 7 additions & 2 deletions pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,11 +1017,16 @@ def visit_less_than(self, term: BoundTerm, literal: Literal[Any]) -> pc.Expressi
def visit_less_than_or_equal(self, term: BoundTerm, literal: Literal[Any]) -> pc.Expression:
return pc.field(self._get_field_name(term)) <= _convert_scalar(literal.value, term.ref().field.field_type)

def _starts_with_input(self, term: BoundTerm) -> pc.Expression:
# starts_with has no kernel for dictionary-encoded input (https://github.com/apache/arrow/issues/40430), unlike the
# comparison and is_in kernels, so read the field as the Arrow type of its Iceberg type (large_string or large_binary)
return pc.field(self._get_field_name(term)).cast(visit(term.ref().field.field_type, _ConvertToArrowSchema()))

def visit_starts_with(self, term: BoundTerm, literal: Literal[Any]) -> pc.Expression:
return pc.starts_with(pc.field(self._get_field_name(term)), literal.value)
return pc.starts_with(self._starts_with_input(term), literal.value)

def visit_not_starts_with(self, term: BoundTerm, literal: Literal[Any]) -> pc.Expression:
return ~pc.starts_with(pc.field(self._get_field_name(term)), literal.value)
return ~pc.starts_with(self._starts_with_input(term), literal.value)

def visit_true(self) -> pc.Expression:
return pc.scalar(True)
Expand Down
83 changes: 77 additions & 6 deletions tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,17 +991,88 @@ def test_expr_not_in_to_pyarrow(bound_reference: BoundReference) -> None:


def test_expr_starts_with_to_pyarrow(bound_reference: BoundReference) -> None:
assert (
repr(expression_to_pyarrow(BoundStartsWith(bound_reference, literal("he"))))
== '<pyarrow.compute.Expression starts_with(foo, {pattern="he", ignore_case=false})>'
assert repr(expression_to_pyarrow(BoundStartsWith(bound_reference, literal("he")))) == (
"<pyarrow.compute.Expression starts_with(cast(foo, {to_type=large_string, allow_int_overflow=false, "
"allow_time_truncate=false, allow_time_overflow=false, allow_decimal_truncate=false, allow_float_truncate=false, "
'allow_invalid_utf8=false}), {pattern="he", ignore_case=false})>'
)


def test_expr_not_starts_with_to_pyarrow(bound_reference: BoundReference) -> None:
assert (
repr(expression_to_pyarrow(BoundNotStartsWith(bound_reference, literal("he"))))
== '<pyarrow.compute.Expression invert(starts_with(foo, {pattern="he", ignore_case=false}))>'
assert repr(expression_to_pyarrow(BoundNotStartsWith(bound_reference, literal("he")))) == (
"<pyarrow.compute.Expression invert(starts_with(cast(foo, {to_type=large_string, allow_int_overflow=false, "
"allow_time_truncate=false, allow_time_overflow=false, allow_decimal_truncate=false, allow_float_truncate=false, "
'allow_invalid_utf8=false}), {pattern="he", ignore_case=false}))>'
)


def test_starts_with_dictionary_encoded_to_pyarrow() -> None:
schema = Schema(
NestedField(1, "fruit", StringType(), required=False),
NestedField(2, "payload", BinaryType(), required=False),
)
fruit = BoundReference(schema.find_field(1), schema.accessor_for_field(1))
payload = BoundReference(schema.find_field(2), schema.accessor_for_field(2))
for string_type, binary_type in [
(pa.large_string(), pa.large_binary()),
(pa.string(), pa.binary()),
(pa.dictionary(pa.int8(), pa.string()), pa.dictionary(pa.int8(), pa.binary())),
]:
arrow_table = pa.table(
{
"id": [1, 2, 3],
"fruit": pa.array(["apple", "banana", "apricot"], string_type),
# b"a\xff" is not valid UTF-8, so binary values must not be read as strings
"payload": pa.array([b"a\xff", b"bb", b"ab"], binary_type),
}
)
for bound_expr, expected in [
(BoundStartsWith(fruit, literal("ap")), [1, 3]),
(BoundNotStartsWith(fruit, literal("ap")), [2]),
(BoundStartsWith(payload, literal(b"a")), [1, 3]),
(BoundNotStartsWith(payload, literal(b"a")), [2]),
]:
assert arrow_table.filter(expression_to_pyarrow(bound_expr))["id"].to_pylist() == expected, (bound_expr, string_type)


def test_row_filter_starts_with_on_dictionary_encoded_strings(catalog: InMemoryCatalog) -> None:
catalog.create_namespace("default")
schema = pa.schema([("id", pa.int64()), ("fruit", pa.large_string()), ("addr", pa.struct([("city", pa.large_string())]))])
ids = pa.array([1, 2, 3, 4], pa.int64())
fruit = pa.array(["apple", "banana", "apricot", "cherry"], pa.large_string())
city = pa.array(["apex", "boston", "apia", "chicago"], pa.large_string())

# dictionary_columns reads the plain strings as dictionary arrays
plain = catalog.create_table("default.starts_with_plain", schema=schema)
plain.append(pa.Table.from_arrays([ids, fruit, pa.StructArray.from_arrays([city], names=["city"])], schema=schema))
for row_filter, expected in [("fruit LIKE 'ap%'", [1, 3]), ("fruit NOT LIKE 'ap%'", [2, 4])]:
result = plain.scan(row_filter=row_filter).to_arrow(dictionary_columns=("fruit",))
assert pa.types.is_dictionary(result.schema.field("fruit").type), row_filter
assert sorted(result["id"].to_pylist()) == expected, row_filter

# A file written from dictionary-encoded input, such as a pandas categorical, keeps the dictionary type
encoded = catalog.create_table("default.starts_with_encoded", schema=schema)
encoded.append(
pa.table(
{
"id": ids,
"fruit": fruit.dictionary_encode(),
"addr": pa.StructArray.from_arrays([city.dictionary_encode()], names=["city"]),
}
)
)
for row_filter, expected in [
("fruit LIKE 'ap%'", [1, 3]),
("fruit NOT LIKE 'ap%'", [2, 4]),
("addr.city LIKE 'ap%'", [1, 3]),
("addr.city NOT LIKE 'ap%'", [2, 4]),
]:
assert sorted(encoded.scan(row_filter=row_filter).to_arrow()["id"].to_pylist()) == expected, row_filter

# Both deletes rewrite the file with the rows the complementary filter keeps
encoded.delete("fruit LIKE 'ap%'")
encoded.delete("addr.city NOT LIKE 'b%'")
assert encoded.scan().to_arrow()["id"].to_pylist() == [2]


def test_uuid_expressions_to_pyarrow() -> None:
Expand Down