@query-farm/vgi 0.44.0 → 0.46.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -1
- package/dist/arrow/impl-arrowjs/index.d.ts +1 -1
- package/dist/arrow/impl-arrowjs/ipc.d.ts +6 -0
- package/dist/arrow/impl-flechette/build.d.ts +2 -2
- package/dist/arrow/impl-flechette/canonical.d.ts +2 -2
- package/dist/arrow/impl-flechette/index.d.ts +1 -1
- package/dist/arrow/impl-flechette/ipc.d.ts +6 -0
- package/dist/arrow/impl-flechette/schema.d.ts +19 -5
- package/dist/arrow/index.d.ts +1 -1
- package/dist/catalog/contents-codec.d.ts +7 -0
- package/dist/catalog/contents-digest.d.ts +2 -1
- package/dist/catalog/interface.d.ts +3 -0
- package/dist/catalog/publication-envelope.d.ts +1 -0
- package/dist/catalog/publication.d.ts +142 -0
- package/dist/catalog/s3-publication.d.ts +38 -0
- package/dist/client/client.d.ts +20 -1
- package/dist/client-entry.js +189 -37
- package/dist/client-entry.js.map +15 -12
- package/dist/codec/asd.d.ts +4 -0
- package/dist/functions/continuation.d.ts +69 -0
- package/dist/functions/table-in-out.d.ts +51 -7
- package/dist/functions/types.d.ts +20 -0
- package/dist/generated/vgi-client.d.ts +7 -14
- package/dist/generated/vgi-protocol-schemas.d.ts +2 -0
- package/dist/generated/vgi-protocol-types.d.ts +26 -3
- package/dist/generated/vgi-service.d.ts +3 -3
- package/dist/index.core.d.ts +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +981 -138
- package/dist/index.js.map +31 -25
- package/dist/protocol/dispatch.d.ts +1 -1
- package/dist/protocol/handlers/catalog/contents.d.ts +4 -4
- package/dist/serve-entry.js +643 -81
- package/dist/serve-entry.js.map +22 -18
- package/dist/util/parent-rows.d.ts +8 -0
- package/dist/worker-cf-entry.d.ts +1 -1
- package/dist/worker-cf-entry.js +821 -131
- package/dist/worker-cf-entry.js.map +29 -24
- package/package.json +4 -4
- package/src/arrow/impl-arrowjs/index.ts +1 -0
- package/src/arrow/impl-arrowjs/ipc.ts +23 -0
- package/src/arrow/impl-flechette/build.ts +4 -2
- package/src/arrow/impl-flechette/canonical.ts +14 -7
- package/src/arrow/impl-flechette/index.ts +1 -0
- package/src/arrow/impl-flechette/ipc.ts +19 -0
- package/src/arrow/index.ts +1 -1
- package/src/catalog/contents-codec.ts +77 -0
- package/src/catalog/contents-digest.ts +20 -17
- package/src/catalog/interface.ts +3 -0
- package/src/catalog/publication-envelope.ts +62 -0
- package/src/catalog/publication.ts +290 -0
- package/src/catalog/read-only.ts +6 -2
- package/src/catalog/s3-publication.ts +81 -0
- package/src/client/client.ts +82 -30
- package/src/codec/asd.ts +40 -1
- package/src/codec/conform.ts +2 -1
- package/src/filter-pushdown/collector.ts +17 -1
- package/src/functions/continuation.ts +202 -0
- package/src/functions/table-in-out.ts +131 -56
- package/src/functions/types.ts +20 -0
- package/src/generated/vgi-client.ts +8 -16
- package/src/generated/vgi-protocol-schemas.ts +14 -6
- package/src/generated/vgi-protocol-types.ts +39 -5
- package/src/generated/vgi-service.ts +4 -3
- package/src/index.core.ts +6 -0
- package/src/index.ts +5 -0
- package/src/protocol/dispatch.ts +1 -1
- package/src/protocol/handlers/catalog/admin.ts +7 -4
- package/src/protocol/handlers/catalog/contents.ts +50 -35
- package/src/protocol/handlers/function.ts +12 -0
- package/src/protocol/state-serializer.ts +7 -1
- package/src/util/parent-rows.ts +41 -0
- package/src/worker-cf-entry.ts +3 -0
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
import { type VgiBatch, type VgiSchema, isBatch } from "../arrow/index.js";
|
|
7
7
|
import type { OutputCollector } from "@query-farm/vgi-rpc";
|
|
8
|
-
import { batchFromColumns } from "../util/arrow/index.js";
|
|
8
|
+
import { batchFromColumns, filterBatch } from "../util/arrow/index.js";
|
|
9
|
+
import { decodeParentRows, encodeParentRows, PARENT_ROW_METADATA_KEY } from "../util/parent-rows.js";
|
|
9
10
|
import { ComparisonOp, type FilterExpression } from "./types.js";
|
|
10
11
|
import { expressionToSql, PushdownFilters } from "./evaluate.js";
|
|
11
12
|
|
|
@@ -108,6 +109,21 @@ export class FilteringOutputCollector {
|
|
|
108
109
|
} else {
|
|
109
110
|
batch = batchFromColumns(batchOrColumns as Record<string, any[]>, this.inner.outputSchema as any);
|
|
110
111
|
}
|
|
112
|
+
const encoded = metadata?.get(PARENT_ROW_METADATA_KEY);
|
|
113
|
+
if (encoded !== undefined && batch.numRows > 0 && this.filters.filters.length > 0) {
|
|
114
|
+
// Provenance is per output row, so it must lose the rows the filter
|
|
115
|
+
// does: filter both with one mask. Mirrors vgi-python's
|
|
116
|
+
// _FilteringOutputCollector.
|
|
117
|
+
const mask = this.filters.evaluate(batch as any);
|
|
118
|
+
const parents = decodeParentRows(encoded);
|
|
119
|
+
const kept: number[] = [];
|
|
120
|
+
for (let i = 0; i < parents.length; i++) if (mask[i]) kept.push(parents[i]);
|
|
121
|
+
const md = new Map(metadata);
|
|
122
|
+
if (kept.length > 0) md.set(PARENT_ROW_METADATA_KEY, encodeParentRows(kept));
|
|
123
|
+
else md.delete(PARENT_ROW_METADATA_KEY);
|
|
124
|
+
this.inner.emit(filterBatch(batch as any, mask) as any, md);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
111
127
|
const filtered = this.filters.apply(batch as any);
|
|
112
128
|
this.inner.emit(filtered as any, metadata);
|
|
113
129
|
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Copyright 2025, 2026 Query Farm LLC - https://query.farm
|
|
2
|
+
// Continued output (table-in-out exchange).
|
|
3
|
+
//
|
|
4
|
+
// A table-in-out exchange answers each input batch with one reply. A function
|
|
5
|
+
// can answer one input with several: it emits each reply but the last with
|
|
6
|
+
// `moreOutputMetadata(true)`, which puts `vgi.status: HAVE_MORE_OUTPUT` on the
|
|
7
|
+
// reply. The client then asks for the next reply with a continuation tick -- a
|
|
8
|
+
// zero-row batch of the input schema carrying `vgi.status: CONTINUE` -- and the
|
|
9
|
+
// framework answers it by calling the function's `resume()`. Every reply's
|
|
10
|
+
// parent rows index the last real (non-tick) input batch.
|
|
11
|
+
//
|
|
12
|
+
// `resume()` gets no input, so the function's state must hold where to resume.
|
|
13
|
+
// Over HTTP that state reaches the next turn only inside the stream's state
|
|
14
|
+
// token, so keep it small: positions to resume from, never buffered output rows.
|
|
15
|
+
//
|
|
16
|
+
// The protocol is strict, with no capability negotiation: while output is
|
|
17
|
+
// pending a tick is the only valid input, and a tick is valid only then. Cache
|
|
18
|
+
// control may only ride an input's first reply, and "more" is refused outside a
|
|
19
|
+
// table-in-out exchange. Mirrors vgi-python's `emit(more=True)` / `resume()`.
|
|
20
|
+
|
|
21
|
+
import type { OutputCollector } from "@query-farm/vgi-rpc";
|
|
22
|
+
import type { VgiBatch } from "../arrow/index.js";
|
|
23
|
+
|
|
24
|
+
/** Metadata key carrying a table-in-out exchange status, on the batch itself. */
|
|
25
|
+
export const STATUS_KEY = "vgi.status";
|
|
26
|
+
/** Reply status: this input has more output; ask for it with a tick. */
|
|
27
|
+
export const HAVE_MORE_OUTPUT = "HAVE_MORE_OUTPUT";
|
|
28
|
+
/** Input status: a continuation tick, asking for the last input's next reply. */
|
|
29
|
+
export const CONTINUE = "CONTINUE";
|
|
30
|
+
|
|
31
|
+
const CACHE_KEY_PREFIX = "vgi.cache.";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Mark an emitted table-in-out reply as one of several for the same input
|
|
35
|
+
* batch when `more` is true: the client asks for the next one with a
|
|
36
|
+
* continuation tick, which the framework answers through the function's
|
|
37
|
+
* `resume()`. `false` adds nothing, so the last reply can pass its own
|
|
38
|
+
* condition. Composes with {@link parentRowsMetadata} and
|
|
39
|
+
* `cacheControlMetadata` via `extra`:
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* out.emit(batch, moreOutputMetadata(state.row < state.counts.length,
|
|
43
|
+
* parentRowsMetadata(parents, batch.numRows)));
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* Valid only from the `process()` or `resume()` of a table-in-out function
|
|
47
|
+
* that implements `resume()`; the framework reads it off the emitted reply.
|
|
48
|
+
*/
|
|
49
|
+
export function moreOutputMetadata(more: boolean, extra?: Map<string, string>): Map<string, string> {
|
|
50
|
+
const md = new Map<string, string>(extra ?? []);
|
|
51
|
+
if (more) md.set(STATUS_KEY, HAVE_MORE_OUTPUT);
|
|
52
|
+
return md;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The metadata the turn's data batch went out with (per-emit over batch-carried). */
|
|
56
|
+
function replyMetadata(out: OutputCollector): Map<string, string> | null {
|
|
57
|
+
const idx = out.dataBatchIdx;
|
|
58
|
+
if (idx == null) return null;
|
|
59
|
+
const emitted = out.batches[idx];
|
|
60
|
+
const md = new Map<string, string>((emitted.batch as any).metadata ?? []);
|
|
61
|
+
for (const [k, v] of emitted.metadata ?? []) md.set(k, v);
|
|
62
|
+
return md;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Refuse "more" on a stream that cannot continue: anything but a table-in-out
|
|
67
|
+
* exchange, where the client would never send the tick. Run by the dispatcher
|
|
68
|
+
* after every non-table-in-out turn.
|
|
69
|
+
*/
|
|
70
|
+
export function refuseMoreOutput(name: string, out: OutputCollector): void {
|
|
71
|
+
if (replyMetadata(out)?.get(STATUS_KEY) === HAVE_MORE_OUTPUT) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`${name}: HAVE_MORE_OUTPUT (moreOutputMetadata) is only valid in a table-in-out ` +
|
|
74
|
+
`function's process() or resume()`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The exchange state a continuable table-in-out turn reads and updates. */
|
|
80
|
+
export interface ContinuationState {
|
|
81
|
+
/**
|
|
82
|
+
* Whether the last reply said HAVE_MORE_OUTPUT, so the next input must be a
|
|
83
|
+
* tick. Carried in the HTTP state token (see protocol/state-serializer.ts).
|
|
84
|
+
*/
|
|
85
|
+
morePending?: boolean;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Run one table-in-out exchange turn under the continued-output rules.
|
|
90
|
+
*
|
|
91
|
+
* Classifies the input (a tick or real input), refuses the inputs the protocol
|
|
92
|
+
* forbids, calls `run(tick)` -- which calls `process()` for real input and
|
|
93
|
+
* `resume()` for a tick -- and records from the emitted reply whether output is
|
|
94
|
+
* now pending.
|
|
95
|
+
*
|
|
96
|
+
* `resumable` is whether the function implements `resume()`: one that does not
|
|
97
|
+
* may not say more.
|
|
98
|
+
*/
|
|
99
|
+
export async function continuationTurn(
|
|
100
|
+
name: string,
|
|
101
|
+
eState: ContinuationState,
|
|
102
|
+
input: VgiBatch,
|
|
103
|
+
out: OutputCollector,
|
|
104
|
+
resumable: boolean,
|
|
105
|
+
run: (tick: boolean) => Promise<void>,
|
|
106
|
+
): Promise<void> {
|
|
107
|
+
// The status rides the input batch's own metadata; vgi-rpc also surfaces it
|
|
108
|
+
// on the collector, so read either.
|
|
109
|
+
const inputStatus =
|
|
110
|
+
((input as any)?.metadata as Map<string, string> | undefined)?.get(STATUS_KEY) ??
|
|
111
|
+
out.inputMetadata?.get(STATUS_KEY);
|
|
112
|
+
const tick = inputStatus === CONTINUE;
|
|
113
|
+
if (tick && !eState.morePending) {
|
|
114
|
+
throw new Error(`${name}: continuation tick received, but no output is pending`);
|
|
115
|
+
}
|
|
116
|
+
if (tick && input.numRows !== 0) {
|
|
117
|
+
throw new Error(`${name}: a continuation tick must have no rows, got ${input.numRows}`);
|
|
118
|
+
}
|
|
119
|
+
if (!tick && eState.morePending) {
|
|
120
|
+
throw new Error(`${name}: input received while output is pending; expected a continuation tick`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
await run(tick);
|
|
124
|
+
|
|
125
|
+
const md = replyMetadata(out);
|
|
126
|
+
// One reply per turn: a turn that emits none leaves the client waiting on
|
|
127
|
+
// the byte-stream transports (and, over HTTP, answers with a token-only body
|
|
128
|
+
// the client cannot tell from an empty result). An empty answer is a 0-row
|
|
129
|
+
// batch. Matches vgi-rpc-python's OutputCollector.validate(). (Several data
|
|
130
|
+
// batches are already refused by vgi-rpc's OutputCollector.emit.)
|
|
131
|
+
if (md == null) {
|
|
132
|
+
throw new Error(
|
|
133
|
+
tick
|
|
134
|
+
? `${name}: resume() must emit a batch for every continuation tick`
|
|
135
|
+
: `${name}: process() must emit a batch for every input batch (No data batch was emitted)`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
const more = md.get(STATUS_KEY) === HAVE_MORE_OUTPUT;
|
|
139
|
+
if (more && !resumable) {
|
|
140
|
+
throw new Error(`${name}: emitted HAVE_MORE_OUTPUT but does not implement resume()`);
|
|
141
|
+
}
|
|
142
|
+
if (tick) {
|
|
143
|
+
// Every tick must be answered (above), and never with cache control.
|
|
144
|
+
for (const key of md.keys()) {
|
|
145
|
+
if (key.startsWith(CACHE_KEY_PREFIX)) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`${name}: cache control is only valid on an input's first reply, ` +
|
|
148
|
+
`not on one answering a continuation tick (${key})`,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
eState.morePending = more;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Refuse, at definition, a continuable function the client cannot drive.
|
|
158
|
+
*
|
|
159
|
+
* `resume()` continues from the function's state alone, so it needs an
|
|
160
|
+
* `initialState` (the state is what the HTTP token carries between turns).
|
|
161
|
+
* `maxWorkers: 1` runs the function on one connection shared by every
|
|
162
|
+
* pipeline, which cannot leave output pending while another's input waits.
|
|
163
|
+
*/
|
|
164
|
+
export function checkResumeContract(
|
|
165
|
+
name: string,
|
|
166
|
+
config: { resume?: unknown; initialState?: unknown; maxWorkers?: number },
|
|
167
|
+
): void {
|
|
168
|
+
if (config.resume == null) return;
|
|
169
|
+
if (typeof config.resume !== "function") {
|
|
170
|
+
throw new Error(`${name}: resume must be a function`);
|
|
171
|
+
}
|
|
172
|
+
if (config.maxWorkers === 1) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`${name}: resume() (continued output) is not supported with maxWorkers: 1; ` +
|
|
175
|
+
`the client runs such a function on one shared connection.`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
if (config.initialState == null) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
`${name}: resume() continues from its state alone, so the function must ` +
|
|
181
|
+
`declare an initialState holding where to resume.`,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Check `inputRowsPerCall`: a positive integer, on a blended row-transform
|
|
188
|
+
* function only (the extension refuses it at bind too).
|
|
189
|
+
*/
|
|
190
|
+
export function checkInputRowsPerCall(name: string, value: unknown, blended: boolean): number | undefined {
|
|
191
|
+
if (value == null) return undefined;
|
|
192
|
+
if (!blended) {
|
|
193
|
+
throw new Error(
|
|
194
|
+
`${name}: inputRowsPerCall applies only to a blended row-transform function, ` +
|
|
195
|
+
`whose per-row input the client can split into calls`,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 1) {
|
|
199
|
+
throw new Error(`${name}: inputRowsPerCall must be a positive integer, got ${String(value)}`);
|
|
200
|
+
}
|
|
201
|
+
return value;
|
|
202
|
+
}
|
|
@@ -43,6 +43,8 @@ import {
|
|
|
43
43
|
import { FunctionStability } from "../types.js";
|
|
44
44
|
import { BoundStorage, storage as defaultStorage, FrameworkNS } from "./storage.js";
|
|
45
45
|
import { serializeUserState, deserializeUserState } from "../protocol/state-serializer.js";
|
|
46
|
+
import { PARENT_ROW_METADATA_KEY, encodeParentRows } from "../util/parent-rows.js";
|
|
47
|
+
import { checkInputRowsPerCall, checkResumeContract, continuationTurn } from "./continuation.js";
|
|
46
48
|
|
|
47
49
|
/**
|
|
48
50
|
* State-log key the streaming FINALIZE drain uses within
|
|
@@ -140,6 +142,26 @@ export interface TableInOutConfig<
|
|
|
140
142
|
batch: VgiBatch,
|
|
141
143
|
out: OutputCollector
|
|
142
144
|
) => void | Promise<void>;
|
|
145
|
+
/**
|
|
146
|
+
* Resume: emit the next reply for the last input batch (continued output).
|
|
147
|
+
*
|
|
148
|
+
* One input batch can produce several replies: `process()` emits the first
|
|
149
|
+
* with {@link moreOutputMetadata}`(true)`, and the framework then calls
|
|
150
|
+
* `resume()`, with no input, for each next reply until one is emitted
|
|
151
|
+
* without it. Like `process()`, each call emits exactly one batch.
|
|
152
|
+
*
|
|
153
|
+
* `resume()` continues from `state` alone, so a function that defines it
|
|
154
|
+
* must define `initialState`. Mutate the state in place: it survives between
|
|
155
|
+
* calls on every transport -- over HTTP inside the stream's state token -- so
|
|
156
|
+
* keep it small (positions to resume from, never buffered output). Not
|
|
157
|
+
* allowed with `maxWorkers: 1`, which runs on one connection shared by every
|
|
158
|
+
* pipeline and so cannot leave output pending.
|
|
159
|
+
*/
|
|
160
|
+
resume?: (
|
|
161
|
+
params: TableInOutProcessParams<TArgs>,
|
|
162
|
+
state: TState,
|
|
163
|
+
out: OutputCollector,
|
|
164
|
+
) => void | Promise<void>;
|
|
143
165
|
/** Finalize: emit final batches after all input processed.
|
|
144
166
|
* Receives all worker states collected from storage (matches Python's finish(params, states)). */
|
|
145
167
|
finalize?: (
|
|
@@ -164,7 +186,9 @@ export function defineTableInOutFunction<
|
|
|
164
186
|
TState = null,
|
|
165
187
|
>(config: TableInOutConfig<TArgs, TState>): VgiFunction {
|
|
166
188
|
assertArrowTypes((config as any).args, `defineTableInOutFunction("${config.name}"): args`);
|
|
167
|
-
|
|
189
|
+
checkResumeContract(config.name, config);
|
|
190
|
+
// A classic (TABLE-input) function cannot split its input into calls.
|
|
191
|
+
checkInputRowsPerCall(config.name, (config as any).inputRowsPerCall, false);
|
|
168
192
|
|
|
169
193
|
// Build argument specs
|
|
170
194
|
const specs: ArgumentSpec[] = [];
|
|
@@ -485,37 +509,52 @@ export function defineTableInOutFunction<
|
|
|
485
509
|
return {
|
|
486
510
|
outputSchema,
|
|
487
511
|
inputSchema: request.bind_call.input_schema,
|
|
488
|
-
|
|
512
|
+
continuesOutput: true,
|
|
513
|
+
exchangeInit: () => ({ state, processParams, filterHistory: carriedFilterHistory, morePending: false }),
|
|
489
514
|
exchangeFn: async (
|
|
490
515
|
eState: {
|
|
491
516
|
state: TState;
|
|
492
517
|
processParams: TableInOutProcessParams<TArgs>;
|
|
493
518
|
filterHistory: FilterDeltaHistory;
|
|
519
|
+
morePending: boolean;
|
|
494
520
|
},
|
|
495
521
|
input: VgiBatch,
|
|
496
522
|
out: OutputCollector
|
|
497
523
|
) => {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
524
|
+
await continuationTurn(config.name, eState, input, out, config.resume != null, async (tick) => {
|
|
525
|
+
// Surface conditional-revalidation validators off the input batch's
|
|
526
|
+
// custom metadata (attached by the C++ WriteInputBatch), so process()
|
|
527
|
+
// can answer a 0-row `notModified` batch instead of recomputing. A
|
|
528
|
+
// tick never carries them, so this also withdraws the last input's
|
|
529
|
+
// from resume().
|
|
530
|
+
applyRevalidationValidators(eState.processParams, input);
|
|
531
|
+
applyDynamicFilterUpdate(eState, input);
|
|
532
|
+
// Bake emit metadata onto the batch (0-row HTTP survival), then
|
|
533
|
+
// reconcile emitted batches to the (possibly projected) output schema
|
|
534
|
+
// by name — a process() may emit its full declared schema and let the
|
|
535
|
+
// framework project (mirrors Python's OutputCollector.emit reconcile).
|
|
536
|
+
let wrappedOut: OutputCollector = makeMetadataBakingCollector(out);
|
|
537
|
+
if (projIds) {
|
|
538
|
+
wrappedOut = makeSchemaReconcilingCollector(wrappedOut, outputSchema);
|
|
539
|
+
}
|
|
540
|
+
const currentFilters = eState.processParams.pushdownFilters;
|
|
541
|
+
if (config.autoApplyFilters && currentFilters) {
|
|
542
|
+
wrappedOut = new FilteringOutputCollector(wrappedOut, currentFilters) as unknown as OutputCollector;
|
|
543
|
+
}
|
|
544
|
+
if (tick) {
|
|
545
|
+
await config.resume!(eState.processParams, eState.state, wrappedOut);
|
|
546
|
+
} else {
|
|
547
|
+
await processFn(eState.processParams, eState.state, input, wrappedOut);
|
|
548
|
+
}
|
|
549
|
+
});
|
|
516
550
|
|
|
517
|
-
// Auto-persist accumulated state for FINALIZE recovery (matches
|
|
518
|
-
|
|
551
|
+
// Auto-persist accumulated state for FINALIZE recovery (matches
|
|
552
|
+
// Python/Go pattern) -- every turn, ticks included, since resume()
|
|
553
|
+
// advances the state finalize() must see. Only the FINALIZE phase
|
|
554
|
+
// reads it back, and only finalize() uses what it reads: a function
|
|
555
|
+
// without one advertises has_finalize=false, so nothing would ever
|
|
556
|
+
// collect the write.
|
|
557
|
+
if (config.finalize && eState.state != null) {
|
|
519
558
|
const serialized = serializeUserState(eState.state);
|
|
520
559
|
if (serialized != null) {
|
|
521
560
|
await boundStorage.put(serialized);
|
|
@@ -661,10 +700,7 @@ function makeSchemaReconcilingCollector(
|
|
|
661
700
|
// Per-output-row provenance (batched correlated LATERAL)
|
|
662
701
|
// ============================================================================
|
|
663
702
|
|
|
664
|
-
|
|
665
|
-
* little-endian int32[] mapping each output row to the input row that
|
|
666
|
-
* produced it. Shared by string with the C++ extension and vgi-python. */
|
|
667
|
-
export const PARENT_ROW_METADATA_KEY = "vgi_rpc.parent_row#b64";
|
|
703
|
+
export { PARENT_ROW_METADATA_KEY };
|
|
668
704
|
|
|
669
705
|
/**
|
|
670
706
|
* Fold per-output-row provenance into an emit metadata map.
|
|
@@ -703,24 +739,10 @@ export function parentRowsMetadata(
|
|
|
703
739
|
// Nothing to map; skip the base64+pack for an empty emit.
|
|
704
740
|
return md;
|
|
705
741
|
}
|
|
706
|
-
|
|
707
|
-
const dv = new DataView(raw.buffer);
|
|
708
|
-
for (let i = 0; i < parentRows.length; i++) {
|
|
709
|
-
dv.setInt32(i * 4, parentRows[i], true);
|
|
710
|
-
}
|
|
711
|
-
md.set(PARENT_ROW_METADATA_KEY, base64Encode(raw));
|
|
742
|
+
md.set(PARENT_ROW_METADATA_KEY, encodeParentRows(parentRows));
|
|
712
743
|
return md;
|
|
713
744
|
}
|
|
714
745
|
|
|
715
|
-
function base64Encode(bytes: Uint8Array): string {
|
|
716
|
-
if (typeof (globalThis as any).Buffer !== "undefined") {
|
|
717
|
-
return (globalThis as any).Buffer.from(bytes).toString("base64");
|
|
718
|
-
}
|
|
719
|
-
let bin = "";
|
|
720
|
-
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
|
|
721
|
-
return (globalThis as any).btoa(bin);
|
|
722
|
-
}
|
|
723
|
-
|
|
724
746
|
// ============================================================================
|
|
725
747
|
// Blended ("UNNEST-style") row-transform functions
|
|
726
748
|
// ============================================================================
|
|
@@ -734,7 +756,7 @@ function base64Encode(bytes: Uint8Array): string {
|
|
|
734
756
|
export type RowTransformProcessParams<TArgs = Record<string, any>> =
|
|
735
757
|
TableInOutProcessParams<TArgs>;
|
|
736
758
|
|
|
737
|
-
export interface RowTransformConfig<TArgs = Record<string, any
|
|
759
|
+
export interface RowTransformConfig<TArgs = Record<string, any>, TState = null> {
|
|
738
760
|
name: string;
|
|
739
761
|
description?: string;
|
|
740
762
|
/**
|
|
@@ -774,12 +796,47 @@ export interface RowTransformConfig<TArgs = Record<string, any>> {
|
|
|
774
796
|
* per-row map (DuckDB forbids FinalExecute under correlated LATERAL, one of
|
|
775
797
|
* the call shapes blended must serve). Accumulating functions use a classic
|
|
776
798
|
* TableInput table-in-out or a TableBufferingFunction.
|
|
799
|
+
*
|
|
800
|
+
* `state` is the stream's state from {@link initialState} (`null` without
|
|
801
|
+
* one); a function that continues its output keeps its resume position there.
|
|
777
802
|
*/
|
|
778
803
|
process: (
|
|
779
804
|
params: RowTransformProcessParams<TArgs>,
|
|
780
805
|
batch: VgiBatch,
|
|
781
806
|
out: OutputCollector,
|
|
807
|
+
state: TState,
|
|
808
|
+
) => void | Promise<void>;
|
|
809
|
+
/**
|
|
810
|
+
* The stream's initial state, needed only by a function with {@link resume}:
|
|
811
|
+
* where the next reply resumes. Mutate it in place; it survives between
|
|
812
|
+
* calls on every transport (over HTTP inside the stream's state token), so
|
|
813
|
+
* keep it small: positions to resume from, never buffered output rows.
|
|
814
|
+
*/
|
|
815
|
+
initialState?: (params: RowTransformProcessParams<TArgs>) => TState;
|
|
816
|
+
/**
|
|
817
|
+
* Continued output: emit the next reply for the last input batch.
|
|
818
|
+
*
|
|
819
|
+
* Large output for one input batch (an upstream API paged across many
|
|
820
|
+
* requests) need not be built into one batch: `process()` emits the first
|
|
821
|
+
* reply with {@link moreOutputMetadata}`(true)`, and the framework calls
|
|
822
|
+
* `resume()`, with no input, for each next reply until one is emitted
|
|
823
|
+
* without it. Each call emits exactly one batch, whose parent rows index the
|
|
824
|
+
* input batch `process()` received. Requires {@link initialState}; not
|
|
825
|
+
* allowed with `maxWorkers: 1`.
|
|
826
|
+
*/
|
|
827
|
+
resume?: (
|
|
828
|
+
params: RowTransformProcessParams<TArgs>,
|
|
829
|
+
state: TState,
|
|
830
|
+
out: OutputCollector,
|
|
782
831
|
) => void | Promise<void>;
|
|
832
|
+
/**
|
|
833
|
+
* The most input rows one worker call receives: the client's batched-LATERAL
|
|
834
|
+
* operator splits each input chunk (after dedup) into calls of at most this
|
|
835
|
+
* many rows, in order. Unset sends a whole chunk per call. A positive
|
|
836
|
+
* integer. Surfaces as `FunctionInfo.input_rows_per_call`; mirrors
|
|
837
|
+
* vgi-python's `Meta.input_rows_per_call`.
|
|
838
|
+
*/
|
|
839
|
+
inputRowsPerCall?: number;
|
|
783
840
|
// Metadata
|
|
784
841
|
projectionPushdown?: boolean;
|
|
785
842
|
filterPushdown?: boolean;
|
|
@@ -806,7 +863,8 @@ export interface RowTransformConfig<TArgs = Record<string, any>> {
|
|
|
806
863
|
*/
|
|
807
864
|
export function defineRowTransformFunction<
|
|
808
865
|
TArgs = Record<string, any>,
|
|
809
|
-
|
|
866
|
+
TState = null,
|
|
867
|
+
>(config: RowTransformConfig<TArgs, TState>): VgiFunction {
|
|
810
868
|
// Foot-gun guard (mirrors Python's resolve_metadata check): a blended
|
|
811
869
|
// function needs at least one positional Arg — its per-row input column.
|
|
812
870
|
const numPositional = Object.keys(config.args ?? {}).length + (config.varargs ? 1 : 0);
|
|
@@ -816,6 +874,8 @@ export function defineRowTransformFunction<
|
|
|
816
874
|
`positional arg (its per-row input column); found none.`,
|
|
817
875
|
);
|
|
818
876
|
}
|
|
877
|
+
checkResumeContract(config.name, config);
|
|
878
|
+
const inputRowsPerCall = checkInputRowsPerCall(config.name, config.inputRowsPerCall, true);
|
|
819
879
|
|
|
820
880
|
// Positional args are the per-row input columns — REAL typed args on the
|
|
821
881
|
// wire (no TableInput placeholder). Named args follow.
|
|
@@ -884,6 +944,7 @@ export function defineRowTransformFunction<
|
|
|
884
944
|
// Map-shaped, per-row: a blended function never has a finalize.
|
|
885
945
|
hasFinalize: false,
|
|
886
946
|
inputFromArgs: true,
|
|
947
|
+
inputRowsPerCall,
|
|
887
948
|
};
|
|
888
949
|
|
|
889
950
|
// Only NAMED args are on the wire — positional args are the input columns,
|
|
@@ -1018,30 +1079,44 @@ export function defineRowTransformFunction<
|
|
|
1018
1079
|
);
|
|
1019
1080
|
}
|
|
1020
1081
|
|
|
1082
|
+
const state = config.initialState
|
|
1083
|
+
? config.initialState(processParams)
|
|
1084
|
+
: (null as TState);
|
|
1085
|
+
|
|
1021
1086
|
return {
|
|
1022
1087
|
outputSchema,
|
|
1023
1088
|
inputSchema: request.bind_call.input_schema,
|
|
1024
|
-
|
|
1089
|
+
continuesOutput: true,
|
|
1090
|
+
exchangeInit: () => ({ state, processParams, filterHistory: carriedFilterHistory, morePending: false }),
|
|
1025
1091
|
exchangeFn: async (
|
|
1026
1092
|
eState: {
|
|
1027
|
-
state:
|
|
1093
|
+
state: TState;
|
|
1028
1094
|
processParams: RowTransformProcessParams<TArgs>;
|
|
1029
1095
|
filterHistory: FilterDeltaHistory;
|
|
1096
|
+
morePending: boolean;
|
|
1030
1097
|
},
|
|
1031
1098
|
input: VgiBatch,
|
|
1032
1099
|
out: OutputCollector,
|
|
1033
1100
|
) => {
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1101
|
+
await continuationTurn(config.name, eState, input, out, config.resume != null, async (tick) => {
|
|
1102
|
+
// A tick carries no validators, so this also withdraws the last
|
|
1103
|
+
// input's from resume().
|
|
1104
|
+
applyRevalidationValidators(eState.processParams, input);
|
|
1105
|
+
applyDynamicFilterUpdate(eState, input);
|
|
1106
|
+
let wrappedOut: OutputCollector = makeMetadataBakingCollector(out);
|
|
1107
|
+
if (projIds) {
|
|
1108
|
+
wrappedOut = makeSchemaReconcilingCollector(wrappedOut, outputSchema);
|
|
1109
|
+
}
|
|
1110
|
+
const currentFilters = eState.processParams.pushdownFilters;
|
|
1111
|
+
if (config.autoApplyFilters && currentFilters) {
|
|
1112
|
+
wrappedOut = new FilteringOutputCollector(wrappedOut, currentFilters) as unknown as OutputCollector;
|
|
1113
|
+
}
|
|
1114
|
+
if (tick) {
|
|
1115
|
+
await config.resume!(eState.processParams, eState.state, wrappedOut);
|
|
1116
|
+
} else {
|
|
1117
|
+
await config.process(eState.processParams, input, wrappedOut, eState.state);
|
|
1118
|
+
}
|
|
1119
|
+
});
|
|
1045
1120
|
},
|
|
1046
1121
|
};
|
|
1047
1122
|
},
|
package/src/functions/types.ts
CHANGED
|
@@ -32,6 +32,12 @@ export interface HandlerState<T = any> {
|
|
|
32
32
|
* rebuilds the filters this one left.
|
|
33
33
|
*/
|
|
34
34
|
filterHistory?: import("../filter-pushdown/history.js").FilterDeltaHistory;
|
|
35
|
+
/**
|
|
36
|
+
* A table-in-out exchange's continued-output flag: the last reply said
|
|
37
|
+
* HAVE_MORE_OUTPUT, so the next input must be a continuation tick. Carried
|
|
38
|
+
* in the HTTP cursor like `filterHistory`.
|
|
39
|
+
*/
|
|
40
|
+
morePending?: boolean;
|
|
35
41
|
[key: string]: any;
|
|
36
42
|
}
|
|
37
43
|
|
|
@@ -109,6 +115,13 @@ export interface FunctionMeta {
|
|
|
109
115
|
* ResolvedMetadata.input_from_args.
|
|
110
116
|
*/
|
|
111
117
|
inputFromArgs?: boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Blended only: the most input rows one worker call receives; the client
|
|
120
|
+
* splits each input chunk into calls of at most this many rows. Unset sends
|
|
121
|
+
* a whole chunk per call. Set by {@link defineRowTransformFunction}'s
|
|
122
|
+
* `inputRowsPerCall`; surfaces as FunctionInfo.input_rows_per_call.
|
|
123
|
+
*/
|
|
124
|
+
inputRowsPerCall?: number;
|
|
112
125
|
/**
|
|
113
126
|
* table_buffering: force ParallelSink=false in the C++ operator
|
|
114
127
|
* (single-thread, source-ordered ingest).
|
|
@@ -222,6 +235,13 @@ export interface StreamHandlers {
|
|
|
222
235
|
* from DuckDB's Top-N optimizer).
|
|
223
236
|
*/
|
|
224
237
|
onTick?: (state: any, tickMetadata: Map<string, string> | undefined) => void | Promise<void>;
|
|
238
|
+
/**
|
|
239
|
+
* A table-in-out exchange, which may continue one input's output across
|
|
240
|
+
* replies (see functions/continuation.ts). It keeps `morePending` on its
|
|
241
|
+
* handler state, which the dispatcher carries in the HTTP state token. Every
|
|
242
|
+
* other stream's reply is refused if it says HAVE_MORE_OUTPUT.
|
|
243
|
+
*/
|
|
244
|
+
continuesOutput?: boolean;
|
|
225
245
|
}
|
|
226
246
|
|
|
227
247
|
/**
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// GENERATED by vgi.codegen.ts_client. DO NOT EDIT BY HAND.
|
|
3
3
|
//
|
|
4
4
|
// Generator: vgi-gen-ts-client v1
|
|
5
|
-
// Content hash:
|
|
5
|
+
// Content hash: 7a9035b498e5
|
|
6
6
|
//
|
|
7
7
|
// To regenerate:
|
|
8
8
|
// uv run --project ~/Development/vgi-python vgi-gen-ts-client \
|
|
@@ -56,7 +56,6 @@ export interface CatalogInfo {
|
|
|
56
56
|
export interface SchemaInfo {
|
|
57
57
|
comment?: string | null;
|
|
58
58
|
tags: Record<string, string>;
|
|
59
|
-
attach_opaque_data: Uint8Array;
|
|
60
59
|
path: string[];
|
|
61
60
|
estimated_object_count?: Record<string, number> | null;
|
|
62
61
|
}
|
|
@@ -163,6 +162,7 @@ export interface FunctionInfo {
|
|
|
163
162
|
sink_order_dependent: boolean;
|
|
164
163
|
requires_input_batch_index: boolean;
|
|
165
164
|
input_from_args: boolean;
|
|
165
|
+
input_rows_per_call?: number | null;
|
|
166
166
|
required_settings: string[];
|
|
167
167
|
required_secrets: SecretLookupEntry[];
|
|
168
168
|
}
|
|
@@ -430,24 +430,16 @@ export interface CatalogsResponse {
|
|
|
430
430
|
items: CatalogInfo[];
|
|
431
431
|
}
|
|
432
432
|
|
|
433
|
-
export interface SchemaContents {
|
|
434
|
-
path: string[];
|
|
435
|
-
schema: Uint8Array;
|
|
436
|
-
tables: Uint8Array[];
|
|
437
|
-
views: Uint8Array[];
|
|
438
|
-
scalar_functions: Uint8Array[];
|
|
439
|
-
aggregate_functions: Uint8Array[];
|
|
440
|
-
table_functions: Uint8Array[];
|
|
441
|
-
scalar_macros: Uint8Array[];
|
|
442
|
-
table_macros: Uint8Array[];
|
|
443
|
-
indexes: Uint8Array[];
|
|
444
|
-
}
|
|
445
|
-
|
|
446
433
|
export interface CatalogContentsResponse {
|
|
447
434
|
catalog_version: number;
|
|
448
435
|
etag?: string | null;
|
|
449
436
|
not_modified: boolean;
|
|
450
|
-
schemas
|
|
437
|
+
schemas?: Uint8Array | null;
|
|
438
|
+
tables?: Uint8Array | null;
|
|
439
|
+
views?: Uint8Array | null;
|
|
440
|
+
functions?: Uint8Array | null;
|
|
441
|
+
macros?: Uint8Array | null;
|
|
442
|
+
indexes?: Uint8Array | null;
|
|
451
443
|
}
|
|
452
444
|
|
|
453
445
|
export interface CopyFromFormatsResponse {
|