Skip to content

query: run (if:) filters that need geometry after the other filters - #239

Open
cldellow wants to merge 1 commit into
metadata-filters-firstfrom
if-geometry-filters-last
Open

cldellow wants to merge 1 commit into
metadata-filters-firstfrom
if-geometry-filters-last

Conversation

@cldellow

Copy link
Copy Markdown
Owner

Stacked on #43 (metadata-filters-first): it extends the filter ordering that PR introduces. The tables compare against #43's branch.

Fixes #238.

Change

  • Query_Constraint (statements/statement.h) gains filter_needs_geometry(), false by default.
  • Filter_Constraint (statements/filter.cc) returns true when its criterion's request_context() asks for geometry (Set_Usage::GEOMETRY).
  • Query_Statement::apply_all_filters (statements/query.cc) now runs three passes: metadata-only filters, then the other filters except those needing geometry, then those. Within a pass the query order is kept.

Three files, 15 lines added, 2 removed. No change to the database format, the query API or the results.

Impact

US northeast full-history extract (Geofabrik), warm cache, 4-core VM. Builds run alternately; median user+sys CPU and peak RSS per query. Base is #43's branch (b5e4a92).

Queries with a geometry (if:) before a spatial filter, 5 alternating runs:

Query CPU, base CPU, this PR change peak, base peak, this PR
f3p285 5.44 s 0.14 s -97% 187 MB 23 MB
f3p695 4.20 s 0.05 s -99% 113 MB 20 MB
f4e1209 6.57 s 0.56 s -91% 203 MB 37 MB
f4e1380 5.52 s 0.13 s -98% 184 MB 27 MB
f4e212 17.12 s 17.63 s +3% 76 MB 76 MB
f4e304 1.49 s 1.46 s -2% 25 MB 26 MB
total 40.3 s 20.0 s -50.5%
Query CPU B/A (95% CI) this PR faster
f3p285 0.024 (0.020–0.030) 5 of 5
f3p695 0.011 (0.010–0.013) 5 of 5
f4e1209 0.084 (0.073–0.096) 5 of 5
f4e1380 0.024 (0.023–0.025) 5 of 5
f4e212 1.026 (0.975–1.080) 1 of 5
f4e304 0.976 (0.840–1.134) 3 of 5

f4e212 and f4e304 are controls. f4e304 is way(if:version()>2)(poly:...): a metadata (if:) before an expensive polygon test. An earlier version of this change moved every (if:) filter last and made that query about 15% slower, so filters that need no geometry now keep their place. f4e212 has a geometry (if:) that the polygon filter doesn't narrow much.

The 20 production-shaped queries (the 20 heaviest query templates on the public servers in June 2026, moved to Boston and New England coordinates), 3 runs each. None has an (if:) filter, so the only difference is one virtual call per constraint; the total moved , within noise. Identical output on all 20.

Fuzzing, against all-changes. Identical output on 2,500 queries with the evaluators feature: seed 4, 1,500 queries, run on the first version; seed 5, 1,000 queries, on this version. Total CPU fell 9.3% over seed 4's 1,478 completed queries (4.20 → 0.13 s and 5.39 → 0.41 s among them) and 3.2% over seed 5's 988, which has no case this large and ran 4 jobs in parallel.

Queries

User names and user ids drawn from the extract are replaced with (user) and N.

f3p285

[timeout:60][maxsize:536870912][out:json];
wr(if:length()>10)(around:10,40.580976,-75.522374)->.s0;
way(r.s0:"house")->.s1;
rel.s0(39.638451,-80.311484,39.819817,-80.194662)->.s2;
.s2 <<->.s3;
.s2 out meta center qt 20;

f3p695

[timeout:60][maxsize:536870912][out:json];
rel(42.682518,-73.840076,42.718593,-73.543843)(id:3005173,6989276,7672245,7820514,9677800,11587250,11965467,15270817,17283238,19718903,20632593)->.s0;
nwr(if:length()>1000)(around:10,44.239956,-75.979621)(if:uid()>N)->.s1;
.s0 out skel;
.s1 out meta qt;

f4e1209

[timeout:60][maxsize:536870912];
wr(if:(date(timestamp())<=lon()))(40.800085,-73.694023,40.831245,-73.649382)->.s0;
.s0 convert item ::id=id(),k2=number(t["addr:country"]),k3=lrs_union(lrs_isect(t["surface"],"a;b;c"),lrs_isect(lrs_union(t["maxspeed"],"name"),"yes")),k1=lrs_union(t["ele"],"name"),k0=1->.s1;
.s0 out ids;

f4e1380

[timeout:60][maxsize:536870912];
wr(if:(is_closed()&&lrs_in(length(),lrs_union("1;2",t["foot"]))))(42.832616,-72.442535,42.963214,-72.428395)[!"height"]->.s0;
if ((!(s0.sum(lrs_min("a;b;c"))==3))) { make yes n=s0.count(nwr)->.s1; } else { make no n=0->.s1; }
node(way_link.s1:1)->.s2;
way(bn.s2)->.s3;
.s1 convert row k1=(suffix((type()+suffix(t["tiger:zip_left"])))+(t["highway"]+count_distinct_members())),::geom=trace(geom()),k0=((is_number(t["building"])?(timestamp()):(count_members()))<lrs_min(lrs_union(t["footway"],"a;b;c")))->.s4;
.s2 convert row ::id=id(),tags=count_tags();
out;
.s3 out tags;

f4e212

[timeout:60][maxsize:536870912];
wr(poly:"40.630631 -74.181555 40.630618 -74.183851 40.630701 -74.182742 40.630640 -74.183781 40.630665 -74.183721 40.630937 -74.180484 40.630937 -74.180815 40.630779 -74.182693 40.630759 -74.183420 40.630683 -74.184335 40.630854 -74.182996 40.631152 -74.181111 40.631369 -74.180497 40.630877 -74.183711 40.630989 -74.183188 40.631451 -74.181242 40.631875 -74.179976 40.631536 -74.181931 40.631414 -74.182659 40.631015 -74.184322 40.631645 -74.182273 40.631416 -74.183097 40.632182 -74.180673 40.631143 -74.184180 40.632056 -74.181772 40.632278 -74.181323 40.631927 -74.182323 40.631370 -74.183996 40.632451 -74.181272 40.632833 -74.180497 40.632909 -74.180337 40.631887 -74.183067 40.632579 -74.181555 40.632802 -74.181187 40.631446 -74.184237 40.631316 -74.184604 40.632661 -74.182113 40.632996 -74.181455 40.631771 -74.183891 40.632339 -74.182903 40.632668 -74.182333 40.632878 -74.181950 40.631969 -74.183741 40.633411 -74.181547 40.631469 -74.184791 40.632974 -74.182348 40.632105 -74.183824 40.633598 -74.181448 40.632682 -74.182918 40.632383 -74.183619 40.631765 -74.184587 40.632754 -74.183281 40.632503 -74.183812 40.632461 -74.183914 40.632751 -74.183673 40.632700 -74.183954 40.634305 -74.182147 40.634202 -74.182424 40.633546 -74.183188 40.632686 -74.184158 40.634558 -74.182674 40.632622 -74.184477 40.633455 -74.183707 40.632804 -74.184317 40.633887 -74.183341 40.633571 -74.183994 40.634875 -74.182962 40.633829 -74.183926 40.634396 -74.183511 40.633371 -74.184321 40.632423 -74.185081 40.632772 -74.184886 40.634773 -74.183514 40.631873 -74.185530 40.634107 -74.184043 40.634226 -74.183983 40.633866 -74.184342 40.634207 -74.184149 40.633318 -74.184718 40.633366 -74.184727 40.634852 -74.183892 40.632983 -74.185135 40.634052 -74.184658 40.634004 -74.184798 40.634558 -74.184919 40.632137 -74.185923 40.635430 -74.185024 40.634268 -74.185373 40.632410 -74.186059 40.633180 -74.185967 40.632346 -74.186188 40.634847 -74.185781 40.634646 -74.185883 40.632210 -74.186254 40.634833 -74.186114 40.633385 -74.186288 40.633608 -74.186468 40.633298 -74.186489 40.632058 -74.186556 40.633474 -74.186618 40.634296 -74.186751 40.632411 -74.186640 40.635565 -74.186996 40.633931 -74.186863 40.634338 -74.186947 40.632688 -74.186832 40.633618 -74.187146 40.632754 -74.186985 40.633832 -74.187343 40.634889 -74.187610 40.635274 -74.187995 40.632911 -74.187454 40.635263 -74.188518 40.632753 -74.187545 40.634237 -74.188290 40.632886 -74.187738 40.632786 -74.187698 40.634753 -74.188990 40.634928 -74.189206 40.634412 -74.188947 40.635058 -74.189378 40.632446 -74.187852 40.632435 -74.187866 40.632626 -74.188028 40.633525 -74.188750 40.634426 -74.189435 40.633992 -74.189146 40.632485 -74.188080 40.632922 -74.188500 40.634020 -74.189441 40.632109 -74.187887 40.631831 -74.187665 40.633888 -74.189722 40.634623 -74.190431 40.633397 -74.189449 40.633199 -74.189587 40.632697 -74.189093 40.632960 -74.189401 40.632107 -74.188503 40.633924 -74.190733 40.632076 -74.188664 40.632437 -74.189258 40.633218 -74.190502 40.633317 -74.190697 40.632651 -74.189935 40.632541 -74.189867 40.631568 -74.188331 40.631702 -74.188581 40.632630 -74.190103 40.633037 -74.190838 40.631689 -74.188756 40.632154 -74.189639 40.631598 -74.188640 40.632343 -74.190078 40.631634 -74.188788 40.633034 -74.191572 40.632112 -74.189797 40.631665 -74.188940 40.633235 -74.192397 40.631517 -74.188937 40.631505 -74.188933 40.632309 -74.191370 40.631617 -74.189660 40.631185 -74.188548 40.631300 -74.188939 40.632471 -74.192276 40.631184 -74.188874 40.631544 -74.190066 40.631258 -74.189206 40.631444 -74.189820 40.631319 -74.189588 40.631356 -74.189731 40.631939 -74.192239 40.631626 -74.192337 40.630983 -74.189387 40.630970 -74.189553 40.631085 -74.190806 40.630711 -74.188770 40.630996 -74.191891 40.631001 -74.192620 40.630627 -74.188822 40.630735 -74.190097 40.630891 -74.192177 40.630686 -74.189820 40.630900 -74.193650 40.630661 -74.191484 40.630671 -74.192967 40.630573 -74.190833 40.630526 -74.190341 40.630430 -74.190179 40.630428 -74.193489 40.630409 -74.190395 40.630400 -74.191579 40.630295 -74.191604 40.630160 -74.192000 40.630276 -74.188850 40.630109 -74.190778 40.629927 -74.192230 40.629834 -74.193212 40.629998 -74.191127 40.630089 -74.190143 40.630013 -74.190639 40.629650 -74.193628 40.630014 -74.190139 40.630073 -74.189573 40.629833 -74.190953 40.630099 -74.188866 40.629718 -74.191201 40.629731 -74.190976 40.629080 -74.193374 40.629558 -74.190781 40.629282 -74.192083 40.629561 -74.190593 40.629779 -74.189436 40.629031 -74.192753 40.629211 -74.191234 40.629706 -74.189297 40.629438 -74.190321 40.629353 -74.190191 40.628461 -74.192829 40.628665 -74.191967 40.628239 -74.192357 40.629047 -74.190071 40.628164 -74.191805 40.629253 -74.189223 40.629431 -74.188810 40.627994 -74.191903 40.629406 -74.188762 40.628801 -74.189720 40.627854 -74.191420 40.627656 -74.191540 40.629438 -74.188232 40.628540 -74.189644 40.628495 -74.189704 40.627870 -74.190351 40.628286 -74.189717 40.628133 -74.189819 40.628721 -74.188864 40.627719 -74.190221 40.627831 -74.189910 40.627183 -74.190557 40.629112 -74.188134 40.628759 -74.188503 40.627545 -74.189498 40.626566 -74.190455 40.626589 -74.190273 40.627458 -74.189334 40.628852 -74.187989 40.627336 -74.189206 40.628458 -74.188176 40.627920 -74.188619 40.629081 -74.187606 40.627663 -74.188653 40.627791 -74.188405 40.627040 -74.188849 40.629018 -74.187455 40.628400 -74.187835 40.626719 -74.188729 40.626221 -74.188965 40.628262 -74.187764 40.627758 -74.187994 40.625821 -74.189039 40.627750 -74.187769 40.626873 -74.188078 40.628924 -74.187133 40.626647 -74.187998 40.626664 -74.187839 40.627617 -74.187423 40.628337 -74.187130 40.627318 -74.187408 40.626083 -74.187565 40.627606 -74.187172 40.627616 -74.187075 40.628071 -74.186976 40.626362 -74.187204 40.627432 -74.187009 40.626622 -74.187102 40.626570 -74.187014 40.627111 -74.186921 40.628286 -74.186730 40.626910 -74.186811 40.628016 -74.186698 40.625479 -74.186819 40.628036 -74.186650 40.625480 -74.186766 40.626477 -74.186578 40.628390 -74.186516 40.628025 -74.186514 40.625737 -74.186413 40.627008 -74.186300 40.627073 -74.186272 40.625579 -74.186154 40.626626 -74.186216 40.628413 -74.186352 40.626847 -74.186222 40.628774 -74.186305 40.627830 -74.186183 40.627419 -74.186114 40.625978 -74.185882 40.627509 -74.186096 40.628779 -74.186226 40.625521 -74.185616 40.625840 -74.185570 40.626254 -74.185650 40.628843 -74.186182 40.628416 -74.186055 40.627561 -74.185773 40.625983 -74.185356 40.628305 -74.185920 40.627856 -74.185761 40.628512 -74.185952 40.628607 -74.185907 40.625620 -74.184900 40.626887 -74.185252 40.628906 -74.185931 40.627632 -74.185295 40.628487 -74.185653 40.627578 -74.185215 40.626825 -74.184683 40.627860 -74.185196 40.626636 -74.184451 40.626475 -74.184294 40.627196 -74.184653 40.628639 -74.185478 40.628558 -74.185393 40.627867 -74.184968 40.626999 -74.184347 40.627949 -74.184859 40.626788 -74.184048 40.627860 -74.184720 40.628838 -74.185301 40.628503 -74.184955 40.626778 -74.183493 40.628710 -74.184913 40.627120 -74.183030 40.628358 -74.184229 40.627131 -74.182818 40.627788 -74.183359 40.626722 -74.181732 40.628251 -74.183670 40.627627 -74.182815 40.628875 -74.184452 40.627918 -74.183146 40.628046 -74.183091 40.628204 -74.183050 40.628988 -74.184262 40.629358 -74.184738 40.629023 -74.184151 40.628010 -74.182397 40.627413 -74.181378 40.628886 -74.183765 40.627979 -74.181759 40.629377 -74.184380 40.629504 -74.184576 40.627865 -74.181100 40.628182 -74.181698 40.628247 -74.181511 40.629219 -74.183722 40.628505 -74.181879 40.628676 -74.182073 40.629531 -74.184229 40.628701 -74.182044 40.629199 -74.183188 40.629713 -74.184385 40.628492 -74.180590 40.629604 -74.183959 40.628696 -74.181126 40.629430 -74.183334 40.628939 -74.181120 40.629209 -74.182029 40.628814 -74.180468 40.629280 -74.182167 40.629620 -74.183445 40.628999 -74.180629 40.629873 -74.184027 40.629500 -74.181727 40.629881 -74.183397 40.629973 -74.183644 40.629489 -74.179820 40.629661 -74.181026 40.629904 -74.182545 40.630075 -74.183775 40.629541 -74.179464 40.630066 -74.183154 40.630022 -74.182627 40.630068 -74.182922 40.629877 -74.180974 40.629839 -74.180400 40.629918 -74.180995 40.629878 -74.179429 40.630065 -74.180780 40.630130 -74.181282 40.630202 -74.182413 40.630318 -74.183675 40.630344 -74.181732 40.630359 -74.181335 40.630401 -74.183677 40.630398 -74.180918 40.630424 -74.183542 40.630423 -74.182187")->.s0;
complete(3).s0->.s1 { node(w); way(bn)["ele"]; }
.s0 out count;
.s1 out tags geom asc;

f4e304

[timeout:60][maxsize:536870912][out:json];
way(if:version()>2)(poly:"42.653896 -73.763673 42.659175 -73.758712 42.664419 -73.754858 42.658609 -73.773408 42.668674 -73.764519 42.672867 -73.763185 42.682956 -73.766108 42.672046 -73.774382 42.664983 -73.781595 42.678357 -73.783602 42.661129 -73.783315 42.683763 -73.786463 42.672580 -73.798109 42.637272 -73.827398 42.640914 -73.812227 42.635586 -73.810298 42.616572 -73.805762 42.629920 -73.787023 42.632289 -73.784688 42.615967 -73.785768 42.633000 -73.781119 42.625916 -73.772039 42.635097 -73.761709 42.635432 -73.747745 42.645671 -73.766280 42.646278 -73.764973 42.646627 -73.761917 42.648754 -73.753077 42.649088 -73.760033 42.649190 -73.753902")->.s0;
foreach.s0->.it{ .it out ids; (.it; .it >;)->.x; .x out count; }
.s0 out skel;

Testing

  • Byte-identical output to the base on the 6 queries above and the 20 production-shaped queries.
  • fuzz.py against all-changes, seeds 4 and 5 with evaluators,complete: 2,500 queries, no difference.
  • CI: fuzz, history and props run on this PR.

Limitations and open questions

  • Only (if:) filters needing geometry move. One that needs tags only is cheap per element but still loads tags; it keeps its place.
  • Other filters (around, poly, bbox) still run in query order among themselves. Ordering them by cost is a separate, larger question.


Generated by Claude Code

Query_Statement::apply_all_filters runs metadata-only filters first and
the others in query order. An (if:) filter builds what its expression
needs for every element it evaluates (Set_With_Context::prefetch); for
geometry that is a way or relation geometry store over all candidates.
Written before a spatial filter, as in wr(if:length()>10)(around:10,...),
it computed the geometry of every relation in the coarse candidate ranges,
which the exact around test then nearly all dropped.

Query_Constraint gains filter_needs_geometry(). Filter_Constraint answers
it from its criterion's request_context() (Set_Usage::GEOMETRY), and
apply_all_filters runs such filters in a last pass. (if:) filters on tags,
ids or metadata keep their place, so they still drop candidates before an
expensive spatial test. The filters only remove elements, so the order
doesn't change the result.

On a US northeast full-history extract (Geofabrik), warm cache, 4-core VM,
median of 5 alternating runs:

  [out:json]; wr(if:length()>10)(around:10,40.580976,-75.522374)->.s0; way(r.s0:"house")->.s1; rel.s0(39.638451,-80.311484,39.819817,-80.194662)->.s2; .s2 <<->.s3; .s2 out meta center qt 20;
      CPU 5.44 s -> 0.14 s, peak RSS 187 MB -> 23 MB

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJWpJaGbt8ML6jz9N35
@cldellow cldellow added query-memory query-perf Query performance change; collected on all-changes labels Nov 26, 2026 — with Claude
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

query-memory query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants