@query-farm/vgi 0.48.3 → 0.48.4
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/dist/catalog/descriptors.d.ts +30 -0
- package/dist/catalog/interface.d.ts +28 -0
- package/dist/catalog/read-only.d.ts +16 -2
- package/dist/client-entry.js.map +2 -2
- package/dist/index.core.d.ts +2 -0
- package/dist/index.js +230 -34
- package/dist/index.js.map +12 -11
- package/dist/protocol/handlers/catalog/shared.d.ts +8 -0
- package/dist/serve-entry.js +43 -9
- package/dist/serve-entry.js.map +6 -6
- package/dist/worker-cf-entry.js +136 -19
- package/dist/worker-cf-entry.js.map +11 -10
- package/dist/write-results.d.ts +57 -0
- package/package.json +1 -1
- package/src/catalog/descriptors.ts +30 -0
- package/src/catalog/interface.ts +51 -0
- package/src/catalog/read-only.ts +123 -18
- package/src/index.core.ts +23 -0
- package/src/protocol/handlers/catalog/shared.ts +17 -0
- package/src/protocol/handlers/catalog/table.ts +37 -0
- package/src/protocol/handlers/function.ts +42 -14
- package/src/write-results.ts +174 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Copyright 2025, 2026 Query Farm LLC - https://query.farm
|
|
2
|
+
// SPDX-License-Identifier: LicenseRef-QueryFarm-Source-Available-1.0
|
|
3
|
+
|
|
4
|
+
// Writable-table result modes shared by catalogs and write functions.
|
|
5
|
+
// Port of vgi-python's `vgi/write_results.py`, plus the `write_options` decoder
|
|
6
|
+
// its write functions use.
|
|
7
|
+
//
|
|
8
|
+
// A writable table advertises, per operation (insert / update / delete), the
|
|
9
|
+
// richest result mode its write function can produce. Modes are ordered
|
|
10
|
+
// `count < rows < changes`, and a table that supports one mode supports every
|
|
11
|
+
// lower one. DuckDB asks for one mode per statement, in the `write_options`
|
|
12
|
+
// named argument of the write function's bind:
|
|
13
|
+
//
|
|
14
|
+
// count -- plain INSERT/UPDATE/DELETE: one row, `count BIGINT NOT NULL`
|
|
15
|
+
// rows -- `... RETURNING`: the written rows, in the table's schema
|
|
16
|
+
// changes -- old/new pairs: `old STRUCT(<table>)`, `new STRUCT(<table>)`
|
|
17
|
+
//
|
|
18
|
+
// UPDATE and DELETE identify rows by the table's row-id column (the one field
|
|
19
|
+
// carrying `is_row_id` metadata), which the scan function must emit.
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
type VgiBatch,
|
|
23
|
+
type VgiField,
|
|
24
|
+
type VgiSchema,
|
|
25
|
+
batchFromColumns,
|
|
26
|
+
deserializeBatch,
|
|
27
|
+
field,
|
|
28
|
+
int64,
|
|
29
|
+
iterRows,
|
|
30
|
+
schema,
|
|
31
|
+
struct,
|
|
32
|
+
} from "./arrow/index.js";
|
|
33
|
+
import type { BindRequest } from "./protocol/types.js";
|
|
34
|
+
import { toUint8Array } from "./util/bytes.js";
|
|
35
|
+
|
|
36
|
+
export type WriteOperation = "insert" | "update" | "delete";
|
|
37
|
+
export type WriteResultMode = "count" | "rows" | "changes";
|
|
38
|
+
|
|
39
|
+
export const WRITE_OPERATIONS: readonly WriteOperation[] = ["insert", "update", "delete"];
|
|
40
|
+
export const WRITE_RESULT_MODES: readonly WriteResultMode[] = ["count", "rows", "changes"];
|
|
41
|
+
const MODE_RANK: Record<string, number> = { count: 0, rows: 1, changes: 2 };
|
|
42
|
+
|
|
43
|
+
/** Field metadata key that marks a table's row-id column. */
|
|
44
|
+
export const ROW_ID_METADATA_KEY = "is_row_id";
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A row-id field for a writable table's columns and its scan's output:
|
|
48
|
+
* `rowIdField()` is DuckDB's conventional `rowid BIGINT`.
|
|
49
|
+
*
|
|
50
|
+
* DuckDB reads the row id as a *virtual* column, which it only requests from a
|
|
51
|
+
* scan that supports projection pushdown. So a table with UPDATE or DELETE
|
|
52
|
+
* needs a scan function that (1) emits this field, (2) is declared with
|
|
53
|
+
* `projectionPushdown: true` and honours `params.outputSchema`, and (3) is
|
|
54
|
+
* listed in its schema's `functions`, which is how DuckDB learns (2). Missing
|
|
55
|
+
* any of these, UPDATE/DELETE fail with "Virtual columns require projection
|
|
56
|
+
* pushdown". Row ids must be unique and stable for the life of a statement.
|
|
57
|
+
*/
|
|
58
|
+
export function rowIdField(name = "rowid", type = int64()): VgiField {
|
|
59
|
+
return field(name, type, false, new Map([[ROW_ID_METADATA_KEY, ""]]));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Return a validated copy of an operation -> maximum-mode mapping. */
|
|
63
|
+
export function validateWriteResultModes(
|
|
64
|
+
modes: Partial<Record<string, string>>,
|
|
65
|
+
): Partial<Record<WriteOperation, WriteResultMode>> {
|
|
66
|
+
const out: Partial<Record<WriteOperation, WriteResultMode>> = {};
|
|
67
|
+
const unknown = Object.keys(modes).filter((op) => !WRITE_OPERATIONS.includes(op as WriteOperation)).sort();
|
|
68
|
+
if (unknown.length) throw new Error(`Unknown write operation(s): ${unknown.join(", ")}`);
|
|
69
|
+
for (const [operation, mode] of Object.entries(modes)) {
|
|
70
|
+
if (mode === undefined) continue;
|
|
71
|
+
if (!(mode in MODE_RANK)) throw new Error(`Unknown write result mode '${mode}' for '${operation}'`);
|
|
72
|
+
out[operation as WriteOperation] = mode as WriteResultMode;
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Whether a table whose maximum is `maximum` supports `requested`. */
|
|
78
|
+
export function supportsWriteResultMode(maximum: string, requested: string): boolean {
|
|
79
|
+
return maximum in MODE_RANK && requested in MODE_RANK && MODE_RANK[maximum]! >= MODE_RANK[requested]!;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Validate a wire value and narrow it to a `WriteResultMode`. */
|
|
83
|
+
export function coerceWriteResultMode(value: unknown): WriteResultMode {
|
|
84
|
+
if (typeof value !== "string" || !(value in MODE_RANK)) {
|
|
85
|
+
throw new Error(`Unknown write result mode: ${JSON.stringify(value)}`);
|
|
86
|
+
}
|
|
87
|
+
return value as WriteResultMode;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The table's user columns: `columns` without the row-id field. Write results
|
|
92
|
+
* (`rows`, and both halves of `changes`) use this schema.
|
|
93
|
+
*/
|
|
94
|
+
export function userColumns(columns: VgiSchema): VgiSchema {
|
|
95
|
+
return schema(columns.fields.filter((f) => !f.metadata?.has(ROW_ID_METADATA_KEY)));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The exact output schema a write function must bind for `mode`. */
|
|
99
|
+
export function writeResultSchema(mode: WriteResultMode, tableSchema: VgiSchema): VgiSchema {
|
|
100
|
+
if (mode === "count") return schema([field("count", int64(), false)]);
|
|
101
|
+
if (mode === "rows") return tableSchema;
|
|
102
|
+
const rowType = struct(tableSchema.fields as VgiField[]);
|
|
103
|
+
return schema([field("old", rowType, true), field("new", rowType, true)]);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A `count` result batch. */
|
|
107
|
+
export function writeCountBatch(count: number | bigint): VgiBatch {
|
|
108
|
+
return batchFromColumns({ count: [BigInt(count)] }, writeResultSchema("count", schema([])));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A `rows` result batch: the written rows as plain objects keyed by column. */
|
|
112
|
+
export function writeRowsBatch(tableSchema: VgiSchema, rows: readonly Record<string, unknown>[]): VgiBatch {
|
|
113
|
+
const columns: Record<string, unknown[]> = {};
|
|
114
|
+
for (const f of tableSchema.fields) columns[f.name] = rows.map((r) => r[f.name] ?? null);
|
|
115
|
+
return batchFromColumns(columns, tableSchema);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A `changes` result batch, one OLD/NEW pair per written row: `old` is null
|
|
120
|
+
* for an insert, `new` is null for a delete.
|
|
121
|
+
*/
|
|
122
|
+
export function writeChangesBatch(
|
|
123
|
+
tableSchema: VgiSchema,
|
|
124
|
+
oldRows: readonly (Record<string, unknown> | null)[],
|
|
125
|
+
newRows: readonly (Record<string, unknown> | null)[],
|
|
126
|
+
): VgiBatch {
|
|
127
|
+
if (oldRows.length !== newRows.length) throw new Error("oldRows and newRows must have equal length");
|
|
128
|
+
const pick = (row: Record<string, unknown> | null) =>
|
|
129
|
+
row == null ? null : Object.fromEntries(tableSchema.fields.map((f) => [f.name, row[f.name] ?? null]));
|
|
130
|
+
return batchFromColumns(
|
|
131
|
+
{ old: oldRows.map(pick), new: newRows.map(pick) },
|
|
132
|
+
writeResultSchema("changes", tableSchema),
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The result batch for `mode`, from the written rows' before/after images. */
|
|
137
|
+
export function writeResultBatch(
|
|
138
|
+
mode: WriteResultMode,
|
|
139
|
+
tableSchema: VgiSchema,
|
|
140
|
+
oldRows: readonly (Record<string, unknown> | null)[],
|
|
141
|
+
newRows: readonly (Record<string, unknown> | null)[],
|
|
142
|
+
): VgiBatch {
|
|
143
|
+
if (mode === "count") return writeCountBatch(newRows.length);
|
|
144
|
+
if (mode === "changes") return writeChangesBatch(tableSchema, oldRows, newRows);
|
|
145
|
+
// RETURNING shows the row as written: the new image, or the old one for a delete.
|
|
146
|
+
return writeRowsBatch(
|
|
147
|
+
tableSchema,
|
|
148
|
+
newRows.map((row, i) => (row ?? oldRows[i]) as Record<string, unknown>),
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** What DuckDB asked a write function for, decoded from `write_options`. */
|
|
153
|
+
export interface WriteOptions {
|
|
154
|
+
resultMode: WriteResultMode;
|
|
155
|
+
/** `throw` (default), or the INSERT ... ON CONFLICT action DuckDB requested. */
|
|
156
|
+
onConflict: string;
|
|
157
|
+
onConflictColumns: string[];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Decode the `write_options` named argument of a write function's bind (a
|
|
162
|
+
* serialized one-row batch). Missing options mean a plain `count` write.
|
|
163
|
+
*/
|
|
164
|
+
export function writeOptions(bindCall: Pick<BindRequest, "arguments">): WriteOptions {
|
|
165
|
+
const out: WriteOptions = { resultMode: "count", onConflict: "throw", onConflictColumns: [] };
|
|
166
|
+
const raw = bindCall.arguments?.get("write_options", null);
|
|
167
|
+
if (raw == null) return out;
|
|
168
|
+
const row = [...iterRows(deserializeBatch(toUint8Array(raw)))][0] as Record<string, unknown> | undefined;
|
|
169
|
+
if (!row) return out;
|
|
170
|
+
if (row.result_mode != null) out.resultMode = coerceWriteResultMode(row.result_mode);
|
|
171
|
+
if (row.on_conflict != null) out.onConflict = String(row.on_conflict);
|
|
172
|
+
if (row.on_conflict_columns != null) out.onConflictColumns = Array.from(row.on_conflict_columns as Iterable<string>);
|
|
173
|
+
return out;
|
|
174
|
+
}
|