Skip to content

Commit

Permalink
Merge #52715
Browse files Browse the repository at this point in the history
52715: builtins: add st_asgeojson for recordsets r=sumeerbhola a=otan

Release note (sql change): Add ST_AsGeoJSON for recordsets, putting row
contents into the properties field of a GeoJSON object.

Co-authored-by: Oliver Tan <[email protected]>
  • Loading branch information
craig[bot] and otan committed Aug 14, 2020
2 parents 41b82b2 + 747a867 commit 6ff42f2
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 4 deletions.
8 changes: 8 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,14 @@ has no relationship with the commit order of concurrent transactions.</p>
</ul>
<p>This variant will cast all geometry_str arguments into Geometry types.</p>
</span></td></tr>
<tr><td><a name="st_asgeojson"></a><code>st_asgeojson(row: tuple) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the GeoJSON representation of a given Geometry. Coordinates have a maximum of 9 decimal digits.</p>
</span></td></tr>
<tr><td><a name="st_asgeojson"></a><code>st_asgeojson(row: tuple, geo_column: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the GeoJSON representation of a given Geometry, using geo_column as the geometry for the given Feature. Coordinates have a maximum of 9 decimal digits.</p>
</span></td></tr>
<tr><td><a name="st_asgeojson"></a><code>st_asgeojson(row: tuple, geo_column: <a href="string.html">string</a>, max_decimal_digits: <a href="int.html">int</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the GeoJSON representation of a given Geometry, using geo_column as the geometry for the given Feature. max_decimal_digits will be output for each coordinate value.</p>
</span></td></tr>
<tr><td><a name="st_asgeojson"></a><code>st_asgeojson(row: tuple, geo_column: <a href="string.html">string</a>, max_decimal_digits: <a href="int.html">int</a>, pretty: <a href="bool.html">bool</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the GeoJSON representation of a given Geometry, using geo_column as the geometry for the given Feature. max_decimal_digits will be output for each coordinate value. Output will be pretty printed in JSON if pretty is true.</p>
</span></td></tr>
<tr><td><a name="st_ashexewkb"></a><code>st_ashexewkb(geography: geography) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the EWKB representation in hex of a given Geography.</p>
</span></td></tr>
<tr><td><a name="st_ashexewkb"></a><code>st_ashexewkb(geography: geography, xdr_or_ndr: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the EWKB representation in hex of a given Geography. This variant has a second argument denoting the encoding - <code>xdr</code> for big endian and <code>ndr</code> for little endian.</p>
Expand Down
213 changes: 213 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/geospatial
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,219 @@ FROM parse_test ORDER BY id ASC
{"type":"Point","coordinates":[]} {"type":"Point","coordinates":[]} {"type":"Point","coordinates":[]}
NULL NULL NULL

# Since id in parse_test can change between tests, make a new table
# that has a consistent id over multiple logic test runs.
statement ok
CREATE TABLE parse_test_geojson AS
SELECT
row_number() OVER (ORDER BY id) as id,
geom,
geog
FROM parse_test

query T
SELECT
ST_AsGeoJSON(t.*)
FROM ( VALUES
(1),
(2)
) t(row_id)
----
{"geometry": {"type": null}, "properties": {"row_id": 1}, "type": "Feature"}
{"geometry": {"type": null}, "properties": {"row_id": 2}, "type": "Feature"}

query TTT
SELECT
ST_AsGeoJSON(parse_test_geojson.*),
ST_AsGeoJSON(parse_test_geojson.*, 'geom'),
ST_AsGeoJSON(parse_test_geojson.*, 'geog')
FROM parse_test_geojson ORDER BY id ASC
----
{"geometry": {"coordinates": [1.555, -2], "type": "Point"}, "properties": {"geog": {"coordinates": [1.555, -2], "type": "Point"}, "id": 1}, "type": "Feature"} {"geometry": {"coordinates": [1.555, -2], "type": "Point"}, "properties": {"geog": {"coordinates": [1.555, -2], "type": "Point"}, "id": 1}, "type": "Feature"} {"geometry": {"coordinates": [1.555, -2], "type": "Point"}, "properties": {"geom": {"coordinates": [1.555, -2], "type": "Point"}, "id": 1}, "type": "Feature"}
{"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 2], "type": "Point"}, "id": 2}, "type": "Feature"} {"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 2], "type": "Point"}, "id": 2}, "type": "Feature"} {"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geom": {"coordinates": [1, 2], "type": "Point"}, "id": 2}, "type": "Feature"}
{"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 2], "type": "Point"}, "id": 3}, "type": "Feature"} {"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 2], "type": "Point"}, "id": 3}, "type": "Feature"} {"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geom": {"coordinates": [1, 2], "type": "Point"}, "id": 3}, "type": "Feature"}
{"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 2], "type": "Point"}, "id": 4}, "type": "Feature"} {"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 2], "type": "Point"}, "id": 4}, "type": "Feature"} {"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geom": {"coordinates": [1, 2], "type": "Point"}, "id": 4}, "type": "Feature"}
{"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 2], "type": "Point"}, "id": 5}, "type": "Feature"} {"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 2], "type": "Point"}, "id": 5}, "type": "Feature"} {"geometry": {"coordinates": [1, 2], "type": "Point"}, "properties": {"geom": {"coordinates": [1, 2], "type": "Point"}, "id": 5}, "type": "Feature"}
{"geometry": {"coordinates": [1, 1], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 1], "type": "Point"}, "id": 6}, "type": "Feature"} {"geometry": {"coordinates": [1, 1], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 1], "type": "Point"}, "id": 6}, "type": "Feature"} {"geometry": {"coordinates": [1, 1], "type": "Point"}, "properties": {"geom": {"coordinates": [1, 1], "type": "Point"}, "id": 6}, "type": "Feature"}
{"geometry": {"coordinates": [1, 1], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 1], "type": "Point"}, "id": 7}, "type": "Feature"} {"geometry": {"coordinates": [1, 1], "type": "Point"}, "properties": {"geog": {"coordinates": [1, 1], "type": "Point"}, "id": 7}, "type": "Feature"} {"geometry": {"coordinates": [1, 1], "type": "Point"}, "properties": {"geom": {"coordinates": [1, 1], "type": "Point"}, "id": 7}, "type": "Feature"}
{"geometry": {"coordinates": [], "type": "Point"}, "properties": {"geog": {"coordinates": [], "type": "Point"}, "id": 8}, "type": "Feature"} {"geometry": {"coordinates": [], "type": "Point"}, "properties": {"geog": {"coordinates": [], "type": "Point"}, "id": 8}, "type": "Feature"} {"geometry": {"coordinates": [], "type": "Point"}, "properties": {"geom": {"coordinates": [], "type": "Point"}, "id": 8}, "type": "Feature"}
{"geometry": {"type": null}, "properties": {"geog": null, "geom": null, "id": 9}, "type": "Feature"} {"geometry": {"type": null}, "properties": {"geog": null, "id": 9}, "type": "Feature"} {"geometry": {"type": null}, "properties": {"geom": null, "id": 9}, "type": "Feature"}

query T
SELECT
ST_AsGeoJSON(parse_test_geojson.*, 'geog', 3, true)
FROM parse_test_geojson ORDER BY id ASC
----
{
"geometry": {
"coordinates": [
1.555,
-2
],
"type": "Point"
},
"properties": {
"geom": {
"coordinates": [
1.555,
-2
],
"type": "Point"
},
"id": 1
},
"type": "Feature"
}
{
"geometry": {
"coordinates": [
1,
2
],
"type": "Point"
},
"properties": {
"geom": {
"coordinates": [
1,
2
],
"type": "Point"
},
"id": 2
},
"type": "Feature"
}
{
"geometry": {
"coordinates": [
1,
2
],
"type": "Point"
},
"properties": {
"geom": {
"coordinates": [
1,
2
],
"type": "Point"
},
"id": 3
},
"type": "Feature"
}
{
"geometry": {
"coordinates": [
1,
2
],
"type": "Point"
},
"properties": {
"geom": {
"coordinates": [
1,
2
],
"type": "Point"
},
"id": 4
},
"type": "Feature"
}
{
"geometry": {
"coordinates": [
1,
2
],
"type": "Point"
},
"properties": {
"geom": {
"coordinates": [
1,
2
],
"type": "Point"
},
"id": 5
},
"type": "Feature"
}
{
"geometry": {
"coordinates": [
1,
1
],
"type": "Point"
},
"properties": {
"geom": {
"coordinates": [
1,
1
],
"type": "Point"
},
"id": 6
},
"type": "Feature"
}
{
"geometry": {
"coordinates": [
1,
1
],
"type": "Point"
},
"properties": {
"geom": {
"coordinates": [
1,
1
],
"type": "Point"
},
"id": 7
},
"type": "Feature"
}
{
"geometry": {
"coordinates": [],
"type": "Point"
},
"properties": {
"geom": {
"coordinates": [],
"type": "Point"
},
"id": 8
},
"type": "Feature"
}
{
"geometry": {
"type": null
},
"properties": {
"geom": null,
"id": 9
},
"type": "Feature"
}

statement error "geom_no_exist" column not found
SELECT
ST_AsGeoJSON(parse_test.*, 'geom_no_exist')
FROM parse_test ORDER BY id asc

# tests casts
query TTT
SELECT
Expand Down
Loading

0 comments on commit 6ff42f2

Please sign in to comment.