@query-farm/vgi 0.6.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.
Files changed (206) hide show
  1. package/LICENSE +134 -0
  2. package/README.md +494 -0
  3. package/dist/__dbg.test-d.d.ts +1 -0
  4. package/dist/arguments/argument-spec.d.ts +51 -0
  5. package/dist/arguments/arguments.d.ts +13 -0
  6. package/dist/arrow/codec/branded.d.ts +83 -0
  7. package/dist/arrow/codec/registry.d.ts +29 -0
  8. package/dist/arrow/codec/repr.d.ts +23 -0
  9. package/dist/arrow/codec/type-descriptors.d.ts +109 -0
  10. package/dist/arrow/impl-arrowjs/build.d.ts +19 -0
  11. package/dist/arrow/impl-arrowjs/canonical.d.ts +15 -0
  12. package/dist/arrow/impl-arrowjs/empty.d.ts +7 -0
  13. package/dist/arrow/impl-arrowjs/filter.d.ts +11 -0
  14. package/dist/arrow/impl-arrowjs/index.d.ts +11 -0
  15. package/dist/arrow/impl-arrowjs/ipc.d.ts +24 -0
  16. package/dist/arrow/impl-arrowjs/iterate.d.ts +39 -0
  17. package/dist/arrow/impl-arrowjs/project.d.ts +10 -0
  18. package/dist/arrow/impl-arrowjs/schema.d.ts +62 -0
  19. package/dist/arrow/impl-arrowjs/statistics.d.ts +13 -0
  20. package/dist/arrow/impl-flechette/build.d.ts +14 -0
  21. package/dist/arrow/impl-flechette/canonical.d.ts +18 -0
  22. package/dist/arrow/impl-flechette/empty.d.ts +9 -0
  23. package/dist/arrow/impl-flechette/filter.d.ts +10 -0
  24. package/dist/arrow/impl-flechette/index.d.ts +11 -0
  25. package/dist/arrow/impl-flechette/ipc.d.ts +33 -0
  26. package/dist/arrow/impl-flechette/iterate.d.ts +29 -0
  27. package/dist/arrow/impl-flechette/message-meta.d.ts +10 -0
  28. package/dist/arrow/impl-flechette/normalize-type.d.ts +11 -0
  29. package/dist/arrow/impl-flechette/project.d.ts +3 -0
  30. package/dist/arrow/impl-flechette/schema.d.ts +47 -0
  31. package/dist/arrow/impl-flechette/statistics.d.ts +13 -0
  32. package/dist/arrow/index.d.ts +11 -0
  33. package/dist/arrow/predicates.d.ts +47 -0
  34. package/dist/arrow/repr.test-d.d.ts +52 -0
  35. package/dist/arrow/schema-types.d.ts +71 -0
  36. package/dist/arrow/types.d.ts +57 -0
  37. package/dist/catalog/attach-option.d.ts +33 -0
  38. package/dist/catalog/attach-options.d.ts +30 -0
  39. package/dist/catalog/composite.d.ts +33 -0
  40. package/dist/catalog/descriptors.d.ts +154 -0
  41. package/dist/catalog/interface.d.ts +239 -0
  42. package/dist/catalog/read-only.d.ts +33 -0
  43. package/dist/client/client.d.ts +189 -0
  44. package/dist/client/errors.d.ts +26 -0
  45. package/dist/client/helpers.d.ts +12 -0
  46. package/dist/client/protocol.d.ts +16 -0
  47. package/dist/client/types.d.ts +125 -0
  48. package/dist/client-entry.d.ts +11 -0
  49. package/dist/client-entry.js +48 -0
  50. package/dist/client-entry.js.map +9 -0
  51. package/dist/codec/asd.d.ts +25 -0
  52. package/dist/crypto.d.ts +36 -0
  53. package/dist/errors.d.ts +24 -0
  54. package/dist/filter-pushdown/collector.d.ts +32 -0
  55. package/dist/filter-pushdown/deserialize.d.ts +16 -0
  56. package/dist/filter-pushdown/evaluate.d.ts +51 -0
  57. package/dist/filter-pushdown/index.d.ts +4 -0
  58. package/dist/filter-pushdown/types.d.ts +84 -0
  59. package/dist/functions/aggregate.d.ts +129 -0
  60. package/dist/functions/copy-from.d.ts +81 -0
  61. package/dist/functions/copy-to.d.ts +121 -0
  62. package/dist/functions/registry.d.ts +16 -0
  63. package/dist/functions/scalar.d.ts +98 -0
  64. package/dist/functions/storage-cf-do.d.ts +47 -0
  65. package/dist/functions/storage.d.ts +86 -0
  66. package/dist/functions/table-buffering.d.ts +99 -0
  67. package/dist/functions/table-in-out.d.ts +64 -0
  68. package/dist/functions/table.d.ts +118 -0
  69. package/dist/functions/types.d.ts +157 -0
  70. package/dist/generated/vgi-client.d.ts +466 -0
  71. package/dist/generated/vgi-protocol-schemas.d.ts +115 -0
  72. package/dist/index.core.d.ts +42 -0
  73. package/dist/index.d.ts +2 -0
  74. package/dist/index.js +10888 -0
  75. package/dist/index.js.map +85 -0
  76. package/dist/metadata/resolve.d.ts +3 -0
  77. package/dist/metadata/serialize.d.ts +12 -0
  78. package/dist/metadata/types.d.ts +47 -0
  79. package/dist/protocol/dispatch.d.ts +23 -0
  80. package/dist/protocol/handlers/aggregate.d.ts +3 -0
  81. package/dist/protocol/handlers/catalog/admin.d.ts +3 -0
  82. package/dist/protocol/handlers/catalog/index.d.ts +3 -0
  83. package/dist/protocol/handlers/catalog/index_methods.d.ts +3 -0
  84. package/dist/protocol/handlers/catalog/macro.d.ts +3 -0
  85. package/dist/protocol/handlers/catalog/shared.d.ts +70 -0
  86. package/dist/protocol/handlers/catalog/table.d.ts +3 -0
  87. package/dist/protocol/handlers/catalog/view.d.ts +3 -0
  88. package/dist/protocol/handlers/function.d.ts +8 -0
  89. package/dist/protocol/handlers/shared.d.ts +31 -0
  90. package/dist/protocol/handlers/table-buffering.d.ts +3 -0
  91. package/dist/protocol/serialize.d.ts +4 -0
  92. package/dist/protocol/serializers/arguments.d.ts +3 -0
  93. package/dist/protocol/serializers/bind.d.ts +9 -0
  94. package/dist/protocol/serializers/cardinality.d.ts +7 -0
  95. package/dist/protocol/serializers/init.d.ts +9 -0
  96. package/dist/protocol/serializers/shared.d.ts +12 -0
  97. package/dist/protocol/state-serializer.d.ts +23 -0
  98. package/dist/protocol/types.d.ts +129 -0
  99. package/dist/secrets/helpers.d.ts +15 -0
  100. package/dist/types.d.ts +52 -0
  101. package/dist/util/arrow/index.d.ts +1 -0
  102. package/dist/util/bytes.d.ts +2 -0
  103. package/dist/util/statistics.d.ts +7 -0
  104. package/dist/worker-cf-entry.d.ts +30 -0
  105. package/dist/worker-cf-entry.js +8767 -0
  106. package/dist/worker-cf-entry.js.map +74 -0
  107. package/dist/worker.d.ts +36 -0
  108. package/package.json +96 -0
  109. package/src/arguments/argument-spec.ts +200 -0
  110. package/src/arguments/arguments.ts +81 -0
  111. package/src/arrow/codec/branded.ts +206 -0
  112. package/src/arrow/codec/registry.ts +674 -0
  113. package/src/arrow/codec/repr.ts +181 -0
  114. package/src/arrow/codec/type-descriptors.ts +137 -0
  115. package/src/arrow/impl-arrowjs/build.ts +88 -0
  116. package/src/arrow/impl-arrowjs/canonical.ts +509 -0
  117. package/src/arrow/impl-arrowjs/empty.ts +72 -0
  118. package/src/arrow/impl-arrowjs/filter.ts +53 -0
  119. package/src/arrow/impl-arrowjs/index.ts +54 -0
  120. package/src/arrow/impl-arrowjs/ipc.ts +67 -0
  121. package/src/arrow/impl-arrowjs/iterate.ts +159 -0
  122. package/src/arrow/impl-arrowjs/project.ts +73 -0
  123. package/src/arrow/impl-arrowjs/schema.ts +256 -0
  124. package/src/arrow/impl-arrowjs/statistics.ts +185 -0
  125. package/src/arrow/impl-flechette/build.ts +65 -0
  126. package/src/arrow/impl-flechette/canonical.ts +289 -0
  127. package/src/arrow/impl-flechette/empty.ts +20 -0
  128. package/src/arrow/impl-flechette/filter.ts +42 -0
  129. package/src/arrow/impl-flechette/index.ts +54 -0
  130. package/src/arrow/impl-flechette/ipc.ts +110 -0
  131. package/src/arrow/impl-flechette/iterate.ts +121 -0
  132. package/src/arrow/impl-flechette/message-meta.ts +175 -0
  133. package/src/arrow/impl-flechette/normalize-type.ts +99 -0
  134. package/src/arrow/impl-flechette/project.ts +64 -0
  135. package/src/arrow/impl-flechette/schema.ts +202 -0
  136. package/src/arrow/impl-flechette/statistics.ts +198 -0
  137. package/src/arrow/index.ts +129 -0
  138. package/src/arrow/predicates.ts +66 -0
  139. package/src/arrow/repr.test-d.ts +189 -0
  140. package/src/arrow/schema-types.ts +213 -0
  141. package/src/arrow/types.ts +74 -0
  142. package/src/catalog/attach-option.ts +107 -0
  143. package/src/catalog/attach-options.ts +79 -0
  144. package/src/catalog/composite.ts +180 -0
  145. package/src/catalog/descriptors.ts +164 -0
  146. package/src/catalog/interface.ts +721 -0
  147. package/src/catalog/read-only.ts +909 -0
  148. package/src/client/client.ts +1348 -0
  149. package/src/client/errors.ts +69 -0
  150. package/src/client/helpers.ts +56 -0
  151. package/src/client/protocol.ts +33 -0
  152. package/src/client/types.ts +140 -0
  153. package/src/client-entry.ts +112 -0
  154. package/src/codec/asd.ts +147 -0
  155. package/src/crypto.ts +143 -0
  156. package/src/errors.ts +61 -0
  157. package/src/filter-pushdown/collector.ts +147 -0
  158. package/src/filter-pushdown/deserialize.ts +253 -0
  159. package/src/filter-pushdown/evaluate.ts +591 -0
  160. package/src/filter-pushdown/index.ts +28 -0
  161. package/src/filter-pushdown/types.ts +91 -0
  162. package/src/functions/aggregate.ts +384 -0
  163. package/src/functions/copy-from.ts +282 -0
  164. package/src/functions/copy-to.ts +319 -0
  165. package/src/functions/registry.ts +258 -0
  166. package/src/functions/scalar.ts +356 -0
  167. package/src/functions/storage-cf-do.ts +303 -0
  168. package/src/functions/storage.ts +521 -0
  169. package/src/functions/table-buffering.ts +562 -0
  170. package/src/functions/table-in-out.ts +373 -0
  171. package/src/functions/table.ts +521 -0
  172. package/src/functions/types.ts +192 -0
  173. package/src/generated/vgi-client.ts +560 -0
  174. package/src/generated/vgi-protocol-schemas.ts +864 -0
  175. package/src/index.core.ts +267 -0
  176. package/src/index.ts +11 -0
  177. package/src/metadata/resolve.ts +109 -0
  178. package/src/metadata/serialize.ts +242 -0
  179. package/src/metadata/types.ts +60 -0
  180. package/src/protocol/dispatch.ts +52 -0
  181. package/src/protocol/handlers/aggregate.ts +355 -0
  182. package/src/protocol/handlers/catalog/admin.ts +368 -0
  183. package/src/protocol/handlers/catalog/index.ts +25 -0
  184. package/src/protocol/handlers/catalog/index_methods.ts +65 -0
  185. package/src/protocol/handlers/catalog/macro.ts +114 -0
  186. package/src/protocol/handlers/catalog/shared.ts +291 -0
  187. package/src/protocol/handlers/catalog/table.ts +401 -0
  188. package/src/protocol/handlers/catalog/view.ts +119 -0
  189. package/src/protocol/handlers/function.ts +302 -0
  190. package/src/protocol/handlers/shared.ts +80 -0
  191. package/src/protocol/handlers/table-buffering.ts +207 -0
  192. package/src/protocol/serialize.ts +23 -0
  193. package/src/protocol/serializers/arguments.ts +160 -0
  194. package/src/protocol/serializers/bind.ts +212 -0
  195. package/src/protocol/serializers/cardinality.ts +45 -0
  196. package/src/protocol/serializers/init.ts +247 -0
  197. package/src/protocol/serializers/shared.ts +46 -0
  198. package/src/protocol/state-serializer.ts +181 -0
  199. package/src/protocol/types.ts +153 -0
  200. package/src/secrets/helpers.ts +70 -0
  201. package/src/types.ts +71 -0
  202. package/src/util/arrow/index.ts +23 -0
  203. package/src/util/bytes.ts +14 -0
  204. package/src/util/statistics.ts +40 -0
  205. package/src/worker-cf-entry.ts +172 -0
  206. package/src/worker.ts +212 -0
@@ -0,0 +1,69 @@
1
+ // Copyright 2025, 2026 Query Farm LLC - https://query.farm
2
+ // VgiClient error class + RpcClient wrapper that translates RpcError into
3
+ // VgiClientError, preserving the worker's remote traceback.
4
+
5
+ import { RpcError, type RpcClient } from "@query-farm/vgi-rpc";
6
+
7
+ /** Error thrown by VgiClient when an RPC call fails or returns unexpected data. */
8
+ export class VgiClientError extends Error {
9
+ /** Remote traceback from the worker, when an RpcError carried one. */
10
+ readonly remoteTraceback?: string;
11
+ /** Underlying error type from the worker (e.g. "ValueError"), when known. */
12
+ readonly errorType?: string;
13
+
14
+ constructor(
15
+ message: string,
16
+ options?: { remoteTraceback?: string; errorType?: string; cause?: unknown },
17
+ ) {
18
+ super(message, options?.cause != null ? { cause: options.cause } : undefined);
19
+ this.name = "VgiClientError";
20
+ if (options?.remoteTraceback) this.remoteTraceback = options.remoteTraceback;
21
+ if (options?.errorType) this.errorType = options.errorType;
22
+ }
23
+
24
+ /**
25
+ * Build a VgiClientError from an underlying RpcError, preserving the
26
+ * remote error type, message, and traceback so the worker-raised exception
27
+ * shows up at the top of the stack rather than buried under VGI framing.
28
+ * Mirrors Python's ClientError.from_rpc_error.
29
+ */
30
+ static fromRpcError(e: RpcError): VgiClientError {
31
+ const head = `${e.errorType}: ${e.errorMessage}`;
32
+ const message = e.remoteTraceback
33
+ ? `${head}\n\nRemote traceback:\n${e.remoteTraceback}`
34
+ : head;
35
+ return new VgiClientError(message, {
36
+ remoteTraceback: e.remoteTraceback || undefined,
37
+ errorType: e.errorType,
38
+ cause: e,
39
+ });
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Wrap an RpcClient so any RpcError thrown by call/stream is rethrown as a
45
+ * VgiClientError carrying the worker's remote traceback. Other errors
46
+ * propagate unchanged.
47
+ */
48
+ export function wrapRpcWithErrorEnrichment(rpc: RpcClient): RpcClient {
49
+ return {
50
+ async call(method, params) {
51
+ try {
52
+ return await rpc.call(method, params);
53
+ } catch (e) {
54
+ if (e instanceof RpcError) throw VgiClientError.fromRpcError(e);
55
+ throw e;
56
+ }
57
+ },
58
+ async stream(method, params) {
59
+ try {
60
+ return await rpc.stream(method, params);
61
+ } catch (e) {
62
+ if (e instanceof RpcError) throw VgiClientError.fromRpcError(e);
63
+ throw e;
64
+ }
65
+ },
66
+ describe: () => rpc.describe(),
67
+ close: () => rpc.close(),
68
+ };
69
+ }
@@ -0,0 +1,56 @@
1
+ // Copyright 2025, 2026 Query Farm LLC - https://query.farm
2
+ // Internal helpers shared by VgiClient methods.
3
+
4
+ import { toUint8Array } from "../util/bytes.js";
5
+
6
+ /**
7
+ * Deserialize a List<Binary> of serialized info batches into typed info objects.
8
+ */
9
+ export function deserializeInfoList<T>(
10
+ items: unknown,
11
+ deserializeFn: (bytes: Uint8Array) => T,
12
+ ): T[] {
13
+ if (!items) return [];
14
+ const arr: unknown[] = Array.isArray(items)
15
+ ? items
16
+ : [...(items as Iterable<unknown>)];
17
+ return arr
18
+ .filter((b) => b != null)
19
+ .map((b) => deserializeFn(toUint8Array(b)));
20
+ }
21
+
22
+ /**
23
+ * Deserialize Arrow Map entries into a plain Record<string, string>.
24
+ */
25
+ export function deserializeTags(mapVal: unknown): Record<string, string> {
26
+ const tags: Record<string, string> = {};
27
+ if (!mapVal) return tags;
28
+ const iterable = mapVal as { [Symbol.iterator]?: () => Iterator<unknown> };
29
+ if (typeof iterable[Symbol.iterator] !== "function") return tags;
30
+ for (const entry of iterable as Iterable<unknown>) {
31
+ if (Array.isArray(entry)) {
32
+ tags[String(entry[0])] = String(entry[1]);
33
+ } else if (entry && typeof entry === "object") {
34
+ const e = entry as Record<string, unknown> & { [0]?: unknown; [1]?: unknown };
35
+ tags[String(e.key ?? e[0] ?? "")] = String(e.value ?? e[1] ?? "");
36
+ }
37
+ }
38
+ return tags;
39
+ }
40
+
41
+ /**
42
+ * Convert sync/async iterable to async iterator.
43
+ */
44
+ export function toAsyncIterator<T>(
45
+ input: Iterable<T> | AsyncIterable<T>,
46
+ ): AsyncIterator<T> {
47
+ if (Symbol.asyncIterator in (input as any)) {
48
+ return (input as AsyncIterable<T>)[Symbol.asyncIterator]();
49
+ }
50
+ const syncIter = (input as Iterable<T>)[Symbol.iterator]();
51
+ return {
52
+ async next() {
53
+ return syncIter.next();
54
+ },
55
+ };
56
+ }
@@ -0,0 +1,33 @@
1
+ // Copyright 2025, 2026 Query Farm LLC - https://query.farm
2
+ // Wrap/unwrap helpers for the VGI double-wrapping protocol.
3
+ // Mirrors the server's dispatch.ts:64-84 (unwrapRequest/wrapResult).
4
+
5
+ import {
6
+ serializeBatch,
7
+ deserializeBatch,
8
+ batchToScalarDict,
9
+ } from "../util/arrow/index.js";
10
+ import { toUint8Array } from "../util/bytes.js";
11
+ import type { VgiBatch } from "../arrow/index.js";
12
+ export { toUint8Array };
13
+
14
+ /**
15
+ * Wrap a RecordBatch as a { request: Uint8Array } dict for RPC params.
16
+ * DuckDB wraps ArrowSerializableDataclass params in a single "request" Binary column.
17
+ */
18
+ export function wrapRequest(batch: VgiBatch): { request: Uint8Array } {
19
+ return { request: serializeBatch(batch) };
20
+ }
21
+
22
+ /**
23
+ * Unwrap a { result: Uint8Array } response dict.
24
+ * The "result" Binary column contains a serialized inner batch;
25
+ * returns its row 0 as a flat dict.
26
+ */
27
+ export function unwrapResult(
28
+ result: Record<string, any>,
29
+ ): Record<string, any> {
30
+ const resultBytes = toUint8Array(result.result);
31
+ const innerBatch = deserializeBatch(resultBytes);
32
+ return batchToScalarDict(innerBatch);
33
+ }
@@ -0,0 +1,140 @@
1
+ // Copyright 2025, 2026 Query Farm LLC - https://query.farm
2
+ // Client option types for VgiClient.
3
+
4
+ import type { VgiBatch } from "../arrow/index.js";
5
+ import type { Arguments } from "../arguments/arguments.js";
6
+ import type { AttachOptionValue } from "../catalog/attach-options.js";
7
+ import type { BindResponse } from "../protocol/types.js";
8
+ import { OrderByDirection, OrderByNullOrder } from "../protocol/types.js";
9
+
10
+ export type { AttachOptionValue };
11
+ export { OrderByDirection, OrderByNullOrder };
12
+
13
+ /** ORDER BY pushdown hint passed at init time. */
14
+ export interface OrderByPushdown {
15
+ columnName: string;
16
+ direction: OrderByDirection;
17
+ nullOrder: OrderByNullOrder;
18
+ /** Optional LIMIT to combine with ORDER BY. */
19
+ limit?: bigint | number | null;
20
+ }
21
+
22
+ /** TABLESAMPLE pushdown hint passed at init time. */
23
+ export interface TablesamplePushdown {
24
+ /** Sample percentage in [0, 100]. */
25
+ percentage: number;
26
+ /** Optional sampling seed. */
27
+ seed?: bigint | number | null;
28
+ }
29
+
30
+ /** Callback invoked after a successful bind, before init. */
31
+ export type BindResultCallback = (response: BindResponse) => void;
32
+
33
+ /**
34
+ * Options bag for VgiClient.catalogAttach.
35
+ *
36
+ * Pass `options` as a plain key→value map — the client serializes to an
37
+ * Arrow RecordBatch with per-value type inference (see AttachOptionValue).
38
+ * For Arrow types that inference can't express (Decimal, Timestamp, exact
39
+ * int width, nested struct), use `optionsBytes` to supply pre-serialized
40
+ * bytes. Providing both throws.
41
+ *
42
+ * `dataVersionSpec` / `implementationVersion` are sent to versioned
43
+ * catalogs for attach-time validation; workers that aren't versioned
44
+ * ignore them.
45
+ */
46
+ export interface CatalogAttachOptions {
47
+ options?: Record<string, AttachOptionValue>;
48
+ optionsBytes?: Uint8Array;
49
+ dataVersionSpec?: string | null;
50
+ implementationVersion?: string | null;
51
+ }
52
+
53
+ /** Conflict resolution strategy for create operations. */
54
+ export type OnCreateConflict = "error" | "ignore" | "replace";
55
+
56
+ /** DuckDB catalog function type filter (sent as uppercase wire values). */
57
+ export type CatalogFunctionType = "SCALAR_FUNCTION" | "TABLE_FUNCTION";
58
+
59
+ /** Macro type filter for schema contents listing (uppercase wire values). */
60
+ export type CatalogMacroType = "SCALAR_MACRO" | "TABLE_MACRO";
61
+
62
+ /** Options for constructing a VgiClient. */
63
+ export interface VgiClientOptions {
64
+ /** Pre-existing attach ID to bind this client to a specific catalog. */
65
+ attachOpaqueData?: Uint8Array;
66
+ }
67
+
68
+ /** Options for calling a table function. */
69
+ export interface TableFunctionOptions {
70
+ /** Name of the function to call. */
71
+ functionName: string;
72
+ /** Positional and named arguments. */
73
+ arguments?: Arguments;
74
+ /** Column indices to project (filter pushdown). */
75
+ projectionIds?: number[];
76
+ /** Filter pushdown batch. */
77
+ pushdownFilters?: VgiBatch;
78
+ /** DuckDB settings to pass to the function. */
79
+ settings?: VgiBatch;
80
+ /** Transaction ID for transactional catalogs. */
81
+ transactionOpaqueData?: Uint8Array;
82
+ /** Attach ID to bind this call to a specific catalog attach. Overrides the client-level attachOpaqueData. */
83
+ attachOpaqueData?: Uint8Array;
84
+ /** ORDER BY pushdown hint from DuckDB's RowGroupPruner. */
85
+ orderBy?: OrderByPushdown;
86
+ /** TABLESAMPLE pushdown hint from DuckDB's SamplingPushdown optimizer. */
87
+ tablesample?: TablesamplePushdown;
88
+ /** Join-key value batches, one per join-keys column. */
89
+ joinKeys?: VgiBatch[];
90
+ /** Invoked after bind, before init. Receives the bind response. */
91
+ onBind?: BindResultCallback;
92
+ }
93
+
94
+ /** Options for calling a scalar function. */
95
+ export interface ScalarFunctionOptions {
96
+ /** Name of the function to call. */
97
+ functionName: string;
98
+ /** Input batches to process. */
99
+ input: Iterable<VgiBatch> | AsyncIterable<VgiBatch>;
100
+ /** Positional and named arguments. */
101
+ arguments?: Arguments;
102
+ /** DuckDB settings to pass to the function. */
103
+ settings?: VgiBatch;
104
+ /** DuckDB secrets to pass to the function. */
105
+ secrets?: VgiBatch;
106
+ /** Transaction ID for transactional catalogs. */
107
+ transactionOpaqueData?: Uint8Array;
108
+ /** Attach ID to bind this call to a specific catalog attach. Overrides the client-level attachOpaqueData. */
109
+ attachOpaqueData?: Uint8Array;
110
+ /** Invoked after bind, before init. Receives the bind response. */
111
+ onBind?: BindResultCallback;
112
+ }
113
+
114
+ /** Options for calling a table-in-out function. */
115
+ export interface TableInOutFunctionOptions {
116
+ /** Name of the function to call. */
117
+ functionName: string;
118
+ /** Input batches to process. */
119
+ input: Iterable<VgiBatch> | AsyncIterable<VgiBatch>;
120
+ /** Positional and named arguments. */
121
+ arguments?: Arguments;
122
+ /** Column indices to project (filter pushdown). */
123
+ projectionIds?: number[];
124
+ /** Filter pushdown batch. */
125
+ pushdownFilters?: VgiBatch;
126
+ /** DuckDB settings to pass to the function. */
127
+ settings?: VgiBatch;
128
+ /** Transaction ID for transactional catalogs. */
129
+ transactionOpaqueData?: Uint8Array;
130
+ /** Attach ID to bind this call to a specific catalog attach. Overrides the client-level attachOpaqueData. */
131
+ attachOpaqueData?: Uint8Array;
132
+ /** ORDER BY pushdown hint from DuckDB's RowGroupPruner. */
133
+ orderBy?: OrderByPushdown;
134
+ /** TABLESAMPLE pushdown hint from DuckDB's SamplingPushdown optimizer. */
135
+ tablesample?: TablesamplePushdown;
136
+ /** Join-key value batches, one per join-keys column. */
137
+ joinKeys?: VgiBatch[];
138
+ /** Invoked after bind, before init. Receives the bind response. */
139
+ onBind?: BindResultCallback;
140
+ }
@@ -0,0 +1,112 @@
1
+ // Copyright 2025, 2026 Query Farm LLC - https://query.farm
2
+ // VGI Client — browser-safe entry point.
3
+ //
4
+ // Import from "vgi/client" instead of "vgi" when targeting browsers, Deno,
5
+ // or any environment that cannot load Node.js built-ins. This entry point
6
+ // excludes server-side code (Worker, Protocol, Storage) that depends on
7
+ // node:fs, node:os, and process.
8
+ //
9
+ // Usage:
10
+ // import { VgiClient, Arguments } from "vgi/client";
11
+ // import { httpConnect } from "@query-farm/vgi-rpc"; // or your own RpcClient
12
+ //
13
+ // const rpc = httpConnect("https://my-vgi-server/");
14
+ // const client = new VgiClient(rpc);
15
+
16
+ // Client
17
+ export { VgiClient, VgiClientError } from "./client/client.js";
18
+ export type {
19
+ VgiClientOptions,
20
+ TableFunctionOptions,
21
+ ScalarFunctionOptions,
22
+ TableInOutFunctionOptions,
23
+ OnCreateConflict,
24
+ CatalogFunctionType,
25
+ } from "./client/types.js";
26
+
27
+ // Arguments
28
+ export { Arguments } from "./arguments/arguments.js";
29
+ export {
30
+ type ArgumentSpec,
31
+ argumentSpecsToSchema,
32
+ schemaToArgumentSpecs,
33
+ } from "./arguments/argument-spec.js";
34
+
35
+ // Core enums
36
+ export {
37
+ FunctionType,
38
+ FunctionStability,
39
+ NullHandling,
40
+ OrderPreservation,
41
+ OrderDependence,
42
+ DistinctDependence,
43
+ TableInOutPhase,
44
+ } from "./types.js";
45
+
46
+ // Protocol types (needed for advanced client use)
47
+ export type {
48
+ BindRequest,
49
+ BindResponse,
50
+ InitRequest,
51
+ GlobalInitResponse,
52
+ TableCardinality,
53
+ } from "./protocol/types.js";
54
+
55
+ // Catalog info types
56
+ export {
57
+ type SchemaInfo,
58
+ encodeSchemaInfo,
59
+ decodeSchemaInfo,
60
+ type TableInfo,
61
+ encodeTableInfo,
62
+ decodeTableInfo,
63
+ type ViewInfo,
64
+ encodeViewInfo,
65
+ decodeViewInfo,
66
+ type FunctionInfo,
67
+ encodeFunctionInfo,
68
+ decodeFunctionInfo,
69
+ type MacroInfo,
70
+ encodeMacroInfo,
71
+ decodeMacroInfo,
72
+ type CatalogAttachResult,
73
+ type AttachOpaqueData,
74
+ type TransactionOpaqueData,
75
+ } from "./catalog/interface.js";
76
+
77
+ // Errors
78
+ export {
79
+ VgiError,
80
+ RowCountMismatchError,
81
+ FunctionNotFoundError,
82
+ CatalogReadOnlyError,
83
+ CatalogNotFoundError,
84
+ CatalogAlreadyExistsError,
85
+ ArgumentValidationError,
86
+ NoCatalogError,
87
+ } from "./errors.js";
88
+
89
+ // Arrow utilities
90
+ export {
91
+ iterRows,
92
+ batchFromRows,
93
+ batchFromColumns,
94
+ emptyBatch,
95
+ filterBatch,
96
+ projectSchema,
97
+ projectBatch,
98
+ batchToScalarDict,
99
+ safeNumber,
100
+ serializeSchema,
101
+ deserializeSchema,
102
+ serializeBatch,
103
+ deserializeBatch,
104
+ } from "./util/arrow/index.js";
105
+
106
+ // Byte utilities
107
+ export { toUint8Array } from "./util/bytes.js";
108
+
109
+ // Re-export transport types from vgi-rpc (type-only — zero runtime cost).
110
+ // Users import the actual connect functions (httpConnect, pipeConnect) from
111
+ // "@query-farm/vgi-rpc" directly so their bundler can tree-shake the server-side code.
112
+ export type { RpcClient, StreamSession, LogMessage } from "@query-farm/vgi-rpc";
@@ -0,0 +1,147 @@
1
+ // Copyright 2025, 2026 Query Farm LLC - https://query.farm
2
+ // SPDX-License-Identifier: LicenseRef-QueryFarm-Source-Available-1.0
3
+
4
+ /**
5
+ * Generic ArrowSerializableDataclass (ASD) codec.
6
+ *
7
+ * Bridges between typed TS objects (snake_case, matching vgi-python dataclass
8
+ * field names) and Arrow IPC bytes (single-row batch with the dataclass'
9
+ * ARROW_SCHEMA). Used by the generated vgi-client.ts encode<X>/decode<X>
10
+ * wrappers to make the typed client interfaces honest at the wire boundary.
11
+ *
12
+ * Encoding delegates to the facade's `batchFromRows` (which handles the
13
+ * complex types — Decimal/BigInt/List/Map/Struct/Dictionary — uniformly
14
+ * across both arrow-js and flechette backends). Decoding pulls each field's
15
+ * value with `batchToScalarDict`, then runs `normalizeValue` to convert
16
+ * Arrow representations (MapRow, BigInt, etc.) back into plain JS shapes.
17
+ */
18
+
19
+ import {
20
+ type VgiSchema,
21
+ type VgiField,
22
+ type VgiDataType,
23
+ isBinary,
24
+ isBool,
25
+ isDictionary,
26
+ isInt,
27
+ isList,
28
+ isMap,
29
+ isStruct,
30
+ batchFromRows,
31
+ serializeBatch,
32
+ deserializeBatch,
33
+ } from "../arrow/index.js";
34
+
35
+ /**
36
+ * Encode a typed object as a single-row Arrow IPC stream using `schema`.
37
+ * Matches Python's `ArrowSerializableDataclass.serialize_to_bytes()`.
38
+ */
39
+ export function encodeASD(
40
+ schema: VgiSchema,
41
+ obj: Record<string, any>,
42
+ ): Uint8Array {
43
+ return serializeBatch(batchFromRows([obj], schema));
44
+ }
45
+
46
+ /**
47
+ * Decode a single-row Arrow IPC stream into a typed object using `schema`.
48
+ * Matches Python's `ArrowSerializableDataclass.deserialize_from_bytes()`.
49
+ */
50
+ export function decodeASD<T>(
51
+ schema: VgiSchema,
52
+ bytes: Uint8Array,
53
+ ): T {
54
+ const batch = deserializeBatch(bytes);
55
+ if (batch.numRows === 0) {
56
+ const names = schema.fields.map((f) => f.name).join(",");
57
+ throw new Error(`decodeASD: empty batch (expected 1 row for ${names})`);
58
+ }
59
+ const out: Record<string, any> = {};
60
+ for (const field of schema.fields) {
61
+ const col = batch.getChild(field.name);
62
+ const raw = col ? col.get(0) : null;
63
+ out[field.name] = normalizeValue(raw, field.type);
64
+ }
65
+ return out as T;
66
+ }
67
+
68
+ // --------------------------------------------------------------------------- //
69
+ // Decode side: Arrow value -> normalized JS value
70
+ // --------------------------------------------------------------------------- //
71
+
72
+ function normalizeValue(raw: any, type: VgiDataType): any {
73
+ if (raw == null) return null;
74
+
75
+ if (isList(type)) {
76
+ const childType = (type as any).children[0].type as VgiDataType;
77
+ const out: any[] = [];
78
+ for (const item of raw) out.push(normalizeValue(item, childType));
79
+ return out;
80
+ }
81
+
82
+ if (isMap(type)) {
83
+ const out: Record<string, string> = {};
84
+ // arrow-js MapRow is iterable of [k,v]; flechette returns [[k,v],...] arrays.
85
+ if (raw[Symbol.iterator]) {
86
+ for (const entry of raw) {
87
+ if (Array.isArray(entry)) {
88
+ out[String(entry[0])] = String(entry[1] ?? "");
89
+ } else if (entry && typeof entry === "object") {
90
+ const k = entry.key ?? entry[0];
91
+ const v = entry.value ?? entry[1];
92
+ out[String(k)] = v == null ? "" : String(v);
93
+ }
94
+ }
95
+ } else if (typeof raw === "object") {
96
+ // Plain-object map (flechette without useMap): Object.entries fallback
97
+ for (const [k, v] of Object.entries(raw)) {
98
+ out[String(k)] = v == null ? "" : String(v);
99
+ }
100
+ }
101
+ return out;
102
+ }
103
+
104
+ if (isStruct(type)) {
105
+ const childFields = (type as any).children as VgiField[];
106
+ const out: Record<string, any> = {};
107
+ for (const cf of childFields) {
108
+ out[cf.name] = normalizeValue(raw[cf.name], cf.type);
109
+ }
110
+ return out;
111
+ }
112
+
113
+ if (isDictionary(type)) {
114
+ return raw == null ? null : String(raw);
115
+ }
116
+
117
+ if (isBinary(type)) {
118
+ return toUint8Array(raw);
119
+ }
120
+
121
+ if (isInt(type) && (type as any).bitWidth === 64) {
122
+ // Prefer number when representable; callers that need BigInt can convert.
123
+ if (typeof raw === "bigint") {
124
+ if (raw >= BigInt(Number.MIN_SAFE_INTEGER) && raw <= BigInt(Number.MAX_SAFE_INTEGER)) {
125
+ return Number(raw);
126
+ }
127
+ return raw;
128
+ }
129
+ return raw;
130
+ }
131
+
132
+ if (isBool(type)) {
133
+ return Boolean(raw);
134
+ }
135
+
136
+ // Utf8, Int8/16/32, Float32/64 — pass through as-is.
137
+ return raw;
138
+ }
139
+
140
+ function toUint8Array(val: any): Uint8Array {
141
+ if (val instanceof Uint8Array) return val;
142
+ if (val instanceof ArrayBuffer) return new Uint8Array(val);
143
+ if (val && val.buffer instanceof ArrayBuffer) {
144
+ return new Uint8Array(val.buffer, val.byteOffset, val.byteLength);
145
+ }
146
+ return new Uint8Array(0);
147
+ }