Skip to content

Commit 719d00e

Browse files
committed
docs improvements
1 parent 83538b9 commit 719d00e

4 files changed

Lines changed: 155 additions & 2 deletions

File tree

‎docs/generator-api.md‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,74 @@
11
# Generator API (Advanced)
22

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+
---
472

573
## Table Function Generator (No Input)
674

‎docs/lifecycle.md‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,40 @@
22

33
Understanding when lifecycle methods are called is critical for resource management and distributed processing.
44

5-
## Single-Process Lifecycle (max_processes=1)
5+
## Scalar Function Lifecycle
6+
7+
Scalar functions have a simplified lifecycle with no finalize phase. Processing ends when input is exhausted.
8+
9+
```
10+
┌─────────────────────────────────────────────────────────────────┐
11+
│ __init__(invocation, logger) │
12+
│ ↓ │
13+
│ output_schema (property accessed, must be single column) │
14+
│ ↓ │
15+
│ setup() ← Acquire resources here │
16+
│ ↓ │
17+
│ ┌─────────────────────────────────────────────────────────┐ │
18+
│ │ process(batch1) → compute(batch1) → Array │ │
19+
│ │ ↓ │ │
20+
│ │ [return output with same row count] │ │
21+
│ │ ↓ │ │
22+
│ │ process(batch2) → compute(batch2) → Array │ │
23+
│ │ ↓ │ │
24+
│ │ ... (repeat for all batches) │ │
25+
│ │ ↓ │ │
26+
│ │ Input stream ends (generator closed) │ │
27+
│ └─────────────────────────────────────────────────────────┘ │
28+
│ ↓ │
29+
│ teardown() ← Release resources (always called) │
30+
└─────────────────────────────────────────────────────────────────┘
31+
```
32+
33+
**Key differences from Table-In-Out:**
34+
- No `finalize()` phase - processing ends when input ends
35+
- No `save_state()` / `load_states()` - not designed for distributed aggregation
36+
- Output must have exactly 1 column with same row count as input
37+
38+
## Table-In-Out Single-Process Lifecycle (max_processes=1)
639

740
```
841
┌─────────────────────────────────────────────────────────────────┐

‎docs/metadata.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Functions can define a nested `Meta` class to provide introspection metadata. No
44

55
## Basic Usage
66

7+
Metadata works with all function types: `ScalarFunction`, `TableFunctionGenerator`, and `TableInOutFunction`.
8+
79
```python
810
from vgi import TableInOutFunction, Arg
911

@@ -23,6 +25,28 @@ class SumColumnsFunction(TableInOutFunction):
2325
...
2426
```
2527

28+
```python
29+
from vgi import ScalarFunction, Arg
30+
import pyarrow as pa
31+
import pyarrow.compute as pc
32+
33+
class DoubleColumn(ScalarFunction):
34+
"""Double the values in a numeric column."""
35+
36+
class Meta:
37+
name = "double"
38+
categories = ["numeric", "transform"]
39+
40+
column = Arg[str](0, doc="Column to double")
41+
42+
@property
43+
def output_type(self) -> pa.DataType:
44+
return self.input_schema.field(self.column).type
45+
46+
def compute(self, batch):
47+
return pc.multiply(batch.column(self.column), 2)
48+
```
49+
2650
## Accessing Metadata
2751

2852
```python

‎docs/protocol.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22

33
This document describes the Arrow IPC protocol between client and worker.
44

5+
## Scalar Function (row transform)
6+
7+
Scalar functions transform input batches to single-column output with 1:1 row mapping.
8+
They are used for per-row computations like `upper()`, `abs()`, or `concat()`.
9+
10+
```
11+
Client Worker
12+
│ │
13+
│──── Invocation (function, args) ─────▶│
14+
│ │ instantiate function
15+
│◀──── OutputSpec (output schema) ──────│ (single column)
16+
│ │
17+
│──── Input Batch 1 ───────────────────▶│
18+
│◀──── Output Batch 1 ─────────────────│ compute() / process()
19+
│ │ (same row count)
20+
│──── Input Batch 2 ───────────────────▶│
21+
│◀──── Output Batch 2 ─────────────────│
22+
│ │
23+
│──── (generator closed) ──────────────▶│
24+
│ │ teardown()
25+
```
26+
27+
**Key differences from Table-In-Out:**
28+
- Output schema must have exactly one column
29+
- Output row count must equal input row count (1:1 mapping)
30+
- No finalize phase - processing ends when input stream ends
31+
- No `NEED_MORE_INPUT` status (always expects more input until closed)
32+
533
## Table Function (no input)
634

735
Table functions generate data without receiving input batches.

0 commit comments

Comments
 (0)