@query-farm/vgi 0.9.0 → 0.11.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.
@@ -3827,6 +3827,13 @@ class CatalogReadOnlyError extends VgiError {
3827
3827
  this.name = "CatalogReadOnlyError";
3828
3828
  }
3829
3829
  }
3830
+ class ArgumentValidationError extends VgiError {
3831
+ constructor(message) {
3832
+ super(message);
3833
+ this.name = "ArgumentValidationError";
3834
+ }
3835
+ }
3836
+
3830
3837
  class NoCatalogError extends VgiError {
3831
3838
  constructor() {
3832
3839
  super("No catalog is configured for this worker");
@@ -4966,6 +4973,187 @@ init_arrow();
4966
4973
 
4967
4974
  // src/functions/aggregate.ts
4968
4975
  init_arrow();
4976
+
4977
+ // src/arguments/argument-spec.ts
4978
+ init_arrow();
4979
+ function safeJson(value) {
4980
+ try {
4981
+ const encoded = JSON.stringify(value);
4982
+ if (encoded === undefined) {
4983
+ return JSON.stringify(String(value));
4984
+ }
4985
+ return encoded;
4986
+ } catch {
4987
+ return JSON.stringify(String(value));
4988
+ }
4989
+ }
4990
+ function formatRange(ge, le, gt, lt) {
4991
+ if (ge === undefined && le === undefined && gt === undefined && lt === undefined) {
4992
+ return;
4993
+ }
4994
+ let low;
4995
+ if (gt !== undefined) {
4996
+ low = `(${gt}`;
4997
+ } else if (ge !== undefined) {
4998
+ low = `[${ge}`;
4999
+ } else {
5000
+ low = "(-inf";
5001
+ }
5002
+ let high;
5003
+ if (lt !== undefined) {
5004
+ high = `${lt})`;
5005
+ } else if (le !== undefined) {
5006
+ high = `${le}]`;
5007
+ } else {
5008
+ high = "+inf)";
5009
+ }
5010
+ return `${low}, ${high}`;
5011
+ }
5012
+ function constraintSpecFields(constraints) {
5013
+ const fields = {};
5014
+ if (constraints === undefined)
5015
+ return fields;
5016
+ if ("default" in constraints && constraints.default !== undefined) {
5017
+ fields.defaultJson = safeJson(constraints.default);
5018
+ }
5019
+ if (constraints.choices !== undefined) {
5020
+ fields.choicesJson = safeJson([...constraints.choices]);
5021
+ }
5022
+ const range = formatRange(constraints.ge, constraints.le, constraints.gt, constraints.lt);
5023
+ if (range !== undefined) {
5024
+ fields.rangeNotation = range;
5025
+ }
5026
+ if (constraints.pattern !== undefined) {
5027
+ fields.pattern = constraints.pattern;
5028
+ }
5029
+ return fields;
5030
+ }
5031
+ function toNumber(value) {
5032
+ if (typeof value === "number")
5033
+ return value;
5034
+ if (typeof value === "bigint")
5035
+ return Number(value);
5036
+ return;
5037
+ }
5038
+ function choiceEquals(choice, value) {
5039
+ const cn = toNumber(choice);
5040
+ const vn = toNumber(value);
5041
+ if (cn !== undefined && vn !== undefined)
5042
+ return cn === vn;
5043
+ return choice === value;
5044
+ }
5045
+ function validateConstConstraints(name, constraints, value) {
5046
+ if (value === null || value === undefined)
5047
+ return;
5048
+ const num = toNumber(value);
5049
+ if (num !== undefined) {
5050
+ if (constraints.ge !== undefined && num < constraints.ge) {
5051
+ throw new ArgumentValidationError(`argument '${name}' must be >= ${constraints.ge}`);
5052
+ }
5053
+ if (constraints.le !== undefined && num > constraints.le) {
5054
+ throw new ArgumentValidationError(`argument '${name}' must be <= ${constraints.le}`);
5055
+ }
5056
+ if (constraints.gt !== undefined && num <= constraints.gt) {
5057
+ throw new ArgumentValidationError(`argument '${name}' must be > ${constraints.gt}`);
5058
+ }
5059
+ if (constraints.lt !== undefined && num >= constraints.lt) {
5060
+ throw new ArgumentValidationError(`argument '${name}' must be < ${constraints.lt}`);
5061
+ }
5062
+ }
5063
+ if (constraints.choices !== undefined && constraints.choices.length > 0) {
5064
+ if (!constraints.choices.some((c) => choiceEquals(c, value))) {
5065
+ throw new ArgumentValidationError(`argument '${name}' must be one of ${JSON.stringify([...constraints.choices])}`);
5066
+ }
5067
+ }
5068
+ if (constraints.pattern !== undefined && typeof value === "string") {
5069
+ let re;
5070
+ try {
5071
+ re = new RegExp(constraints.pattern);
5072
+ } catch {
5073
+ re = undefined;
5074
+ }
5075
+ if (re !== undefined && !re.test(value)) {
5076
+ throw new ArgumentValidationError(`argument '${name}' must match pattern ${JSON.stringify(constraints.pattern)}`);
5077
+ }
5078
+ }
5079
+ }
5080
+ function argumentSpecSortKey(spec) {
5081
+ if (typeof spec.position === "number") {
5082
+ return [0, spec.position];
5083
+ }
5084
+ return [1, spec.position];
5085
+ }
5086
+ function argumentSpecsToSchema(specs) {
5087
+ const sorted = [...specs].sort((a, b) => {
5088
+ const ka = argumentSpecSortKey(a);
5089
+ const kb = argumentSpecSortKey(b);
5090
+ if (ka[0] !== kb[0])
5091
+ return ka[0] - kb[0];
5092
+ if (typeof ka[1] === "number" && typeof kb[1] === "number") {
5093
+ return ka[1] - kb[1];
5094
+ }
5095
+ return String(ka[1]).localeCompare(String(kb[1]));
5096
+ });
5097
+ const fields = [];
5098
+ for (const spec of sorted) {
5099
+ const metadata = new Map;
5100
+ if (typeof spec.position === "string") {
5101
+ metadata.set(VGI_ARG_KEY, VGI_ARG_NAMED);
5102
+ }
5103
+ if (spec.isTableInput) {
5104
+ metadata.set(VGI_TYPE_KEY, VGI_TYPE_TABLE);
5105
+ } else if (spec.isAnyType) {
5106
+ metadata.set(VGI_TYPE_KEY, VGI_TYPE_ANY);
5107
+ }
5108
+ if (spec.isVarargs) {
5109
+ metadata.set(VGI_VARARGS_KEY, VGI_VARARGS_TRUE);
5110
+ }
5111
+ if (spec.isConst) {
5112
+ metadata.set(VGI_CONST_KEY, VGI_CONST_TRUE);
5113
+ }
5114
+ if (spec.doc) {
5115
+ metadata.set(VGI_DOC_KEY, spec.doc);
5116
+ }
5117
+ if (spec.defaultJson !== undefined) {
5118
+ metadata.set(VGI_DEFAULT_KEY, spec.defaultJson);
5119
+ }
5120
+ if (spec.choicesJson !== undefined) {
5121
+ metadata.set(VGI_CHOICES_KEY, spec.choicesJson);
5122
+ }
5123
+ if (spec.rangeNotation !== undefined) {
5124
+ metadata.set(VGI_RANGE_KEY, spec.rangeNotation);
5125
+ }
5126
+ if (spec.pattern !== undefined) {
5127
+ metadata.set(VGI_PATTERN_KEY, spec.pattern);
5128
+ }
5129
+ const field3 = field2(spec.name, spec.arrowType, true, metadata.size > 0 ? metadata : undefined);
5130
+ fields.push(field3);
5131
+ }
5132
+ return schema2(fields);
5133
+ }
5134
+ function macroArgumentsSchema(parameters, parameterDefaultValues, parameterDocs) {
5135
+ const docs = parameterDocs ?? {};
5136
+ const defaultTypes = new Map;
5137
+ if (parameterDefaultValues && parameterDefaultValues.length > 0) {
5138
+ const batch = deserializeBatch(parameterDefaultValues);
5139
+ for (const f of batch.schema.fields) {
5140
+ defaultTypes.set(f.name, f.type);
5141
+ }
5142
+ }
5143
+ const fields = [];
5144
+ for (const name of parameters) {
5145
+ const metadata = new Map;
5146
+ const doc = docs[name];
5147
+ if (doc) {
5148
+ metadata.set(VGI_DOC_KEY, doc);
5149
+ }
5150
+ const fieldType = defaultTypes.get(name) ?? nullType2();
5151
+ fields.push(field2(name, fieldType, true, metadata.size > 0 ? metadata : undefined));
5152
+ }
5153
+ return schema2(fields);
5154
+ }
5155
+
5156
+ // src/functions/aggregate.ts
4969
5157
  var _NODE_FS_MOD = "node:fs";
4970
5158
  var _NODE_PATH_MOD = "node:path";
4971
5159
  var _NODE_OS_MOD = "node:os";
@@ -5096,7 +5284,8 @@ function defineAggregate(config) {
5096
5284
  arrowType: isAnyType2 ? nullType2() : type,
5097
5285
  isAnyType: isAnyType2,
5098
5286
  isVarargs: varargsSet.has(name),
5099
- isConst
5287
+ isConst,
5288
+ ...constraintSpecFields(config.argConstraints?.[name])
5100
5289
  });
5101
5290
  }
5102
5291
  }
@@ -5342,8 +5531,9 @@ function extractArgMap(cfg, args) {
5342
5531
  }
5343
5532
  }
5344
5533
  for (let i = 0;i < constNames.length; i++) {
5534
+ let v;
5345
5535
  try {
5346
- let v = args.get(i);
5536
+ v = args.get(i);
5347
5537
  const field3 = args.argumentsSchema?.fields.find((f) => f.name === `positional_${i}`);
5348
5538
  if (field3 && isDecimal(field3.type)) {
5349
5539
  const scale = field3.type.scale;
@@ -5354,12 +5544,17 @@ function extractArgMap(cfg, args) {
5354
5544
  } else if (typeof v === "bigint") {
5355
5545
  v = Number(v);
5356
5546
  }
5357
- out[constNames[i]] = v;
5358
5547
  } catch {
5359
5548
  const def = cfg.argDefaults?.[constNames[i]];
5360
5549
  if (def !== undefined)
5361
5550
  out[constNames[i]] = def;
5551
+ continue;
5552
+ }
5553
+ const constraints = cfg.argConstraints?.[constNames[i]];
5554
+ if (constraints !== undefined) {
5555
+ validateConstConstraints(constNames[i], constraints, v);
5362
5556
  }
5557
+ out[constNames[i]] = v;
5363
5558
  }
5364
5559
  if (cfg.argDefaults) {
5365
5560
  for (const [name, def] of Object.entries(cfg.argDefaults)) {
@@ -6989,138 +7184,6 @@ function buildVgiProtocol(config) {
6989
7184
 
6990
7185
  // src/functions/scalar.ts
6991
7186
  init_arrow();
6992
-
6993
- // src/arguments/argument-spec.ts
6994
- init_arrow();
6995
- function safeJson(value) {
6996
- try {
6997
- const encoded = JSON.stringify(value);
6998
- if (encoded === undefined) {
6999
- return JSON.stringify(String(value));
7000
- }
7001
- return encoded;
7002
- } catch {
7003
- return JSON.stringify(String(value));
7004
- }
7005
- }
7006
- function formatRange(ge, le, gt, lt) {
7007
- if (ge === undefined && le === undefined && gt === undefined && lt === undefined) {
7008
- return;
7009
- }
7010
- let low;
7011
- if (gt !== undefined) {
7012
- low = `(${gt}`;
7013
- } else if (ge !== undefined) {
7014
- low = `[${ge}`;
7015
- } else {
7016
- low = "(-inf";
7017
- }
7018
- let high;
7019
- if (lt !== undefined) {
7020
- high = `${lt})`;
7021
- } else if (le !== undefined) {
7022
- high = `${le}]`;
7023
- } else {
7024
- high = "+inf)";
7025
- }
7026
- return `${low}, ${high}`;
7027
- }
7028
- function constraintSpecFields(constraints) {
7029
- const fields = {};
7030
- if (constraints === undefined)
7031
- return fields;
7032
- if ("default" in constraints && constraints.default !== undefined) {
7033
- fields.defaultJson = safeJson(constraints.default);
7034
- }
7035
- if (constraints.choices !== undefined) {
7036
- fields.choicesJson = safeJson([...constraints.choices]);
7037
- }
7038
- const range = formatRange(constraints.ge, constraints.le, constraints.gt, constraints.lt);
7039
- if (range !== undefined) {
7040
- fields.rangeNotation = range;
7041
- }
7042
- if (constraints.pattern !== undefined) {
7043
- fields.pattern = constraints.pattern;
7044
- }
7045
- return fields;
7046
- }
7047
- function argumentSpecSortKey(spec) {
7048
- if (typeof spec.position === "number") {
7049
- return [0, spec.position];
7050
- }
7051
- return [1, spec.position];
7052
- }
7053
- function argumentSpecsToSchema(specs) {
7054
- const sorted = [...specs].sort((a, b) => {
7055
- const ka = argumentSpecSortKey(a);
7056
- const kb = argumentSpecSortKey(b);
7057
- if (ka[0] !== kb[0])
7058
- return ka[0] - kb[0];
7059
- if (typeof ka[1] === "number" && typeof kb[1] === "number") {
7060
- return ka[1] - kb[1];
7061
- }
7062
- return String(ka[1]).localeCompare(String(kb[1]));
7063
- });
7064
- const fields = [];
7065
- for (const spec of sorted) {
7066
- const metadata = new Map;
7067
- if (typeof spec.position === "string") {
7068
- metadata.set(VGI_ARG_KEY, VGI_ARG_NAMED);
7069
- }
7070
- if (spec.isTableInput) {
7071
- metadata.set(VGI_TYPE_KEY, VGI_TYPE_TABLE);
7072
- } else if (spec.isAnyType) {
7073
- metadata.set(VGI_TYPE_KEY, VGI_TYPE_ANY);
7074
- }
7075
- if (spec.isVarargs) {
7076
- metadata.set(VGI_VARARGS_KEY, VGI_VARARGS_TRUE);
7077
- }
7078
- if (spec.isConst) {
7079
- metadata.set(VGI_CONST_KEY, VGI_CONST_TRUE);
7080
- }
7081
- if (spec.doc) {
7082
- metadata.set(VGI_DOC_KEY, spec.doc);
7083
- }
7084
- if (spec.defaultJson !== undefined) {
7085
- metadata.set(VGI_DEFAULT_KEY, spec.defaultJson);
7086
- }
7087
- if (spec.choicesJson !== undefined) {
7088
- metadata.set(VGI_CHOICES_KEY, spec.choicesJson);
7089
- }
7090
- if (spec.rangeNotation !== undefined) {
7091
- metadata.set(VGI_RANGE_KEY, spec.rangeNotation);
7092
- }
7093
- if (spec.pattern !== undefined) {
7094
- metadata.set(VGI_PATTERN_KEY, spec.pattern);
7095
- }
7096
- const field3 = field2(spec.name, spec.arrowType, true, metadata.size > 0 ? metadata : undefined);
7097
- fields.push(field3);
7098
- }
7099
- return schema2(fields);
7100
- }
7101
- function macroArgumentsSchema(parameters, parameterDefaultValues, parameterDocs) {
7102
- const docs = parameterDocs ?? {};
7103
- const defaultTypes = new Map;
7104
- if (parameterDefaultValues && parameterDefaultValues.length > 0) {
7105
- const batch = deserializeBatch(parameterDefaultValues);
7106
- for (const f of batch.schema.fields) {
7107
- defaultTypes.set(f.name, f.type);
7108
- }
7109
- }
7110
- const fields = [];
7111
- for (const name of parameters) {
7112
- const metadata = new Map;
7113
- const doc = docs[name];
7114
- if (doc) {
7115
- metadata.set(VGI_DOC_KEY, doc);
7116
- }
7117
- const fieldType = defaultTypes.get(name) ?? nullType2();
7118
- fields.push(field2(name, fieldType, true, metadata.size > 0 ? metadata : undefined));
7119
- }
7120
- return schema2(fields);
7121
- }
7122
-
7123
- // src/functions/scalar.ts
7124
7187
  function defineScalarFunction(config) {
7125
7188
  const repr = config.repr ?? "rich";
7126
7189
  const specs = [];
@@ -7164,6 +7227,14 @@ function defineScalarFunction(config) {
7164
7227
  }
7165
7228
  }
7166
7229
  }
7230
+ const constConstraints = new Map;
7231
+ if (config.parameters) {
7232
+ for (const p of config.parameters) {
7233
+ if (p.const && (p.choices !== undefined || p.ge !== undefined || p.le !== undefined || p.gt !== undefined || p.lt !== undefined || p.pattern !== undefined)) {
7234
+ constConstraints.set(p.name, p);
7235
+ }
7236
+ }
7237
+ }
7167
7238
  const meta = {
7168
7239
  name: config.name,
7169
7240
  description: config.description,
@@ -7190,7 +7261,11 @@ function defineScalarFunction(config) {
7190
7261
  let constIdx = 0;
7191
7262
  for (const spec of specs) {
7192
7263
  if (spec.isConst) {
7193
- constArgs[spec.name] = request.arguments.get(constIdx, undefined);
7264
+ const value = request.arguments.get(constIdx, undefined);
7265
+ constArgs[spec.name] = value;
7266
+ const constraints = constConstraints.get(spec.name);
7267
+ if (constraints)
7268
+ validateConstConstraints(spec.name, constraints, value);
7194
7269
  constIdx++;
7195
7270
  }
7196
7271
  }
@@ -7338,7 +7413,8 @@ function defineTableFunction(config) {
7338
7413
  arrowType: isAny ? nullType2() : type,
7339
7414
  isAnyType: isAny,
7340
7415
  isVarargs: varargsSet.has(name),
7341
- doc: config.argDocs?.[name]
7416
+ doc: config.argDocs?.[name],
7417
+ ...constraintSpecFields(config.argConstraints?.[name])
7342
7418
  });
7343
7419
  }
7344
7420
  }
@@ -7377,6 +7453,9 @@ function defineTableFunction(config) {
7377
7453
  }
7378
7454
  if (typeof val === "bigint")
7379
7455
  val = safeNumber(val);
7456
+ const constraints = config.argConstraints?.[spec.name];
7457
+ if (constraints)
7458
+ validateConstConstraints(spec.name, constraints, val);
7380
7459
  args[spec.name] = val;
7381
7460
  }
7382
7461
  return args;
@@ -8863,4 +8942,4 @@ export {
8863
8942
  Arguments
8864
8943
  };
8865
8944
 
8866
- //# debugId=5A27AF075D6D5AD464756E2164756E21
8945
+ //# debugId=781A5FE31E548B0664756E2164756E21