-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQL: Convert ST_Distance into query when possible (#40595)
* SQL: Convert ST_Distance into query when possible Adds additional optimization logic to convert ST_Distance function calls into geo_distance query when it is called in WHERE clauses.
- Loading branch information
Showing
7 changed files
with
186 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...n/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.expression.function.scalar.geo; | ||
|
||
import org.elasticsearch.xpack.sql.expression.predicate.PredicateBiFunction; | ||
|
||
class StDistanceFunction implements PredicateBiFunction<Object, Object, Double> { | ||
|
||
@Override | ||
public String name() { | ||
return "ST_DISTANCE"; | ||
} | ||
|
||
@Override | ||
public String symbol() { | ||
return "ST_DISTANCE"; | ||
} | ||
|
||
@Override | ||
public Double doApply(Object s1, Object s2) { | ||
return StDistanceProcessor.process(s1, s2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/query/GeoDistanceQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "))"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters