mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-25 11:49:53 -05:00
As finalized in the design review on 10th January, `st_dwithin` shall be used for $center and $centerSphere
For index pushdown `st_dwithin` uses `st_expand` method to calculate the bounding box for given point, so we're using `st_expand` directly at the time of index pushdown, and `st_dwithin` at runtime.
This is part 1 of 2 that implements that change - this returns some false positives as st_dwithin returns true for polygons of which at least 1 point lies within the specified distance. It returns true even if all other points lie outside.
The next part will handle checking all points for distance in runtime.
**Brief summary of design discussion -**
$center used ST_BUFFER to create a buffer area, but using the same for geography has a few drawbacks -
1. If the buffer becomes bigger than a UTM zone or it crosses **the international dateline** it results in undefined behaviour. For example, the above query returns false if we increase the buffer radius to **12000000 (12000 km)** as it crosses the dateline at **(180 90)**.
Hence the query behavior looks like this -
```
[direct: mongos] db> db.geoquerytest.find({"a.b": {$geoWithin: {$centerSphere: [[0, 0], 1.57]}}}) //100013.6 km
[
{ _id: 1, a: { b: [ 10, 10 ] } },
{ _id: 2, a: { b: { long: Long("20"), lat: 20 } } },
{
_id: 3,
a: [
{ b: { long: 25, lat: 25 } },
[ 30, Decimal128("30") ],
[ 35, 35 ]
]
},
{ _id: 5, a: { b: { x: Long("12"), y: 12 } } }
]
[direct: mongos] db> db.geoquerytest.find({"a.b": {$geoWithin: {$centerSphere: [[0, 0], 1.6]}}}) //10205 km
```
2. The circle is **imperfect**. It has 8 segments in each quadrant (by default) which may result in boundary cases like below failing -
`SELECT ST_Covers(ST_BUFFER('Point(0 0)'::geography, 630000), 'Point(4.9757 2.66966)'::geography);` returns **false**.
Similar test on native mongo -
```
test> db.coll4.find({"a": {$geoWithin: {$centerSphere: [[0, 0], 0.0987]}}}) //629.5 km
[
{
_id: ObjectId("658304b574dc0e47e5cab903"),
a: { type: 'Point', coordinates: [ 4.9757, 2.66966 ] }
}
]
```
3. ST_Covers doesn't work well with polygons with holes. So this returns false -
```
SELECT ST_Covers(ST_BUFFER('Point(0 0)'::geography, 50000), 'MultiPolygon(((0.1 0.2, 0.1 0.3, 0.2 0.3, 0.2 0.2, 0.1 0.2), (0.15 0.22, 0.15 0.25, 0.18 0.25, 0.18 0.22, 0.15 0.22)))'::geography);
```
It was decided that ST_DWITHIN be used to overcome these limitations.
There's a bug in Explain behavior where it has a recheck condition even though recheck is set to false, it'll be tracked with this- https://msdata.visualstudio.com/CosmosDB/_workitems/edit/2968574
Related work items: #2903014, #2950457