Scalar functions
On this page
One row in, one value out — the simplest function shape.
function defineScalarFunction
Section titled “function defineScalarFunction”export function defineScalarFunction<P extends Record<string, VgiDataType> = Record<string, VgiDataType>,R extends VgiDataType = VgiDataType,M extends Repr = "rich",>(config: ScalarFunctionConfig<P, R, M>): VgiFunctioninterface ScalarBindParameters
Section titled “interface ScalarBindParameters”export interface ScalarBindParametersFields
constArgsRecord<string, any>Constant argument values (resolved at bind time)
argumentsSchemaVgiSchemaSchema of all arguments including columnar params
settingsRecord<string, any>Settings from DuckDB
secretsRecord<string, Record<string, any>>Secrets from DuckDB
bindCallBindRequestOriginal bind request — exposes attach_opaque_data, transaction_opaque_data, function_type, etc.
type ScalarComputeResult
Section titled “type ScalarComputeResult”export type ScalarComputeResult<R extends VgiDataType, M extends Repr> =| Array<ScalarOutputValue<R, M>>| Iterable<ScalarOutputValue<R, M>>| VgiBatch;Description
Accepted compute return shapes: an array (or iterable) of output values in
the declared representation, or a pre-built VgiBatch. The representation is
enforced statically — returning a Date under repr: 'raw' (which expects a
branded Date32/Date64Ms), or a branded value under repr: 'rich' (which
expects a Date), is a COMPILE error.
type ScalarComputeRow
Section titled “type ScalarComputeRow”export type ScalarComputeRow<P extends Record<string, VgiDataType>,M extends Repr,> = { [K in keyof P]: ValueFor<P[K], M> | null };Description
The typed compute row for a scalar function: one property per declared
columnar param, valued in the representation (rich / raw) selected by the
function’s repr. (Provided as a convenience type for authors who build rows
out of the input batch.)
interface ScalarFunctionConfig
Section titled “interface ScalarFunctionConfig”export interface ScalarFunctionConfig<P extends Record<string, VgiDataType> = Record<string, VgiDataType>,R extends VgiDataType = VgiDataType,M extends Repr = "rich",>Fields
namestringcompute( batch: VgiBatch, consts: Record<string, any>, info: { settings: Record<string, any>; secrets: Record<string, Record<string, any>>; auth: AuthContext; } // NoInfer pins R/M fromreturns/reprso the compute return type is // CHECKED against them rather than widening them to fit a wrong value. ) => ScalarComputeResult<NoInfer<R>, NoInfer<M>>Process: receives the columnar input batch + const values, returns the output column as an array/iterable of values (statically typed from
returnsandrepr) or a pre-built VgiBatch.descriptionstringoptionalparamsPoptionalColumnar params (receive Arrow arrays at process time)
constParamsRecord<string, VgiDataType>optionalConstant params (receive scalar values resolved at bind time)
argDocsRecord<string, string>optionalPer-argument descriptions keyed by param name, surfaced as
vgi_docfield metadata (and via the extension’svgi_function_arguments()). Applies toparamsandconstParams; for the orderedparameterslist use each entry’sdocfield instead.parametersScalarParameterDef[]optionalOrdered parameter list. When provided, overrides
paramsandconstParams. Use this when parameters need non-default ordering (e.g. const, param, const).returnsRoptionalOutput type (static)
outputType(params: ScalarBindParameters) => VgiDataType | Promise<VgiDataType>optionalDynamic output type at bind time
reprMoptionalValue representation for compute I/O.
'rich'(default) uses JSDatefor date32/date64 and plain number/bigint elsewhere.'raw'uses the branded unit-carrying aliases (Date32, TimestampMicros, UnscaledDecimal, …). The choice flows into compute()’s statically-checked return type and selects the runtime converter used to build the output column.stabilityFunctionStabilityoptionalnullHandlingNullHandlingoptionalexamplesFunctionExample[]optionalcategoriesstring[]optionaltagsRecord<string, string>optionalmaxWorkersnumberoptionalrequiredSettingsstring[]optionalrequiredSecretsstring[]optionalcacheControlCacheControloptionalResult-cache opt-in: when set, this CacheControl’s
vgi.cache.*metadata rides every output batch’s custom metadata (per-batch — NOT the schema, which the IPC stream fixes at open). A pure, deterministic scalar only — advertising this on a non-pure scalar serves stale rows. Mirrors vgi-python’sScalarFunction.CACHE_CONTROL.Per-VALUE memoization (the extension remembering one output per distinct input value) additionally requires
perValue: true, which is off by default because a memo serve only beats a worker call when the call is expensive — see {@link CacheControl.perValue}.
type ScalarOutputValue
Section titled “type ScalarOutputValue”export type ScalarOutputValue<R extends VgiDataType, M extends Repr> =ValueFor<R, M> | null;Description
A single output value for a scalar function under representation M.
interface ScalarParameterDef
Section titled “interface ScalarParameterDef”export interface ScalarParameterDefDescription
Ordered parameter definition for scalar functions.
Fields
namestringtypeVgiDataTypeconstbooleanoptionalIf true, this is a constant parameter (scalar value resolved at bind time).
varargsbooleanoptionalIf true, this parameter accepts variable number of arguments.
docstringoptionalHuman-readable per-argument description (surfaced as
vgi_doc).choicesreadonly unknown[]optionalClosed set of allowed values (surfaced as
vgi_choices).genumberoptionalInclusive lower bound,
>=(folded intovgi_range).lenumberoptionalInclusive upper bound,
<=(folded intovgi_range).gtnumberoptionalExclusive lower bound,
>(folded intovgi_range).ltnumberoptionalExclusive upper bound,
<(folded intovgi_range).patternstringoptionalRegex the value must match (surfaced as
vgi_pattern).defaultunknownoptionalDefault value (surfaced as
vgi_default, JSON-encoded).