Skip to content

Commit

Permalink
Revert to existing H3_BOUNDARY until SF improve H3_CELL_TO_BOUNDARY_P…
Browse files Browse the repository at this point in the history
…ERFORMANCE
  • Loading branch information
DeanSherwin committed Mar 27, 2024
1 parent d0e78a9 commit 0f8cc80
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { h3Distance, h3IsPentagon } from '../src/h3/h3_kring_distances/h3core_cu

export default {
h3Distance,
h3IsPentagon
};
h3IsPentagon
};
35 changes: 27 additions & 8 deletions clouds/snowflake/modules/sql/h3/H3_BOUNDARY.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
--------------------------------
-- Copyright (C) 2021-2024 CARTO
--------------------------------
----------------------------
-- Copyright (C) 2021 CARTO
----------------------------
-- TODO: Re-implement this using SF's native H3_CELL_TO_BOUNDARY when they improve performance
-- Shortcut story/chore ID: 398796

CREATE OR REPLACE FUNCTION @@SF_SCHEMA@@._H3_BOUNDARY
(index STRING)
RETURNS STRING
LANGUAGE JAVASCRIPT
IMMUTABLE
AS $$
if (!INDEX) {
return null;
}

@@SF_LIBRARY_H3_BOUNDARY@@

if (!h3BoundaryLib.h3IsValid(INDEX)) {
return null;
}

const coords = h3BoundaryLib.h3ToGeoBoundary(INDEX, true);
const uniqueCoords = h3BoundaryLib.removeNextDuplicates(coords);
return `POLYGON((${uniqueCoords.map(c => `${c[0]} ${c[1]}`).join(',')}))`;
$$;

CREATE OR REPLACE SECURE FUNCTION @@SF_SCHEMA@@.H3_BOUNDARY
(index STRING)
RETURNS GEOGRAPHY
IMMUTABLE
AS $$
IFF(
@@SF_SCHEMA@@.H3_ISVALID(INDEX),
H3_CELL_TO_BOUNDARY(INDEX),
NULL
)
TRY_TO_GEOGRAPHY(@@SF_SCHEMA@@._H3_BOUNDARY(INDEX))
$$;
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ test('H3_KRING_DISTANCES should fail if any invalid argument', async () => {

query = 'SELECT H3_KRING_DISTANCES(\'8928308280fffff\', -1)';
await expect(runQuery(query)).rejects.toThrow(/Invalid input size/);
});
});

0 comments on commit 0f8cc80

Please sign in to comment.