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

builtins: add st_asgeojson for recordsets #52715

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,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 @@ -420,6 +420,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