Skip to content

geo: avoid false negatives for geography ST_DWithin - #175071

Open
Alignyx wants to merge 2 commits into
cockroachdb:masterfrom
Alignyx:fix-80374-geography-intersection-false-negatives
Open

Alignyx wants to merge 2 commits into
cockroachdb:masterfrom
Alignyx:fix-80374-geography-intersection-false-negatives

Conversation

@Alignyx

@Alignyx Alignyx commented Sep 12, 2026

Copy link
Copy Markdown

Summary

Prevent false negatives when a geography ST_DWithin query uses the geography inverted index. The fix keeps the inverted scan and exact residual predicate, but removes two lossy bounding-box assumptions that can discard a true match before exact evaluation.

Fixes #80374

What was wrong

Geography is represented on the sphere, and a polygon can describe the large side of its boundary. Its coordinate bounding box does not necessarily contain the represented spherical region. The DWithin covering path used a bounding-box fallback in this situation, so the generated S2 spans could omit cells that contain a qualifying indexed value.

The optional geography pre-filter made the same assumption: it derived a bounding rectangle and rejected candidates from that rectangle before the exact ST_DWithin residual predicate ran. For a large-side geography polygon, that rejection is not conservative and can turn a true result into a false negative.

Implementation

  • Replace the bounding-box fallback in s2GeographyIndex.DWithin with the covering produced from the actual S2 region representation.
  • Add a single relationship/type guard for pre-filter construction. The guard disables the optional pre-filter only for Geography plus DWithin.
  • Apply the guard at both production construction sites: inverted filter extraction and the geography inverted-expression builder.

Other geometry relationships and other geography relationships retain their existing pre-filter behavior. The inverted index scan remains in the plan for geography DWithin; only the unsafe bounding-cap pruning is removed. Any extra candidates admitted by the conservative spans are still checked by the exact residual predicate.

Why this fixes the reported case

The query-side S2 covering now follows the represented spherical region rather than a coordinate rectangle that may describe only the short side of a polygon boundary. The optional pre-filter can no longer reject a geography DWithin candidate using that non-conservative rectangle. This removes the false-negative path while preserving exact result filtering and index-based execution.

Validation

  • Built the affected geography index and query-observation targets.
  • Ran the geography index package tests.
  • Checked the reported intersection/DWithin cases, preserved relationships, and held-out large-side geography cases.
  • Verified that the optimizer continues to produce inverted-index scans with the exact residual predicate where applicable.
  • Ran the configured full regression and compared it with the baseline; all configured checks passed.

@Alignyx
Alignyx requested review from a team as code owners September 12, 2026 02:18
@Alignyx
Alignyx requested review from michae2 and removed request for a team September 12, 2026 02:18
@blathers-crl

blathers-crl Bot commented Sep 12, 2026

Copy link
Copy Markdown

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

Before a member of our team reviews your PR, I have some potential action items for you:

  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Sep 12, 2026
Use the reported oriented polygon to compare forced primary and inverted scans with both argument orders and sphere/spheroid distance modes. Exercise runtime inverted-join filters with matching, nonmatching, and NULL probes, including NULL and empty indexed values.

Release note: None
@blathers-crl

blathers-crl Bot commented Sep 13, 2026

Copy link
Copy Markdown

Thank you for updating your pull request.

Before a member of our team reviews your PR, I have some potential action items for you:

  • We notice you have more than one commit in your PR. We try break logical changes into separate commits, but commits such as "fix typo" or "address review commits" should be squashed into one commit and pushed with --force
  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

geoindex: false negatives producing incorrect results for geography intersection

1 participant