Skip to content

Commit c0418d3

Browse files
rustyconoverclaude
andcommitted
Add Arrow-based argument specification serialization
Implement serialization/deserialization of function argument specifications using Apache Arrow schemas. This enables functions to describe their argument signatures (types, positions, special markers) in a format that can be transmitted over IPC and understood by DuckDB for function registration. Key features: - Single Arrow schema format with positional args first, named args after - Metadata markers for special types (TableInput, AnyArrow, varargs) - Exact Arrow type preservation through schema serialization - Extract specs from function classes with Arg descriptors New public API: - ArgumentSpec: Dataclass for argument specifications - argument_specs_to_schema(): Convert specs to Arrow schema - schema_to_argument_specs(): Convert schema back to specs - extract_argument_specs(): Extract from function class 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent df0e0b8 commit c0418d3

4 files changed

Lines changed: 1081 additions & 0 deletions

File tree

‎docs/argument-serialization.md‎

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
# Argument Specification Serialization
2+
3+
This document describes how VGI function argument specifications are serialized
4+
to Apache Arrow schemas for IPC transmission and DuckDB function registration.
5+
6+
## Quick Reference
7+
8+
| Metadata Key | Value | Meaning |
9+
|--------------|-------|---------|
10+
| `vgi_arg` | `named` | Named argument (not positional) |
11+
| `vgi_type` | `table` | Table input argument |
12+
| `vgi_type` | `any` | Any Arrow type argument |
13+
| `vgi_varargs` | `true` | Variable arguments |
14+
15+
## Schema Format
16+
17+
Arguments are serialized as a **single Arrow schema** where each field
18+
represents one argument.
19+
20+
### Field Order
21+
22+
1. **Positional arguments** come first, in order (field index = position index)
23+
2. **Named arguments** follow, marked with metadata
24+
25+
### Field Components
26+
27+
| Component | Source |
28+
|-----------|--------|
29+
| Field name | Python attribute name |
30+
| Field type | Exact Arrow data type |
31+
| Field metadata | Markers for named, table, any, varargs |
32+
33+
## Positional Arguments
34+
35+
Positional arguments have no special metadata. Their position index is
36+
determined by their order in the schema.
37+
38+
```python
39+
# Arg[int](0) becomes:
40+
pa.field("count", pa.int64())
41+
42+
# Arg[str](1) becomes:
43+
pa.field("name", pa.utf8())
44+
```
45+
46+
## Named Arguments
47+
48+
Named arguments have `vgi_arg=named` metadata. The field name is the
49+
argument key used in SQL.
50+
51+
```python
52+
# Arg[str]("format") becomes:
53+
pa.field("format", pa.utf8(), metadata={b"vgi_arg": b"named"})
54+
```
55+
56+
## Special Types
57+
58+
### Table Input
59+
60+
Table input arguments (`Arg[TableInput]`) receive streaming RecordBatches
61+
rather than scalar values.
62+
63+
- **Arrow type**: `pa.null()`
64+
- **Metadata**: `{b"vgi_type": b"table"}`
65+
66+
```python
67+
# Arg[TableInput](1) becomes:
68+
pa.field("data", pa.null(), metadata={b"vgi_type": b"table"})
69+
```
70+
71+
### Any Type
72+
73+
Any-type arguments (`Arg[AnyArrow]`) accept any valid Arrow scalar type
74+
at runtime.
75+
76+
- **Arrow type**: `pa.null()`
77+
- **Metadata**: `{b"vgi_type": b"any"}`
78+
79+
```python
80+
# Arg[AnyArrow](0) becomes:
81+
pa.field("value", pa.null(), metadata={b"vgi_type": b"any"})
82+
```
83+
84+
### Variable Arguments
85+
86+
Varargs arguments (`varargs=True`) collect all remaining positional
87+
arguments from their position onwards.
88+
89+
- **Arrow type**: The element type (e.g., `pa.int64()` for int varargs)
90+
- **Metadata**: `{b"vgi_varargs": b"true"}`
91+
92+
```python
93+
# Arg[str](0, varargs=True) becomes:
94+
pa.field("columns", pa.utf8(), metadata={b"vgi_varargs": b"true"})
95+
```
96+
97+
## Combined Metadata
98+
99+
Fields can have multiple metadata keys. For example, a named argument
100+
that accepts any type:
101+
102+
```python
103+
# Arg[AnyArrow]("threshold") becomes:
104+
pa.field("threshold", pa.null(), metadata={
105+
b"vgi_arg": b"named",
106+
b"vgi_type": b"any",
107+
})
108+
```
109+
110+
## Complete Examples
111+
112+
### Example 1: Simple Function
113+
114+
```python
115+
class MyFunction(TableInOutFunction):
116+
count = Arg[int](0) # Positional 0
117+
name = Arg[str](1) # Positional 1
118+
verbose = Arg[bool]("verbose") # Named
119+
120+
# Serializes to:
121+
schema = pa.schema([
122+
pa.field("count", pa.int64()),
123+
pa.field("name", pa.utf8()),
124+
pa.field("verbose", pa.bool_(), metadata={b"vgi_arg": b"named"}),
125+
])
126+
```
127+
128+
### Example 2: Function with Table Input
129+
130+
```python
131+
class TransformFunction(TableInOutFunction):
132+
multiplier = Arg[float](0)
133+
data: TableInput = Arg[TableInput](1)
134+
135+
# Serializes to:
136+
schema = pa.schema([
137+
pa.field("multiplier", pa.float64()),
138+
pa.field("data", pa.null(), metadata={b"vgi_type": b"table"}),
139+
])
140+
```
141+
142+
### Example 3: Function with Varargs
143+
144+
```python
145+
class SumColumnsFunction(TableInOutFunction):
146+
columns = Arg[str](0, varargs=True)
147+
148+
# Serializes to:
149+
schema = pa.schema([
150+
pa.field("columns", pa.utf8(), metadata={b"vgi_varargs": b"true"}),
151+
])
152+
```
153+
154+
### Example 4: Complex Function
155+
156+
```python
157+
class ComplexFunction(TableInOutFunction):
158+
count = Arg[int](0)
159+
data: TableInput = Arg[TableInput](1)
160+
extra = Arg[float](2, varargs=True)
161+
format = Arg[str]("format")
162+
threshold: AnyArrow = Arg[AnyArrow]("threshold")
163+
164+
# Serializes to:
165+
schema = pa.schema([
166+
pa.field("count", pa.int64()),
167+
pa.field("data", pa.null(), metadata={b"vgi_type": b"table"}),
168+
pa.field("extra", pa.float64(), metadata={b"vgi_varargs": b"true"}),
169+
pa.field("format", pa.utf8(), metadata={b"vgi_arg": b"named"}),
170+
pa.field("threshold", pa.null(), metadata={
171+
b"vgi_arg": b"named",
172+
b"vgi_type": b"any",
173+
}),
174+
])
175+
```
176+
177+
## Serialization Code
178+
179+
### Serialize to Bytes
180+
181+
```python
182+
from vgi.argument_spec import argument_specs_to_schema
183+
184+
# Create schema from specs
185+
schema = argument_specs_to_schema(specs)
186+
187+
# Serialize to bytes
188+
schema_bytes = schema.serialize().to_pybytes()
189+
```
190+
191+
### Deserialize from Bytes
192+
193+
```python
194+
import pyarrow as pa
195+
from vgi.argument_spec import schema_to_argument_specs
196+
197+
# Deserialize schema
198+
schema = pa.ipc.read_schema(pa.py_buffer(schema_bytes))
199+
200+
# Convert to ArgumentSpec objects
201+
specs = schema_to_argument_specs(schema)
202+
```
203+
204+
## Parsing Algorithm
205+
206+
To parse a schema back to argument specifications:
207+
208+
1. Initialize `position_index = 0`
209+
2. For each field in schema:
210+
- Check if field has `vgi_arg=named` metadata
211+
- If named: `position = field.name` (string)
212+
- If positional: `position = position_index`, then increment `position_index`
213+
- Check for `vgi_type` metadata (`table` or `any`)
214+
- Check for `vgi_varargs` metadata
215+
- Create `ArgumentSpec` with extracted info
216+
217+
```python
218+
def parse_schema(schema):
219+
specs = []
220+
position_index = 0
221+
222+
for field in schema:
223+
metadata = field.metadata or {}
224+
225+
# Determine position type
226+
if metadata.get(b"vgi_arg") == b"named":
227+
position = field.name # Named argument
228+
else:
229+
position = position_index # Positional argument
230+
position_index += 1
231+
232+
# Check special types
233+
vgi_type = metadata.get(b"vgi_type")
234+
is_table_input = (vgi_type == b"table")
235+
is_any_type = (vgi_type == b"any")
236+
is_varargs = (metadata.get(b"vgi_varargs") == b"true")
237+
238+
specs.append(ArgumentSpec(
239+
name=field.name,
240+
position=position,
241+
arrow_type=field.type,
242+
is_table_input=is_table_input,
243+
is_any_type=is_any_type,
244+
is_varargs=is_varargs,
245+
))
246+
247+
return specs
248+
```
249+
250+
## Not Included
251+
252+
The following are **not** serialized in the schema:
253+
254+
- **Default values** - handled at runtime by the `Arg` descriptor
255+
- **Validation constraints** (`ge`, `le`, `choices`, `pattern`) - Python-side validation
256+
- **Documentation strings** - available via `ParameterInfo` in metadata
257+
258+
These are implementation details of the Python function runtime, not part
259+
of the argument type specification needed for function registration.

0 commit comments

Comments
 (0)