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

chore(bq,sf,rs,pg|h3,quadbin): added "geo" aliases for certain functions #526

Merged
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
2 changes: 1 addition & 1 deletion clouds/bigquery/modules/doc/h3/H3_FROMGEOGPOINT.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ H3_FROMGEOGPOINT(point, resolution)

**Description**

Returns the H3 cell index that the point belongs to in the required `resolution`. It will return `null` on error (invalid geography type or resolution out of bounds).
Returns the H3 cell index that the point belongs to in the required `resolution`. It will return `null` on error (invalid geography type or resolution out of bounds). This function is an alias for `H3_FROMGEOPOINT`.

* `point`: `GEOGRAPHY` point to get the H3 cell from.
* `resolution`: `INT64` number between 0 and 15 with the [H3 resolution](https://h3geo.org/docs/core-library/restable).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ QUADBIN_FROMGEOGPOINT(point, resolution)

**Description**

Returns the Quadbin of a given point at a given level of detail.
Returns the Quadbin of a given point at a given level of detail. This function is an alias for `QUADBIN_FROMGEOPOINT`.

* `point`: `GEOGRAPHY` point to get the Quadbin from.
* `resolution`: `INT64` level of detail or zoom.
Expand Down
15 changes: 12 additions & 3 deletions clouds/bigquery/modules/sql/h3/H3_FROMGEOGPOINT.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
----------------------------
-- Copyright (C) 2021 CARTO
----------------------------
--------------------------------
-- Copyright (C) 2021-2024 CARTO
--------------------------------

CREATE OR REPLACE FUNCTION `@@BQ_DATASET@@.H3_FROMGEOGPOINT`
(geog GEOGRAPHY, resolution INT64)
Expand All @@ -10,3 +10,12 @@ AS (
SAFE.ST_X(geog), SAFE.ST_Y(geog), resolution
)
);

CREATE OR REPLACE FUNCTION `@@BQ_DATASET@@.H3_FROMGEOPOINT`
(geo GEOGRAPHY, resolution INT64)
RETURNS STRING
AS (
`@@BQ_DATASET@@.H3_FROMGEOGPOINT`(
geo, resolution
)
);
15 changes: 12 additions & 3 deletions clouds/bigquery/modules/sql/quadbin/QUADBIN_FROMGEOGPOINT.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
----------------------------
-- Copyright (C) 2022 CARTO
----------------------------
--------------------------------
-- Copyright (C) 2022-2024 CARTO
--------------------------------

CREATE OR REPLACE FUNCTION `@@BQ_DATASET@@.QUADBIN_FROMGEOGPOINT`
(point GEOGRAPHY, resolution INT64)
Expand All @@ -10,3 +10,12 @@ AS (
ST_X(point), ST_Y(point), resolution
)
);

CREATE OR REPLACE FUNCTION `@@BQ_DATASET@@.QUADBIN_FROMGEOPOINT`
(point GEOGRAPHY, resolution INT64)
RETURNS INT64
AS (
`@@BQ_DATASET@@.QUADBIN_FROMGEOGPOINT`(
point, resolution
)
);
31 changes: 31 additions & 0 deletions clouds/bigquery/modules/test/h3/H3_FROMGEOGPOINT.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,35 @@ test('H3_FROMGEOGPOINT returns NULL with non POINT geographies', async () => {
null,
null
]);
});

test('H3_FROMGEOPOINT returns the proper INT64', async () => {
const query = `
WITH inputs AS
(
SELECT 1 AS id, ST_GEOGPOINT(-122.0553238, 37.3615593) as geom, 5 as resolution UNION ALL
SELECT 2 AS id, ST_GEOGPOINT(-164.991559, 30.943387) as geom, 5 as resolution UNION ALL
SELECT 3 AS id, ST_GEOGPOINT(71.52790329909925, 46.04189431883772) as geom, 15 as resolution UNION ALL

-- null inputs
SELECT 4 AS id, NULL AS geom, 5 as resolution UNION ALL
SELECT 5 AS id, ST_GEOGPOINT(-122.0553238, 37.3615593) as geom, -1 as resolution UNION ALL
SELECT 6 AS id, ST_GEOGPOINT(-122.0553238, 37.3615593) as geom, 20 as resolution UNION ALL
SELECT 7 AS id, ST_GEOGPOINT(-122.0553238, 37.3615593) as geom, NULL as resolution
)
SELECT CAST(\`@@BQ_DATASET@@.H3_FROMGEOPOINT\`(geom, resolution) AS STRING) as h3_id
FROM inputs
ORDER BY id ASC
`;
const rows = await runQuery(query);
expect(rows.length).toEqual(7);
expect(rows.map((r) => r.h3_id)).toEqual([
'85283473fffffff',
'8547732ffffffff',
'8f2000000000000',
null,
null,
null,
null
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ test('QUADBIN_FROMGEOGPOINT should work', async () => {
const rows = await runQuery(query);
expect(rows.length).toEqual(1);
expect(rows[0].output).toEqual('5209574053332910079');
});

test('QUADBIN_FROMGEOPOINT should work', async () => {
const query = 'SELECT CAST(`@@BQ_DATASET@@.QUADBIN_FROMGEOPOINT`(ST_GEOGPOINT(40.4168, -3.7038), 4) AS STRING) AS output';
const rows = await runQuery(query);
expect(rows.length).toEqual(1);
expect(rows[0].output).toEqual('5209574053332910079');
});
2 changes: 1 addition & 1 deletion clouds/postgres/modules/doc/h3/H3_FROMGEOGPOINT.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ H3_FROMGEOGPOINT(point, resolution)

**Description**

Returns the H3 cell index that the point belongs to in the required `resolution`. It will return `null` on error (invalid geography type or resolution out of bounds).
Returns the H3 cell index that the point belongs to in the required `resolution`. It will return `null` on error (invalid geography type or resolution out of bounds). This function is an alias for `H3_FROMGEOPOINT`.

* `point`: `GEOMETRY` point to get the H3 cell from.
* `resolution`: `INT` number between 0 and 15 with the [H3 resolution](https://h3geo.org/docs/core-library/restable).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ QUADBIN_FROMGEOGPOINT(point, resolution)

**Description**

Returns the Quadbin of a given point at a given level of detail.
Returns the Quadbin of a given point at a given level of detail. This function is an alias for `QUADBIN_FROMGEOPOINT`.

* `point`: `GEOMETRY` point to get the Quadbin from.
* `resolution`: `BIGINT` level of detail or zoom.
Expand Down
17 changes: 14 additions & 3 deletions clouds/postgres/modules/sql/h3/H3_FROMGEOGPOINT.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
----------------------------
-- Copyright (C) 2023 CARTO
----------------------------
--------------------------------
-- Copyright (C) 2023-2024 CARTO
--------------------------------

CREATE OR REPLACE FUNCTION @@PG_SCHEMA@@.H3_FROMGEOGPOINT(
geog GEOMETRY,
Expand All @@ -17,3 +17,14 @@ $BODY$
END
$BODY$
LANGUAGE sql IMMUTABLE PARALLEL SAFE;

CREATE OR REPLACE FUNCTION @@PG_SCHEMA@@.H3_FROMGEOPOINT(
geo GEOMETRY,
resolution INT
)
RETURNS VARCHAR(16)
AS
$BODY$
SELECT @@PG_SCHEMA@@.H3_FROMGEOGPOINT(geo, resolution)
$BODY$
LANGUAGE sql IMMUTABLE PARALLEL SAFE;
17 changes: 14 additions & 3 deletions clouds/postgres/modules/sql/quadbin/QUADBIN_FROMGEOGPOINT.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
----------------------------
-- Copyright (C) 2022 CARTO
----------------------------
--------------------------------
-- Copyright (C) 2022-2024 CARTO
--------------------------------

CREATE OR REPLACE FUNCTION @@PG_SCHEMA@@.QUADBIN_FROMGEOGPOINT(
point GEOMETRY,
Expand All @@ -20,3 +20,14 @@ $BODY$
FROM __geom4326;
$BODY$
LANGUAGE sql IMMUTABLE PARALLEL SAFE;

CREATE OR REPLACE FUNCTION @@PG_SCHEMA@@.QUADBIN_FROMGEOPOINT(
point GEOMETRY,
resolution INT
)
RETURNS BIGINT
AS
$BODY$
SELECT @@PG_SCHEMA@@.QUADBIN_FROMGEOGPOINT(point, resolution)
$BODY$
LANGUAGE sql IMMUTABLE PARALLEL SAFE;
31 changes: 31 additions & 0 deletions clouds/postgres/modules/test/h3/test_H3_FROMGEOGPOINT.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,34 @@ def test_h3_fromgeogpoint_non_points():
assert result[0][0] is None
assert result[1][0] is None
assert result[2][0] is None


def test_h3_fromgeopoint():
"""Returns the proper index."""
result = run_query(
"""
WITH inputs AS
(
SELECT 1 AS id, ST_POINT(-122.0553238, 37.3615593) as geom, 5 as resolution UNION ALL
SELECT 2 AS id, ST_POINT(-164.991559, 30.943387) as geom, 5 as resolution UNION ALL
SELECT 3 AS id, ST_POINT(71.52790329909925, 46.04189431883772) as geom, 15 as resolution UNION ALL

-- null inputs
SELECT 4 AS id, NULL AS geom, 5 as resolution UNION ALL
SELECT 5 AS id, ST_POINT(-122.0553238, 37.3615593) as geom, -1 as resolution UNION ALL
SELECT 6 AS id, ST_POINT(-122.0553238, 37.3615593) as geom, 20 as resolution UNION ALL
SELECT 7 AS id, ST_POINT(-122.0553238, 37.3615593) as geom, NULL as resolution
)
SELECT @@PG_SCHEMA@@.H3_FROMGEOPOINT(geom, resolution) as h3_id
FROM inputs
ORDER BY id ASC
""" # noqa
)
assert len(result) == 7
assert result[0][0] == '85283473fffffff'
assert result[1][0] == '8547732ffffffff'
assert result[2][0] == '8f2000000000000'
assert result[3][0] is None
assert result[4][0] is None
assert result[5][0] is None
assert result[6][0] is None
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ def test_quadbin_fromgeogpoint_other_srid():
"""
)
assert result[0][0] == 5209574053332910079


def test_quadbin_fromgeopoint_no_srid():
"""Computes quadbin for point with no SRID."""
result = run_query(
'SELECT @@PG_SCHEMA@@.QUADBIN_FROMGEOPOINT(ST_MAKEPOINT(40.4168, -3.7038), 4)'
)
assert result[0][0] == 5209574053332910079
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ QUADBIN_FROMGEOGPOINT(point, resolution)

**Description**

Returns the Quadbin of a given point at a given level of detail.
Returns the Quadbin of a given point at a given level of detail. This function is an alias for `QUADBIN_FROMGEOPOINT`.

* `point`: `GEOMETRY` point to get the Quadbin from.
* `resolution`: `INT` level of detail or zoom.
Expand Down
15 changes: 12 additions & 3 deletions clouds/redshift/modules/sql/quadbin/QUADBIN_FROMGEOGPOINT.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
----------------------------
-- Copyright (C) 2022 CARTO
----------------------------
--------------------------------
-- Copyright (C) 2022-2024 CARTO
--------------------------------

CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.QUADBIN_FROMGEOGPOINT
(GEOMETRY, INT)
Expand All @@ -13,3 +13,12 @@ AS $$
ELSE @@RS_SCHEMA@@.QUADBIN_FROMLONGLAT(ST_X(ST_TRANSFORM($1, 4326)), ST_Y(ST_TRANSFORM($1, 4326)), $2)
END
$$ LANGUAGE sql;

CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.QUADBIN_FROMGEOPOINT
(GEOMETRY, INT)
-- (point, resolution)
RETURNS BIGINT
STABLE
AS $$
SELECT @@RS_SCHEMA@@.QUADBIN_FROMGEOGPOINT($1, $2)
$$ LANGUAGE sql;
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ def test_quadbin_fromgeogpoint():

assert len(result[0]) == 1
assert result[0][0] == 5209574053332910079


def test_quadbin_fromgeopoint():
result = run_query(
'SELECT @@RS_SCHEMA@@.QUADBIN_FROMGEOPOINT(ST_POINT(40.4168, -3.7038),4)'
)

assert len(result[0]) == 1
assert result[0][0] == 5209574053332910079
2 changes: 1 addition & 1 deletion clouds/snowflake/modules/doc/h3/H3_FROMGEOGPOINT.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ H3_FROMGEOGPOINT(point, resolution)

**Description**

Returns the H3 cell index that the point belongs to in the required `resolution`. It will return `null` on error (invalid geography type or resolution out of bounds).
Returns the H3 cell index that the point belongs to in the required `resolution`. It will return `null` on error (invalid geography type or resolution out of bounds). This function is an alias for `H3_FROMGEOPOINT`.

* `point`: `GEOGRAPHY` point to get the H3 cell from.
* `resolution`: `INT` number between 0 and 15 with the [H3 resolution](https://h3geo.org/docs/core-library/restable).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ QUADBIN_FROMGEOGPOINT(point, resolution)

**Description**

Returns the Quadbin of a given point at a given level of detail.
Returns the Quadbin of a given point at a given level of detail. This function is an alias for `QUADBIN_FROMGEOPOINT`.

* `point`: `GEOGRAPHY` point to get the Quadbin from.
* `resolution`: `INT` level of detail or zoom.
Expand Down
8 changes: 8 additions & 0 deletions clouds/snowflake/modules/sql/h3/H3_FROMGEOGPOINT.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ AS $$
H3_POINT_TO_CELL_STRING(GEOG, RESOLUTION),
NULL)
$$;

CREATE OR REPLACE SECURE FUNCTION @@SF_SCHEMA@@.H3_FROMGEOPOINT
(geo GEOGRAPHY, resolution INT)
RETURNS STRING
IMMUTABLE
AS $$
@@SF_SCHEMA@@.H3_FROMGEOGPOINT(geo, resolution)
$$;
14 changes: 11 additions & 3 deletions clouds/snowflake/modules/sql/quadbin/QUADBIN_FROMGEOGPOINT.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
----------------------------
-- Copyright (C) 2022 CARTO
----------------------------
--------------------------------
-- Copyright (C) 2022-2024 CARTO
--------------------------------

CREATE OR REPLACE SECURE FUNCTION @@SF_SCHEMA@@.QUADBIN_FROMGEOGPOINT
(point GEOGRAPHY, resolution INT)
Expand All @@ -17,3 +17,11 @@ IMMUTABLE
AS $$
@@SF_SCHEMA@@._QUADBIN_FROMLONGLAT(ST_X(point), ST_Y(point), resolution)
$$;

CREATE OR REPLACE SECURE FUNCTION @@SF_SCHEMA@@.QUADBIN_FROMGEOPOINT
(point GEOGRAPHY, resolution INT)
RETURNS BIGINT
IMMUTABLE
AS $$
@@SF_SCHEMA@@._QUADBIN_FROMGEOGPOINT(point, resolution)
$$;
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,37 @@ test('H3_FROMGEOGPOINT returns NULL with non POINT geographies', async () => {
null,
null
]);
});

test('H3_FROMGEOPOINT returns the proper INT64', async () => {
const query = `
WITH inputs AS
(
SELECT 1 AS id, ST_POINT(-122.0553238, 37.3615593) as geom, 5 as resolution UNION ALL
SELECT 2 AS id, ST_POINT(-164.991559, 30.943387) as geom, 5 as resolution UNION ALL
SELECT 3 AS id, ST_POINT(71.52790329909925, 46.04189431883772) as geom, 15 as resolution UNION ALL

-- null inputs
SELECT 4 AS id, TRY_TO_GEOGRAPHY(NULL) AS geom, 5 as resolution UNION ALL
SELECT 5 AS id, ST_POINT(-122.0553238, 37.3615593) as geom, -1 as resolution UNION ALL
SELECT 6 AS id, ST_POINT(-122.0553238, 37.3615593) as geom, 20 as resolution UNION ALL
SELECT 7 AS id, ST_POINT(-122.0553238, 37.3615593) as geom, NULL as resolution
)
SELECT
CAST(H3_FROMGEOPOINT(geom, resolution) AS STRING) as h3_id
FROM inputs
ORDER BY id ASC
`;

const rows = await runQuery(query);
expect(rows.length).toEqual(7);
expect(rows.map((r) => r.H3_ID)).toEqual([
'85283473fffffff',
'8547732ffffffff',
'8f2000000000000',
null,
null,
null,
null
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ test('QUADBIN_FROMGEOGPOINT should work', async () => {
const rows = await runQuery(query);
expect(rows.length).toEqual(1);
expect(rows[0].OUTPUT).toEqual('5209574053332910079');
});

test('QUADBIN_FROMGEOPOINT should work', async () => {
const query = 'SELECT CAST(QUADBIN_FROMGEOPOINT(ST_POINT(40.4168, -3.7038), 4) AS STRING) AS OUTPUT';
const rows = await runQuery(query);
expect(rows.length).toEqual(1);
expect(rows[0].OUTPUT).toEqual('5209574053332910079');
});
Loading