3737import os
3838import signal
3939import time
40- from dataclasses import dataclass
4140from collections .abc import Iterator
41+ from dataclasses import dataclass
4242from typing import Annotated , Any
4343
4444import pyarrow as pa
4949from vgi_rpc .utils import empty_batch
5050
5151from vgi .arguments import Arg , Setting , TableInput
52- from vgi .invocation import BindResponse , GlobalInitResponse
52+ from vgi .function_storage import BoundStorage
53+ from vgi .invocation import BindResponse
5354from vgi .metadata import FunctionExample
5455from vgi .schema_utils import schema
55- from vgi .table_function import BindParams , InitParams , ProcessParams , TableCardinality
56+ from vgi .table_function import BindParams , ProcessParams , TableCardinality
5657from vgi .table_in_out_function import (
5758 TableInOutFunction ,
5859 TableInOutGenerator ,
@@ -188,16 +189,14 @@ def process(
188189 silently accepts the trailing zero-row emit but would raise on any
189190 non-empty emit — process() is sink-only.
190191 """
191- from vgi .function_storage import BoundStorage as _BS
192-
193192 assert params .state_id is not None , (
194193 "buffered_table_process did not populate ProcessParams.state_id"
195194 )
196195 sink = pa .BufferOutputStream ()
197196 with pa .ipc .new_stream (sink , batch .schema ) as writer :
198197 writer .write_batch (batch )
199198 params .storage .state_append (
200- b"buf" , _BS .pack_int_key (params .state_id ), sink .getvalue ().to_pybytes ()
199+ b"buf" , BoundStorage .pack_int_key (params .state_id ), sink .getvalue ().to_pybytes ()
201200 )
202201 out .emit (empty_batch (params .output_schema ))
203202
@@ -219,10 +218,8 @@ def finalize(
219218 cls , finalize_state_id : int , params : ProcessParams [SingleTableArguments ]
220219 ) -> Iterator [pa .RecordBatch ]:
221220 """Yield the buffered batches for one finalize_state_id, one per RPC."""
222- from vgi .function_storage import BoundStorage as _BS
223-
224221 for batch_bytes in params .storage .state_log_scan (
225- b"buf" , _BS .pack_int_key (finalize_state_id )
222+ b"buf" , BoundStorage .pack_int_key (finalize_state_id )
226223 ):
227224 yield pa .ipc .open_stream (batch_bytes ).read_next_batch ()
228225
@@ -550,9 +547,8 @@ def combine(
550547 """
551548 if not state_ids :
552549 return []
553- from vgi .function_storage import BoundStorage as _BS
554550
555- keys = [_BS .pack_int_key (sid ) for sid in state_ids ]
551+ keys = [BoundStorage .pack_int_key (sid ) for sid in state_ids ]
556552 stored = params .storage .state_get_many (b"buf" , keys )
557553 merged : dict [str , pa .Scalar [Any ]] = {name : pa .scalar (0 , type = field .type )
558554 for name , field in zip (params .output_schema .names ,
@@ -564,17 +560,15 @@ def combine(
564560 for name in params .output_schema .names :
565561 merged [name ] = pc .add (merged [name ], partial .column (name )[0 ])
566562 merged_state = SumAllColumnsState (partial_sums = cls ._scalars_to_single_row_batch (merged ))
567- params .storage .state_put (b"buf" , _BS .pack_int_key (0 ), merged_state .serialize_to_bytes ())
563+ params .storage .state_put (b"buf" , BoundStorage .pack_int_key (0 ), merged_state .serialize_to_bytes ())
568564 return [0 ]
569565
570566 @classmethod
571567 def finalize (
572568 cls , finalize_state_id : int , params : ProcessParams [SumAllColumnsFunctionArguments ]
573569 ) -> Iterator [pa .RecordBatch ]:
574570 """Yield one row carrying the merged column sums."""
575- from vgi .function_storage import BoundStorage as _BS
576-
577- stored = params .storage .state_get (b"buf" , _BS .pack_int_key (finalize_state_id ))
571+ stored = params .storage .state_get (b"buf" , BoundStorage .pack_int_key (finalize_state_id ))
578572 if stored is None :
579573 # No data at all — emit zeros so SQL like
580574 # `SELECT * FROM sum_all_columns((SELECT 1 WHERE 1=0))` still
@@ -899,7 +893,8 @@ def process( # type: ignore[override]
899893class LargeStateState (ArrowSerializableDataclass ):
900894 """Holds a single large bytes buffer; the C++ side ships this through
901895 combine/finalize via the RPC's outer envelope, exercising IPC chunking
902- on the response path."""
896+ on the response path.
897+ """
903898
904899 payload : bytes = b""
905900
@@ -950,9 +945,8 @@ def combine(
950945 def finalize (
951946 cls , finalize_state_id : int , params : ProcessParams [SingleTableArguments ]
952947 ) -> Iterator [pa .RecordBatch ]:
953- from vgi .function_storage import BoundStorage as _BS
954948
955- stored = params .storage .state_get (b"buf" , _BS .pack_int_key (finalize_state_id ))
949+ stored = params .storage .state_get (b"buf" , BoundStorage .pack_int_key (finalize_state_id ))
956950 if stored is None :
957951 return
958952 state = LargeStateState .deserialize_from_bytes (stored )
@@ -1049,14 +1043,13 @@ def process(
10491043 "— Meta.requires_input_batch_index plumbing is broken"
10501044 )
10511045 assert params .state_id is not None
1052- from vgi .function_storage import BoundStorage as _BS
10531046
10541047 sink = pa .BufferOutputStream ()
10551048 with pa .ipc .new_stream (sink , batch .schema ) as writer :
10561049 writer .write_batch (batch )
10571050 params .storage .state_append (
10581051 b"buf" ,
1059- _BS .pack_int_key (params .state_id ),
1052+ BoundStorage .pack_int_key (params .state_id ),
10601053 _pack_indexed_batch (params .batch_index , sink .getvalue ().to_pybytes ()),
10611054 )
10621055 out .emit (empty_batch (params .output_schema ))
@@ -1078,15 +1071,14 @@ def combine(
10781071 def finalize (
10791072 cls , finalize_state_id : int , params : ProcessParams [SingleTableArguments ]
10801073 ) -> Iterator [pa .RecordBatch ]:
1081- from vgi .function_storage import BoundStorage as _BS
10821074
10831075 # NB. There is exactly one finalize() per state_id (combine returned
10841076 # state_ids unchanged), so we only see this thread's log here. To get
10851077 # a globally ordered output the caller should choose Meta.sink_order_dependent
10861078 # OR use a single Sink thread; otherwise per-thread output order is
10871079 # batch_index-ascending only within this slice.
10881080 log_bytes = params .storage .state_log_scan (
1089- b"buf" , _BS .pack_int_key (finalize_state_id )
1081+ b"buf" , BoundStorage .pack_int_key (finalize_state_id )
10901082 )
10911083 pairs = [_unpack_indexed_batch (b ) for b in log_bytes ]
10921084 pairs .sort (key = lambda p : p [0 ])
0 commit comments