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: 9 additions & 0 deletions changelog.d/ITL-1360.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
### Breaking changes and migration

- **`SyncPipelineOrchestrator.run` takes `held` instead of `held_rows`** — a job's sync run
passes the held nodes' output streams (`held: Mapping[node, StreamProtocol] | None`); a
subclass that overrides `run` with the old `held_rows` parameter fails with `TypeError`
before the override runs: rename the parameter to `held` and read each held node's rows
from its stream (`iter_data()`). The async orchestrator still takes `held_rows`.
(ITL-1347, ITL-1360)

### Changed

- **A job node's `execute` returns its output stream** — `execute` of a source, operator,
Expand Down
21 changes: 13 additions & 8 deletions docs/dev/scripts/itl_1347/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,22 @@ def _without(table: pa.Table, volatile: set[str]) -> pa.Table:
return table.drop_columns([c for c in table.column_names if c in volatile])


def _rows_as_multiset(table: pa.Table) -> list[str]:
"""The rows as a sorted list of their serialized values, duplicates kept.

Floats are taken by their bits (``_bitwise``), so NaN payloads stay distinct.
def _rows_as_multiset(table: pa.Table) -> list[bytes]:
"""The rows as a sorted list of their raw Arrow values, duplicates kept.

Each row is taken alone, its columns in name order and through ``_bits`` (so
NaN payloads and ``-0.0`` stay distinct), and written in the Arrow IPC format:
two rows are equal exactly when their Arrow values are. Never Python objects,
which lose what Arrow holds (``time64[ns]`` 1 and 2 become one microsecond
``time``; CodeRabbit on PR #510).
"""
table = _bitwise(table)
names = sorted(table.column_names)
return sorted(
json.dumps([repr(row[n]) for n in names])
for row in table.select(names).to_pylist()
)
keys: list[bytes] = []
for batch in table.select(names).to_batches():
for index in range(batch.num_rows):
keys.append(batch.take(pa.array([index])).serialize().to_pybytes())
return sorted(keys)


def compare(
Expand Down
23 changes: 14 additions & 9 deletions src/orcapod/core/nodes/source_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,14 @@ def close(self) -> None:

@property
def opened_on(self) -> StreamProtocol | None:
"""The node's output stream, read on this first access (a conversion's first
read): a channel not read yet gives it as it is when it is a complete output
in the row-built form."""
return self._output_feed().opened_on
"""The node's output stream once the channel's first read made it, else
``None``.

Inspecting it never runs the node or takes its binding: the first read does
(``open_channel``'s guarantee). An unread source-node channel so answers
``None``, and a conversion (``to_stream``) reads it like any other.
"""
return self._feed.opened_on if self._feed is not None else None


def _closed_feed(node: SourceNodeBase) -> CompleteStreamFeed:
Expand Down Expand Up @@ -1881,18 +1885,19 @@ def _rows_table(
Raises:
UnboundSourceError: When no concrete source is attached.
"""
if not reads_as_its_class(self, SourceJobNode):
# A subclass, or a node with a member replaced on the instance, may read
# its rows its own way (an ``iter_data`` that filters, or rows of its own
# without a bound source): its rows, as main iterated them (ITL-1347, the
# one rule).
return None
if self._bound_source is None:
raise UnboundSourceError(
f"SourceJobNode '{self._name}' has no concrete source bound. "
"Call job.bind(sources={'<name>': source}) before running."
)
if self._declared_form is not None:
return None
if not reads_as_its_class(self, SourceJobNode):
# A subclass, or a node with a member replaced on the instance, may read
# its rows its own way (an ``iter_data`` that filters): its rows, as main
# iterated them (ITL-1347, the one rule).
return None
if (
not table_is_rows(self._bound_source)
or self._bound_source.data_context_key != self.data_context_key
Expand Down
3 changes: 0 additions & 3 deletions src/orcapod/core/operators/static_output_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,6 @@ def _flag_by_values(table: pa.Table) -> pa.Table:
Returns:
The same arrays under the new field flags, with no schema metadata.
"""
from orcapod.system_constants import constants

prefixes = (constants.SYSTEM_TAG_PREFIX, constants.SOURCE_PREFIX)
fields = [
field.with_nullable(table.column(i).null_count > 0)
Expand Down Expand Up @@ -557,7 +555,6 @@ def _type_source_info_by_values(table: pa.Table) -> pa.Table:
the rows (``_rows_to_table``), which raises the error a table built from
rows raises.
"""
from orcapod.system_constants import constants
from orcapod.utils import arrow_utils

columns = list(table.columns)
Expand Down
51 changes: 40 additions & 11 deletions src/orcapod/core/streams/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,27 +534,56 @@ def __init__(self, stream: StreamProtocol) -> None:
"""
self._stream = stream
self._batches: deque[pa.RecordBatch] | None = None
self._failure: BaseException | None = None

async def next(self) -> pa.RecordBatch | None:
"""Return the stream's next record batch, or ``None`` at the end.

The stream's table is read on the first call, not when the channel is opened;
batches with no rows are skipped.
"""
if self._failure is not None:
# The stream was read once and preparing its batches failed: the same
# error again, never a second read of the stream.
raise self._failure
if self._batches is None:
# The table first: reading it is the stream's one read (an unpolled
# source polls here), and its keys are then known without another.
table = self._stream.as_table(columns=ZERO_ROW_INPUT_COLUMNS)
# The rows' keys: a source whose keys() states its declared order has
# rows in its table's order (``keys_of_rows``).
table = row_built_columns(table, keys_of_rows(table, self._stream.keys()))
self._batches = deque(
batch
for batch in table.to_batches(max_chunksize=SOURCE_BATCH_ROWS)
if batch.num_rows
)
try:
self._batches = deque(
batch
for batch in self._row_built_table().to_batches(
max_chunksize=SOURCE_BATCH_ROWS
)
if batch.num_rows
)
except BaseException as failure:
self._failure = failure
raise
return self._batches.popleft() if self._batches else None

def _row_built_table(self) -> pa.Table:
"""The stream's table in the row-built form: its one read.

The table first: reading it is the stream's one read (an unpolled source polls
here), and its keys are then known without another. Its system tags must be
what rows give (``system_tags_as_rows_give``: non-null text, no field
metadata); otherwise, as on the eager routes of a job, the table is the one
its rows build (``main``'s row path), so a narrow or nullable system tag is
retyped as a row retypes it. Only a stream that holds such a column (a raw
``ArrowTableStream``) is read that way, and its reads have no effect.
"""
table = self._stream.as_table(columns=ZERO_ROW_INPUT_COLUMNS)
if not system_tags_as_rows_give(table):
from orcapod.core.operators.static_output_pod import (
StaticOutputOperatorPod,
)

rows = list(self._stream.iter_data())
built = StaticOutputOperatorPod._materialize_upstream(rows, self._stream) # pyright: ignore[reportPrivateUsage] # main's row path
table = built.as_table(columns=ZERO_ROW_INPUT_COLUMNS)
# The rows' keys: a source whose keys() states its declared order has rows in
# its table's order (``keys_of_rows``).
return row_built_columns(table, keys_of_rows(table, self._stream.keys()))

def close(self) -> None:
"""Stop reading: the batches not read are dropped."""
self._batches = deque()
Expand Down
8 changes: 5 additions & 3 deletions src/orcapod/protocols/core_protocols/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,11 @@ def arrow_schema(self) -> "pa.Schema":
def arrow_types_declared(self) -> bool:
"""Whether ``arrow_schema()`` holds the rows' own Arrow types.

``False`` when the stream cannot type itself without rows (an unbound slot, a
``PollingSource`` never polled; ITL-1089), or its rows decide its types (a
narrower bind, an output held as its rows). Its schema is then the best
``False`` when the stream cannot type itself without rows (an unbound slot
declared in Python types, a ``PollingSource`` never polled; ITL-1089), or its
rows decide its types (a narrower bind, an output held as its rows). An unbound
slot declared with explicit Arrow schemas answers ``True``: its rows will have
exactly those types, since a bind refuses a source of any other. Its schema is then the best
description available without reading a row: a slot's or source's explicit
Arrow declaration when it states one, otherwise its Python schema converted;
a source whose schema is known only by polling, and which has not polled, has
Expand Down
7 changes: 4 additions & 3 deletions tests/test_core/operators/test_record_batch_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,8 @@ def test_empty_source_info_lists_are_typed_from_their_tokens(layout: str) -> Non
def test_a_system_tag_with_a_null_is_refused_by_the_job_sync_path() -> None:
"""Pins a stream no job can run: a raw stream whose system-tag column holds a string
and a null. The job's sync path refuses it (the rows' system-tag fields differ in
their flags), while the channel gives both rows; a job never sees such a stream,
their flags), and so does the channel, which builds such a stream from
its rows (CodeRabbit on PR #510); a job never sees such a stream,
because a source with a nullable system tag cannot be bound to a slot
(``SourceSpecMismatchError``)."""
raw = ArrowTableStream(
Expand All @@ -671,8 +672,8 @@ def test_a_system_tag_with_a_null_is_refused_by_the_job_sync_path() -> None:
"refused",
"ArrowInvalid",
)
channel = run_on_channels(SelectDataColumns(["x"]), [raw], None)
assert channel.as_table(columns=ALL).column(SYSTEM_TAG).to_pylist() == ["s", None]
with pytest.raises(pa.ArrowInvalid):
run_on_channels(SelectDataColumns(["x"]), [raw], None)


# --- Ending the output and closing the inputs ------------------------------------------------
Expand Down
Loading
Loading