Skip to content

Commit cc80534

Browse files
rustyconoverclaude
andauthored
Specify intersects_extent@1 and evaluate it as DuckDB 2.0 does (#105)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c702d3a commit cc80534

8 files changed

Lines changed: 11403 additions & 3 deletions

File tree

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
"""Generate the duckdb.spatial/intersects_extent@1 conformance cases.
2+
3+
The expected results come from DuckDB core's `&&` (st_intersects_extent), the
4+
reference semantics in vgi-filter-encoding-v2-spec.md section 5.11.1. Regenerate
5+
with the DuckDB release the cases should track:
6+
7+
uv run --no-project --with duckdb==2.0.0 python -I \
8+
docs/protocol/conformance/gen_intersects_extent_v1.py \
9+
docs/protocol/conformance/intersects_extent_v1.json
10+
"""
11+
12+
import json
13+
import struct
14+
import sys
15+
16+
import duckdb
17+
18+
WKT = [
19+
("pt_origin", "POINT(0 0)"),
20+
("pt_one", "POINT(1 1)"),
21+
("pt_half", "POINT(0.5 0.5)"),
22+
("pt_far", "POINT(5 5)"),
23+
("pt_neg", "POINT(-1 -1)"),
24+
("pt_just_outside", "POINT(1.0000000000000002 0.5)"),
25+
("pt_huge", "POINT(1e300 -1e300)"),
26+
("pt_nan_x", "POINT(nan 1)"),
27+
("pt_nan_y", "POINT(1 nan)"),
28+
("pt_nan_xy", "POINT(nan nan)"),
29+
("pt_inf_x", "POINT(inf 0.5)"),
30+
("pt_neginf_x", "POINT(-inf 0.5)"),
31+
("pt_empty", "POINT EMPTY"),
32+
("pt_z", "POINT Z (0.5 0.5 99)"),
33+
("pt_m", "POINT M (0.5 0.5 7)"),
34+
("pt_zm", "POINT ZM (2 2 1 1)"),
35+
("pt_z_nan_z", "POINT Z (0.5 0.5 nan)"),
36+
("ls_diag", "LINESTRING(0 0, 1 1)"),
37+
("ls_edge", "LINESTRING(1 0, 2 0)"),
38+
("ls_far", "LINESTRING(3 3, 4 4)"),
39+
("ls_empty", "LINESTRING EMPTY"),
40+
("ls_nan_vertex", "LINESTRING(nan 0, 2 2)"),
41+
("poly_unit", "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))"),
42+
("poly_far", "POLYGON((2 2, 3 2, 3 3, 2 3, 2 2))"),
43+
("poly_hole", "POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (4 4, 6 4, 6 6, 4 6, 4 4))"),
44+
("poly_empty", "POLYGON EMPTY"),
45+
("mpt", "MULTIPOINT((0 0), (2 2))"),
46+
("mpt_with_empty", "MULTIPOINT(EMPTY, (3 3))"),
47+
("mpt_empty", "MULTIPOINT EMPTY"),
48+
("mls", "MULTILINESTRING((0 0, 0 1), (5 5, 6 6))"),
49+
("mpoly", "MULTIPOLYGON(((2 2, 3 2, 3 3, 2 3, 2 2)), ((7 7, 8 7, 8 8, 7 8, 7 7)))"),
50+
("mpoly_empty", "MULTIPOLYGON EMPTY"),
51+
("gc_empty", "GEOMETRYCOLLECTION EMPTY"),
52+
("gc_empty_parts", "GEOMETRYCOLLECTION(POINT EMPTY, LINESTRING EMPTY)"),
53+
("gc_mixed", "GEOMETRYCOLLECTION(POINT EMPTY, POINT(0.25 0.75))"),
54+
("gc_nested", "GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(POINT(2.5 2.5)), LINESTRING(9 9, 10 10))"),
55+
("gc_nan_part", "GEOMETRYCOLLECTION(POINT(nan 1), POINT(3 3))"),
56+
("ls_nan_nan_vertex", "LINESTRING(nan nan, 5 5)"),
57+
("ls_all_nan", "LINESTRING(nan nan, nan nan)"),
58+
("ls_mixed_nan", "LINESTRING(nan 1, 1 nan)"),
59+
("poly_nan_vertex", "POLYGON((nan nan, 5 5, 6 6, nan nan))"),
60+
("mpt_nan_member", "MULTIPOINT((nan nan), (5 5))"),
61+
("gc_nan_nan_point", "GEOMETRYCOLLECTION(POINT(nan nan), POINT(3 3))"),
62+
("pt_z_nan_xy", "POINT Z (nan nan 5)"),
63+
("pt_z_all_nan", "POINT Z (nan nan nan)"),
64+
("mpt_z_nan_xy_member", "MULTIPOINT Z ((nan nan 5), (9 9 9))"),
65+
]
66+
67+
68+
def to_be(wkb: bytes) -> bytes:
69+
"""Rewrite little-endian ISO WKB as big-endian."""
70+
out = bytearray()
71+
pos = 0
72+
73+
def rd(fmt):
74+
nonlocal pos
75+
v = struct.unpack_from("<" + fmt, wkb, pos)
76+
pos += struct.calcsize(fmt)
77+
return v
78+
79+
def geom():
80+
nonlocal pos
81+
assert wkb[pos] == 1
82+
pos += 1
83+
out.append(0)
84+
(t,) = rd("I")
85+
out.extend(struct.pack(">I", t))
86+
base, dims = t % 1000, 2 + {0: 0, 1: 1, 2: 1, 3: 2}[t // 1000]
87+
88+
def pts(n):
89+
for _ in range(n * dims):
90+
out.extend(struct.pack(">d", rd("d")[0]))
91+
92+
if base == 1:
93+
pts(1)
94+
elif base == 2:
95+
(n,) = rd("I")
96+
out.extend(struct.pack(">I", n))
97+
pts(n)
98+
elif base == 3:
99+
(r,) = rd("I")
100+
out.extend(struct.pack(">I", r))
101+
for _ in range(r):
102+
(n,) = rd("I")
103+
out.extend(struct.pack(">I", n))
104+
pts(n)
105+
elif base in (4, 5, 6, 7):
106+
(n,) = rd("I")
107+
out.extend(struct.pack(">I", n))
108+
for _ in range(n):
109+
geom()
110+
else:
111+
raise ValueError(t)
112+
113+
geom()
114+
assert pos == len(wkb)
115+
return bytes(out)
116+
117+
118+
con = duckdb.connect()
119+
con.execute("LOAD spatial")
120+
version = con.execute(
121+
"SELECT version(), (SELECT extension_version FROM duckdb_extensions() WHERE extension_name='spatial')"
122+
).fetchall()[0]
123+
geoms = []
124+
for name, wkt in WKT:
125+
le = bytes(con.execute("SELECT ST_AsWKB(?::GEOMETRY)", [wkt]).fetchall()[0][0])
126+
be = to_be(le)
127+
# round-trip check: DuckDB reads the BE bytes as the same geometry
128+
same = con.execute("SELECT ST_AsWKB(ST_GeomFromWKB(?)) = ?", [be, le]).fetchall()[0][0]
129+
assert same, name
130+
geoms.append({"name": name, "wkt": wkt, "wkb_le": le.hex(), "wkb_be": be.hex()})
131+
cases = []
132+
for a in geoms:
133+
for b in geoms:
134+
r = con.execute(
135+
"SELECT ST_GeomFromWKB(from_hex(?)) && ST_GeomFromWKB(from_hex(?))", [a["wkb_le"], b["wkb_le"]]
136+
).fetchall()[0][0]
137+
cases.append({"left": a["name"], "right": b["name"], "expected": r})
138+
doc = {
139+
"description": (
140+
"duckdb.spatial/intersects_extent@1 (`a && b`) results from DuckDB core. An SDK evaluator must"
141+
" return `expected` for every case, for both the little-endian (wkb_le) and big-endian (wkb_be)"
142+
" encodings of each side. Generated by gen_intersects_extent_v1.py."
143+
),
144+
"duckdb_version": version[0],
145+
"spatial_version": version[1],
146+
"geometries": geoms,
147+
"cases": cases,
148+
}
149+
with open(sys.argv[1], "w") as fh:
150+
json.dump(doc, fh, indent=1)
151+
print(len(geoms), "geometries,", len(cases), "cases,", sum(c["expected"] for c in cases), "true")

0 commit comments

Comments
 (0)