|
1 | 1 | # Generator API (Advanced) |
2 | 2 |
|
3 | | -This document covers the generator-based APIs for both table functions and table-in-out functions. |
| 3 | +This document covers the generator-based APIs for scalar, table, and table-in-out functions. |
| 4 | + |
| 5 | +## Scalar Function Generator (Row Transform) |
| 6 | + |
| 7 | +Use `ScalarFunctionGenerator` when you need full generator control for scalar transforms, |
| 8 | +including yielding log messages during processing. For most use cases, prefer the simpler |
| 9 | +`ScalarFunction` with its `compute()` callback. |
| 10 | + |
| 11 | +### Basic Template |
| 12 | + |
| 13 | +```python |
| 14 | +import pyarrow as pa |
| 15 | +import pyarrow.compute as pc |
| 16 | +from vgi import ScalarFunctionGenerator, Output, Arg |
| 17 | +from vgi.log import Level, Message |
| 18 | + |
| 19 | +class MyScalarFunction(ScalarFunctionGenerator): |
| 20 | + """Transform each row to a single output value.""" |
| 21 | + |
| 22 | + column = Arg[str](0, doc="Column to transform") |
| 23 | + |
| 24 | + @property |
| 25 | + def output_schema(self) -> pa.Schema: |
| 26 | + # Must have exactly one column |
| 27 | + return pa.schema([("result", pa.int64())]) |
| 28 | + |
| 29 | + def process(self, batch: pa.RecordBatch) -> ScalarOutputGenerator: |
| 30 | + # Priming yield - REQUIRED |
| 31 | + _ = yield Output(self.empty_output_batch) |
| 32 | + |
| 33 | + while True: |
| 34 | + # Optional: yield log messages |
| 35 | + yield Message(Level.INFO, f"Processing {batch.num_rows} rows") |
| 36 | + |
| 37 | + # Compute result - must have same row count as input |
| 38 | + result = pc.multiply(batch.column(self.column), 2) |
| 39 | + output = pa.RecordBatch.from_arrays([result], schema=self.output_schema) |
| 40 | + |
| 41 | + batch = yield Output(output) |
| 42 | + if batch is None: |
| 43 | + break |
| 44 | +``` |
| 45 | + |
| 46 | +### ScalarFunctionGenerator Methods |
| 47 | + |
| 48 | +| Method | When to Override | Default | |
| 49 | +|--------|------------------|---------| |
| 50 | +| `process(batch)` | Always - transform input | Required | |
| 51 | +| `output_schema` | Define single-column output | Required | |
| 52 | +| `setup()` | Acquire resources | No-op | |
| 53 | +| `teardown()` | Release resources | No-op | |
| 54 | + |
| 55 | +### Key Constraints |
| 56 | + |
| 57 | +1. **Single-column output**: `output_schema` must have exactly one column |
| 58 | +2. **1:1 row mapping**: Output `num_rows` must equal input `num_rows` |
| 59 | +3. **No finalize phase**: Processing ends when input stream ends |
| 60 | +4. **Priming yield required**: Generator must start with `_ = yield Output(...)` |
| 61 | + |
| 62 | +### When to Use Generator vs Callback API |
| 63 | + |
| 64 | +| Feature | ScalarFunctionGenerator | ScalarFunction | |
| 65 | +|---------|------------------------|----------------| |
| 66 | +| API style | Generator with `process()` | Callback with `compute()` | |
| 67 | +| Logging | Yield `Message` directly | Call `self.log(level, msg)` | |
| 68 | +| Complexity | More control, more code | Simpler, less code | |
| 69 | +| Use when | Need log messages mid-batch | Standard transforms | |
| 70 | + |
| 71 | +--- |
4 | 72 |
|
5 | 73 | ## Table Function Generator (No Input) |
6 | 74 |
|
|
0 commit comments