-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bq,sf,rs,pg|quadbin): add function QUADBIN_DISTANCE (#457)
- Loading branch information
1 parent
e95e430
commit e5ab01b
Showing
17 changed files
with
339 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## QUADBIN_DISTANCE | ||
|
||
```sql:signature | ||
QUADBIN_DISTANCE(origin, destination) | ||
``` | ||
|
||
**Description** | ||
|
||
Returns the [Chebyshev distance](https://en.wikipedia.org/wiki/Chebyshev_distance) between two quadbin indexes. The origin and destination indices must have the same resolution. Otherwise `NULL` will be returned. | ||
|
||
* `origin`: `INT64` origin quadbin index. | ||
* `destination`: `INT64` destination quadbin index. | ||
|
||
**Return type** | ||
|
||
`GEOGRAPHY` | ||
|
||
**Example** | ||
|
||
```sql | ||
SELECT carto.QUADBIN_DISTANCE(5207251884775047167, 5207128739472736255); | ||
-- 1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
---------------------------- | ||
-- Copyright (C) 2023 CARTO | ||
---------------------------- | ||
|
||
CREATE OR REPLACE FUNCTION `@@BQ_DATASET@@.QUADBIN_DISTANCE` | ||
(origin INT64, destination INT64) | ||
RETURNS INT64 | ||
AS (( | ||
IF(origin IS NULL OR destination IS NULL, | ||
NULL, | ||
(WITH __quadbin_coords AS ( | ||
SELECT | ||
`@@BQ_DATASET@@.QUADBIN_TOZXY`(origin) AS origin_coords, | ||
`@@BQ_DATASET@@.QUADBIN_TOZXY`(destination) AS destination_coords | ||
) | ||
SELECT IF(origin_coords.z != destination_coords.z, | ||
NULL, | ||
GREATEST( | ||
ABS(destination_coords.x - origin_coords.x), | ||
ABS(destination_coords.y - origin_coords.y) | ||
) | ||
) | ||
FROM __quadbin_coords) | ||
) | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
clouds/bigquery/modules/test/quadbin/QUADBIN_DISTANCE.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const { runQuery } = require('../../../common/test-utils'); | ||
|
||
test('QUADBIN_DISTANCE should work', async () => { | ||
const query = 'SELECT `@@BQ_DATASET@@.QUADBIN_DISTANCE`(5207251884775047167, 5207128739472736255) AS output'; | ||
const rows = await runQuery(query); | ||
expect(rows.length).toEqual(1); | ||
expect(rows[0].output).toEqual(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## QUADBIN_DISTANCE | ||
|
||
```sql:signature | ||
QUADBIN_DISTANCE(origin, destination) | ||
``` | ||
|
||
**Description** | ||
|
||
Returns the [Chebyshev distance](https://en.wikipedia.org/wiki/Chebyshev_distance) between two quadbin indexes. The origin and destination indices must have the same resolution. Otherwise `NULL` will be returned. | ||
|
||
* `origin`: `BIGINT` origin quadbin index. | ||
* `destination`: `BIGINT` destination quadbin index. | ||
|
||
**Return type** | ||
|
||
`GEOGRAPHY` | ||
|
||
**Example** | ||
|
||
```sql | ||
SELECT carto.QUADBIN_DISTANCE(5207251884775047167, 5207128739472736255); | ||
-- 1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
---------------------------- | ||
-- Copyright (C) 2023 CARTO | ||
---------------------------- | ||
|
||
CREATE OR REPLACE FUNCTION @@PG_SCHEMA@@.QUADBIN_DISTANCE | ||
(origin BIGINT, destination BIGINT) | ||
RETURNS BIGINT | ||
AS | ||
$BODY$ | ||
SELECT | ||
CASE WHEN origin IS NULL OR destination IS NULL THEN | ||
NULL | ||
ELSE | ||
(WITH __quadbin_coords AS ( | ||
SELECT | ||
@@PG_SCHEMA@@.QUADBIN_TOZXY(origin) AS origin_coords, | ||
@@PG_SCHEMA@@.QUADBIN_TOZXY(destination) AS destination_coords | ||
) | ||
SELECT | ||
CASE WHEN (origin_coords->>'z')::INT != (destination_coords->>'z')::INT THEN | ||
NULL | ||
ELSE | ||
GREATEST( | ||
ABS((destination_coords->>'x')::BIGINT - (origin_coords->>'x')::BIGINT), | ||
ABS((destination_coords->>'y')::BIGINT - (origin_coords->>'y')::BIGINT) | ||
) | ||
END | ||
FROM __quadbin_coords | ||
) | ||
END | ||
$BODY$ | ||
LANGUAGE sql IMMUTABLE PARALLEL SAFE; |
10 changes: 10 additions & 0 deletions
10
clouds/postgres/modules/test/quadbin/test_QUADBIN_DISTANCE.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from test_utils import run_query | ||
|
||
|
||
def test_quadbin_resolution(): | ||
"""Computes distance between quadbins.""" | ||
result = run_query( | ||
'SELECT @@PG_SCHEMA@@.QUADBIN_DISTANCE' | ||
'(5207251884775047167, 5207128739472736255)' | ||
) | ||
assert result[0][0] == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## QUADBIN_DISTANCE | ||
|
||
```sql:signature | ||
QUADBIN_DISTANCE(origin, destination) | ||
``` | ||
|
||
**Description** | ||
|
||
Returns the [Chebyshev distance](https://en.wikipedia.org/wiki/Chebyshev_distance) between two quadbin indexes. The origin and destination indices must have the same resolution. Otherwise `NULL` will be returned. | ||
|
||
* `origin`: `BIGINT` origin quadbin index. | ||
* `destination`: `BIGINT` destination quadbin index. | ||
|
||
**Return type** | ||
|
||
`GEOGRAPHY` | ||
|
||
**Example** | ||
|
||
```sql | ||
SELECT carto.QUADBIN_DISTANCE(5207251884775047167, 5207128739472736255); | ||
-- 1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
---------------------------- | ||
-- Copyright (C) 2023 CARTO | ||
---------------------------- | ||
|
||
CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.QUADBIN_DISTANCE | ||
(origin BIGINT, destination BIGINT) | ||
RETURNS BIGINT | ||
STABLE | ||
AS $$ | ||
SELECT CASE WHEN $1 IS NULL OR $2 IS NULL THEN | ||
NULL | ||
ELSE | ||
CASE WHEN @@RS_SCHEMA@@.QUADBIN_RESOLUTION($1) != @@RS_SCHEMA@@.QUADBIN_RESOLUTION($2) THEN | ||
NULL | ||
ELSE | ||
GREATEST( | ||
ABS( | ||
(@@RS_SCHEMA@@.__QUADBIN_TOZXY_X($2) >> (32 - CAST(@@RS_SCHEMA@@.QUADBIN_RESOLUTION($2) AS INT))) - | ||
(@@RS_SCHEMA@@.__QUADBIN_TOZXY_X($1) >> (32 - CAST(@@RS_SCHEMA@@.QUADBIN_RESOLUTION($1) AS INT))) | ||
), | ||
ABS( | ||
(@@RS_SCHEMA@@.__QUADBIN_TOZXY_Y($2) >> (32 - CAST(@@RS_SCHEMA@@.QUADBIN_RESOLUTION($2) AS INT))) - | ||
(@@RS_SCHEMA@@.__QUADBIN_TOZXY_Y($1) >> (32 - CAST(@@RS_SCHEMA@@.QUADBIN_RESOLUTION($1) AS INT))) | ||
) | ||
) | ||
END | ||
END | ||
$$ LANGUAGE sql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
---------------------------- | ||
-- Copyright (C) 2022 CARTO | ||
---------------------------- | ||
|
||
CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_INTERLEAVED5 | ||
(BIGINT) | ||
-- (quadbin) | ||
RETURNS BIGINT | ||
STABLE | ||
AS $$ | ||
SELECT ($1 | ($1 >> 16)) & CAST(FROM_HEX('00000000FFFFFFFF') AS BIGINT) | ||
$$ LANGUAGE sql; | ||
|
||
CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_INTERLEAVED4 | ||
(BIGINT) | ||
-- (quadbin) | ||
RETURNS BIGINT | ||
STABLE | ||
AS $$ | ||
SELECT @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_INTERLEAVED5( | ||
($1 | ($1 >> 8)) & CAST(FROM_HEX('0000FFFF0000FFFF') AS BIGINT) | ||
) | ||
$$ LANGUAGE sql; | ||
|
||
CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_INTERLEAVED3 | ||
(BIGINT) | ||
-- (quadbin) | ||
RETURNS BIGINT | ||
STABLE | ||
AS $$ | ||
SELECT @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_INTERLEAVED4( | ||
($1 | ($1 >> 4)) & CAST(FROM_HEX('00FF00FF00FF00FF') AS BIGINT) | ||
) | ||
$$ LANGUAGE sql; | ||
|
||
CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_INTERLEAVED2 | ||
(BIGINT) | ||
-- (quadbin) | ||
RETURNS BIGINT | ||
STABLE | ||
AS $$ | ||
SELECT @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_INTERLEAVED3( | ||
($1 | ($1 >> 2)) & CAST(FROM_HEX('0F0F0F0F0F0F0F0F') AS BIGINT) | ||
) | ||
$$ LANGUAGE sql; | ||
|
||
CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_INTERLEAVED1 | ||
(BIGINT) | ||
-- (quadbin) | ||
RETURNS BIGINT | ||
STABLE | ||
AS $$ | ||
SELECT @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_INTERLEAVED2( | ||
($1 | ($1 >> 1)) & CAST(FROM_HEX('3333333333333333') AS BIGINT) | ||
) | ||
$$ LANGUAGE sql; | ||
|
||
CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_PREINTERLEAVED | ||
(BIGINT) | ||
-- (quadbin) | ||
RETURNS BIGINT | ||
STABLE | ||
AS $$ | ||
SELECT @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_INTERLEAVED1( | ||
$1 & CAST(FROM_HEX('5555555555555555') AS BIGINT) | ||
) | ||
$$ LANGUAGE sql; | ||
|
||
CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.__QUADBIN_TOZXY_X | ||
(BIGINT) | ||
-- (quadbin) | ||
RETURNS BIGINT | ||
STABLE | ||
AS $$ | ||
SELECT @@RS_SCHEMA@@.__QUADBIN_TOZXY_XY_PREINTERLEAVED( | ||
($1 & CAST(FROM_HEX('00FFFFFFFFFFFFF') AS BIGINT)) << 12 | ||
) | ||
$$ LANGUAGE sql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
---------------------------- | ||
-- Copyright (C) 2022 CARTO | ||
---------------------------- | ||
|
||
CREATE OR REPLACE FUNCTION @@RS_SCHEMA@@.__QUADBIN_TOZXY_Y | ||
(BIGINT) | ||
-- (quadbin) | ||
RETURNS BIGINT | ||
STABLE | ||
AS $$ | ||
SELECT @@RS_SCHEMA@@.__QUADBIN_TOZXY_X($1 >> 1) | ||
$$ LANGUAGE sql; |
11 changes: 11 additions & 0 deletions
11
clouds/redshift/modules/test/quadbin/test_QUADBIN_DISTANCE.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from test_utils import run_query | ||
|
||
|
||
def test_quadbin_distance(): | ||
result = run_query( | ||
'SELECT @@RS_SCHEMA@@.QUADBIN_DISTANCE' | ||
'(5207251884775047167, 5207128739472736255)' | ||
) | ||
|
||
assert len(result[0]) == 1 | ||
assert result[0][0] == 1 |
Oops, something went wrong.