@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 CHANGED
@@ -226,7 +226,14 @@ Five factories, each mapping to a DuckDB function shape with its own lifecycle:
226
226
  - **`defineTableInOutFunction`** — table-in / table-out
227
227
  (`SELECT * FROM f(TABLE t, …)`). Per-partition `state`, a streaming `process` per
228
228
  input batch, and a `finalize` that sees every worker's state — for windowing,
229
- reshaping, enrichment, sessionization.
229
+ reshaping, enrichment, sessionization. `defineRowTransformFunction` is its
230
+ blended form, whose positional args are per-row input columns
231
+ (`FROM t, f(t.x)`); `inputRowsPerCall` caps the input rows one call receives.
232
+ Either can answer one input with several replies (an upstream API read a page
233
+ at a time): emit each reply but the last with `moreOutputMetadata(true)`, and
234
+ implement `resume(params, state, out)`, which the framework calls for each
235
+ next reply. It continues from `initialState`'s state alone, which over HTTP
236
+ rides the stream's state token, so keep positions there, not rows.
230
237
  - **`defineTableBufferingFunction`** — table-in / table-out that must observe **all**
231
238
  input before producing output (global sort, top-N, pivot). Sink (`process`) →
232
239
  `combine` → source (`finalize`), with explicit sink/source ordering controls.
@@ -9,7 +9,7 @@ export declare function writeCanonicalColumn(type: VgiDataType, canonical: unkno
9
9
  export declare function writeCanonicalBatch(columns: Record<string, unknown[]>, schema: VgiSchema, numRows: number, maxBatchRows?: number): VgiBatch;
10
10
  /**
11
11
  * Convert canonical values into the shape flechette's columnFromArray needs.
12
- * Date columns: canonical day-number / ms-bigint -> JS Date. Map columns:
12
+ * Date32 columns: canonical day-number -> JS Date; Date64 stays ms-bigint. Map columns:
13
13
  * canonical Array<[k,v]> -> Map. Struct/List recurse. Everything else passes
14
14
  * through.
15
15
  */
@@ -1,9 +1,23 @@
1
- import { TimeUnit as F_TimeUnit, DateUnit as F_DateUnit, IntervalUnit as F_IntervalUnit, UnionMode as F_UnionMode } from "@query-farm/flechette";
2
1
  import type { VgiField, VgiSchema, VgiDataType } from "../types.js";
3
- export declare const TimeUnit: typeof F_TimeUnit;
4
- export declare const DateUnit: typeof F_DateUnit;
5
- export declare const IntervalUnit: typeof F_IntervalUnit;
6
- export declare const UnionMode: typeof F_UnionMode;
2
+ export declare const TimeUnit: {
3
+ readonly SECOND: 0;
4
+ readonly MILLISECOND: 1;
5
+ readonly MICROSECOND: 2;
6
+ readonly NANOSECOND: 3;
7
+ };
8
+ export declare const DateUnit: {
9
+ readonly DAY: 0;
10
+ readonly MILLISECOND: 1;
11
+ };
12
+ export declare const IntervalUnit: {
13
+ readonly YEAR_MONTH: 0;
14
+ readonly DAY_TIME: 1;
15
+ readonly MONTH_DAY_NANO: 2;
16
+ };
17
+ export declare const UnionMode: {
18
+ readonly Sparse: 0;
19
+ readonly Dense: 1;
20
+ };
7
21
  export declare function schema(fields: readonly VgiField[], metadata?: Map<string, string>): VgiSchema;
8
22
  export declare function field(name: string, type: VgiDataType, nullable?: boolean, metadata?: Map<string, string>): VgiField;
9
23
  export declare const nullType: () => VgiDataType;
@@ -1,9 +1,28 @@
1
1
  import { type VgiBatch } from "../arrow/index.js";
2
- import type { RpcClient } from "@query-farm/vgi-rpc";
2
+ import type { RpcClient, StreamSession } from "@query-farm/vgi-rpc";
3
3
  import { type SchemaInfo, type TableInfo, type ViewInfo, type FunctionInfo, type MacroInfo, type MacroType, type CatalogAttachResult, type SchemaContentsInfo, type IndexInfo, type AttachOpaqueData, type TransactionOpaqueData, type CatalogInfo, type ScanFunctionResult } from "../catalog/interface.js";
4
4
  import { VgiClientError } from "./errors.js";
5
5
  export { VgiClientError };
6
6
  import type { VgiClientOptions, TableFunctionOptions, ScalarFunctionOptions, TableInOutFunctionOptions, OnCreateConflict, CatalogFunctionType, CatalogMacroType, CatalogAttachOptions, AttachOptionValue, CatalogSnapshot, LoadCatalogOptions } from "./types.js";
7
+ /**
8
+ * Send one input batch on a table-in-out exchange and yield the rows of every
9
+ * reply it gets (continued output, docs/table_in_out.md "Continued Output").
10
+ *
11
+ * A reply saying `vgi.status: HAVE_MORE_OUTPUT` has more output for the same
12
+ * input: ask for the next with a continuation tick (a zero-row batch of the
13
+ * input schema carrying `vgi.status: CONTINUE`) until a reply stops saying
14
+ * more. Only then may the next input go out, or the input close. The row-level
15
+ * `StreamSession.exchange` drops reply metadata, so this uses the raw exchange
16
+ * every transport's session implements.
17
+ *
18
+ * Refused, as the extension refuses them: `not_modified` on a reply that says
19
+ * more or answers a tick (a 304 stands for an input's whole output), and the
20
+ * worker ending the stream while output is pending.
21
+ *
22
+ * Exported for tests only (not re-exported from the package entry points).
23
+ * @internal
24
+ */
25
+ export declare function exchangeContinued(session: StreamSession, functionName: string, input: VgiBatch): AsyncGenerator<Record<string, any>[]>;
7
26
  /**
8
27
  * High-level client for calling VGI worker functions and catalog API.
9
28
  *
@@ -1914,6 +1914,14 @@ function map2(keyField, valueField, keysSorted) {
1914
1914
  function dictionary2(valueType, indexType, ordered, id) {
1915
1915
  return dictionary(valueType, indexType, ordered, id);
1916
1916
  }
1917
+ // src/cache-control.ts
1918
+ var CACHE_NOT_MODIFIED_KEY = "vgi.cache.not_modified";
1919
+
1920
+ // src/functions/continuation.ts
1921
+ var STATUS_KEY = "vgi.status";
1922
+ var HAVE_MORE_OUTPUT = "HAVE_MORE_OUTPUT";
1923
+ var CONTINUE = "CONTINUE";
1924
+
1917
1925
  // src/arguments/arguments.ts
1918
1926
  class Arguments {
1919
1927
  positional;
@@ -2705,6 +2713,7 @@ var FunctionInfoSchema = schema2([
2705
2713
  field2("sink_order_dependent", bool2(), false),
2706
2714
  field2("requires_input_batch_index", bool2(), false),
2707
2715
  field2("input_from_args", bool2(), false),
2716
+ field2("input_rows_per_call", int642(), true),
2708
2717
  field2("required_settings", list2(field2("item", utf82(), true)), false),
2709
2718
  field2("required_secrets", list2(field2("item", struct2([field2("secret_type", utf82(), false), field2("scope", utf82(), true), field2("secret_name", utf82(), true)]), true)), false)
2710
2719
  ]);
@@ -3869,6 +3878,42 @@ function toAsyncIterator(input) {
3869
3878
  function onConflictName(onConflict) {
3870
3879
  return String(onConflict).toUpperCase();
3871
3880
  }
3881
+ function isNotModified(md) {
3882
+ const v = md.get(CACHE_NOT_MODIFIED_KEY);
3883
+ return v != null && v !== "0" && v.toLowerCase() !== "false";
3884
+ }
3885
+ async function* exchangeContinued(session, functionName, input) {
3886
+ const raw = session;
3887
+ if (typeof raw.exchangeRaw !== "function") {
3888
+ throw new VgiClientError(`tableInOutFunction(${functionName}): the transport's stream session has no exchangeRaw(), ` + `which continued output needs to read each reply's vgi.status`);
3889
+ }
3890
+ const exchangeRaw = raw.exchangeRaw.bind(session);
3891
+ let tick = false;
3892
+ let reply = await exchangeRaw({ batch: input, metadata: new Map });
3893
+ for (;; ) {
3894
+ if (reply === null) {
3895
+ if (tick) {
3896
+ throw new VgiClientError(`tableInOutFunction(${functionName}): the worker ended the stream while output was pending`);
3897
+ }
3898
+ return;
3899
+ }
3900
+ const md = new Map([...reply.batch.metadata ?? [], ...reply.metadata]);
3901
+ const more = md.get(STATUS_KEY) === HAVE_MORE_OUTPUT;
3902
+ if (isNotModified(md) && (more || tick)) {
3903
+ throw new VgiClientError(`tableInOutFunction(${functionName}): the worker answered not_modified on a continued reply ` + `(${tick ? "answering a continuation tick" : "one saying HAVE_MORE_OUTPUT"}); ` + `not_modified must be an input's only reply`);
3904
+ }
3905
+ const rows = [...iterRows(reply.batch)];
3906
+ if (rows.length > 0)
3907
+ yield rows;
3908
+ if (!more)
3909
+ return;
3910
+ tick = true;
3911
+ reply = await exchangeRaw({
3912
+ batch: emptyBatch(input.schema),
3913
+ metadata: new Map([[STATUS_KEY, CONTINUE]])
3914
+ });
3915
+ }
3916
+ }
3872
3917
 
3873
3918
  class VgiClient {
3874
3919
  rpc;
@@ -4033,13 +4078,9 @@ class VgiClient {
4033
4078
  joinKeys: opts.joinKeys
4034
4079
  });
4035
4080
  try {
4036
- const outRows = await inputSession.exchange([...iterRows(firstBatch)]);
4037
- if (outRows.length > 0)
4038
- yield outRows;
4081
+ yield* exchangeContinued(inputSession, opts.functionName, firstBatch);
4039
4082
  for await (const batch of { [Symbol.asyncIterator]: () => inputIter }) {
4040
- const result = await inputSession.exchange([...iterRows(batch)]);
4041
- if (result.length > 0)
4042
- yield result;
4083
+ yield* exchangeContinued(inputSession, opts.functionName, batch);
4043
4084
  }
4044
4085
  } finally {
4045
4086
  inputSession.close();
@@ -4084,13 +4125,13 @@ class VgiClient {
4084
4125
  });
4085
4126
  const outputSchema = bindResp.output_schema;
4086
4127
  try {
4087
- const outRows = await inputSession.exchange([...iterRows(firstBatch)]);
4088
- if (outRows.length > 0)
4089
- yield batchFromRows(outRows, outputSchema);
4128
+ for await (const rows of exchangeContinued(inputSession, opts.functionName, firstBatch)) {
4129
+ yield batchFromRows(rows, outputSchema);
4130
+ }
4090
4131
  for await (const batch of { [Symbol.asyncIterator]: () => inputIter }) {
4091
- const result = await inputSession.exchange([...iterRows(batch)]);
4092
- if (result.length > 0)
4093
- yield batchFromRows(result, outputSchema);
4132
+ for await (const rows of exchangeContinued(inputSession, opts.functionName, batch)) {
4133
+ yield batchFromRows(rows, outputSchema);
4134
+ }
4094
4135
  }
4095
4136
  } finally {
4096
4137
  inputSession.close();
@@ -4875,4 +4916,4 @@ export {
4875
4916
  typeSignature
4876
4917
  };
4877
4918
 
4878
- //# debugId=D223782952AA2BC964756E2164756E21
4919
+ //# debugId=8E373AB802AEADEE64756E2164756E21