Number of dimensions of an array parameter

From: Thomas Hallgren <thomas(at)tada(dot)se>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Number of dimensions of an array parameter
Date: 2006-05-08 16:33:56
Message-ID: 445F72F4.5050906@tada.se
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I can create a function that takes a two dimension int array:

CREATE FUNCTION twodims(int[][]) RETURNS void AS ...

but there's nothing stopping me from calling this function with an
arbitrary number of dimensions on the array.

I'd like to map a parameter like the one above to a corresponding
representation in Java (it would be int[][] there too). As it turns out,
I can't do that. PostgreSQL will not store any information that can tell
me how many dimensions that where used in the declaration, i.e. it's
impossible to write a language VALIDATOR that, based on the information
in pg_proc, builds a signature where the number of dimensions is reflected.

This leaves me with two choices:

Only allow arrays with one dimension unless the parameter is of a domain
type (domains are apparently stored with the actual number of
dimensions). Any call that uses an array parameter with more then one
dimension will yield an exception.
--OR--
Always map to Object[] instead of mapping to the correct type, . This
will work since an array in Java is also an Object and all primitive
types can be represented as objects (i.e. int can be a
java.lang.Integer). The strong typing and the ability to use primitives
are lost however.

I'm leaning towards #1 and hoping that PostgreSQL will enhance the
parameter type declarations to include the dimensions in future releases.

... After some more testing ...

Unfortunately, I run into problems even when I use domains. Consider the
following:

thhal=# CREATE DOMAIN twodims as int[][];
CREATE DOMAIN
thhal=# SELECT typndims FROM pg_type WHERE typname = 'twodims';
typndims
----------
2
(1 row)

thhal=# SELECT array_dims('{{{1,2,3},{3,4,3}},{{5,3,2},{9,9,9}}}'::twodims);
array_dims
-----------------
[1:2][1:2][1:3]
(1 row)

IMO, there is something seriously wrong here. Clearly the number of
dimensions is a property of the type. Any array with a different number
of dimensions should yield an error or at least be coerced into the
right number of dimensions.

Kind Regards,
Thomas Hallgren

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2006-05-08 16:48:39 Re: Number of dimensions of an array parameter
Previous Message Martijn van Oosterhout 2006-05-08 14:48:44 Re: [PATCH] Magic block for modules