Skip to content

Commit

Permalink
WIP: GiST support
Browse files Browse the repository at this point in the history
  • Loading branch information
zachasme committed Sep 24, 2021
1 parent fe75ef9 commit 7abd722
Show file tree
Hide file tree
Showing 8 changed files with 808 additions and 25 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ avoid adding features or APIs which do not map onto the
Click to see more.
</summary>

- Add `gist` operator class (see [#42], thanks [@abelvm])

</details>


## [3.7.1] - 2021-06-23

- Update `h3` core library to `v3.7.1`
Expand Down Expand Up @@ -178,7 +181,11 @@ avoid adding features or APIs which do not map onto the
[#31]: https://github.com/bytesandbrains/h3-pg/pull/31
[#37]: https://github.com/bytesandbrains/h3-pg/issues/37
[#38]: https://github.com/bytesandbrains/h3-pg/issues/38
<<<<<<< HEAD
[#41]: https://github.com/bytesandbrains/h3-pg/issues/41
=======
[#42]: https://github.com/bytesandbrains/h3-pg/issues/42
>>>>>>> 371058d (WIP: GiST support)
[@abelvm]: https://github.com/AbelVM
[@komzpa]: https://github.com/Komzpa
[@kmacdough]: https://github.com/kmacdough
Expand Down
52 changes: 52 additions & 0 deletions h3/sql/install/13-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 gist_h3index_ops DEFAULT 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);
37 changes: 37 additions & 0 deletions h3/sql/updates/h3--3.7.1--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 gist_h3index_ops DEFAULT 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: 26 additions & 25 deletions h3/src/include/extension.in.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

#include <h3api.h> // Main H3 include

void _PG_init(void);
void _PG_init(void);
extern bool h3_guc_strict;

typedef struct
{
H3Index *indices;
int *distances;
} hexDistanceTuple;
H3Index *indices;
int *distances;
} hexDistanceTuple;

#define MAX_H3_RES 15

Expand All @@ -41,9 +41,9 @@ typedef struct
*/

#ifdef USE_FLOAT8_BYVAL
#define DatumGetH3Index(X) ((H3Index) (X))
#define DatumGetH3Index(X) ((H3Index)(X))
#else
#define DatumGetH3Index(X) (* ((H3Index *) DatumGetPointer(X)))
#define DatumGetH3Index(X) (*((H3Index *)DatumGetPointer(X)))
#endif

/*
Expand All @@ -55,9 +55,9 @@ typedef struct
*/

#ifdef USE_FLOAT8_BYVAL
#define H3IndexGetDatum(X) ((Datum) (X))
#define H3IndexGetDatum(X) ((Datum)(X))
#else
#define H3IndexGetDatum(X) Int64GetDatum((int64) (X))
#define H3IndexGetDatum(X) Int64GetDatum((int64)(X))
#endif

/* Macros for fetching arguments and returning results of h3 index type */
Expand All @@ -66,37 +66,38 @@ typedef struct
#define PG_RETURN_H3INDEX(x) return H3IndexGetDatum(x)

/* helper functions to return sets from user fctx */
Datum srf_return_h3_indexes_from_user_fctx(PG_FUNCTION_ARGS);
Datum srf_return_h3_index_distances_from_user_fctx(PG_FUNCTION_ARGS);
Datum srf_return_h3_indexes_from_user_fctx(PG_FUNCTION_ARGS);
Datum srf_return_h3_index_distances_from_user_fctx(PG_FUNCTION_ARGS);

/* macros to pass on fcinfo to above helpers */
#define SRF_RETURN_H3_INDEXES_FROM_USER_FCTX() \
return srf_return_h3_indexes_from_user_fctx(fcinfo)
#define SRF_RETURN_H3_INDEX_DISTANCES_FROM_USER_FCTX() \
return srf_return_h3_index_distances_from_user_fctx(fcinfo)

#define ASSERT(condition, code, msg, ...) \
if (0 == (condition)) ereport(ERROR, ( \
errcode(code), \
errmsg(msg, ##__VA_ARGS__) \
))
#define ASSERT(condition, code, msg, ...) \
if (0 == (condition)) \
ereport(ERROR, ( \
errcode(code), \
errmsg(msg, ##__VA_ARGS__)))

#define ASSERT_EXTERNAL(condition, msg, ...) \
ASSERT(condition, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION, msg, ##__VA_ARGS__)

#define ENSURE_TYPEFUNC_COMPOSITE(x) \
ASSERT( \
x == TYPEFUNC_COMPOSITE, \
ERRCODE_INVALID_PARAMETER_VALUE, \
#define ENSURE_TYPEFUNC_COMPOSITE(x) \
ASSERT( \
x == TYPEFUNC_COMPOSITE, \
ERRCODE_INVALID_PARAMETER_VALUE, \
"Function returning record called in context " \
"that cannot accept type record" \
)
"that cannot accept type record")

#define DEBUG(msg, ...) \
ereport(ERROR, ( \
errmsg(msg, ##__VA_ARGS__) \
))
#define DEBUG(msg, ...) \
ereport(ERROR, ( \
errmsg(msg, ##__VA_ARGS__)))

#define DEBUG_H3INDEX(h3index) DEBUG("index: %lx", h3index)
#define PRINT_H3INDEX(h3index) \
ereport(NOTICE, ( \
errmsg("index: %lx", h3index)))

#endif
Loading

0 comments on commit 7abd722

Please sign in to comment.