@peridotvault/peridotbase-sdk 0.0.0-stage → 0.2.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.
- package/dist/auth.d.ts +33 -0
- package/dist/auth.js +72 -0
- package/dist/client.d.ts +1149 -0
- package/dist/client.js +6030 -0
- package/dist/ddl.d.ts +31 -0
- package/dist/ddl.js +28 -0
- package/dist/errors.d.ts +32 -0
- package/dist/errors.js +187 -0
- package/dist/expr.d.ts +127 -0
- package/dist/expr.js +1177 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +28 -0
- package/dist/org-multisig.d.ts +29 -0
- package/dist/org-multisig.js +90 -0
- package/dist/pda.d.ts +95 -0
- package/dist/pda.js +327 -0
- package/dist/sql.d.ts +244 -0
- package/dist/sql.js +1523 -0
- package/dist/tooling.d.ts +68 -0
- package/dist/tooling.js +432 -0
- package/dist/v3.d.ts +357 -0
- package/dist/v3.js +1649 -0
- package/dist/values.d.ts +229 -0
- package/dist/values.js +1362 -0
- package/dist/verify.d.ts +37 -0
- package/dist/verify.js +210 -0
- package/idl/peridotbase_core.json +17404 -0
- package/package.json +43 -4
- package/README.md +0 -3
package/dist/expr.js
ADDED
|
@@ -0,0 +1,1177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.expr = exports.MAX_EXPR_DEPTH = exports.MAX_EXPR_STEPS = exports.MAX_EXPR_BYTES = void 0;
|
|
4
|
+
exports.compileExpression = compileExpression;
|
|
5
|
+
exports.evalRangeOp = evalRangeOp;
|
|
6
|
+
exports.evalArithmetic = evalArithmetic;
|
|
7
|
+
exports.compareValues = compareValues;
|
|
8
|
+
exports.evaluate = evaluate;
|
|
9
|
+
exports.castValue = castValue;
|
|
10
|
+
exports.callFunction = callFunction;
|
|
11
|
+
exports.evaluateBytecode = evaluateBytecode;
|
|
12
|
+
exports.predicatePasses = predicatePasses;
|
|
13
|
+
exports.checkPasses = checkPasses;
|
|
14
|
+
const values_1 = require("./values");
|
|
15
|
+
/** Bytecode limits mirrored from the Rust expression engine. */
|
|
16
|
+
exports.MAX_EXPR_BYTES = 256;
|
|
17
|
+
exports.MAX_EXPR_STEPS = 512;
|
|
18
|
+
exports.MAX_EXPR_DEPTH = 64;
|
|
19
|
+
exports.expr = {
|
|
20
|
+
col(index, side = "new") {
|
|
21
|
+
return { k: "col", index, side };
|
|
22
|
+
},
|
|
23
|
+
lit(value) {
|
|
24
|
+
return { k: "lit", value };
|
|
25
|
+
},
|
|
26
|
+
eq(a, b) {
|
|
27
|
+
return { k: "cmp", op: "eq", a, b };
|
|
28
|
+
},
|
|
29
|
+
ne(a, b) {
|
|
30
|
+
return { k: "cmp", op: "ne", a, b };
|
|
31
|
+
},
|
|
32
|
+
lt(a, b) {
|
|
33
|
+
return { k: "cmp", op: "lt", a, b };
|
|
34
|
+
},
|
|
35
|
+
le(a, b) {
|
|
36
|
+
return { k: "cmp", op: "le", a, b };
|
|
37
|
+
},
|
|
38
|
+
gt(a, b) {
|
|
39
|
+
return { k: "cmp", op: "gt", a, b };
|
|
40
|
+
},
|
|
41
|
+
ge(a, b) {
|
|
42
|
+
return { k: "cmp", op: "ge", a, b };
|
|
43
|
+
},
|
|
44
|
+
and(a, b) {
|
|
45
|
+
return { k: "and", a, b };
|
|
46
|
+
},
|
|
47
|
+
or(a, b) {
|
|
48
|
+
return { k: "or", a, b };
|
|
49
|
+
},
|
|
50
|
+
not(a) {
|
|
51
|
+
return { k: "not", a };
|
|
52
|
+
},
|
|
53
|
+
isNull(a) {
|
|
54
|
+
return { k: "isnull", a };
|
|
55
|
+
},
|
|
56
|
+
isNotNull(a) {
|
|
57
|
+
return { k: "isnotnull", a };
|
|
58
|
+
},
|
|
59
|
+
add(a, b) {
|
|
60
|
+
return { k: "arith", op: "add", a, b };
|
|
61
|
+
},
|
|
62
|
+
sub(a, b) {
|
|
63
|
+
return { k: "arith", op: "sub", a, b };
|
|
64
|
+
},
|
|
65
|
+
mul(a, b) {
|
|
66
|
+
return { k: "arith", op: "mul", a, b };
|
|
67
|
+
},
|
|
68
|
+
div(a, b) {
|
|
69
|
+
return { k: "arith", op: "div", a, b };
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
const CMP_OPCODE = {
|
|
73
|
+
eq: 0x14,
|
|
74
|
+
ne: 0x15,
|
|
75
|
+
lt: 0x16,
|
|
76
|
+
le: 0x17,
|
|
77
|
+
gt: 0x18,
|
|
78
|
+
ge: 0x19,
|
|
79
|
+
};
|
|
80
|
+
const RANGE_OPCODE = {
|
|
81
|
+
contains: 0x1a,
|
|
82
|
+
contained: 0x1b,
|
|
83
|
+
overlaps: 0x1c,
|
|
84
|
+
adjacent: 0x1d,
|
|
85
|
+
leftOf: 0x23,
|
|
86
|
+
rightOf: 0x24,
|
|
87
|
+
};
|
|
88
|
+
const ARITH_OPCODE = {
|
|
89
|
+
add: 0x28,
|
|
90
|
+
sub: 0x29,
|
|
91
|
+
mul: 0x2a,
|
|
92
|
+
div: 0x2b,
|
|
93
|
+
};
|
|
94
|
+
/** Calendar opcodes (mirrors `OP_*_INTERVAL` / `OP_*_DIFF` in expr.rs). */
|
|
95
|
+
const CALENDAR_OPCODE = {
|
|
96
|
+
dateAdd: 0x25,
|
|
97
|
+
tsAdd: 0x26,
|
|
98
|
+
dateSub: 0x27,
|
|
99
|
+
tsSub: 0x2c,
|
|
100
|
+
intervalAdd: 0x2d,
|
|
101
|
+
intervalSub: 0x2e,
|
|
102
|
+
dateDiff: 0x2f,
|
|
103
|
+
tsDiff: 0x30,
|
|
104
|
+
};
|
|
105
|
+
function isIntervalValue(value) {
|
|
106
|
+
return typeof value === "object" && value !== null && "interval" in value;
|
|
107
|
+
}
|
|
108
|
+
function calendarOpcode(op, left, right) {
|
|
109
|
+
if (op === "mul" || op === "div")
|
|
110
|
+
return undefined;
|
|
111
|
+
if (left === "date" && right === "interval") {
|
|
112
|
+
return op === "add" ? CALENDAR_OPCODE.dateAdd : CALENDAR_OPCODE.dateSub;
|
|
113
|
+
}
|
|
114
|
+
if (left === "timestamp" && right === "interval") {
|
|
115
|
+
return op === "add" ? CALENDAR_OPCODE.tsAdd : CALENDAR_OPCODE.tsSub;
|
|
116
|
+
}
|
|
117
|
+
if (left === "interval" && right === "interval") {
|
|
118
|
+
return op === "add" ? CALENDAR_OPCODE.intervalAdd : CALENDAR_OPCODE.intervalSub;
|
|
119
|
+
}
|
|
120
|
+
if (op === "sub" && left === "date" && right === "date")
|
|
121
|
+
return CALENDAR_OPCODE.dateDiff;
|
|
122
|
+
if (op === "sub" && left === "timestamp" && right === "timestamp")
|
|
123
|
+
return CALENDAR_OPCODE.tsDiff;
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
function inferType(node, columns) {
|
|
127
|
+
if (node.k === "col")
|
|
128
|
+
return columns[node.index]?.type;
|
|
129
|
+
if (node.k === "lit") {
|
|
130
|
+
const v = node.value;
|
|
131
|
+
if (v === null || v === undefined)
|
|
132
|
+
return undefined;
|
|
133
|
+
if (typeof v === "boolean")
|
|
134
|
+
return "bool";
|
|
135
|
+
if (typeof v === "number" || typeof v === "bigint")
|
|
136
|
+
return "i64";
|
|
137
|
+
if (typeof v === "string")
|
|
138
|
+
return "text";
|
|
139
|
+
if (v instanceof Uint8Array)
|
|
140
|
+
return "uuid";
|
|
141
|
+
if (isIntervalValue(v))
|
|
142
|
+
return "interval";
|
|
143
|
+
if (typeof v === "object" && "range" in v)
|
|
144
|
+
return "range";
|
|
145
|
+
if (typeof v === "object" && "decimal" in v)
|
|
146
|
+
return "decimal";
|
|
147
|
+
}
|
|
148
|
+
if (node.k === "rangep")
|
|
149
|
+
return "bool";
|
|
150
|
+
if (node.k === "arith")
|
|
151
|
+
return inferType(node.a, columns) ?? inferType(node.b, columns);
|
|
152
|
+
if (node.k === "cast")
|
|
153
|
+
return node.type;
|
|
154
|
+
if (node.k === "coalesce") {
|
|
155
|
+
for (const arg of node.args) {
|
|
156
|
+
const type = inferType(arg, columns);
|
|
157
|
+
if (type !== undefined)
|
|
158
|
+
return type;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (node.k === "nullif")
|
|
162
|
+
return inferType(node.a, columns) ?? inferType(node.b, columns);
|
|
163
|
+
if (node.k === "case") {
|
|
164
|
+
if (node.operand)
|
|
165
|
+
return inferType(node.operand, columns);
|
|
166
|
+
for (const entry of node.whens) {
|
|
167
|
+
const type = inferType(entry.then, columns);
|
|
168
|
+
if (type !== undefined)
|
|
169
|
+
return type;
|
|
170
|
+
}
|
|
171
|
+
return node.otherwise ? inferType(node.otherwise, columns) : undefined;
|
|
172
|
+
}
|
|
173
|
+
if (node.k === "func") {
|
|
174
|
+
if (["length", "round", "abs"].includes(node.fn)) {
|
|
175
|
+
return node.args[0] ? inferType(node.args[0], columns) : undefined;
|
|
176
|
+
}
|
|
177
|
+
return "text";
|
|
178
|
+
}
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
class Writer {
|
|
182
|
+
constructor() {
|
|
183
|
+
this.chunks = [];
|
|
184
|
+
}
|
|
185
|
+
push(...bytes) {
|
|
186
|
+
this.chunks.push(...bytes);
|
|
187
|
+
}
|
|
188
|
+
u16(value) {
|
|
189
|
+
this.push(value & 0xff, (value >> 8) & 0xff);
|
|
190
|
+
}
|
|
191
|
+
i32(value) {
|
|
192
|
+
const buffer = Buffer.alloc(4);
|
|
193
|
+
buffer.writeInt32LE(value);
|
|
194
|
+
this.push(...buffer);
|
|
195
|
+
}
|
|
196
|
+
i64(value) {
|
|
197
|
+
const buffer = Buffer.alloc(8);
|
|
198
|
+
buffer.writeBigInt64LE(value);
|
|
199
|
+
this.push(...buffer);
|
|
200
|
+
}
|
|
201
|
+
i128(value) {
|
|
202
|
+
let v = BigInt.asUintN(128, value);
|
|
203
|
+
for (let i = 0; i < 16; i += 1) {
|
|
204
|
+
this.push(Number(v & 0xffn));
|
|
205
|
+
v >>= 8n;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
bytes(value) {
|
|
209
|
+
this.push(...Array.from(value));
|
|
210
|
+
}
|
|
211
|
+
get length() {
|
|
212
|
+
return this.chunks.length;
|
|
213
|
+
}
|
|
214
|
+
patchU16(at, value) {
|
|
215
|
+
this.chunks[at] = value & 0xff;
|
|
216
|
+
this.chunks[at + 1] = (value >> 8) & 0xff;
|
|
217
|
+
}
|
|
218
|
+
toBuffer() {
|
|
219
|
+
return Buffer.from(this.chunks);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function literalTypeOf(value) {
|
|
223
|
+
if (value === null || value === undefined)
|
|
224
|
+
return undefined;
|
|
225
|
+
if (typeof value === "boolean")
|
|
226
|
+
return "bool";
|
|
227
|
+
if (typeof value === "bigint")
|
|
228
|
+
return "i64";
|
|
229
|
+
if (typeof value === "number") {
|
|
230
|
+
// JS numbers reaching the compiler are decimal literals with fractions.
|
|
231
|
+
return Number.isInteger(value) ? "i64" : "decimal";
|
|
232
|
+
}
|
|
233
|
+
if (typeof value === "string") {
|
|
234
|
+
// A caller may pass a decimal string ("19.99"); the compiler only needs
|
|
235
|
+
// the target column to coerce it, so leave strings as text here.
|
|
236
|
+
return "text";
|
|
237
|
+
}
|
|
238
|
+
if (value instanceof Uint8Array)
|
|
239
|
+
return "uuid";
|
|
240
|
+
if (isIntervalValue(value))
|
|
241
|
+
return "interval";
|
|
242
|
+
if (typeof value === "object" && "range" in value)
|
|
243
|
+
return "range";
|
|
244
|
+
if (typeof value === "object" && "decimal" in value)
|
|
245
|
+
return "decimal";
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
function compileNode(node, columns, expected) {
|
|
249
|
+
const writer = new Writer();
|
|
250
|
+
switch (node.k) {
|
|
251
|
+
case "lit": {
|
|
252
|
+
const type = expected ?? literalTypeOf(node.value);
|
|
253
|
+
if (node.value === null || node.value === undefined) {
|
|
254
|
+
writer.push(0x01);
|
|
255
|
+
return writer;
|
|
256
|
+
}
|
|
257
|
+
let value;
|
|
258
|
+
if (type !== undefined) {
|
|
259
|
+
const scale = type === "decimal"
|
|
260
|
+
? (columns.find((c) => c.type === "decimal")?.scale ?? 0)
|
|
261
|
+
: 0;
|
|
262
|
+
value = (0, values_1.coerceValue)(node.value, { type, scale });
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
value = node.value;
|
|
266
|
+
}
|
|
267
|
+
if (typeof value === "boolean") {
|
|
268
|
+
writer.push(0x02, value ? 1 : 0);
|
|
269
|
+
}
|
|
270
|
+
else if (typeof value === "bigint" || typeof value === "number") {
|
|
271
|
+
writer.push(0x03);
|
|
272
|
+
writer.i64(BigInt(value.toString()));
|
|
273
|
+
}
|
|
274
|
+
else if (typeof value === "string") {
|
|
275
|
+
const bytes = Buffer.from(value, "utf8");
|
|
276
|
+
writer.push(0x05);
|
|
277
|
+
writer.u16(bytes.length);
|
|
278
|
+
writer.bytes(bytes);
|
|
279
|
+
}
|
|
280
|
+
else if (value instanceof Uint8Array) {
|
|
281
|
+
writer.push(0x06);
|
|
282
|
+
writer.bytes(Buffer.from(value));
|
|
283
|
+
}
|
|
284
|
+
else if (value && typeof value === "object" && "range" in value) {
|
|
285
|
+
const parsed = value.range;
|
|
286
|
+
writer.push(0x07);
|
|
287
|
+
writer.i64(BigInt(parsed.start));
|
|
288
|
+
writer.i64(BigInt(parsed.end));
|
|
289
|
+
writer.push(parsed.flags & 0xff);
|
|
290
|
+
}
|
|
291
|
+
else if (isIntervalValue(value)) {
|
|
292
|
+
const parsed = value.interval;
|
|
293
|
+
writer.push(0x08);
|
|
294
|
+
writer.i32(parsed.months);
|
|
295
|
+
writer.i32(parsed.days);
|
|
296
|
+
writer.i64(BigInt(parsed.micros));
|
|
297
|
+
}
|
|
298
|
+
else if (value && typeof value === "object" && "decimal" in value) {
|
|
299
|
+
writer.push(0x04);
|
|
300
|
+
writer.i128(value.decimal);
|
|
301
|
+
writer.push(value.scale & 0xff);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
throw new Error(`unsupported literal: ${String(node.value)}`);
|
|
305
|
+
}
|
|
306
|
+
return writer;
|
|
307
|
+
}
|
|
308
|
+
case "signer": {
|
|
309
|
+
writer.push(0x0c);
|
|
310
|
+
return writer;
|
|
311
|
+
}
|
|
312
|
+
case "col": {
|
|
313
|
+
if (node.index < 0 || node.index >= columns.length) {
|
|
314
|
+
throw new Error(`column index out of range: ${node.index}`);
|
|
315
|
+
}
|
|
316
|
+
const op = node.side === "old" ? 0x0b : 0x0a;
|
|
317
|
+
writer.push(op, node.index);
|
|
318
|
+
return writer;
|
|
319
|
+
}
|
|
320
|
+
case "cmp": {
|
|
321
|
+
const leftType = inferType(node.a, columns);
|
|
322
|
+
const rightType = inferType(node.b, columns);
|
|
323
|
+
// A column/literal comparison must coerce the literal to the column type.
|
|
324
|
+
// Prefer the column side over `literalTypeOf`; otherwise `price >= 0`
|
|
325
|
+
// would compile the literal as I64 and fail against a DECIMAL column.
|
|
326
|
+
const columnType = node.a.k === "col" ? leftType : node.b.k === "col" ? rightType : undefined;
|
|
327
|
+
const childType = columnType ?? leftType ?? rightType;
|
|
328
|
+
writer.bytes(compileNode(node.a, columns, childType ?? leftType).toBuffer());
|
|
329
|
+
writer.bytes(compileNode(node.b, columns, childType ?? rightType).toBuffer());
|
|
330
|
+
writer.push(CMP_OPCODE[node.op]);
|
|
331
|
+
return writer;
|
|
332
|
+
}
|
|
333
|
+
case "rangep": {
|
|
334
|
+
writer.bytes(compileNode(node.a, columns).toBuffer());
|
|
335
|
+
writer.bytes(compileNode(node.b, columns).toBuffer());
|
|
336
|
+
writer.push(RANGE_OPCODE[node.op]);
|
|
337
|
+
return writer;
|
|
338
|
+
}
|
|
339
|
+
case "and":
|
|
340
|
+
case "or": {
|
|
341
|
+
writer.bytes(compileNode(node.a, columns).toBuffer());
|
|
342
|
+
writer.bytes(compileNode(node.b, columns).toBuffer());
|
|
343
|
+
writer.push(node.k === "and" ? 0x1e : 0x1f);
|
|
344
|
+
return writer;
|
|
345
|
+
}
|
|
346
|
+
case "not": {
|
|
347
|
+
writer.bytes(compileNode(node.a, columns).toBuffer());
|
|
348
|
+
writer.push(0x20);
|
|
349
|
+
return writer;
|
|
350
|
+
}
|
|
351
|
+
case "isnull":
|
|
352
|
+
case "isnotnull": {
|
|
353
|
+
writer.bytes(compileNode(node.a, columns).toBuffer());
|
|
354
|
+
writer.push(node.k === "isnull" ? 0x21 : 0x22);
|
|
355
|
+
return writer;
|
|
356
|
+
}
|
|
357
|
+
case "between": {
|
|
358
|
+
// a BETWEEN low AND high == (a >= low) AND (a <= high)
|
|
359
|
+
const lowered = {
|
|
360
|
+
k: "and",
|
|
361
|
+
a: { k: "cmp", op: "ge", a: node.a, b: node.low },
|
|
362
|
+
b: { k: "cmp", op: "le", a: node.a, b: node.high },
|
|
363
|
+
};
|
|
364
|
+
writer.bytes(compileNode(lowered, columns, expected).toBuffer());
|
|
365
|
+
if (node.negated)
|
|
366
|
+
writer.push(0x20);
|
|
367
|
+
return writer;
|
|
368
|
+
}
|
|
369
|
+
case "in": {
|
|
370
|
+
if (node.values.length === 0)
|
|
371
|
+
throw new Error("IN requires at least one value");
|
|
372
|
+
// a IN (v1, v2, ...) == (a = v1) OR (a = v2) OR ...
|
|
373
|
+
let chain = { k: "cmp", op: "eq", a: node.a, b: node.values[0] };
|
|
374
|
+
for (const value of node.values.slice(1)) {
|
|
375
|
+
chain = { k: "or", a: chain, b: { k: "cmp", op: "eq", a: node.a, b: value } };
|
|
376
|
+
}
|
|
377
|
+
writer.bytes(compileNode(chain, columns, expected).toBuffer());
|
|
378
|
+
if (node.negated)
|
|
379
|
+
writer.push(0x20);
|
|
380
|
+
return writer;
|
|
381
|
+
}
|
|
382
|
+
case "arith": {
|
|
383
|
+
const leftType = inferType(node.a, columns);
|
|
384
|
+
const rightType = inferType(node.b, columns);
|
|
385
|
+
const intervalSide = leftType === "interval" || rightType === "interval";
|
|
386
|
+
if (intervalSide || (node.op === "sub" && leftType === rightType && (leftType === "date" || leftType === "timestamp"))) {
|
|
387
|
+
const opcode = calendarOpcode(node.op, leftType, rightType);
|
|
388
|
+
if (opcode !== undefined) {
|
|
389
|
+
writer.bytes(compileNode(node.a, columns, leftType).toBuffer());
|
|
390
|
+
writer.bytes(compileNode(node.b, columns, rightType).toBuffer());
|
|
391
|
+
writer.push(opcode);
|
|
392
|
+
return writer;
|
|
393
|
+
}
|
|
394
|
+
throw new Error(`unsupported calendar arithmetic: ${leftType ?? "unknown"} ${node.op} ${rightType ?? "unknown"}`);
|
|
395
|
+
}
|
|
396
|
+
const type = leftType ?? rightType;
|
|
397
|
+
writer.bytes(compileNode(node.a, columns, type).toBuffer());
|
|
398
|
+
writer.bytes(compileNode(node.b, columns, type).toBuffer());
|
|
399
|
+
writer.push(ARITH_OPCODE[node.op]);
|
|
400
|
+
return writer;
|
|
401
|
+
}
|
|
402
|
+
case "cast": {
|
|
403
|
+
if (node.a.k === "lit") {
|
|
404
|
+
return compileNode(node.a, columns, node.type);
|
|
405
|
+
}
|
|
406
|
+
const source = inferType(node.a, columns);
|
|
407
|
+
if (source !== node.type) {
|
|
408
|
+
throw new Error(`CAST(${source ?? "unknown"} AS ${node.type}) is not supported in on-chain CHECK/policy bytecode`);
|
|
409
|
+
}
|
|
410
|
+
return compileNode(node.a, columns, node.type);
|
|
411
|
+
}
|
|
412
|
+
case "coalesce": {
|
|
413
|
+
if (node.args.length === 0)
|
|
414
|
+
throw new Error("COALESCE requires at least one argument");
|
|
415
|
+
let lowered = node.args[node.args.length - 1];
|
|
416
|
+
for (let index = node.args.length - 2; index >= 0; index -= 1) {
|
|
417
|
+
const arg = node.args[index];
|
|
418
|
+
lowered = {
|
|
419
|
+
k: "case",
|
|
420
|
+
whens: [{ when: { k: "isnotnull", a: arg }, then: arg }],
|
|
421
|
+
otherwise: lowered,
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
return compileNode(lowered, columns, expected);
|
|
425
|
+
}
|
|
426
|
+
case "nullif": {
|
|
427
|
+
const lowered = {
|
|
428
|
+
k: "case",
|
|
429
|
+
whens: [
|
|
430
|
+
{
|
|
431
|
+
when: { k: "cmp", op: "eq", a: node.a, b: node.b },
|
|
432
|
+
then: { k: "lit", value: null },
|
|
433
|
+
},
|
|
434
|
+
],
|
|
435
|
+
otherwise: node.a,
|
|
436
|
+
};
|
|
437
|
+
return compileNode(lowered, columns, expected);
|
|
438
|
+
}
|
|
439
|
+
case "case": {
|
|
440
|
+
const endJumps = [];
|
|
441
|
+
for (const entry of node.whens) {
|
|
442
|
+
const condition = node.operand
|
|
443
|
+
? { k: "cmp", op: "eq", a: node.operand, b: entry.when }
|
|
444
|
+
: entry.when;
|
|
445
|
+
writer.bytes(compileNode(condition, columns).toBuffer());
|
|
446
|
+
writer.push(0x32);
|
|
447
|
+
const falseAt = writer.length;
|
|
448
|
+
writer.u16(0);
|
|
449
|
+
writer.bytes(compileNode(entry.then, columns, expected).toBuffer());
|
|
450
|
+
writer.push(0x33);
|
|
451
|
+
endJumps.push(writer.length);
|
|
452
|
+
writer.u16(0);
|
|
453
|
+
writer.patchU16(falseAt, writer.length);
|
|
454
|
+
}
|
|
455
|
+
if (node.otherwise) {
|
|
456
|
+
writer.bytes(compileNode(node.otherwise, columns, expected).toBuffer());
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
writer.push(0x01);
|
|
460
|
+
}
|
|
461
|
+
const end = writer.length;
|
|
462
|
+
for (const at of endJumps)
|
|
463
|
+
writer.patchU16(at, end);
|
|
464
|
+
return writer;
|
|
465
|
+
}
|
|
466
|
+
case "func":
|
|
467
|
+
throw new Error(`scalar function ${node.fn.toUpperCase()}() is client-side only; it cannot be stored in on-chain CHECK/policy bytecode`);
|
|
468
|
+
default:
|
|
469
|
+
throw new Error(`unsupported expression node: ${node.k}`);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/** Compiles an expression AST into the program's stack bytecode. */
|
|
473
|
+
function compileExpression(node, columns = []) {
|
|
474
|
+
const buffer = compileNode(node, columns).toBuffer();
|
|
475
|
+
if (buffer.length > exports.MAX_EXPR_BYTES) {
|
|
476
|
+
throw new Error(`expression is ${buffer.length} bytes; the program limit is ${exports.MAX_EXPR_BYTES}`);
|
|
477
|
+
}
|
|
478
|
+
return buffer;
|
|
479
|
+
}
|
|
480
|
+
function triOf(value) {
|
|
481
|
+
if (value === null || value === undefined)
|
|
482
|
+
return null;
|
|
483
|
+
if (typeof value === "boolean")
|
|
484
|
+
return value;
|
|
485
|
+
throw new Error("expression did not produce a boolean");
|
|
486
|
+
}
|
|
487
|
+
const RANGE_LOWER_INC = 0x01;
|
|
488
|
+
const RANGE_UPPER_INC = 0x02;
|
|
489
|
+
/** Plain `{start, end, flags}` view of a range value, or `null` for other values. */
|
|
490
|
+
function plainRange(value) {
|
|
491
|
+
if (value && typeof value === "object" && "range" in value) {
|
|
492
|
+
const range = value.range;
|
|
493
|
+
return {
|
|
494
|
+
start: BigInt(range.start.toString()),
|
|
495
|
+
end: BigInt(range.end.toString()),
|
|
496
|
+
flags: Number(range.flags) & 0x3f,
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
return null;
|
|
500
|
+
}
|
|
501
|
+
function plainScalar(value) {
|
|
502
|
+
if (typeof value === "bigint")
|
|
503
|
+
return value;
|
|
504
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
505
|
+
return BigInt(Math.trunc(value));
|
|
506
|
+
if (typeof value === "string" && /^-?\d+$/.test(value.trim()))
|
|
507
|
+
return BigInt(value.trim());
|
|
508
|
+
return null;
|
|
509
|
+
}
|
|
510
|
+
function rangeEmpty(range) {
|
|
511
|
+
const bothInclusive = (range.flags & (RANGE_LOWER_INC | RANGE_UPPER_INC)) ===
|
|
512
|
+
(RANGE_LOWER_INC | RANGE_UPPER_INC);
|
|
513
|
+
return range.start > range.end || (range.start === range.end && !bothInclusive);
|
|
514
|
+
}
|
|
515
|
+
function rangeContainsRange(outer, inner) {
|
|
516
|
+
if (rangeEmpty(inner))
|
|
517
|
+
return true;
|
|
518
|
+
if (rangeEmpty(outer))
|
|
519
|
+
return false;
|
|
520
|
+
const lowerOk = outer.start < inner.start ||
|
|
521
|
+
(outer.start === inner.start &&
|
|
522
|
+
((outer.flags & RANGE_LOWER_INC) !== 0 || (inner.flags & RANGE_LOWER_INC) === 0));
|
|
523
|
+
const upperOk = outer.end > inner.end ||
|
|
524
|
+
(outer.end === inner.end &&
|
|
525
|
+
((outer.flags & RANGE_UPPER_INC) !== 0 || (inner.flags & RANGE_UPPER_INC) === 0));
|
|
526
|
+
return lowerOk && upperOk;
|
|
527
|
+
}
|
|
528
|
+
function rangeContainsValue(range, value) {
|
|
529
|
+
if (rangeEmpty(range))
|
|
530
|
+
return false;
|
|
531
|
+
const lowerOk = value > range.start || (value === range.start && (range.flags & RANGE_LOWER_INC) !== 0);
|
|
532
|
+
const upperOk = value < range.end || (value === range.end && (range.flags & RANGE_UPPER_INC) !== 0);
|
|
533
|
+
return lowerOk && upperOk;
|
|
534
|
+
}
|
|
535
|
+
function rangeOverlaps(a, b) {
|
|
536
|
+
if (rangeEmpty(a) || rangeEmpty(b))
|
|
537
|
+
return false;
|
|
538
|
+
const leftOk = a.start < b.end ||
|
|
539
|
+
(a.start === b.end &&
|
|
540
|
+
(a.flags & RANGE_LOWER_INC) !== 0 &&
|
|
541
|
+
(b.flags & RANGE_UPPER_INC) !== 0);
|
|
542
|
+
const rightOk = b.start < a.end ||
|
|
543
|
+
(b.start === a.end &&
|
|
544
|
+
(b.flags & RANGE_LOWER_INC) !== 0 &&
|
|
545
|
+
(a.flags & RANGE_UPPER_INC) !== 0);
|
|
546
|
+
return leftOk && rightOk;
|
|
547
|
+
}
|
|
548
|
+
function rangeLeftOf(a, b) {
|
|
549
|
+
if (rangeEmpty(a) || rangeEmpty(b))
|
|
550
|
+
return false;
|
|
551
|
+
return (a.end < b.start ||
|
|
552
|
+
(a.end === b.start &&
|
|
553
|
+
((a.flags & RANGE_UPPER_INC) === 0 || (b.flags & RANGE_LOWER_INC) === 0)));
|
|
554
|
+
}
|
|
555
|
+
function rangeRightOf(a, b) {
|
|
556
|
+
if (rangeEmpty(a) || rangeEmpty(b))
|
|
557
|
+
return false;
|
|
558
|
+
return (a.start > b.end ||
|
|
559
|
+
(a.start === b.end &&
|
|
560
|
+
((a.flags & RANGE_LOWER_INC) === 0 || (b.flags & RANGE_UPPER_INC) === 0)));
|
|
561
|
+
}
|
|
562
|
+
function rangeAdjacent(a, b) {
|
|
563
|
+
if (rangeEmpty(a) || rangeEmpty(b))
|
|
564
|
+
return false;
|
|
565
|
+
const upperToLower = a.end === b.start &&
|
|
566
|
+
((a.flags & RANGE_UPPER_INC) !== 0) !== ((b.flags & RANGE_LOWER_INC) !== 0);
|
|
567
|
+
const lowerToUpper = b.end === a.start &&
|
|
568
|
+
((b.flags & RANGE_UPPER_INC) !== 0) !== ((a.flags & RANGE_LOWER_INC) !== 0);
|
|
569
|
+
return upperToLower || lowerToUpper;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Evaluates a PostgreSQL range predicate. `null` is SQL NULL; a non-range
|
|
573
|
+
* operand where a range is required throws, mirroring the program.
|
|
574
|
+
*/
|
|
575
|
+
function evalRangeOp(op, a, b) {
|
|
576
|
+
if (a === null || a === undefined || b === null || b === undefined)
|
|
577
|
+
return null;
|
|
578
|
+
const left = plainRange(a);
|
|
579
|
+
const right = plainRange(b);
|
|
580
|
+
const requireRange = (value, range) => {
|
|
581
|
+
if (range === null)
|
|
582
|
+
throw new Error(`range operator requires a range operand, got ${String(value)}`);
|
|
583
|
+
return range;
|
|
584
|
+
};
|
|
585
|
+
switch (op) {
|
|
586
|
+
case "contains": {
|
|
587
|
+
const outer = requireRange(a, left);
|
|
588
|
+
if (right)
|
|
589
|
+
return rangeContainsRange(outer, right);
|
|
590
|
+
const scalar = plainScalar(b);
|
|
591
|
+
if (scalar === null)
|
|
592
|
+
throw new Error(`range @> requires a range or integer, got ${String(b)}`);
|
|
593
|
+
return rangeContainsValue(outer, scalar);
|
|
594
|
+
}
|
|
595
|
+
case "contained": {
|
|
596
|
+
const owner = requireRange(b, right);
|
|
597
|
+
if (left)
|
|
598
|
+
return rangeContainsRange(owner, left);
|
|
599
|
+
const scalar = plainScalar(a);
|
|
600
|
+
if (scalar === null)
|
|
601
|
+
throw new Error(`range <@ requires a range or integer, got ${String(a)}`);
|
|
602
|
+
return rangeContainsValue(owner, scalar);
|
|
603
|
+
}
|
|
604
|
+
case "overlaps":
|
|
605
|
+
return rangeOverlaps(requireRange(a, left), requireRange(b, right));
|
|
606
|
+
case "leftOf":
|
|
607
|
+
return rangeLeftOf(requireRange(a, left), requireRange(b, right));
|
|
608
|
+
case "rightOf":
|
|
609
|
+
return rangeRightOf(requireRange(a, left), requireRange(b, right));
|
|
610
|
+
case "adjacent":
|
|
611
|
+
return rangeAdjacent(requireRange(a, left), requireRange(b, right));
|
|
612
|
+
default:
|
|
613
|
+
throw new Error(`unsupported range predicate: ${op}`);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Arithmetic mirror shared by the predicate evaluator and the query planner.
|
|
618
|
+
* Calendar operations need the bound operand kind, which the SQL binder stores
|
|
619
|
+
* on the `arith` node.
|
|
620
|
+
*/
|
|
621
|
+
function evalArithmetic(op, a, b, kind) {
|
|
622
|
+
if (isIntervalValue(a) || isIntervalValue(b) || kind !== undefined) {
|
|
623
|
+
return evalCalendar(op, a, b, kind);
|
|
624
|
+
}
|
|
625
|
+
const x = BigInt(a.toString());
|
|
626
|
+
const y = BigInt(b.toString());
|
|
627
|
+
switch (op) {
|
|
628
|
+
case "add":
|
|
629
|
+
return x + y;
|
|
630
|
+
case "sub":
|
|
631
|
+
return x - y;
|
|
632
|
+
case "mul":
|
|
633
|
+
return x * y;
|
|
634
|
+
case "div":
|
|
635
|
+
return y === 0n ? null : x / y;
|
|
636
|
+
}
|
|
637
|
+
return null;
|
|
638
|
+
}
|
|
639
|
+
function evalCalendar(op, a, b, kind) {
|
|
640
|
+
const asInterval = (v) => {
|
|
641
|
+
if (!isIntervalValue(v))
|
|
642
|
+
throw new Error("calendar arithmetic expects INTERVAL operands");
|
|
643
|
+
return v.interval;
|
|
644
|
+
};
|
|
645
|
+
if (op === "mul" || op === "div") {
|
|
646
|
+
throw new Error("INTERVAL multiplication and division are not supported");
|
|
647
|
+
}
|
|
648
|
+
if (kind === "date" && !isIntervalValue(a) && !isIntervalValue(b)) {
|
|
649
|
+
return BigInt(a.toString()) - BigInt(b.toString());
|
|
650
|
+
}
|
|
651
|
+
if (kind === "timestamp" && !isIntervalValue(a) && !isIntervalValue(b)) {
|
|
652
|
+
const seconds = BigInt(a.toString()) - BigInt(b.toString());
|
|
653
|
+
return { interval: { months: 0, days: 0, micros: seconds * 1000000n } };
|
|
654
|
+
}
|
|
655
|
+
if (isIntervalValue(a) && isIntervalValue(b)) {
|
|
656
|
+
const left = asInterval(a);
|
|
657
|
+
const right = asInterval(b);
|
|
658
|
+
const sign = op === "add" ? 1 : -1;
|
|
659
|
+
return {
|
|
660
|
+
interval: {
|
|
661
|
+
months: left.months + sign * right.months,
|
|
662
|
+
days: left.days + sign * right.days,
|
|
663
|
+
micros: left.micros + BigInt(sign) * right.micros,
|
|
664
|
+
},
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
const interval = asInterval(b);
|
|
668
|
+
const negative = op === "sub";
|
|
669
|
+
const delta = negative
|
|
670
|
+
? { months: -interval.months, days: -interval.days, micros: -interval.micros }
|
|
671
|
+
: interval;
|
|
672
|
+
if (typeof a === "bigint" || typeof a === "number") {
|
|
673
|
+
// DATE is a day count and TIMESTAMP a second count, so the bound column
|
|
674
|
+
// type must decide. Fall back to the sub-day heuristic only for hand-built
|
|
675
|
+
// nodes that carry no calendar kind.
|
|
676
|
+
const resolved = kind ?? (delta.micros !== 0n ? "timestamp" : "date");
|
|
677
|
+
if (resolved === "timestamp") {
|
|
678
|
+
return (0, values_1.timestampAddInterval)(BigInt(a.toString()), delta);
|
|
679
|
+
}
|
|
680
|
+
return (0, values_1.dateAddInterval)(BigInt(a.toString()), delta);
|
|
681
|
+
}
|
|
682
|
+
throw new Error("calendar arithmetic expects a DATE/TIMESTAMP or INTERVAL left operand");
|
|
683
|
+
}
|
|
684
|
+
function compareValues(a, b) {
|
|
685
|
+
if (a === null || a === undefined || b === null || b === undefined)
|
|
686
|
+
return null;
|
|
687
|
+
const numeric = (v) => {
|
|
688
|
+
if (typeof v === "bigint")
|
|
689
|
+
return { n: v, scale: 0 };
|
|
690
|
+
if (typeof v === "number") {
|
|
691
|
+
if (!Number.isFinite(v))
|
|
692
|
+
return null;
|
|
693
|
+
const text = v.toString();
|
|
694
|
+
if (!text.includes("."))
|
|
695
|
+
return { n: BigInt(text), scale: 0 };
|
|
696
|
+
const [whole, frac] = text.split(".");
|
|
697
|
+
return { n: BigInt(`${whole}${frac}`), scale: frac.length };
|
|
698
|
+
}
|
|
699
|
+
if (typeof v === "string" && /^-?\d+(\.\d+)?$/.test(v)) {
|
|
700
|
+
const [whole, frac = ""] = v.split(".");
|
|
701
|
+
return { n: BigInt(`${whole}${frac}`), scale: frac.length };
|
|
702
|
+
}
|
|
703
|
+
if (v && typeof v === "object" && "decimal" in v) {
|
|
704
|
+
const d = v;
|
|
705
|
+
return { n: d.decimal, scale: d.scale };
|
|
706
|
+
}
|
|
707
|
+
return null;
|
|
708
|
+
};
|
|
709
|
+
const leftNumeric = numeric(a);
|
|
710
|
+
const rightNumeric = numeric(b);
|
|
711
|
+
if (leftNumeric &&
|
|
712
|
+
rightNumeric &&
|
|
713
|
+
(typeof a !== "string" || typeof b !== "string" || a.includes(".") || b.includes("."))) {
|
|
714
|
+
const scale = Math.max(leftNumeric.scale, rightNumeric.scale);
|
|
715
|
+
const left = leftNumeric.n * 10n ** BigInt(scale - leftNumeric.scale);
|
|
716
|
+
const right = rightNumeric.n * 10n ** BigInt(scale - rightNumeric.scale);
|
|
717
|
+
return left === right ? 0 : left < right ? -1 : 1;
|
|
718
|
+
}
|
|
719
|
+
const norm = (v) => {
|
|
720
|
+
if (isIntervalValue(v))
|
|
721
|
+
return (0, values_1.intervalTotalMicros)(v.interval);
|
|
722
|
+
if (v && typeof v === "object" && "decimal" in v)
|
|
723
|
+
return v.decimal;
|
|
724
|
+
if (v && typeof v === "object" && "address" in v) {
|
|
725
|
+
return Buffer.from(v.address).toString("hex");
|
|
726
|
+
}
|
|
727
|
+
if (v && typeof v === "object" && "inet" in v) {
|
|
728
|
+
return `${Buffer.from(v.inet).toString("hex")}/${v.prefix}`;
|
|
729
|
+
}
|
|
730
|
+
if (v && typeof v === "object" && "mac" in v) {
|
|
731
|
+
return Buffer.from(v.mac).toString("hex");
|
|
732
|
+
}
|
|
733
|
+
if (v && typeof v === "object" && "bit" in v) {
|
|
734
|
+
return `${v.length}:${Buffer.from(v.bit).toString("hex")}`;
|
|
735
|
+
}
|
|
736
|
+
if (v instanceof Uint8Array)
|
|
737
|
+
return Buffer.from(v).toString("hex");
|
|
738
|
+
if (typeof v === "boolean")
|
|
739
|
+
return v ? 1n : 0n;
|
|
740
|
+
if (typeof v === "number" || typeof v === "bigint")
|
|
741
|
+
return BigInt(v.toString());
|
|
742
|
+
return v;
|
|
743
|
+
};
|
|
744
|
+
const na = norm(a);
|
|
745
|
+
const nb = norm(b);
|
|
746
|
+
if (na === nb)
|
|
747
|
+
return 0;
|
|
748
|
+
// Numeric values may reach the client as display strings (aggregate output,
|
|
749
|
+
// scalar subqueries). Compare them numerically instead of lexicographically.
|
|
750
|
+
if ((typeof na === "bigint" || typeof na === "number") &&
|
|
751
|
+
typeof nb === "string" &&
|
|
752
|
+
/^-?\d+$/.test(nb)) {
|
|
753
|
+
const right = BigInt(nb);
|
|
754
|
+
return na === right ? 0 : na < right ? -1 : 1;
|
|
755
|
+
}
|
|
756
|
+
if ((typeof nb === "bigint" || typeof nb === "number") &&
|
|
757
|
+
typeof na === "string" &&
|
|
758
|
+
/^-?\d+$/.test(na)) {
|
|
759
|
+
const left = BigInt(na);
|
|
760
|
+
return left === nb ? 0 : left < nb ? -1 : 1;
|
|
761
|
+
}
|
|
762
|
+
return na < nb ? -1 : 1;
|
|
763
|
+
}
|
|
764
|
+
function evaluate(node, row, oldRow, actor) {
|
|
765
|
+
switch (node.k) {
|
|
766
|
+
case "lit":
|
|
767
|
+
return node.value;
|
|
768
|
+
case "col": {
|
|
769
|
+
const source = node.side === "old" ? oldRow : row;
|
|
770
|
+
return source ? source[node.index] : null;
|
|
771
|
+
}
|
|
772
|
+
case "signer":
|
|
773
|
+
return actor ?? null;
|
|
774
|
+
case "rangep":
|
|
775
|
+
return evalRangeOp(node.op, evaluate(node.a, row, oldRow), evaluate(node.b, row, oldRow));
|
|
776
|
+
case "cmp": {
|
|
777
|
+
const a = evaluate(node.a, row, oldRow);
|
|
778
|
+
const b = evaluate(node.b, row, oldRow);
|
|
779
|
+
const c = compareValues(a, b);
|
|
780
|
+
if (c === null)
|
|
781
|
+
return null;
|
|
782
|
+
switch (node.op) {
|
|
783
|
+
case "eq":
|
|
784
|
+
return c === 0;
|
|
785
|
+
case "ne":
|
|
786
|
+
return c !== 0;
|
|
787
|
+
case "lt":
|
|
788
|
+
return c < 0;
|
|
789
|
+
case "le":
|
|
790
|
+
return c <= 0;
|
|
791
|
+
case "gt":
|
|
792
|
+
return c > 0;
|
|
793
|
+
case "ge":
|
|
794
|
+
return c >= 0;
|
|
795
|
+
}
|
|
796
|
+
return null;
|
|
797
|
+
}
|
|
798
|
+
case "and": {
|
|
799
|
+
const a = triOf(evaluate(node.a, row, oldRow));
|
|
800
|
+
const b = triOf(evaluate(node.b, row, oldRow));
|
|
801
|
+
if (a === false || b === false)
|
|
802
|
+
return false;
|
|
803
|
+
if (a === null || b === null)
|
|
804
|
+
return null;
|
|
805
|
+
return true;
|
|
806
|
+
}
|
|
807
|
+
case "or": {
|
|
808
|
+
const a = triOf(evaluate(node.a, row, oldRow));
|
|
809
|
+
const b = triOf(evaluate(node.b, row, oldRow));
|
|
810
|
+
if (a === true || b === true)
|
|
811
|
+
return true;
|
|
812
|
+
if (a === null || b === null)
|
|
813
|
+
return null;
|
|
814
|
+
return false;
|
|
815
|
+
}
|
|
816
|
+
case "not": {
|
|
817
|
+
const a = triOf(evaluate(node.a, row, oldRow));
|
|
818
|
+
return a === null ? null : !a;
|
|
819
|
+
}
|
|
820
|
+
case "isnull":
|
|
821
|
+
return evaluate(node.a, row, oldRow) === null;
|
|
822
|
+
case "isnotnull":
|
|
823
|
+
return evaluate(node.a, row, oldRow) !== null;
|
|
824
|
+
case "between": {
|
|
825
|
+
const value = evaluate(node.a, row, oldRow);
|
|
826
|
+
const low = evaluate(node.low, row, oldRow);
|
|
827
|
+
const high = evaluate(node.high, row, oldRow);
|
|
828
|
+
const lower = compareValues(value, low);
|
|
829
|
+
const upper = compareValues(value, high);
|
|
830
|
+
if (lower === null || upper === null)
|
|
831
|
+
return null;
|
|
832
|
+
const inside = lower >= 0 && upper <= 0;
|
|
833
|
+
return node.negated ? !inside : inside;
|
|
834
|
+
}
|
|
835
|
+
case "in": {
|
|
836
|
+
const value = evaluate(node.a, row, oldRow);
|
|
837
|
+
if (value === null || value === undefined)
|
|
838
|
+
return null;
|
|
839
|
+
let sawNull = false;
|
|
840
|
+
for (const candidate of node.values) {
|
|
841
|
+
const other = evaluate(candidate, row, oldRow);
|
|
842
|
+
if (other === null || other === undefined) {
|
|
843
|
+
sawNull = true;
|
|
844
|
+
continue;
|
|
845
|
+
}
|
|
846
|
+
if (compareValues(value, other) === 0) {
|
|
847
|
+
return node.negated ? false : true;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
if (sawNull)
|
|
851
|
+
return null;
|
|
852
|
+
return node.negated ? true : false;
|
|
853
|
+
}
|
|
854
|
+
case "arith": {
|
|
855
|
+
const a = evaluate(node.a, row, oldRow);
|
|
856
|
+
const b = evaluate(node.b, row, oldRow);
|
|
857
|
+
if (a === null || a === undefined || b === null || b === undefined)
|
|
858
|
+
return null;
|
|
859
|
+
return evalArithmetic(node.op, a, b, node.calendar);
|
|
860
|
+
}
|
|
861
|
+
case "case": {
|
|
862
|
+
if (node.operand) {
|
|
863
|
+
const operand = evaluate(node.operand, row, oldRow);
|
|
864
|
+
for (const entry of node.whens) {
|
|
865
|
+
const when = evaluate(entry.when, row, oldRow);
|
|
866
|
+
if (compareValues(operand, when) === 0) {
|
|
867
|
+
return evaluate(entry.then, row, oldRow);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
else {
|
|
872
|
+
for (const entry of node.whens) {
|
|
873
|
+
if (triOf(evaluate(entry.when, row, oldRow)) === true) {
|
|
874
|
+
return evaluate(entry.then, row, oldRow);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
return node.otherwise ? evaluate(node.otherwise, row, oldRow) : null;
|
|
879
|
+
}
|
|
880
|
+
case "coalesce": {
|
|
881
|
+
for (const arg of node.args) {
|
|
882
|
+
const value = evaluate(arg, row, oldRow);
|
|
883
|
+
if (value !== null && value !== undefined)
|
|
884
|
+
return value;
|
|
885
|
+
}
|
|
886
|
+
return null;
|
|
887
|
+
}
|
|
888
|
+
case "nullif": {
|
|
889
|
+
const a = evaluate(node.a, row, oldRow);
|
|
890
|
+
const b = evaluate(node.b, row, oldRow);
|
|
891
|
+
return compareValues(a, b) === 0 ? null : a;
|
|
892
|
+
}
|
|
893
|
+
case "cast":
|
|
894
|
+
return castValue(evaluate(node.a, row, oldRow), node);
|
|
895
|
+
case "func":
|
|
896
|
+
return callFunction(node.fn, node.args.map((arg) => evaluate(arg, row, oldRow)));
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
function castValue(value, node) {
|
|
900
|
+
if (value === null || value === undefined)
|
|
901
|
+
return null;
|
|
902
|
+
if (node.type === "text") {
|
|
903
|
+
const text = typeof value === "string"
|
|
904
|
+
? value
|
|
905
|
+
: typeof value === "boolean"
|
|
906
|
+
? value
|
|
907
|
+
? "true"
|
|
908
|
+
: "false"
|
|
909
|
+
: typeof value === "object" && value !== null && "decimal" in value
|
|
910
|
+
? decimalText(value)
|
|
911
|
+
: String(value);
|
|
912
|
+
if (node.maxLen && node.maxLen > 0 && text.length > node.maxLen) {
|
|
913
|
+
throw new Error(`CAST value exceeds VARCHAR(${node.maxLen})`);
|
|
914
|
+
}
|
|
915
|
+
return text;
|
|
916
|
+
}
|
|
917
|
+
return (0, values_1.coerceValue)(value, { type: node.type, scale: node.scale });
|
|
918
|
+
}
|
|
919
|
+
function decimalText(value) {
|
|
920
|
+
const negative = value.decimal < 0n;
|
|
921
|
+
const digits = (negative ? -value.decimal : value.decimal)
|
|
922
|
+
.toString()
|
|
923
|
+
.padStart(value.scale + 1, "0");
|
|
924
|
+
const whole = digits.slice(0, digits.length - value.scale) || "0";
|
|
925
|
+
const frac = value.scale > 0 ? "." + digits.slice(digits.length - value.scale) : "";
|
|
926
|
+
return `${negative ? "-" : ""}${whole}${frac}`;
|
|
927
|
+
}
|
|
928
|
+
function callFunction(fn, args) {
|
|
929
|
+
if (args.some((value) => value === null || value === undefined))
|
|
930
|
+
return null;
|
|
931
|
+
const text = (value) => String(value);
|
|
932
|
+
switch (fn) {
|
|
933
|
+
case "lower":
|
|
934
|
+
return text(args[0]).toLowerCase();
|
|
935
|
+
case "upper":
|
|
936
|
+
return text(args[0]).toUpperCase();
|
|
937
|
+
case "length":
|
|
938
|
+
return BigInt(text(args[0]).length);
|
|
939
|
+
case "trim":
|
|
940
|
+
return text(args[0]).trim();
|
|
941
|
+
case "ltrim":
|
|
942
|
+
return text(args[0]).replace(/^\s+/, "");
|
|
943
|
+
case "rtrim":
|
|
944
|
+
return text(args[0]).replace(/\s+$/, "");
|
|
945
|
+
case "abs": {
|
|
946
|
+
const value = args[0];
|
|
947
|
+
if (typeof value === "bigint")
|
|
948
|
+
return value < 0n ? -value : value;
|
|
949
|
+
if (typeof value === "number")
|
|
950
|
+
return Math.abs(value);
|
|
951
|
+
if (value && typeof value === "object" && "decimal" in value) {
|
|
952
|
+
const d = value;
|
|
953
|
+
return { decimal: d.decimal < 0n ? -d.decimal : d.decimal, scale: d.scale };
|
|
954
|
+
}
|
|
955
|
+
throw new Error("ABS requires a numeric argument");
|
|
956
|
+
}
|
|
957
|
+
case "round": {
|
|
958
|
+
const digits = args.length > 1 ? Number(args[1]) : 0;
|
|
959
|
+
if (!Number.isInteger(digits) || digits < 0) {
|
|
960
|
+
throw new Error("ROUND scale must be a non-negative integer");
|
|
961
|
+
}
|
|
962
|
+
const value = args[0];
|
|
963
|
+
if (typeof value === "bigint")
|
|
964
|
+
return value;
|
|
965
|
+
if (typeof value === "number") {
|
|
966
|
+
const factor = 10 ** digits;
|
|
967
|
+
const scaled = value * factor;
|
|
968
|
+
const rounded = scaled >= 0 ? Math.floor(scaled + 0.5) : Math.ceil(scaled - 0.5);
|
|
969
|
+
return rounded / factor;
|
|
970
|
+
}
|
|
971
|
+
if (value && typeof value === "object" && "decimal" in value) {
|
|
972
|
+
const d = value;
|
|
973
|
+
if (digits >= d.scale)
|
|
974
|
+
return value;
|
|
975
|
+
const factor = 10n ** BigInt(d.scale - digits);
|
|
976
|
+
const halfway = factor / 2n;
|
|
977
|
+
const rounded = d.decimal >= 0n
|
|
978
|
+
? (d.decimal + halfway) / factor
|
|
979
|
+
: -((-d.decimal + halfway) / factor);
|
|
980
|
+
return { decimal: rounded, scale: digits };
|
|
981
|
+
}
|
|
982
|
+
throw new Error("ROUND requires a numeric argument");
|
|
983
|
+
}
|
|
984
|
+
default:
|
|
985
|
+
throw new Error(`unsupported scalar function: ${fn}`);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
/** Interprets the program's policy bytecode for read-side filtering. */
|
|
989
|
+
function evaluateBytecode(code, row, oldRow, actor) {
|
|
990
|
+
const stack = [];
|
|
991
|
+
let pc = 0;
|
|
992
|
+
const u8 = () => code[pc++];
|
|
993
|
+
const u16 = () => {
|
|
994
|
+
const value = code[pc] | (code[pc + 1] << 8);
|
|
995
|
+
pc += 2;
|
|
996
|
+
return value;
|
|
997
|
+
};
|
|
998
|
+
const pop = () => {
|
|
999
|
+
if (stack.length === 0)
|
|
1000
|
+
throw new Error("policy bytecode stack underflow");
|
|
1001
|
+
return stack.pop();
|
|
1002
|
+
};
|
|
1003
|
+
while (pc < code.length) {
|
|
1004
|
+
const op = u8();
|
|
1005
|
+
switch (op) {
|
|
1006
|
+
case 0x01:
|
|
1007
|
+
stack.push(null);
|
|
1008
|
+
break;
|
|
1009
|
+
case 0x02:
|
|
1010
|
+
stack.push(code[pc++] !== 0);
|
|
1011
|
+
break;
|
|
1012
|
+
case 0x03: {
|
|
1013
|
+
const bytes = Buffer.from(code.subarray(pc, pc + 8));
|
|
1014
|
+
pc += 8;
|
|
1015
|
+
stack.push(bytes.readBigInt64LE(0));
|
|
1016
|
+
break;
|
|
1017
|
+
}
|
|
1018
|
+
case 0x04: {
|
|
1019
|
+
const bytes = Buffer.from(code.subarray(pc, pc + 16));
|
|
1020
|
+
pc += 16;
|
|
1021
|
+
const scale = code[pc++];
|
|
1022
|
+
let value = 0n;
|
|
1023
|
+
for (let index = 15; index >= 0; index -= 1) {
|
|
1024
|
+
value = (value << 8n) | BigInt(bytes[index]);
|
|
1025
|
+
}
|
|
1026
|
+
stack.push({ decimal: BigInt.asIntN(128, value), scale });
|
|
1027
|
+
break;
|
|
1028
|
+
}
|
|
1029
|
+
case 0x05: {
|
|
1030
|
+
const len = u16();
|
|
1031
|
+
stack.push(code.subarray(pc, pc + len).toString("utf8"));
|
|
1032
|
+
pc += len;
|
|
1033
|
+
break;
|
|
1034
|
+
}
|
|
1035
|
+
case 0x06: {
|
|
1036
|
+
stack.push(Uint8Array.from(code.subarray(pc, pc + 16)));
|
|
1037
|
+
pc += 16;
|
|
1038
|
+
break;
|
|
1039
|
+
}
|
|
1040
|
+
case 0x07: {
|
|
1041
|
+
const start = Buffer.from(code.subarray(pc, pc + 8)).readBigInt64LE(0);
|
|
1042
|
+
const end = Buffer.from(code.subarray(pc + 8, pc + 16)).readBigInt64LE(0);
|
|
1043
|
+
const flags = code[pc + 16];
|
|
1044
|
+
pc += 17;
|
|
1045
|
+
stack.push({ range: { start, end, flags } });
|
|
1046
|
+
break;
|
|
1047
|
+
}
|
|
1048
|
+
case 0x08: {
|
|
1049
|
+
const months = Buffer.from(code.subarray(pc, pc + 4)).readInt32LE(0);
|
|
1050
|
+
const days = Buffer.from(code.subarray(pc + 4, pc + 8)).readInt32LE(0);
|
|
1051
|
+
const micros = Buffer.from(code.subarray(pc + 8, pc + 16)).readBigInt64LE(0);
|
|
1052
|
+
pc += 16;
|
|
1053
|
+
stack.push({ interval: { months, days, micros } });
|
|
1054
|
+
break;
|
|
1055
|
+
}
|
|
1056
|
+
case 0x0a:
|
|
1057
|
+
stack.push(row[u8()] ?? null);
|
|
1058
|
+
break;
|
|
1059
|
+
case 0x0b:
|
|
1060
|
+
stack.push(oldRow ? oldRow[u8()] ?? null : null);
|
|
1061
|
+
break;
|
|
1062
|
+
case 0x0c:
|
|
1063
|
+
stack.push(actor ?? null);
|
|
1064
|
+
break;
|
|
1065
|
+
case 0x14:
|
|
1066
|
+
case 0x15:
|
|
1067
|
+
case 0x16:
|
|
1068
|
+
case 0x17:
|
|
1069
|
+
case 0x18:
|
|
1070
|
+
case 0x19: {
|
|
1071
|
+
const right = pop();
|
|
1072
|
+
const left = pop();
|
|
1073
|
+
const cmp = compareValues(left, right);
|
|
1074
|
+
if (cmp === null)
|
|
1075
|
+
stack.push(null);
|
|
1076
|
+
else if (op === 0x14)
|
|
1077
|
+
stack.push(cmp === 0);
|
|
1078
|
+
else if (op === 0x15)
|
|
1079
|
+
stack.push(cmp !== 0);
|
|
1080
|
+
else if (op === 0x16)
|
|
1081
|
+
stack.push(cmp < 0);
|
|
1082
|
+
else if (op === 0x17)
|
|
1083
|
+
stack.push(cmp <= 0);
|
|
1084
|
+
else if (op === 0x18)
|
|
1085
|
+
stack.push(cmp > 0);
|
|
1086
|
+
else
|
|
1087
|
+
stack.push(cmp >= 0);
|
|
1088
|
+
break;
|
|
1089
|
+
}
|
|
1090
|
+
case 0x1e: {
|
|
1091
|
+
const right = triOf(pop());
|
|
1092
|
+
const left = triOf(pop());
|
|
1093
|
+
stack.push(left === false || right === false ? false : left === null || right === null ? null : true);
|
|
1094
|
+
break;
|
|
1095
|
+
}
|
|
1096
|
+
case 0x1f: {
|
|
1097
|
+
const right = triOf(pop());
|
|
1098
|
+
const left = triOf(pop());
|
|
1099
|
+
stack.push(left === true || right === true ? true : left === null || right === null ? null : false);
|
|
1100
|
+
break;
|
|
1101
|
+
}
|
|
1102
|
+
case 0x20: {
|
|
1103
|
+
const value = triOf(pop());
|
|
1104
|
+
stack.push(value === null ? null : !value);
|
|
1105
|
+
break;
|
|
1106
|
+
}
|
|
1107
|
+
case 0x21:
|
|
1108
|
+
stack.push(pop() === null);
|
|
1109
|
+
break;
|
|
1110
|
+
case 0x22:
|
|
1111
|
+
stack.push(pop() !== null);
|
|
1112
|
+
break;
|
|
1113
|
+
case 0x25:
|
|
1114
|
+
case 0x26:
|
|
1115
|
+
case 0x27:
|
|
1116
|
+
case 0x2c:
|
|
1117
|
+
case 0x2d:
|
|
1118
|
+
case 0x2e:
|
|
1119
|
+
case 0x2f:
|
|
1120
|
+
case 0x30: {
|
|
1121
|
+
const right = pop();
|
|
1122
|
+
const left = pop();
|
|
1123
|
+
if (left === null || right === null) {
|
|
1124
|
+
stack.push(null);
|
|
1125
|
+
break;
|
|
1126
|
+
}
|
|
1127
|
+
const kind = op === 0x25 || op === 0x27 || op === 0x2f
|
|
1128
|
+
? "date"
|
|
1129
|
+
: op === 0x26 || op === 0x2c || op === 0x30
|
|
1130
|
+
? "timestamp"
|
|
1131
|
+
: "interval";
|
|
1132
|
+
const sub = op === 0x27 || op === 0x2c || op === 0x2e;
|
|
1133
|
+
stack.push(evalArithmetic(sub ? "sub" : "add", left, right, kind));
|
|
1134
|
+
break;
|
|
1135
|
+
}
|
|
1136
|
+
case 0x28:
|
|
1137
|
+
case 0x29:
|
|
1138
|
+
case 0x2a:
|
|
1139
|
+
case 0x2b: {
|
|
1140
|
+
const right = pop();
|
|
1141
|
+
const left = pop();
|
|
1142
|
+
if (left === null || right === null) {
|
|
1143
|
+
stack.push(null);
|
|
1144
|
+
break;
|
|
1145
|
+
}
|
|
1146
|
+
const x = BigInt(left.toString());
|
|
1147
|
+
const y = BigInt(right.toString());
|
|
1148
|
+
stack.push(op === 0x28 ? x + y : op === 0x29 ? x - y : op === 0x2a ? x * y : y === 0n ? null : x / y);
|
|
1149
|
+
break;
|
|
1150
|
+
}
|
|
1151
|
+
case 0x32: {
|
|
1152
|
+
const target = u16();
|
|
1153
|
+
if (!triOf(pop()))
|
|
1154
|
+
pc = target;
|
|
1155
|
+
break;
|
|
1156
|
+
}
|
|
1157
|
+
case 0x33:
|
|
1158
|
+
pc = u16();
|
|
1159
|
+
break;
|
|
1160
|
+
default:
|
|
1161
|
+
throw new Error(`unsupported policy opcode 0x${op.toString(16)}`);
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
if (stack.length !== 1)
|
|
1165
|
+
throw new Error("invalid policy bytecode result");
|
|
1166
|
+
return stack[0];
|
|
1167
|
+
}
|
|
1168
|
+
/** Predicate semantics: only TRUE passes. */
|
|
1169
|
+
function predicatePasses(node, row, oldRow, actor) {
|
|
1170
|
+
if (!node)
|
|
1171
|
+
return true;
|
|
1172
|
+
return triOf(evaluate(node, row, oldRow, actor)) === true;
|
|
1173
|
+
}
|
|
1174
|
+
/** CHECK semantics: passes unless FALSE (UNKNOWN passes). */
|
|
1175
|
+
function checkPasses(node, row) {
|
|
1176
|
+
return triOf(evaluate(node, row)) !== false;
|
|
1177
|
+
}
|