@query-farm/vgi 0.46.0 → 0.47.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 (89) hide show
  1. package/README.md +11 -5
  2. package/dist/arrow/impl-arrowjs/index.d.ts +1 -1
  3. package/dist/arrow/impl-arrowjs/iterate.d.ts +2 -0
  4. package/dist/arrow/impl-flechette/index.d.ts +1 -1
  5. package/dist/arrow/impl-flechette/iterate.d.ts +2 -0
  6. package/dist/arrow/index.d.ts +1 -1
  7. package/dist/catalog/column-expressions.d.ts +8 -0
  8. package/dist/catalog/column-metadata.d.ts +11 -0
  9. package/dist/catalog/composite.d.ts +2 -1
  10. package/dist/catalog/descriptors.d.ts +21 -29
  11. package/dist/catalog/interface.d.ts +15 -20
  12. package/dist/catalog/macro-overloads.d.ts +4 -0
  13. package/dist/catalog/read-only.d.ts +1 -1
  14. package/dist/catalog/unconstrained-columns.d.ts +2 -0
  15. package/dist/client/client.d.ts +18 -13
  16. package/dist/client-entry.js +753 -103
  17. package/dist/client-entry.js.map +16 -14
  18. package/dist/functions/projection.d.ts +18 -0
  19. package/dist/functions/table.d.ts +6 -1
  20. package/dist/functions/types.d.ts +1 -0
  21. package/dist/generated/vgi-client.d.ts +177 -36
  22. package/dist/generated/vgi-protocol-schemas.d.ts +51 -1
  23. package/dist/generated/vgi-protocol-types.d.ts +317 -3
  24. package/dist/generated/vgi-service.d.ts +266 -29
  25. package/dist/index.core.d.ts +2 -1
  26. package/dist/index.js +1480 -238
  27. package/dist/index.js.map +40 -34
  28. package/dist/protocol/dispatch.d.ts +9 -9
  29. package/dist/protocol/handlers/aggregate.d.ts +1 -1
  30. package/dist/protocol/handlers/catalog/index.d.ts +1 -1
  31. package/dist/protocol/handlers/catalog/metadata.d.ts +1 -0
  32. package/dist/protocol/handlers/function.d.ts +1 -1
  33. package/dist/protocol/handlers/table-buffering.d.ts +1 -1
  34. package/dist/protocol/handlers/unimplemented.d.ts +1 -1
  35. package/dist/protocol/serializers/bind.d.ts +5 -1
  36. package/dist/protocol/types.d.ts +5 -0
  37. package/dist/rpc-server.d.ts +6 -6
  38. package/dist/serve-entry.js +1593 -452
  39. package/dist/serve-entry.js.map +31 -26
  40. package/dist/util/arrow/index.d.ts +1 -1
  41. package/dist/worker-cf-entry.js +1814 -589
  42. package/dist/worker-cf-entry.js.map +39 -33
  43. package/dist/worker.d.ts +1 -1
  44. package/package.json +5 -4
  45. package/src/arrow/impl-arrowjs/index.ts +1 -1
  46. package/src/arrow/impl-arrowjs/iterate.ts +9 -1
  47. package/src/arrow/impl-arrowjs/project.ts +1 -1
  48. package/src/arrow/impl-flechette/index.ts +1 -1
  49. package/src/arrow/impl-flechette/ipc.ts +12 -0
  50. package/src/arrow/impl-flechette/iterate.ts +5 -0
  51. package/src/arrow/impl-flechette/project.ts +6 -8
  52. package/src/arrow/index.ts +1 -1
  53. package/src/catalog/column-expressions.ts +224 -0
  54. package/src/catalog/column-metadata.ts +151 -0
  55. package/src/catalog/composite.ts +5 -1
  56. package/src/catalog/contents-codec.ts +6 -1
  57. package/src/catalog/descriptors.ts +21 -29
  58. package/src/catalog/interface.ts +70 -55
  59. package/src/catalog/macro-overloads.ts +44 -0
  60. package/src/catalog/read-only.ts +63 -71
  61. package/src/catalog/unconstrained-columns.ts +23 -0
  62. package/src/client/client.ts +100 -58
  63. package/src/codec/asd.ts +3 -1
  64. package/src/functions/projection.ts +207 -0
  65. package/src/functions/table.ts +10 -0
  66. package/src/functions/types.ts +1 -0
  67. package/src/generated/vgi-client.ts +199 -37
  68. package/src/generated/vgi-protocol-schemas.ts +455 -41
  69. package/src/generated/vgi-protocol-types.ts +879 -5
  70. package/src/generated/vgi-service.ts +514 -42
  71. package/src/http/client-bundle.ts +1 -1
  72. package/src/http/fetch.ts +1 -1
  73. package/src/index.core.ts +4 -0
  74. package/src/protocol/dispatch.ts +9 -9
  75. package/src/protocol/handlers/aggregate.ts +1 -1
  76. package/src/protocol/handlers/catalog/admin.ts +10 -5
  77. package/src/protocol/handlers/catalog/index.ts +2 -2
  78. package/src/protocol/handlers/catalog/macro.ts +6 -5
  79. package/src/protocol/handlers/catalog/metadata.ts +16 -0
  80. package/src/protocol/handlers/catalog/table.ts +72 -26
  81. package/src/protocol/handlers/catalog/view.ts +24 -3
  82. package/src/protocol/handlers/function.ts +67 -1
  83. package/src/protocol/handlers/table-buffering.ts +1 -1
  84. package/src/protocol/handlers/unimplemented.ts +6 -6
  85. package/src/protocol/serializers/bind.ts +17 -1
  86. package/src/protocol/types.ts +6 -0
  87. package/src/rpc-server.ts +9 -9
  88. package/src/util/arrow/index.ts +1 -1
  89. package/src/worker.ts +1 -1
@@ -59,7 +59,7 @@ import { serializeAttachOptions } from "../catalog/attach-options.js";
59
59
  import { VgiClientError, wrapRpcWithErrorEnrichment } from "./errors.js";
60
60
  import { normalizeSchemaPath } from "../schema-path.js";
61
61
  import { encodeASD } from "../codec/asd.js";
62
- import { ClientCapabilitiesSchema, MacroCreateRequestSchema } from "../generated/vgi-protocol-schemas.js";
62
+ import { ClientCapabilitiesSchema, MacroCreateRequestSchema, TableCreateRequestSchema } from "../generated/vgi-protocol-schemas.js";
63
63
  import { decodeCatalogStreams } from "../catalog/contents-codec.js";
64
64
  import { decodeCatalogContentsResponse } from "../generated/vgi-protocol-types.js";
65
65
  import { deserializeInfoList, deserializeTags, toAsyncIterator } from "./helpers.js";
@@ -743,7 +743,7 @@ export class VgiClient {
743
743
  catalog_version_frozen: inner.catalog_version_frozen ?? false,
744
744
  catalog_version: Number(inner.catalog_version ?? 0),
745
745
  attach_opaque_data_required: inner.attach_opaque_data_required ?? true,
746
- default_schema: inner.default_schema ?? "main",
746
+ default_schema: inner.default_schema ?? null,
747
747
  settings: inner.settings
748
748
  ? (Array.isArray(inner.settings) ? inner.settings : [...inner.settings]).map(toUint8Array)
749
749
  : [],
@@ -1189,21 +1189,19 @@ export class VgiClient {
1189
1189
  name: string,
1190
1190
  columns: Uint8Array,
1191
1191
  onConflict: OnCreateConflict,
1192
- notNullConstraints?: number[],
1193
- uniqueConstraints?: number[][],
1194
- checkConstraints?: string[],
1192
+ constraintDefinitions: Uint8Array[] = [],
1195
1193
  transactionOpaqueData?: TransactionOpaqueData,
1196
1194
  ): Promise<void> {
1197
1195
  await this.rpc.call("catalog_table_create", {
1196
+ request: encodeASD(TableCreateRequestSchema, {
1198
1197
  attach_opaque_data: attachOpaqueData,
1199
1198
  schema_path: normalizeSchemaPath(schemaPath),
1200
1199
  name,
1201
1200
  columns,
1202
1201
  on_conflict: onConflictName(onConflict),
1203
- not_null_constraints: notNullConstraints ?? null,
1204
- unique_constraints: uniqueConstraints ?? null,
1205
- check_constraints: checkConstraints ?? null,
1202
+ constraint_definitions: constraintDefinitions,
1206
1203
  transaction_opaque_data: transactionOpaqueData ?? null,
1204
+ }),
1207
1205
  });
1208
1206
  }
1209
1207
 
@@ -1294,20 +1292,20 @@ export class VgiClient {
1294
1292
  attachOpaqueData: AttachOpaqueData,
1295
1293
  schemaPath: string[] | string,
1296
1294
  name: string,
1297
- columnName: string,
1298
- columnType: string,
1299
- defaultValue?: string | null,
1300
- ignoreNotFound?: boolean,
1295
+ columnPath: string[],
1296
+ columnDefinition: Uint8Array,
1297
+ ignoreNotFound: boolean = false,
1298
+ ifColumnNotExists: boolean = false,
1301
1299
  transactionOpaqueData?: TransactionOpaqueData,
1302
1300
  ): Promise<void> {
1303
1301
  await this.rpc.call("catalog_table_column_add", {
1304
1302
  attach_opaque_data: attachOpaqueData,
1305
1303
  schema_path: normalizeSchemaPath(schemaPath),
1306
1304
  name,
1307
- column_name: columnName,
1308
- column_type: columnType,
1309
- default_value: defaultValue ?? null,
1310
- ignore_not_found: ignoreNotFound ?? null,
1305
+ column_path: columnPath,
1306
+ column_definition: columnDefinition,
1307
+ ignore_not_found: ignoreNotFound,
1308
+ if_column_not_exists: ifColumnNotExists,
1311
1309
  transaction_opaque_data: transactionOpaqueData ?? null,
1312
1310
  });
1313
1311
  }
@@ -1317,16 +1315,20 @@ export class VgiClient {
1317
1315
  attachOpaqueData: AttachOpaqueData,
1318
1316
  schemaPath: string[] | string,
1319
1317
  name: string,
1320
- columnName: string,
1321
- ignoreNotFound?: boolean,
1318
+ columnPath: string[],
1319
+ ignoreNotFound: boolean = false,
1320
+ ifColumnExists: boolean = false,
1321
+ cascade: boolean = false,
1322
1322
  transactionOpaqueData?: TransactionOpaqueData,
1323
1323
  ): Promise<void> {
1324
1324
  await this.rpc.call("catalog_table_column_drop", {
1325
1325
  attach_opaque_data: attachOpaqueData,
1326
1326
  schema_path: normalizeSchemaPath(schemaPath),
1327
1327
  name,
1328
- column_name: columnName,
1329
- ignore_not_found: ignoreNotFound ?? null,
1328
+ column_path: columnPath,
1329
+ ignore_not_found: ignoreNotFound,
1330
+ if_column_exists: ifColumnExists,
1331
+ cascade: cascade,
1330
1332
  transaction_opaque_data: transactionOpaqueData ?? null,
1331
1333
  });
1332
1334
  }
@@ -1336,39 +1338,60 @@ export class VgiClient {
1336
1338
  attachOpaqueData: AttachOpaqueData,
1337
1339
  schemaPath: string[] | string,
1338
1340
  name: string,
1339
- columnName: string,
1340
- newName: string,
1341
- ignoreNotFound?: boolean,
1341
+ columnPath: string[],
1342
+ newColumnName: string,
1343
+ ignoreNotFound: boolean = false,
1342
1344
  transactionOpaqueData?: TransactionOpaqueData,
1343
1345
  ): Promise<void> {
1344
1346
  await this.rpc.call("catalog_table_column_rename", {
1345
1347
  attach_opaque_data: attachOpaqueData,
1346
1348
  schema_path: normalizeSchemaPath(schemaPath),
1347
1349
  name,
1348
- column_name: columnName,
1349
- new_name: newName,
1350
- ignore_not_found: ignoreNotFound ?? null,
1350
+ column_path: columnPath,
1351
+ new_column_name: newColumnName,
1352
+ ignore_not_found: ignoreNotFound,
1353
+ transaction_opaque_data: transactionOpaqueData ?? null,
1354
+ });
1355
+ }
1356
+
1357
+ /** Set or clear the comment on a column. */
1358
+ async tableColumnCommentSet(
1359
+ attachOpaqueData: AttachOpaqueData,
1360
+ schemaPath: string[] | string,
1361
+ name: string,
1362
+ columnPath: string[],
1363
+ comment: string | null,
1364
+ ignoreNotFound: boolean = false,
1365
+ transactionOpaqueData?: TransactionOpaqueData,
1366
+ ): Promise<void> {
1367
+ await this.rpc.call("catalog_table_column_comment_set", {
1368
+ attach_opaque_data: attachOpaqueData,
1369
+ schema_path: normalizeSchemaPath(schemaPath),
1370
+ name,
1371
+ column_path: columnPath,
1372
+ comment,
1373
+ ignore_not_found: ignoreNotFound,
1351
1374
  transaction_opaque_data: transactionOpaqueData ?? null,
1352
1375
  });
1353
1376
  }
1354
1377
 
1355
- /** Set the default value for a column. */
1378
+ /** Set the default using a serialized canonical SqlExpression record. */
1356
1379
  async tableColumnDefaultSet(
1357
1380
  attachOpaqueData: AttachOpaqueData,
1358
1381
  schemaPath: string[] | string,
1359
1382
  name: string,
1360
- columnName: string,
1361
- defaultValue: string,
1362
- ignoreNotFound?: boolean,
1383
+ columnPath: string[],
1384
+ expression: Uint8Array,
1385
+ ignoreNotFound: boolean = false,
1363
1386
  transactionOpaqueData?: TransactionOpaqueData,
1364
1387
  ): Promise<void> {
1365
1388
  await this.rpc.call("catalog_table_column_default_set", {
1366
1389
  attach_opaque_data: attachOpaqueData,
1367
1390
  schema_path: normalizeSchemaPath(schemaPath),
1368
1391
  name,
1369
- column_name: columnName,
1370
- default_value: defaultValue,
1371
- ignore_not_found: ignoreNotFound ?? null,
1392
+ column_path: columnPath,
1393
+ expression: expression,
1394
+ ignore_not_found: ignoreNotFound,
1372
1395
  transaction_opaque_data: transactionOpaqueData ?? null,
1373
1396
  });
1374
1397
  }
@@ -1378,16 +1401,16 @@ export class VgiClient {
1378
1401
  attachOpaqueData: AttachOpaqueData,
1379
1402
  schemaPath: string[] | string,
1380
1403
  name: string,
1381
- columnName: string,
1382
- ignoreNotFound?: boolean,
1404
+ columnPath: string[],
1405
+ ignoreNotFound: boolean = false,
1383
1406
  transactionOpaqueData?: TransactionOpaqueData,
1384
1407
  ): Promise<void> {
1385
1408
  await this.rpc.call("catalog_table_column_default_drop", {
1386
1409
  attach_opaque_data: attachOpaqueData,
1387
1410
  schema_path: normalizeSchemaPath(schemaPath),
1388
1411
  name,
1389
- column_name: columnName,
1390
- ignore_not_found: ignoreNotFound ?? null,
1412
+ column_path: columnPath,
1413
+ ignore_not_found: ignoreNotFound,
1391
1414
  transaction_opaque_data: transactionOpaqueData ?? null,
1392
1415
  });
1393
1416
  }
@@ -1397,24 +1420,27 @@ export class VgiClient {
1397
1420
  *
1398
1421
  * `columnDefinition` is a serialized Arrow Schema with a single field whose
1399
1422
  * name identifies the target column and whose type is the new column type.
1400
- * `expression` is an optional SQL expression used to convert existing values.
1423
+ * `expression` is a serialized canonical SqlExpression for USING, or null
1424
+ * when no conversion expression was supplied.
1401
1425
  */
1402
1426
  async tableColumnTypeChange(
1403
1427
  attachOpaqueData: AttachOpaqueData,
1404
1428
  schemaPath: string[] | string,
1405
1429
  name: string,
1430
+ columnPath: string[],
1406
1431
  columnDefinition: Uint8Array,
1407
- expression?: string | null,
1408
- ignoreNotFound?: boolean,
1432
+ expression: Uint8Array | null = null,
1433
+ ignoreNotFound: boolean = false,
1409
1434
  transactionOpaqueData?: TransactionOpaqueData,
1410
1435
  ): Promise<void> {
1411
1436
  await this.rpc.call("catalog_table_column_type_change", {
1412
1437
  attach_opaque_data: attachOpaqueData,
1413
1438
  schema_path: normalizeSchemaPath(schemaPath),
1414
1439
  name,
1440
+ column_path: columnPath,
1415
1441
  column_definition: columnDefinition,
1416
- expression: expression ?? null,
1417
- ignore_not_found: ignoreNotFound ?? false,
1442
+ expression: expression,
1443
+ ignore_not_found: ignoreNotFound,
1418
1444
  transaction_opaque_data: transactionOpaqueData ?? null,
1419
1445
  });
1420
1446
  }
@@ -1424,16 +1450,16 @@ export class VgiClient {
1424
1450
  attachOpaqueData: AttachOpaqueData,
1425
1451
  schemaPath: string[] | string,
1426
1452
  name: string,
1427
- columnName: string,
1428
- ignoreNotFound?: boolean,
1453
+ columnPath: string[],
1454
+ ignoreNotFound: boolean = false,
1429
1455
  transactionOpaqueData?: TransactionOpaqueData,
1430
1456
  ): Promise<void> {
1431
1457
  await this.rpc.call("catalog_table_not_null_set", {
1432
1458
  attach_opaque_data: attachOpaqueData,
1433
1459
  schema_path: normalizeSchemaPath(schemaPath),
1434
1460
  name,
1435
- column_name: columnName,
1436
- ignore_not_found: ignoreNotFound ?? null,
1461
+ column_path: columnPath,
1462
+ ignore_not_found: ignoreNotFound,
1437
1463
  transaction_opaque_data: transactionOpaqueData ?? null,
1438
1464
  });
1439
1465
  }
@@ -1443,16 +1469,16 @@ export class VgiClient {
1443
1469
  attachOpaqueData: AttachOpaqueData,
1444
1470
  schemaPath: string[] | string,
1445
1471
  name: string,
1446
- columnName: string,
1447
- ignoreNotFound?: boolean,
1472
+ columnPath: string[],
1473
+ ignoreNotFound: boolean = false,
1448
1474
  transactionOpaqueData?: TransactionOpaqueData,
1449
1475
  ): Promise<void> {
1450
1476
  await this.rpc.call("catalog_table_not_null_drop", {
1451
1477
  attach_opaque_data: attachOpaqueData,
1452
1478
  schema_path: normalizeSchemaPath(schemaPath),
1453
1479
  name,
1454
- column_name: columnName,
1455
- ignore_not_found: ignoreNotFound ?? null,
1480
+ column_path: columnPath,
1481
+ ignore_not_found: ignoreNotFound,
1456
1482
  transaction_opaque_data: transactionOpaqueData ?? null,
1457
1483
  });
1458
1484
  }
@@ -1484,12 +1510,22 @@ export class VgiClient {
1484
1510
  definition: string,
1485
1511
  onConflict: OnCreateConflict,
1486
1512
  transactionOpaqueData?: TransactionOpaqueData,
1513
+ aliases?: string[],
1514
+ security: "regular" | "barrier" = "regular",
1515
+ comment?: string | null,
1516
+ tags?: Record<string, string> | null,
1517
+ columnComments?: Record<string, string> | null,
1487
1518
  ): Promise<void> {
1488
1519
  await this.rpc.call("catalog_view_create", {
1489
1520
  attach_opaque_data: attachOpaqueData,
1490
1521
  schema_path: normalizeSchemaPath(schemaPath),
1491
1522
  name,
1492
1523
  definition,
1524
+ aliases: aliases ?? null,
1525
+ security,
1526
+ comment: comment ?? null,
1527
+ tags: tags ?? null,
1528
+ column_comments: columnComments ?? null,
1493
1529
  on_conflict: onConflictName(onConflict),
1494
1530
  transaction_opaque_data: transactionOpaqueData ?? null,
1495
1531
  });
@@ -1556,6 +1592,18 @@ export class VgiClient {
1556
1592
  // Macro Catalog API
1557
1593
  // ==========================================================================
1558
1594
 
1595
+ /** Set or clear a comment on one raw view output name. */
1596
+ async viewColumnCommentSet(
1597
+ attachOpaqueData: AttachOpaqueData, schemaPath: string[] | string, name: string, columnPath: string[],
1598
+ comment?: string | null, ignoreNotFound = false, transactionOpaqueData?: TransactionOpaqueData,
1599
+ ): Promise<void> {
1600
+ await this.rpc.call("catalog_view_column_comment_set", {
1601
+ attach_opaque_data: attachOpaqueData, schema_path: normalizeSchemaPath(schemaPath), name,
1602
+ column_path: columnPath, comment: comment ?? null, ignore_not_found: ignoreNotFound,
1603
+ transaction_opaque_data: transactionOpaqueData ?? null,
1604
+ });
1605
+ }
1606
+
1559
1607
  /** Get a macro by name, or null if not found. */
1560
1608
  async macroGet(
1561
1609
  attachOpaqueData: AttachOpaqueData,
@@ -1599,11 +1647,8 @@ export class VgiClient {
1599
1647
  schemaPath: string[] | string,
1600
1648
  name: string,
1601
1649
  macroType: MacroType,
1602
- parameters: string[],
1603
- definition: string,
1650
+ overloads: import("../generated/vgi-client.js").MacroOverload[],
1604
1651
  onConflict: OnCreateConflict,
1605
- parameterDefaultValues?: Uint8Array | null,
1606
- argumentsSchema?: Uint8Array | null,
1607
1652
  transactionOpaqueData?: TransactionOpaqueData,
1608
1653
  ): Promise<void> {
1609
1654
  const innerBatch = batchFromColumns(
@@ -1612,11 +1657,8 @@ export class VgiClient {
1612
1657
  schema_path: [normalizeSchemaPath(schemaPath)],
1613
1658
  name: [name],
1614
1659
  macro_type: [macroType],
1615
- parameters: [parameters],
1616
- definition: [definition],
1660
+ overloads: [overloads],
1617
1661
  on_conflict: [onConflictName(onConflict)],
1618
- parameter_default_values: [parameterDefaultValues ?? null],
1619
- arguments_schema: [argumentsSchema ?? null],
1620
1662
  transaction_opaque_data: [transactionOpaqueData ?? null],
1621
1663
  },
1622
1664
  MacroCreateRequestSchema,
package/src/codec/asd.ts CHANGED
@@ -189,7 +189,9 @@ function normalizeValue(raw: any, type: VgiDataType): any {
189
189
  const valueType = ((entries as any).children as VgiField[])[1].type;
190
190
  const out: Record<string, any> = {};
191
191
  const put = (k: unknown, v: unknown): void => {
192
- out[String(k)] = normalizeValue(v, valueType);
192
+ Object.defineProperty(out, String(k), {
193
+ value: normalizeValue(v, valueType), enumerable: true, writable: true, configurable: true,
194
+ });
193
195
  };
194
196
  // arrow-js MapRow is iterable of [k,v]; flechette returns [[k,v],...] arrays.
195
197
  if (raw[Symbol.iterator]) {
@@ -0,0 +1,207 @@
1
+ // Copyright 2025, 2026 Query Farm LLC - https://query.farm
2
+ import {
3
+ schema,
4
+ serializeSchema,
5
+ deserializeBatch,
6
+ type VgiSchema,
7
+ } from "../arrow/index.js";
8
+ import { validateEncoded } from "../catalog/column-expressions.js";
9
+ import { decodeSqlExpression } from "../generated/vgi-protocol-types.js";
10
+ import { batchToScalarDict } from "../util/arrow/index.js";
11
+ import type { BindRequest, BindResponse } from "../protocol/types.js";
12
+ import {
13
+ normalizeBindRepeatability,
14
+ parseBindRepeatability,
15
+ } from "../protocol/serializers/bind.js";
16
+ export const SCALAR_PROJECTION_PROFILE = "vgi.expression.duckdb.scalar.v1";
17
+ export interface ProjectionProposal {
18
+ bindCall: BindRequest;
19
+ bindResult: BindResponse;
20
+ expressions: Uint8Array[];
21
+ resultSchema: VgiSchema;
22
+ }
23
+ export interface ProjectionAcceptance {
24
+ acceptedIndices: number[];
25
+ bindResult: BindResponse | null;
26
+ }
27
+ /** Strict envelope boundary for the projection RPC and its original bind records. */
28
+ export function projectionRecord(
29
+ bytes: Uint8Array,
30
+ expected: VgiSchema,
31
+ ): Record<string, any> {
32
+ if (!bytes.length || bytes.length > 8 * 1024 * 1024)
33
+ throw new Error("invalid projection record size");
34
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
35
+ let offset = 0,
36
+ schemas = 0,
37
+ batches = 0;
38
+ while (offset < bytes.length) {
39
+ if (bytes.length - offset < 4)
40
+ throw new Error("truncated projection IPC prefix");
41
+ const prefix = view.getInt32(offset, true) === -1 ? 8 : 4;
42
+ if (bytes.length - offset < prefix)
43
+ throw new Error("truncated projection IPC prefix");
44
+ const length = view.getInt32(offset + prefix - 4, true);
45
+ offset += prefix;
46
+ if (length === 0) {
47
+ break;
48
+ }
49
+ if (length < 0 || offset + length > bytes.length)
50
+ throw new Error("invalid projection IPC message");
51
+ const table = offset + view.getUint32(offset, true);
52
+ const vtable = table - view.getInt32(table, true);
53
+ const slot = (id: number) =>
54
+ 4 + id * 2 < view.getUint16(vtable, true)
55
+ ? view.getUint16(vtable + 4 + id * 2, true)
56
+ : 0;
57
+ const kindSlot = slot(1),
58
+ bodySlot = slot(3);
59
+ const kind = kindSlot ? view.getUint8(table + kindSlot) : 0;
60
+ const body = bodySlot
61
+ ? Number(view.getBigInt64(table + bodySlot, true))
62
+ : 0;
63
+ if (
64
+ body < 0 ||
65
+ body > bytes.length - offset - length ||
66
+ !Number.isSafeInteger(body)
67
+ )
68
+ throw new Error("invalid projection IPC body");
69
+ if (kind === 1 && schemas === 0 && batches === 0) schemas++;
70
+ else if (kind === 2 && schemas === 1 && batches === 0) {
71
+ /* dictionary before record */
72
+ } else if (kind === 3 && schemas === 1 && batches === 0) batches++;
73
+ else throw new Error("projection requires one complete IPC batch");
74
+ offset += length + body;
75
+ }
76
+ // Arrow permits physical EOF after a complete message, with or without EOS.
77
+ if (offset !== bytes.length || schemas !== 1 || batches !== 1)
78
+ throw new Error("projection requires one complete IPC batch");
79
+ const batch = deserializeBatch(bytes);
80
+ // IPC dictionary ids are stream-local and may be reassigned on re-encoding.
81
+ // Compare the exact Arrow shape and metadata instead of those transport ids.
82
+ const metadata = (m: ReadonlyMap<string, string>) =>
83
+ [...m].sort(([a], [b]) => a.localeCompare(b));
84
+ const fieldShape = (f: any): unknown => [
85
+ f.name,
86
+ f.nullable,
87
+ String(f.type),
88
+ metadata(f.metadata),
89
+ (f.type.children ?? []).map(fieldShape),
90
+ ];
91
+ const shape = (s: VgiSchema) =>
92
+ JSON.stringify([s.fields.map(fieldShape), metadata(s.metadata)]);
93
+ if (batch.numRows !== 1 || shape(batch.schema) !== shape(expected))
94
+ throw new Error("invalid projection record schema/row count");
95
+ for (let i = 0; i < expected.fields.length; i++)
96
+ if (!expected.fields[i]!.nullable && batch.getChildAt(i)?.get(0) == null)
97
+ throw new Error("required projection field is NULL");
98
+ return batchToScalarDict(batch);
99
+ }
100
+ export function validateProjectionOffer(result: BindResponse): void {
101
+ const profiles = result.projection_expression_profiles ?? [];
102
+ if (
103
+ profiles.length > 64 ||
104
+ new Set(profiles).size !== profiles.length ||
105
+ profiles.some(
106
+ (p) =>
107
+ typeof p !== "string" ||
108
+ p.length > 256 ||
109
+ !/^[a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)*\.v[1-9][0-9]*$/.test(p),
110
+ )
111
+ )
112
+ throw new Error("unsupported projection profile offer");
113
+ }
114
+ export function validateProjectionProposal(p: ProjectionProposal): void {
115
+ parseBindRepeatability(p.bindResult.repeatability);
116
+ validateProjectionOffer(p.bindResult);
117
+ if (
118
+ p.bindCall.function_type !== "table" ||
119
+ p.bindCall.input_schema !== null ||
120
+ p.bindCall.schema_path == null
121
+ )
122
+ throw new Error("projection requires an ordinary table and explicit owner");
123
+ if (
124
+ [
125
+ p.bindResult.lookup_secret_types,
126
+ p.bindResult.lookup_scopes,
127
+ p.bindResult.lookup_names,
128
+ ].some((v) => v?.length)
129
+ )
130
+ throw new Error("projection cannot request secrets");
131
+ if (
132
+ p.expressions.length < 1 ||
133
+ p.expressions.length > 1024 ||
134
+ p.resultSchema.fields.length !== p.expressions.length ||
135
+ p.resultSchema.metadata.size
136
+ )
137
+ throw new Error("invalid projection result schema/count");
138
+ const names = [
139
+ ...p.bindResult.output_schema.fields,
140
+ ...p.resultSchema.fields,
141
+ ].map((f) => f.name.replace(/[A-Z]/g, (c) => c.toLowerCase()));
142
+ if (
143
+ names.some((n) => !n || n.includes("\0")) ||
144
+ new Set(names).size !== names.length
145
+ )
146
+ throw new Error("ambiguous projection column names");
147
+ let context: string | undefined;
148
+ for (const raw of p.expressions) {
149
+ validateEncoded(
150
+ btoa(Array.from(raw, (b) => String.fromCharCode(b)).join("")),
151
+ p.bindCall.schema_path,
152
+ );
153
+ const expression = decodeSqlExpression(raw);
154
+ if (
155
+ !(p.bindResult.projection_expression_profiles ?? []).includes(
156
+ expression.profile,
157
+ )
158
+ )
159
+ throw new Error("projection profile was not offered");
160
+ const next = JSON.stringify(expression.evaluation_context);
161
+ if (context !== undefined && next !== context)
162
+ throw new Error("projection contexts differ");
163
+ context = next;
164
+ }
165
+ }
166
+ export function validateProjectionAcceptance(
167
+ p: ProjectionProposal,
168
+ r: ProjectionAcceptance,
169
+ ): void {
170
+ validateProjectionProposal(p);
171
+ if ((r.acceptedIndices.length === 0) !== (r.bindResult === null))
172
+ throw new Error("projection indices and bind result must occur together");
173
+ let previous = -1;
174
+ for (const i of r.acceptedIndices) {
175
+ if (!Number.isSafeInteger(i) || i <= previous || i >= p.expressions.length)
176
+ throw new Error("invalid accepted projection index");
177
+ previous = i;
178
+ }
179
+ if (!r.bindResult) return;
180
+ validateProjectionOffer(r.bindResult);
181
+ if (
182
+ normalizeBindRepeatability(r.bindResult.repeatability) !==
183
+ p.bindResult.repeatability
184
+ )
185
+ throw new Error("accepted projection changed bind repeatability");
186
+ if (
187
+ JSON.stringify(r.bindResult.projection_expression_profiles ?? []) !==
188
+ JSON.stringify(p.bindResult.projection_expression_profiles ?? []) ||
189
+ [
190
+ r.bindResult.lookup_secret_types,
191
+ r.bindResult.lookup_scopes,
192
+ r.bindResult.lookup_names,
193
+ ].some((v) => v?.length)
194
+ )
195
+ throw new Error("accepted projection changed bind offer/secrets");
196
+ const expected = schema(
197
+ [
198
+ ...p.bindResult.output_schema.fields,
199
+ ...r.acceptedIndices.map((i) => p.resultSchema.fields[i]),
200
+ ],
201
+ p.bindResult.output_schema.metadata,
202
+ );
203
+ const a = serializeSchema(expected),
204
+ b = serializeSchema(r.bindResult.output_schema);
205
+ if (a.length !== b.length || a.some((v, i) => v !== b[i]))
206
+ throw new Error("accepted projection changed append-only schema");
207
+ }
@@ -21,6 +21,7 @@ import type {
21
21
  import type {
22
22
  BindRequest,
23
23
  BindResponse,
24
+ BindRepeatability,
24
25
  InitRequest,
25
26
  GlobalInitResponse,
26
27
  TableFunctionCardinalityRequest,
@@ -216,6 +217,8 @@ export interface TableFunctionConfig<
216
217
  | {
217
218
  outputSchema: VgiSchema;
218
219
  opaqueData?: Uint8Array;
220
+ projectionExpressionProfiles?: string[];
221
+ repeatability?: BindRepeatability;
219
222
  lookupSecretTypes?: string[];
220
223
  lookupScopes?: string[];
221
224
  lookupNames?: string[];
@@ -223,10 +226,13 @@ export interface TableFunctionConfig<
223
226
  | Promise<{
224
227
  outputSchema: VgiSchema;
225
228
  opaqueData?: Uint8Array;
229
+ projectionExpressionProfiles?: string[];
230
+ repeatability?: BindRepeatability;
226
231
  lookupSecretTypes?: string[];
227
232
  lookupScopes?: string[];
228
233
  lookupNames?: string[];
229
234
  }>;
235
+ projection?: (params: TableBindParams<TArgs>, proposal: import("./projection.js").ProjectionProposal) => import("./projection.js").ProjectionAcceptance | Promise<import("./projection.js").ProjectionAcceptance>;
230
236
  /** Init (optional). May be async — common when storage is HTTP-backed. */
231
237
  onInit?: (params: {
232
238
  args: TArgs;
@@ -460,12 +466,16 @@ export function defineTableFunction<
460
466
  return {
461
467
  output_schema: result.outputSchema,
462
468
  opaque_data: result.opaqueData ?? null,
469
+ projection_expression_profiles: result.projectionExpressionProfiles ?? [],
470
+ repeatability: result.repeatability === undefined ? "UNKNOWN" : result.repeatability,
463
471
  lookup_secret_types: result.lookupSecretTypes,
464
472
  lookup_scopes: result.lookupScopes,
465
473
  lookup_names: result.lookupNames,
466
474
  };
467
475
  },
468
476
 
477
+ projection: config.projection ? (proposal) => config.projection!({args: extractArgs(proposal.bindCall), bindCall:proposal.bindCall, settings:batchToScalarDict(proposal.bindCall.settings),secrets:batchToSecretDict(proposal.bindCall.secrets),resolvedSecretsProvided:proposal.bindCall.resolved_secrets_provided},proposal) : undefined,
478
+
469
479
  async globalInit(request: InitRequest): Promise<GlobalInitResponse> {
470
480
  const executionId = new Uint8Array(16);
471
481
  crypto.getRandomValues(executionId);
@@ -273,6 +273,7 @@ export interface VgiFunction {
273
273
  * function is not split-capable: the whole scan is one unit of work, and the
274
274
  * framework answers with a single empty-payload split.
275
275
  */
276
+ projection?(proposal: import("./projection.js").ProjectionProposal): import("./projection.js").ProjectionAcceptance | Promise<import("./projection.js").ProjectionAcceptance>;
276
277
  plan?(request: import("../protocol/serializers/splits.js").TableFunctionPlanRequest):
277
278
  | import("../protocol/serializers/splits.js").PlanResult
278
279
  | Promise<import("../protocol/serializers/splits.js").PlanResult>;