vgi.worker
Module overview
VGI Worker base class for hosting user-defined functions and catalogs.
A worker is a subprocess that communicates via stdin/stdout using Arrow IPC. Workers are spawned by a client as needed and terminate once they detect their input stream has been closed.
SUPPORTED FUNCTION TYPES
The worker supports three function types, dispatched based on class inheritance:
-
ScalarFunction/ScalarFunctionGenerator: Transforms input batches to single-column output with 1:1 row mapping. Use for per-row computations. -
TableInOutFunction/TableInOutGenerator: Reads input batches, produces output batches. Use for transforming, filtering, or aggregating input. -
TableFunctionGenerator: Generates output batches without reading input. Use for data generation functions like sequence(), range(), etc.
QUICK START
Create a worker by subclassing Worker and listing your functions:
from vgi.worker import Workerfrom vgi.scalar_function import ScalarFunctionfrom vgi.table_in_out_function import TableInOutGeneratorfrom vgi.table_function import TableFunctionGenerator
class DoubleColumn(ScalarFunction): # Single-column output with 1:1 row mapping ...
class EchoFunction(TableInOutGenerator): # Transforms input batches ...
class SequenceFunction(TableFunctionGenerator): # Generates output without input ...
class MyWorker(Worker): functions = [DoubleColumn, EchoFunction, SequenceFunction]
if __name__ == "__main__": MyWorker().run()Function names are derived from metadata (Meta.name or class name converted to snake_case). No manual name mapping required.
KEY CLASSES
Worker - Base class to subclass (set functions attribute)See Also:
vgi.client.Client : Spawns workers and sends data to them vgi.function.Function : Base class for all functions vgi._test_fixtures.worker : Example worker with built-in functions
function run_table_buffering_finalize_tick
Section titled “function run_table_buffering_finalize_tick”run_table_buffering_finalize_tick(
state: Any,
out: Any,
ctx: Any,
) -> None
One tick of cls.finalize(params, fid, state, out).
Lazy-imported by TableBufferingFinalizeState.produce() to break
the protocol→worker import cycle. Cold-resolves func_cls + params on
every call (no in-process cache — different worker processes may
handle different ticks under HTTP).
Applies the pushdown contract symmetric with the streaming
TableInOutExchangeState (protocol.py:1106-1186): narrow
params.output_schema to the projected slots and, when
Meta.auto_apply_filters is True, wrap out in a filtering
collector so the user’s finalize() doesn’t need to know.
class Worker
Section titled “class Worker”Description
Base class for VGI workers that host user-defined functions.
Subclass this and define a functions class attribute listing your function
classes. Function names are derived from metadata (Meta.name or snake_case
of class name). The worker handles the VGI protocol via vgi_rpc.RpcServer.
Multiple functions can share the same name if they have different argument signatures (function overloading). The worker will select the appropriate function based on the invocation’s arguments.
Catalog interface
If catalog_interface is not set but functions is non-empty, a default
read-only catalog interface is created automatically. This exposes the
worker’s functions via the catalog protocol, allowing clients to discover
available functions.
To customize the catalog, set catalog_interface to a CatalogInterface
subclass. To disable the catalog entirely, set catalog_interface = None
and catalog_name = None. A catalog-less worker is reachable only from
the pure-Python Client — DuckDB reaches VGI functions exclusively
through ATTACH, which requires a catalog.
Attributes
attribute functions
Section titled “attribute functions”Sequence[type[Function]]
Function classes this worker hosts.
attribute protocol_class
Section titled “attribute protocol_class”type[VgiProtocol]
Protocol class handed to RpcServer; defaults to the
real VgiProtocol. Test fixtures override it with a subclass
that redeclares protocol_version to exercise version-mismatch
enforcement.
attribute catalog_interface
Section titled “attribute catalog_interface”type[CatalogInterface] | None
Custom CatalogInterface subclass, or None
to use the auto-generated default (or disable the catalog).
attribute catalog_name
Section titled “attribute catalog_name”str | None
Name of the default catalog; set to None to disable the
default catalog.
attribute catalog
Section titled “attribute catalog”Catalog | None
Optional declarative Catalog describing the worker’s
schemas, tables, and views.
Methods
method resolve_token
Section titled “method resolve_token”resolve_token(token: str) -> TokenIdentity | NoneResolve an opaque bearer credential to the identity it authenticates as.
Override to enable POST {prefix}/__introspect_token__, which a
reverse proxy calls when it terminates the only public listener and
must know which principal a credential is before it can authorize
anything. Until it is overridden the route does not exist at all —
not “exists and refuses”, absent — so no worker grows a
credential-to-identity oracle by upgrading a dependency.
Enabling it also requires an allowlist of principals permitted to ask
(--introspect-principals / VGI_INTROSPECT_PRINCIPALS). There is
no permissive default: authenticating and introspecting are different
capabilities, and a deployment where any valid credential may
introspect lets any user resolve any other user’s credential to its
owner. Overriding this without setting the allowlist is a startup
error rather than a silently-open endpoint.
This is deliberately not “run the credential back through the
worker’s own authenticate chain” — see
vgi_rpc.http.server._introspect for the four ways that breaks.
Write a narrow lookup against whatever store issued the credential.
method main
Section titled “method main”main() -> NoneRun this worker as a CLI application with logging options.
By default, serves over stdin/stdout (pipe transport).
Pass --http to serve over HTTP instead.
Supports --quiet, --debug, --log-level,
--log-logger, and --log-format for logging control.
HTTP-specific options (only used with --http):
--host, --port, --prefix, --cors-origins,
--describe/--no-describe.
Requires the http extra for HTTP mode: pip install vgi[http]
method bind
Section titled “method bind”bind(request: BindRequest, ctx: CallContext) -> BindResponseResolve output schema and validate arguments.
Implements VgiProtocol.bind().
method table_function_cardinality
Section titled “method table_function_cardinality”table_function_cardinality(
request: TableFunctionCardinalityRequest,
ctx: CallContext,
) -> TableCardinalityEstimate the cardinality of a table function’s output.
Implements VgiProtocol.table_function_cardinality().
method table_function_statistics
Section titled “method table_function_statistics”table_function_statistics(
request: TableFunctionStatisticsRequest,
ctx: CallContext,
) -> bytes | NoneReturn per-column statistics for a table function’s output.
Implements VgiProtocol.table_function_statistics(). Returns IPC bytes of the serialized ColumnStatistics batch (same wire shape as catalog_table_column_statistics_get), or None when stats are unknown.
method table_function_dynamic_to_string
Section titled “method table_function_dynamic_to_string”table_function_dynamic_to_string(
request: TableFunctionDynamicToStringRequest,
ctx: CallContext,
) -> TableFunctionDynamicToStringResponseReturn user diagnostics for EXPLAIN ANALYZE Extra Info.
Implements VgiProtocol.table_function_dynamic_to_string(). Fired once per parallel scan thread post-execution. Best-effort: any exception (including a misbehaving user override) is logged and an empty response is returned so the EA query never aborts.
method aggregate_bind
Section titled “method aggregate_bind”aggregate_bind(
request: AggregateBindRequest,
ctx: CallContext,
) -> AggregateBindResponseBind an aggregate function, return output schema and execution_id.
method aggregate_update
Section titled “method aggregate_update”aggregate_update(
request: AggregateUpdateRequest,
ctx: CallContext,
) -> AggregateUpdateResponseAccumulate rows from a DataChunk into per-group state.
method aggregate_combine
Section titled “method aggregate_combine”aggregate_combine(
request: AggregateCombineRequest,
ctx: CallContext,
) -> AggregateCombineResponseMerge source states into target states.
method aggregate_finalize
Section titled “method aggregate_finalize”aggregate_finalize(
request: AggregateFinalizeRequest,
ctx: CallContext,
) -> AggregateFinalizeResponseProduce results for a chunk of group_ids.
method aggregate_destructor
Section titled “method aggregate_destructor”aggregate_destructor(
request: AggregateDestructorRequest,
ctx: CallContext,
) -> AggregateDestructorResponseBest-effort cleanup of aggregate states.
method table_buffering_process
Section titled “method table_buffering_process”table_buffering_process(request: Any, ctx: CallContext) -> AnySink one input batch; return worker-chosen state_id (unary).
method table_buffering_combine
Section titled “method table_buffering_combine”table_buffering_combine(request: Any, ctx: CallContext) -> AnyEnd-of-input bridge: hand all state_ids to user combine().
method table_buffering_destructor
Section titled “method table_buffering_destructor”table_buffering_destructor(request: Any, ctx: CallContext) -> AnyBest-effort end-of-query cleanup.
method aggregate_window_init
Section titled “method aggregate_window_init”aggregate_window_init(
request: AggregateWindowInitRequest,
ctx: CallContext,
) -> AggregateWindowInitResponseCache a partition on the worker for windowed aggregation.
method aggregate_window
Section titled “method aggregate_window”aggregate_window(
request: AggregateWindowRequest,
ctx: CallContext,
) -> AggregateWindowResponseCompute one output row for a windowed aggregate.
method aggregate_window_batch
Section titled “method aggregate_window_batch”aggregate_window_batch(
request: AggregateWindowBatchRequest,
ctx: CallContext,
) -> AggregateWindowBatchResponseCompute count window output rows in a single batched RPC.
method aggregate_window_destructor
Section titled “method aggregate_window_destructor”aggregate_window_destructor(
request: AggregateWindowDestructorRequest,
ctx: CallContext,
) -> AggregateWindowDestructorResponseEvict a cached partition from storage.
method aggregate_streaming_open
Section titled “method aggregate_streaming_open”aggregate_streaming_open(
request: AggregateStreamingOpenRequest,
ctx: CallContext,
) -> AggregateStreamingOpenResponseOpen a streaming-partitioned aggregate session.
method aggregate_streaming_chunk
Section titled “method aggregate_streaming_chunk”aggregate_streaming_chunk(
request: AggregateStreamingChunkRequest,
ctx: CallContext,
) -> AggregateStreamingChunkResponseProcess one chunk of streaming input.
method aggregate_streaming_close
Section titled “method aggregate_streaming_close”aggregate_streaming_close(
request: AggregateStreamingCloseRequest,
ctx: CallContext,
) -> AggregateStreamingCloseResponseEnd a streaming-partitioned aggregate session.
method init
Section titled “method init”init(
request: InitRequest,
ctx: CallContext,
) -> Stream[ProcessState, GlobalInitResponse]Initialize a function execution and return a processing stream.
Implements VgiProtocol.init(). Creates the appropriate state object based on function type and creates the appropriate state object.
method catalog_catalogs
Section titled “method catalog_catalogs”catalog_catalogs() -> CatalogsResponseList available catalog discovery records.
method catalog_attach
Section titled “method catalog_attach”catalog_attach(
request: CatalogAttachRequest,
*,
ctx: CallContext | None = None,
) -> CatalogAttachResultAttach to a catalog with options.
method catalog_detach
Section titled “method catalog_detach”catalog_detach(attach_opaque_data: bytes) -> NoneDetach from a catalog.
method catalog_create
Section titled “method catalog_create”catalog_create(request: CatalogCreateRequest) -> NoneCreate a new catalog.
method catalog_drop
Section titled “method catalog_drop”catalog_drop(name: str) -> NoneDrop a catalog.
method catalog_version
Section titled “method catalog_version”catalog_version(
attach_opaque_data: bytes,
transaction_opaque_data: bytes | None = None,
*,
ctx: CallContext | None = None,
) -> CatalogVersionResponseGet the current catalog version.
method catalog_transaction_begin
Section titled “method catalog_transaction_begin”catalog_transaction_begin(
attach_opaque_data: bytes,
) -> TransactionBeginResponseBegin a new transaction.
method catalog_transaction_commit
Section titled “method catalog_transaction_commit”catalog_transaction_commit(
attach_opaque_data: bytes,
transaction_opaque_data: bytes,
) -> NoneCommit a transaction.
method catalog_transaction_rollback
Section titled “method catalog_transaction_rollback”catalog_transaction_rollback(
attach_opaque_data: bytes,
transaction_opaque_data: bytes,
) -> NoneRollback a transaction.
method catalog_schemas
Section titled “method catalog_schemas”catalog_schemas(
attach_opaque_data: bytes,
transaction_opaque_data: bytes | None = None,
) -> SchemasResponseList schemas in the catalog.
method catalog_schema_get
Section titled “method catalog_schema_get”catalog_schema_get(
attach_opaque_data: bytes,
name: str,
transaction_opaque_data: bytes | None = None,
) -> SchemasResponseGet information about a schema. Returns 0 or 1 items.
method catalog_schema_create
Section titled “method catalog_schema_create”catalog_schema_create(
attach_opaque_data: bytes,
name: str,
on_conflict: OnConflict = OnConflict.ERROR,
comment: str | None = None,
tags: dict[str, str] | None = None,
transaction_opaque_data: bytes | None = None,
) -> NoneCreate a new schema.
method catalog_schema_drop
Section titled “method catalog_schema_drop”catalog_schema_drop(
attach_opaque_data: bytes,
name: str,
ignore_not_found: bool = False,
cascade: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneDrop a schema.
method catalog_schema_contents_tables
Section titled “method catalog_schema_contents_tables”catalog_schema_contents_tables(
attach_opaque_data: bytes,
name: str,
transaction_opaque_data: bytes | None = None,
) -> TablesResponseList tables in a schema.
method catalog_schema_contents_views
Section titled “method catalog_schema_contents_views”catalog_schema_contents_views(
attach_opaque_data: bytes,
name: str,
transaction_opaque_data: bytes | None = None,
) -> ViewsResponseList views in a schema.
method catalog_schema_contents_functions
Section titled “method catalog_schema_contents_functions”catalog_schema_contents_functions(
attach_opaque_data: bytes,
name: str,
type: SchemaObjectType,
transaction_opaque_data: bytes | None = None,
) -> FunctionsResponseList functions in a schema (scalar or table).
method catalog_copy_from_formats
Section titled “method catalog_copy_from_formats”catalog_copy_from_formats(
attach_opaque_data: bytes,
transaction_opaque_data: bytes | None = None,
) -> CopyFromFormatsResponseList custom COPY ... FROM formats advertised by this catalog.
method catalog_table_get
Section titled “method catalog_table_get”catalog_table_get(
attach_opaque_data: bytes,
schema_name: str,
name: str,
at_unit: str | None = None,
at_value: str | None = None,
transaction_opaque_data: bytes | None = None,
) -> TablesResponseGet information about a table. Returns 0 or 1 items.
method catalog_table_create
Section titled “method catalog_table_create”catalog_table_create(request: TableCreateRequest) -> NoneCreate a new table.
method catalog_table_drop
Section titled “method catalog_table_drop”catalog_table_drop(
attach_opaque_data: bytes,
schema_name: str,
name: str,
ignore_not_found: bool = False,
cascade: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneDrop a table.
method catalog_table_scan_function_get
Section titled “method catalog_table_scan_function_get”catalog_table_scan_function_get(
attach_opaque_data: bytes,
schema_name: str,
name: str,
at_unit: str | None = None,
at_value: str | None = None,
transaction_opaque_data: bytes | None = None,
) -> bytesGet the scan function for a table. Returns ScanFunctionResult as IPC bytes.
method catalog_table_scan_branches_get
Section titled “method catalog_table_scan_branches_get”catalog_table_scan_branches_get(
attach_opaque_data: bytes,
schema_name: str,
name: str,
at_unit: str | None = None,
at_value: str | None = None,
transaction_opaque_data: bytes | None = None,
) -> bytesGet the list of scan branches for a multi-branch table.
Returns ScanBranchesResult as IPC bytes. The CatalogInterface base
provides a default-impl shim that wraps the legacy
table_scan_function_get as a one-branch result, so every existing
single-source worker automatically responds correctly here without
further code changes.
method catalog_table_column_statistics_get
Section titled “method catalog_table_column_statistics_get”catalog_table_column_statistics_get(
attach_opaque_data: bytes,
schema_name: str,
name: str,
transaction_opaque_data: bytes | None = None,
) -> bytes | NoneGet column statistics for a table. Returns IPC bytes or None.
method catalog_table_insert_function_get
Section titled “method catalog_table_insert_function_get”catalog_table_insert_function_get(
attach_opaque_data: bytes,
schema_name: str,
name: str,
transaction_opaque_data: bytes | None = None,
writable_branch_function_name: str | None = None,
) -> bytesGet the insert function for a table. Returns WriteFunctionResult as IPC bytes.
method catalog_table_update_function_get
Section titled “method catalog_table_update_function_get”catalog_table_update_function_get(
attach_opaque_data: bytes,
schema_name: str,
name: str,
transaction_opaque_data: bytes | None = None,
) -> bytesGet the update function for a table. Returns WriteFunctionResult as IPC bytes.
method catalog_table_delete_function_get
Section titled “method catalog_table_delete_function_get”catalog_table_delete_function_get(
attach_opaque_data: bytes,
schema_name: str,
name: str,
transaction_opaque_data: bytes | None = None,
) -> bytesGet the delete function for a table. Returns WriteFunctionResult as IPC bytes.
method catalog_table_comment_set
Section titled “method catalog_table_comment_set”catalog_table_comment_set(
attach_opaque_data: bytes,
schema_name: str,
name: str,
comment: str | None = None,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneSet or clear the comment on a table.
method catalog_table_column_comment_set
Section titled “method catalog_table_column_comment_set”catalog_table_column_comment_set(
attach_opaque_data: bytes,
schema_name: str,
name: str,
column_name: str,
comment: str | None = None,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneSet or clear the comment on a table column.
method catalog_table_rename
Section titled “method catalog_table_rename”catalog_table_rename(
attach_opaque_data: bytes,
schema_name: str,
name: str,
new_name: str,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneRename a table.
method catalog_table_column_add
Section titled “method catalog_table_column_add”catalog_table_column_add(
attach_opaque_data: bytes,
schema_name: str,
name: str,
column_definition: bytes,
ignore_not_found: bool = False,
if_column_not_exists: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneAdd a new column to a table.
method catalog_table_column_drop
Section titled “method catalog_table_column_drop”catalog_table_column_drop(
attach_opaque_data: bytes,
schema_name: str,
name: str,
column_name: str,
ignore_not_found: bool = False,
if_column_exists: bool = False,
cascade: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneDrop a column from a table.
method catalog_table_column_rename
Section titled “method catalog_table_column_rename”catalog_table_column_rename(
attach_opaque_data: bytes,
schema_name: str,
name: str,
column_name: str,
new_column_name: str,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneRename a column.
method catalog_table_column_default_set
Section titled “method catalog_table_column_default_set”catalog_table_column_default_set(
attach_opaque_data: bytes,
schema_name: str,
name: str,
column_name: str,
expression: str,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneSet the default value expression for a column.
method catalog_table_column_default_drop
Section titled “method catalog_table_column_default_drop”catalog_table_column_default_drop(
attach_opaque_data: bytes,
schema_name: str,
name: str,
column_name: str,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneRemove the default value from a column.
method catalog_table_column_type_change
Section titled “method catalog_table_column_type_change”catalog_table_column_type_change(
attach_opaque_data: bytes,
schema_name: str,
name: str,
column_definition: bytes,
expression: str | None = None,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneChange the type of a column.
method catalog_table_not_null_drop
Section titled “method catalog_table_not_null_drop”catalog_table_not_null_drop(
attach_opaque_data: bytes,
schema_name: str,
name: str,
column_name: str,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneRemove NOT NULL constraint from a column.
method catalog_table_not_null_set
Section titled “method catalog_table_not_null_set”catalog_table_not_null_set(
attach_opaque_data: bytes,
schema_name: str,
name: str,
column_name: str,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneAdd NOT NULL constraint to a column.
method catalog_view_get
Section titled “method catalog_view_get”catalog_view_get(
attach_opaque_data: bytes,
schema_name: str,
name: str,
transaction_opaque_data: bytes | None = None,
) -> ViewsResponseGet information about a view. Returns 0 or 1 items.
method catalog_view_create
Section titled “method catalog_view_create”catalog_view_create(
attach_opaque_data: bytes,
schema_name: str,
name: str,
definition: str,
on_conflict: OnConflict,
transaction_opaque_data: bytes | None = None,
) -> NoneCreate a new view.
method catalog_view_drop
Section titled “method catalog_view_drop”catalog_view_drop(
attach_opaque_data: bytes,
schema_name: str,
name: str,
ignore_not_found: bool = False,
cascade: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneDrop a view.
method catalog_view_rename
Section titled “method catalog_view_rename”catalog_view_rename(
attach_opaque_data: bytes,
schema_name: str,
name: str,
new_name: str,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneRename a view.
method catalog_view_comment_set
Section titled “method catalog_view_comment_set”catalog_view_comment_set(
attach_opaque_data: bytes,
schema_name: str,
name: str,
comment: str | None = None,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneSet or clear the comment on a view.
method catalog_macro_get
Section titled “method catalog_macro_get”catalog_macro_get(
attach_opaque_data: bytes,
schema_name: str,
name: str,
transaction_opaque_data: bytes | None = None,
) -> MacrosResponseGet information about a macro. Returns 0 or 1 items.
method catalog_macro_create
Section titled “method catalog_macro_create”catalog_macro_create(request: MacroCreateRequest) -> NoneCreate a new macro.
method catalog_macro_drop
Section titled “method catalog_macro_drop”catalog_macro_drop(
attach_opaque_data: bytes,
schema_name: str,
name: str,
ignore_not_found: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneDrop a macro.
method catalog_schema_contents_macros
Section titled “method catalog_schema_contents_macros”catalog_schema_contents_macros(
attach_opaque_data: bytes,
name: str,
type: SchemaObjectType,
transaction_opaque_data: bytes | None = None,
) -> MacrosResponseList macros in a schema (scalar or table).
method catalog_index_get
Section titled “method catalog_index_get”catalog_index_get(
attach_opaque_data: bytes,
schema_name: str,
name: str,
transaction_opaque_data: bytes | None = None,
) -> IndexesResponseGet information about an index. Returns 0 or 1 items.
method catalog_index_create
Section titled “method catalog_index_create”catalog_index_create(request: IndexCreateRequest) -> NoneCreate a new index.
method catalog_index_drop
Section titled “method catalog_index_drop”catalog_index_drop(
attach_opaque_data: bytes,
schema_name: str,
name: str,
ignore_not_found: bool = False,
cascade: bool = False,
transaction_opaque_data: bytes | None = None,
) -> NoneDrop an index.
method catalog_schema_contents_indexes
Section titled “method catalog_schema_contents_indexes”catalog_schema_contents_indexes(
attach_opaque_data: bytes,
name: str,
transaction_opaque_data: bytes | None = None,
) -> IndexesResponseList indexes in a schema.
method run
Section titled “method run”run(otel_config: Any = None) -> NoneRun the worker, reading from stdin and writing to stdout.