@query-farm/vgi 0.45.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-flechette/canonical.d.ts +1 -1
- package/dist/arrow/impl-flechette/schema.d.ts +19 -5
- package/dist/client/client.d.ts +20 -1
- package/dist/client-entry.js +54 -13
- package/dist/client-entry.js.map +8 -6
- 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 +1 -0
- package/dist/generated/vgi-protocol-schemas.d.ts +1 -1
- package/dist/index.core.d.ts +1 -0
- package/dist/index.js +244 -59
- package/dist/index.js.map +14 -12
- package/dist/serve-entry.js +35 -5
- package/dist/serve-entry.js.map +8 -7
- package/dist/util/parent-rows.d.ts +8 -0
- package/dist/worker-cf-entry.d.ts +1 -1
- package/dist/worker-cf-entry.js +197 -47
- package/dist/worker-cf-entry.js.map +14 -12
- package/package.json +4 -4
- package/src/arrow/impl-flechette/canonical.ts +7 -5
- package/src/catalog/read-only.ts +6 -0
- package/src/client/client.ts +80 -11
- 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 +2 -1
- package/src/generated/vgi-protocol-schemas.ts +3 -2
- package/src/index.core.ts +6 -0
- 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
|
@@ -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 \
|
|
@@ -162,6 +162,7 @@ export interface FunctionInfo {
|
|
|
162
162
|
sink_order_dependent: boolean;
|
|
163
163
|
requires_input_batch_index: boolean;
|
|
164
164
|
input_from_args: boolean;
|
|
165
|
+
input_rows_per_call?: number | null;
|
|
165
166
|
required_settings: string[];
|
|
166
167
|
required_secrets: SecretLookupEntry[];
|
|
167
168
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// GENERATED by vgi.codegen.ts_schemas. DO NOT EDIT BY HAND.
|
|
3
3
|
//
|
|
4
4
|
// Generator: vgi-gen-ts-schemas v2
|
|
5
|
-
// Content hash:
|
|
5
|
+
// Content hash: 31082ec470b8
|
|
6
6
|
//
|
|
7
7
|
// To regenerate:
|
|
8
8
|
// uv run --project ~/Development/vgi-python vgi-gen-ts-schemas \
|
|
@@ -30,7 +30,7 @@ import {
|
|
|
30
30
|
} from "../arrow/index.js";
|
|
31
31
|
|
|
32
32
|
/** Canonical fingerprint of the nested catalog wire schemas. */
|
|
33
|
-
export const VGI_CATALOG_CONTRACT_HASH = "
|
|
33
|
+
export const VGI_CATALOG_CONTRACT_HASH = "0aa39285365f5f4326ce85c4d41d2705ea992dee41cf98d07640a2767b6ac3fb";
|
|
34
34
|
|
|
35
35
|
// Origin: CatalogInfo
|
|
36
36
|
export const CatalogInfoSchema = schema([
|
|
@@ -126,6 +126,7 @@ export const FunctionInfoSchema = schema([
|
|
|
126
126
|
field("sink_order_dependent", bool(), false),
|
|
127
127
|
field("requires_input_batch_index", bool(), false),
|
|
128
128
|
field("input_from_args", bool(), false),
|
|
129
|
+
field("input_rows_per_call", int64(), true),
|
|
129
130
|
field("required_settings", list(field("item", utf8(), true)), false),
|
|
130
131
|
field("required_secrets", list(field("item", struct([field("secret_type", utf8(), false), field("scope", utf8(), true), field("secret_name", utf8(), true)]), true)), false),
|
|
131
132
|
]);
|
package/src/index.core.ts
CHANGED
|
@@ -52,6 +52,12 @@ export {
|
|
|
52
52
|
type RowTransformConfig,
|
|
53
53
|
type RowTransformProcessParams,
|
|
54
54
|
} from "./functions/table-in-out.js";
|
|
55
|
+
export {
|
|
56
|
+
moreOutputMetadata,
|
|
57
|
+
STATUS_KEY,
|
|
58
|
+
HAVE_MORE_OUTPUT,
|
|
59
|
+
CONTINUE,
|
|
60
|
+
} from "./functions/continuation.js";
|
|
55
61
|
export {
|
|
56
62
|
defineTableBufferingFunction,
|
|
57
63
|
type TableBufferingConfig,
|
|
@@ -8,6 +8,7 @@ import type { AuthContext } from "@query-farm/vgi-rpc";
|
|
|
8
8
|
import { exchangeStream, type VgiService } from "../../generated/vgi-service.js";
|
|
9
9
|
import type { FunctionRegistry } from "../../functions/registry.js";
|
|
10
10
|
import type { StreamHandlers, HandlerState } from "../../functions/types.js";
|
|
11
|
+
import { refuseMoreOutput } from "../../functions/continuation.js";
|
|
11
12
|
import {
|
|
12
13
|
deserializeBindRequest,
|
|
13
14
|
serializeBindResponse,
|
|
@@ -410,6 +411,11 @@ export function functionHandlers(config: FunctionHandlerConfig): Partial<VgiServ
|
|
|
410
411
|
if (state.userState != null && handlerState?.state !== undefined) {
|
|
411
412
|
handlerState!.state = state.userState;
|
|
412
413
|
}
|
|
414
|
+
// Whether the last reply said more, so only a continuation tick is
|
|
415
|
+
// valid now (functions/continuation.ts).
|
|
416
|
+
if (handlers.continuesOutput) {
|
|
417
|
+
handlerState!.morePending = state.morePending === true;
|
|
418
|
+
}
|
|
413
419
|
}
|
|
414
420
|
|
|
415
421
|
if (state.isProducer && handlers.producerFn) {
|
|
@@ -427,6 +433,11 @@ export function functionHandlers(config: FunctionHandlerConfig): Partial<VgiServ
|
|
|
427
433
|
} else if (handlers.exchangeFn) {
|
|
428
434
|
await handlers.exchangeFn(handlerState, input, out);
|
|
429
435
|
}
|
|
436
|
+
// Only a table-in-out exchange can continue its output; anywhere else
|
|
437
|
+
// the client would never send the tick.
|
|
438
|
+
if (!handlers.continuesOutput) {
|
|
439
|
+
refuseMoreOutput(state.functionName, out);
|
|
440
|
+
}
|
|
430
441
|
|
|
431
442
|
// Save mutated user state for the next exchange round
|
|
432
443
|
if (handlerState?.state !== undefined) {
|
|
@@ -435,6 +446,7 @@ export function functionHandlers(config: FunctionHandlerConfig): Partial<VgiServ
|
|
|
435
446
|
// And the dynamic-filter history this turn left (compacted: bounded by
|
|
436
447
|
// the number of predicate ids, not the number of turns).
|
|
437
448
|
state.filterHistory = handlerState?.filterHistory ?? null;
|
|
449
|
+
state.morePending = handlerState?.morePending === true;
|
|
438
450
|
},
|
|
439
451
|
headerInit: (params: any, state: any, ctx: any) => {
|
|
440
452
|
// During init, _initResponse is still in memory.
|
|
@@ -50,6 +50,9 @@ export const EXCHANGE_STATE_SCHEMA = makeSchema([
|
|
|
50
50
|
// null when no delta has been applied. Bounded by the number of predicate
|
|
51
51
|
// ids, so the token stays flat-sized however many turns the scan takes.
|
|
52
52
|
field("filter_history", binary(), true),
|
|
53
|
+
// A table-in-out exchange's last reply said HAVE_MORE_OUTPUT, so the next
|
|
54
|
+
// input must be a continuation tick (functions/continuation.ts). A flag bit.
|
|
55
|
+
field("more_pending", bool(), false),
|
|
53
56
|
]);
|
|
54
57
|
|
|
55
58
|
const TEXT_ENCODER = new TextEncoder();
|
|
@@ -61,6 +64,7 @@ const FLAG_PRODUCER = 1;
|
|
|
61
64
|
const FLAG_OPAQUE_DATA = 2;
|
|
62
65
|
const FLAG_USER_STATE = 4;
|
|
63
66
|
const FLAG_FILTER_HISTORY = 8;
|
|
67
|
+
const FLAG_MORE_PENDING = 16;
|
|
64
68
|
|
|
65
69
|
/**
|
|
66
70
|
* Child field name for an inferred list. Arrow matches list children by
|
|
@@ -243,7 +247,8 @@ export const arrowStateSerializer: StateSerializer = {
|
|
|
243
247
|
(state.isProducer ? FLAG_PRODUCER : 0) |
|
|
244
248
|
(opaqueData ? FLAG_OPAQUE_DATA : 0) |
|
|
245
249
|
(userState ? FLAG_USER_STATE : 0) |
|
|
246
|
-
(filterHistory ? FLAG_FILTER_HISTORY : 0)
|
|
250
|
+
(filterHistory ? FLAG_FILTER_HISTORY : 0) |
|
|
251
|
+
(state.morePending ? FLAG_MORE_PENDING : 0);
|
|
247
252
|
const chunks = [functionName, initRequest, executionId];
|
|
248
253
|
if (opaqueData) chunks.push(opaqueData);
|
|
249
254
|
if (userState) chunks.push(userState);
|
|
@@ -302,6 +307,7 @@ export const arrowStateSerializer: StateSerializer = {
|
|
|
302
307
|
__isProducer: isProducer,
|
|
303
308
|
userState: deserializeUserState(userStateBytes),
|
|
304
309
|
filterHistory: decodeFilterHistory(filterHistoryBytes),
|
|
310
|
+
morePending: (flags & FLAG_MORE_PENDING) !== 0,
|
|
305
311
|
};
|
|
306
312
|
},
|
|
307
313
|
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Copyright 2025, 2026 Query Farm LLC - https://query.farm
|
|
2
|
+
// Per-output-row provenance wire encoding (batched correlated LATERAL): base64
|
|
3
|
+
// of a raw little-endian int32[] under `vgi_rpc.parent_row#b64`. Shared by the
|
|
4
|
+
// emit helper (functions/table-in-out.ts) and the auto-apply filter
|
|
5
|
+
// (filter-pushdown/collector.ts), which must drop the same rows from both.
|
|
6
|
+
|
|
7
|
+
/** Metadata key carrying per-output-row provenance: base64 of a raw
|
|
8
|
+
* little-endian int32[] mapping each output row to the input row that
|
|
9
|
+
* produced it. Shared by string with the C++ extension and vgi-python. */
|
|
10
|
+
export const PARENT_ROW_METADATA_KEY = "vgi_rpc.parent_row#b64";
|
|
11
|
+
|
|
12
|
+
/** Encode parent-row indices as the wire's base64 little-endian int32[]. */
|
|
13
|
+
export function encodeParentRows(parentRows: ArrayLike<number>): string {
|
|
14
|
+
const raw = new Uint8Array(parentRows.length * 4);
|
|
15
|
+
const dv = new DataView(raw.buffer);
|
|
16
|
+
for (let i = 0; i < parentRows.length; i++) {
|
|
17
|
+
dv.setInt32(i * 4, parentRows[i], true);
|
|
18
|
+
}
|
|
19
|
+
if (typeof (globalThis as any).Buffer !== "undefined") {
|
|
20
|
+
return (globalThis as any).Buffer.from(raw).toString("base64");
|
|
21
|
+
}
|
|
22
|
+
let bin = "";
|
|
23
|
+
for (let i = 0; i < raw.length; i++) bin += String.fromCharCode(raw[i]);
|
|
24
|
+
return (globalThis as any).btoa(bin);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Decode the wire's base64 little-endian int32[] back into parent-row indices. */
|
|
28
|
+
export function decodeParentRows(encoded: string): Int32Array {
|
|
29
|
+
let raw: Uint8Array;
|
|
30
|
+
if (typeof (globalThis as any).Buffer !== "undefined") {
|
|
31
|
+
raw = new Uint8Array((globalThis as any).Buffer.from(encoded, "base64"));
|
|
32
|
+
} else {
|
|
33
|
+
const bin = (globalThis as any).atob(encoded) as string;
|
|
34
|
+
raw = new Uint8Array(bin.length);
|
|
35
|
+
for (let i = 0; i < bin.length; i++) raw[i] = bin.charCodeAt(i);
|
|
36
|
+
}
|
|
37
|
+
const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
|
|
38
|
+
const out = new Int32Array(raw.byteLength >> 2);
|
|
39
|
+
for (let i = 0; i < out.length; i++) out[i] = dv.getInt32(i * 4, true);
|
|
40
|
+
return out;
|
|
41
|
+
}
|
package/src/worker-cf-entry.ts
CHANGED
|
@@ -42,6 +42,9 @@ export {
|
|
|
42
42
|
defineRowTransformFunction,
|
|
43
43
|
parentRowsMetadata,
|
|
44
44
|
PARENT_ROW_METADATA_KEY,
|
|
45
|
+
// Continued output: a table-in-out reply that says more follow, answered
|
|
46
|
+
// through the function's resume(). Composes with parentRowsMetadata.
|
|
47
|
+
moreOutputMetadata,
|
|
45
48
|
// Encodes ArgumentConstraints into the discovery-facing ArgumentSpec fields.
|
|
46
49
|
// The blended config has no `argConstraints` of its own, so a worker that
|
|
47
50
|
// wants ge/le/choices/pattern on a row-transform's args applies them itself.
|