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
5 changes: 5 additions & 0 deletions changelog.d/ITL-1380.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Fixed

- Join operators reject mismatched Arrow extension parameters in shared keys, including
nested extension types. MergeJoin also rejects mismatched extension parameters in
colliding data columns. Errors identify the column and both logical types.
15 changes: 15 additions & 0 deletions docs/dev/design_issues/ITL-1380-JE1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## `src/orcapod/core/operators/` — join type checking

### JE1 — Joins accepted keys with different logical type identifiers
**Status:** resolved
**Severity:** critical
**Issue:** ITL-1380

PyArrow can consider two instances of one extension type class equal even when they
serialize different parameters. This allowed shared tag keys with different logical
type identifiers to match and allowed incompatible colliding columns past MergeJoin's
type check.

**Fix:** Shared Arrow types must agree after large normalization and carry the same full
extension identifiers throughout the type, including nested children. Join errors name
the column and show the identifiers that each input provided.
6 changes: 5 additions & 1 deletion src/orcapod/core/nodes/function_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,11 @@ def _concat_row_tables(tables: "list[pa.Table]") -> "pa.Table":
for name, row_type in zip(table.schema.names, table.schema.types):
expected = seen.setdefault(name, row_type)
if arrow_utils.shared_arrow_type(expected, row_type) is None:
message = f"Rows disagree on the Arrow type of column {name!r}: {expected} and {row_type}"
message = (
f"Rows disagree on the Arrow type of column {name!r}: "
f"{arrow_utils.describe_arrow_type(expected)} and "
f"{arrow_utils.describe_arrow_type(row_type)}"
)
raise InputValidationError(message)
return arrow_utils.concat_tables_by_name(tables)

Expand Down
4 changes: 3 additions & 1 deletion src/orcapod/core/operators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ def require_shared_arrow_type(
"""
shared = arrow_utils.shared_arrow_type(left_type, right_type)
if shared is None:
left_description = arrow_utils.describe_arrow_type(left_type)
right_description = arrow_utils.describe_arrow_type(right_type)
raise InputValidationError(
f"{operator} inputs disagree on the Arrow type of column {name!r}: "
+ f"{left_type} in one input and {right_type} in the other. {operator} "
+ f"{left_description} in one input and {right_description} in the other. {operator} "
+ "never widens or promotes a type; cast one input explicitly so both "
+ "have the same Arrow type."
)
Expand Down
41 changes: 38 additions & 3 deletions src/orcapod/utils/arrow_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import hashlib
import json
from collections import defaultdict
from collections.abc import Callable, Mapping, Collection, Sequence
from collections.abc import Callable, Iterator, Mapping, Collection, Sequence
from typing import Any, TYPE_CHECKING, cast

from orcapod.system_constants import constants
Expand All @@ -12,6 +12,7 @@

if TYPE_CHECKING:
import pyarrow as pa
from orcapod.logical_types.identifier import LogicalTypeIdentifier
from orcapod.protocols.semantic_types_protocols import TypeConverterProtocol
else:
pa = LazyModule("pyarrow")
Expand Down Expand Up @@ -319,6 +320,34 @@ def normalize_schema_to_large_types(
return schema


def _extension_identifiers(arrow_type: pa.DataType) -> Iterator[LogicalTypeIdentifier]:
"""Yield Python extension identifiers in Arrow type-tree order, including repeats."""
if isinstance(arrow_type, pa.ExtensionType):
from orcapod.logical_types.identifier import arrow_type_identifier

yield arrow_type_identifier(arrow_type)
if isinstance(arrow_type, pa.BaseExtensionType):
yield from _extension_identifiers(arrow_type.storage_type)
elif pa.types.is_dictionary(arrow_type):
# Dictionary values are not exposed as child fields.
yield from _extension_identifiers(arrow_type.value_type)
else:
for i in range(arrow_type.num_fields):
yield from _extension_identifiers(arrow_type.field(i).type)


def describe_arrow_type(arrow_type: pa.DataType) -> str:
"""Render an Arrow type and the full identifiers of any Python extensions."""
identifiers = ", ".join(
identifier.text for identifier in _extension_identifiers(arrow_type)
)
return (
f"{arrow_type} (logical identifiers: {identifiers})"
if identifiers
else str(arrow_type)
)


def shared_arrow_type(
left_type: "pa.DataType", right_type: "pa.DataType"
) -> "pa.DataType | None":
Expand All @@ -332,7 +361,9 @@ def shared_arrow_type(
(``normalize_to_large_types`` with ``keep_null=True``: ``string`` to
``large_string``, ``binary`` to ``large_binary``, ``list`` to
``large_list``, recursively), which the Arrow hasher treats as identical.
If the normalized types are equal, that is the shared type. Any other
The normalized types must be equal and carry the same logical type
identifier at each position. Arrow equality can ignore a Python extension
type's serialized metadata, including inside nested types. Any other
difference (integer or float width, timestamp unit, decimal precision,
struct fields, nullability of a nested field, logical type) means there is
no shared type: nothing is widened, promoted or picked. The result does
Expand All @@ -348,7 +379,11 @@ def shared_arrow_type(
"""
normalized_left = normalize_to_large_types(left_type, keep_null=True)
normalized_right = normalize_to_large_types(right_type, keep_null=True)
if normalized_left == normalized_right:
if normalized_left is normalized_right or (
normalized_left == normalized_right
and tuple(_extension_identifiers(normalized_left))
== tuple(_extension_identifiers(normalized_right))
):
return normalized_left
return None

Expand Down
25 changes: 16 additions & 9 deletions tests/test_core/operators/several_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,7 @@ def make_source(
The source. A column is nullable exactly when it holds a null.
"""
arrangement = arrangement or Arrangement(spec.tags, TAGS_FIRST, PLAIN)
names = _layout(spec, arrangement.tag_order, arrangement.layout)
columns = {name: spec.columns[name] for name in names}
if rows is not None:
columns = {name: column.take(list(rows)) for name, column in columns.items()}
schema = pa.schema(
pa.field(name, column.type, nullable=column.null_count > 0)
for name, column in columns.items()
)
table = pa.table(list(columns.values()), schema=schema)
table = make_table(spec, arrangement, rows=rows)
if arrangement.roles == PLAIN:
tag_only: bool | list[str] = True
elif arrangement.roles == DATA_TAGS:
Expand All @@ -218,6 +210,21 @@ def make_source(
)


def make_table(
spec: Spec, arrangement: Arrangement, *, rows: Sequence[int] | None = None
) -> pa.Table:
"""Build the arranged Arrow input, marking only columns with nulls nullable."""
names = _layout(spec, arrangement.tag_order, arrangement.layout)
columns = {name: spec.columns[name] for name in names}
if rows is not None:
columns = {name: column.take(list(rows)) for name, column in columns.items()}
schema = pa.schema(
pa.field(name, column.type, nullable=column.null_count > 0)
for name, column in columns.items()
)
return pa.table(list(columns.values()), schema=schema)


def row_fingerprint(
tag: TagProtocol, data: DataProtocol, *, source_info: bool = True
) -> tuple[str, ...]:
Expand Down
Loading
Loading