Skip to content
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

SQL: Add ST_Distance function to geosql #39973

Merged
merged 4 commits into from
Mar 22, 2019

Conversation

imotov
Copy link
Contributor

@imotov imotov commented Mar 12, 2019

Adds ST_Distance function that works on points. No other shapes
are supported at the moments. No optimization and conversion into
geo_distance query is done yet. It will be part of another PR.

Relates to #29872

Adds ST_Distance function that works on points. No other shapes
are supported at the moments. No optimization and conversion into
geo_distance filter is done yet.

Relates to elastic#29872
@imotov imotov added >enhancement :Analytics/Geo Indexing, search aggregations of geo points and shapes :Analytics/SQL SQL querying v8.0.0 labels Mar 12, 2019
@imotov imotov requested review from costin, astefan and matriv March 12, 2019 18:38
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-analytics-geo

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search

Copy link
Member

@costin costin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @astefan @matriv ?


selectDistance
// tag::distance
SELECT ST_Distance(ST_WKTToSQL('POINT (10 20)'), ST_WKTToSQL('POINT (20 30)')) distance;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a query with rounding as well (to the nearest m I suppose).


@Override
protected String scriptMethodName() {
return "distance";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add the st prefix to avoid potential future name clashes with another 'distance' method?

}

// processes doc value as a geometry
public static <T> GeoShape geoDocValue(Map<String, ScriptDocValues<T>> doc, String fieldName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the future the plan is to move away from docvalues and use source instead. Would that work for geo or not? In other words, are the normalized doc values needed (like it is for dates due to formatting) or is the input already normalized (e.g. numbers)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do it for hit extractors, but we just need to do some additional logic for different geo_point format parsing, which is not a huge deal. However, source is not available in sorting and filtering script contexts, so I am not sure how this is going to work there.

@@ -417,7 +418,8 @@ public static TemporalAmount parseInterval(Source source, String value, DataType

entries.add(new Entry(IntervalDayTime.class, IntervalDayTime.NAME, IntervalDayTime::new));
entries.add(new Entry(IntervalYearMonth.class, IntervalYearMonth.NAME, IntervalYearMonth::new));
entries.add(new Entry(GeoShape.class, GeoShape.NAME, GeoShape::new));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the new literal is a non interval, the getNamedWriteables could be extracted to a separate class Literals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I think I will extract this in a separate PR that will go into master and 7.x first. And then resolve conflicts in my branch before merging it. Otherwise, I will be fighting this refactoring until I merge geosql.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #40058

Copy link
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Left few comments, though.



selectCitiesByDistance
SELECT region, city, ST_Distance(location, ST_WktToSQL('POINT (-71 42)')) distance FROM geo WHERE distance < 5000000 ORDER BY region, city;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does GROUP BY distance work? Maybe round the result of the ST_Distance method and then use it a GROUP BY?

throw new SqlIllegalArgumentException("distance calculation is only supported for points; received [{}]", shape1);
}
if (shape2.shapeBuilder instanceof PointBuilder == false) {
throw new SqlIllegalArgumentException("distance calculation is only supported for points; received [{}]", shape1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message here I think it should refer to shape2.


@Override
public boolean equals(Object o) {
if (this == o) return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use curly brackets for all these conditions, please? I think this is the overall style used in the other classes from SQL.

if (this == o) {
   return true;
}
if (o == null || getClass() != o.getClass()) {
   return false;
}

imotov added a commit to imotov/elasticsearch that referenced this pull request Mar 14, 2019
Since other classes besides intervals can be serialized as part of
the Cursor, the getNamedWritables method should be moved from Intervals
to a more generic class Literals.

Relates to elastic#39973
imotov added a commit that referenced this pull request Mar 15, 2019
Since other classes besides intervals can be serialized as part of
the Cursor, the getNamedWritables method should be moved from Intervals
to a more generic class Literals.

Relates to #39973
imotov added a commit that referenced this pull request Mar 15, 2019
Since other classes besides intervals can be serialized as part of
the Cursor, the getNamedWritables method should be moved from Intervals
to a more generic class Literals.

Relates to #39973
Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

public StDistanceProcessor(Processor source1, Processor source2) {
super(source1, source2);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: extra empty line

public String getWriteableName() {
return NAME;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

@imotov imotov merged commit 82ad403 into elastic:geosql Mar 22, 2019
@imotov imotov removed the v8.0.0 label Apr 24, 2019
@imotov imotov deleted the add-st-distance-for-points-only branch May 1, 2020 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/Geo Indexing, search aggregations of geo points and shapes :Analytics/SQL SQL querying >enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants