-
Notifications
You must be signed in to change notification settings - Fork 25.2k
SQL: Convert ST_Distance into query when possible #40595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
*/ | ||
package org.elasticsearch.xpack.sql.planner; | ||
|
||
import org.elasticsearch.geo.geometry.Geometry; | ||
import org.elasticsearch.geo.geometry.Point; | ||
import org.elasticsearch.search.sort.SortOrder; | ||
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; | ||
import org.elasticsearch.xpack.sql.expression.Attribute; | ||
|
@@ -38,6 +40,8 @@ | |
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeFunction; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeHistogramFunction; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.GeoShape; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.geo.StDistance; | ||
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate; | ||
import org.elasticsearch.xpack.sql.expression.literal.Intervals; | ||
import org.elasticsearch.xpack.sql.expression.predicate.Range; | ||
|
@@ -85,6 +89,7 @@ | |
import org.elasticsearch.xpack.sql.querydsl.agg.TopHitsAgg; | ||
import org.elasticsearch.xpack.sql.querydsl.query.BoolQuery; | ||
import org.elasticsearch.xpack.sql.querydsl.query.ExistsQuery; | ||
import org.elasticsearch.xpack.sql.querydsl.query.GeoDistanceQuery; | ||
import org.elasticsearch.xpack.sql.querydsl.query.MatchQuery; | ||
import org.elasticsearch.xpack.sql.querydsl.query.MultiMatchQuery; | ||
import org.elasticsearch.xpack.sql.querydsl.query.NestedQuery; | ||
|
@@ -675,6 +680,21 @@ private static Query translateQuery(BinaryComparison bc) { | |
return new RangeQuery(source, name, value, true, null, false, format); | ||
} | ||
if (bc instanceof LessThan) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't a similar query be applied for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good question. Now that I am thinking about it and after discussions with @iverase, it looks like it should be applied only to |
||
if (bc.left() instanceof StDistance && value instanceof Number) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we expect more similar functions to be optimized in a similar fashion? If the answer is yes, then likely we could have more infrastructure to help identify such patterns. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we will. There will be some cases for shapes, but I don't see the similarity clearly at the moment to create a good abstraction at the moment. I would rather postpone this until we get there and then try to wrap it. |
||
// Special case for ST_Distance translatable into geo_distance query | ||
StDistance stDistance = (StDistance) bc.left(); | ||
if (stDistance.left() instanceof FieldAttribute && stDistance.right().foldable()) { | ||
Object geoShape = valueOf(stDistance.right()); | ||
if (geoShape instanceof GeoShape) { | ||
Geometry geometry = ((GeoShape) geoShape).toGeometry(); | ||
if (geometry instanceof Point) { | ||
String field = nameOf(stDistance.left()); | ||
return new GeoDistanceQuery(source, field, ((Number) value).doubleValue(), | ||
((Point) geometry).getLat(), ((Point) geometry).getLon()); | ||
} | ||
} | ||
} | ||
} | ||
return new RangeQuery(source, name, null, false, value, false, format); | ||
} | ||
if (bc instanceof LessThanOrEqual) { | ||
|
@@ -966,6 +986,9 @@ public QueryTranslation translate(Expression exp, boolean onAggs) { | |
|
||
protected static Query handleQuery(ScalarFunction sf, Expression field, Supplier<Query> query) { | ||
Query q = query.get(); | ||
if (field instanceof StDistance && q instanceof GeoDistanceQuery) { | ||
return wrapIfNested(q, ((StDistance) field).left()); | ||
} | ||
if (field instanceof FieldAttribute) { | ||
return wrapIfNested(q, field); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.sql.querydsl.query; | ||
|
||
import org.elasticsearch.common.unit.DistanceUnit; | ||
import org.elasticsearch.index.query.QueryBuilder; | ||
import org.elasticsearch.index.query.QueryBuilders; | ||
import org.elasticsearch.xpack.sql.tree.Source; | ||
|
||
import java.util.Objects; | ||
|
||
public class GeoDistanceQuery extends LeafQuery { | ||
|
||
private final String field; | ||
private final double lat; | ||
private final double lon; | ||
private final double distance; | ||
|
||
public GeoDistanceQuery(Source source, String field, double distance, double lat, double lon) { | ||
super(source); | ||
this.field = field; | ||
this.distance = distance; | ||
this.lat = lat; | ||
this.lon = lon; | ||
} | ||
|
||
public String field() { | ||
return field; | ||
} | ||
|
||
public double lat() { | ||
return lat; | ||
} | ||
|
||
public double lon() { | ||
return lon; | ||
} | ||
|
||
public double distance() { | ||
return distance; | ||
} | ||
|
||
@Override | ||
public QueryBuilder asBuilder() { | ||
return QueryBuilders.geoDistanceQuery(field).distance(distance, DistanceUnit.METERS).point(lat, lon); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(field, distance, lat, lon); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
GeoDistanceQuery other = (GeoDistanceQuery) obj; | ||
return Objects.equals(field, other.field) && | ||
Objects.equals(distance, other.distance) && | ||
Objects.equals(lat, other.lat) && | ||
Objects.equals(lon, other.lon); | ||
} | ||
|
||
@Override | ||
protected String innerToString() { | ||
return field + ":" + "(" + distance + "," + "(" + lat + ", " + lon + "))"; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's already a rule that does this for
BinaryOperator
s - ifStDistance
extends that calls (and it looks like it can), the swapping will happen automatically.