Class EnableSpatialDistancePushdown


When a spatial distance predicate can be pushed down to lucene, this is done by capturing the distance within the same function. In principle this is like re-writing the predicate:
WHERE ST_DISTANCE(field, TO_GEOPOINT("POINT(0 0)")) <= 10000
as:
WHERE ST_INTERSECTS(field, TO_GEOSHAPE("CIRCLE(0,0,10000)"))
.

In addition, since the distance could be calculated in a preceding EVAL command, we also need to consider the case:

     FROM index
     | EVAL distance = ST_DISTANCE(field, TO_GEOPOINT("POINT(0 0)"))
     | WHERE distance <= 10000
 
And re-write that as:
     FROM index
     | WHERE ST_INTERSECTS(field, TO_GEOSHAPE("CIRCLE(0,0,10000)"))
     | EVAL distance = ST_DISTANCE(field, TO_GEOPOINT("POINT(0 0)"))
 
Note that the WHERE clause is both rewritten to an intersection and pushed down closer to the EsQueryExec, which allows the predicate to be pushed down to Lucene in a later rule, PushFiltersToSource.