Kohei KaiGai <kai...@heterodb.com> writes: > Thanks, the attached patch add cube--1.5 for binary send/recv functions and > modification of cube type using the new ALTER TYPE.
Hm, that was already superseded by events (112d411fb). As long as we get this done for v14, we can just treat it as an add-on to cube 1.5, so here's a quick rebase onto HEAD. Scanning the code, I have a couple of gripes. I'm not sure it's a good plan to just send the "header" field raw like that --- would breaking it into a dimension field and a point bool be better? In any case, the receive function has to be more careful than this about accepting only valid header values. Also, I don't think "offsetof(NDBOX, x[nitems])" is per project style. It at least used to be true that MSVC couldn't cope with that, so we prefer offsetof(NDBOX, x) + nitems * sizeof(whatever) regards, tom lane
diff --git a/contrib/cube/cube--1.4--1.5.sql b/contrib/cube/cube--1.4--1.5.sql index 54492e5d18..4b5bf8d205 100644 --- a/contrib/cube/cube--1.4--1.5.sql +++ b/contrib/cube/cube--1.4--1.5.sql @@ -6,3 +6,16 @@ -- Remove @ and ~ DROP OPERATOR @ (cube, cube); DROP OPERATOR ~ (cube, cube); + +-- Add binary input/output handlers +CREATE FUNCTION cube_recv(internal) +RETURNS cube +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + +CREATE FUNCTION cube_send(cube) +RETURNS bytea +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + +ALTER TYPE cube SET ( RECEIVE = cube_recv, SEND = cube_send ); diff --git a/contrib/cube/cube.c b/contrib/cube/cube.c index 6f810b26c5..d6b0dd75b0 100644 --- a/contrib/cube/cube.c +++ b/contrib/cube/cube.c @@ -13,6 +13,7 @@ #include "access/gist.h" #include "access/stratnum.h" #include "cubedata.h" +#include "libpq/pqformat.h" #include "utils/array.h" #include "utils/float.h" @@ -31,6 +32,8 @@ PG_FUNCTION_INFO_V1(cube_in); PG_FUNCTION_INFO_V1(cube_a_f8_f8); PG_FUNCTION_INFO_V1(cube_a_f8); PG_FUNCTION_INFO_V1(cube_out); +PG_FUNCTION_INFO_V1(cube_send); +PG_FUNCTION_INFO_V1(cube_recv); PG_FUNCTION_INFO_V1(cube_f8); PG_FUNCTION_INFO_V1(cube_f8_f8); PG_FUNCTION_INFO_V1(cube_c_f8); @@ -319,6 +322,55 @@ cube_out(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(buf.data); } +/* + * cube_send - a binary output handler for cube type + */ +Datum +cube_send(PG_FUNCTION_ARGS) +{ + NDBOX *cube = PG_GETARG_NDBOX_P(0); + StringInfoData buf; + int32 i, nitems = DIM(cube); + + pq_begintypsend(&buf); + pq_sendint32(&buf, cube->header); + for (i=0; i < nitems; i++) + { + pq_sendfloat8(&buf, LL_COORD(cube, i)); + } + if (!cube_is_point_internal(cube)) + { + for (i=0; i < nitems; i++) + { + pq_sendfloat8(&buf, UR_COORD(cube, i)); + } + } + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); +} + +/* + * cube_recv - a binary input handler for cube type + */ +Datum +cube_recv(PG_FUNCTION_ARGS) +{ + StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); + int32 header; + int i, nitems; + NDBOX *cube; + + header = pq_getmsgint(buf, sizeof(int32)); + nitems = (header & DIM_MASK); + if ((header & POINT_BIT) == 0) + nitems += nitems; + cube = palloc(offsetof(NDBOX, x[nitems])); + SET_VARSIZE(cube, offsetof(NDBOX, x[nitems])); + cube->header = header; + for (i=0; i < nitems; i++) + cube->x[i] = pq_getmsgfloat8(buf); + + PG_RETURN_NDBOX_P(cube); +} /***************************************************************************** * GiST functions