Help on creating C function that reads int2[] (using "int2vector")

From: Rodrigo Hjort <rodrigo(dot)hjort(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Help on creating C function that reads int2[] (using "int2vector")
Date: 2015-11-30 02:19:56
Message-ID: CAAjEfRzJAoX8A6a7fo+tfHUGkDnfsPQJCPD+PO9CPAy8f9YVvA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello PG Hackers,

I created a custom C function with this signature:

CREATE FUNCTION calculate_hash(numbers int2[])
RETURNS int8
AS 'MODULE_PATHNAME', 'pg_calculate_hash'
LANGUAGE C
IMMUTABLE STRICT;

And here is the function source code (inspired in codes I found in
src/backend/utils/adt/int.c):

PG_FUNCTION_INFO_V1(pg_calculate_hash);
Datum
pg_calculate_hash(PG_FUNCTION_ARGS)
{
int2vector *int2Array = (int2vector *) PG_GETARG_POINTER(0);
const int qtd = int2Array->dim1;

elog(DEBUG1, "pg_calculate_hash(qtd=%d)", qtd);

elog(DEBUG2, " [ndim=%d, dataoffset=%d, elemtype=%d, dim1=%d,
lbound1=%d]",
int2Array->ndim, int2Array->dataoffset, int2Array->elemtype,
int2Array->dim1, int2Array->lbound1);

[...]
}

In order to test it against a table structure, I executed these
instructions on psql:

db=# create table ss (s int2[]);
CREATE TABLE

db=# \d+ ss
Table "public.ss"
Column | Type | Modifiers | Storage | Stats target | Description
--------+------------+-----------+----------+--------------+-------------
s | smallint[] | | extended | |
Has OIDs: no

db=# insert into ss values ('[0:5]={58,17,15,36,59,54}');
INSERT 0 1

db=# select * from ss;
s
---------------------------
[0:5]={58,17,15,36,59,54}
(1 row)

Then, whenever calling the function passing the int2[] column directly,
strange values are read into the "int2vector" object:

db=# set client_min_messages to debug2;
SET

db=# select s, calculate_hash(s) from ss;
DEBUG: pg_calculate_hash(qtd=0)
DEBUG: [ndim=0, dataoffset=5376, elemtype=1536, dim1=0,
lbound1=285227520]
s | calculate_hash
---------------------------+---------------
[0:5]={58,17,15,36,59,54} | 0
(1 row)

On the other hand, when I double-cast the int2[] column value, it works as
expected (reading the proper "int2vector" structure):

db=# select s, calculate_hash(s::varchar::int2[]) from ss;
DEBUG: pg_calculate_hash(qtd=6)
DEBUG: [ndim=1, dataoffset=0, elemtype=21, dim1=6, lbound1=0]
s | calculate_hash
---------------------------+--------------------
[0:5]={58,17,15,36,59,54} | 441352797842128896
(1 row)

Please, what is wrong with that function code?

Thanks in advance.

The whole project is on GitHub:
https://github.com/hjort/mega-sena/tree/master/src

Best regards,

--
Rodrigo Hjort
www.hjort.co

Browse pgsql-hackers by date

  From Date Subject
Next Message Rodrigo Hjort 2015-11-30 02:26:51 Issue on C function that reads int2[] (using "int2vector")
Previous Message Andrew Dunstan 2015-11-29 22:57:17 Re: Re: In-core regression tests for replication, cascading, archiving, PITR, etc.