-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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: Improve null handling in Geo Functions #40708
Changes from 3 commits
ab8e2a3
c7f35ca
dd3e122
4004d7a
8feb38c
284ec3b
e49c1de
cf96e18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,32 +127,8 @@ private Object unwrapMultiValue(Object values) { | |
if (values == null) { | ||
return null; | ||
} | ||
if (dataType == DataType.GEO_POINT) { | ||
Object value; | ||
if (values instanceof List && ((List<?>) values).size() == 1) { | ||
value = ((List<?>) values).get(0); | ||
} else { | ||
value = values; | ||
} | ||
try { | ||
GeoPoint geoPoint = GeoUtils.parseGeoPoint(value, true); | ||
return new GeoShape(geoPoint.lon(), geoPoint.lat()); | ||
} catch (ElasticsearchParseException ex) { | ||
throw new SqlIllegalArgumentException("Cannot parse geo_point value (returned by [{}])", fieldName); | ||
} | ||
} | ||
if (dataType == DataType.GEO_SHAPE) { | ||
Object value; | ||
if (values instanceof List && ((List<?>) values).size() == 1) { | ||
value = ((List<?>) values).get(0); | ||
} else { | ||
value = values; | ||
} | ||
try { | ||
return new GeoShape(value); | ||
} catch (IOException ex) { | ||
throw new SqlIllegalArgumentException("Cannot read geo_shape value (returned by [{}])", fieldName); | ||
} | ||
if (dataType == DataType.GEO_POINT || dataType == DataType.GEO_SHAPE) { | ||
return extractGeoShape(values); | ||
} | ||
if (values instanceof List) { | ||
List<?> list = (List<?>) values; | ||
|
@@ -180,6 +156,33 @@ private Object unwrapMultiValue(Object values) { | |
throw new SqlIllegalArgumentException("Type {} (returned by [{}]) is not supported", values.getClass().getSimpleName(), fieldName); | ||
} | ||
|
||
private Object extractGeoShape(Object values) { | ||
Object value; | ||
if (values instanceof List) { | ||
if (((List<?>) values).size() > 0) { | ||
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 don't think this is the correct approach here. If I'm not mistaken you are extracting here the first value from a field that has an array of geo shapes. SQL has added recently a "multi field leniency" attribute to both REST and drivers support: https://www.elastic.co/guide/en/elasticsearch/reference/6.7/sql-rest.html#sql-rest-fields. Also, would be good to see some tests with arrays of geo shapes and not only in one-level fields, but also multi nested fields (not the 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. Sorry for the delay and thanks for bringing this to my attention. That's very good point and it turned out to be quite tricky to get right. I am still working on it. I might need to make some changes in master first thought to change the way extractSource handles |
||
value = ((List<?>) values).get(0); | ||
} else { | ||
return null; | ||
} | ||
} else { | ||
value = values; | ||
} | ||
if (dataType == DataType.GEO_SHAPE) { | ||
try { | ||
return new GeoShape(value); | ||
} catch (IOException ex) { | ||
throw new SqlIllegalArgumentException("Cannot read geo_shape value (returned by [{}])", fieldName); | ||
} | ||
} else { | ||
try { | ||
GeoPoint geoPoint = GeoUtils.parseGeoPoint(value, true); | ||
return new GeoShape(geoPoint.lon(), geoPoint.lat()); | ||
} catch (ElasticsearchParseException ex) { | ||
throw new SqlIllegalArgumentException("Cannot parse geo_point value (returned by [{}])", fieldName); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings({ "unchecked", "rawtypes" }) | ||
Object extractFromSource(Map<String, Object> map) { | ||
Object value = null; | ||
|
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 think it's worth adding an
isGeoBased()
method toDataType
to perform this double check.