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

GiST support #42

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ avoid adding features or APIs which do not map onto the
- Use CMake for entire build (see [#70])
- Add helper to fix pass-by-value migration (see [#111])
- Allow distance operator `<->` to work for cells at different resolutions (using center child).
- Add `gist` operator class (see [#42], thanks [@abelvm])

## [4.0.3] - 2022-11-04

Expand Down Expand Up @@ -241,6 +242,7 @@ avoid adding features or APIs which do not map onto the
[#37]: https://github.com/zachasme/h3-pg/issues/37
[#38]: https://github.com/zachasme/h3-pg/issues/38
[#41]: https://github.com/zachasme/h3-pg/issues/41
[#42]: https://github.com/zachasme/h3-pg/pull/42
[#55]: https://github.com/zachasme/h3-pg/issues/55
[#64]: https://github.com/zachasme/h3-pg/issues/64
[#65]: https://github.com/zachasme/h3-pg/pull/65
Expand Down
24 changes: 24 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,30 @@ Returns true if A contains B.
Returns true if A is contained by B.


### h3index_gist_consistent(`internal`, `h3index`, `smallint`, `oid`, `internal`) ⇒ `boolean`


### h3index_gist_union(`internal`, `internal`) ⇒ `h3index`


### h3index_gist_compress(`internal`) ⇒ `internal`


### h3index_gist_decompress(`internal`) ⇒ `internal`


### h3index_gist_penalty(`internal`, `internal`, `internal`) ⇒ `internal`


### h3index_gist_picksplit(`internal`, `internal`) ⇒ `internal`


### h3index_gist_same(`h3index`, `h3index`, `internal`) ⇒ `internal`


### h3index_gist_distance(`internal`, `h3index`, `smallint`, `oid`, `internal`) ⇒ `float8`


# Type casts

### `h3index` :: `bigint`
Expand Down
3 changes: 3 additions & 0 deletions h3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ PostgreSQL_add_extension(postgresql_h3
src/binding/regions.c
src/binding/traversal.c
src/binding/vertex.c
src/algos.c
src/deprecated.c
src/extension.c
src/guc.c
src/init.c
src/opclass_btree.c
src/opclass_gist.c
src/opclass_hash.c
src/operators.c
src/srf.c
Expand All @@ -36,6 +38,7 @@ PostgreSQL_add_extension(postgresql_h3
sql/install/11-opclass_btree.sql
sql/install/12-opclass_hash.sql
sql/install/13-opclass_brin.sql
sql/install/14-opclass_gist.sql
sql/install/20-casts.sql
sql/install/30-extension.sql
sql/install/99-deprecated.sql
Expand Down
52 changes: 52 additions & 0 deletions h3/sql/install/14-opclass_gist.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2019-2020 Bytes & Brains
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

-- ---------- ---------- ---------- ---------- ---------- ---------- ----------
-- GiST Operator Class (opclass_gist.c)
-- ---------- ---------- ---------- ---------- ---------- ---------- ----------

CREATE OR REPLACE FUNCTION h3index_gist_consistent(internal, h3index, smallint, oid, internal) RETURNS boolean
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_union(internal, internal) RETURNS h3index
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_compress(internal) RETURNS internal
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_decompress(internal) RETURNS internal
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_penalty(internal, internal, internal) RETURNS internal
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_picksplit(internal, internal) RETURNS internal
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_same(h3index, h3index, internal) RETURNS internal
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_distance(internal, h3index, smallint, oid, internal) RETURNS float8
AS 'h3' LANGUAGE C STRICT;

CREATE OPERATOR CLASS experimental_h3index_ops FOR TYPE h3index USING gist AS
OPERATOR 3 && , -- RTOverlapStrategyNumber
OPERATOR 6 = , -- RTSameStrategyNumber
OPERATOR 7 @> , -- RTContainsStrategyNumber
OPERATOR 8 <@ , -- RTContainedByStrategyNumber
OPERATOR 15 <-> (h3index, h3index) FOR ORDER BY integer_ops,

FUNCTION 1 h3index_gist_consistent(internal, h3index, smallint, oid, internal),
FUNCTION 2 h3index_gist_union(internal, internal),
-- FUNCTION 3 h3index_gist_compress(internal),
-- FUNCTION 4 h3index_gist_decompress(internal),
FUNCTION 5 h3index_gist_penalty(internal, internal, internal),
FUNCTION 6 h3index_gist_picksplit(internal, internal),
FUNCTION 7 h3index_gist_same(h3index, h3index, internal),
FUNCTION 8 (h3index, h3index) h3index_gist_distance(internal, h3index, smallint, oid, internal);
37 changes: 37 additions & 0 deletions h3/sql/updates/h3--4.1.3--unreleased.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,40 @@

-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "ALTER EXTENSION h3 UPDATE TO 'unreleased'" to load this file. \quit

-- ---------- ---------- ---------- ---------- ---------- ---------- ----------
-- GiST Operator Class (opclass_gist.c)
-- ---------- ---------- ---------- ---------- ---------- ---------- ----------

CREATE OR REPLACE FUNCTION h3index_gist_consistent(internal, h3index, smallint, oid, internal) RETURNS boolean
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_union(internal, internal) RETURNS h3index
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_compress(internal) RETURNS internal
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_decompress(internal) RETURNS internal
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_penalty(internal, internal, internal) RETURNS internal
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_picksplit(internal, internal) RETURNS internal
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_same(h3index, h3index, internal) RETURNS internal
AS 'h3' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION h3index_gist_distance(internal, h3index, smallint, oid, internal) RETURNS float8
AS 'h3' LANGUAGE C STRICT;

CREATE OPERATOR CLASS experimental_h3index_ops FOR TYPE h3index USING gist AS
OPERATOR 3 && ,
OPERATOR 6 = ,
OPERATOR 7 @> ,
OPERATOR 8 <@ ,
OPERATOR 15 <-> (h3index, h3index) FOR ORDER BY integer_ops,

FUNCTION 1 h3index_gist_consistent(internal, h3index, smallint, oid, internal),
FUNCTION 2 h3index_gist_union(internal, internal),
-- FUNCTION 3 h3index_gist_compress(internal),
-- FUNCTION 4 h3index_gist_decompress(internal),
FUNCTION 5 h3index_gist_penalty(internal, internal, internal),
FUNCTION 6 h3index_gist_picksplit(internal, internal),
FUNCTION 7 h3index_gist_same(h3index, h3index, internal),
FUNCTION 8 (h3index, h3index) h3index_gist_distance(internal, h3index, smallint, oid, internal);
51 changes: 51 additions & 0 deletions h3/src/algos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 Bytes & Brains
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <h3api.h>
#include "algos.h"
#include "error.h"

H3Index
finest_common_ancestor(H3Index a, H3Index b)
{
int aRes,
bRes,
coarsestRes;
H3Index aParent,
bParent;

if (a == b)
return a;

/* do not even share the basecell */
if (getBaseCellNumber(a) != getBaseCellNumber(b))
return H3_NULL;

aRes = getResolution(a);
bRes = getResolution(b);
coarsestRes = (aRes < bRes) ? aRes : bRes;

/* iterate backwards through resolutions */
for (int i = coarsestRes; i > 0; i--)
{
h3_assert(cellToParent(a, i, &aParent));
h3_assert(cellToParent(b, i, &bParent));
if (aParent == bParent)
return aParent;
}

return H3_NULL;
}
24 changes: 24 additions & 0 deletions h3/src/algos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2023 Bytes & Brains
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef H3_ALGOS_H
#define H3_ALGOS_H

#include <h3api.h>

H3Index finest_common_ancestor(H3Index, H3Index);

#endif /* H3_ALGOS_H */
Loading
Loading