Skip to content

Commit 97ef496

Browse files
authored
Merge pull request #5 from Query-farm/consolidate-output-complete
refactor: consolidate _OutputComplete classes into shared module
2 parents b88995a + 187ddf5 commit 97ef496

5 files changed

Lines changed: 95 additions & 138 deletions

File tree

‎vgi/examples/table.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ def output_schema(self) -> pa.Schema:
700700
When vgi_verbose_mode is "true", includes an extra "details" column.
701701
This demonstrates how settings can affect the bind result.
702702
"""
703-
fields: list[pa.Field] = [
703+
fields: list[pa.Field[pa.DataType]] = [
704704
pa.field("id", pa.int64()),
705705
pa.field("value", pa.float64()),
706706
]

‎vgi/output_complete.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""Internal output normalization for VGI function generators.
2+
3+
This module provides the _OutputComplete class used by all function types
4+
to normalize generator yields into a consistent format with guaranteed
5+
non-None batches.
6+
7+
This is an internal module - users should not import from here directly.
8+
"""
9+
10+
from dataclasses import dataclass
11+
from typing import TYPE_CHECKING
12+
13+
import pyarrow as pa
14+
15+
import vgi.log
16+
17+
if TYPE_CHECKING:
18+
from vgi.table_function import Output as TableOutput
19+
from vgi.table_in_out_function import Output as TableInOutOutput
20+
21+
__all__ = ["OutputComplete"]
22+
23+
24+
@dataclass(frozen=True, slots=True)
25+
class OutputComplete:
26+
"""Internal: Output with guaranteed non-None batch.
27+
28+
Used by the framework to normalize generator yields. When the user yields
29+
None, Output with None batch, or Message, this class ensures we always
30+
have a valid RecordBatch for the protocol.
31+
32+
Attributes:
33+
batch: Always a valid RecordBatch (never None).
34+
has_more: If True, generator expects another send() call.
35+
Only used by TableInOutGeneratorFunction.
36+
log_message: Present when user yielded Message directly.
37+
38+
"""
39+
40+
batch: pa.RecordBatch
41+
has_more: bool = False
42+
log_message: vgi.log.Message | None = None
43+
44+
@classmethod
45+
def from_process_result(
46+
cls,
47+
source: "vgi.log.Message | TableOutput | TableInOutOutput | None",
48+
empty_batch: pa.RecordBatch,
49+
) -> "OutputComplete":
50+
"""Create from user's yield value.
51+
52+
Args:
53+
source: What the user yielded (Output, Message, or None).
54+
empty_batch: Empty batch to substitute when needed.
55+
56+
Returns:
57+
Normalized output with guaranteed non-None batch.
58+
59+
"""
60+
if source is None:
61+
return cls(batch=empty_batch)
62+
if isinstance(source, vgi.log.Message):
63+
# When yielding a log message, has_more=True so the caller
64+
# re-sends the current input after the message is processed
65+
return cls(batch=empty_batch, has_more=True, log_message=source)
66+
# source is Output (either TableOutput or TableInOutOutput)
67+
# TableOutput doesn't have has_more, TableInOutOutput does
68+
has_more = getattr(source, "has_more", False)
69+
return cls(
70+
batch=source.batch if source.batch is not None else empty_batch,
71+
has_more=has_more,
72+
)

‎vgi/scalar_function.py‎

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def compute(self, batch: pa.RecordBatch) -> pa.Array:
5050
import vgi.function
5151
import vgi.log
5252
from vgi.exceptions import SchemaValidationError
53+
from vgi.output_complete import OutputComplete
5354
from vgi.table_function import Output, ProtocolOutput
5455

5556
__all__ = [
@@ -165,37 +166,6 @@ class ProtocolInput:
165166
metadata: pa.KeyValueMetadata | None = None
166167

167168

168-
@dataclass(frozen=True, slots=True)
169-
class _ScalarOutputComplete:
170-
"""Internal: Output with guaranteed non-None batch for scalar functions."""
171-
172-
batch: pa.RecordBatch
173-
log_message: vgi.log.Message | None = None
174-
175-
@classmethod
176-
def from_process_result(
177-
cls,
178-
source: vgi.log.Message | Output,
179-
empty_batch: pa.RecordBatch,
180-
) -> _ScalarOutputComplete:
181-
"""Create from user's yield value.
182-
183-
Args:
184-
source: What the user yielded (Output or Message).
185-
empty_batch: Empty batch to substitute when yielding Message.
186-
187-
Returns:
188-
Normalized output with guaranteed non-None batch.
189-
190-
"""
191-
if isinstance(source, vgi.log.Message):
192-
return cls(batch=empty_batch, log_message=source)
193-
# source is Output
194-
return cls(
195-
batch=source.batch if source.batch is not None else empty_batch,
196-
)
197-
198-
199169
class ScalarFunctionGenerator(vgi.function.Function[vgi.function.FunctionInitInput]):
200170
"""Generator-based base class for scalar functions.
201171
@@ -299,23 +269,23 @@ def _process_and_validate(
299269
self,
300270
generator: ScalarOutputGenerator,
301271
input_batch: pa.RecordBatch,
302-
) -> _ScalarOutputComplete:
272+
) -> OutputComplete:
303273
"""Process a batch and validate schemas and row count.
304274
305275
Args:
306276
generator: The user's process() generator.
307277
input_batch: The input RecordBatch to process.
308278
309279
Returns:
310-
_ScalarOutputComplete with validated output batch.
280+
OutputComplete with validated output batch.
311281
312282
Raises:
313283
SchemaValidationError: If input or output batch schema doesn't match.
314284
RowCountMismatchError: If output row count doesn't match input.
315285
316286
"""
317287
self._validate_input_schema(input_batch)
318-
result: _ScalarOutputComplete = _ScalarOutputComplete.from_process_result(
288+
result: OutputComplete = OutputComplete.from_process_result(
319289
generator.send(input_batch),
320290
self.empty_output_batch,
321291
)
@@ -330,22 +300,22 @@ def _process_with_exception_handling(
330300
self,
331301
generator: ScalarOutputGenerator,
332302
input_batch: pa.RecordBatch,
333-
) -> _ScalarOutputComplete:
303+
) -> OutputComplete:
334304
"""Process a batch with exception handling.
335305
336306
Wraps _process_and_validate to catch exceptions and convert them
337-
to _ScalarOutputComplete with an error log message.
307+
to OutputComplete with an error log message.
338308
"""
339309
try:
340310
return self._process_and_validate(generator, input_batch)
341311
except Exception as e:
342-
return _ScalarOutputComplete(
312+
return OutputComplete(
343313
batch=self.empty_output_batch,
344314
log_message=vgi.log.Message.from_exception(e),
345315
)
346316

347317
@final
348-
def _should_terminate(self, result: _ScalarOutputComplete) -> bool:
318+
def _should_terminate(self, result: OutputComplete) -> bool:
349319
"""Check if processing should terminate due to an exception."""
350320
return (
351321
result.log_message is not None

‎vgi/table_function.py‎

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import vgi.function
3232
import vgi.ipc_utils
3333
import vgi.log
34+
from vgi.output_complete import OutputComplete
3435

3536
__all__ = [
3637
"TableCardinality",
@@ -133,47 +134,6 @@ class Output:
133134
OutputGenerator = Generator[vgi.log.Message | Output, None, None]
134135

135136

136-
@dataclass(frozen=True, slots=True)
137-
class _OutputComplete:
138-
"""Internal: Output with guaranteed non-None batch.
139-
140-
Used by the framework to normalize generator yields. When the user yields
141-
None, Output with None batch, or Message, this class ensures we always
142-
have a valid RecordBatch for the protocol.
143-
144-
Attributes:
145-
batch: Always a valid RecordBatch (never None).
146-
log_message: Present when user yielded Message directly.
147-
148-
"""
149-
150-
batch: pa.RecordBatch
151-
log_message: vgi.log.Message | None = None
152-
153-
@classmethod
154-
def from_process_result(
155-
cls,
156-
source: vgi.log.Message | Output,
157-
empty_batch: pa.RecordBatch,
158-
) -> "_OutputComplete":
159-
"""Create from user's yield value.
160-
161-
Args:
162-
source: What the user yielded (Output or Message).
163-
empty_batch: Empty batch to substitute when needed.
164-
165-
Returns:
166-
Normalized output with guaranteed non-None batch.
167-
168-
"""
169-
if isinstance(source, vgi.log.Message):
170-
return cls(batch=empty_batch, log_message=source)
171-
# source is Output
172-
return cls(
173-
batch=source.batch if source.batch is not None else empty_batch,
174-
)
175-
176-
177137
@dataclass(frozen=True, slots=True)
178138
class ProtocolOutput:
179139
"""Output yielded by the generator after each send().
@@ -210,7 +170,7 @@ def metadata(
210170
)
211171

212172
@classmethod
213-
def from_process_result(cls, process_result: "_OutputComplete") -> "ProtocolOutput":
173+
def from_process_result(cls, process_result: "OutputComplete") -> "ProtocolOutput":
214174
"""Create a ProtocolOutput from an Output and status.
215175
216176
Args:
@@ -384,7 +344,7 @@ def process(self) -> OutputGenerator:
384344
"""
385345

386346
@final
387-
def _process_and_validate(self, generator: OutputGenerator) -> _OutputComplete:
347+
def _process_and_validate(self, generator: OutputGenerator) -> OutputComplete:
388348
"""Process a batch and validate the output schema.
389349
390350
Converts the result of the generator to OutputComplete, and
@@ -400,7 +360,7 @@ def _process_and_validate(self, generator: OutputGenerator) -> _OutputComplete:
400360
SchemaValidationError: If output batch schema doesn't match.
401361
402362
"""
403-
result: _OutputComplete = _OutputComplete.from_process_result(
363+
result: OutputComplete = OutputComplete.from_process_result(
404364
generator.send(None),
405365
self.empty_output_batch,
406366
)
@@ -411,7 +371,7 @@ def _process_and_validate(self, generator: OutputGenerator) -> _OutputComplete:
411371
def _process_with_exception_handling(
412372
self,
413373
generator: OutputGenerator,
414-
) -> _OutputComplete:
374+
) -> OutputComplete:
415375
"""Process a batch with exception handling.
416376
417377
Wraps _process_and_validate to catch exceptions and convert them
@@ -425,13 +385,13 @@ def _process_with_exception_handling(
425385
except StopIteration:
426386
raise
427387
except Exception as e:
428-
return _OutputComplete(
388+
return OutputComplete(
429389
batch=self.empty_output_batch,
430390
log_message=vgi.log.Message.from_exception(e),
431391
)
432392

433393
@final
434-
def _should_terminate(self, result: _OutputComplete) -> bool:
394+
def _should_terminate(self, result: OutputComplete) -> bool:
435395
"""Check if processing should terminate due to an exception."""
436396
return (
437397
result.log_message is not None

0 commit comments

Comments
 (0)