|
| 1 | +"""Multi-worker partitioned sequence fixtures, one per ``OrderPreservation`` mode. |
| 2 | +
|
| 3 | +These three fixtures are clones of :class:`PartitionedSequenceFunction` (see |
| 4 | +``sequence.py``); the only difference is ``Meta.preserves_order``. They exist |
| 5 | +so SQL integration tests can verify that DuckDB's planner honors each mode |
| 6 | +end-to-end: |
| 7 | +
|
| 8 | +* ``partitioned_preserves_order`` — ``PRESERVES_ORDER`` → DuckDB ``INSERTION_ORDER`` |
| 9 | +* ``partitioned_no_order_guarantee`` — ``NO_ORDER_GUARANTEE`` → DuckDB ``NO_ORDER`` |
| 10 | +* ``partitioned_fixed_order`` — ``FIXED_ORDER`` → DuckDB ``FIXED_ORDER`` |
| 11 | +
|
| 12 | +DuckDB serializes the pipeline (single worker) for ``FIXED_ORDER`` and |
| 13 | +parallelizes for the other two. Tests grep ``conn=`` from ``duckdb_logs()`` |
| 14 | +to count distinct workers — the same pattern used by |
| 15 | +``partitioned_sequence.test``. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import struct |
| 21 | +from dataclasses import dataclass |
| 22 | +from typing import Annotated, ClassVar |
| 23 | + |
| 24 | +import pyarrow as pa |
| 25 | +from vgi_rpc import ArrowSerializableDataclass |
| 26 | +from vgi_rpc.rpc import OutputCollector |
| 27 | + |
| 28 | +from vgi._test_fixtures.table._common import _cardinality_from_count |
| 29 | +from vgi.arguments import Arg |
| 30 | +from vgi.invocation import GlobalInitResponse |
| 31 | +from vgi.metadata import FunctionExample, OrderPreservation |
| 32 | +from vgi.schema_utils import schema |
| 33 | +from vgi.table_function import ( |
| 34 | + InitParams, |
| 35 | + ProcessParams, |
| 36 | + TableFunctionGenerator, |
| 37 | + bind_fixed_schema, |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +@dataclass(slots=True, frozen=True) |
| 42 | +class _OrderModeArgs: |
| 43 | + """Arguments for the order-preservation-mode fixtures.""" |
| 44 | + |
| 45 | + count: Annotated[int, Arg(0, doc="Total number of integers to generate", ge=0)] |
| 46 | + |
| 47 | + |
| 48 | +@dataclass(kw_only=True) |
| 49 | +class _OrderModeState(ArrowSerializableDataclass): |
| 50 | + """Mutable per-worker state for the order-preservation-mode fixtures.""" |
| 51 | + |
| 52 | + current_start: int | None = None |
| 53 | + current_end: int | None = None |
| 54 | + current_idx: int = 0 |
| 55 | + |
| 56 | + |
| 57 | +class _BasePartitionedOrderMode( |
| 58 | + TableFunctionGenerator[_OrderModeArgs, _OrderModeState] |
| 59 | +): |
| 60 | + """Shared multi-worker work-queue logic. Subclasses pin ``Meta``. |
| 61 | +
|
| 62 | + The chunk/batch sizing matches ``PartitionedSequenceFunction``: 1k chunks, |
| 63 | + 1k-row output batches. The primary worker enqueues all chunks during |
| 64 | + ``on_init``; every worker (including the primary) pulls chunks atomically |
| 65 | + via ``params.storage.queue_pop``. |
| 66 | + """ |
| 67 | + |
| 68 | + CHUNK_SIZE: ClassVar[int] = 1000 |
| 69 | + BATCH_SIZE: ClassVar[int] = 1000 |
| 70 | + |
| 71 | + FIXED_SCHEMA: ClassVar[pa.Schema] = schema(n=pa.int64()) |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def on_init(cls, params: InitParams[_OrderModeArgs]) -> GlobalInitResponse: |
| 75 | + work_items: list[bytes] = [] |
| 76 | + for start_idx in range(0, params.args.count, cls.CHUNK_SIZE): |
| 77 | + end_idx = min(start_idx + cls.CHUNK_SIZE, params.args.count) |
| 78 | + work_items.append(struct.pack(">QQ", start_idx, end_idx)) |
| 79 | + params.storage.queue_push(work_items) |
| 80 | + return GlobalInitResponse() |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def initial_state(cls, params: ProcessParams[_OrderModeArgs]) -> _OrderModeState: |
| 84 | + return _OrderModeState() |
| 85 | + |
| 86 | + @classmethod |
| 87 | + def process( |
| 88 | + cls, |
| 89 | + params: ProcessParams[_OrderModeArgs], |
| 90 | + state: _OrderModeState, |
| 91 | + out: OutputCollector, |
| 92 | + ) -> None: |
| 93 | + if state.current_start is None or state.current_idx >= (state.current_end or 0): |
| 94 | + work_data = params.storage.queue_pop() |
| 95 | + if work_data is None: |
| 96 | + out.finish() |
| 97 | + return |
| 98 | + state.current_start, state.current_end = struct.unpack(">QQ", work_data) |
| 99 | + assert state.current_start is not None |
| 100 | + state.current_idx = state.current_start |
| 101 | + |
| 102 | + batch_end_idx = min(state.current_idx + cls.BATCH_SIZE, state.current_end or 0) |
| 103 | + values = list(range(state.current_idx, batch_end_idx)) |
| 104 | + out.emit(pa.RecordBatch.from_pydict({"n": values}, schema=params.output_schema)) |
| 105 | + state.current_idx = batch_end_idx |
| 106 | + |
| 107 | + |
| 108 | +@bind_fixed_schema |
| 109 | +@_cardinality_from_count |
| 110 | +class PartitionedPreservesOrderFunction(_BasePartitionedOrderMode): |
| 111 | + """Multi-worker partitioned sequence — ``PRESERVES_ORDER``.""" |
| 112 | + |
| 113 | + class Meta: |
| 114 | + name = "partitioned_preserves_order" |
| 115 | + description = ( |
| 116 | + "Multi-worker partitioned sequence; preserves_order=PRESERVES_ORDER " |
| 117 | + "(maps to DuckDB INSERTION_ORDER)." |
| 118 | + ) |
| 119 | + categories = ["generator", "utility"] |
| 120 | + preserves_order = OrderPreservation.PRESERVES_ORDER |
| 121 | + examples = [ |
| 122 | + FunctionExample( |
| 123 | + sql="SELECT * FROM partitioned_preserves_order(100)", |
| 124 | + description="Generate 0-99 in parallel; preserves_order=PRESERVES_ORDER", |
| 125 | + ), |
| 126 | + ] |
| 127 | + |
| 128 | + |
| 129 | +@bind_fixed_schema |
| 130 | +@_cardinality_from_count |
| 131 | +class PartitionedNoOrderGuaranteeFunction(_BasePartitionedOrderMode): |
| 132 | + """Multi-worker partitioned sequence — ``NO_ORDER_GUARANTEE``.""" |
| 133 | + |
| 134 | + class Meta: |
| 135 | + name = "partitioned_no_order_guarantee" |
| 136 | + description = ( |
| 137 | + "Multi-worker partitioned sequence; preserves_order=NO_ORDER_GUARANTEE " |
| 138 | + "(maps to DuckDB NO_ORDER)." |
| 139 | + ) |
| 140 | + categories = ["generator", "utility"] |
| 141 | + preserves_order = OrderPreservation.NO_ORDER_GUARANTEE |
| 142 | + examples = [ |
| 143 | + FunctionExample( |
| 144 | + sql="SELECT * FROM partitioned_no_order_guarantee(100)", |
| 145 | + description="Generate 0-99 in parallel; preserves_order=NO_ORDER_GUARANTEE", |
| 146 | + ), |
| 147 | + ] |
| 148 | + |
| 149 | + |
| 150 | +@bind_fixed_schema |
| 151 | +@_cardinality_from_count |
| 152 | +class PartitionedFixedOrderFunction(_BasePartitionedOrderMode): |
| 153 | + """Multi-worker partitioned sequence — ``FIXED_ORDER`` (DuckDB serializes).""" |
| 154 | + |
| 155 | + class Meta: |
| 156 | + name = "partitioned_fixed_order" |
| 157 | + description = ( |
| 158 | + "Multi-worker partitioned sequence; preserves_order=FIXED_ORDER " |
| 159 | + "(DuckDB serializes the pipeline so a single worker produces all rows)." |
| 160 | + ) |
| 161 | + categories = ["generator", "utility"] |
| 162 | + preserves_order = OrderPreservation.FIXED_ORDER |
| 163 | + examples = [ |
| 164 | + FunctionExample( |
| 165 | + sql="SELECT * FROM partitioned_fixed_order(100)", |
| 166 | + description="Generate 0-99; FIXED_ORDER forces single-worker execution", |
| 167 | + ), |
| 168 | + ] |
0 commit comments