Skip to content

Commit

Permalink
Add support for binary i/o
Browse files Browse the repository at this point in the history
  • Loading branch information
robertozimek committed Nov 2, 2024
1 parent 0706e05 commit ce0f888
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
12 changes: 12 additions & 0 deletions h3/sql/install/00-type.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,20 @@ CREATE OR REPLACE FUNCTION
h3index_out(h3index) RETURNS cstring
AS 'h3' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

--@ internal
CREATE OR REPLACE FUNCTION
h3index_recv(internal) RETURNS h3index
AS 'h3' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

--@ internal
CREATE OR REPLACE FUNCTION
h3index_send(h3index) RETURNS bytea
AS 'h3' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE TYPE h3index (
INPUT = h3index_in,
OUTPUT = h3index_out,
RECEIVE = h3index_recv,
SEND = h3index_send,
LIKE = int8
);
15 changes: 15 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,18 @@

-- 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

--@ internal
CREATE OR REPLACE FUNCTION
h3index_recv(internal) RETURNS h3index
AS 'h3' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

--@ internal
CREATE OR REPLACE FUNCTION
h3index_send(h3index) RETURNS byta
AS 'h3' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

ALTER TYPE h3index SET (
RECEIVE = h3index_recv,
SEND = h3index_send
);
30 changes: 30 additions & 0 deletions h3/src/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
#include <h3api.h>

#include <fmgr.h> // PG_FUNCTION_ARGS
#include <libpq/pqformat.h> // needed for send/recv functions

#include "error.h"
#include "type.h"

/* conversion */
PGDLLEXPORT PG_FUNCTION_INFO_V1(h3index_in);
PGDLLEXPORT PG_FUNCTION_INFO_V1(h3index_out);
PGDLLEXPORT PG_FUNCTION_INFO_V1(h3index_send);
PGDLLEXPORT PG_FUNCTION_INFO_V1(h3index_recv);
PGDLLEXPORT PG_FUNCTION_INFO_V1(h3index_to_bigint);
PGDLLEXPORT PG_FUNCTION_INFO_V1(bigint_to_h3index);

Expand All @@ -51,6 +54,33 @@ h3index_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(string);
}

Datum
h3index_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
H3Index h3;

const char *string = pq_getmsgstring(buf);
h3_assert(stringToH3(string, &h3));

PG_RETURN_POINTER(h3);
}

Datum
h3index_send(PG_FUNCTION_ARGS)
{
H3Index h3 = PG_GETARG_H3INDEX(0);
char *string = palloc(17 * sizeof(char));

h3_assert(h3ToString(h3, string, 17));
StringInfoData buf;

pq_begintypsend(&buf);
pq_sendstring(&buf, string);

PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

/* bigint conversion functions */
Datum
h3index_to_bigint(PG_FUNCTION_ARGS)
Expand Down
9 changes: 9 additions & 0 deletions h3/test/expected/type.out
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ SELECT :asbigint = :hexagon::bigint;
SELECT :hexagon = :asbigint::h3index;
t

--
-- TEST binary io
--
COPY (SELECT :hexagon) TO '/tmp/test.bin' (FORMAT binary);
CREATE TEMPORARY TABLE tmp (val h3index);
COPY tmp FROM '/tmp/test.bin' (FORMAT binary);
SELECT val = :hexagon FROM tmp WHERE val = :hexagon;
t

8 changes: 8 additions & 0 deletions h3/test/sql/type.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ SELECT bool_and(:pentagon @> c) FROM (
SELECT :asbigint = :hexagon::bigint;

SELECT :hexagon = :asbigint::h3index;

--
-- TEST binary io
--
COPY (SELECT :hexagon) TO '/tmp/test.bin' (FORMAT binary);
CREATE TEMPORARY TABLE tmp (val h3index);
COPY tmp FROM '/tmp/test.bin' (FORMAT binary);
SELECT val = :hexagon FROM tmp WHERE val = :hexagon;

0 comments on commit ce0f888

Please sign in to comment.