|
| 1 | +"""Decode and evaluate pushed-down filters with an installed vgi-python wheel. |
| 2 | +
|
| 3 | +The ``packaging`` CI job runs this against the built wheel installed with only |
| 4 | +a user-facing engine extra (``haybarn`` or ``duckdb``). The dev extra and dev |
| 5 | +group pull numpy in, which hid the 0.51.0 bug: every filter evaluation called |
| 6 | +the engine's ``create_function``, which needs numpy, and neither engine extra |
| 7 | +declared it, so any pushed-down filter failed to bind. |
| 8 | +
|
| 9 | + python -I tests/packaging/check_filter_evaluation.py [--plain-only] |
| 10 | +
|
| 11 | +``--plain-only`` skips the intersects_extent case; CI uses it after removing |
| 12 | +numpy, to prove an ordinary filter does not need numpy at all. Run it with |
| 13 | +``-I`` from outside the checkout so it imports the installed package. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import json |
| 19 | +import sys |
| 20 | +from collections.abc import Mapping |
| 21 | +from typing import Any |
| 22 | + |
| 23 | +import pyarrow as pa |
| 24 | + |
| 25 | +import vgi |
| 26 | +from vgi import deserialize_filters |
| 27 | + |
| 28 | +_METADATA = { |
| 29 | + b"vgi_filter_encoding": b"vgi.filters.v2", |
| 30 | + b"vgi_filter_version": b"2", |
| 31 | + b"vgi_evaluation_context": b"vgi.none.v1", |
| 32 | +} |
| 33 | +_WKB = {b"ARROW:extension:name": b"geoarrow.wkb", b"ARROW:extension:metadata": b"{}"} |
| 34 | + |
| 35 | + |
| 36 | +def _column(name: str, index: int) -> dict[str, object]: |
| 37 | + return {"node": "column_ref", "column_index": index, "column_name": name} |
| 38 | + |
| 39 | + |
| 40 | +def _snapshot(expression: Mapping[str, object], mode: str, *payload: tuple[pa.Field[Any], object]) -> pa.RecordBatch: |
| 41 | + document = { |
| 42 | + "encoding": "vgi.filters.v2", |
| 43 | + "semantics": "vgi.duckdb.standard.v1", |
| 44 | + "kind": "snapshot", |
| 45 | + "predicates": [ |
| 46 | + {"id": "query:0", "revision": 0, "mode": mode, "source": "query", "expression": dict(expression)} |
| 47 | + ], |
| 48 | + } |
| 49 | + fields: list[pa.Field[Any]] = [ |
| 50 | + pa.field("filter_spec", pa.string(), nullable=False), |
| 51 | + *(field for field, _ in payload), |
| 52 | + ] |
| 53 | + arrays = [pa.array([json.dumps(document)]), *(pa.array([value], type=field.type) for field, value in payload)] |
| 54 | + return pa.RecordBatch.from_arrays(arrays, schema=pa.schema(fields, metadata=_METADATA)) # type: ignore[arg-type] |
| 55 | + |
| 56 | + |
| 57 | +def check_plain() -> None: |
| 58 | + """``WHERE region = 'west' AND amount > 900``, as the pushdown_sqlite starter receives it.""" |
| 59 | + columns: list[pa.Field[Any]] = [pa.field("region", pa.string()), pa.field("amount", pa.int64())] |
| 60 | + schema = pa.schema(columns) |
| 61 | + expression: dict[str, object] = { |
| 62 | + "node": "and", |
| 63 | + "children": [ |
| 64 | + { |
| 65 | + "node": "comparison", |
| 66 | + "op": "eq", |
| 67 | + "left": _column("region", 0), |
| 68 | + "right": {"node": "literal", "value_ref": 0}, |
| 69 | + }, |
| 70 | + { |
| 71 | + "node": "comparison", |
| 72 | + "op": "gt", |
| 73 | + "left": _column("amount", 1), |
| 74 | + "right": {"node": "literal", "value_ref": 1}, |
| 75 | + }, |
| 76 | + ], |
| 77 | + } |
| 78 | + filters = deserialize_filters( |
| 79 | + _snapshot( |
| 80 | + expression, |
| 81 | + "required", |
| 82 | + (pa.field("value_0", pa.string()), "west"), |
| 83 | + (pa.field("value_1", pa.int64()), 900), |
| 84 | + ), |
| 85 | + output_schema=schema, |
| 86 | + ) |
| 87 | + data = pa.RecordBatch.from_pydict( |
| 88 | + {"region": ["west", "west", "east"], "amount": [950, 100, 1000]}, |
| 89 | + schema=schema, |
| 90 | + ) |
| 91 | + got = filters.evaluate(data).to_pylist() |
| 92 | + assert got == [True, False, False], got |
| 93 | + print("plain filter ok:", got) |
| 94 | + |
| 95 | + |
| 96 | +def check_intersects_extent() -> None: |
| 97 | + """``geom && POINT(1 1)``: the duckdb.spatial/intersects_extent@1 UDF, which needs numpy.""" |
| 98 | + point = bytes.fromhex("0101000000000000000000f03f000000000000f03f") # POINT(1 1) |
| 99 | + far = bytes.fromhex("010100000000000000000024400000000000002440") # POINT(10 10) |
| 100 | + schema = pa.schema([pa.field("geom", pa.binary(), metadata=_WKB)]) |
| 101 | + expression: dict[str, object] = { |
| 102 | + "node": "call", |
| 103 | + "function": {"namespace": "duckdb.spatial", "name": "intersects_extent", "version": 1}, |
| 104 | + "arguments": [_column("geom", 0), {"node": "literal", "value_ref": 0}], |
| 105 | + } |
| 106 | + filters = deserialize_filters( |
| 107 | + _snapshot(expression, "advisory", (pa.field("value_0", pa.binary(), metadata=_WKB), point)), |
| 108 | + output_schema=schema, |
| 109 | + extension_functions=frozenset({("duckdb.spatial", "intersects_extent", 1)}), |
| 110 | + ) |
| 111 | + data = pa.RecordBatch.from_arrays([pa.array([point, far, None], pa.binary())], schema=schema) |
| 112 | + got = filters.evaluate(data).to_pylist() |
| 113 | + assert got == [True, False, None], got |
| 114 | + print("intersects_extent filter ok:", got) |
| 115 | + |
| 116 | + |
| 117 | +def main() -> None: |
| 118 | + """Run the checks against the installed wheel.""" |
| 119 | + if "site-packages" not in (vgi.__file__ or ""): |
| 120 | + raise SystemExit(f"vgi imported from {vgi.__file__}, not an installed wheel; run with -I outside the checkout") |
| 121 | + check_plain() |
| 122 | + if "--plain-only" not in sys.argv: |
| 123 | + check_intersects_extent() |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + main() |
0 commit comments