-
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -9,45 +9,33 @@ | |
import org.elasticsearch.xpack.sql.expression.Expression; | ||
import org.elasticsearch.xpack.sql.expression.Expressions; | ||
import org.elasticsearch.xpack.sql.expression.FieldAttribute; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.BinaryScalarFunction; | ||
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe; | ||
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate; | ||
import org.elasticsearch.xpack.sql.expression.predicate.BinaryOperator; | ||
import org.elasticsearch.xpack.sql.expression.predicate.PredicateBiFunction; | ||
import org.elasticsearch.xpack.sql.tree.NodeInfo; | ||
import org.elasticsearch.xpack.sql.tree.Source; | ||
import org.elasticsearch.xpack.sql.type.DataType; | ||
|
||
import static org.elasticsearch.xpack.sql.expression.TypeResolutions.isGeo; | ||
import static org.elasticsearch.xpack.sql.expression.function.scalar.geo.StDistanceProcessor.process; | ||
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder; | ||
|
||
/** | ||
* Calculates the distance between two points | ||
*/ | ||
public class StDistance extends BinaryScalarFunction { | ||
public class StDistance extends BinaryOperator<Object, Object, Double, StDistance.StDistanceFunction> { | ||
|
||
private static final StDistanceFunction FUNCTION = new StDistanceFunction(); | ||
|
||
public StDistance(Source source, Expression source1, Expression source2) { | ||
super(source, source1, source2); | ||
super(source, source1, source2, FUNCTION); | ||
} | ||
|
||
@Override | ||
protected StDistance replaceChildren(Expression newLeft, Expression newRight) { | ||
return new StDistance(source(), newLeft, newRight); | ||
} | ||
|
||
@Override | ||
protected TypeResolution resolveType() { | ||
if (!childrenResolved()) { | ||
return new TypeResolution("Unresolved children"); | ||
} | ||
|
||
TypeResolution resolution = isGeo(left(), functionName(), Expressions.ParamOrdinal.FIRST); | ||
if (resolution.unresolved()) { | ||
return resolution; | ||
} | ||
|
||
return isGeo(right(), functionName(), Expressions.ParamOrdinal.SECOND); | ||
} | ||
|
||
@Override | ||
public DataType dataType() { | ||
return DataType.DOUBLE; | ||
|
@@ -66,8 +54,13 @@ public ScriptTemplate scriptWithField(FieldAttribute field) { | |
} | ||
|
||
@Override | ||
public Object fold() { | ||
return process(left().fold(), right().fold()); | ||
protected TypeResolution resolveInputType(Expression e, Expressions.ParamOrdinal paramOrdinal) { | ||
return isGeo(e, sourceText(), paramOrdinal); | ||
} | ||
|
||
@Override | ||
public StDistance swapLeftAndRight() { | ||
return new StDistance(source(), right(), left()); | ||
} | ||
|
||
@Override | ||
|
@@ -79,4 +72,22 @@ protected Pipe makePipe() { | |
protected String scriptMethodName() { | ||
return "stDistance"; | ||
} | ||
|
||
public static class StDistanceFunction implements PredicateBiFunction<Object, Object, Double> { | ||
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 would define it in as a normal class in a separate file instead. 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. 👍 |
||
|
||
@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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'd rename it to
StDistanceOperator
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.
ST_DISTANCE(sh1, sh2)
is not an operator, we are just reusing some logic that can be applied to both.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.
Sorry, I had a mistake. My suggestion is to rename
StDistance
toStDistanceOperation
andStDistanceFunction
toStDistance
similarly to other functions/operators in SQL.