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
38 changes: 38 additions & 0 deletions src/orcapod/core/operators/streaming_joins.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,23 @@ def close(self, input_index: int) -> list[pa.RecordBatch]:
self._closed[input_index] = True
emitted = self._emitted(self._close(input_index))
if all(self._closed):
self._check_settled_against_empty_inputs()
result = self._check_empty_inputs()
if result is not None and not self._emitted_any and not emitted:
# No row: the output is the sync path's result over the inputs as
# they ended, whose types an empty input decides.
self.zero_row_result = result
return emitted

def _check_settled_against_empty_inputs(self) -> None:
"""Compare the settled token types with the inputs that delivered no rows.

Called once every input has closed. The sync path reads an input with no
rows in its zero-row form, whose source-info token types are the declared
ones, and the others with the types their rows settle to. The kernels whose
sync path accepts any token types accept every such run.
"""

def _check_empty_inputs(self) -> StreamProtocol | None:
"""Apply the sync path's checks to the inputs as they ended.

Expand Down Expand Up @@ -857,6 +867,34 @@ def __init__(self, operator: MergeJoin, inputs: Sequence[StreamProtocol]) -> Non
).type_converter
self._link = _BinaryLink(sorted(self._shared_tags), self._finish)

def _check_settled_against_empty_inputs(self) -> None:
"""Refuse, when every input has closed, a run whose input with rows settled a
shared source-info column to another token type than an input with no rows
declares, as ``MergeJoin`` refuses the two Arrow types of one column.

The sync path reads the input with no rows in its zero-row form (its declared
token types) and the other one by its rows (their settled types); the two
inputs' settled types are compared as each batch arrives
(``_check_settled_token_types``).

Raises:
InputValidationError: If such a column's types differ.
"""
pod = StaticOutputOperatorPod
for empty in (0, 1):
other = 1 - empty
if self._received[empty] or not self._received[other]:
continue
declared = (
pod._materialize_upstream([], self.inputs[empty]) # pyright: ignore[reportPrivateUsage] # the operators' one form of an input
.as_table(columns=TRANSPORT_COLUMNS)
.schema
)
settled = self._token_types[other]
for name in sorted(settled.keys() & set(declared.names)):
types = {empty: declared.field(name).type, other: settled[name]}
_ = require_shared_arrow_type("MergeJoin", name, types[0], types[1])

def _check_settled_token_types(
self, input_index: int, token_types: Mapping[str, pa.DataType]
) -> None:
Expand Down
69 changes: 69 additions & 0 deletions tests/test_channels/test_record_batch_review_findings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,3 +1069,72 @@ async def go() -> OutputChannel:
output = asyncio.run(go())
with pytest.raises(RuntimeError, match="has no result"):
_ = output.result()


# --- CodeRabbit on PR 520: an input with no rows -------------------------------------


@pytest.mark.parametrize("tokens", ["one_level", "two_levels"])
@pytest.mark.parametrize("empty", [0, 1], ids=["left_empty", "right_empty"])
@pytest.mark.parametrize("kernel", ["join", "merge_join", "semi_join"])
def test_settled_tokens_against_an_input_with_no_rows_decide_as_the_sync_path(
kernel: str, empty: int, tokens: str
) -> None:
"""CodeRabbit on PR 520: one input delivers no rows, the other's tokens settle
``_source_x`` to one list level (``[]``) or to the two its type declares. The
sync path reads the empty input in its zero-row form (declared two levels) and
the other by its rows; ``MergeJoin`` refuses two types of one column. The kernel
decides as the sync path does when the last input closes: the same error, or the
same rows."""
from orcapod.core.operators import Join, MergeJoin, SemiJoin
from orcapod.core.operators.static_output_pod import StaticOutputOperatorPod
from orcapod.core.operators.streaming_joins import (
TRANSPORT_COLUMNS,
StreamingJoin,
StreamingMergeJoin,
StreamingSemiJoin,
)

filled = _deep_token_stream(
[[], []] if tokens == "one_level" else [[["a"]], [["b"]]], [1, 2]
)
nothing = _deep_token_stream([], [])
if kernel == "join":
# Join needs disjoint data: the other input under other data names.
nothing = ArrowTableStream(
pa.table(
{
"k": pa.array([], pa.int64()),
"w": pa.array([], pa.int64()),
"_source_w": pa.array([], DEEP_TOKENS),
}
),
tag_columns=["k"],
)
inputs = (nothing, filled) if empty == 0 else (filled, nothing)
operator, make = {
"join": (Join(), StreamingJoin),
"merge_join": (MergeJoin(), StreamingMergeJoin),
"semi_join": (SemiJoin(), StreamingSemiJoin),
}[kernel]

def rebuilt(stream: ArrowTableStream) -> Any: # pyright: ignore[reportExplicitAny]
return StaticOutputOperatorPod._materialize_upstream( # pyright: ignore[reportPrivateUsage] # the job's sync form
list(stream.iter_data()), stream
)

try:
sync: Any = operator.static_process(*map(rebuilt, inputs)).as_table().num_rows # pyright: ignore[reportExplicitAny]
except Exception as error: # noqa: BLE001 # the refusal is the outcome compared
sync = (type(error).__name__, str(error))

instance = make(operator, inputs) # pyright: ignore[reportArgumentType] # each kernel with its own operator, from one table
other = 1 - empty
sent = rebuilt(inputs[other]).as_table(columns=TRANSPORT_COLUMNS)
try:
out = instance.push(other, sent.combine_chunks().to_batches()[0])
out += instance.close(empty) + instance.close(other)
result: Any = sum(batch.num_rows for batch in out) # pyright: ignore[reportExplicitAny]
except Exception as error: # noqa: BLE001 # the refusal is the outcome compared
result = (type(error).__name__, str(error))
assert result == sync
Loading