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: Switch JDBC driver to libs/geo objects for geo_shapes #39204

Merged
merged 4 commits into from
Feb 27, 2019

Conversation

imotov
Copy link
Contributor

@imotov imotov commented Feb 20, 2019

Switches JDBC driver to use libs/geo objects when server returns a
geo_shape. This PR doesn't address the geo_points yet, which will be
addressed in a separate PR.

Switches JDBC driver to use libs/geo objects when server returns a
geo_shape. This PR doesn't address the geo_points yet, which will be
addressed in a separate PR.
@imotov imotov added >enhancement :Analytics/Geo Indexing, search aggregations of geo points and shapes :Analytics/SQL SQL querying labels Feb 20, 2019
@imotov imotov requested review from astefan and matriv February 20, 2019 20:49
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-analytics-geo

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search

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. Left a minor comment.

@@ -269,11 +268,11 @@ else if (type == Types.DOUBLE) {
} else if (type == Types.FLOAT) {
assertEquals(msg, (float) expectedObject, (float) actualObject, lenientFloatingNumbers ? 1f : 0.0f);
} else if (type == Types.OTHER) {
if (expectedObject instanceof Geometry && actualObject instanceof String) {
if (actualObject instanceof org.elasticsearch.geo.geometry.Geometry) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please import to avoid the qualifier.

Copy link
Member

@costin costin Feb 24, 2019

Choose a reason for hiding this comment

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

@bpintea can you please advice what type the ODBC driver indicates (if at all) for geo?

Own update: it looks like both geometry and geography are mapped to -151.

Copy link
Contributor

Choose a reason for hiding this comment

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

@bpintea can you please advice what type the ODBC driver indicates (if at all) for geo?

Own update: it looks like both geometry and geography are mapped to -151.

@costin There isn't any specification on how the geo types should be conveyed in ODBC. Most DBMSes use private types/extensions (like MS SQL's "user defined" -151). WKT/B seems to have most support (also among some BI tools and Excel plug-ins), with GeoJSON picking up as well.

An idea would be to apply the same logic as with KEYWORD, TEXT and IP: give the new geography/geometry types respective names, but reuse a character (12) or the binary (-3) type.
Since Geo is not part of the ODBC standard, no 3rd party existing app could "out of the box" make use of whatever data types we'll implement. OTOH, the values encoding (WKT/B) could be used.

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

@@ -269,11 +268,11 @@ else if (type == Types.DOUBLE) {
} else if (type == Types.FLOAT) {
assertEquals(msg, (float) expectedObject, (float) actualObject, lenientFloatingNumbers ? 1f : 0.0f);
} else if (type == Types.OTHER) {
if (expectedObject instanceof Geometry && actualObject instanceof String) {
if (actualObject instanceof org.elasticsearch.geo.geometry.Geometry) {
Copy link
Member

@costin costin Feb 24, 2019

Choose a reason for hiding this comment

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

@bpintea can you please advice what type the ODBC driver indicates (if at all) for geo?

Own update: it looks like both geometry and geography are mapped to -151.

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 some minor comments.

@@ -269,11 +268,11 @@ else if (type == Types.DOUBLE) {
} else if (type == Types.FLOAT) {
assertEquals(msg, (float) expectedObject, (float) actualObject, lenientFloatingNumbers ? 1f : 0.0f);
} else if (type == Types.OTHER) {
if (expectedObject instanceof Geometry && actualObject instanceof String) {
if (actualObject instanceof org.elasticsearch.geo.geometry.Geometry) {
// We need to convert the actual object to Geometry for comparision
Copy link
Contributor

Choose a reason for hiding this comment

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

Here your comment mentions the actual object needing conversion, but two lines below you perform the operation for the "expected" value instead. Shouldn't be the "actual" one conversion attempt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The comment is leftover from the previous implementation. Before we had string in actual and JTS Geometry in expected. With this change I am trying to reduce dependency on JTS and H2 in this code, so instead of converting everything to H2's JTS Geometry I am converting it to our geometry. I will update the comment. Good catch!

});

@ParametersFactory(argumentFormatting = PARAM_FORMATTING)
public static List<Object[]> readScriptSpec() throws Exception {
Parser parser = new SqlSpecParser();
List<Object[]> tests = new ArrayList<>();
tests.addAll(readScriptSpec("/geo/geosql.sql-spec", parser));
Copy link
Contributor

Choose a reason for hiding this comment

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

Picky comment: can you put the geo script file addition after "ogc"? (this is the order in the other two changes in this class - line 37 and line 56)

selectRegionUsingWktToSqlWithoutConvertion
SELECT region, city, shape, ST_GEOMFROMTEXT(region_point) region_wkt FROM geo ORDER BY region, city;

// TODO: the ordering of geometries differs so ORDER BY needs to be wrapped into string for now
Copy link
Contributor

Choose a reason for hiding this comment

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

I would create an issue for this TODO, just not to lose track of it. If there is one already, apologies.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like order of the elements is not defined by the standard. So, I will order by something else in this test and remove the TODO.


// TODO: the ordering of geometries differs so ORDER BY needs to be wrapped into string for now
selectCitiesWithAGroupByWktToSql
SELECT COUNT(city) city_by_region, ST_GEOMFROMTEXT(region_point) region FROM geo WHERE city LIKE '%a%' GROUP BY ST_GEOMFROMTEXT(region_point) ORDER BY ST_ASWKT(ST_GEOMFROMTEXT(region_point));
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a test with a group by alias: SELECT COUNT(city) city_by_region, ST_GEOMFROMTEXT(region_point) region FROM geo GROUP BY region.

SELECT COUNT(city) city_by_region, ST_GEOMFROMTEXT(region_point) region FROM geo WHERE city LIKE '%a%' GROUP BY ST_GEOMFROMTEXT(region_point) ORDER BY ST_ASWKT(ST_GEOMFROMTEXT(region_point));

selectCitiesWithEOrderByWktToSql
SELECT region, city FROM geo WHERE city LIKE '%e%' ORDER BY ST_ASWKT(ST_GEOMFROMTEXT(region_point)), city;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above - a query with ORDER BY aliased geo function.

"region" VARCHAR(50),
"region_point" VARCHAR(50),
"shape" GEOMETRY

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove empty line?

@imotov imotov merged commit 44542b0 into elastic:geosql Feb 27, 2019
@imotov imotov deleted the issue-35767-integrate-libsgeo branch May 1, 2020 22:18
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.

6 participants