@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.
@@ -9990,6 +9990,11 @@ async function sealTransaction(plaintext, attachEnvelope, auth, signingKey) {
9990
9990
  return plaintext;
9991
9991
  return sealBytes(plaintext, signingKey, transactionAad(auth, attachEnvelope), TRANSACTION_ENVELOPE_VERSION);
9992
9992
  }
9993
+ async function openTransaction(envelope, attachEnvelope, auth, signingKey) {
9994
+ if (!signingKey || envelope.length === 0)
9995
+ return envelope;
9996
+ return openBytes(envelope, signingKey, transactionAad(auth, attachEnvelope), TRANSACTION_ENVELOPE_VERSION, "transaction_opaque_data");
9997
+ }
9993
9998
  async function unwrapParamsOpaque(params, ctx, signingKey) {
9994
9999
  const auth = ctx?.auth;
9995
10000
  let sealedAttach = new Uint8Array(0);
@@ -10270,7 +10275,12 @@ async function liveSplitAnchor(catalogInterface, bindCall, auth, signingKey) {
10270
10275
  if (!catalogInterface?.version || bindCall.attach_opaque_data == null)
10271
10276
  return;
10272
10277
  const raw = toUint8Array(bindCall.attach_opaque_data);
10273
- const txn = bindCall.transaction_opaque_data ? toUint8Array(bindCall.transaction_opaque_data) : undefined;
10278
+ let txn = bindCall.transaction_opaque_data ? toUint8Array(bindCall.transaction_opaque_data) : undefined;
10279
+ if (txn && txn.length > 0 && signingKey) {
10280
+ try {
10281
+ txn = await openTransaction(txn, raw, auth, signingKey);
10282
+ } catch {}
10283
+ }
10274
10284
  const forms = [
10275
10285
  async () => raw,
10276
10286
  async () => openAttach(raw, auth, signingKey)
@@ -10308,14 +10318,22 @@ function functionHandlers(config) {
10308
10318
  return env;
10309
10319
  return openAttach(env, ctx?.auth ?? currentRequestAuth(), signingKey);
10310
10320
  }
10321
+ async function openOpaque(bindCall, ctx) {
10322
+ const sealedAttach = bindCall.attach_opaque_data != null ? toUint8Array(bindCall.attach_opaque_data) : new Uint8Array(0);
10323
+ if (bindCall.transaction_opaque_data != null) {
10324
+ bindCall.transaction_opaque_data = await openTransaction(toUint8Array(bindCall.transaction_opaque_data), sealedAttach, ctx?.auth ?? currentRequestAuth(), signingKey);
10325
+ }
10326
+ bindCall.attach_opaque_data = await stripAttach(bindCall.attach_opaque_data, ctx);
10327
+ }
10311
10328
  async function strippedContext(bindCall, ctx) {
10312
- let attach = null;
10329
+ const copy = { ...bindCall };
10313
10330
  try {
10314
- attach = await stripAttach(bindCall.attach_opaque_data, ctx);
10331
+ await openOpaque(copy, ctx);
10315
10332
  } catch {
10316
- attach = null;
10333
+ copy.attach_opaque_data = null;
10334
+ copy.transaction_opaque_data = null;
10317
10335
  }
10318
- return overloadContext({ ...bindCall, attach_opaque_data: attach }, catalogInterface);
10336
+ return overloadContext(copy, catalogInterface);
10319
10337
  }
10320
10338
  const emptySchema = schema2([]);
10321
10339
  const dummyInputSchema = schema2([
@@ -10324,7 +10342,7 @@ function functionHandlers(config) {
10324
10342
  const bind = async (params, ctx) => {
10325
10343
  const innerParams = unwrapRequest(params.request);
10326
10344
  const request = deserializeBindRequest(innerParams);
10327
- request.attach_opaque_data = await stripAttach(request.attach_opaque_data, ctx);
10345
+ await openOpaque(request, ctx);
10328
10346
  const func = registry.get(request.function_name, overloadContext(request, catalogInterface));
10329
10347
  const response = await func.bind(request);
10330
10348
  validateProjectionOffer(response);
@@ -10339,7 +10357,7 @@ function functionHandlers(config) {
10339
10357
  const innerParams = unwrapRequest(params.request);
10340
10358
  const request = deserializeInitRequest(innerParams);
10341
10359
  const auth = ctx?.auth ?? currentRequestAuth();
10342
- request.bind_call.attach_opaque_data = await stripAttach(request.bind_call.attach_opaque_data, ctx);
10360
+ await openOpaque(request.bind_call, ctx);
10343
10361
  await resolveSplitPayloads(request, innerParams, {
10344
10362
  auth,
10345
10363
  signingKey,
@@ -10384,7 +10402,7 @@ function functionHandlers(config) {
10384
10402
  } else {
10385
10403
  const carried = parseCarriedInitRequest(state.initRequestPacked);
10386
10404
  const request = carried.request;
10387
- request.bind_call.attach_opaque_data = await stripAttach(request.bind_call.attach_opaque_data, undefined);
10405
+ await openOpaque(request.bind_call, undefined);
10388
10406
  await resolveSplitPayloads(request, { bind_call: carried.bindCallIpc }, {
10389
10407
  auth: currentRequestAuth(),
10390
10408
  signingKey,
@@ -12804,6 +12822,15 @@ class CatalogInterface {
12804
12822
  tableScanFunctionGet(attachOpaqueData, schemaPath, name, atUnit, atValue, transactionOpaqueData) {
12805
12823
  throw new CatalogReadOnlyError("table_scan_function_get");
12806
12824
  }
12825
+ tableInsertFunctionGet(attachOpaqueData, schemaPath, name, transactionOpaqueData, writableBranchFunctionName) {
12826
+ throw new CatalogReadOnlyError("table_insert_function_get");
12827
+ }
12828
+ tableUpdateFunctionGet(attachOpaqueData, schemaPath, name, transactionOpaqueData) {
12829
+ throw new CatalogReadOnlyError("table_update_function_get");
12830
+ }
12831
+ tableDeleteFunctionGet(attachOpaqueData, schemaPath, name, transactionOpaqueData) {
12832
+ throw new CatalogReadOnlyError("table_delete_function_get");
12833
+ }
12807
12834
  tableScanBranchesGet(attachOpaqueData, schemaPath, name, atUnit, atValue, transactionOpaqueData) {
12808
12835
  const legacy = this.tableScanFunctionGet(attachOpaqueData, schemaPath, name, atUnit, atValue, transactionOpaqueData);
12809
12836
  return isPromise(legacy) ? legacy.then((r) => singleBranchResult(r)) : singleBranchResult(legacy);
@@ -13754,6 +13781,19 @@ function catalogTableHandlers(getCatalog, signingKey) {
13754
13781
  const scanResult = await cat.tableScanFunctionGet(toUint8Array(params.attach_opaque_data), params.schema_path, params.name, params.at_unit, params.at_value, params.transaction_opaque_data ? toUint8Array(params.transaction_opaque_data) : undefined);
13755
13782
  return wrapResult(scanResult, ScanFunctionResultSchema);
13756
13783
  });
13784
+ const optionalTx = (params) => params.transaction_opaque_data ? toUint8Array(params.transaction_opaque_data) : undefined;
13785
+ const catalogTableInsertFunctionGet = catalogHandler(signingKey, async (params) => {
13786
+ const result = await getCatalog().tableInsertFunctionGet(toUint8Array(params.attach_opaque_data), params.schema_path, params.name, optionalTx(params), params.writable_branch_function_name ?? null);
13787
+ return wrapResult(result, ScanFunctionResultSchema);
13788
+ });
13789
+ const catalogTableUpdateFunctionGet = catalogHandler(signingKey, async (params) => {
13790
+ const result = await getCatalog().tableUpdateFunctionGet(toUint8Array(params.attach_opaque_data), params.schema_path, params.name, optionalTx(params));
13791
+ return wrapResult(result, ScanFunctionResultSchema);
13792
+ });
13793
+ const catalogTableDeleteFunctionGet = catalogHandler(signingKey, async (params) => {
13794
+ const result = await getCatalog().tableDeleteFunctionGet(toUint8Array(params.attach_opaque_data), params.schema_path, params.name, optionalTx(params));
13795
+ return wrapResult(result, ScanFunctionResultSchema);
13796
+ });
13757
13797
  const catalogTableScanBranchesGet = catalogHandler(signingKey, async (params) => {
13758
13798
  const cat = getCatalog();
13759
13799
  const branchesResult = await cat.tableScanBranchesGet(toUint8Array(params.attach_opaque_data), params.schema_path, params.name, params.at_unit, params.at_value, params.transaction_opaque_data ? toUint8Array(params.transaction_opaque_data) : undefined);
@@ -13832,6 +13872,9 @@ function catalogTableHandlers(getCatalog, signingKey) {
13832
13872
  catalogTableDrop,
13833
13873
  catalogTableColumnStatisticsGet,
13834
13874
  catalogTableScanFunctionGet,
13875
+ catalogTableInsertFunctionGet,
13876
+ catalogTableUpdateFunctionGet,
13877
+ catalogTableDeleteFunctionGet,
13835
13878
  catalogTableScanBranchesGet,
13836
13879
  catalogTableCommentSet,
13837
13880
  catalogTableRename,
@@ -14326,6 +14369,24 @@ function cacheControlMetadata(cc, extra) {
14326
14369
  md.set(CACHE_PER_VALUE_KEY, "1");
14327
14370
  return md;
14328
14371
  }
14372
+ // src/write-results.ts
14373
+ var WRITE_OPERATIONS = ["insert", "update", "delete"];
14374
+ var MODE_RANK = { count: 0, rows: 1, changes: 2 };
14375
+ function validateWriteResultModes(modes) {
14376
+ const out = {};
14377
+ const unknown = Object.keys(modes).filter((op) => !WRITE_OPERATIONS.includes(op)).sort();
14378
+ if (unknown.length)
14379
+ throw new Error(`Unknown write operation(s): ${unknown.join(", ")}`);
14380
+ for (const [operation, mode] of Object.entries(modes)) {
14381
+ if (mode === undefined)
14382
+ continue;
14383
+ if (!(mode in MODE_RANK))
14384
+ throw new Error(`Unknown write result mode '${mode}' for '${operation}'`);
14385
+ out[operation] = mode;
14386
+ }
14387
+ return out;
14388
+ }
14389
+
14329
14390
  // src/functions/scalar.ts
14330
14391
  init_arrow();
14331
14392
  function defineScalarFunction(config) {
@@ -15526,6 +15587,9 @@ class ReadOnlyCatalogInterface extends CatalogInterface {
15526
15587
  this._registry = registry;
15527
15588
  if (options.perAttachItemIds)
15528
15589
  this.perAttachItemIds = true;
15590
+ for (const sch of descriptor.schemas ?? [])
15591
+ for (const tbl of sch.tables ?? [])
15592
+ validateWritableTable(tbl);
15529
15593
  for (const sch of descriptor.schemas ?? []) {
15530
15594
  for (const fn of sch.functions ?? [])
15531
15595
  registry.registerInSchema(fn, schemaDescriptorPath(sch), descriptor.name);
@@ -15574,8 +15638,9 @@ class ReadOnlyCatalogInterface extends CatalogInterface {
15574
15638
  const settings = (this._descriptor.settings ?? []).map((s) => serializeSetting(s));
15575
15639
  const secretTypes = (this._descriptor.secretTypes ?? []).map((s) => serializeSecretType(s));
15576
15640
  const attachCatalogs = (this._descriptor.attachCatalogs ?? []).map((c) => encodeAttachCatalogInfo(c));
15577
- const available = this.catalogReadProfiles();
15578
- const read = selectReadProfiles(available, clientCapabilities);
15641
+ const available = catalogCapabilities({ read: this.catalogReadProfiles(), write: this.catalogWriteProfiles() });
15642
+ const advertised = available.read.length + available.write.length > 0;
15643
+ const selected = selectCatalogCapabilities(available, clientCapabilities);
15579
15644
  const hasTimeTravel = this._descriptor.schemas.some((s) => s.tables?.some((t) => t.supportsTimeTravel) ?? false);
15580
15645
  return buildCatalogAttachResult({
15581
15646
  attach_opaque_data: attachOpaqueData,
@@ -15596,17 +15661,21 @@ class ReadOnlyCatalogInterface extends CatalogInterface {
15596
15661
  resolved_data_version: null,
15597
15662
  resolved_implementation_version: null,
15598
15663
  supports_catalog_contents: this.supportsCatalogContents,
15599
- catalog_capabilities: available.length ? encodeCatalogCapabilities({ read: [...available].sort(), ddl: [], write: [], transaction: [] }) : null,
15600
- selected_capabilities: available.length ? encodeCatalogCapabilities({ read, ddl: [], write: [], transaction: [] }) : null
15664
+ catalog_capabilities: advertised ? encodeCatalogCapabilities(available) : null,
15665
+ selected_capabilities: advertised ? encodeCatalogCapabilities(selected) : null
15601
15666
  });
15602
15667
  }
15603
15668
  catalogReadProfiles() {
15604
15669
  const hasConstraints = this._descriptor.schemas.some((s) => s.tables?.some((t) => (t.constraints?.length ?? 0) > 0) ?? false);
15605
15670
  return hasConstraints ? [CONSTRAINT_RECORDS_READ, ROW_SCALAR_EXPRESSION] : [];
15606
15671
  }
15672
+ catalogWriteProfiles() {
15673
+ const writableConstrained = this._descriptor.schemas.some((s) => s.tables?.some((t) => (t.constraints?.length ?? 0) > 0 && (t.insertFunction || t.updateFunction || t.deleteFunction)) ?? false);
15674
+ return writableConstrained ? [CONSTRAINT_ENFORCE] : [];
15675
+ }
15607
15676
  catalogCapabilities(_attachOpaqueData) {
15608
- const read = this.catalogReadProfiles();
15609
- return read.length ? catalogCapabilities({ read }) : null;
15677
+ const caps = catalogCapabilities({ read: this.catalogReadProfiles(), write: this.catalogWriteProfiles() });
15678
+ return caps.read.length + caps.write.length ? caps : null;
15610
15679
  }
15611
15680
  detach(attachOpaqueData) {
15612
15681
  for (const [name, id] of this._attachments) {
@@ -15757,12 +15826,12 @@ class ReadOnlyCatalogInterface extends CatalogInterface {
15757
15826
  columns,
15758
15827
  constraints: [...constraints],
15759
15828
  definition_revision: t.definitionRevision ?? null,
15760
- write_result_modes: {},
15829
+ write_result_modes: writeResultModesFor(t),
15761
15830
  supports_column_statistics: t.statistics != null && Object.keys(t.statistics).length > 0,
15762
15831
  scan_function: t.function && !t.columns ? inlineScanFunction(t.function, t.arguments, this._schemaForFunction(t.function)) : null,
15763
- insert_function: null,
15764
- update_function: null,
15765
- delete_function: null,
15832
+ insert_function: t.insertFunction ? inlineScanFunction(t.insertFunction, undefined, this._schemaForFunction(t.insertFunction)) : null,
15833
+ update_function: t.updateFunction ? inlineScanFunction(t.updateFunction, undefined, this._schemaForFunction(t.updateFunction)) : null,
15834
+ delete_function: t.deleteFunction ? inlineScanFunction(t.deleteFunction, undefined, this._schemaForFunction(t.deleteFunction)) : null,
15766
15835
  cardinality_estimate: t.inlinedCardinality ? Number(t.inlinedCardinality.estimate) : null,
15767
15836
  cardinality_max: t.inlinedCardinality ? Number(t.inlinedCardinality.max) : null,
15768
15837
  required_filters: requiredFilters
@@ -16024,6 +16093,32 @@ class ReadOnlyCatalogInterface extends CatalogInterface {
16024
16093
  }
16025
16094
  throw new Error(`Table '${name}' is not function-backed`);
16026
16095
  }
16096
+ _writeFunctionGet(schemaPath, name, operation) {
16097
+ const schema = this._descriptor.schemas.find((sch) => schemaPathsEqual(schemaDescriptorPath(sch), schemaPath));
16098
+ const table = schema?.tables?.find((t) => t.name.toLowerCase() === name.toLowerCase());
16099
+ if (!table) {
16100
+ throw new VgiError(`Table '${name}' not found in schema ${schemaPathDisplay(schemaPath)}`, "NOT_FOUND");
16101
+ }
16102
+ const fn = writeFunctionOf(table, operation);
16103
+ if (!fn) {
16104
+ throw new CatalogReadOnlyError(`Table ${schemaPathDisplay(schemaPath)}.${table.name} does not support ${operation.toUpperCase()}`);
16105
+ }
16106
+ return {
16107
+ function_name: fn.meta.name,
16108
+ arguments: serializeArgsBatch(new Arguments, argumentSpecsToSchema(fn.argumentSpecs)),
16109
+ required_extensions: [],
16110
+ schema_path: this._schemaForFunction(fn)
16111
+ };
16112
+ }
16113
+ tableInsertFunctionGet(_attachOpaqueData, schemaPath, name, _transactionOpaqueData, _writableBranchFunctionName) {
16114
+ return this._writeFunctionGet(schemaPath, name, "insert");
16115
+ }
16116
+ tableUpdateFunctionGet(_attachOpaqueData, schemaPath, name, _transactionOpaqueData) {
16117
+ return this._writeFunctionGet(schemaPath, name, "update");
16118
+ }
16119
+ tableDeleteFunctionGet(_attachOpaqueData, schemaPath, name, _transactionOpaqueData) {
16120
+ return this._writeFunctionGet(schemaPath, name, "delete");
16121
+ }
16027
16122
  async tableGet(attachOpaqueData, schemaPath, name, atUnit, atValue, transactionOpaqueData) {
16028
16123
  validateAtParams(atUnit, atValue);
16029
16124
  const schema = this._descriptor.schemas.find((s) => schemaPathsEqual(schemaDescriptorPath(s), schemaPath));
@@ -16269,6 +16364,28 @@ function sameSpelling(left, right) {
16269
16364
  const b = normalizeSchemaPath(right);
16270
16365
  return a.length === b.length && a.every((part, i) => part === b[i]);
16271
16366
  }
16367
+ function writeFunctionOf(table, operation) {
16368
+ return operation === "insert" ? table.insertFunction : operation === "update" ? table.updateFunction : table.deleteFunction;
16369
+ }
16370
+ function validateWritableTable(table) {
16371
+ const modes = validateWriteResultModes(table.writeResultModes ?? {});
16372
+ for (const operation of Object.keys(modes)) {
16373
+ if (!writeFunctionOf(table, operation)) {
16374
+ throw new Error(`Table '${table.name}': writeResultModes declares '${operation}' without a corresponding ${operation}Function`);
16375
+ }
16376
+ }
16377
+ if ((table.updateFunction || table.deleteFunction) && !table.function) {
16378
+ throw new Error(`Table '${table.name}': updateFunction and deleteFunction require a scan function (set 'function') to provide row ids`);
16379
+ }
16380
+ }
16381
+ function writeResultModesFor(table) {
16382
+ const out = {};
16383
+ for (const operation of WRITE_OPERATIONS) {
16384
+ if (writeFunctionOf(table, operation))
16385
+ out[operation] = table.writeResultModes?.[operation] ?? "count";
16386
+ }
16387
+ return out;
16388
+ }
16272
16389
  // src/catalog/indexes.ts
16273
16390
  var NO_OPTIONS_B64 = "/////zgAAAAQAAAAAAAKABAADgAHAAgACgAAAAAAAAEQAAAAAAAEAAgACAAAAAQACAAAAAQAAAAAAAAAAAAAAP////9QAAAAFAAAAAAAAAAAAAoADgAMAAcACAAKAAAAAAAAAxAAAAAEAAoAGAAMAAgABAAKAAAAFAAAABgAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/////AAAAAA==";
16274
16391
  function emptyIndexOptions() {
@@ -17043,4 +17160,4 @@ export {
17043
17160
  validateTableConstraints
17044
17161
  };
17045
17162
 
17046
- //# debugId=701969C8B78DAA6864756E2164756E21
17163
+ //# debugId=FAC9CCEE4111237264756E2164756E21