@query-farm/vgi 0.48.6 → 0.49.1
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/README.md +1 -1
- package/dist/catalog/composite.d.ts +2 -0
- package/dist/catalog/descriptors.d.ts +11 -0
- package/dist/catalog/interface.d.ts +39 -2
- package/dist/catalog/scan-planning.d.ts +76 -0
- package/dist/client-entry.js +32 -4
- package/dist/client-entry.js.map +8 -7
- package/dist/generated/vgi-client.d.ts +1 -0
- package/dist/generated/vgi-protocol-schemas.d.ts +1 -1
- package/dist/generated/vgi-service.d.ts +5 -1
- package/dist/index.core.d.ts +1 -0
- package/dist/index.js +301 -18
- package/dist/index.js.map +15 -14
- package/dist/protocol/dispatch.d.ts +2 -2
- package/dist/serve-entry.js +1552 -8
- package/dist/serve-entry.js.map +15 -10
- package/dist/worker-cf-entry.d.ts +2 -2
- package/dist/worker-cf-entry.js +327 -11
- package/dist/worker-cf-entry.js.map +16 -15
- package/package.json +1 -1
- package/src/catalog/column-metadata.ts +7 -0
- package/src/catalog/composite.ts +5 -0
- package/src/catalog/descriptors.ts +11 -0
- package/src/catalog/interface.ts +78 -2
- package/src/catalog/read-only.ts +14 -1
- package/src/catalog/scan-planning.ts +314 -0
- package/src/generated/vgi-client.ts +2 -1
- package/src/generated/vgi-protocol-schemas.ts +6 -2
- package/src/generated/vgi-service.ts +7 -2
- package/src/index.core.ts +11 -0
- package/src/protocol/dispatch.ts +2 -2
- package/src/protocol/handlers/catalog/table.ts +19 -8
- package/src/worker-cf-entry.ts +9 -0
package/dist/serve-entry.js
CHANGED
|
@@ -684,6 +684,18 @@ function isBool(t) {
|
|
|
684
684
|
function isDecimal(t) {
|
|
685
685
|
return t.typeId === TypeId.Decimal;
|
|
686
686
|
}
|
|
687
|
+
function isDate(t) {
|
|
688
|
+
return t.typeId === TypeId.Date;
|
|
689
|
+
}
|
|
690
|
+
function isTime(t) {
|
|
691
|
+
return t.typeId === TypeId.Time;
|
|
692
|
+
}
|
|
693
|
+
function isTimestamp(t) {
|
|
694
|
+
return t.typeId === TypeId.Timestamp;
|
|
695
|
+
}
|
|
696
|
+
function isInterval(t) {
|
|
697
|
+
return t.typeId === TypeId.Interval;
|
|
698
|
+
}
|
|
687
699
|
function isList(t) {
|
|
688
700
|
return t.typeId === TypeId.List;
|
|
689
701
|
}
|
|
@@ -693,6 +705,9 @@ function isStruct(t) {
|
|
|
693
705
|
function isMap(t) {
|
|
694
706
|
return t.typeId === TypeId.Map;
|
|
695
707
|
}
|
|
708
|
+
function isDuration(t) {
|
|
709
|
+
return t.typeId === TypeId.Duration;
|
|
710
|
+
}
|
|
696
711
|
function isDictionary(t) {
|
|
697
712
|
return t.typeId === TypeId.Dictionary;
|
|
698
713
|
}
|
|
@@ -2474,6 +2489,1303 @@ function toUint8Array(val) {
|
|
|
2474
2489
|
}
|
|
2475
2490
|
return new Uint8Array(0);
|
|
2476
2491
|
}
|
|
2492
|
+
// src/filter-pushdown/capabilities.ts
|
|
2493
|
+
var SUPPORTED_EXTENSION_FUNCTIONS = new Set(["duckdb.spatial/intersects_extent@1"]);
|
|
2494
|
+
function filterFunctionIdentityKey(identity) {
|
|
2495
|
+
return `${identity.namespace}/${identity.name}@${identity.version}`;
|
|
2496
|
+
}
|
|
2497
|
+
function supportsExtensionFilterFunction(identity) {
|
|
2498
|
+
return SUPPORTED_EXTENSION_FUNCTIONS.has(filterFunctionIdentityKey(identity));
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2501
|
+
// src/filter-pushdown/types.ts
|
|
2502
|
+
var FILTER_ENCODING = "vgi.filters.v2";
|
|
2503
|
+
var FILTER_VERSION = "2";
|
|
2504
|
+
var DUCKDB_STANDARD_V1 = "vgi.duckdb.standard.v1";
|
|
2505
|
+
var NO_EVALUATION_CONTEXT = "vgi.none.v1";
|
|
2506
|
+
var DUCKDB_SESSION_CONTEXT = "vgi.duckdb.session.v1";
|
|
2507
|
+
var ComparisonOp;
|
|
2508
|
+
((ComparisonOp) => {
|
|
2509
|
+
ComparisonOp["EQ"] = "eq";
|
|
2510
|
+
ComparisonOp["NE"] = "ne";
|
|
2511
|
+
ComparisonOp["LT"] = "lt";
|
|
2512
|
+
ComparisonOp["LE"] = "le";
|
|
2513
|
+
ComparisonOp["GT"] = "gt";
|
|
2514
|
+
ComparisonOp["GE"] = "ge";
|
|
2515
|
+
ComparisonOp["DISTINCT_FROM"] = "distinct_from";
|
|
2516
|
+
ComparisonOp["NOT_DISTINCT_FROM"] = "not_distinct_from";
|
|
2517
|
+
})(ComparisonOp ||= {});
|
|
2518
|
+
|
|
2519
|
+
// src/filter-pushdown/evaluate.ts
|
|
2520
|
+
var RESOLVED_COLUMNS = new WeakMap;
|
|
2521
|
+
function resolveColumn(batch, columnName) {
|
|
2522
|
+
let byName = RESOLVED_COLUMNS.get(batch);
|
|
2523
|
+
if (!byName) {
|
|
2524
|
+
byName = new Map;
|
|
2525
|
+
RESOLVED_COLUMNS.set(batch, byName);
|
|
2526
|
+
}
|
|
2527
|
+
let resolved = byName.get(columnName);
|
|
2528
|
+
if (resolved === undefined) {
|
|
2529
|
+
const index = batch.schema.fields.findIndex((field) => field.name === columnName);
|
|
2530
|
+
if (index < 0) {
|
|
2531
|
+
throw new FilterV2Error(`filter column ${columnName} is unavailable in emitted batch`);
|
|
2532
|
+
}
|
|
2533
|
+
const column = batch.getChildAt(index);
|
|
2534
|
+
const field = batch.schema.fields[index];
|
|
2535
|
+
resolved = column && field ? { type: field.type, column } : null;
|
|
2536
|
+
byName.set(columnName, resolved);
|
|
2537
|
+
}
|
|
2538
|
+
return resolved;
|
|
2539
|
+
}
|
|
2540
|
+
function deepEqual(left, right) {
|
|
2541
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
2542
|
+
return left === right || Number.isNaN(left) && Number.isNaN(right);
|
|
2543
|
+
}
|
|
2544
|
+
if (left instanceof Uint8Array && right instanceof Uint8Array) {
|
|
2545
|
+
return left.length === right.length && left.every((value, index) => value === right[index]);
|
|
2546
|
+
}
|
|
2547
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
2548
|
+
return left.length === right.length && left.every((value, index) => deepEqual(value, right[index]));
|
|
2549
|
+
}
|
|
2550
|
+
if (left && right && typeof left === "object" && typeof right === "object") {
|
|
2551
|
+
const entries = Object.entries(left);
|
|
2552
|
+
const rightObject = right;
|
|
2553
|
+
return entries.length === Object.keys(rightObject).length && entries.every(([key, value]) => (key in rightObject) && deepEqual(value, rightObject[key]));
|
|
2554
|
+
}
|
|
2555
|
+
return left === right;
|
|
2556
|
+
}
|
|
2557
|
+
function orderedCompare(left, right) {
|
|
2558
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
2559
|
+
const leftNan = Number.isNaN(left);
|
|
2560
|
+
const rightNan = Number.isNaN(right);
|
|
2561
|
+
if (leftNan || rightNan)
|
|
2562
|
+
return leftNan === rightNan ? 0 : leftNan ? 1 : -1;
|
|
2563
|
+
}
|
|
2564
|
+
if (deepEqual(left, right))
|
|
2565
|
+
return 0;
|
|
2566
|
+
return left < right ? -1 : 1;
|
|
2567
|
+
}
|
|
2568
|
+
function compare(left, right, op) {
|
|
2569
|
+
if (op === "distinct_from" /* DISTINCT_FROM */) {
|
|
2570
|
+
return left === null || right === null ? left !== right : !deepEqual(left, right);
|
|
2571
|
+
}
|
|
2572
|
+
if (op === "not_distinct_from" /* NOT_DISTINCT_FROM */) {
|
|
2573
|
+
return left === null || right === null ? left === right : deepEqual(left, right);
|
|
2574
|
+
}
|
|
2575
|
+
if (left === null || right === null)
|
|
2576
|
+
return null;
|
|
2577
|
+
const order = orderedCompare(left, right);
|
|
2578
|
+
switch (op) {
|
|
2579
|
+
case "eq" /* EQ */:
|
|
2580
|
+
return order === 0;
|
|
2581
|
+
case "ne" /* NE */:
|
|
2582
|
+
return order !== 0;
|
|
2583
|
+
case "lt" /* LT */:
|
|
2584
|
+
return order < 0;
|
|
2585
|
+
case "le" /* LE */:
|
|
2586
|
+
return order <= 0;
|
|
2587
|
+
case "gt" /* GT */:
|
|
2588
|
+
return order > 0;
|
|
2589
|
+
case "ge" /* GE */:
|
|
2590
|
+
return order >= 0;
|
|
2591
|
+
default:
|
|
2592
|
+
throw new FilterV2Error(`unknown comparison operator ${op}`);
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
function andKleene(values) {
|
|
2596
|
+
if (values.some((value) => value === false))
|
|
2597
|
+
return false;
|
|
2598
|
+
return values.some((value) => value === null) ? null : true;
|
|
2599
|
+
}
|
|
2600
|
+
function orKleene(values) {
|
|
2601
|
+
if (values.some((value) => value === true))
|
|
2602
|
+
return true;
|
|
2603
|
+
return values.some((value) => value === null) ? null : false;
|
|
2604
|
+
}
|
|
2605
|
+
function integerBounds(type) {
|
|
2606
|
+
if (!isInt(type))
|
|
2607
|
+
return null;
|
|
2608
|
+
const width = BigInt(type.bitWidth ?? 32);
|
|
2609
|
+
const signed = type.isSigned ?? type.signed ?? true;
|
|
2610
|
+
return signed ? [-(1n << width - 1n), (1n << width - 1n) - 1n] : [0n, (1n << width) - 1n];
|
|
2611
|
+
}
|
|
2612
|
+
function castValue(value, type) {
|
|
2613
|
+
if (value === null)
|
|
2614
|
+
return null;
|
|
2615
|
+
if (isUtf8(type))
|
|
2616
|
+
return String(value);
|
|
2617
|
+
if (isBool(type)) {
|
|
2618
|
+
if (typeof value === "boolean")
|
|
2619
|
+
return value;
|
|
2620
|
+
if (value === 0 || value === 0n || value === "false")
|
|
2621
|
+
return false;
|
|
2622
|
+
if (value === 1 || value === 1n || value === "true")
|
|
2623
|
+
return true;
|
|
2624
|
+
throw new FilterV2Error(`cannot cast ${String(value)} to BOOLEAN`);
|
|
2625
|
+
}
|
|
2626
|
+
if (isFloat(type)) {
|
|
2627
|
+
const result = Number(value);
|
|
2628
|
+
if (Number.isNaN(result) && String(value).toLowerCase() !== "nan") {
|
|
2629
|
+
throw new FilterV2Error(`cannot cast ${String(value)} to floating point`);
|
|
2630
|
+
}
|
|
2631
|
+
return result;
|
|
2632
|
+
}
|
|
2633
|
+
const bounds = integerBounds(type);
|
|
2634
|
+
if (bounds) {
|
|
2635
|
+
let result;
|
|
2636
|
+
try {
|
|
2637
|
+
result = typeof value === "bigint" ? value : BigInt(typeof value === "number" ? Math.trunc(value) : String(value));
|
|
2638
|
+
} catch {
|
|
2639
|
+
throw new FilterV2Error(`cannot cast ${String(value)} to integer`);
|
|
2640
|
+
}
|
|
2641
|
+
if (result < bounds[0] || result > bounds[1])
|
|
2642
|
+
throw new FilterV2Error("integer cast overflow");
|
|
2643
|
+
return type.bitWidth === 64 ? result : Number(result);
|
|
2644
|
+
}
|
|
2645
|
+
throw new FilterV2Error(`no vgi.duckdb.standard.v1 cast evaluator for Arrow type ${type.typeId}`);
|
|
2646
|
+
}
|
|
2647
|
+
function numericBinary(op, left, right, context) {
|
|
2648
|
+
if (left === null || right === null)
|
|
2649
|
+
return null;
|
|
2650
|
+
if (typeof left !== "number" && typeof left !== "bigint" || typeof right !== "number" && typeof right !== "bigint") {
|
|
2651
|
+
throw new FilterV2Error("arithmetic operands must be numeric");
|
|
2652
|
+
}
|
|
2653
|
+
if (typeof left === "bigint" && typeof right === "bigint") {
|
|
2654
|
+
if ((op === "divide" || op === "modulo") && right === 0n)
|
|
2655
|
+
throw new FilterV2Error("division by zero");
|
|
2656
|
+
switch (op) {
|
|
2657
|
+
case "add":
|
|
2658
|
+
return left + right;
|
|
2659
|
+
case "subtract":
|
|
2660
|
+
return left - right;
|
|
2661
|
+
case "multiply":
|
|
2662
|
+
return left * right;
|
|
2663
|
+
case "divide":
|
|
2664
|
+
return context.integerDivision ? left / right : Number(left) / Number(right);
|
|
2665
|
+
case "modulo":
|
|
2666
|
+
return left % right;
|
|
2667
|
+
}
|
|
2668
|
+
}
|
|
2669
|
+
const a = Number(left);
|
|
2670
|
+
const b = Number(right);
|
|
2671
|
+
if ((op === "divide" || op === "modulo") && b === 0 && context.ieeeFloatingPointOps === false) {
|
|
2672
|
+
throw new FilterV2Error("division by zero");
|
|
2673
|
+
}
|
|
2674
|
+
switch (op) {
|
|
2675
|
+
case "add":
|
|
2676
|
+
return a + b;
|
|
2677
|
+
case "subtract":
|
|
2678
|
+
return a - b;
|
|
2679
|
+
case "multiply":
|
|
2680
|
+
return a * b;
|
|
2681
|
+
case "divide":
|
|
2682
|
+
return a / b;
|
|
2683
|
+
case "modulo":
|
|
2684
|
+
return a % b;
|
|
2685
|
+
}
|
|
2686
|
+
}
|
|
2687
|
+
|
|
2688
|
+
class WkbBoundsReader {
|
|
2689
|
+
view;
|
|
2690
|
+
offset = 0;
|
|
2691
|
+
constructor(bytes) {
|
|
2692
|
+
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
2693
|
+
}
|
|
2694
|
+
read() {
|
|
2695
|
+
const bounds = this.geometry();
|
|
2696
|
+
if (this.offset !== this.view.byteLength)
|
|
2697
|
+
throw new FilterV2Error("WKB has trailing bytes");
|
|
2698
|
+
return bounds;
|
|
2699
|
+
}
|
|
2700
|
+
require(length) {
|
|
2701
|
+
if (length < 0 || this.offset + length > this.view.byteLength)
|
|
2702
|
+
throw new FilterV2Error("truncated WKB geometry");
|
|
2703
|
+
}
|
|
2704
|
+
byte() {
|
|
2705
|
+
this.require(1);
|
|
2706
|
+
return this.view.getUint8(this.offset++);
|
|
2707
|
+
}
|
|
2708
|
+
uint32(littleEndian) {
|
|
2709
|
+
this.require(4);
|
|
2710
|
+
const value = this.view.getUint32(this.offset, littleEndian);
|
|
2711
|
+
this.offset += 4;
|
|
2712
|
+
return value;
|
|
2713
|
+
}
|
|
2714
|
+
float64(littleEndian) {
|
|
2715
|
+
this.require(8);
|
|
2716
|
+
const value = this.view.getFloat64(this.offset, littleEndian);
|
|
2717
|
+
this.offset += 8;
|
|
2718
|
+
return value;
|
|
2719
|
+
}
|
|
2720
|
+
point(littleEndian, dimensions) {
|
|
2721
|
+
const x = this.float64(littleEndian);
|
|
2722
|
+
const y = this.float64(littleEndian);
|
|
2723
|
+
for (let dimension = 2;dimension < dimensions; dimension++)
|
|
2724
|
+
this.float64(littleEndian);
|
|
2725
|
+
if (Number.isNaN(x) && Number.isNaN(y))
|
|
2726
|
+
return null;
|
|
2727
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
2728
|
+
throw new FilterV2Error("WKB coordinate must be finite");
|
|
2729
|
+
return { minX: x, minY: y, maxX: x, maxY: y };
|
|
2730
|
+
}
|
|
2731
|
+
count(littleEndian) {
|
|
2732
|
+
const count = this.uint32(littleEndian);
|
|
2733
|
+
if (count > Math.floor((this.view.byteLength - this.offset) / 4)) {
|
|
2734
|
+
throw new FilterV2Error("WKB element count exceeds payload size");
|
|
2735
|
+
}
|
|
2736
|
+
return count;
|
|
2737
|
+
}
|
|
2738
|
+
merge(left, right) {
|
|
2739
|
+
if (!left)
|
|
2740
|
+
return right;
|
|
2741
|
+
if (!right)
|
|
2742
|
+
return left;
|
|
2743
|
+
return {
|
|
2744
|
+
minX: Math.min(left.minX, right.minX),
|
|
2745
|
+
minY: Math.min(left.minY, right.minY),
|
|
2746
|
+
maxX: Math.max(left.maxX, right.maxX),
|
|
2747
|
+
maxY: Math.max(left.maxY, right.maxY)
|
|
2748
|
+
};
|
|
2749
|
+
}
|
|
2750
|
+
points(littleEndian, dimensions) {
|
|
2751
|
+
let bounds = null;
|
|
2752
|
+
const count = this.count(littleEndian);
|
|
2753
|
+
for (let index = 0;index < count; index++)
|
|
2754
|
+
bounds = this.merge(bounds, this.point(littleEndian, dimensions));
|
|
2755
|
+
return bounds;
|
|
2756
|
+
}
|
|
2757
|
+
geometry() {
|
|
2758
|
+
const byteOrder = this.byte();
|
|
2759
|
+
if (byteOrder !== 0 && byteOrder !== 1)
|
|
2760
|
+
throw new FilterV2Error("invalid WKB byte order");
|
|
2761
|
+
const littleEndian = byteOrder === 1;
|
|
2762
|
+
const encodedType = this.uint32(littleEndian);
|
|
2763
|
+
const hasZ = (encodedType & 2147483648) !== 0;
|
|
2764
|
+
const hasM = (encodedType & 1073741824) !== 0;
|
|
2765
|
+
const hasSrid = (encodedType & 536870912) !== 0;
|
|
2766
|
+
let type = encodedType & 268435455;
|
|
2767
|
+
let dimensions = 2 + Number(hasZ) + Number(hasM);
|
|
2768
|
+
if (type >= 3000) {
|
|
2769
|
+
type -= 3000;
|
|
2770
|
+
dimensions = 4;
|
|
2771
|
+
} else if (type >= 2000) {
|
|
2772
|
+
type -= 2000;
|
|
2773
|
+
dimensions = 3;
|
|
2774
|
+
} else if (type >= 1000) {
|
|
2775
|
+
type -= 1000;
|
|
2776
|
+
dimensions = 3;
|
|
2777
|
+
}
|
|
2778
|
+
if (hasSrid)
|
|
2779
|
+
this.uint32(littleEndian);
|
|
2780
|
+
if (type === 1)
|
|
2781
|
+
return this.point(littleEndian, dimensions);
|
|
2782
|
+
if (type === 2)
|
|
2783
|
+
return this.points(littleEndian, dimensions);
|
|
2784
|
+
if (type === 3) {
|
|
2785
|
+
let bounds = null;
|
|
2786
|
+
const rings = this.count(littleEndian);
|
|
2787
|
+
for (let ring = 0;ring < rings; ring++)
|
|
2788
|
+
bounds = this.merge(bounds, this.points(littleEndian, dimensions));
|
|
2789
|
+
return bounds;
|
|
2790
|
+
}
|
|
2791
|
+
if (type >= 4 && type <= 7) {
|
|
2792
|
+
let bounds = null;
|
|
2793
|
+
const geometries = this.count(littleEndian);
|
|
2794
|
+
for (let index = 0;index < geometries; index++)
|
|
2795
|
+
bounds = this.merge(bounds, this.geometry());
|
|
2796
|
+
return bounds;
|
|
2797
|
+
}
|
|
2798
|
+
throw new FilterV2Error(`unsupported WKB geometry type ${type}`);
|
|
2799
|
+
}
|
|
2800
|
+
}
|
|
2801
|
+
function wkbBBox(bytes) {
|
|
2802
|
+
if (!(bytes instanceof Uint8Array))
|
|
2803
|
+
throw new FilterV2Error("spatial extent arguments must be WKB binary values");
|
|
2804
|
+
return new WkbBoundsReader(bytes).read();
|
|
2805
|
+
}
|
|
2806
|
+
function evaluateCall(expression, args) {
|
|
2807
|
+
if (args.some((value) => value === null))
|
|
2808
|
+
return null;
|
|
2809
|
+
if (typeof expression.function === "string") {
|
|
2810
|
+
const [left, right] = args;
|
|
2811
|
+
switch (expression.function) {
|
|
2812
|
+
case "starts_with":
|
|
2813
|
+
return String(left).startsWith(String(right));
|
|
2814
|
+
case "ends_with":
|
|
2815
|
+
return String(left).endsWith(String(right));
|
|
2816
|
+
case "contains":
|
|
2817
|
+
return String(left).includes(String(right));
|
|
2818
|
+
case "list_contains":
|
|
2819
|
+
return Array.isArray(left) && left.some((value) => value !== null && deepEqual(value, right));
|
|
2820
|
+
}
|
|
2821
|
+
}
|
|
2822
|
+
if (filterFunctionIdentityKey(expression.function) === "duckdb.spatial/intersects_extent@1") {
|
|
2823
|
+
const left = wkbBBox(args[0]);
|
|
2824
|
+
const right = wkbBBox(args[1]);
|
|
2825
|
+
return !!left && !!right && !(left.maxX < right.minX || left.minX > right.maxX || left.maxY < right.minY || left.minY > right.maxY);
|
|
2826
|
+
}
|
|
2827
|
+
throw new FilterV2Error("no evaluator for extension filter function");
|
|
2828
|
+
}
|
|
2829
|
+
function evaluateExpression(expression, batch, row, context) {
|
|
2830
|
+
switch (expression.node) {
|
|
2831
|
+
case "column_ref": {
|
|
2832
|
+
const resolved = resolveColumn(batch, expression.columnName);
|
|
2833
|
+
return resolved ? readCanonicalValue(resolved.type, resolved.column, row) : null;
|
|
2834
|
+
}
|
|
2835
|
+
case "field_ref": {
|
|
2836
|
+
const parent = evaluateExpression(expression.expression, batch, row, context);
|
|
2837
|
+
if (parent === null)
|
|
2838
|
+
return null;
|
|
2839
|
+
if (Array.isArray(parent))
|
|
2840
|
+
return parent[expression.fieldIndex] ?? null;
|
|
2841
|
+
return parent[expression.fieldName] ?? null;
|
|
2842
|
+
}
|
|
2843
|
+
case "literal":
|
|
2844
|
+
return expression.value;
|
|
2845
|
+
case "comparison":
|
|
2846
|
+
return compare(evaluateExpression(expression.left, batch, row, context), evaluateExpression(expression.right, batch, row, context), expression.op);
|
|
2847
|
+
case "and":
|
|
2848
|
+
return andKleene(expression.children.map((child) => evaluateExpression(child, batch, row, context)));
|
|
2849
|
+
case "or":
|
|
2850
|
+
return orKleene(expression.children.map((child) => evaluateExpression(child, batch, row, context)));
|
|
2851
|
+
case "not": {
|
|
2852
|
+
const value = evaluateExpression(expression.expression, batch, row, context);
|
|
2853
|
+
return value === null ? null : !value;
|
|
2854
|
+
}
|
|
2855
|
+
case "is_null": {
|
|
2856
|
+
const result = evaluateExpression(expression.expression, batch, row, context) === null;
|
|
2857
|
+
return expression.negated ? !result : result;
|
|
2858
|
+
}
|
|
2859
|
+
case "in": {
|
|
2860
|
+
const input = evaluateExpression(expression.expression, batch, row, context);
|
|
2861
|
+
if (input === null)
|
|
2862
|
+
return null;
|
|
2863
|
+
const matched = expression.set.values.some((value) => value !== null && deepEqual(input, value));
|
|
2864
|
+
let result = matched ? true : expression.set.values.some((value) => value === null) ? null : false;
|
|
2865
|
+
if (expression.negated && result !== null)
|
|
2866
|
+
result = !result;
|
|
2867
|
+
return result;
|
|
2868
|
+
}
|
|
2869
|
+
case "cast":
|
|
2870
|
+
return castValue(evaluateExpression(expression.expression, batch, row, context), expression.field.type);
|
|
2871
|
+
case "arithmetic":
|
|
2872
|
+
return numericBinary(expression.op, evaluateExpression(expression.left, batch, row, context), evaluateExpression(expression.right, batch, row, context), context);
|
|
2873
|
+
case "negate": {
|
|
2874
|
+
const value = evaluateExpression(expression.expression, batch, row, context);
|
|
2875
|
+
return value === null ? null : typeof value === "bigint" ? -value : -Number(value);
|
|
2876
|
+
}
|
|
2877
|
+
case "call":
|
|
2878
|
+
return evaluateCall(expression, expression.arguments.map((argument) => evaluateExpression(argument, batch, row, context)));
|
|
2879
|
+
case "runtime_filter":
|
|
2880
|
+
throw new FilterV2Error("runtime-filter artifact evaluator is unavailable");
|
|
2881
|
+
}
|
|
2882
|
+
}
|
|
2883
|
+
function expressionColumns(expression, output = new Map) {
|
|
2884
|
+
switch (expression.node) {
|
|
2885
|
+
case "column_ref":
|
|
2886
|
+
output.set(expression.columnIndex, expression.columnName);
|
|
2887
|
+
break;
|
|
2888
|
+
case "field_ref":
|
|
2889
|
+
case "not":
|
|
2890
|
+
case "is_null":
|
|
2891
|
+
case "cast":
|
|
2892
|
+
case "negate":
|
|
2893
|
+
expressionColumns(expression.expression, output);
|
|
2894
|
+
break;
|
|
2895
|
+
case "comparison":
|
|
2896
|
+
case "arithmetic":
|
|
2897
|
+
expressionColumns(expression.left, output);
|
|
2898
|
+
expressionColumns(expression.right, output);
|
|
2899
|
+
break;
|
|
2900
|
+
case "and":
|
|
2901
|
+
case "or":
|
|
2902
|
+
expression.children.forEach((child) => expressionColumns(child, output));
|
|
2903
|
+
break;
|
|
2904
|
+
case "in":
|
|
2905
|
+
expressionColumns(expression.expression, output);
|
|
2906
|
+
break;
|
|
2907
|
+
case "call":
|
|
2908
|
+
expression.arguments.forEach((argument) => expressionColumns(argument, output));
|
|
2909
|
+
break;
|
|
2910
|
+
case "runtime_filter":
|
|
2911
|
+
expressionColumns(expression.input, output);
|
|
2912
|
+
break;
|
|
2913
|
+
}
|
|
2914
|
+
return output;
|
|
2915
|
+
}
|
|
2916
|
+
function booleanColumnLeaf(expression) {
|
|
2917
|
+
const column = (node) => node.node === "column_ref" && node.dataType !== undefined && isBool(node.dataType) ? node.columnName : undefined;
|
|
2918
|
+
if (expression.node === "not") {
|
|
2919
|
+
const name = column(expression.expression);
|
|
2920
|
+
return name === undefined ? undefined : { columnName: name, value: false };
|
|
2921
|
+
}
|
|
2922
|
+
const name = column(expression);
|
|
2923
|
+
return name === undefined ? undefined : { columnName: name, value: true };
|
|
2924
|
+
}
|
|
2925
|
+
function discreteValues(expression, columnName) {
|
|
2926
|
+
const leaf = booleanColumnLeaf(expression);
|
|
2927
|
+
if (leaf && leaf.columnName === columnName)
|
|
2928
|
+
return [leaf.value];
|
|
2929
|
+
if (expression.node === "comparison" && expression.op === "eq" /* EQ */) {
|
|
2930
|
+
if (expression.left.node === "column_ref" && expression.left.columnName === columnName && expression.right.node === "literal")
|
|
2931
|
+
return [expression.right.value];
|
|
2932
|
+
if (expression.right.node === "column_ref" && expression.right.columnName === columnName && expression.left.node === "literal")
|
|
2933
|
+
return [expression.left.value];
|
|
2934
|
+
}
|
|
2935
|
+
if (expression.node === "in" && expression.expression.node === "column_ref" && expression.expression.columnName === columnName && !expression.negated)
|
|
2936
|
+
return expression.set.values;
|
|
2937
|
+
if (expression.node === "and") {
|
|
2938
|
+
for (const child of expression.children) {
|
|
2939
|
+
const values = discreteValues(child, columnName);
|
|
2940
|
+
if (values)
|
|
2941
|
+
return values;
|
|
2942
|
+
}
|
|
2943
|
+
}
|
|
2944
|
+
if (expression.node === "or") {
|
|
2945
|
+
const branches = expression.children.map((child) => discreteValues(child, columnName));
|
|
2946
|
+
if (branches.some((values) => values === null))
|
|
2947
|
+
return null;
|
|
2948
|
+
const result = [];
|
|
2949
|
+
for (const values of branches) {
|
|
2950
|
+
for (const value of values)
|
|
2951
|
+
if (!result.some((known) => deepEqual(known, value)))
|
|
2952
|
+
result.push(value);
|
|
2953
|
+
}
|
|
2954
|
+
return result;
|
|
2955
|
+
}
|
|
2956
|
+
return null;
|
|
2957
|
+
}
|
|
2958
|
+
|
|
2959
|
+
class PushdownFilters {
|
|
2960
|
+
predicates;
|
|
2961
|
+
evaluationContext;
|
|
2962
|
+
options;
|
|
2963
|
+
revisions;
|
|
2964
|
+
requiredIds;
|
|
2965
|
+
version = "2";
|
|
2966
|
+
constructor(predicates, evaluationContext, options, revisions = new Map, requiredIds = new Set) {
|
|
2967
|
+
this.predicates = predicates;
|
|
2968
|
+
this.evaluationContext = evaluationContext;
|
|
2969
|
+
this.options = options;
|
|
2970
|
+
this.revisions = revisions;
|
|
2971
|
+
this.requiredIds = requiredIds;
|
|
2972
|
+
}
|
|
2973
|
+
get filters() {
|
|
2974
|
+
return this.predicates.map((predicate) => predicate.expression);
|
|
2975
|
+
}
|
|
2976
|
+
applyDelta(batch) {
|
|
2977
|
+
return applyFilterDelta(this, batch);
|
|
2978
|
+
}
|
|
2979
|
+
withPredicateOrder(order) {
|
|
2980
|
+
const predicates = this.predicates;
|
|
2981
|
+
if (predicates.length === order.length && predicates.every((predicate, i) => predicate.id === order[i])) {
|
|
2982
|
+
return this;
|
|
2983
|
+
}
|
|
2984
|
+
const byId = new Map(predicates.map((predicate) => [predicate.id, predicate]));
|
|
2985
|
+
if (order.length !== byId.size || new Set(order).size !== order.length || !order.every((id) => byId.has(id))) {
|
|
2986
|
+
throw new FilterV2Error("recorded predicate order does not match the replayed filter state");
|
|
2987
|
+
}
|
|
2988
|
+
return new PushdownFilters(order.map((id) => byId.get(id)), this.evaluationContext, this.options, new Map(this.revisions), new Set(this.requiredIds));
|
|
2989
|
+
}
|
|
2990
|
+
evaluate(batch) {
|
|
2991
|
+
const mask = new Uint8Array(batch.numRows);
|
|
2992
|
+
mask.fill(1);
|
|
2993
|
+
for (const predicate of this.predicates) {
|
|
2994
|
+
if (predicate.expression.node === "runtime_filter" && !predicate.expression.supported)
|
|
2995
|
+
continue;
|
|
2996
|
+
let values;
|
|
2997
|
+
try {
|
|
2998
|
+
values = Array.from({ length: batch.numRows }, (_, row) => evaluateExpression(predicate.expression, batch, row, this.evaluationContext));
|
|
2999
|
+
} catch (error) {
|
|
3000
|
+
if (predicate.mode === "advisory")
|
|
3001
|
+
continue;
|
|
3002
|
+
throw error;
|
|
3003
|
+
}
|
|
3004
|
+
for (let row = 0;row < batch.numRows; row++)
|
|
3005
|
+
mask[row] &= values[row] === true ? 1 : 0;
|
|
3006
|
+
}
|
|
3007
|
+
return mask;
|
|
3008
|
+
}
|
|
3009
|
+
apply(batch) {
|
|
3010
|
+
if (!batch.numRows || !this.predicates.length)
|
|
3011
|
+
return batch;
|
|
3012
|
+
return filterBatch(batch, this.evaluate(batch));
|
|
3013
|
+
}
|
|
3014
|
+
filteredColumns() {
|
|
3015
|
+
const columns = new Set;
|
|
3016
|
+
for (const predicate of this.predicates) {
|
|
3017
|
+
for (const name of expressionColumns(predicate.expression).values())
|
|
3018
|
+
columns.add(name);
|
|
3019
|
+
}
|
|
3020
|
+
return [...columns].sort();
|
|
3021
|
+
}
|
|
3022
|
+
hasFilterForColumn(columnName) {
|
|
3023
|
+
return this.filteredColumns().includes(columnName);
|
|
3024
|
+
}
|
|
3025
|
+
getColumnValues(columnName) {
|
|
3026
|
+
for (const predicate of this.predicates) {
|
|
3027
|
+
const values = discreteValues(predicate.expression, columnName);
|
|
3028
|
+
if (values)
|
|
3029
|
+
return values;
|
|
3030
|
+
}
|
|
3031
|
+
return null;
|
|
3032
|
+
}
|
|
3033
|
+
toSql() {
|
|
3034
|
+
return this.predicates.map((predicate) => predicateToSql(predicate.expression)).join(" AND ");
|
|
3035
|
+
}
|
|
3036
|
+
}
|
|
3037
|
+
function quote(value) {
|
|
3038
|
+
if (value === null)
|
|
3039
|
+
return "NULL";
|
|
3040
|
+
if (typeof value === "string")
|
|
3041
|
+
return `'${value.replaceAll("'", "''")}'`;
|
|
3042
|
+
if (value instanceof Uint8Array)
|
|
3043
|
+
return `X'${[...value].map((byte) => byte.toString(16).padStart(2, "0")).join("")}'`;
|
|
3044
|
+
if (Array.isArray(value))
|
|
3045
|
+
return `[${value.map(quote).join(", ")}]`;
|
|
3046
|
+
return String(value);
|
|
3047
|
+
}
|
|
3048
|
+
function predicateToSql(expression) {
|
|
3049
|
+
const leaf = booleanColumnLeaf(expression);
|
|
3050
|
+
return leaf ? `${leaf.columnName} = ${quote(leaf.value)}` : expressionToSql(expression);
|
|
3051
|
+
}
|
|
3052
|
+
function expressionToSql(expression) {
|
|
3053
|
+
switch (expression.node) {
|
|
3054
|
+
case "column_ref":
|
|
3055
|
+
return expression.columnName;
|
|
3056
|
+
case "field_ref":
|
|
3057
|
+
return `${expressionToSql(expression.expression)}.${expression.fieldName}`;
|
|
3058
|
+
case "literal":
|
|
3059
|
+
return quote(expression.value);
|
|
3060
|
+
case "comparison": {
|
|
3061
|
+
const symbols = {
|
|
3062
|
+
["eq" /* EQ */]: "=",
|
|
3063
|
+
["ne" /* NE */]: "!=",
|
|
3064
|
+
["lt" /* LT */]: "<",
|
|
3065
|
+
["le" /* LE */]: "<=",
|
|
3066
|
+
["gt" /* GT */]: ">",
|
|
3067
|
+
["ge" /* GE */]: ">=",
|
|
3068
|
+
["distinct_from" /* DISTINCT_FROM */]: "IS DISTINCT FROM",
|
|
3069
|
+
["not_distinct_from" /* NOT_DISTINCT_FROM */]: "IS NOT DISTINCT FROM"
|
|
3070
|
+
};
|
|
3071
|
+
return `${expressionToSql(expression.left)} ${symbols[expression.op]} ${expressionToSql(expression.right)}`;
|
|
3072
|
+
}
|
|
3073
|
+
case "and":
|
|
3074
|
+
case "or":
|
|
3075
|
+
return `(${expression.children.map(predicateToSql).join(` ${expression.node.toUpperCase()} `)})`;
|
|
3076
|
+
case "not":
|
|
3077
|
+
return `(NOT ${expressionToSql(expression.expression)})`;
|
|
3078
|
+
case "is_null":
|
|
3079
|
+
return `${expressionToSql(expression.expression)} IS ${expression.negated ? "NOT " : ""}NULL`;
|
|
3080
|
+
case "in":
|
|
3081
|
+
return `${expressionToSql(expression.expression)} ${expression.negated ? "NOT " : ""}IN (${expression.set.values.map(quote).join(", ")})`;
|
|
3082
|
+
case "cast":
|
|
3083
|
+
return `CAST(${expressionToSql(expression.expression)} AS type_${expression.typeRef})`;
|
|
3084
|
+
case "arithmetic": {
|
|
3085
|
+
const symbols = { add: "+", subtract: "-", multiply: "*", divide: "/", modulo: "%" };
|
|
3086
|
+
return `(${expressionToSql(expression.left)} ${symbols[expression.op]} ${expressionToSql(expression.right)})`;
|
|
3087
|
+
}
|
|
3088
|
+
case "negate":
|
|
3089
|
+
return `(-${expressionToSql(expression.expression)})`;
|
|
3090
|
+
case "call": {
|
|
3091
|
+
const name = typeof expression.function === "string" ? expression.function : filterFunctionIdentityKey(expression.function);
|
|
3092
|
+
return `${name}(${expression.arguments.map(expressionToSql).join(", ")})`;
|
|
3093
|
+
}
|
|
3094
|
+
case "runtime_filter":
|
|
3095
|
+
return `${filterFunctionIdentityKey(expression.algorithm)}(${expressionToSql(expression.input)})`;
|
|
3096
|
+
}
|
|
3097
|
+
}
|
|
3098
|
+
|
|
3099
|
+
// src/filter-pushdown/deserialize.ts
|
|
3100
|
+
var MAX_JSON_BYTES = 1 << 20;
|
|
3101
|
+
var MAX_DEPTH = 64;
|
|
3102
|
+
var MAX_NODES = 1e4;
|
|
3103
|
+
var MAX_PREDICATES = 1024;
|
|
3104
|
+
var MAX_PREDICATE_IDS = 4096;
|
|
3105
|
+
var MAX_ARGUMENTS = 256;
|
|
3106
|
+
var MAX_ID_BYTES = 128;
|
|
3107
|
+
var MAX_FINGERPRINT_BYTES = 256;
|
|
3108
|
+
var PAYLOAD_NAME = /^(?:value|type|artifact)_(?:0|[1-9][0-9]*)$/;
|
|
3109
|
+
var IDENTITY_NAMESPACE = /^[a-z][a-z0-9]*(?:\.[a-z][a-z0-9_]*)*$/;
|
|
3110
|
+
var IDENTITY_NAME = /^[a-z][a-z0-9_]*$/;
|
|
3111
|
+
var MODES = new Set(["required", "advisory"]);
|
|
3112
|
+
var SOURCES = new Set(["query", "join", "top_n", "split_refinement", "other"]);
|
|
3113
|
+
var STANDARD_FUNCTIONS = new Set([
|
|
3114
|
+
"starts_with",
|
|
3115
|
+
"ends_with",
|
|
3116
|
+
"contains",
|
|
3117
|
+
"list_contains"
|
|
3118
|
+
]);
|
|
3119
|
+
var KNOWN_RUNTIME_ALGORITHMS = new Set([
|
|
3120
|
+
"duckdb.runtime_filter/bloom@1",
|
|
3121
|
+
"duckdb.runtime_filter/prefix_range@1"
|
|
3122
|
+
]);
|
|
3123
|
+
var KNOWN_ARROW_EXTENSIONS = new Set([
|
|
3124
|
+
"arrow.bool8",
|
|
3125
|
+
"arrow.json",
|
|
3126
|
+
"arrow.uuid",
|
|
3127
|
+
"geoarrow.linestring",
|
|
3128
|
+
"geoarrow.multilinestring",
|
|
3129
|
+
"geoarrow.multipoint",
|
|
3130
|
+
"geoarrow.multipolygon",
|
|
3131
|
+
"geoarrow.point",
|
|
3132
|
+
"geoarrow.polygon",
|
|
3133
|
+
"geoarrow.wkb"
|
|
3134
|
+
]);
|
|
3135
|
+
|
|
3136
|
+
class FilterV2Error extends Error {
|
|
3137
|
+
constructor(message) {
|
|
3138
|
+
super(message);
|
|
3139
|
+
this.name = "FilterV2Error";
|
|
3140
|
+
}
|
|
3141
|
+
}
|
|
3142
|
+
function utf8Length(value) {
|
|
3143
|
+
return new TextEncoder().encode(value).byteLength;
|
|
3144
|
+
}
|
|
3145
|
+
function expectObject(value, where) {
|
|
3146
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
3147
|
+
throw new FilterV2Error(`${where} must be a JSON object`);
|
|
3148
|
+
}
|
|
3149
|
+
return value;
|
|
3150
|
+
}
|
|
3151
|
+
function expectArray(value, where) {
|
|
3152
|
+
if (!Array.isArray(value))
|
|
3153
|
+
throw new FilterV2Error(`${where} must be a JSON array`);
|
|
3154
|
+
return value;
|
|
3155
|
+
}
|
|
3156
|
+
function expectString(value, where, allowEmpty = false) {
|
|
3157
|
+
if (typeof value !== "string" || !allowEmpty && value.length === 0) {
|
|
3158
|
+
throw new FilterV2Error(`${where} must be a${allowEmpty ? "" : " nonempty"} string`);
|
|
3159
|
+
}
|
|
3160
|
+
return value;
|
|
3161
|
+
}
|
|
3162
|
+
function expectBoolean(value, where) {
|
|
3163
|
+
if (typeof value !== "boolean")
|
|
3164
|
+
throw new FilterV2Error(`${where} must be a Boolean`);
|
|
3165
|
+
return value;
|
|
3166
|
+
}
|
|
3167
|
+
function expectUint(value, where, positive = false) {
|
|
3168
|
+
if (!Number.isSafeInteger(value) || value < (positive ? 1 : 0)) {
|
|
3169
|
+
throw new FilterV2Error(`${where} must be a safely representable unsigned integer`);
|
|
3170
|
+
}
|
|
3171
|
+
return value;
|
|
3172
|
+
}
|
|
3173
|
+
function expectKeys(object, required, optional, where) {
|
|
3174
|
+
const allowed = new Set([...required, ...optional]);
|
|
3175
|
+
const missing = required.filter((key) => !(key in object));
|
|
3176
|
+
const unknown = Object.keys(object).filter((key) => !allowed.has(key));
|
|
3177
|
+
if (missing.length)
|
|
3178
|
+
throw new FilterV2Error(`${where} is missing ${missing.join(", ")}`);
|
|
3179
|
+
if (unknown.length)
|
|
3180
|
+
throw new FilterV2Error(`${where} has unknown properties ${unknown.join(", ")}`);
|
|
3181
|
+
}
|
|
3182
|
+
function validateArrowExtensions(field) {
|
|
3183
|
+
const names = [
|
|
3184
|
+
field.metadata?.get("ARROW:extension:name"),
|
|
3185
|
+
field.type.extensionName,
|
|
3186
|
+
field.type.extension_name
|
|
3187
|
+
].filter((value) => typeof value === "string");
|
|
3188
|
+
for (const name of names) {
|
|
3189
|
+
if (!KNOWN_ARROW_EXTENSIONS.has(name))
|
|
3190
|
+
throw new FilterV2Error(`unknown Arrow extension type ${name}`);
|
|
3191
|
+
}
|
|
3192
|
+
const children = field.type.children;
|
|
3193
|
+
children?.forEach(validateArrowExtensions);
|
|
3194
|
+
}
|
|
3195
|
+
|
|
3196
|
+
class StrictJsonParser {
|
|
3197
|
+
text;
|
|
3198
|
+
offset = 0;
|
|
3199
|
+
constructor(text) {
|
|
3200
|
+
this.text = text;
|
|
3201
|
+
}
|
|
3202
|
+
parse() {
|
|
3203
|
+
const result = this.value();
|
|
3204
|
+
this.space();
|
|
3205
|
+
if (this.offset !== this.text.length)
|
|
3206
|
+
throw this.error("trailing JSON data");
|
|
3207
|
+
return result;
|
|
3208
|
+
}
|
|
3209
|
+
error(message) {
|
|
3210
|
+
return new FilterV2Error(`invalid filter JSON at byte ${this.offset}: ${message}`);
|
|
3211
|
+
}
|
|
3212
|
+
space() {
|
|
3213
|
+
while (/\s/.test(this.text[this.offset] ?? ""))
|
|
3214
|
+
this.offset++;
|
|
3215
|
+
}
|
|
3216
|
+
value() {
|
|
3217
|
+
this.space();
|
|
3218
|
+
const ch = this.text[this.offset];
|
|
3219
|
+
if (ch === "{")
|
|
3220
|
+
return this.object();
|
|
3221
|
+
if (ch === "[")
|
|
3222
|
+
return this.array();
|
|
3223
|
+
if (ch === '"')
|
|
3224
|
+
return this.string();
|
|
3225
|
+
for (const [word, value] of [["true", true], ["false", false], ["null", null]]) {
|
|
3226
|
+
if (this.text.startsWith(word, this.offset)) {
|
|
3227
|
+
this.offset += word.length;
|
|
3228
|
+
return value;
|
|
3229
|
+
}
|
|
3230
|
+
}
|
|
3231
|
+
const match = this.text.slice(this.offset).match(/^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/);
|
|
3232
|
+
if (match) {
|
|
3233
|
+
this.offset += match[0].length;
|
|
3234
|
+
const number = Number(match[0]);
|
|
3235
|
+
if (!Number.isFinite(number))
|
|
3236
|
+
throw this.error("non-finite number");
|
|
3237
|
+
return number;
|
|
3238
|
+
}
|
|
3239
|
+
throw this.error("expected a JSON value");
|
|
3240
|
+
}
|
|
3241
|
+
string() {
|
|
3242
|
+
const start = this.offset++;
|
|
3243
|
+
let escaped = false;
|
|
3244
|
+
while (this.offset < this.text.length) {
|
|
3245
|
+
const ch = this.text[this.offset++];
|
|
3246
|
+
if (!escaped && ch === '"') {
|
|
3247
|
+
try {
|
|
3248
|
+
return JSON.parse(this.text.slice(start, this.offset));
|
|
3249
|
+
} catch {
|
|
3250
|
+
throw this.error("invalid string escape");
|
|
3251
|
+
}
|
|
3252
|
+
}
|
|
3253
|
+
if (!escaped && ch.charCodeAt(0) < 32)
|
|
3254
|
+
throw this.error("control character in string");
|
|
3255
|
+
if (!escaped && ch === "\\")
|
|
3256
|
+
escaped = true;
|
|
3257
|
+
else
|
|
3258
|
+
escaped = false;
|
|
3259
|
+
}
|
|
3260
|
+
throw this.error("unterminated string");
|
|
3261
|
+
}
|
|
3262
|
+
object() {
|
|
3263
|
+
this.offset++;
|
|
3264
|
+
const result = {};
|
|
3265
|
+
const names = new Set;
|
|
3266
|
+
this.space();
|
|
3267
|
+
if (this.text[this.offset] === "}") {
|
|
3268
|
+
this.offset++;
|
|
3269
|
+
return result;
|
|
3270
|
+
}
|
|
3271
|
+
while (true) {
|
|
3272
|
+
this.space();
|
|
3273
|
+
if (this.text[this.offset] !== '"')
|
|
3274
|
+
throw this.error("expected object key");
|
|
3275
|
+
const key = this.string();
|
|
3276
|
+
if (names.has(key))
|
|
3277
|
+
throw this.error(`duplicate object key ${JSON.stringify(key)}`);
|
|
3278
|
+
names.add(key);
|
|
3279
|
+
this.space();
|
|
3280
|
+
if (this.text[this.offset++] !== ":")
|
|
3281
|
+
throw this.error("expected ':'");
|
|
3282
|
+
result[key] = this.value();
|
|
3283
|
+
this.space();
|
|
3284
|
+
const separator = this.text[this.offset++];
|
|
3285
|
+
if (separator === "}")
|
|
3286
|
+
return result;
|
|
3287
|
+
if (separator !== ",")
|
|
3288
|
+
throw this.error("expected ',' or '}'");
|
|
3289
|
+
}
|
|
3290
|
+
}
|
|
3291
|
+
array() {
|
|
3292
|
+
this.offset++;
|
|
3293
|
+
const result = [];
|
|
3294
|
+
this.space();
|
|
3295
|
+
if (this.text[this.offset] === "]") {
|
|
3296
|
+
this.offset++;
|
|
3297
|
+
return result;
|
|
3298
|
+
}
|
|
3299
|
+
while (true) {
|
|
3300
|
+
result.push(this.value());
|
|
3301
|
+
this.space();
|
|
3302
|
+
const separator = this.text[this.offset++];
|
|
3303
|
+
if (separator === "]")
|
|
3304
|
+
return result;
|
|
3305
|
+
if (separator !== ",")
|
|
3306
|
+
throw this.error("expected ',' or ']'");
|
|
3307
|
+
}
|
|
3308
|
+
}
|
|
3309
|
+
}
|
|
3310
|
+
function metadataValue(schema, key) {
|
|
3311
|
+
const value = schema.metadata?.get(key);
|
|
3312
|
+
if (value === undefined)
|
|
3313
|
+
throw new FilterV2Error(`missing schema metadata ${key}`);
|
|
3314
|
+
return value;
|
|
3315
|
+
}
|
|
3316
|
+
function parseContext(schema) {
|
|
3317
|
+
if (metadataValue(schema, "vgi_filter_encoding") !== FILTER_ENCODING) {
|
|
3318
|
+
throw new FilterV2Error("unsupported filter encoding");
|
|
3319
|
+
}
|
|
3320
|
+
if (metadataValue(schema, "vgi_filter_version") !== FILTER_VERSION) {
|
|
3321
|
+
throw new FilterV2Error("unsupported filter version");
|
|
3322
|
+
}
|
|
3323
|
+
const profile = metadataValue(schema, "vgi_evaluation_context");
|
|
3324
|
+
const contextKeys = [
|
|
3325
|
+
"vgi_time_zone",
|
|
3326
|
+
"vgi_calendar",
|
|
3327
|
+
"vgi_default_collation",
|
|
3328
|
+
"vgi_ieee_floating_point_ops",
|
|
3329
|
+
"vgi_integer_division",
|
|
3330
|
+
"vgi_context_provider_fingerprint"
|
|
3331
|
+
];
|
|
3332
|
+
if (profile === NO_EVALUATION_CONTEXT) {
|
|
3333
|
+
if (contextKeys.some((key) => schema.metadata.has(key))) {
|
|
3334
|
+
throw new FilterV2Error("vgi.none.v1 forbids DuckDB session context metadata");
|
|
3335
|
+
}
|
|
3336
|
+
return { profile };
|
|
3337
|
+
}
|
|
3338
|
+
if (profile !== DUCKDB_SESSION_CONTEXT)
|
|
3339
|
+
throw new FilterV2Error(`unknown evaluation context ${profile}`);
|
|
3340
|
+
const required = contextKeys.slice(0, 5);
|
|
3341
|
+
for (const key of required)
|
|
3342
|
+
metadataValue(schema, key);
|
|
3343
|
+
const bool = (key) => {
|
|
3344
|
+
const value = metadataValue(schema, key);
|
|
3345
|
+
if (value !== "true" && value !== "false")
|
|
3346
|
+
throw new FilterV2Error(`${key} must be true or false`);
|
|
3347
|
+
return value === "true";
|
|
3348
|
+
};
|
|
3349
|
+
const fingerprint = schema.metadata.get("vgi_context_provider_fingerprint");
|
|
3350
|
+
if (fingerprint !== undefined && (!fingerprint || utf8Length(fingerprint) > MAX_FINGERPRINT_BYTES)) {
|
|
3351
|
+
throw new FilterV2Error("invalid context provider fingerprint");
|
|
3352
|
+
}
|
|
3353
|
+
return {
|
|
3354
|
+
profile,
|
|
3355
|
+
timeZone: metadataValue(schema, "vgi_time_zone"),
|
|
3356
|
+
calendar: metadataValue(schema, "vgi_calendar"),
|
|
3357
|
+
defaultCollation: metadataValue(schema, "vgi_default_collation"),
|
|
3358
|
+
ieeeFloatingPointOps: bool("vgi_ieee_floating_point_ops"),
|
|
3359
|
+
integerDivision: bool("vgi_integer_division"),
|
|
3360
|
+
providerFingerprint: fingerprint
|
|
3361
|
+
};
|
|
3362
|
+
}
|
|
3363
|
+
|
|
3364
|
+
class FilterParser {
|
|
3365
|
+
batch;
|
|
3366
|
+
options;
|
|
3367
|
+
nodes = 0;
|
|
3368
|
+
context;
|
|
3369
|
+
document;
|
|
3370
|
+
constructor(batch, options) {
|
|
3371
|
+
this.batch = batch;
|
|
3372
|
+
this.options = options;
|
|
3373
|
+
if (batch.numRows !== 1)
|
|
3374
|
+
throw new FilterV2Error("filter batch must contain exactly one row");
|
|
3375
|
+
const first = batch.schema.fields[0];
|
|
3376
|
+
if (!first || first.name !== "filter_spec" || first.nullable || !isUtf8(first.type)) {
|
|
3377
|
+
throw new FilterV2Error("first field must be filter_spec: utf8 not null");
|
|
3378
|
+
}
|
|
3379
|
+
const names = batch.schema.fields.map((field) => field.name);
|
|
3380
|
+
if (new Set(names).size !== names.length)
|
|
3381
|
+
throw new FilterV2Error("filter payload names must be unique");
|
|
3382
|
+
for (let i = 1;i < names.length; i++) {
|
|
3383
|
+
const name = names[i];
|
|
3384
|
+
if (!PAYLOAD_NAME.test(name))
|
|
3385
|
+
throw new FilterV2Error(`noncanonical payload name ${name}`);
|
|
3386
|
+
if (name.startsWith("type_") && this.valueAt(i) !== null) {
|
|
3387
|
+
throw new FilterV2Error(`${name} must contain NULL`);
|
|
3388
|
+
}
|
|
3389
|
+
}
|
|
3390
|
+
this.context = parseContext(batch.schema);
|
|
3391
|
+
this.validateContextCapability();
|
|
3392
|
+
const raw = this.valueAt(0);
|
|
3393
|
+
if (typeof raw !== "string" || utf8Length(raw) > MAX_JSON_BYTES) {
|
|
3394
|
+
throw new FilterV2Error("filter_spec must be UTF-8 JSON no larger than 1 MiB");
|
|
3395
|
+
}
|
|
3396
|
+
this.document = expectObject(new StrictJsonParser(raw).parse(), "filter document");
|
|
3397
|
+
}
|
|
3398
|
+
valueAt(index) {
|
|
3399
|
+
const column = this.batch.getChildAt(index);
|
|
3400
|
+
const field = this.batch.schema.fields[index];
|
|
3401
|
+
if (!column || !field)
|
|
3402
|
+
throw new FilterV2Error(`missing payload column ${index}`);
|
|
3403
|
+
return readCanonicalValue(field.type, column, 0);
|
|
3404
|
+
}
|
|
3405
|
+
validateContextCapability() {
|
|
3406
|
+
if (this.context.profile === NO_EVALUATION_CONTEXT)
|
|
3407
|
+
return;
|
|
3408
|
+
const matching = (this.options.evaluationContexts ?? []).filter((value) => value.profile === this.context.profile);
|
|
3409
|
+
if (!matching.length)
|
|
3410
|
+
throw new FilterV2Error(`evaluation context ${this.context.profile} was not advertised`);
|
|
3411
|
+
if (this.context.providerFingerprint !== undefined && !matching.some((value) => value.providerFingerprint === this.context.providerFingerprint)) {
|
|
3412
|
+
throw new FilterV2Error("evaluation context fingerprint was not advertised");
|
|
3413
|
+
}
|
|
3414
|
+
}
|
|
3415
|
+
header(kind, member) {
|
|
3416
|
+
expectKeys(this.document, ["encoding", "semantics", "kind", member], [], "filter document");
|
|
3417
|
+
if (this.document.encoding !== FILTER_ENCODING)
|
|
3418
|
+
throw new FilterV2Error("document encoding must be vgi.filters.v2");
|
|
3419
|
+
if (this.document.semantics !== DUCKDB_STANDARD_V1)
|
|
3420
|
+
throw new FilterV2Error("unsupported filter semantics");
|
|
3421
|
+
if (this.document.kind !== kind)
|
|
3422
|
+
throw new FilterV2Error(`expected a ${kind} document`);
|
|
3423
|
+
return expectArray(this.document[member], member);
|
|
3424
|
+
}
|
|
3425
|
+
snapshot() {
|
|
3426
|
+
const entries = this.header("snapshot", "predicates");
|
|
3427
|
+
if (entries.length > MAX_PREDICATES)
|
|
3428
|
+
throw new FilterV2Error("snapshot predicate limit exceeded");
|
|
3429
|
+
const predicates = entries.map((value, index) => this.predicate(expectObject(value, `predicates[${index}]`), `predicates[${index}]`));
|
|
3430
|
+
const ids = new Set;
|
|
3431
|
+
for (const predicate of predicates) {
|
|
3432
|
+
if (predicate.revision !== 0)
|
|
3433
|
+
throw new FilterV2Error("snapshot revisions must be zero");
|
|
3434
|
+
if (ids.has(predicate.id))
|
|
3435
|
+
throw new FilterV2Error(`duplicate predicate id ${predicate.id}`);
|
|
3436
|
+
ids.add(predicate.id);
|
|
3437
|
+
}
|
|
3438
|
+
return new PushdownFilters(predicates, this.context, this.options, new Map(predicates.map((p) => [p.id, 0])), new Set(predicates.filter((p) => p.mode === "required").map((p) => p.id)));
|
|
3439
|
+
}
|
|
3440
|
+
delta(prior) {
|
|
3441
|
+
if (JSON.stringify(this.context) !== JSON.stringify(prior.evaluationContext)) {
|
|
3442
|
+
throw new FilterV2Error("evaluation context changed within one scan");
|
|
3443
|
+
}
|
|
3444
|
+
const entries = this.header("delta", "updates");
|
|
3445
|
+
const live = new Map(prior.predicates.map((predicate) => [predicate.id, predicate]));
|
|
3446
|
+
const revisions = new Map(prior.revisions);
|
|
3447
|
+
const parsed = [];
|
|
3448
|
+
const seen = new Set;
|
|
3449
|
+
for (let index = 0;index < entries.length; index++) {
|
|
3450
|
+
const where = `updates[${index}]`;
|
|
3451
|
+
const object = expectObject(entries[index], where);
|
|
3452
|
+
const operation = expectString(object.operation, `${where}.operation`);
|
|
3453
|
+
const common = operation === "upsert" ? ["operation", "id", "revision", "mode", "source", "expression"] : ["operation", "id", "revision"];
|
|
3454
|
+
expectKeys(object, common, [], where);
|
|
3455
|
+
if (operation !== "upsert" && operation !== "remove")
|
|
3456
|
+
throw new FilterV2Error(`${where} has invalid operation`);
|
|
3457
|
+
const id = this.predicateId(object.id, where);
|
|
3458
|
+
const revision = expectUint(object.revision, `${where}.revision`);
|
|
3459
|
+
if (seen.has(id))
|
|
3460
|
+
throw new FilterV2Error(`duplicate delta predicate id ${id}`);
|
|
3461
|
+
seen.add(id);
|
|
3462
|
+
if (prior.requiredIds.has(id))
|
|
3463
|
+
throw new FilterV2Error(`delta targets required predicate ${id}`);
|
|
3464
|
+
if (operation === "upsert") {
|
|
3465
|
+
const mode = expectString(object.mode, `${where}.mode`);
|
|
3466
|
+
const source = expectString(object.source, `${where}.source`);
|
|
3467
|
+
if (!MODES.has(mode) || !SOURCES.has(source)) {
|
|
3468
|
+
throw new FilterV2Error(`${where} has unknown mode or source`);
|
|
3469
|
+
}
|
|
3470
|
+
expectObject(object.expression, `${where}.expression`);
|
|
3471
|
+
if (mode !== "advisory")
|
|
3472
|
+
throw new FilterV2Error("delta upserts must be advisory");
|
|
3473
|
+
}
|
|
3474
|
+
if (revision <= (revisions.get(id) ?? -1))
|
|
3475
|
+
continue;
|
|
3476
|
+
parsed.push({ id, revision, predicate: operation === "upsert" ? this.predicate(object, where, true) : undefined });
|
|
3477
|
+
}
|
|
3478
|
+
if (new Set([...revisions.keys(), ...parsed.map((entry) => entry.id)]).size > MAX_PREDICATE_IDS) {
|
|
3479
|
+
throw new FilterV2Error("delta predicate-id limit exceeded");
|
|
3480
|
+
}
|
|
3481
|
+
for (const update of parsed) {
|
|
3482
|
+
revisions.set(update.id, update.revision);
|
|
3483
|
+
if (update.predicate)
|
|
3484
|
+
live.set(update.id, update.predicate);
|
|
3485
|
+
else
|
|
3486
|
+
live.delete(update.id);
|
|
3487
|
+
}
|
|
3488
|
+
return new PushdownFilters([...live.values()], this.context, prior.options, revisions, new Set(prior.requiredIds));
|
|
3489
|
+
}
|
|
3490
|
+
predicateId(value, where) {
|
|
3491
|
+
const id = expectString(value, `${where}.id`);
|
|
3492
|
+
if (utf8Length(id) > MAX_ID_BYTES)
|
|
3493
|
+
throw new FilterV2Error(`${where}.id is too long`);
|
|
3494
|
+
return id;
|
|
3495
|
+
}
|
|
3496
|
+
predicate(object, where, update = false) {
|
|
3497
|
+
expectKeys(object, [...update ? ["operation"] : [], "id", "revision", "mode", "source", "expression"], [], where);
|
|
3498
|
+
const mode = expectString(object.mode, `${where}.mode`);
|
|
3499
|
+
const source = expectString(object.source, `${where}.source`);
|
|
3500
|
+
if (!MODES.has(mode) || !SOURCES.has(source))
|
|
3501
|
+
throw new FilterV2Error(`${where} has unknown mode or source`);
|
|
3502
|
+
const expression = this.expression(expectObject(object.expression, `${where}.expression`), 1, true);
|
|
3503
|
+
if (this.context.profile === NO_EVALUATION_CONTEXT && requiresSessionContext(expression)) {
|
|
3504
|
+
throw new FilterV2Error("context-dependent expression requires vgi.duckdb.session.v1");
|
|
3505
|
+
}
|
|
3506
|
+
if (expression.node === "runtime_filter" && mode !== "advisory") {
|
|
3507
|
+
throw new FilterV2Error("runtime_filter predicates must be advisory");
|
|
3508
|
+
}
|
|
3509
|
+
if (expression.node !== "runtime_filter" && !isBooleanExpression(expression)) {
|
|
3510
|
+
throw new FilterV2Error("predicate root must resolve to BOOLEAN");
|
|
3511
|
+
}
|
|
3512
|
+
return {
|
|
3513
|
+
id: this.predicateId(object.id, where),
|
|
3514
|
+
revision: expectUint(object.revision, `${where}.revision`),
|
|
3515
|
+
mode,
|
|
3516
|
+
source,
|
|
3517
|
+
expression
|
|
3518
|
+
};
|
|
3519
|
+
}
|
|
3520
|
+
payload(prefix, rawRef) {
|
|
3521
|
+
const ref = expectUint(rawRef, `${prefix}_ref`);
|
|
3522
|
+
const name = `${prefix}_${ref}`;
|
|
3523
|
+
const indexes = this.batch.schema.fields.flatMap((field, index) => field.name === name ? [index] : []);
|
|
3524
|
+
const index = indexes.length === 1 ? indexes[0] : undefined;
|
|
3525
|
+
if (index === undefined)
|
|
3526
|
+
throw new FilterV2Error(`missing or duplicate payload ${name}`);
|
|
3527
|
+
return { ref, field: this.batch.schema.fields[index], value: this.valueAt(index) };
|
|
3528
|
+
}
|
|
3529
|
+
identity(raw, where) {
|
|
3530
|
+
const object = expectObject(raw, where);
|
|
3531
|
+
expectKeys(object, ["namespace", "name", "version"], [], where);
|
|
3532
|
+
const identity = {
|
|
3533
|
+
namespace: expectString(object.namespace, `${where}.namespace`),
|
|
3534
|
+
name: expectString(object.name, `${where}.name`),
|
|
3535
|
+
version: expectUint(object.version, `${where}.version`, true)
|
|
3536
|
+
};
|
|
3537
|
+
if (!IDENTITY_NAMESPACE.test(identity.namespace) || !IDENTITY_NAME.test(identity.name)) {
|
|
3538
|
+
throw new FilterV2Error(`${where} has a noncanonical identity`);
|
|
3539
|
+
}
|
|
3540
|
+
return identity;
|
|
3541
|
+
}
|
|
3542
|
+
expression(object, depth, root = false) {
|
|
3543
|
+
if (depth > MAX_DEPTH)
|
|
3544
|
+
throw new FilterV2Error("expression depth limit exceeded");
|
|
3545
|
+
if (++this.nodes > MAX_NODES)
|
|
3546
|
+
throw new FilterV2Error("expression node limit exceeded");
|
|
3547
|
+
const node = object.node;
|
|
3548
|
+
const child = (value) => this.expression(expectObject(value, "child expression"), depth + 1);
|
|
3549
|
+
if (node === "column_ref") {
|
|
3550
|
+
expectKeys(object, ["node", "column_index", "column_name"], [], "column_ref");
|
|
3551
|
+
const columnIndex = expectUint(object.column_index, "column_ref.column_index");
|
|
3552
|
+
const columnName = expectString(object.column_name, "column_ref.column_name");
|
|
3553
|
+
const field = this.options.outputSchema.fields[columnIndex];
|
|
3554
|
+
if (!field || field.name !== columnName) {
|
|
3555
|
+
throw new FilterV2Error(`column_ref ${columnName} does not match index ${columnIndex}`);
|
|
3556
|
+
}
|
|
3557
|
+
validateArrowExtensions(field);
|
|
3558
|
+
return { node, columnIndex, columnName, dataType: field.type };
|
|
3559
|
+
}
|
|
3560
|
+
if (node === "field_ref") {
|
|
3561
|
+
expectKeys(object, ["node", "expression", "field_index", "field_name"], [], "field_ref");
|
|
3562
|
+
const expression = child(object.expression);
|
|
3563
|
+
const parent = expressionType(expression);
|
|
3564
|
+
if (!parent || !isStruct(parent))
|
|
3565
|
+
throw new FilterV2Error("field_ref input must be STRUCT");
|
|
3566
|
+
const fieldIndex = expectUint(object.field_index, "field_ref.field_index");
|
|
3567
|
+
const fieldName = expectString(object.field_name, "field_ref.field_name");
|
|
3568
|
+
const field = parent.children?.[fieldIndex];
|
|
3569
|
+
if (!field || field.name !== fieldName)
|
|
3570
|
+
throw new FilterV2Error("field_ref name/index mismatch");
|
|
3571
|
+
return { node, expression, fieldIndex, fieldName, dataType: field.type };
|
|
3572
|
+
}
|
|
3573
|
+
if (node === "literal") {
|
|
3574
|
+
expectKeys(object, ["node", "value_ref"], [], "literal");
|
|
3575
|
+
const payload = this.payload("value", object.value_ref);
|
|
3576
|
+
validateArrowExtensions(payload.field);
|
|
3577
|
+
return { node, valueRef: payload.ref, field: payload.field, value: payload.value };
|
|
3578
|
+
}
|
|
3579
|
+
if (node === "comparison") {
|
|
3580
|
+
expectKeys(object, ["node", "op", "left", "right"], [], "comparison");
|
|
3581
|
+
const op = expectString(object.op, "comparison.op");
|
|
3582
|
+
if (!Object.values(ComparisonOp).includes(op))
|
|
3583
|
+
throw new FilterV2Error(`unknown comparison operator ${op}`);
|
|
3584
|
+
return { node, op, left: child(object.left), right: child(object.right) };
|
|
3585
|
+
}
|
|
3586
|
+
if (node === "and" || node === "or") {
|
|
3587
|
+
expectKeys(object, ["node", "children"], [], node);
|
|
3588
|
+
const children = expectArray(object.children, `${node}.children`).map(child);
|
|
3589
|
+
if (children.length < 2 || children.some((value) => !isBooleanExpression(value))) {
|
|
3590
|
+
throw new FilterV2Error(`${node} requires at least two Boolean children`);
|
|
3591
|
+
}
|
|
3592
|
+
return { node, children };
|
|
3593
|
+
}
|
|
3594
|
+
if (node === "not") {
|
|
3595
|
+
expectKeys(object, ["node", "expression"], [], "not");
|
|
3596
|
+
const expression = child(object.expression);
|
|
3597
|
+
if (!isBooleanExpression(expression))
|
|
3598
|
+
throw new FilterV2Error("not input must be Boolean");
|
|
3599
|
+
return { node, expression };
|
|
3600
|
+
}
|
|
3601
|
+
if (node === "is_null") {
|
|
3602
|
+
expectKeys(object, ["node", "expression", "negated"], [], "is_null");
|
|
3603
|
+
return { node, expression: child(object.expression), negated: expectBoolean(object.negated, "is_null.negated") };
|
|
3604
|
+
}
|
|
3605
|
+
if (node === "in") {
|
|
3606
|
+
expectKeys(object, ["node", "expression", "set", "negated"], [], "in");
|
|
3607
|
+
const setObject = expectObject(object.set, "in.set");
|
|
3608
|
+
const kind = setObject.kind;
|
|
3609
|
+
let parsedSet;
|
|
3610
|
+
if (kind === "literal") {
|
|
3611
|
+
expectKeys(setObject, ["kind", "value_ref"], [], "in.set");
|
|
3612
|
+
const payload = this.payload("value", setObject.value_ref);
|
|
3613
|
+
validateArrowExtensions(payload.field);
|
|
3614
|
+
if (!isList(payload.field.type) || !Array.isArray(payload.value)) {
|
|
3615
|
+
throw new FilterV2Error("literal IN payload must be a non-NULL list");
|
|
3616
|
+
}
|
|
3617
|
+
parsedSet = { kind, valueRef: payload.ref, field: payload.field, values: payload.value };
|
|
3618
|
+
} else if (kind === "external") {
|
|
3619
|
+
expectKeys(setObject, ["kind", "batch_index", "column_index", "column_name"], [], "in.set");
|
|
3620
|
+
const batchIndex = expectUint(setObject.batch_index, "in.set.batch_index");
|
|
3621
|
+
const columnIndex = expectUint(setObject.column_index, "in.set.column_index");
|
|
3622
|
+
const columnName = expectString(setObject.column_name, "in.set.column_name");
|
|
3623
|
+
const batch = this.options.joinKeyBatches?.[batchIndex];
|
|
3624
|
+
const field = batch?.schema.fields[columnIndex];
|
|
3625
|
+
const column = batch?.getChildAt(columnIndex);
|
|
3626
|
+
if (!batch || !field || !column || field.name !== columnName) {
|
|
3627
|
+
throw new FilterV2Error("external IN reference is unavailable or mismatched");
|
|
3628
|
+
}
|
|
3629
|
+
validateArrowExtensions(field);
|
|
3630
|
+
const values = Array.from({ length: batch.numRows }, (_, index) => readCanonicalValue(field.type, column, index));
|
|
3631
|
+
parsedSet = { kind, batchIndex, columnIndex, columnName, values, batch };
|
|
3632
|
+
} else {
|
|
3633
|
+
throw new FilterV2Error("unknown IN set kind");
|
|
3634
|
+
}
|
|
3635
|
+
return { node, expression: child(object.expression), set: parsedSet, negated: expectBoolean(object.negated, "in.negated") };
|
|
3636
|
+
}
|
|
3637
|
+
if (node === "cast") {
|
|
3638
|
+
expectKeys(object, ["node", "expression", "type_ref"], [], "cast");
|
|
3639
|
+
const payload = this.payload("type", object.type_ref);
|
|
3640
|
+
validateArrowExtensions(payload.field);
|
|
3641
|
+
if (payload.value !== null)
|
|
3642
|
+
throw new FilterV2Error("cast type payload must be NULL");
|
|
3643
|
+
return { node, expression: child(object.expression), typeRef: payload.ref, field: payload.field };
|
|
3644
|
+
}
|
|
3645
|
+
if (node === "arithmetic") {
|
|
3646
|
+
expectKeys(object, ["node", "op", "left", "right"], [], "arithmetic");
|
|
3647
|
+
const op = expectString(object.op, "arithmetic.op");
|
|
3648
|
+
if (!["add", "subtract", "multiply", "divide", "modulo"].includes(op)) {
|
|
3649
|
+
throw new FilterV2Error(`unknown arithmetic operator ${op}`);
|
|
3650
|
+
}
|
|
3651
|
+
return { node, op, left: child(object.left), right: child(object.right) };
|
|
3652
|
+
}
|
|
3653
|
+
if (node === "negate") {
|
|
3654
|
+
expectKeys(object, ["node", "expression"], [], "negate");
|
|
3655
|
+
return { node, expression: child(object.expression) };
|
|
3656
|
+
}
|
|
3657
|
+
if (node === "call") {
|
|
3658
|
+
expectKeys(object, ["node", "function", "arguments"], ["options"], "call");
|
|
3659
|
+
let fn;
|
|
3660
|
+
if (typeof object.function === "string") {
|
|
3661
|
+
if (!STANDARD_FUNCTIONS.has(object.function)) {
|
|
3662
|
+
throw new FilterV2Error(`unknown standard filter function ${object.function}`);
|
|
3663
|
+
}
|
|
3664
|
+
fn = object.function;
|
|
3665
|
+
} else {
|
|
3666
|
+
fn = this.identity(object.function, "call.function");
|
|
3667
|
+
const key = filterFunctionIdentityKey(fn);
|
|
3668
|
+
if (!supportsExtensionFilterFunction(fn))
|
|
3669
|
+
throw new FilterV2Error(`unknown extension function ${key}`);
|
|
3670
|
+
if (!(this.options.extensionFunctions ?? []).some((value) => filterFunctionIdentityKey(value) === key)) {
|
|
3671
|
+
throw new FilterV2Error(`extension function ${key} was not advertised`);
|
|
3672
|
+
}
|
|
3673
|
+
}
|
|
3674
|
+
const args = expectArray(object.arguments, "call.arguments");
|
|
3675
|
+
if (args.length > MAX_ARGUMENTS)
|
|
3676
|
+
throw new FilterV2Error("call argument limit exceeded");
|
|
3677
|
+
if (args.length !== 2)
|
|
3678
|
+
throw new FilterV2Error("registered v2 filter functions require exactly two arguments");
|
|
3679
|
+
const parsedArgs = args.map(child);
|
|
3680
|
+
if (typeof fn === "string")
|
|
3681
|
+
validateStandardCall(fn, parsedArgs);
|
|
3682
|
+
const options = object.options === undefined ? undefined : expectObject(object.options, "call.options");
|
|
3683
|
+
if (typeof fn === "string" && options !== undefined)
|
|
3684
|
+
throw new FilterV2Error("standard functions forbid options");
|
|
3685
|
+
if (options && Object.keys(options).length)
|
|
3686
|
+
throw new FilterV2Error("extension function options are unsupported");
|
|
3687
|
+
return { node, function: fn, arguments: parsedArgs, options };
|
|
3688
|
+
}
|
|
3689
|
+
if (node === "runtime_filter") {
|
|
3690
|
+
expectKeys(object, ["node", "algorithm", "input", "artifact_ref", "null_handling"], [], "runtime_filter");
|
|
3691
|
+
if (!root)
|
|
3692
|
+
throw new FilterV2Error("runtime_filter is allowed only at a predicate root");
|
|
3693
|
+
const algorithm = this.identity(object.algorithm, "runtime_filter.algorithm");
|
|
3694
|
+
const key = filterFunctionIdentityKey(algorithm);
|
|
3695
|
+
if (!KNOWN_RUNTIME_ALGORITHMS.has(key))
|
|
3696
|
+
throw new FilterV2Error(`unknown runtime filter ${key}`);
|
|
3697
|
+
const payload = this.payload("artifact", object.artifact_ref);
|
|
3698
|
+
validateArrowExtensions(payload.field);
|
|
3699
|
+
const nullHandling = expectString(object.null_handling, "runtime_filter.null_handling");
|
|
3700
|
+
if (nullHandling !== "pass" && nullHandling !== "reject")
|
|
3701
|
+
throw new FilterV2Error("invalid runtime null handling");
|
|
3702
|
+
const supported = (this.options.runtimeAlgorithms ?? []).some((value) => filterFunctionIdentityKey(value) === key);
|
|
3703
|
+
if (supported)
|
|
3704
|
+
throw new FilterV2Error("runtime algorithm was advertised without an evaluator");
|
|
3705
|
+
return {
|
|
3706
|
+
node,
|
|
3707
|
+
algorithm,
|
|
3708
|
+
input: child(object.input),
|
|
3709
|
+
artifactRef: payload.ref,
|
|
3710
|
+
field: payload.field,
|
|
3711
|
+
artifact: payload.value,
|
|
3712
|
+
nullHandling,
|
|
3713
|
+
supported
|
|
3714
|
+
};
|
|
3715
|
+
}
|
|
3716
|
+
throw new FilterV2Error(`unknown expression node ${String(node)}`);
|
|
3717
|
+
}
|
|
3718
|
+
}
|
|
3719
|
+
function expressionType(expression) {
|
|
3720
|
+
switch (expression.node) {
|
|
3721
|
+
case "column_ref":
|
|
3722
|
+
return expression.dataType;
|
|
3723
|
+
case "field_ref":
|
|
3724
|
+
return expression.dataType;
|
|
3725
|
+
case "literal":
|
|
3726
|
+
return expression.field.type;
|
|
3727
|
+
case "cast":
|
|
3728
|
+
return expression.field.type;
|
|
3729
|
+
default:
|
|
3730
|
+
return;
|
|
3731
|
+
}
|
|
3732
|
+
}
|
|
3733
|
+
function validateStandardCall(fn, args) {
|
|
3734
|
+
const types = args.map(expressionType);
|
|
3735
|
+
let matches = false;
|
|
3736
|
+
if (fn === "starts_with" || fn === "ends_with" || fn === "contains") {
|
|
3737
|
+
matches = types.every((type) => type !== undefined && isUtf8(type));
|
|
3738
|
+
} else if (fn === "list_contains") {
|
|
3739
|
+
const listType = types[0];
|
|
3740
|
+
const needleType = types[1];
|
|
3741
|
+
const element = listType && isList(listType) ? listType.children?.[0]?.type : undefined;
|
|
3742
|
+
matches = element !== undefined && needleType !== undefined && typeSignature(element) === typeSignature(needleType);
|
|
3743
|
+
}
|
|
3744
|
+
if (!matches)
|
|
3745
|
+
throw new FilterV2Error(`${fn} arguments do not bind under vgi.duckdb.standard.v1`);
|
|
3746
|
+
}
|
|
3747
|
+
function isBooleanExpression(expression) {
|
|
3748
|
+
if (["comparison", "and", "or", "not", "is_null", "in", "runtime_filter", "call"].includes(expression.node)) {
|
|
3749
|
+
return true;
|
|
3750
|
+
}
|
|
3751
|
+
const type = expressionType(expression);
|
|
3752
|
+
return type !== undefined && isBool(type);
|
|
3753
|
+
}
|
|
3754
|
+
function requiresSessionContext(expression) {
|
|
3755
|
+
switch (expression.node) {
|
|
3756
|
+
case "arithmetic":
|
|
3757
|
+
return expression.op === "divide" || expression.op === "modulo" || requiresSessionContext(expression.left) || requiresSessionContext(expression.right);
|
|
3758
|
+
case "cast":
|
|
3759
|
+
const source = expressionType(expression.expression);
|
|
3760
|
+
const contextual = (type) => !!type && (isUtf8(type) || isDate(type) || isTime(type) || isTimestamp(type));
|
|
3761
|
+
return contextual(source) && contextual(expression.field.type) || requiresSessionContext(expression.expression);
|
|
3762
|
+
case "field_ref":
|
|
3763
|
+
case "not":
|
|
3764
|
+
case "is_null":
|
|
3765
|
+
case "negate":
|
|
3766
|
+
return requiresSessionContext(expression.expression);
|
|
3767
|
+
case "comparison":
|
|
3768
|
+
return requiresSessionContext(expression.left) || requiresSessionContext(expression.right);
|
|
3769
|
+
case "and":
|
|
3770
|
+
case "or":
|
|
3771
|
+
return expression.children.some(requiresSessionContext);
|
|
3772
|
+
case "in":
|
|
3773
|
+
return requiresSessionContext(expression.expression);
|
|
3774
|
+
case "call":
|
|
3775
|
+
return expression.arguments.some(requiresSessionContext);
|
|
3776
|
+
case "runtime_filter":
|
|
3777
|
+
return requiresSessionContext(expression.input);
|
|
3778
|
+
default:
|
|
3779
|
+
return false;
|
|
3780
|
+
}
|
|
3781
|
+
}
|
|
3782
|
+
function deserializeFilters(batch, options) {
|
|
3783
|
+
return new FilterParser(batch, options).snapshot();
|
|
3784
|
+
}
|
|
3785
|
+
function applyFilterDelta(prior, batch) {
|
|
3786
|
+
return new FilterParser(batch, prior.options).delta(prior);
|
|
3787
|
+
}
|
|
3788
|
+
|
|
2477
3789
|
// src/filter-pushdown/history.ts
|
|
2478
3790
|
var MAX_ENCODED_DELTA_BYTES = 24 << 20;
|
|
2479
3791
|
var MAX_DELTA_IPC_BYTES = 17 << 20;
|
|
@@ -3411,7 +4723,7 @@ import {
|
|
|
3411
4723
|
Protocol
|
|
3412
4724
|
} from "@query-farm/vgi-rpc";
|
|
3413
4725
|
var VGI_PROTOCOL_NAME = "vgi.v3";
|
|
3414
|
-
var VGI_PROTOCOL_VERSION = "3.
|
|
4726
|
+
var VGI_PROTOCOL_VERSION = "3.2.0";
|
|
3415
4727
|
function exchangeStream(callbacks) {
|
|
3416
4728
|
return { kind: "exchange", ...callbacks };
|
|
3417
4729
|
}
|
|
@@ -4281,7 +5593,8 @@ var VGI_V3_METHODS = [
|
|
|
4281
5593
|
field2("name", utf82(), false),
|
|
4282
5594
|
field2("at_unit", utf82(), true),
|
|
4283
5595
|
field2("at_value", utf82(), true),
|
|
4284
|
-
field2("transaction_opaque_data", binary2(), true)
|
|
5596
|
+
field2("transaction_opaque_data", binary2(), true),
|
|
5597
|
+
field2("pushdown_filters", binary2(), true)
|
|
4285
5598
|
]),
|
|
4286
5599
|
result: schema2([
|
|
4287
5600
|
field2("result", binary2(), false)
|
|
@@ -4883,7 +6196,7 @@ function createVgiProtocol(service) {
|
|
|
4883
6196
|
}
|
|
4884
6197
|
|
|
4885
6198
|
// src/generated/vgi-protocol-schemas.ts
|
|
4886
|
-
var VGI_CATALOG_CONTRACT_HASH = "
|
|
6199
|
+
var VGI_CATALOG_CONTRACT_HASH = "d9304d52d78b0ceb90319d9a83759781df4a711b72443790654335c8cfcad9bf";
|
|
4887
6200
|
var CatalogInfoSchema = schema2([
|
|
4888
6201
|
field2("name", utf82(), false),
|
|
4889
6202
|
field2("implementation_version", utf82(), true),
|
|
@@ -4919,7 +6232,8 @@ var TableInfoSchema = schema2([
|
|
|
4919
6232
|
field2("bind_result", binary2(), true),
|
|
4920
6233
|
field2("required_filters", list2(field2("item", list2(field2("item", list2(field2("item", utf82(), true)), true)), true)), false),
|
|
4921
6234
|
field2("options", binary2(), true),
|
|
4922
|
-
field2("layout", binary2(), true)
|
|
6235
|
+
field2("layout", binary2(), true),
|
|
6236
|
+
field2("scan_planning", utf82(), false)
|
|
4923
6237
|
]);
|
|
4924
6238
|
var ViewInfoSchema = schema2([
|
|
4925
6239
|
field2("comment", utf82(), true),
|
|
@@ -5243,7 +6557,9 @@ var ScanBranchSchema = schema2([
|
|
|
5243
6557
|
field2("format_name", utf82(), true),
|
|
5244
6558
|
field2("format_locations", list2(field2("item", utf82(), true)), true),
|
|
5245
6559
|
field2("format_options", binary2(), true),
|
|
5246
|
-
field2("schema_path", list2(field2("item", utf82(), true)), true)
|
|
6560
|
+
field2("schema_path", list2(field2("item", utf82(), true)), true),
|
|
6561
|
+
field2("row_count", int642(), true),
|
|
6562
|
+
field2("column_statistics", binary2(), true)
|
|
5247
6563
|
]);
|
|
5248
6564
|
var ScanSplitSchema = schema2([
|
|
5249
6565
|
field2("payload", binary2(), false),
|
|
@@ -6086,7 +7402,8 @@ var CatalogTableScanBranchesGetParamsSchema = schema2([
|
|
|
6086
7402
|
field2("name", utf82(), false),
|
|
6087
7403
|
field2("at_unit", utf82(), true),
|
|
6088
7404
|
field2("at_value", utf82(), true),
|
|
6089
|
-
field2("transaction_opaque_data", binary2(), true)
|
|
7405
|
+
field2("transaction_opaque_data", binary2(), true),
|
|
7406
|
+
field2("pushdown_filters", binary2(), true)
|
|
6090
7407
|
]);
|
|
6091
7408
|
var CatalogTableScanFunctionGetParamsSchema = schema2([
|
|
6092
7409
|
field2("attach_opaque_data", binary2(), false),
|
|
@@ -10128,6 +11445,219 @@ function servedTableInfo(info) {
|
|
|
10128
11445
|
return served === schema ? info : { ...info, columns: serializeSchema(served) };
|
|
10129
11446
|
}
|
|
10130
11447
|
|
|
11448
|
+
// src/catalog/scan-planning.ts
|
|
11449
|
+
var SCAN_PLANNING_BIND = "bind";
|
|
11450
|
+
var SCAN_PLANNING_FILTERED = "filtered";
|
|
11451
|
+
var SCAN_PLANNING_MODES = Object.freeze([SCAN_PLANNING_BIND, SCAN_PLANNING_FILTERED]);
|
|
11452
|
+
function validateScanPlanning(tableName, scanPlanning, hasInlineScanFunction) {
|
|
11453
|
+
const mode = scanPlanning ?? SCAN_PLANNING_BIND;
|
|
11454
|
+
if (mode !== SCAN_PLANNING_BIND && mode !== SCAN_PLANNING_FILTERED) {
|
|
11455
|
+
throw new Error(`Table '${tableName}': scan_planning must be one of ${JSON.stringify(SCAN_PLANNING_MODES)}, got ${JSON.stringify(mode)}`);
|
|
11456
|
+
}
|
|
11457
|
+
if (mode === SCAN_PLANNING_FILTERED && hasInlineScanFunction) {
|
|
11458
|
+
throw new Error(`Table '${tableName}': scan_planning='filtered' cannot be combined with an inlined scan_function; ` + "the scan redirect must be resolved per query with the query's filters");
|
|
11459
|
+
}
|
|
11460
|
+
return mode;
|
|
11461
|
+
}
|
|
11462
|
+
|
|
11463
|
+
class ScanPlanningFilters {
|
|
11464
|
+
raw;
|
|
11465
|
+
tableColumns;
|
|
11466
|
+
_decoded = null;
|
|
11467
|
+
constructor(raw, tableColumns) {
|
|
11468
|
+
this.raw = raw;
|
|
11469
|
+
this.tableColumns = tableColumns;
|
|
11470
|
+
}
|
|
11471
|
+
decode() {
|
|
11472
|
+
if (this._decoded == null) {
|
|
11473
|
+
this._decoded = (async () => {
|
|
11474
|
+
const columns = await this.tableColumns();
|
|
11475
|
+
if (columns == null) {
|
|
11476
|
+
throw new Error("pushdown_filters: the table's declared columns could not be resolved");
|
|
11477
|
+
}
|
|
11478
|
+
return deserializeFilters(deserializeBatch(this.raw), { outputSchema: columns });
|
|
11479
|
+
})();
|
|
11480
|
+
this._decoded.catch(() => {
|
|
11481
|
+
this._decoded = null;
|
|
11482
|
+
});
|
|
11483
|
+
}
|
|
11484
|
+
return this._decoded;
|
|
11485
|
+
}
|
|
11486
|
+
async mayMatch(statistics) {
|
|
11487
|
+
return filtersMayMatch(await this.decode(), statistics);
|
|
11488
|
+
}
|
|
11489
|
+
}
|
|
11490
|
+
function filtersMayMatch(filters, statistics) {
|
|
11491
|
+
const list = Symbol.iterator in statistics ? statistics : Object.values(statistics);
|
|
11492
|
+
const byName = new Map;
|
|
11493
|
+
for (const stat of list)
|
|
11494
|
+
byName.set(stat.columnName, stat);
|
|
11495
|
+
if (byName.size === 0)
|
|
11496
|
+
return true;
|
|
11497
|
+
for (const predicate of filters.predicates) {
|
|
11498
|
+
if (provablyFalse(predicate.expression, byName))
|
|
11499
|
+
return false;
|
|
11500
|
+
}
|
|
11501
|
+
return true;
|
|
11502
|
+
}
|
|
11503
|
+
var FLIPPED = {
|
|
11504
|
+
["eq" /* EQ */]: "eq" /* EQ */,
|
|
11505
|
+
["ne" /* NE */]: "ne" /* NE */,
|
|
11506
|
+
["lt" /* LT */]: "gt" /* GT */,
|
|
11507
|
+
["le" /* LE */]: "ge" /* GE */,
|
|
11508
|
+
["gt" /* GT */]: "lt" /* LT */,
|
|
11509
|
+
["ge" /* GE */]: "le" /* LE */
|
|
11510
|
+
};
|
|
11511
|
+
function columnStat(expr, stats) {
|
|
11512
|
+
if (expr.node !== "column_ref")
|
|
11513
|
+
return null;
|
|
11514
|
+
const stat = stats.get(expr.columnName);
|
|
11515
|
+
return stat == null ? null : { stat, type: stat.arrowType ?? expr.dataType };
|
|
11516
|
+
}
|
|
11517
|
+
function outside(ref, value, valueType) {
|
|
11518
|
+
const lo = ref.stat.min ?? null;
|
|
11519
|
+
const hi = ref.stat.max ?? null;
|
|
11520
|
+
const cmpLo = lo == null ? null : compare2(lo, ref.type, value, valueType);
|
|
11521
|
+
const cmpHi = hi == null ? null : compare2(hi, ref.type, value, valueType);
|
|
11522
|
+
return cmpLo != null && cmpLo > 0 || cmpHi != null && cmpHi < 0;
|
|
11523
|
+
}
|
|
11524
|
+
function provablyFalse(expr, stats) {
|
|
11525
|
+
switch (expr.node) {
|
|
11526
|
+
case "and":
|
|
11527
|
+
return expr.children.some((child) => provablyFalse(child, stats));
|
|
11528
|
+
case "or":
|
|
11529
|
+
return expr.children.length > 0 && expr.children.every((child) => provablyFalse(child, stats));
|
|
11530
|
+
case "comparison": {
|
|
11531
|
+
let op = expr.op;
|
|
11532
|
+
let ref = columnStat(expr.left, stats);
|
|
11533
|
+
let literal = expr.right;
|
|
11534
|
+
if (ref == null) {
|
|
11535
|
+
ref = columnStat(expr.right, stats);
|
|
11536
|
+
literal = expr.left;
|
|
11537
|
+
op = FLIPPED[expr.op];
|
|
11538
|
+
}
|
|
11539
|
+
if (ref == null || op == null || !(op in FLIPPED) || literal.node !== "literal")
|
|
11540
|
+
return false;
|
|
11541
|
+
if (ref.stat.hasNotNull === false)
|
|
11542
|
+
return true;
|
|
11543
|
+
const c = literal.value;
|
|
11544
|
+
if (c == null)
|
|
11545
|
+
return true;
|
|
11546
|
+
const litType = literal.field?.type;
|
|
11547
|
+
const lo = ref.stat.min ?? null;
|
|
11548
|
+
const hi = ref.stat.max ?? null;
|
|
11549
|
+
const cmpLo = lo == null ? null : compare2(lo, ref.type, c, litType);
|
|
11550
|
+
const cmpHi = hi == null ? null : compare2(hi, ref.type, c, litType);
|
|
11551
|
+
switch (op) {
|
|
11552
|
+
case "eq" /* EQ */:
|
|
11553
|
+
return cmpLo != null && cmpLo > 0 || cmpHi != null && cmpHi < 0;
|
|
11554
|
+
case "ne" /* NE */:
|
|
11555
|
+
return cmpLo === 0 && cmpHi === 0;
|
|
11556
|
+
case "lt" /* LT */:
|
|
11557
|
+
return cmpLo != null && cmpLo >= 0;
|
|
11558
|
+
case "le" /* LE */:
|
|
11559
|
+
return cmpLo != null && cmpLo > 0;
|
|
11560
|
+
case "gt" /* GT */:
|
|
11561
|
+
return cmpHi != null && cmpHi <= 0;
|
|
11562
|
+
case "ge" /* GE */:
|
|
11563
|
+
return cmpHi != null && cmpHi < 0;
|
|
11564
|
+
default:
|
|
11565
|
+
return false;
|
|
11566
|
+
}
|
|
11567
|
+
}
|
|
11568
|
+
case "in": {
|
|
11569
|
+
if (expr.negated || expr.set.kind !== "literal")
|
|
11570
|
+
return false;
|
|
11571
|
+
const ref = columnStat(expr.expression, stats);
|
|
11572
|
+
if (ref == null)
|
|
11573
|
+
return false;
|
|
11574
|
+
if (ref.stat.hasNotNull === false)
|
|
11575
|
+
return true;
|
|
11576
|
+
if (ref.stat.min == null && ref.stat.max == null)
|
|
11577
|
+
return false;
|
|
11578
|
+
const setType = expr.set.field?.type;
|
|
11579
|
+
const values = expr.set.values.filter((v) => v != null);
|
|
11580
|
+
if (values.length === 0)
|
|
11581
|
+
return true;
|
|
11582
|
+
return values.every((v) => outside(ref, v, setType));
|
|
11583
|
+
}
|
|
11584
|
+
case "is_null": {
|
|
11585
|
+
const ref = columnStat(expr.expression, stats);
|
|
11586
|
+
if (ref == null)
|
|
11587
|
+
return false;
|
|
11588
|
+
return expr.negated ? ref.stat.hasNotNull === false : ref.stat.hasNull === false;
|
|
11589
|
+
}
|
|
11590
|
+
default:
|
|
11591
|
+
return false;
|
|
11592
|
+
}
|
|
11593
|
+
}
|
|
11594
|
+
function compare2(a, aType, b, bType) {
|
|
11595
|
+
const x = comparable(a, aType);
|
|
11596
|
+
const y = comparable(b, bType);
|
|
11597
|
+
if (x == null || y == null || x.kind !== y.kind)
|
|
11598
|
+
return null;
|
|
11599
|
+
const u = x.value;
|
|
11600
|
+
const v = y.value;
|
|
11601
|
+
if (typeof u === "number" && Number.isNaN(u))
|
|
11602
|
+
return null;
|
|
11603
|
+
if (typeof v === "number" && Number.isNaN(v))
|
|
11604
|
+
return null;
|
|
11605
|
+
if (typeof u === "string" && typeof v === "string")
|
|
11606
|
+
return compareCodePoints2(u, v);
|
|
11607
|
+
return u < v ? -1 : u > v ? 1 : 0;
|
|
11608
|
+
}
|
|
11609
|
+
function compareCodePoints2(a, b) {
|
|
11610
|
+
const ia = a[Symbol.iterator]();
|
|
11611
|
+
const ib = b[Symbol.iterator]();
|
|
11612
|
+
for (;; ) {
|
|
11613
|
+
const x = ia.next();
|
|
11614
|
+
const y = ib.next();
|
|
11615
|
+
if (x.done || y.done)
|
|
11616
|
+
return x.done && y.done ? 0 : x.done ? -1 : 1;
|
|
11617
|
+
const cx = x.value.codePointAt(0);
|
|
11618
|
+
const cy = y.value.codePointAt(0);
|
|
11619
|
+
if (cx !== cy)
|
|
11620
|
+
return cx < cy ? -1 : 1;
|
|
11621
|
+
}
|
|
11622
|
+
}
|
|
11623
|
+
var MS_PER_DAY2 = 86400000;
|
|
11624
|
+
var NS_PER_UNIT = [1000000000n, 1000000n, 1000n, 1n];
|
|
11625
|
+
function comparable(v, type) {
|
|
11626
|
+
if (type != null) {
|
|
11627
|
+
if (isDate(type)) {
|
|
11628
|
+
if (v instanceof Date)
|
|
11629
|
+
return { kind: "date", value: v.getTime() };
|
|
11630
|
+
if (typeof v === "number" || typeof v === "bigint") {
|
|
11631
|
+
return { kind: "date", value: type.unit === 0 ? Number(v) * MS_PER_DAY2 : Number(v) };
|
|
11632
|
+
}
|
|
11633
|
+
return null;
|
|
11634
|
+
}
|
|
11635
|
+
if (isTimestamp(type)) {
|
|
11636
|
+
const scale = NS_PER_UNIT[type.unit ?? -1];
|
|
11637
|
+
if (scale == null)
|
|
11638
|
+
return null;
|
|
11639
|
+
if (v instanceof Date)
|
|
11640
|
+
return { kind: "timestamp", value: BigInt(v.getTime()) * 1000000n };
|
|
11641
|
+
if (typeof v === "bigint")
|
|
11642
|
+
return { kind: "timestamp", value: v * scale };
|
|
11643
|
+
if (typeof v === "number" && Number.isSafeInteger(v))
|
|
11644
|
+
return { kind: "timestamp", value: BigInt(v) * scale };
|
|
11645
|
+
return null;
|
|
11646
|
+
}
|
|
11647
|
+
if (isDecimal(type) || isTime(type) || isInterval(type) || isDuration(type))
|
|
11648
|
+
return null;
|
|
11649
|
+
}
|
|
11650
|
+
if (v instanceof Date)
|
|
11651
|
+
return null;
|
|
11652
|
+
if (typeof v === "number" || typeof v === "bigint")
|
|
11653
|
+
return { kind: "numeric", value: v };
|
|
11654
|
+
if (typeof v === "string")
|
|
11655
|
+
return { kind: "string", value: v };
|
|
11656
|
+
if (typeof v === "boolean")
|
|
11657
|
+
return { kind: "boolean", value: v ? 1 : 0 };
|
|
11658
|
+
return null;
|
|
11659
|
+
}
|
|
11660
|
+
|
|
10131
11661
|
// src/catalog/column-metadata.ts
|
|
10132
11662
|
function scalarText(value) {
|
|
10133
11663
|
for (let i = 0;i < value.length; i++) {
|
|
@@ -10259,12 +11789,16 @@ function validateColumnMetadataIpc(bytes) {
|
|
|
10259
11789
|
vector(schema, 1, (f) => field(f, 0));
|
|
10260
11790
|
}
|
|
10261
11791
|
function validateTableInfoColumns(info) {
|
|
11792
|
+
validateScanPlanning(info.name, info.scan_planning, info.scan_function != null);
|
|
10262
11793
|
if (info.columns.length) {
|
|
10263
11794
|
validateColumnMetadataIpc(info.columns);
|
|
10264
11795
|
validateColumnExpressionsIpc(info.columns, info.schema_path);
|
|
10265
11796
|
}
|
|
10266
11797
|
}
|
|
10267
11798
|
function servedValidatedTableInfo(info) {
|
|
11799
|
+
const scanPlanning = validateScanPlanning(info.name, info.scan_planning, info.scan_function != null);
|
|
11800
|
+
if (info.scan_planning !== scanPlanning)
|
|
11801
|
+
info = { ...info, scan_planning: scanPlanning };
|
|
10268
11802
|
if (info.columns.length)
|
|
10269
11803
|
validateColumnMetadataIpc(info.columns);
|
|
10270
11804
|
const served = servedTableInfo(info);
|
|
@@ -11660,7 +13194,17 @@ function catalogTableHandlers(getCatalog, signingKey, attachSecretKey) {
|
|
|
11660
13194
|
});
|
|
11661
13195
|
const catalogTableScanBranchesGet = catalogHandler(keys, async (params) => {
|
|
11662
13196
|
const cat = getCatalog();
|
|
11663
|
-
const
|
|
13197
|
+
const attach = toUint8Array(params.attach_opaque_data);
|
|
13198
|
+
const tx = params.transaction_opaque_data ? toUint8Array(params.transaction_opaque_data) : undefined;
|
|
13199
|
+
const schemaPath = params.schema_path;
|
|
13200
|
+
const name = params.name;
|
|
13201
|
+
const atUnit = params.at_unit;
|
|
13202
|
+
const atValue = params.at_value;
|
|
13203
|
+
const filters = params.pushdown_filters != null ? new ScanPlanningFilters(toUint8Array(params.pushdown_filters), async () => {
|
|
13204
|
+
const info = await cat.tableGet(attach, schemaPath, name, atUnit, atValue, tx);
|
|
13205
|
+
return info && info.columns.length ? deserializeSchema(toUint8Array(info.columns)) : null;
|
|
13206
|
+
}) : null;
|
|
13207
|
+
const branchesResult = await cat.tableScanBranchesPlan(attach, schemaPath, name, atUnit, atValue, tx, filters);
|
|
11664
13208
|
return wrapResult(branchesResult, ScanBranchesResultSchema);
|
|
11665
13209
|
});
|
|
11666
13210
|
const catalogTableCommentSet = catalogHandler(keys, async (params) => {
|
|
@@ -12324,4 +13868,4 @@ export {
|
|
|
12324
13868
|
serveVgiWorker
|
|
12325
13869
|
};
|
|
12326
13870
|
|
|
12327
|
-
//# debugId=
|
|
13871
|
+
//# debugId=5DB2BEB30336CB9464756E2164756E21
|