diff --git a/src/orcapod/core/operators/streaming_joins.py b/src/orcapod/core/operators/streaming_joins.py index 6b9d64e7c..ab8ef14d9 100644 --- a/src/orcapod/core/operators/streaming_joins.py +++ b/src/orcapod/core/operators/streaming_joins.py @@ -1,10 +1,12 @@ """Streaming join kernels: symmetric hash joins on Arrow record batches (ITL-1362). -The async path of ``Join``, ``MergeJoin`` and ``SemiJoin`` (ITL-1347, stage 5) reads its -inputs as Arrow record batches, in whatever order they arrive, and must give the rows of -the operator's sync ``static_process`` over the same inputs, as a multiset (row order is -not a result on the async path; developer, November 22, 2026). A kernel is that -computation without the channels: the channel layer creates one per run from the input +The async path of ``Join``, ``MergeJoin``, ``SemiJoin`` and ``Rejoin`` (ITL-1347, stage +5) is to read its inputs as Arrow record batches, in whatever order they arrive; these +kernels are that computation, not yet wired into ``async_execute``. A kernel gives the +rows of the operator's sync ``static_process`` over the same inputs as a multiset (row +order is not a result on the async path; developer, November 22, 2026), within the +limits each kernel's docstring states (``StreamingRejoin``: repeated origins with three +or more inputs). A kernel is that computation without the channels: the channel layer creates one per run from the input streams (the descriptions) and hands it each arriving record batch (``push``) and each input's end (``close``); every call returns the output rows it decided, as record batches. @@ -99,6 +101,10 @@ _merged_list, # pyright: ignore[reportPrivateUsage] # MergeJoin's one rule _output_types, # pyright: ignore[reportPrivateUsage] # MergeJoin's one rule ) +from orcapod.core.operators.rejoin import ( + Rejoin, + _described, # pyright: ignore[reportPrivateUsage] # Rejoin's one rule +) from orcapod.core.operators.semijoin import ( SemiJoin, _has_whole_key, # pyright: ignore[reportPrivateUsage] # SemiJoin's one rule @@ -996,3 +1002,515 @@ def _close(self, input_index: int) -> list[pa.Table]: # No right row will arrive: the undecided left rows never match. self._waiting = None return [] + + +# The occurrence-number column of a Rejoin key frame, and the prefix of the other side's +# columns in the warning's pairs (never an input column: those never start so). +_OCCURRENCE = "__streaming_occurrence" +_OTHER = "__streaming_other_" + + +def _with_context(table: pa.Table, context_key: str) -> pa.Table: + """``table`` with the data context column every row of it carries on the sync path. + + An input a job rebuilds from rows carries its producer's data context on every row + (``StaticOutputOperatorPod._as_upstream_stream``), and ``Rejoin`` matches on it. + Transport batches carry no context column, so the kernel adds the input's own. + """ + return table.append_column( + pa.field(constants.CONTEXT_KEY, pa.large_string(), nullable=False), + pa.array([context_key] * table.num_rows, pa.large_string()), + ) + + +def _with_system_tags(table: pa.Table, keys: Sequence[str]) -> pa.Table: + """The rows of ``table`` whose system tags are all present: a null matches nothing.""" + if not table.num_rows: + return table + return table.filter(_has_whole_key(table, keys)) + + +def _take_rows(table: pa.Table, rows: np.ndarray[Any, np.dtype[np.int64]]) -> pa.Table: + """The rows of ``table`` with these numbers, in this order, without combining chunks. + + Arrow's ``take`` on a chunked column concatenates its chunks first, a copy of the + whole column. Here only the chunks that hold a wanted row are read: each one's rows + are taken from it alone, and the small result is put in the order asked for. + + Args: + table: A table, of any number of chunks. + rows: Row numbers in ``table``. + + Returns: + The rows, as one chunk. + """ + batches = table.to_batches() + if len(batches) <= 1: + return table.take(pa.array(rows, pa.int64())) + lengths = np.fromiter((b.num_rows for b in batches), np.int64, len(batches)) + starts = np.concatenate([np.zeros(1, np.int64), np.cumsum(lengths)[:-1]]) + chunks = np.searchsorted(starts, rows, side="right") - 1 + order = np.argsort(chunks, kind="stable") + sorted_rows, sorted_chunks = rows[order], chunks[order] + edges = np.flatnonzero(np.diff(sorted_chunks)) + 1 + parts: list[pa.Table] = [] + for group in np.split(np.arange(len(sorted_rows)), edges): + chunk = int(sorted_chunks[group[0]]) + local = sorted_rows[group] - starts[chunk] + parts.append( + pa.Table.from_batches([batches[chunk]]).take(pa.array(local, pa.int64())) + ) + taken = pa.concat_tables(parts).combine_chunks() + return taken.take(pa.array(np.argsort(order, kind="stable"), pa.int64())) + + +def _occurrences(frame: pl.DataFrame, among: Sequence[str]) -> pl.DataFrame: + """Number each row among the rows before it equal to it in ``among`` (0, 1, ...). + + With it, rows that repeat an origin row are matched one to one in the order they + arrived (``Rejoin``'s occurrence rule), and no join ever pairs every repeat of one + side with every repeat of the other: the pairs made are only the pairs kept. + """ + return frame.with_columns( + pl.int_range(pl.len()).over(list(among)).alias(_OCCURRENCE) + ) + + +class _Remaining: + """An input's rows not matched yet, with a key frame and a mask of removed rows. + + Removing a row only clears its mask bit; the rows are compacted when the removed + ones are the majority, so a batch never copies the whole table. + + Args: + columns: The shared columns of the key frame (system tags first). + row: The row-number column. + """ + + def __init__(self, columns: Sequence[str], row: str) -> None: + self.columns: list[str] = list(columns) + self.row: str = row + self.closed: bool = False + self.schema: pa.Schema | None = None + self._tables: list[pa.Table] = [] + self._frames: list[pl.DataFrame] = [] + self._alive: list[np.ndarray[Any, np.dtype[np.bool_]]] = [] + self._table: pa.Table | None = None + self._frame: pl.DataFrame | None = None + self.size: int = 0 + self.rows: int = 0 + + def append(self, table: pa.Table) -> None: + """Hold ``table``'s rows.""" + if not table.num_rows: + return + frame = polars_key_frame(table, self.columns, self.row) + self._frames.append(frame.with_columns(pl.col(self.row) + self.size)) + self._tables.append(table) + self._alive.append(np.ones(table.num_rows, dtype=np.bool_)) + self._table, self._frame = None, None + self.size += table.num_rows + self.rows += table.num_rows + + def table(self) -> pa.Table: + """Every row appended and not compacted away, numbered as in ``frame``.""" + if self._table is None: + self._table = pa.concat_tables(self._tables) + self._tables = [self._table] + return self._table + + def frame(self) -> pl.DataFrame: + """The key frame of the rows not removed, in the order they arrived.""" + if self._frame is None: + frame = pl.concat(self._frames, rechunk=False) + alive = np.concatenate(self._alive) + self._frames, self._alive = [frame], [alive] + self._frame = frame.filter(pl.Series(alive)) + return self._frame + + def remove(self, rows: np.ndarray[Any, np.dtype[np.int64]]) -> None: + """Remove the rows with these numbers (each held and not removed yet).""" + if not len(rows): + return + self.frame() + alive = self._alive[0] + alive[rows] = False + self.rows -= len(rows) + self._frame = None + if self.rows * 2 < self.size: + self._compact() + + def _compact(self) -> None: + """Drop the removed rows and number the rest from 0.""" + alive = np.concatenate(self._alive) + table = self.table().filter(pa.array(alive)) + self.clear() + self.append(table) + + def clear(self) -> None: + """Drop every row held.""" + self._tables, self._frames, self._alive = [], [], [] + self._table, self._frame = None, None + self.size = 0 + self.rows = 0 + + +class _RejoinLink: + """One step of ``Rejoin``: a symmetric match of two sides on every shared column. + + Args: + system: The system-tag columns, sorted. + others: The other columns both sides have (shared user tags, a shared data-tag's + source info, the data context), sorted. + finish: Turns the pairs' table into the link's output; given both schemas. + """ + + def __init__( + self, + system: Sequence[str], + others: Sequence[str], + finish: Callable[[pa.Table, pa.Schema, pa.Schema], pa.Table], + ) -> None: + self.system: list[str] = list(system) + self.others: list[str] = list(others) + self.shared: list[str] = [*system, *others] + self.sides: tuple[_Remaining, _Remaining] = ( + _Remaining(self.shared, _ROW[0]), + _Remaining(self.shared, _ROW[1]), + ) + self._finish: Callable[[pa.Table, pa.Schema, pa.Schema], pa.Table] = finish + # The shared columns other than system tags in which rows of one origin row + # (their system tags match) were seen to differ. + self.differing: set[str] = set() + + @property + def held_rows(self) -> int: + """The unmatched rows both sides hold.""" + return self.sides[0].rows + self.sides[1].rows + + @property + def exhausted(self) -> bool: + """Whether the link can never emit again: a side closed holding no row.""" + return any(side.closed and not side.rows for side in self.sides) + + def release(self) -> None: + """End both sides: the link's output is never needed again.""" + for side in self.sides: + side.closed = True + side.clear() + + def close(self, side: int) -> None: + """Note that ``side`` ends: the other side's rows can never match again.""" + self.sides[side].closed = True + self.sides[1 - side].clear() + + def push(self, side: int, table: pa.Table) -> pa.Table | None: + """Match ``table``'s rows with the other side's remaining rows. + + Args: + side: 0 for the left side, 1 for the right. + table: The arriving rows (with the data context column). + + Returns: + The output rows of the matched pairs, or ``None`` when there is none. + """ + this, other = self.sides[side], self.sides[1 - side] + if this.schema is None: + this.schema = table.schema + table = _with_system_tags(table, self.system) + if not table.num_rows: + return None + row, other_row = _ROW[side], _ROW[1 - side] + frame = _occurrences(polars_key_frame(table, self.shared, row), self.shared) + result = None + unmatched = np.ones(table.num_rows, dtype=np.bool_) + if other.rows: + held = _occurrences(other.frame(), self.shared) + pairs = frame.join( + held, on=[*self.shared, _OCCURRENCE], how="inner", nulls_equal=True + ) + if pairs.height: + mine = pairs.get_column(row).to_numpy() + theirs = pairs.get_column(other_row).to_numpy() + unmatched[mine] = False + tables = (table, other.table()) if side == 0 else (other.table(), table) + rows = (mine, theirs) if side == 0 else (theirs, mine) + result = self._pairs(*tables, *rows) + other.remove(theirs) + rest = table.filter(pa.array(unmatched)) if not unmatched.all() else table + if rest.num_rows and other.rows and self.others: + self._note_differing(rest, other, side) + if not other.closed: + this.append(rest) + return result + + def _pairs( + self, + left: pa.Table, + right: pa.Table, + left_rows: np.ndarray[Any, np.dtype[np.int64]], + right_rows: np.ndarray[Any, np.dtype[np.int64]], + ) -> pa.Table: + """The matched pairs as ``Rejoin._rejoin_pair`` builds them, by row number. + + Values are taken from the chunks that hold them (``_take_rows``): no held + table is copied. + """ + left_taken = _take_rows(left, left_rows) + right_taken = _take_rows(right, right_rows) + names: list[str] = [] + arrays: list[Any] = [] + for name in left.column_names: + names.append(name) + arrays.append(left_taken.column(name)) + for name in right.column_names: + if name not in left.column_names: + names.append(name) + arrays.append(right_taken.column(name)) + assert self.sides[0].schema is not None + assert self.sides[1].schema is not None + return self._finish( + pa.Table.from_arrays(arrays, names=names), + self.sides[0].schema, + self.sides[1].schema, + ) + + def _note_differing(self, rest: pa.Table, other: _Remaining, side: int) -> None: + """Note the shared columns in which unmatched rows of one origin row differ. + + One more join, of the arriving rows that matched nothing on the system tags only + against the other side's remaining rows, pairs the rows of one origin row (their + repeats in arrival order), with both sides' prepared values of the other shared + columns. Each such column is then compared over those pairs in one vectorized + expression, ``eq_missing`` on the prepared values: ``Join``'s equality (NaN + equals NaN, ``-0.0`` equals ``0.0``, at any depth) with a null equal to a null, + as the sync path compares them. + """ + row = _ROW[side] + names = [name for name in self.others if name not in self.differing] + if not names: + return + origin = [*self.system, _OCCURRENCE] + mine = _occurrences(polars_key_frame(rest, self.shared, row), self.system) + theirs = _occurrences(other.frame(), self.system).select( + [*origin, *(pl.col(name).alias(_OTHER + name) for name in names)] + ) + pairs = mine.select([*origin, *names]).join(theirs, on=origin, how="inner") + if not pairs.height: + return + differ = pairs.select( + (~pl.col(name).eq_missing(pl.col(_OTHER + name))).any().alias(name) + for name in names + ).row(0, named=True) + self.differing.update(name for name, differs in differ.items() if differs) + + +class StreamingRejoin(StreamingJoinKernel): + """``Rejoin`` on record batches: symmetric matches on every shared column. + + ITL-1355's rule, as ``Rejoin.static_process`` applies it: the inputs are taken in + ``Join``'s canonical order (by pipeline hash, ties in call order) and rejoined pair by + pair, the rows rejoined so far with the next input, each step an inner join on every + column both sides have (system tags, shared user tags, a shared data-tag's source + info, the data context) under ``Join``'s equality, a null equal to a null outside the + system tags and a null system tag matching nothing. Each step is a ``_RejoinLink``: + + - each side keeps only its rows not matched yet (a matched row leaves at once), so + the memory held is the unmatched rows; + - an arriving batch is matched, in one vectorized join, with the other side's + remaining rows; rows that repeat an origin row pair one to one in arrival order; + - once one side closes, the other side's remaining rows are dropped, and an arriving + row either matches at once or is dropped. + + Cost, per arriving batch and per step it reaches: at most two Polars joins against + the other side's remaining rows, the match and, when some arriving rows matched + nothing and the other side holds rows, one more on the system tags for the warning + (each other shared column then compared in one vectorized expression); and the + occurrence numbers of the remaining rows. That is O(held rows) per batch, as in the + ``Join`` kernels, plus O(m log m) to take the m pairs it makes from the held chunks + (``_take_rows``: one vectorized chunk lookup and one sort for all of them), never by + copying a whole held table; a compaction copies the remaining rows when half of + those held were removed. A long run of one-row batches against a large buffer is + quadratic in the rows. + + Rows: they equal the sync path's as a multiset, for every arrival schedule, when + origins are unique (system tags unique in each input, as every source makes them), + and also with repeated origins for two inputs (and at the first step of a chain): + the k-th repeat of one input pairs with the k-th of the other, in each input's row + order, as on the sync path. With three or more inputs and repeated origins, the + pairing of repeats at a later step can depend on arrival and differ from the sync + path, which ranks the rows rejoined so far in its table order among the rows that + survived the earlier step. The approved spec names this risk ("Duplicates ... 'match + once' pairs duplicates in arrival order, which can differ between sync and async + (risks; ITL-1356 removes the case)"); the developer's rule for ``Rejoin`` is that + system tags are unique by construction. The exact alternative (option X) is + survival-aware ranking: per-step counters, and rows of a repeated origin held until + every earlier row of that origin has a final fate at the step before. + + The data context: a job's rows carry their producer's data context, which ``Rejoin`` + matches on. Transport batches carry none, so each input's rows are given the data + context of its stream (``StaticOutputOperatorPod._upstream_context``): inputs of two + data contexts match nothing, and a link holds their rows to the end. + + The warning: rows of one origin row (system tags equal) that differ in another + shared column are noted per batch, between an arriving row that matched nothing and + the other side's remaining rows; when the last input closes, the kernel issues + ``RejoinDifferingColumnWarning`` once for the columns noted, through the operator + (``Rejoin._warn_differing_once``), as the sync path issues it once for its run. + + - Guaranteed: with two inputs and unique origins, the warning is the sync path's + (the same columns, once) for every arrival schedule. + - Best effort otherwise (provisional, November 22, 2026; the rows are never + affected): the kernel reports only differences between rows the chain actually + compares, so a difference can be missed, and with repeated origins it can depend + on arrival. With three or more inputs a step compares the rows rejoined so far + with the next input: a difference between two inputs for an origin row that an + input before the later of them lacks is not seen (whether or not the two are + adjacent in the chain). With repeated origins a repeat already matched is no + longer compared, while the sync path compares repeats by occurrence whatever + their matches. The sync path compares every pair of inputs over all their rows; + the alternative (option B) is pairwise detection, which would keep every input's + shared-column keys for every row until all inputs close. + + ``Rejoin`` refuses nothing for what rows hold: every refusal it raises for its inputs' + columns and types comes before the first batch, except the base class's check of an + input that ends empty, which can refuse at the last close (``StreamingJoinKernel``). + + Args: + operator: The ``Rejoin``. + inputs: Its input streams, in call order. + """ + + def __init__(self, operator: Rejoin, inputs: Sequence[StreamProtocol]) -> None: + super().__init__(operator, inputs) + self._rejoin = operator + order = sorted( + range(len(inputs)), key=lambda i: inputs[i].pipeline_hash().to_hex() + ) + self._position = {index: position for position, index in enumerate(order)} + self._contexts = [ + StaticOutputOperatorPod._upstream_context(stream).context_key # pyright: ignore[reportPrivateUsage] # the operators' one rule + for stream in inputs + ] + self._user_tags: set[str] = { + name for stream in inputs for name in stream.keys()[0] + } + self._links: list[_RejoinLink] = [] + if len(inputs) == 1: + return + layouts = [self._layouts[i] for i in order] + system = sorted( + name + for name in layouts[0].names + if name.startswith(constants.SYSTEM_TAG_PREFIX) + ) + left_names = [*layouts[0].names, constants.CONTEXT_KEY] + user_tags = set(inputs[order[0]].keys()[0]) + for index in order[1:]: + right_names = [*self._layouts[index].names, constants.CONTEXT_KEY] + others = sorted( + name + for name in right_names + if name in left_names and name not in system + ) + next_tags = set(inputs[index].keys()[0]) + self._links.append( + _RejoinLink(system, others, self._link_finish(user_tags & next_tags)) + ) + left_names = [ + *left_names, + *(name for name in right_names if name not in left_names), + ] + user_tags |= next_tags + + @property + def held_rows(self) -> int: + return sum(link.held_rows for link in self._links) + + def _link_finish( + self, shared_tags: set[str] + ) -> Callable[[pa.Table, pa.Schema, pa.Schema], pa.Table]: + """What one step does to its pairs, as ``Rejoin._rejoin_pair`` does.""" + + def finish( + table: pa.Table, left_schema: pa.Schema, right_schema: pa.Schema + ) -> pa.Table: + table = arrow_utils.restore_schema_nullability( + table, left_schema, right_schema + ) + return Join._shared_key_nullability( # pyright: ignore[reportPrivateUsage] # Join's one rule + table, left_schema, right_schema, shared_tags + ) + + return finish + + def _push(self, input_index: int, table: pa.Table) -> list[pa.Table]: + """Feed an input's rows into its step and forward what each step emits. + + Args: + input_index: The input, in call order. + table: Its rows, never empty. + + Returns: + The last step's output, possibly none. + """ + if not self._links: + return [table] + table = _with_context(table, self._contexts[input_index]) + position = self._position[input_index] + step, side = (0, 0) if position == 0 else (position - 1, 1) + result = self._links[step].push(side, table) + while result is not None and step < len(self._links) - 1: + step += 1 + result = self._links[step].push(0, result) + # A match can take the last row a closed side held: that step is exhausted. + self._release_exhausted() + return [] if result is None else [result] + + def _release_exhausted(self) -> None: + """Release what a step that can never emit again makes useless. + + Such a step (a side closed holding no row, ``_RejoinLink.exhausted``) closes its + successor's left side, and every earlier step is released: its rows could only + reach this one. A step is exhausted by a close, or by a match that takes the + last row a closed side held. + """ + for step, link in enumerate(self._links): + if not link.exhausted: + continue + for earlier in self._links[:step]: + earlier.release() + if ( + step + 1 < len(self._links) + and not self._links[step + 1].sides[0].closed + ): + self._links[step + 1].close(0) + + def _close(self, input_index: int) -> list[pa.Table]: + """End an input's side, every side that can no longer receive, and warn last. + + A step that can never emit again closes its successor's left side and releases + every earlier step, whose rows could only reach it. When the last input closes, + the differing columns noted are warned of once. + + Args: + input_index: The input, in call order. + + Returns: + No table. + """ + if not self._links: + return [] + position = self._position[input_index] + step, side = (0, 0) if position == 0 else (position - 1, 1) + self._links[step].close(side) + self._release_exhausted() + if all(self._closed): + differing = {name for link in self._links for name in link.differing} + if differing: + self._rejoin._warn_differing_once( # pyright: ignore[reportPrivateUsage] # Rejoin's one rule + tuple( + sorted(_described(name, self._user_tags) for name in differing) + ) + ) + return [] diff --git a/tests/test_core/operators/test_streaming_rejoin.py b/tests/test_core/operators/test_streaming_rejoin.py new file mode 100644 index 000000000..f33819534 --- /dev/null +++ b/tests/test_core/operators/test_streaming_rejoin.py @@ -0,0 +1,777 @@ +"""The streaming Rejoin kernel gives the sync Rejoin's rows, within its stated limits. + +ITL-1362 (ITL-1347, stage 5): ``StreamingRejoin`` is the streaming form of ``Rejoin`` on +Arrow record batches, not yet wired into ``async_execute`` (which is still the barrier). +It applies ITL-1355's rule: an inner join on every column the inputs share (system +tags, shared user tags, a shared data-tag's source info, the data context), ``Join``'s +equality, a null equal to a null outside the system tags, a null system tag matching +nothing, each row matching at most once. Its rows equal the sync rows as a multiset for +every schedule with unique origins, and with repeated origins for two inputs; with +three or more inputs and repeated origins, arrival order can change the pairing +(``test_repeats_with_three_inputs_can_pair_by_arrival``), and the warning is guaranteed +only for two inputs with unique origins; those documented differences are pinned below. +These are direct tests of the kernel, not of an async execution path. The oracle and the arrivals are those of +``test_streaming_joins``: the sync operator over the inputs rebuilt from rows, compared +as a multiset, after random batch cuts and interleavings. +""" + +from __future__ import annotations + +import dataclasses +import itertools +import math +import random +import warnings +from collections.abc import Sequence +from pathlib import Path +from typing import Any + +import polars as pl +import pyarrow as pa +import pytest + +from orcapod import contexts +from orcapod.core.operators import Rejoin +from orcapod.core.operators.streaming_joins import ( + StreamingJoinKernel, + StreamingRejoin, +) +from orcapod.core.sources import ArrowTableSource +from orcapod.core.streams import ArrowTableStream +from orcapod.errors import InputValidationError, RejoinDifferingColumnWarning +from orcapod.protocols.core_protocols import StreamProtocol +from orcapod.system_constants import constants + +from .test_streaming_joins import ( + _arrivals, # pyright: ignore[reportPrivateUsage] # the shared harness + _rows, # pyright: ignore[reportPrivateUsage] # the shared harness + _check, # pyright: ignore[reportPrivateUsage] # the shared harness + _row_built, # pyright: ignore[reportPrivateUsage] # the shared harness + _run, # pyright: ignore[reportPrivateUsage] # the shared harness + _sent, # pyright: ignore[reportPrivateUsage] # the shared harness + _transport, # pyright: ignore[reportPrivateUsage] # the shared harness + assert_same_rows, +) + + +@pytest.fixture(autouse=True) +def _quiet_provisional() -> Any: + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + yield + + +def _kernel(inputs: Sequence[StreamProtocol]) -> StreamingJoinKernel: + return StreamingRejoin(Rejoin(), inputs) + + +def _origin(n: int = 12, *, data_tags: bool = False) -> ArrowTableSource: + """``n`` rows: tags ``subject`` and ``unit``; data ``rate``, ``depth``, ``area``.""" + return ArrowTableSource( + pa.table( + { + "subject": pa.array([f"s{i % 3}" for i in range(n)], pa.large_string()), + "unit": pa.array(list(range(n)), pa.int64()), + "rate": pa.array([float(i) for i in range(n)], pa.float64()), + "depth": pa.array([10 * i for i in range(n)], pa.int64()), + "area": pa.array(["V1"] * n, pa.large_string()), + } + ), + tag_columns=["subject", "unit"], + tag_only_columns=False if data_tags else True, + source_id="units", + ) + + +def _branches(origin: Any) -> list[Any]: + """Three branches of one stream, each with its own data column and rows.""" + return [ + origin.select_data_columns(["rate"]), + origin.polars_filter(pl.col("depth") >= 30).select_data_columns(["depth"]), + origin.polars_filter(pl.col("rate") < 9.0).select_data_columns(["area"]), + ] + + +# --------------------------------------------------------------------------- +# The rows, against the sync path +# --------------------------------------------------------------------------- + + +@pytest.mark.parametrize("data_tags", [False, True]) +def test_two_branches_in_both_orders(data_tags: bool) -> None: + first, second, _ = _branches(_origin(data_tags=data_tags)) + for inputs in ((first, second), (second, first)): + _check(_kernel, Rejoin().static_process, inputs) + + +@pytest.mark.parametrize("order", list(itertools.permutations(range(3)))) +def test_three_branches_in_every_order(order: tuple[int, ...]) -> None: + branches = _branches(_origin(data_tags=True)) + _check(_kernel, Rejoin().static_process, [branches[i] for i in order], range(6)) + + +def test_a_single_input_passes_its_rows() -> None: + _check(_kernel, Rejoin().static_process, (_branches(_origin())[0],)) + + +def test_a_shared_tag_that_differs_on_some_rows_matches_only_where_equal() -> None: + origin = _origin() + left = origin.select_data_columns(["rate"]).add_tags(run=1) + right = origin.polars_filter(pl.col("unit") < 6).select_data_columns(["depth"]) + right = right.add_tags(run=1) + other = origin.polars_filter(pl.col("unit") >= 6).select_data_columns(["depth"]) + other = other.add_tags(run=2) + for inputs in ((left, right), (right, left), (left, other), (other, left)): + _check(_kernel, Rejoin().static_process, inputs) + + +def _hand_branch( + data: str, *, origin: list[str | None], tag: list[int | None] +) -> ArrowTableStream: + """A hand-built branch: tag ``k``, one system tag, data column ``data``.""" + n = len(origin) + return ArrowTableStream( + pa.table( + { + "k": pa.array(tag, pa.int64()), + data: pa.array([float(i) for i in range(n)], pa.float64()), + "origin": pa.array(origin, pa.large_string()), + } + ), + tag_columns=["k"], + system_tag_columns=["origin"], + ) + + +def test_a_null_system_tag_matches_nothing_and_a_null_tag_equals_a_null() -> None: + # Rows rebuilt from datagrams cannot hold a null system tag (provenance is never + # null), so the oracle here is the sync path over the raw streams, and the + # kernel is fed their tables. + left = _hand_branch("a", origin=["r1", None, "r3", "r4"], tag=[1, 2, None, 4]) + right = _hand_branch("b", origin=["r1", None, "r3", "r4"], tag=[1, 2, None, 5]) + for inputs in ((left, right), (right, left)): + expected = _transport(Rejoin().static_process(*inputs)) + assert sorted(expected.column("k").to_pylist(), key=str) == [1, None] + tables = [_transport(stream) for stream in inputs] + for seed in range(6): + kernel = StreamingRejoin(Rejoin(), inputs) + batches = _run(kernel, _arrivals(tables, random.Random(seed))) + assert_same_rows(batches, kernel, expected) + assert kernel.held_rows == 0 + + +def _repeated(n: int, data: str, k: list[int] | None = None) -> ArrowTableStream: + """``n`` rows of ONE origin row: tag ``k``, one system tag, data ``data`` = 0..n-1.""" + return ArrowTableStream( + pa.table( + { + "k": pa.array(k or [1] * n, pa.int64()), + data: pa.array(range(n), pa.int64()), + "origin": pa.array(["x"] * n, pa.large_string()), + } + ), + tag_columns=["k"], + system_tag_columns=["origin"], + ) + + +def test_repeats_of_an_origin_row_pair_one_to_one_in_arrival_order() -> None: + # Each input's batches arrive in its row order, so the arrival-order pairing of + # the occurrence rule is the sync path's row-order pairing. + for left, right in ( + (_repeated(9, "a"), _repeated(6, "b")), + (_repeated(8, "a", [1, 2] * 4), _repeated(7, "b", [2, 1, 2, 2, 1, 1, 2])), + ): + for inputs in ((left, right), (right, left)): + _check(_kernel, Rejoin().static_process, inputs) + + +def test_thousands_of_repeats_stay_linear() -> None: + # 3,000 rows of one origin row on each side, in one-row batches: each arriving row + # pairs with one remaining row (never with every repeat), and the remaining set + # holds at most the rows not paired yet. + n = 3000 + left, right = _repeated(n, "a"), _repeated(n - 7, "b") + kernel = StreamingRejoin(Rejoin(), (left, right)) + out: list[pa.RecordBatch] = [] + largest = 0 + lefts = _sent(left).combine_chunks().to_batches(max_chunksize=1) + rights = _sent(right).combine_chunks().to_batches(max_chunksize=1) + for index in range(n): + out += kernel.push(0, lefts[index]) + if index < len(rights): + out += kernel.push(1, rights[index]) + largest = max(largest, kernel.held_rows) + assert largest <= 8 + assert sum(batch.num_rows for batch in out) == n - 7 + assert kernel.held_rows == 7 + kernel.close(1) + assert kernel.held_rows == 0 + kernel.close(0) + + +# --------------------------------------------------------------------------- +# What the kernel holds +# --------------------------------------------------------------------------- + + +def test_only_unmatched_rows_are_held() -> None: + first, second, _ = _branches(_origin()) + kernel = StreamingRejoin(Rejoin(), (first, second)) + first_rows, second_rows = _sent(first), _sent(second) + kernel.push(0, first_rows.combine_chunks().to_batches()[0]) + assert kernel.held_rows == first_rows.num_rows + emitted = kernel.push(1, second_rows.combine_chunks().to_batches()[0]) + matched = sum(batch.num_rows for batch in emitted) + assert matched == second_rows.num_rows + # Every matched row left both sides: only the first input's unmatched rows stay. + assert kernel.held_rows == first_rows.num_rows - matched + kernel.close(1) + assert kernel.held_rows == 0 + kernel.close(0) + + +def test_after_one_input_closes_the_other_inputs_rows_are_not_held() -> None: + first, second, _ = _branches(_origin()) + second_rows = _sent(second).combine_chunks() + kernel = StreamingRejoin(Rejoin(), (first, second)) + kernel.push(1, second_rows.slice(0, 3).to_batches()[0]) + assert kernel.held_rows == 3 + # The first input ends with no row: the second's rows can never match. + kernel.close(0) + assert kernel.held_rows == 0 + kernel.push(1, second_rows.slice(3).to_batches()[0]) + assert kernel.held_rows == 0 + assert kernel.close(1) == [] + + +def test_three_inputs_release_every_step_once_an_input_ends_empty() -> None: + branches = _branches(_origin(data_tags=True)) + for empty in range(3): + kernel = StreamingRejoin(Rejoin(), branches) + kernel.close(empty) + for index in range(3): + if index != empty: + kernel.push( + index, _sent(branches[index]).combine_chunks().to_batches()[0] + ) + assert kernel.held_rows == 0, empty + + +# --------------------------------------------------------------------------- +# The warning and the data context +# --------------------------------------------------------------------------- + + +def _warnings_of(run: Any) -> list[str]: + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + run() + return sorted( + str(w.message) + for w in caught + if issubclass(w.category, RejoinDifferingColumnWarning) + ) + + +def _sync_warnings(inputs: Sequence[StreamProtocol]) -> list[str]: + rebuilt = [_row_built(stream) for stream in inputs] + return _warnings_of(lambda: Rejoin().static_process(*rebuilt)) + + +def _kernel_warnings(inputs: Sequence[StreamProtocol], seed: int) -> list[str]: + tables = [_sent(stream) for stream in inputs] + + def run() -> None: + kernel = StreamingRejoin(Rejoin(), inputs) + _run(kernel, _arrivals(tables, random.Random(seed))) + + return _warnings_of(run) + + +def test_two_inputs_that_differ_in_shared_columns_warn_as_the_sync_path() -> None: + # Rows of one origin row whose shared tag ``k`` differs: a non-match, and one + # warning for the run naming the column, as on the sync path. + left = _hand_branch("a", origin=["r1", "r2", "r3"], tag=[1, 2, 3]) + for right in ( + _hand_branch("b", origin=["r1", "r2", "r3"], tag=[1, 9, 3]), + _hand_branch("b", origin=["r1", "r2", "r3"], tag=[1, 2, 3]), + _hand_branch("b", origin=["r4", "r2", "r3"], tag=[1, None, 3]), + ): + for inputs in ((left, right), (right, left)): + want = _sync_warnings(inputs) + for seed in range(6): + assert _kernel_warnings(inputs, seed) == want + _check(_kernel, Rejoin().static_process, inputs, range(4)) + + +OTHER_CONTEXT = "std:v1:test-other" + + +@pytest.fixture +def other_context(monkeypatch: pytest.MonkeyPatch) -> contexts.DataContext: + """A copy of the default data context under another key (as ``test_rejoin``).""" + other = dataclasses.replace( + contexts.get_default_context(), context_key=OTHER_CONTEXT + ) + registry = contexts._get_registry() # pyright: ignore[reportPrivateUsage] # the test registers a second context + original = registry.get_context + + def get_context(key: Any = None) -> contexts.DataContext: # pyright: ignore[reportExplicitAny] # the registry's own argument + return other if key == OTHER_CONTEXT else original(key) + + monkeypatch.setattr(registry, "get_context", get_context) + return other + + +def _in_context(context: contexts.DataContext, data: str) -> ArrowTableStream: + """A raw stream of the given data context: tag ``g``, one system tag, ``data``.""" + return ArrowTableStream( + pa.table( + { + "g": pa.array([1, 2, 3], pa.int64()), + data: pa.array([0.5, 0.25, 0.125], pa.float64()), + "origin": pa.array(["r1", "r2", "r3"], pa.large_string()), + constants.CONTEXT_KEY: pa.array( + [context.context_key] * 3, pa.large_string() + ), + } + ), + tag_columns=["g"], + system_tag_columns=["origin"], + data_context=context, + ) + + +def test_inputs_of_two_data_contexts_match_nothing_and_warn_as_the_sync_path( + other_context: contexts.DataContext, +) -> None: + default = contexts.get_default_context() + for inputs in ( + (_in_context(default, "a"), _in_context(other_context, "b")), + (_in_context(other_context, "b"), _in_context(default, "a")), + ): + want = _sync_warnings(inputs) + assert want + for seed in range(4): + assert _kernel_warnings(inputs, seed) == want + _check(_kernel, Rejoin().static_process, inputs, range(4)) + + +# --------------------------------------------------------------------------- +# Refusals before the first batch (e1, ITL-856) +# --------------------------------------------------------------------------- + + +def test_refusals_are_the_sync_paths_before_the_first_batch() -> None: + origin = _origin() + joined = origin.select_data_columns(["rate"]).join( + ArrowTableSource( + pa.table({"subject": pa.array(["s0"], pa.large_string()), "x": [1]}), + tag_columns=["subject"], + tag_only_columns=True, + source_id="x", + ) + ) + left = _hand_branch("a", origin=["r1"], tag=[1]) + retyped = ArrowTableStream( + pa.table( + { + "k": pa.array([1], pa.int32()), + "b": pa.array([0.0]), + "origin": pa.array(["r1"], pa.large_string()), + } + ), + tag_columns=["k"], + system_tag_columns=["origin"], + ) + for inputs in ( + (origin.select_data_columns(["depth"]), joined), + (left, retyped), + (left, _hand_branch("a", origin=["r1"], tag=[1])), + ): + with pytest.raises(InputValidationError) as sync_error: + Rejoin().static_process(*map(_row_built, inputs)) + with pytest.raises(InputValidationError) as kernel_error: + StreamingRejoin(Rejoin(), inputs) + assert str(kernel_error.value) == str(sync_error.value) + + +def test_every_batch_names_the_output_columns_in_order() -> None: + branches = _branches(_origin(data_tags=True)) + kernel = StreamingRejoin(Rejoin(), branches) + batches = _run( + kernel, _arrivals([_sent(stream) for stream in branches], random.Random(1)) + ) + assert batches + expected = _transport(Rejoin().static_process(*map(_row_built, branches))) + assert_same_rows(batches, kernel, expected) + + +def test_three_inputs_miss_a_difference_the_middle_input_lacks() -> None: + # A documented difference from the sync path (StreamingRejoin's docstring): with + # three inputs the warning is detected per link, so a difference between the first + # and the last input (canonical order) for an origin row the middle input lacks is + # warned about by the sync path only. The rows are identical. The alternative, + # pairwise detection with every input's keys kept to the end, was not chosen + # (option B, November 22, 2026). + names = ["a", "b", "c"] + order = sorted( + names, + key=lambda n: ( + _hand_branch(n, origin=["r1", "r2"], tag=[1, 2]).pipeline_hash().to_hex() + ), + ) + first, middle, last = order + inputs = [ + _hand_branch(first, origin=["r1", "r2"], tag=[1, 2]), + _hand_branch(middle, origin=["r1"], tag=[1]), + _hand_branch(last, origin=["r1", "r2"], tag=[1, 9]), + ] + assert [ + inputs[i] + for i in sorted(range(3), key=lambda i: inputs[i].pipeline_hash().to_hex()) + ] == inputs + assert len(_sync_warnings(inputs)) == 1 + for seed in range(4): + assert _kernel_warnings(inputs, seed) == [] + _check(_kernel, Rejoin().static_process, inputs, range(4)) + + +def test_a_match_that_takes_a_closed_inputs_last_row_releases_the_chain() -> None: + # Canonical order first, middle, last: the last input sends its only row (x) and + # closes; the first sends x and y; the middle sends x, which matches through to + # the last input's x. The last step then holds nothing on a closed side and can + # never emit again, so the first step's y, and any row the first input sends + # after, are not held. + names = sorted( + "abc", + key=lambda n: _hand_branch(n, origin=["x"], tag=[1]).pipeline_hash().to_hex(), + ) + first = _hand_branch(names[0], origin=["x", "y"], tag=[1, 2]) + middle = _hand_branch(names[1], origin=["x"], tag=[1]) + last = _hand_branch(names[2], origin=["x"], tag=[1]) + inputs = [first, middle, last] + assert sorted(inputs, key=lambda s: s.pipeline_hash().to_hex()) == inputs + kernel = StreamingRejoin(Rejoin(), inputs) + assert kernel.push(2, _sent(last).combine_chunks().to_batches()[0]) == [] + assert kernel.close(2) == [] + assert kernel.push(0, _sent(first).combine_chunks().to_batches()[0]) == [] + emitted = kernel.push(1, _sent(middle).combine_chunks().to_batches()[0]) + assert sum(batch.num_rows for batch in emitted) == 1 + assert kernel.held_rows == 0 + more = _hand_branch(names[0], origin=["z"], tag=[3]) + assert kernel.push(0, _sent(more).combine_chunks().to_batches()[0]) == [] + assert kernel.held_rows == 0 + + +# --------------------------------------------------------------------------- +# Documented differences from the sync path (StreamingRejoin's docstring) +# --------------------------------------------------------------------------- + + +def _in_canonical_order(make: Any, candidates: Sequence[tuple[str, ...]]) -> list[Any]: + """The first ``make(names)`` whose streams are already in canonical order.""" + for names in candidates: + inputs = make(names) + if sorted(inputs, key=lambda s: s.pipeline_hash().to_hex()) == inputs: + return inputs + raise AssertionError("no arrangement in canonical order") + + +def _pushed( + inputs: Sequence[StreamProtocol], schedule: Sequence[tuple[int, int, int]] +) -> tuple[pa.Table, list[str]]: + """The kernel's rows and warnings for ``schedule``: (input, first row, row count), + each input's rows in its own order, then every input closed.""" + rows = [_sent(stream).combine_chunks() for stream in inputs] + out: list[pa.RecordBatch] = [] + + def run() -> None: + kernel = StreamingRejoin(Rejoin(), inputs) + for index, start, count in schedule: + out.extend( + kernel.push(index, rows[index].slice(start, count).to_batches()[0]) + ) + for index in range(len(inputs)): + out.extend(kernel.close(index)) + + caught = _warnings_of(run) + return pa.Table.from_batches(out), caught + + +def test_repeats_with_three_inputs_can_pair_by_arrival() -> None: + # The spec's named risk ("Duplicates ... 'match once' pairs duplicates in arrival + # order, which can differ between sync and async (risks; ITL-1356 removes the + # case)"). One origin row repeated: the first two inputs pair their repeats on + # tag k (orders [1, 2] and [2, 1]); the third, without k, pairs by occurrence + # with the rows rejoined so far. The sync path ranks those in its table order, + # among the rows that survived the first step; the kernel ranks them as they + # arrive. Two schedules that keep each input's row order: one gives the sync + # rows, one does not. The exact alternative is option X: survival-aware ranking + # with per-step counters, rows of a repeated origin held until every earlier row + # of that origin has a final fate at the step before. + def keyed(data: str, k: list[int]) -> ArrowTableStream: + return _repeated(len(k), data, k) + + def plain(data: str) -> ArrowTableStream: + return ArrowTableStream( + pa.table( + { + data: pa.array([10, 20], pa.int64()), + "origin": pa.array(["x", "x"], pa.large_string()), + } + ), + tag_columns=[], + system_tag_columns=["origin"], + ) + + inputs = _in_canonical_order( + lambda n: [keyed(n[0], [1, 2]), keyed(n[1], [2, 1]), plain(n[2])], + list(itertools.permutations("abcdefgh", 3)), + ) + want = _rows(_transport(Rejoin().static_process(*map(_row_built, inputs)))) + whole, _ = _pushed(inputs, [(0, 0, 2), (1, 0, 2), (2, 0, 2)]) + one_by_one, _ = _pushed(inputs, [(0, 0, 2), (1, 0, 1), (1, 1, 1), (2, 0, 2)]) + assert _rows(whole) == want + assert _rows(one_by_one) != want + assert whole.num_rows == one_by_one.num_rows == 2 + + +def test_the_warning_for_repeats_of_two_inputs_can_depend_on_arrival() -> None: + # Best effort with repeated origins: the sync path compares repeats by occurrence + # whatever their matches, the kernel only rows that matched nothing. One origin, + # k = [1, 2] against [2, 1]: the rows are the sync rows in both schedules; the + # warning is the sync path's when the rows alternate, and missed when the first + # input's rows are all held first. The alternative is option B: pairwise + # detection with every input's keys kept to the end. + inputs = [ + _hand_branch("a", origin=["o", "o"], tag=[1, 2]), + _hand_branch("b", origin=["o", "o"], tag=[2, 1]), + ] + want_rows = _rows(_transport(Rejoin().static_process(*map(_row_built, inputs)))) + assert len(_sync_warnings(inputs)) == 1 + buffered, buffered_warnings = _pushed(inputs, [(0, 0, 2), (1, 0, 1), (1, 1, 1)]) + alternating, alternating_warnings = _pushed( + inputs, [(0, 0, 1), (1, 0, 1), (0, 1, 1), (1, 1, 1)] + ) + assert _rows(buffered) == _rows(alternating) == want_rows + assert buffered_warnings == [] + assert alternating_warnings == _sync_warnings(inputs) + + +def test_the_warning_misses_adjacent_inputs_when_an_earlier_input_lacks_the_row() -> ( + None +): + # Best effort with three inputs: the first input (canonical order) has only origin + # y; the next two have origin x with different k. They are adjacent, but their x + # rows are never compared: the step that would compare them sees the rows + # rejoined so far, which lack x. The sync path warns; the rows are identical. The + # alternative is option B: pairwise detection with every input's keys kept. + inputs = _in_canonical_order( + lambda n: [ + _hand_branch(n[0], origin=["y"], tag=[1]), + _hand_branch(n[1], origin=["x"], tag=[1]), + _hand_branch(n[2], origin=["x"], tag=[2]), + ], + list(itertools.permutations("abcdef", 3)), + ) + assert len(_sync_warnings(inputs)) == 1 + for seed in range(4): + assert _kernel_warnings(inputs, seed) == [] + _check(_kernel, Rejoin().static_process, inputs, range(4)) + + +def test_two_inputs_with_unique_origins_warn_as_the_sync_path_for_every_schedule() -> ( + None +): + # The guarantee: two inputs, unique origins, every arrival schedule. + left = _hand_branch("a", origin=["r1", "r2", "r3", "r4"], tag=[1, 2, 3, None]) + right = _hand_branch("b", origin=["r4", "r3", "r2", "r5"], tag=[None, 7, 2, 1]) + for inputs in ((left, right), (right, left)): + want = _sync_warnings(inputs) + assert len(want) == 1 + for seed in range(24): + assert _kernel_warnings(inputs, seed) == want + + +# --------------------------------------------------------------------------- +# Cost: one join per batch, no whole-table copy +# --------------------------------------------------------------------------- + + +_POOLS: list[Any] = [] + + +def test_each_batch_costs_one_join_and_copies_no_held_table( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # N repeats held in N one-row chunks, then their N partners one row at a time: + # each partner batch runs one Polars join against the held rows (O(held) per + # batch, as in the Join kernels) and takes its values from the held chunks by row + # number; no held table is copied, so a batch allocates far less Arrow memory + # than the held rows occupy. + n = 400 + left, right = _repeated(n, "a"), _repeated(n, "b") + kernel = StreamingRejoin(Rejoin(), (left, right)) + for batch in _sent(left).combine_chunks().to_batches(max_chunksize=1): + kernel.push(0, batch) + held_bytes = _sent(left).nbytes + joins = 0 + original = pl.DataFrame.join + + def counted(self: pl.DataFrame, *args: Any, **kwargs: Any) -> pl.DataFrame: + nonlocal joins + joins += 1 + return original(self, *args, **kwargs) + + monkeypatch.setattr(pl.DataFrame, "join", counted) + previous = pa.default_memory_pool() + large: list[int] = [] + for index, batch in enumerate( + _sent(right).combine_chunks().to_batches(max_chunksize=1) + ): + before_joins = joins + # A pool of its own per batch: its peak counts every Arrow allocation of the + # batch, also one freed before the batch returns. Pools are kept for the + # process: arrays allocated in one may outlive the test. + pool = pa.proxy_memory_pool(previous) + _POOLS.append(pool) + pa.set_memory_pool(pool) + try: + emitted = kernel.push(1, batch) + finally: + pa.set_memory_pool(previous) + assert sum(b.num_rows for b in emitted) == 1 + assert joins - before_joins == 1 + if (pool.max_memory() or 0) >= held_bytes / 4: + large.append(index) + # Only the compactions copy the remaining rows: when half the rows held were + # removed since the last one, so at most log2(n) times in the run. + assert len(large) <= math.ceil(math.log2(n)), large + assert kernel.held_rows == 0 + + +def test_a_partly_matching_batch_costs_two_joins( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # Held: origins x and z with k = [1, 1]. Arriving in one batch: x and z with + # k = [2, 1]. z matches (the match join); x matched nothing but shares its + # origin with a held row, so one more join on the system tags finds the pair and + # a vectorized comparison names k. Two joins, and the warning is the sync path's. + left = _hand_branch("a", origin=["x", "z"], tag=[1, 1]) + right = _hand_branch("b", origin=["x", "z"], tag=[2, 1]) + joins = 0 + original = pl.DataFrame.join + + def counted(self: pl.DataFrame, *args: Any, **kwargs: Any) -> pl.DataFrame: + nonlocal joins + joins += 1 + return original(self, *args, **kwargs) + + kernel = StreamingRejoin(Rejoin(), (left, right)) + kernel.push(0, _sent(left).combine_chunks().to_batches()[0]) + monkeypatch.setattr(pl.DataFrame, "join", counted) + caught: list[pa.RecordBatch] = [] + + def run() -> None: + caught.extend(kernel.push(1, _sent(right).combine_chunks().to_batches()[0])) + caught.extend(kernel.close(0) + kernel.close(1)) + + found = _warnings_of(run) + monkeypatch.undo() + assert joins == 2 + assert sum(batch.num_rows for batch in caught) == 1 + assert found == _sync_warnings((left, right)) + assert len(found) == 1 + + +def test_four_inputs_keep_buffered_matches_when_earlier_steps_release() -> None: + # Canonical inputs I0..I3. x and y match through the first two steps and wait at + # the last step for I3. Then I1 closes: the steps before the last can never emit + # again and are released, but the last step keeps its two buffered rows, which + # I3 then matches. A late row of I0 (z) goes to a released step and is not held. + # Every call order of the four inputs gives the sync rows. + names = sorted( + "abcd", + key=lambda n: _hand_branch(n, origin=["x"], tag=[1]).pipeline_hash().to_hex(), + ) + canonical = [ + _hand_branch(names[0], origin=["x", "y", "z"], tag=[1, 2, 3]), + *(_hand_branch(n, origin=["x", "y"], tag=[1, 2]) for n in names[1:]), + ] + assert sorted(canonical, key=lambda s: s.pipeline_hash().to_hex()) == canonical + expected = _transport(Rejoin().static_process(*map(_row_built, canonical))) + assert expected.num_rows == 2 + sent = [_sent(stream).combine_chunks() for stream in canonical] + for call_order in itertools.permutations(range(4)): + inputs = [canonical[i] for i in call_order] + at = {position: call_order.index(position) for position in range(4)} + kernel = StreamingRejoin(Rejoin(), inputs) + out: list[pa.RecordBatch] = [] + out += kernel.push(at[0], sent[0].slice(0, 2).to_batches()[0]) + out += kernel.push(at[1], sent[1].to_batches()[0]) + out += kernel.push(at[2], sent[2].to_batches()[0]) + assert kernel.held_rows == 2 + out += kernel.close(at[1]) + assert kernel.held_rows == 2 + out += kernel.push(at[0], sent[0].slice(2, 1).to_batches()[0]) + assert kernel.held_rows == 2 + out += kernel.push(at[3], sent[3].to_batches()[0]) + assert kernel.held_rows == 0 + for position in (0, 2, 3): + out += kernel.close(at[position]) + assert_same_rows(out, kernel, expected) + + +def test_extension_and_nested_payloads_cross_held_chunks_and_compaction() -> None: + # A logical (extension) payload and a nested payload, held in one-row chunks and + # matched in another order, across the compactions: every value is taken from + # its chunk, bit for bit, and the rows are the sync rows. + n = 40 + path_type = contexts.get_default_context().type_converter.python_type_to_arrow_type( + Path + ) + assert isinstance(path_type, pa.BaseExtensionType) + nested_type = pa.large_list( + pa.struct([("a", pa.int64()), ("b", pa.large_list(pa.int64()))]) + ) + origin = ArrowTableSource( + pa.table( + { + "unit": pa.array(range(n), pa.int64()), + "path": pa.ExtensionArray.from_storage( + path_type, + pa.array( + [f"/data/f{i}.bin" for i in range(n)], path_type.storage_type + ), + ), + "nested": pa.array( + [[{"a": i, "b": [i, i + 1]}] for i in range(n)], nested_type + ), + "other": pa.array([float(i) for i in range(n)], pa.float64()), + } + ), + tag_columns=["unit"], + tag_only_columns=True, + source_id="payloads", + ) + held = origin.select_data_columns(["path", "nested"]) + arriving = origin.select_data_columns(["other"]) + expected = _transport( + Rejoin().static_process(_row_built(held), _row_built(arriving)) + ) + assert expected.num_rows == n + for inputs, first in (((held, arriving), 0), ((arriving, held), 1)): + kernel = StreamingRejoin(Rejoin(), inputs) + out: list[pa.RecordBatch] = [] + for batch in _sent(held).combine_chunks().to_batches(max_chunksize=1): + out += kernel.push(first, batch) + rows = _sent(arriving).combine_chunks() + for start in reversed(range(0, n, 3)): + out += kernel.push(1 - first, rows.slice(start, 3).to_batches()[0]) + out += kernel.close(0) + kernel.close(1) + assert kernel.held_rows == 0 + assert_same_rows(out, kernel, expected) + got = pa.Table.from_batches(out) + assert got.schema.field("path").type == path_type