SQL: Add support for shape type (#46464)

Enables support for Cartesian geometries shape type. We still need to
decide how to handle the distance function since it is currently using
the haversine distance formula and returns results in meters, which
doesn't make any sense for Cartesian geometries.

Closes #46412
Relates to #43644
This commit is contained in:
Igor Motov 2019-09-25 13:43:05 -04:00
parent 0c575dc1e8
commit ae202fda21
20 changed files with 146 additions and 114 deletions

View File

@ -5,14 +5,15 @@
beta[] beta[]
The geo functions work with geometries stored in `geo_point` and `geo_shape` fields, or returned by other geo functions. The geo functions work with geometries stored in `geo_point`, `geo_shape` and `shape` fields, or returned by other geo functions.
==== Limitations ==== Limitations
Both <<geo-point, `geo_point`>> and <<geo-shape, `geo_shape`>> types are represented in SQL as geometry and can be used <<geo-point, `geo_point`>>, <<geo-shape, `geo_shape`>> and <<shape, `shape`>> and types are represented in SQL as
interchangeably with the following exceptions: geometry and can be used interchangeably with the following exceptions:
* `geo_shape` fields don't have doc values, therefore these fields cannot be used for filtering, grouping or sorting. * `geo_shape` and `shape` fields don't have doc values, therefore these fields cannot be used for filtering, grouping
or sorting.
* `geo_points` fields are indexed and have doc values by default, however only latitude and longitude are stored and * `geo_points` fields are indexed and have doc values by default, however only latitude and longitude are stored and
indexed with some loss of precision from the original values (4.190951585769653E-8 for the latitude and indexed with some loss of precision from the original values (4.190951585769653E-8 for the latitude and

View File

@ -83,6 +83,7 @@ s|SQL precision
| interval_minute_to_second | 23 | interval_minute_to_second | 23
| geo_point | 52 | geo_point | 52
| geo_shape | 2,147,483,647 | geo_shape | 2,147,483,647
| shape | 2,147,483,647
|=== |===

View File

@ -46,7 +46,8 @@ public enum EsType implements SQLType {
INTERVAL_HOUR_TO_SECOND( ExtraTypes.INTERVAL_HOUR_SECOND), INTERVAL_HOUR_TO_SECOND( ExtraTypes.INTERVAL_HOUR_SECOND),
INTERVAL_MINUTE_TO_SECOND(ExtraTypes.INTERVAL_MINUTE_SECOND), INTERVAL_MINUTE_TO_SECOND(ExtraTypes.INTERVAL_MINUTE_SECOND),
GEO_POINT( ExtraTypes.GEOMETRY), GEO_POINT( ExtraTypes.GEOMETRY),
GEO_SHAPE( ExtraTypes.GEOMETRY); GEO_SHAPE( ExtraTypes.GEOMETRY),
SHAPE( ExtraTypes.GEOMETRY);
private final Integer type; private final Integer type;

View File

@ -248,6 +248,7 @@ final class TypeConverter {
return Duration.parse(v.toString()); return Duration.parse(v.toString());
case GEO_POINT: case GEO_POINT:
case GEO_SHAPE: case GEO_SHAPE:
case SHAPE:
try { try {
return WKT.fromWKT(v.toString()); return WKT.fromWKT(v.toString());
} catch (IOException | ParseException ex) { } catch (IOException | ParseException ex) {

View File

@ -94,6 +94,7 @@ final class TypeUtils {
types.put(EsType.INTERVAL_MINUTE_TO_SECOND, Duration.class); types.put(EsType.INTERVAL_MINUTE_TO_SECOND, Duration.class);
types.put(EsType.GEO_POINT, String.class); types.put(EsType.GEO_POINT, String.class);
types.put(EsType.GEO_SHAPE, String.class); types.put(EsType.GEO_SHAPE, String.class);
types.put(EsType.SHAPE, String.class);
TYPE_TO_CLASS = unmodifiableMap(types); TYPE_TO_CLASS = unmodifiableMap(types);

View File

@ -73,25 +73,25 @@ public class GeoDataLoader {
createString("name", createIndex); createString("name", createIndex);
// Type specific // Type specific
createIndex.startObject("shore").field("type", "geo_shape").endObject(); // lakes createIndex.startObject("shore").field("type", "shape").endObject(); // lakes
createString("aliases", createIndex); // road_segments createString("aliases", createIndex); // road_segments
createIndex.startObject("num_lanes").field("type", "integer").endObject(); // road_segments, divided_routes createIndex.startObject("num_lanes").field("type", "integer").endObject(); // road_segments, divided_routes
createIndex.startObject("centerline").field("type", "geo_shape").endObject(); // road_segments, streams createIndex.startObject("centerline").field("type", "shape").endObject(); // road_segments, streams
createIndex.startObject("centerlines").field("type", "geo_shape").endObject(); // divided_routes createIndex.startObject("centerlines").field("type", "shape").endObject(); // divided_routes
createIndex.startObject("boundary").field("type", "geo_shape").endObject(); // forests, named_places createIndex.startObject("boundary").field("type", "shape").endObject(); // forests, named_places
createIndex.startObject("position").field("type", "geo_shape").endObject(); // bridges, buildings createIndex.startObject("position").field("type", "shape").endObject(); // bridges, buildings
createString("address", createIndex); // buildings createString("address", createIndex); // buildings
createIndex.startObject("footprint").field("type", "geo_shape").endObject(); // buildings createIndex.startObject("footprint").field("type", "shape").endObject(); // buildings
createIndex.startObject("type").field("type", "keyword").endObject(); // ponds createIndex.startObject("type").field("type", "keyword").endObject(); // ponds
createIndex.startObject("shores").field("type", "geo_shape").endObject(); // ponds createIndex.startObject("shores").field("type", "shape").endObject(); // ponds
createIndex.startObject("neatline").field("type", "geo_shape").endObject(); // map_neatlines createIndex.startObject("neatline").field("type", "shape").endObject(); // map_neatlines
} }
createIndex.endObject(); createIndex.endObject();

View File

@ -1,16 +1,16 @@
city,region,region_point,location,shape city,region,region_point,location,geoshape,shape
Mountain View,Americas,POINT(-105.2551 54.5260),POINT (-122.083843 37.386483),POINT (-122.083843 37.386483) Mountain View,Americas,POINT(-105.2551 54.5260),POINT (-122.083843 37.386483),POINT (-122.083843 37.386483)),POINT (-122.083843 37.386483)
Chicago,Americas,POINT(-105.2551 54.5260),POINT (-87.637874 41.888783),POINT (-87.637874 41.888783) Chicago,Americas,POINT(-105.2551 54.5260),POINT (-87.637874 41.888783),POINT (-87.637874 41.888783),POINT (-87.637874 41.888783)
New York,Americas,POINT(-105.2551 54.5260),POINT (-73.990027 40.745171),POINT (-73.990027 40.745171) New York,Americas,POINT(-105.2551 54.5260),POINT (-73.990027 40.745171),POINT (-73.990027 40.745171),POINT (-73.990027 40.745171)
San Francisco,Americas,POINT(-105.2551 54.5260),POINT (-122.394228 37.789541),POINT (-122.394228 37.789541) San Francisco,Americas,POINT(-105.2551 54.5260),POINT (-122.394228 37.789541),POINT (-122.394228 37.789541),POINT (-122.394228 37.789541)
Phoenix,Americas,POINT(-105.2551 54.5260),POINT (-111.973505 33.376242),POINT (-111.973505 33.376242) Phoenix,Americas,POINT(-105.2551 54.5260),POINT (-111.973505 33.376242),POINT (-111.973505 33.376242),POINT (-111.973505 33.376242)
Amsterdam,Europe,POINT(15.2551 54.5260),POINT (4.850312 52.347557),POINT (4.850312 52.347557) Amsterdam,Europe,POINT(15.2551 54.5260),POINT (4.850312 52.347557),POINT (4.850312 52.347557),POINT (4.850312 52.347557)
Berlin,Europe,POINT(15.2551 54.5260),POINT (13.390889 52.486701),POINT (13.390889 52.486701) Berlin,Europe,POINT(15.2551 54.5260),POINT (13.390889 52.486701),POINT (13.390889 52.486701),POINT (13.390889 52.486701)
Munich,Europe,POINT(15.2551 54.5260),POINT (11.537505 48.146321),POINT (11.537505 48.146321) Munich,Europe,POINT(15.2551 54.5260),POINT (11.537505 48.146321),POINT (11.537505 48.146321),POINT (11.537505 48.146321)
London,Europe,POINT(15.2551 54.5260),POINT (-0.121672 51.510871),POINT (-0.121672 51.510871) London,Europe,POINT(15.2551 54.5260),POINT (-0.121672 51.510871),POINT (-0.121672 51.510871),POINT (-0.121672 51.510871)
Paris,Europe,POINT(15.2551 54.5260),POINT (2.351773 48.845538),POINT (2.351773 48.845538) Paris,Europe,POINT(15.2551 54.5260),POINT (2.351773 48.845538),POINT (2.351773 48.845538),POINT (2.351773 48.845538)
Singapore,Asia,POINT(100.6197 34.0479),POINT (103.855535 1.295868),POINT (103.855535 1.295868) Singapore,Asia,POINT(100.6197 34.0479),POINT (103.855535 1.295868),POINT (103.855535 1.295868),POINT (103.855535 1.295868)
Hong Kong,Asia,POINT(100.6197 34.0479),POINT (114.183925 22.281397),POINT (114.183925 22.281397) Hong Kong,Asia,POINT(100.6197 34.0479),POINT (114.183925 22.281397),POINT (114.183925 22.281397),POINT (114.183925 22.281397)
Seoul,Asia,POINT(100.6197 34.0479),POINT (127.060851 37.509132),POINT (127.060851 37.509132) Seoul,Asia,POINT(100.6197 34.0479),POINT (127.060851 37.509132),POINT (127.060851 37.509132),POINT (127.060851 37.509132)
Tokyo,Asia,POINT(100.6197 34.0479),POINT (139.76402225 35.669616),POINT (139.76402225 35.669616) Tokyo,Asia,POINT(100.6197 34.0479),POINT (139.76402225 35.669616),POINT (139.76402225 35.669616),POINT (139.76402225 35.669616)
Sydney,Asia,POINT(100.6197 34.0479),POINT (151.208629 -33.863385),POINT (151.208629 -33.863385) Sydney,Asia,POINT(100.6197 34.0479),POINT (151.208629 -33.863385),POINT (151.208629 -33.863385),POINT (151.208629 -33.863385)

1 city region region_point location geoshape shape
2 Mountain View Americas POINT(-105.2551 54.5260) POINT (-122.083843 37.386483) POINT (-122.083843 37.386483)) POINT (-122.083843 37.386483)
3 Chicago Americas POINT(-105.2551 54.5260) POINT (-87.637874 41.888783) POINT (-87.637874 41.888783) POINT (-87.637874 41.888783)
4 New York Americas POINT(-105.2551 54.5260) POINT (-73.990027 40.745171) POINT (-73.990027 40.745171) POINT (-73.990027 40.745171)
5 San Francisco Americas POINT(-105.2551 54.5260) POINT (-122.394228 37.789541) POINT (-122.394228 37.789541) POINT (-122.394228 37.789541)
6 Phoenix Americas POINT(-105.2551 54.5260) POINT (-111.973505 33.376242) POINT (-111.973505 33.376242) POINT (-111.973505 33.376242)
7 Amsterdam Europe POINT(15.2551 54.5260) POINT (4.850312 52.347557) POINT (4.850312 52.347557) POINT (4.850312 52.347557)
8 Berlin Europe POINT(15.2551 54.5260) POINT (13.390889 52.486701) POINT (13.390889 52.486701) POINT (13.390889 52.486701)
9 Munich Europe POINT(15.2551 54.5260) POINT (11.537505 48.146321) POINT (11.537505 48.146321) POINT (11.537505 48.146321)
10 London Europe POINT(15.2551 54.5260) POINT (-0.121672 51.510871) POINT (-0.121672 51.510871) POINT (-0.121672 51.510871)
11 Paris Europe POINT(15.2551 54.5260) POINT (2.351773 48.845538) POINT (2.351773 48.845538) POINT (2.351773 48.845538)
12 Singapore Asia POINT(100.6197 34.0479) POINT (103.855535 1.295868) POINT (103.855535 1.295868) POINT (103.855535 1.295868)
13 Hong Kong Asia POINT(100.6197 34.0479) POINT (114.183925 22.281397) POINT (114.183925 22.281397) POINT (114.183925 22.281397)
14 Seoul Asia POINT(100.6197 34.0479) POINT (127.060851 37.509132) POINT (127.060851 37.509132) POINT (127.060851 37.509132)
15 Tokyo Asia POINT(100.6197 34.0479) POINT (139.76402225 35.669616) POINT (139.76402225 35.669616) POINT (139.76402225 35.669616)
16 Sydney Asia POINT(100.6197 34.0479) POINT (151.208629 -33.863385) POINT (151.208629 -33.863385) POINT (151.208629 -33.863385)

View File

@ -1,33 +1,33 @@
{"index":{"_id": "1"}} {"index":{"_id": "1"}}
{"region": "Americas", "city": "Mountain View", "location": {"lat":"37.386483", "lon":"-122.083843"}, "location_no_dv": {"lat":"37.386483", "lon":"-122.083843"}, "shape": "POINT (-122.083843 37.386483 30)", "region_point": "POINT(-105.2551 54.5260)"} {"region": "Americas", "city": "Mountain View", "location": {"lat":"37.386483", "lon":"-122.083843"}, "location_no_dv": {"lat":"37.386483", "lon":"-122.083843"}, "geoshape": "POINT (-122.083843 37.386483 30)", "shape": "POINT (-122.083843 37.386483 30)", "region_point": "POINT(-105.2551 54.5260)"}
{"index":{"_id": "2"}} {"index":{"_id": "2"}}
{"region": "Americas", "city": "Chicago", "location": [-87.637874, 41.888783], "location_no_dv": [-87.637874, 41.888783], "shape": {"type" : "point", "coordinates" : [-87.637874, 41.888783, 181]}, "region_point": "POINT(-105.2551 54.5260)"} {"region": "Americas", "city": "Chicago", "location": [-87.637874, 41.888783], "location_no_dv": [-87.637874, 41.888783], "geoshape": {"type" : "point", "coordinates" : [-87.637874, 41.888783, 181]}, "shape": {"type" : "point", "coordinates" : [-87.637874, 41.888783, 181]}, "region_point": "POINT(-105.2551 54.5260)"}
{"index":{"_id": "3"}} {"index":{"_id": "3"}}
{"region": "Americas", "city": "New York", "location": "40.745171,-73.990027", "location_no_dv": "40.745171,-73.990027", "shape": "POINT (-73.990027 40.745171 10)", "region_point": "POINT(-105.2551 54.5260)"} {"region": "Americas", "city": "New York", "location": "40.745171,-73.990027", "location_no_dv": "40.745171,-73.990027", "geoshape": "POINT (-73.990027 40.745171 10)", "shape": "POINT (-73.990027 40.745171 10)", "region_point": "POINT(-105.2551 54.5260)"}
{"index":{"_id": "4"}} {"index":{"_id": "4"}}
{"region": "Americas", "city": "San Francisco", "location": "37.789541,-122.394228", "location_no_dv": "37.789541,-122.394228", "shape": "POINT (-122.394228 37.789541 16)", "region_point": "POINT(-105.2551 54.5260)"} {"region": "Americas", "city": "San Francisco", "location": "37.789541,-122.394228", "location_no_dv": "37.789541,-122.394228", "geoshape": "POINT (-122.394228 37.789541 16)", "shape": "POINT (-122.394228 37.789541 16)", "region_point": "POINT(-105.2551 54.5260)"}
{"index":{"_id": "5"}} {"index":{"_id": "5"}}
{"region": "Americas", "city": "Phoenix", "location": "33.376242,-111.973505", "location_no_dv": "33.376242,-111.973505", "shape": "POINT (-111.973505 33.376242 331)", "region_point": "POINT(-105.2551 54.5260)"} {"region": "Americas", "city": "Phoenix", "location": "33.376242,-111.973505", "location_no_dv": "33.376242,-111.973505", "geoshape": "POINT (-111.973505 33.376242 331)", "shape": "POINT (-111.973505 33.376242 331)", "region_point": "POINT(-105.2551 54.5260)"}
{"index":{"_id": "6"}} {"index":{"_id": "6"}}
{"region": "Europe", "city": "Amsterdam", "location": "52.347557,4.850312", "location_no_dv": "52.347557,4.850312", "shape": "POINT (4.850312 52.347557 2)", "region_point": "POINT(15.2551 54.5260)"} {"region": "Europe", "city": "Amsterdam", "location": "52.347557,4.850312", "location_no_dv": "52.347557,4.850312", "geoshape": "POINT (4.850312 52.347557 2)", "shape": "POINT (4.850312 52.347557 2)", "region_point": "POINT(15.2551 54.5260)"}
{"index":{"_id": "7"}} {"index":{"_id": "7"}}
{"region": "Europe", "city": "Berlin", "location": "52.486701,13.390889", "location_no_dv": "52.486701,13.390889", "shape": "POINT (13.390889 52.486701 34)", "region_point": "POINT(15.2551 54.5260)"} {"region": "Europe", "city": "Berlin", "location": "52.486701,13.390889", "location_no_dv": "52.486701,13.390889", "geoshape": "POINT (13.390889 52.486701 34)", "shape": "POINT (13.390889 52.486701 34)", "region_point": "POINT(15.2551 54.5260)"}
{"index":{"_id": "8"}} {"index":{"_id": "8"}}
{"region": "Europe", "city": "Munich", "location": "48.146321,11.537505", "location_no_dv": "48.146321,11.537505", "shape": "POINT (11.537505 48.146321 519)", "region_point": "POINT(15.2551 54.5260)"} {"region": "Europe", "city": "Munich", "location": "48.146321,11.537505", "location_no_dv": "48.146321,11.537505", "geoshape": "POINT (11.537505 48.146321 519)", "shape": "POINT (11.537505 48.146321 519)", "region_point": "POINT(15.2551 54.5260)"}
{"index":{"_id": "9"}} {"index":{"_id": "9"}}
{"region": "Europe", "city": "London", "location": "51.510871,-0.121672", "location_no_dv": "51.510871,-0.121672", "shape": "POINT (-0.121672 51.510871 11)", "region_point": "POINT(15.2551 54.5260)"} {"region": "Europe", "city": "London", "location": "51.510871,-0.121672", "location_no_dv": "51.510871,-0.121672", "geoshape": "POINT (-0.121672 51.510871 11)", "shape": "POINT (-0.121672 51.510871 11)", "region_point": "POINT(15.2551 54.5260)"}
{"index":{"_id": "10"}} {"index":{"_id": "10"}}
{"region": "Europe", "city": "Paris", "location": "48.845538,2.351773", "location_no_dv": "48.845538,2.351773", "shape": "POINT (2.351773 48.845538 35)", "region_point": "POINT(15.2551 54.5260)"} {"region": "Europe", "city": "Paris", "location": "48.845538,2.351773", "location_no_dv": "48.845538,2.351773", "geoshape": "POINT (2.351773 48.845538 35)", "shape": "POINT (2.351773 48.845538 35)", "region_point": "POINT(15.2551 54.5260)"}
{"index":{"_id": "11"}} {"index":{"_id": "11"}}
{"region": "Asia", "city": "Singapore", "location": "1.295868,103.855535", "location_no_dv": "1.295868,103.855535", "shape": "POINT (103.855535 1.295868 15)", "region_point": "POINT(100.6197 34.0479)"} {"region": "Asia", "city": "Singapore", "location": "1.295868,103.855535", "location_no_dv": "1.295868,103.855535", "geoshape": "POINT (103.855535 1.295868 15)", "shape": "POINT (103.855535 1.295868 15)", "region_point": "POINT(100.6197 34.0479)"}
{"index":{"_id": "12"}} {"index":{"_id": "12"}}
{"region": "Asia", "city": "Hong Kong", "location": "22.281397,114.183925", "location_no_dv": "22.281397,114.183925", "shape": "POINT (114.183925 22.281397 552)", "region_point": "POINT(100.6197 34.0479)"} {"region": "Asia", "city": "Hong Kong", "location": "22.281397,114.183925", "location_no_dv": "22.281397,114.183925", "geoshape": "POINT (114.183925 22.281397 552)", "shape": "POINT (114.183925 22.281397 552)", "region_point": "POINT(100.6197 34.0479)"}
{"index":{"_id": "13"}} {"index":{"_id": "13"}}
{"region": "Asia", "city": "Seoul", "location": "37.509132,127.060851", "location_no_dv": "37.509132,127.060851", "shape": "POINT (127.060851 37.509132 38)", "region_point": "POINT(100.6197 34.0479)"} {"region": "Asia", "city": "Seoul", "location": "37.509132,127.060851", "location_no_dv": "37.509132,127.060851", "geoshape": "POINT (127.060851 37.509132 38)", "shape": "POINT (127.060851 37.509132 38)", "region_point": "POINT(100.6197 34.0479)"}
{"index":{"_id": "14"}} {"index":{"_id": "14"}}
{"region": "Asia", "city": "Tokyo", "location": "35.669616,139.76402225", "location_no_dv": "35.669616,139.76402225", "shape": "POINT (139.76402225 35.669616 40)", "region_point": "POINT(100.6197 34.0479)"} {"region": "Asia", "city": "Tokyo", "location": "35.669616,139.76402225", "location_no_dv": "35.669616,139.76402225", "geoshape": "POINT (139.76402225 35.669616 40)", "shape": "POINT (139.76402225 35.669616 40)", "region_point": "POINT(100.6197 34.0479)"}
{"index":{"_id": "15"}} {"index":{"_id": "15"}}
{"region": "Asia", "city": "Sydney", "location": "-33.863385,151.208629", "location_no_dv": "-33.863385,151.208629", "shape": "POINT (151.208629 -33.863385 100)", "region_point": "POINT(100.6197 34.0479)"} {"region": "Asia", "city": "Sydney", "location": "-33.863385,151.208629", "location_no_dv": "-33.863385,151.208629", "geoshape": "POINT (151.208629 -33.863385 100)", "shape": "POINT (151.208629 -33.863385 100)", "region_point": "POINT(100.6197 34.0479)"}

View File

@ -16,42 +16,43 @@ DESCRIBE "geo";
column:s | type:s | mapping:s column:s | type:s | mapping:s
city | VARCHAR | keyword city | VARCHAR | keyword
geoshape | GEOMETRY | geo_shape
location | GEOMETRY | geo_point location | GEOMETRY | geo_point
location_no_dv | GEOMETRY | geo_point location_no_dv | GEOMETRY | geo_point
region | VARCHAR | keyword region | VARCHAR | keyword
region_point | VARCHAR | keyword region_point | VARCHAR | keyword
shape | GEOMETRY | geo_shape shape | GEOMETRY | shape
; ;
// SELECT ALL // SELECT ALL
// TODO: For now we just get geopoint formatted as is and we also need to convert it to STRING to work with CSV // TODO: For now we just get geopoint formatted as is and we also need to convert it to STRING to work with CSV
selectAllPointsAsStrings selectAllPointsAsStrings
SELECT city, CAST(location AS STRING) location, CAST(location_no_dv AS STRING) location_no_dv, CAST(shape AS STRING) shape, region FROM "geo" ORDER BY "city"; SELECT city, CAST(location AS STRING) location, CAST(location_no_dv AS STRING) location_no_dv, CAST(geoshape AS STRING) geoshape, CAST(shape AS STRING) shape, region FROM "geo" ORDER BY "city";
city:s | location:s | location_no_dv:s | shape:s | region:s city:s | location:s | location_no_dv:s | geoshape:s | shape:s | region:s
Amsterdam |point (4.850311987102032 52.347556999884546) |point (4.850312 52.347557) |point (4.850312 52.347557 2.0) |Europe Amsterdam |point (4.850311987102032 52.347556999884546) |point (4.850312 52.347557) |point (4.850312 52.347557 2.0) |point (4.850312 52.347557 2.0) |Europe
Berlin |point (13.390888944268227 52.48670099303126) |point (13.390889 52.486701) |point (13.390889 52.486701 34.0) |Europe Berlin |point (13.390888944268227 52.48670099303126) |point (13.390889 52.486701) |point (13.390889 52.486701 34.0) |point (13.390889 52.486701 34.0) |Europe
Chicago |point (-87.63787407428026 41.888782968744636) |point (-87.637874 41.888783) |point (-87.637874 41.888783 181.0) |Americas Chicago |point (-87.63787407428026 41.888782968744636) |point (-87.637874 41.888783) |point (-87.637874 41.888783 181.0) |point (-87.637874 41.888783 181.0) |Americas
Hong Kong |point (114.18392493389547 22.28139698971063) |point (114.183925 22.281397) |point (114.183925 22.281397 552.0) |Asia Hong Kong |point (114.18392493389547 22.28139698971063) |point (114.183925 22.281397) |point (114.183925 22.281397 552.0) |point (114.183925 22.281397 552.0) |Asia
London |point (-0.12167204171419144 51.51087098289281)|point (-0.121672 51.510871) |point (-0.121672 51.510871 11.0) |Europe London |point (-0.12167204171419144 51.51087098289281)|point (-0.121672 51.510871) |point (-0.121672 51.510871 11.0) |point (-0.121672 51.510871 11.0) |Europe
Mountain View |point (-122.08384302444756 37.38648299127817) |point (-122.083843 37.386483) |point (-122.083843 37.386483 30.0) |Americas Mountain View |point (-122.08384302444756 37.38648299127817) |point (-122.083843 37.386483) |point (-122.083843 37.386483 30.0) |point (-122.083843 37.386483 30.0) |Americas
Munich |point (11.537504978477955 48.14632098656148) |point (11.537505 48.146321) |point (11.537505 48.146321 519.0) |Europe Munich |point (11.537504978477955 48.14632098656148) |point (11.537505 48.146321) |point (11.537505 48.146321 519.0) |point (11.537505 48.146321 519.0) |Europe
New York |point (-73.9900270756334 40.74517097789794) |point (-73.990027 40.745171) |point (-73.990027 40.745171 10.0) |Americas New York |point (-73.9900270756334 40.74517097789794) |point (-73.990027 40.745171) |point (-73.990027 40.745171 10.0) |point (-73.990027 40.745171 10.0) |Americas
Paris |point (2.3517729341983795 48.84553796611726) |point (2.351773 48.845538) |point (2.351773 48.845538 35.0) |Europe Paris |point (2.3517729341983795 48.84553796611726) |point (2.351773 48.845538) |point (2.351773 48.845538 35.0) |point (2.351773 48.845538 35.0) |Europe
Phoenix |point (-111.97350500151515 33.37624196894467) |point (-111.973505 33.376242) |point (-111.973505 33.376242 331.0) |Americas Phoenix |point (-111.97350500151515 33.37624196894467) |point (-111.973505 33.376242) |point (-111.973505 33.376242 331.0)|point (-111.973505 33.376242 331.0)|Americas
San Francisco |point (-122.39422800019383 37.789540970698) |point (-122.394228 37.789541) |point (-122.394228 37.789541 16.0) |Americas San Francisco |point (-122.39422800019383 37.789540970698) |point (-122.394228 37.789541) |point (-122.394228 37.789541 16.0) |point (-122.394228 37.789541 16.0) |Americas
Seoul |point (127.06085099838674 37.50913198571652) |point (127.060851 37.509132) |point (127.060851 37.509132 38.0) |Asia Seoul |point (127.06085099838674 37.50913198571652) |point (127.060851 37.509132) |point (127.060851 37.509132 38.0) |point (127.060851 37.509132 38.0) |Asia
Singapore |point (103.8555349688977 1.2958679627627134) |point (103.855535 1.295868) |point (103.855535 1.295868 15.0) |Asia Singapore |point (103.8555349688977 1.2958679627627134) |point (103.855535 1.295868) |point (103.855535 1.295868 15.0) |point (103.855535 1.295868 15.0) |Asia
Sydney |point (151.20862897485495 -33.863385021686554)|point (151.208629 -33.863385) |point (151.208629 -33.863385 100.0) |Asia Sydney |point (151.20862897485495 -33.863385021686554)|point (151.208629 -33.863385) |point (151.208629 -33.863385 100.0)|point (151.208629 -33.863385 100.0)|Asia
Tokyo |point (139.76402222178876 35.66961596254259) |point (139.76402225 35.669616)|point (139.76402225 35.669616 40.0) |Asia Tokyo |point (139.76402222178876 35.66961596254259) |point (139.76402225 35.669616)|point (139.76402225 35.669616 40.0)|point (139.76402225 35.669616 40.0)|Asia
; ;
// TODO: Both shape and location contain the same data for now, we should change it later to make things more interesting // TODO: Both shape and location contain the same data for now, we should change it later to make things more interesting
selectAllPointsAsWKT selectAllPointsAsWKT
SELECT city, ST_ASWKT(location) location_wkt, ST_ASWKT(shape) shape_wkt, region FROM "geo" ORDER BY "city"; SELECT city, ST_ASWKT(location) location_wkt, ST_ASWKT(geoshape) geoshape_wkt, region FROM "geo" ORDER BY "city";
city:s | location_wkt:s | shape_wkt:s | region:s city:s | location_wkt:s | geoshape_wkt:s | region:s
Amsterdam |point (4.850311987102032 52.347556999884546) |point (4.850312 52.347557 2.0) |Europe Amsterdam |point (4.850311987102032 52.347556999884546) |point (4.850312 52.347557 2.0) |Europe
Berlin |point (13.390888944268227 52.48670099303126) |point (13.390889 52.486701 34.0) |Europe Berlin |point (13.390888944268227 52.48670099303126) |point (13.390889 52.486701 34.0) |Europe
Chicago |point (-87.63787407428026 41.888782968744636) |point (-87.637874 41.888783 181.0) |Americas Chicago |point (-87.63787407428026 41.888782968744636) |point (-87.637874 41.888783 181.0) |Americas
@ -262,7 +263,7 @@ SELECT COUNT(*) cnt, FLOOR(ST_Y(location)/45) north, FLOOR(ST_X(location)/90) ea
; ;
selectFilterByXOfLocation selectFilterByXOfLocation
SELECT city, ST_X(shape) x, ST_Y(shape) y, ST_Z(shape) z, ST_X(location) lx, ST_Y(location) ly FROM geo WHERE lx > 0 ORDER BY ly; SELECT city, ST_X(geoshape) x, ST_Y(geoshape) y, ST_Z(geoshape) z, ST_X(location) lx, ST_Y(location) ly FROM geo WHERE lx > 0 ORDER BY ly;
city:s | x:d | y:d | z:d | lx:d | ly:d city:s | x:d | y:d | z:d | lx:d | ly:d
Sydney |151.208629 |-33.863385 |100.0 |151.20862897485495|-33.863385021686554 Sydney |151.208629 |-33.863385 |100.0 |151.20862897485495|-33.863385021686554

View File

@ -17,11 +17,14 @@
"type": "geo_point", "type": "geo_point",
"doc_values": "false" "doc_values": "false"
}, },
"shape": { "geoshape": {
"type": "geo_shape" "type": "geo_shape"
}, },
"region_point": { "region_point": {
"type": "keyword" "type": "keyword"
},
"shape": {
"type": "shape"
} }
} }
} }

View File

@ -4,6 +4,7 @@ CREATE TABLE "geo" (
"region" VARCHAR(50), "region" VARCHAR(50),
"region_point" VARCHAR(50), "region_point" VARCHAR(50),
"location" POINT, "location" POINT,
"geoshape" GEOMETRY,
"shape" GEOMETRY "shape" GEOMETRY
) )
AS SELECT * FROM CSVREAD('classpath:/geo/geo.csv'); AS SELECT * FROM CSVREAD('classpath:/geo/geo.csv');

View File

@ -19,18 +19,18 @@ address | VARCHAR | text
address.keyword | VARCHAR | keyword address.keyword | VARCHAR | keyword
aliases | VARCHAR | text aliases | VARCHAR | text
aliases.keyword | VARCHAR | keyword aliases.keyword | VARCHAR | keyword
boundary | GEOMETRY | geo_shape boundary | GEOMETRY | shape
centerline | GEOMETRY | geo_shape centerline | GEOMETRY | shape
centerlines | GEOMETRY | geo_shape centerlines | GEOMETRY | shape
fid | INTEGER | integer fid | INTEGER | integer
footprint | GEOMETRY | geo_shape footprint | GEOMETRY | shape
name | VARCHAR | text name | VARCHAR | text
name.keyword | VARCHAR | keyword name.keyword | VARCHAR | keyword
neatline | GEOMETRY | geo_shape neatline | GEOMETRY | shape
num_lanes | INTEGER | integer num_lanes | INTEGER | integer
ogc_type | VARCHAR | keyword ogc_type | VARCHAR | keyword
position | GEOMETRY | geo_shape position | GEOMETRY | shape
shore | GEOMETRY | geo_shape shore | GEOMETRY | shape
shores | GEOMETRY | geo_shape shores | GEOMETRY | shape
type | VARCHAR | keyword type | VARCHAR | keyword
; ;

View File

@ -7,9 +7,10 @@ SYS COLUMNS TABLE LIKE 'geo';
TABLE_CAT:s | TABLE_SCHEM:s| TABLE_NAME:s | COLUMN_NAME:s | DATA_TYPE:i | TYPE_NAME:s | COLUMN_SIZE:i|BUFFER_LENGTH:i|DECIMAL_DIGITS:i|NUM_PREC_RADIX:i| NULLABLE:i| REMARKS:s | COLUMN_DEF:s |SQL_DATA_TYPE:i|SQL_DATETIME_SUB:i|CHAR_OCTET_LENGTH:i|ORDINAL_POSITION:i|IS_NULLABLE:s|SCOPE_CATALOG:s|SCOPE_SCHEMA:s|SCOPE_TABLE:s|SOURCE_DATA_TYPE:sh|IS_AUTOINCREMENT:s|IS_GENERATEDCOLUMN:s TABLE_CAT:s | TABLE_SCHEM:s| TABLE_NAME:s | COLUMN_NAME:s | DATA_TYPE:i | TYPE_NAME:s | COLUMN_SIZE:i|BUFFER_LENGTH:i|DECIMAL_DIGITS:i|NUM_PREC_RADIX:i| NULLABLE:i| REMARKS:s | COLUMN_DEF:s |SQL_DATA_TYPE:i|SQL_DATETIME_SUB:i|CHAR_OCTET_LENGTH:i|ORDINAL_POSITION:i|IS_NULLABLE:s|SCOPE_CATALOG:s|SCOPE_SCHEMA:s|SCOPE_TABLE:s|SOURCE_DATA_TYPE:sh|IS_AUTOINCREMENT:s|IS_GENERATEDCOLUMN:s
integTest|null |geo |city |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |1 |YES |null |null |null |null |NO |NO integTest|null |geo |city |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |1 |YES |null |null |null |null |NO |NO
integTest|null |geo |location |114 |GEO_POINT |58 |16 |null |null |1 |null |null |114 |0 |null |2 |YES |null |null |null |null |NO |NO integTest|null |geo |geoshape |114 |GEO_SHAPE |2147483647 |2147483647 |null |null |1 |null |null |114 |0 |null |2 |YES |null |null |null |null |NO |NO
integTest|null |geo |location_no_dv |114 |GEO_POINT |58 |16 |null |null |1 |null |null |114 |0 |null |3 |YES |null |null |null |null |NO |NO integTest|null |geo |location |114 |GEO_POINT |58 |16 |null |null |1 |null |null |114 |0 |null |3 |YES |null |null |null |null |NO |NO
integTest|null |geo |region |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |4 |YES |null |null |null |null |NO |NO integTest|null |geo |location_no_dv |114 |GEO_POINT |58 |16 |null |null |1 |null |null |114 |0 |null |4 |YES |null |null |null |null |NO |NO
integTest|null |geo |region_point |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |5 |YES |null |null |null |null |NO |NO integTest|null |geo |region |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |5 |YES |null |null |null |null |NO |NO
integTest|null |geo |shape |114 |GEO_SHAPE |2147483647 |2147483647 |null |null |1 |null |null |114 |0 |null |6 |YES |null |null |null |null |NO |NO integTest|null |geo |region_point |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO
integTest|null |geo |shape |114 |SHAPE |2147483647 |2147483647 |null |null |1 |null |null |114 |0 |null |7 |YES |null |null |null |null |NO |NO
; ;

View File

@ -69,6 +69,7 @@ import static org.elasticsearch.xpack.sql.stats.FeatureMetric.LOCAL;
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.ORDERBY; import static org.elasticsearch.xpack.sql.stats.FeatureMetric.ORDERBY;
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.WHERE; import static org.elasticsearch.xpack.sql.stats.FeatureMetric.WHERE;
import static org.elasticsearch.xpack.sql.type.DataType.GEO_SHAPE; import static org.elasticsearch.xpack.sql.type.DataType.GEO_SHAPE;
import static org.elasticsearch.xpack.sql.type.DataType.SHAPE;
import static org.elasticsearch.xpack.sql.util.CollectionUtils.combine; import static org.elasticsearch.xpack.sql.util.CollectionUtils.combine;
/** /**
@ -758,6 +759,9 @@ public final class Verifier {
if (fa.field().getDataType() == GEO_SHAPE) { if (fa.field().getDataType() == GEO_SHAPE) {
localFailures.add(fail(fa, "geo shapes cannot be used for filtering")); localFailures.add(fail(fa, "geo shapes cannot be used for filtering"));
} }
if (fa.field().getDataType() == SHAPE) {
localFailures.add(fail(fa, "shapes cannot be used for filtering"));
}
}, FieldAttribute.class); }, FieldAttribute.class);
}, Filter.class); }, Filter.class);
@ -766,6 +770,9 @@ public final class Verifier {
if (fa.field().getDataType() == GEO_SHAPE) { if (fa.field().getDataType() == GEO_SHAPE) {
localFailures.add(fail(fa, "geo shapes cannot be used in grouping")); localFailures.add(fail(fa, "geo shapes cannot be used in grouping"));
} }
if (fa.field().getDataType() == SHAPE) {
localFailures.add(fail(fa, "shapes cannot be used in grouping"));
}
}, FieldAttribute.class)), Aggregate.class); }, FieldAttribute.class)), Aggregate.class);
@ -774,6 +781,9 @@ public final class Verifier {
if (fa.field().getDataType() == GEO_SHAPE) { if (fa.field().getDataType() == GEO_SHAPE) {
localFailures.add(fail(fa, "geo shapes cannot be used for sorting")); localFailures.add(fail(fa, "geo shapes cannot be used for sorting"));
} }
if (fa.field().getDataType() == SHAPE) {
localFailures.add(fail(fa, "shapes cannot be used for sorting"));
}
}, FieldAttribute.class)), OrderBy.class); }, FieldAttribute.class)), OrderBy.class);
} }

View File

@ -190,6 +190,13 @@ public class FieldHitExtractor implements HitExtractor {
throw new SqlIllegalArgumentException("Cannot read geo_shape value [{}] (returned by [{}])", values, fieldName); throw new SqlIllegalArgumentException("Cannot read geo_shape value [{}] (returned by [{}])", values, fieldName);
} }
} }
if (dataType == DataType.SHAPE) {
try {
return new GeoShape(values);
} catch (IOException ex) {
throw new SqlIllegalArgumentException("Cannot read shape value [{}] (returned by [{}])", values, fieldName);
}
}
if (values instanceof Map) { if (values instanceof Map) {
throw new SqlIllegalArgumentException("Objects (returned by [{}]) are not supported", fieldName); throw new SqlIllegalArgumentException("Objects (returned by [{}]) are not supported", fieldName);
} }

View File

@ -65,7 +65,7 @@ public class GeoShape implements ToXContentFragment, NamedWriteable {
try { try {
shape = parse(value); shape = parse(value);
} catch (ParseException ex) { } catch (ParseException ex) {
throw new SqlIllegalArgumentException("Cannot parse [" + value + "] as a geo_shape value", ex); throw new SqlIllegalArgumentException("Cannot parse [" + value + "] as a geo_shape or shape value", ex);
} }
} }
@ -74,7 +74,7 @@ public class GeoShape implements ToXContentFragment, NamedWriteable {
try { try {
shape = parse(value); shape = parse(value);
} catch (ParseException ex) { } catch (ParseException ex) {
throw new SqlIllegalArgumentException("Cannot parse [" + value + "] as a geo_shape value", ex); throw new SqlIllegalArgumentException("Cannot parse [" + value + "] as a geo_shape or shape value", ex);
} }
} }

View File

@ -59,6 +59,8 @@ public enum DataType {
GEO_POINT( ExtTypes.GEOMETRY, Double.BYTES*2, Integer.MAX_VALUE, 25 * 2 + 8, false, false, false), GEO_POINT( ExtTypes.GEOMETRY, Double.BYTES*2, Integer.MAX_VALUE, 25 * 2 + 8, false, false, false),
// IP can be v4 or v6. The latter has 2^128 addresses or 340,282,366,920,938,463,463,374,607,431,768,211,456 // IP can be v4 or v6. The latter has 2^128 addresses or 340,282,366,920,938,463,463,374,607,431,768,211,456
// aka 39 chars // aka 39 chars
SHAPE( ExtTypes.GEOMETRY, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, false, false, false),
// display size = 2 doubles + len("POINT( )")
IP( "ip", JDBCType.VARCHAR, 39, 39, 0, false, false, true), IP( "ip", JDBCType.VARCHAR, 39, 39, 0, false, false, true),
// //
// INTERVALS // INTERVALS
@ -254,7 +256,7 @@ public enum DataType {
} }
public boolean isGeo() { public boolean isGeo() {
return this == GEO_POINT || this == GEO_SHAPE; return this == GEO_POINT || this == GEO_SHAPE || this == SHAPE;
} }
public boolean isDateBased() { public boolean isDateBased() {
@ -276,7 +278,8 @@ public enum DataType {
|| this == DATETIME || this == DATETIME
|| this == SCALED_FLOAT // because of scaling_factor || this == SCALED_FLOAT // because of scaling_factor
|| this == GEO_POINT || this == GEO_POINT
|| this == GEO_SHAPE; || this == GEO_SHAPE
|| this == SHAPE;
} }
public static DataType fromOdbcType(String odbcType) { public static DataType fromOdbcType(String odbcType) {

View File

@ -462,7 +462,7 @@ public class FieldHitExtractorTests extends AbstractSqlWireSerializingTestCase<F
public void testGeoShapeExtraction() { public void testGeoShapeExtraction() {
String fieldName = randomAlphaOfLength(5); String fieldName = randomAlphaOfLength(5);
FieldHitExtractor fe = new FieldHitExtractor(fieldName, DataType.GEO_SHAPE, UTC, false); FieldHitExtractor fe = new FieldHitExtractor(fieldName, randomBoolean() ? DataType.GEO_SHAPE : DataType.SHAPE, UTC, false);
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put(fieldName, "POINT (1 2)"); map.put(fieldName, "POINT (1 2)");
assertEquals(new GeoShape(1, 2), fe.extractFromSource(map)); assertEquals(new GeoShape(1, 2), fe.extractFromSource(map));
@ -474,7 +474,7 @@ public class FieldHitExtractorTests extends AbstractSqlWireSerializingTestCase<F
public void testMultipleGeoShapeExtraction() { public void testMultipleGeoShapeExtraction() {
String fieldName = randomAlphaOfLength(5); String fieldName = randomAlphaOfLength(5);
FieldHitExtractor fe = new FieldHitExtractor(fieldName, DataType.GEO_SHAPE, UTC, false); FieldHitExtractor fe = new FieldHitExtractor(fieldName, randomBoolean() ? DataType.GEO_SHAPE : DataType.SHAPE, UTC, false);
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put(fieldName, "POINT (1 2)"); map.put(fieldName, "POINT (1 2)");
assertEquals(new GeoShape(1, 2), fe.extractFromSource(map)); assertEquals(new GeoShape(1, 2), fe.extractFromSource(map));
@ -487,7 +487,8 @@ public class FieldHitExtractorTests extends AbstractSqlWireSerializingTestCase<F
SqlException ex = expectThrows(SqlException.class, () -> fe.extractFromSource(map2)); SqlException ex = expectThrows(SqlException.class, () -> fe.extractFromSource(map2));
assertThat(ex.getMessage(), is("Arrays (returned by [" + fieldName + "]) are not supported")); assertThat(ex.getMessage(), is("Arrays (returned by [" + fieldName + "]) are not supported"));
FieldHitExtractor lenientFe = new FieldHitExtractor(fieldName, DataType.GEO_SHAPE, UTC, false, true); FieldHitExtractor lenientFe = new FieldHitExtractor(fieldName,
randomBoolean() ? DataType.GEO_SHAPE : DataType.SHAPE, UTC, false, true);
assertEquals(new GeoShape(1, 2), lenientFe.extractFromSource(map2)); assertEquals(new GeoShape(1, 2), lenientFe.extractFromSource(map2));
} }

View File

@ -33,11 +33,11 @@ public class StWkttosqlProcessorTests extends ESTestCase {
assertEquals("Cannot parse [some random string] as a geo_shape value", siae.getMessage()); assertEquals("Cannot parse [some random string] as a geo_shape value", siae.getMessage());
siae = expectThrows(SqlIllegalArgumentException.class, () -> procPoint.process("point (foo bar)")); siae = expectThrows(SqlIllegalArgumentException.class, () -> procPoint.process("point (foo bar)"));
assertEquals("Cannot parse [point (foo bar)] as a geo_shape value", siae.getMessage()); assertEquals("Cannot parse [point (foo bar)] as a geo_shape or shape value", siae.getMessage());
siae = expectThrows(SqlIllegalArgumentException.class, () -> procPoint.process("point (10 10")); siae = expectThrows(SqlIllegalArgumentException.class, () -> procPoint.process("point (10 10"));
assertEquals("Cannot parse [point (10 10] as a geo_shape value", siae.getMessage()); assertEquals("Cannot parse [point (10 10] as a geo_shape or shape value", siae.getMessage());
} }
public void testCoerce() { public void testCoerce() {

View File

@ -49,7 +49,7 @@ public class SysTypesTests extends ESTestCase {
"INTERVAL_YEAR", "INTERVAL_MONTH", "INTERVAL_DAY", "INTERVAL_HOUR", "INTERVAL_MINUTE", "INTERVAL_SECOND", "INTERVAL_YEAR", "INTERVAL_MONTH", "INTERVAL_DAY", "INTERVAL_HOUR", "INTERVAL_MINUTE", "INTERVAL_SECOND",
"INTERVAL_YEAR_TO_MONTH", "INTERVAL_DAY_TO_HOUR", "INTERVAL_DAY_TO_MINUTE", "INTERVAL_DAY_TO_SECOND", "INTERVAL_YEAR_TO_MONTH", "INTERVAL_DAY_TO_HOUR", "INTERVAL_DAY_TO_MINUTE", "INTERVAL_DAY_TO_SECOND",
"INTERVAL_HOUR_TO_MINUTE", "INTERVAL_HOUR_TO_SECOND", "INTERVAL_MINUTE_TO_SECOND", "INTERVAL_HOUR_TO_MINUTE", "INTERVAL_HOUR_TO_SECOND", "INTERVAL_MINUTE_TO_SECOND",
"GEO_SHAPE", "GEO_POINT", "UNSUPPORTED", "OBJECT", "NESTED"); "GEO_SHAPE", "GEO_POINT", "SHAPE", "UNSUPPORTED", "OBJECT", "NESTED");
cmd.execute(session(), wrap(p -> { cmd.execute(session(), wrap(p -> {
SchemaRowSet r = (SchemaRowSet) p.rowSet(); SchemaRowSet r = (SchemaRowSet) p.rowSet();