Skip to content

Commit 7da9e2d

Browse files
rustyconoverclaude
andcommitted
buffered_table: migrate handler + 4 fixtures to unified state_* (Turn 2)
The worker's buffered_table_process handler and the buffered_table fixtures' combine()/finalize() methods read/write the same storage rows, so they must move to the unified API together (the original Turn 2 plan called for "one fixture" but that wasn't a coherent slice). Migrated: - worker.py buffered_table_process — reads/writes via state_get/put under namespace b"buf", key = pack_int_key(state_id). When func_cls.state_class is None (forthcoming use in Step 3 for BufferInputFunction-via-log_append), the put step is skipped. - BufferInputFunction.finalize — state_get instead of aggregate_get - SumAllColumnsFunction.combine + finalize — state_get_many / state_put - LargeStateFunction.finalize - BatchIndexBufferInputFunction.combine + finalize Subclasses (OrderedBufferInputFunction, BatchIndexBufferInputFunction parents, all CrashOn* / HangOn* / Exception* fixtures) inherit the migrated parents' methods, so no per-subclass changes needed. Aggregate fixtures (vgi_sum, vgi_count, etc. via aggregate_update / aggregate_combine RPC handlers) are NOT touched here — they go through a different worker code path that still uses aggregate_state_* and will migrate in Turn 3. Verification: 27 buffered_table integration tests pass; 152 total subprocess tests + 88 HTTP test cases (5364 assertions) green. The state_* path is end-to-end functional through the C++ extension. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 1f0337f commit 7da9e2d

2 files changed

Lines changed: 45 additions & 24 deletions

File tree

‎vgi/_test_fixtures/table_in_out.py‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,12 @@ def finalize(
227227
``buffered_table_finalize`` RPC consumes one ``yield`` and reports
228228
``has_more`` based on whether the iterator is exhausted.
229229
"""
230-
stored = params.storage.aggregate_get([finalize_state_id])
231-
if not stored or stored[0] is None:
230+
from vgi.function_storage import BoundStorage as _BS
231+
232+
stored = params.storage.state_get(b"buf", _BS.pack_int_key(finalize_state_id))
233+
if stored is None:
232234
return
233-
state = BufferInputState.deserialize_from_bytes(stored[0][1])
235+
state = BufferInputState.deserialize_from_bytes(stored)
234236
for batch_bytes in state.batches:
235237
yield pa.ipc.open_stream(batch_bytes).read_next_batch()
236238

@@ -558,27 +560,32 @@ def combine(
558560
"""
559561
if not state_ids:
560562
return []
561-
stored = params.storage.aggregate_get(state_ids)
563+
from vgi.function_storage import BoundStorage as _BS
564+
565+
keys = [_BS.pack_int_key(sid) for sid in state_ids]
566+
stored = params.storage.state_get_many(b"buf", keys)
562567
merged: dict[str, pa.Scalar[Any]] = {name: pa.scalar(0, type=field.type)
563568
for name, field in zip(params.output_schema.names,
564569
params.output_schema)}
565570
for entry in stored:
566571
if entry is None:
567572
continue
568-
partial = SumAllColumnsState.deserialize_from_bytes(entry[1]).partial_sums
573+
partial = SumAllColumnsState.deserialize_from_bytes(entry).partial_sums
569574
for name in params.output_schema.names:
570575
merged[name] = pc.add(merged[name], partial.column(name)[0])
571576
merged_state = SumAllColumnsState(partial_sums=cls._scalars_to_single_row_batch(merged))
572-
params.storage.aggregate_put([(0, merged_state.serialize_to_bytes())])
577+
params.storage.state_put(b"buf", _BS.pack_int_key(0), merged_state.serialize_to_bytes())
573578
return [0]
574579

575580
@classmethod
576581
def finalize(
577582
cls, finalize_state_id: int, params: ProcessParams[SumAllColumnsFunctionArguments]
578583
) -> Iterator[pa.RecordBatch]:
579584
"""Yield one row carrying the merged column sums."""
580-
stored = params.storage.aggregate_get([finalize_state_id])
581-
if not stored or stored[0] is None:
585+
from vgi.function_storage import BoundStorage as _BS
586+
587+
stored = params.storage.state_get(b"buf", _BS.pack_int_key(finalize_state_id))
588+
if stored is None:
582589
# No data at all — emit zeros so SQL like
583590
# `SELECT * FROM sum_all_columns((SELECT 1 WHERE 1=0))` still
584591
# produces a row with the expected shape.
@@ -587,7 +594,7 @@ def finalize(
587594
yield pa.RecordBatch.from_pydict({name: [val] for name, val in sums.items()},
588595
schema=params.output_schema)
589596
return
590-
partial = SumAllColumnsState.deserialize_from_bytes(stored[0][1]).partial_sums
597+
partial = SumAllColumnsState.deserialize_from_bytes(stored).partial_sums
591598
sums = {name: partial.column(name)[0] for name in params.output_schema.names}
592599
yield pa.RecordBatch.from_pydict({name: [val] for name, val in sums.items()},
593600
schema=params.output_schema)
@@ -953,10 +960,12 @@ def combine(
953960
def finalize(
954961
cls, finalize_state_id: int, params: ProcessParams[SingleTableArguments]
955962
) -> Iterator[pa.RecordBatch]:
956-
stored = params.storage.aggregate_get([finalize_state_id])
957-
if not stored or stored[0] is None:
963+
from vgi.function_storage import BoundStorage as _BS
964+
965+
stored = params.storage.state_get(b"buf", _BS.pack_int_key(finalize_state_id))
966+
if stored is None:
958967
return
959-
state = LargeStateState.deserialize_from_bytes(stored[0][1])
968+
state = LargeStateState.deserialize_from_bytes(stored)
960969
# Emit one row with the payload length so the test can assert
961970
# we round-tripped the right number of bytes through combine.
962971
yield pa.RecordBatch.from_pydict(
@@ -1073,25 +1082,30 @@ def combine(
10731082
"""Merge per-state buffers, sort by batch_index, write to slot 0."""
10741083
if not state_ids:
10751084
return []
1076-
stored = params.storage.aggregate_get(state_ids)
1085+
from vgi.function_storage import BoundStorage as _BS
1086+
1087+
keys = [_BS.pack_int_key(sid) for sid in state_ids]
1088+
stored = params.storage.state_get_many(b"buf", keys)
10771089
all_pairs: list[IndexedBatch] = []
10781090
for entry in stored:
10791091
if entry is None:
10801092
continue
1081-
partial = BatchIndexBufferInputState.deserialize_from_bytes(entry[1])
1093+
partial = BatchIndexBufferInputState.deserialize_from_bytes(entry)
10821094
all_pairs.extend(partial.pairs)
10831095
all_pairs.sort(key=lambda p: p.batch_index)
10841096
merged = BatchIndexBufferInputState(pairs=all_pairs)
1085-
params.storage.aggregate_put([(0, merged.serialize_to_bytes())])
1097+
params.storage.state_put(b"buf", _BS.pack_int_key(0), merged.serialize_to_bytes())
10861098
return [0]
10871099

10881100
@classmethod
10891101
def finalize(
10901102
cls, finalize_state_id: int, params: ProcessParams[SingleTableArguments]
10911103
) -> Iterator[pa.RecordBatch]:
1092-
stored = params.storage.aggregate_get([finalize_state_id])
1093-
if not stored or stored[0] is None:
1104+
from vgi.function_storage import BoundStorage as _BS
1105+
1106+
stored = params.storage.state_get(b"buf", _BS.pack_int_key(finalize_state_id))
1107+
if stored is None:
10941108
return
1095-
state = BatchIndexBufferInputState.deserialize_from_bytes(stored[0][1])
1109+
state = BatchIndexBufferInputState.deserialize_from_bytes(stored)
10961110
for entry in state.pairs:
10971111
yield pa.ipc.open_stream(entry.batch_bytes).read_next_batch()

‎vgi/worker.py‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,10 +2417,16 @@ def buffered_table_process(
24172417

24182418
batch = pa.ipc.open_stream(request.input_batch).read_next_batch()
24192419

2420-
# Load existing state for this state_id; create initial if absent.
2421-
stored = params.storage.aggregate_get([request.state_id])
2422-
if stored[0] is not None and func_cls.state_class is not None:
2423-
state = func_cls.state_class.deserialize_from_bytes(stored[0][1])
2420+
# Load existing state for this state_id from the unified state_*
2421+
# store. Buffered_table fixtures live under namespace b"buf"; key is
2422+
# the int state_id packed as 8-byte little-endian (BoundStorage
2423+
# convenience). Create initial state if absent.
2424+
from vgi.function_storage import BoundStorage as _BS
2425+
2426+
_state_key = _BS.pack_int_key(request.state_id)
2427+
stored_value = params.storage.state_get(b"buf", _state_key)
2428+
if stored_value is not None and func_cls.state_class is not None:
2429+
state = func_cls.state_class.deserialize_from_bytes(stored_value)
24242430
else:
24252431
state = func_cls.initial_state(params)
24262432

@@ -2453,8 +2459,9 @@ def client_log(self, level: "_Level", message: str, **extra: str) -> None:
24532459

24542460
func_cls.process(params, state, batch, _NoOpCollector())
24552461

2456-
# Persist the updated state.
2457-
params.storage.aggregate_put([(request.state_id, state.serialize_to_bytes())])
2462+
# Persist the updated state — namespace b"buf", key = packed state_id.
2463+
if func_cls.state_class is not None:
2464+
params.storage.state_put(b"buf", _state_key, state.serialize_to_bytes())
24582465
return BufferedTableProcessResponse()
24592466

24602467
def buffered_table_combine(

0 commit comments

Comments
 (0)