From 5768e842bbbd0dfff7aaa1c01125c285ca7ebf42 Mon Sep 17 00:00:00 2001 From: "Edgar Y. Walker" Date: Mon, 23 Nov 2026 03:59:16 -0800 Subject: [PATCH] fix(streaming_joins): MergeJoin compares settled token types with an input that delivered no rows (ITL-1362) CodeRabbit on PR 520: when one input delivers no rows, the sync path reads it in its zero-row form (its declared source-info token types) and the other by its rows (the types they settle to); MergeJoin refuses two types of one column. The kernel compared the two inputs' settled types only, so a left input whose tokens are all [] (one list level) against an empty right declaring two levels completed with no row. When every input has closed, StreamingMergeJoin now compares each input with rows against every input without rows (require_shared_arrow_type, the sync error). Test: every kernel (Join, MergeJoin, SemiJoin), either input empty, tokens one or two levels deep: the kernel's outcome equals the sync path's (the same error, or the same rows). The two MergeJoin one-level cases fail without the fix. Co-Authored-By: Claude Opus 5.5 --- src/orcapod/core/operators/streaming_joins.py | 38 ++++++++++ .../test_record_batch_review_findings.py | 69 +++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/src/orcapod/core/operators/streaming_joins.py b/src/orcapod/core/operators/streaming_joins.py index 16e93f458..8089f0715 100644 --- a/src/orcapod/core/operators/streaming_joins.py +++ b/src/orcapod/core/operators/streaming_joins.py @@ -493,6 +493,7 @@ 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 @@ -500,6 +501,15 @@ def close(self, input_index: int) -> list[pa.RecordBatch]: 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. @@ -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: diff --git a/tests/test_channels/test_record_batch_review_findings.py b/tests/test_channels/test_record_batch_review_findings.py index 4b775ff11..2a2902143 100644 --- a/tests/test_channels/test_record_batch_review_findings.py +++ b/tests/test_channels/test_record_batch_review_findings.py @@ -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