-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
imotov
merged 4 commits into
elastic:geosql
from
imotov:add-st-distance-for-points-only
Mar 22, 2019
Merged
Changes from all commits
Commits
Show all changes
4 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 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
16 changes: 16 additions & 0 deletions
16
.../multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/GeoJdbcCsvSpecIT.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,16 @@ | ||
/* | ||
* 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.qa.multi_node; | ||
|
||
import org.elasticsearch.xpack.sql.qa.geo.GeoCsvSpecTestCase; | ||
import org.elasticsearch.xpack.sql.qa.jdbc.CsvTestUtils.CsvTestCase; | ||
|
||
public class GeoJdbcCsvSpecIT extends GeoCsvSpecTestCase { | ||
public GeoJdbcCsvSpecIT(String fileName, String groupName, String testName, Integer lineNumber, CsvTestCase testCase) { | ||
super(fileName, groupName, testName, lineNumber, testCase); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
.../multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/GeoJdbcSqlSpecIT.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,15 @@ | ||
/* | ||
* 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.qa.multi_node; | ||
|
||
import org.elasticsearch.xpack.sql.qa.geo.GeoSqlSpecTestCase; | ||
|
||
public class GeoJdbcSqlSpecIT extends GeoSqlSpecTestCase { | ||
public GeoJdbcSqlSpecIT(String fileName, String groupName, String testName, Integer lineNumber, String query) { | ||
super(fileName, groupName, testName, lineNumber, query); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,4 +146,58 @@ Asia |Singapore | |
Asia |Sydney | ||
Europe |Amsterdam | ||
Europe |Berlin | ||
; | ||
; | ||
|
||
|
||
selectCitiesByDistance | ||
SELECT region, city, ST_Distance(location, ST_WktToSQL('POINT (-71 42)')) distance FROM geo WHERE distance < 5000000 ORDER BY region, city; | ||
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. Does GROUP BY distance work? Maybe round the result of the ST_Distance method and then use it a GROUP BY? |
||
|
||
region:s | city:s | distance:d | ||
Americas |Chicago |1373941.5140200066 | ||
Americas |Mountain View |4335936.909375596 | ||
Americas |New York |285839.6579622518 | ||
Americas |Phoenix |3692895.0346903414 | ||
Americas |San Francisco |4343565.010996301 | ||
; | ||
|
||
selectCitiesByDistanceFloored | ||
SELECT region, city, FLOOR(ST_Distance(location, ST_WktToSQL('POINT (-71 42)'))) distance FROM geo WHERE distance < 5000000 ORDER BY region, city; | ||
|
||
region:s | city:s | distance:l | ||
Americas |Chicago |1373941 | ||
Americas |Mountain View |4335936 | ||
Americas |New York |285839 | ||
Americas |Phoenix |3692895 | ||
Americas |San Francisco |4343565 | ||
; | ||
|
||
selectCitiesOrderByDistance | ||
SELECT region, city FROM geo ORDER BY ST_Distance(location, ST_WktToSQL('POINT (-71 42)')) ; | ||
|
||
region:s | city:s | ||
Americas |New York | ||
Americas |Chicago | ||
Americas |Phoenix | ||
Americas |Mountain View | ||
Americas |San Francisco | ||
Europe |London | ||
Europe |Paris | ||
Europe |Amsterdam | ||
Europe |Berlin | ||
Europe |Munich | ||
Asia |Tokyo | ||
Asia |Seoul | ||
Asia |Hong Kong | ||
Asia |Singapore | ||
Asia |Sydney | ||
; | ||
|
||
groupCitiesByDistance | ||
SELECT COUNT(*) count, FIRST(region) region FROM geo GROUP BY FLOOR(ST_Distance(location, ST_WktToSQL('POINT (-71 42)'))/5000000); | ||
|
||
count:l | region:s | ||
5 |Americas | ||
5 |Europe | ||
3 |Asia | ||
2 |Asia | ||
; |
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
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
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
82 changes: 82 additions & 0 deletions
82
.../src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistance.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,82 @@ | ||
/* | ||
* 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.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.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 StDistance(Source source, Expression source1, Expression source2) { | ||
super(source, source1, source2); | ||
} | ||
|
||
@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; | ||
} | ||
|
||
@Override | ||
protected NodeInfo<StDistance> info() { | ||
return NodeInfo.create(this, StDistance::new, left(), right()); | ||
} | ||
|
||
@Override | ||
public ScriptTemplate scriptWithField(FieldAttribute field) { | ||
return new ScriptTemplate(processScript("{sql}.geoDocValue(doc,{})"), | ||
paramsBuilder().variable(field.exactAttribute().name()).build(), | ||
dataType()); | ||
} | ||
|
||
@Override | ||
public Object fold() { | ||
return process(left().fold(), right().fold()); | ||
} | ||
|
||
@Override | ||
protected Pipe makePipe() { | ||
return new StDistancePipe(source(), this, Expressions.pipe(left()), Expressions.pipe(right())); | ||
} | ||
|
||
@Override | ||
protected String scriptMethodName() { | ||
return "stDistance"; | ||
} | ||
} |
Oops, something went wrong.
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.
Please add a query with rounding as well (to the nearest m I suppose).