Skip to content

Commit 931d515

Browse files
rustyconoverclaude
andcommitted
metadata: add OrderPreservation.FIXED_ORDER + 3 multi-worker fixtures
FIXED_ORDER maps to DuckDB OrderPreservationType::FIXED_ORDER, completing the trio so worker functions can express all three of DuckDB's row-order modes via Meta.preserves_order. The accompanying VGI extension change finally wires the parsed value onto TableFunction::order_preservation_type (it was dead in the C++ side until now). The new partitioned_{preserves_order,no_order_guarantee,fixed_order} fixtures share a multi-worker work-queue base and exist so the extension side can integration-test the planner behavior end-to-end. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent da7915b commit 931d515

4 files changed

Lines changed: 197 additions & 3 deletions

File tree

‎vgi/_test_fixtures/table/__init__.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
ProjectedDataFunction,
4949
SampleEchoFunction,
5050
)
51+
from vgi._test_fixtures.table.order_modes import (
52+
PartitionedFixedOrderFunction,
53+
PartitionedNoOrderGuaranteeFunction,
54+
PartitionedPreservesOrderFunction,
55+
)
5156
from vgi._test_fixtures.table.pairs import (
5257
ConstantColumnsFunction,
5358
MakePairsIntFunction,
@@ -116,6 +121,9 @@
116121
"NamedParamsEchoFunction",
117122
"NestedSequenceFunction",
118123
"OrderEchoFunction",
124+
"PartitionedFixedOrderFunction",
125+
"PartitionedNoOrderGuaranteeFunction",
126+
"PartitionedPreservesOrderFunction",
119127
"PartitionedSequenceFunction",
120128
"ProductsScanFunction",
121129
"ProfilingDemoFunction",
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
]

‎vgi/_test_fixtures/worker.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@
118118
NamedParamsEchoFunction,
119119
NestedSequenceFunction,
120120
OrderEchoFunction,
121+
PartitionedFixedOrderFunction,
122+
PartitionedNoOrderGuaranteeFunction,
123+
PartitionedPreservesOrderFunction,
121124
PartitionedSequenceFunction,
122125
ProductsScanFunction,
123126
ProfilingDemoFunction,
@@ -291,6 +294,9 @@ def _build_enum_stats() -> dict[str, ColumnStatisticsInput]:
291294
NestedSequenceFunction,
292295
ProfilingDemoFunction,
293296
OrderEchoFunction,
297+
PartitionedFixedOrderFunction,
298+
PartitionedNoOrderGuaranteeFunction,
299+
PartitionedPreservesOrderFunction,
294300
PartitionedSequenceFunction,
295301
ProjectedDataFunction,
296302
SampleEchoFunction,

‎vgi/metadata.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,26 @@ class NullHandling(Enum):
135135
class OrderPreservation(Enum):
136136
"""Row order preservation behavior.
137137
138-
Maps to DuckDB's OrderPreservationType enum.
138+
Maps to DuckDB's ``OrderPreservationType`` enum:
139+
140+
* ``PRESERVES_ORDER`` → ``OrderPreservationType::INSERTION_ORDER``
141+
(DuckDB default — operator maintains child operator order).
142+
* ``NO_ORDER_GUARANTEE`` → ``OrderPreservationType::NO_ORDER``
143+
(operator may freely reorder its input/output).
144+
* ``FIXED_ORDER`` → ``OrderPreservationType::FIXED_ORDER``
145+
(operator outputs rows in a fixed, mandatory order — DuckDB
146+
serializes the pipeline so a single worker produces all rows).
139147
"""
140148

141149
PRESERVES_ORDER = auto()
142-
"""Output rows are in same order as input rows."""
150+
"""Output rows are in same order as input rows (DuckDB INSERTION_ORDER)."""
143151

144152
NO_ORDER_GUARANTEE = auto()
145-
"""Output order is undefined (may be reordered)."""
153+
"""Output order is undefined; may be reordered (DuckDB NO_ORDER)."""
154+
155+
FIXED_ORDER = auto()
156+
"""Output is in a fixed mandatory order; DuckDB serializes the pipeline
157+
(single worker) to preserve it (DuckDB FIXED_ORDER)."""
146158

147159

148160
class OrderDependence(Enum):

0 commit comments

Comments
 (0)