Re: [SQL] ARRAY() returning NULL instead of ARRAY[]

Lists: pgsql-hackerspgsql-patchespgsql-sql
From: Markus Bertheau <twanger(at)bluetwanger(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Subject: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-23 18:33:39
Message-ID: 1116873219.7306.0.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Hi,

why does SELECT ARRAY(SELECT 1 WHERE FALSE) return NULL instead of
ARRAY[] resp. '{}'?

Markus

--
Markus Bertheau <twanger(at)bluetwanger(dot)de>


From: "Jan B(dot)" <jan(at)monso(dot)de>
To: Markus Bertheau <twanger(at)bluetwanger(dot)de>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-23 18:54:07
Message-ID: 429226CF.4090207@monso.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Markus Bertheau wrote:
> Hi,
>
> why does SELECT ARRAY(SELECT 1 WHERE FALSE) return NULL instead of
> ARRAY[] resp. '{}'?
>
> Markus
>

Perhaps Arrays always have to contain at least one element? (I don't
know for sure.)

SELECT array[];
ERROR: syntax error at or near "]" at character 14
LINE 1: SELECT array[];

SELECT array[1];
array
-------
{1}
(1 row)


From: Markus Bertheau <twanger(at)bluetwanger(dot)de>
To: "Jan B(dot)" <jan(at)monso(dot)de>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-23 18:58:11
Message-ID: 1116874691.7306.3.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Dnia 23-05-2005, pon o godzinie 18:54 +0000, Jan B. napisał(a):

> Perhaps Arrays always have to contain at least one element? (I don't
> know for sure.)

They can:

template1=# select '{}'::TEXT[];
text
------
{}
(1 запись)

I don't know, why the ARRAY[] syntax doesn't work for empty arrays.

Markus

--
Markus Bertheau <twanger(at)bluetwanger(dot)de>


From: Joe Conway <mail(at)joeconway(dot)com>
To: Markus Bertheau <twanger(at)bluetwanger(dot)de>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-23 20:04:50
Message-ID: 42923762.7010708@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Markus Bertheau wrote:
> why does SELECT ARRAY(SELECT 1 WHERE FALSE) return NULL instead of
> ARRAY[] resp. '{}'?
>

Why would you expect an empty array instead of a NULL? NULL is what
you'd get for other data types -- for example:

regression=# SELECT (SELECT 1 WHERE FALSE) IS NULL;
?column?
----------
t
(1 row)

Joe


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Markus Bertheau <twanger(at)bluetwanger(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-24 04:06:00
Message-ID: 16774.1116907560@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Joe Conway <mail(at)joeconway(dot)com> writes:
> Markus Bertheau wrote:
>> why does SELECT ARRAY(SELECT 1 WHERE FALSE) return NULL instead of
>> ARRAY[] resp. '{}'?

> Why would you expect an empty array instead of a NULL?

I think he's got a good point, actually. We document the ARRAY-with-
parens-around-a-SELECT syntax as

The resulting one-dimensional array will have an element for
each row in the subquery result, with an element type matching
that of the subquery's output column.

To me, that implies that a subquery result of no rows generates a
one-dimensional array of no elements, not a null array.

This is not the same as

SELECT ARRAY[(SELECT 1 WHERE FALSE)];

We define a scalar subquery that returns no rows as returning null, so
this is equivalent to

SELECT ARRAY[NULL];

which *ought* to yield an array containing a single NULL element,
but since we can't yet handle arrays containing nulls we punt and
return a null array value. That's wrong too ... but it's a different
issue. The point Markus is complaining about seems like it should
be easily fixable.

regards, tom lane


From: Markus Bertheau <twanger(at)bluetwanger(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Joe Conway <mail(at)joeconway(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-24 06:02:25
Message-ID: 1116914546.7306.0.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Dnia 24-05-2005, wto o godzinie 00:06 -0400, Tom Lane napisał(a):
> Joe Conway <mail(at)joeconway(dot)com> writes:
> > Markus Bertheau wrote:
> >> why does SELECT ARRAY(SELECT 1 WHERE FALSE) return NULL instead of
> >> ARRAY[] resp. '{}'?
>
> > Why would you expect an empty array instead of a NULL?
>
> I think he's got a good point, actually. We document the ARRAY-with-
> parens-around-a-SELECT syntax as
>
> The resulting one-dimensional array will have an element for
> each row in the subquery result, with an element type matching
> that of the subquery's output column.
>
> To me, that implies that a subquery result of no rows generates a
> one-dimensional array of no elements, not a null array.
>
> The point Markus is complaining about seems like it should
> be easily fixable.

Great :) Is this a TODO?

--
Markus Bertheau <twanger(at)bluetwanger(dot)de>


From: Markus Bertheau <twanger(at)bluetwanger(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-24 06:08:23
Message-ID: 1116914903.7306.3.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Dnia 24-05-2005, wto o godzinie 00:06 -0400, Tom Lane napisał(a):
> Joe Conway <mail(at)joeconway(dot)com> writes:
> > Markus Bertheau wrote:
> >> why does SELECT ARRAY(SELECT 1 WHERE FALSE) return NULL instead of
> >> ARRAY[] resp. '{}'?
>
> > Why would you expect an empty array instead of a NULL?
>
> I think he's got a good point, actually. We document the ARRAY-with-
> parens-around-a-SELECT syntax as
>
> The resulting one-dimensional array will have an element for
> each row in the subquery result, with an element type matching
> that of the subquery's output column.
>
> To me, that implies that a subquery result of no rows generates a
> one-dimensional array of no elements, not a null array.
>
> The point Markus is complaining about seems like it should
> be easily fixable.

Great. Does this belong on the TODO?

--
Markus Bertheau <twanger(at)bluetwanger(dot)de>


From: Achilleus Mantzios <achill(at)matrix(dot)gatewaynet(dot)com>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Markus Bertheau <twanger(at)bluetwanger(dot)de>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-24 06:21:18
Message-ID: Pine.LNX.4.44.0505240911160.10967-100000@matrix.gatewaynet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

O Joe Conway έγραψε στις May 23, 2005 :

> Markus Bertheau wrote:
> > why does SELECT ARRAY(SELECT 1 WHERE FALSE) return NULL instead of
> > ARRAY[] resp. '{}'?
> >
>
> Why would you expect an empty array instead of a NULL? NULL is what
> you'd get for other data types -- for example:

One could ask in the same fashion why someone would want a table
if this table contains no rows.

A null value may mean "dont know",
wheras a '{}' (empty) value may mean "empty set".

For instance lets consider the case where an array holds
the factors of a polynomial formula.

An null value might mean that the person defining
the formulas haven't been bothered with this one yet.
An empty value might mean that the person indicates
that has worked on this particular one, but he/she has no data yet.

Ok extreme cases, but to me there is a clean distinction
between a null array and an empty array.

Also what is definately needed is arrays that may contain
null values.

>
> regression=# SELECT (SELECT 1 WHERE FALSE) IS NULL;
> ?column?
> ----------
> t
> (1 row)
>
> Joe
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq
>

--
-Achilleus


From: Joe Conway <mail(at)joeconway(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Markus Bertheau <twanger(at)bluetwanger(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-26 19:06:44
Message-ID: 42961E44.7040701@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Tom Lane wrote:
> I think he's got a good point, actually. We document the ARRAY-with-
> parens-around-a-SELECT syntax as
>
> The resulting one-dimensional array will have an element for
> each row in the subquery result, with an element type matching
> that of the subquery's output column.
>
> To me, that implies that a subquery result of no rows generates a
> one-dimensional array of no elements, not a null array.

OK, looks like I'm outnumbered.

But as far as I know, we have never had a way to produce a
one-dimensional empty array. Empty arrays thus far have been dimensionless.

Assuming we really want an empty 1D array, I created the attached patch.
This works fine, but now leaves a few oddities to be dealt with, e.g.:

regression=# select array_dims(array(select 1 where false));
array_dims
------------
[1:0]
(1 row)

Any thoughts on how this should be handled for an empty 1D array?

> The point Markus is complaining about seems like it should
> be easily fixable.

Well, "easily" is a relative term. My Postgres hacking neurons have
gotten kind of rusty lately -- but then maybe that was your underlying
point ;-)

Joe

Attachment Content-Type Size
current.81.diff text/x-patch 7.8 KB

From: Joe Conway <mail(at)joeconway(dot)com>
To: "Patches (PostgreSQL)" <pgsql-patches(at)postgresql(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Markus Bertheau <twanger(at)bluetwanger(dot)de>
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-31 21:31:16
Message-ID: 429CD7A4.7020702@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Joe Conway wrote:
> OK, looks like I'm outnumbered.
>
> But as far as I know, we have never had a way to produce a
> one-dimensional empty array. Empty arrays thus far have been dimensionless.
>
> Assuming we really want an empty 1D array, I created the attached patch.
> This works fine, but now leaves a few oddities to be dealt with, e.g.:
>
> regression=# select array_dims(array(select 1 where false));
> array_dims
> ------------
> [1:0]
> (1 row)
>
> Any thoughts on how this should be handled for an empty 1D array?

Any thoughts or objections? If not, I'll commit the attached in a day or so.

Thanks,

Joe

> ------------------------------------------------------------------------
>
> Index: src/backend/executor/nodeSubplan.c
> ===================================================================
> RCS file: /cvsroot/pgsql/src/backend/executor/nodeSubplan.c,v
> retrieving revision 1.69
> diff -c -r1.69 nodeSubplan.c
> *** src/backend/executor/nodeSubplan.c 6 May 2005 17:24:54 -0000 1.69
> --- src/backend/executor/nodeSubplan.c 26 May 2005 18:52:16 -0000
> ***************
> *** 215,220 ****
> --- 215,221 ----
> ListCell *pvar;
> ListCell *l;
> ArrayBuildState *astate = NULL;
> + Oid element_type = planstate->ps_ResultTupleSlot->tts_tupleDescriptor->attrs[0]->atttypid;
>
> /*
> * We are probably in a short-lived expression-evaluation context.
> ***************
> *** 259,268 ****
> *
> * For EXPR_SUBLINK we require the subplan to produce no more than one
> * tuple, else an error is raised. For ARRAY_SUBLINK we allow the
> ! * subplan to produce more than one tuple. In either case, if zero
> ! * tuples are produced, we return NULL. Assuming we get a tuple, we
> ! * just use its first column (there can be only one non-junk column in
> ! * this case).
> */
> result = BoolGetDatum(subLinkType == ALL_SUBLINK);
> *isNull = false;
> --- 260,269 ----
> *
> * For EXPR_SUBLINK we require the subplan to produce no more than one
> * tuple, else an error is raised. For ARRAY_SUBLINK we allow the
> ! * subplan to produce more than one tuple. In the former case, if zero
> ! * tuples are produced, we return NULL. In the latter, we return an
> ! * empty array. Assuming we get a tuple, we just use its first column
> ! * (there can be only one non-junk column in this case).
> */
> result = BoolGetDatum(subLinkType == ALL_SUBLINK);
> *isNull = false;
> ***************
> *** 432,458 ****
> }
> }
>
> ! if (!found)
> {
> /*
> * deal with empty subplan result. result/isNull were previously
> ! * initialized correctly for all sublink types except EXPR, ARRAY,
> * and MULTIEXPR; for those, return NULL.
> */
> if (subLinkType == EXPR_SUBLINK ||
> - subLinkType == ARRAY_SUBLINK ||
> subLinkType == MULTIEXPR_SUBLINK)
> {
> result = (Datum) 0;
> *isNull = true;
> }
> }
> - else if (subLinkType == ARRAY_SUBLINK)
> - {
> - Assert(astate != NULL);
> - /* We return the result in the caller's context */
> - result = makeArrayResult(astate, oldcontext);
> - }
>
> MemoryContextSwitchTo(oldcontext);
>
> --- 433,459 ----
> }
> }
>
> ! if (subLinkType == ARRAY_SUBLINK)
> ! {
> ! if (!astate)
> ! astate = initArrayResult(element_type, oldcontext);
> ! /* We return the result in the caller's context */
> ! result = makeArrayResult(astate, oldcontext);
> ! }
> ! else if (!found)
> {
> /*
> * deal with empty subplan result. result/isNull were previously
> ! * initialized correctly for all sublink types except EXPR
> * and MULTIEXPR; for those, return NULL.
> */
> if (subLinkType == EXPR_SUBLINK ||
> subLinkType == MULTIEXPR_SUBLINK)
> {
> result = (Datum) 0;
> *isNull = true;
> }
> }
>
> MemoryContextSwitchTo(oldcontext);
>
> ***************
> *** 925,930 ****
> --- 926,932 ----
> ListCell *l;
> bool found = false;
> ArrayBuildState *astate = NULL;
> + Oid element_type = planstate->ps_ResultTupleSlot->tts_tupleDescriptor->attrs[0]->atttypid;
>
> /*
> * Must switch to child query's per-query memory context.
> ***************
> *** 1010,1016 ****
> }
> }
>
> ! if (!found)
> {
> if (subLinkType == EXISTS_SUBLINK)
> {
> --- 1012,1033 ----
> }
> }
>
> ! if (subLinkType == ARRAY_SUBLINK)
> ! {
> ! /* There can be only one param... */
> ! int paramid = linitial_int(subplan->setParam);
> ! ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
> !
> ! prm->execPlan = NULL;
> !
> ! if (!astate)
> ! astate = initArrayResult(element_type, oldcontext);
> !
> ! /* We build the result in query context so it won't disappear */
> ! prm->value = makeArrayResult(astate, econtext->ecxt_per_query_memory);
> ! prm->isnull = false;
> ! }
> ! else if (!found)
> {
> if (subLinkType == EXISTS_SUBLINK)
> {
> ***************
> *** 1035,1052 ****
> }
> }
> }
> - else if (subLinkType == ARRAY_SUBLINK)
> - {
> - /* There can be only one param... */
> - int paramid = linitial_int(subplan->setParam);
> - ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
> -
> - Assert(astate != NULL);
> - prm->execPlan = NULL;
> - /* We build the result in query context so it won't disappear */
> - prm->value = makeArrayResult(astate, econtext->ecxt_per_query_memory);
> - prm->isnull = false;
> - }
>
> MemoryContextSwitchTo(oldcontext);
> }
> --- 1052,1057 ----
> Index: src/backend/utils/adt/arrayfuncs.c
> ===================================================================
> RCS file: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v
> retrieving revision 1.120
> diff -c -r1.120 arrayfuncs.c
> *** src/backend/utils/adt/arrayfuncs.c 1 May 2005 18:56:18 -0000 1.120
> --- src/backend/utils/adt/arrayfuncs.c 26 May 2005 18:52:16 -0000
> ***************
> *** 3252,3257 ****
> --- 3252,3293 ----
> &my_extra->amstate);
> }
>
> +
> + /*
> + * initArrayResult - initialize an ArrayBuildState for an array result
> + *
> + * rcontext is where to keep working state
> + */
> + ArrayBuildState *
> + initArrayResult(Oid element_type, MemoryContext rcontext)
> + {
> + ArrayBuildState *astate;
> + MemoryContext arr_context,
> + oldcontext;
> +
> + /* Make a temporary context to hold all the junk */
> + arr_context = AllocSetContextCreate(rcontext,
> + "accumArrayResult",
> + ALLOCSET_DEFAULT_MINSIZE,
> + ALLOCSET_DEFAULT_INITSIZE,
> + ALLOCSET_DEFAULT_MAXSIZE);
> + oldcontext = MemoryContextSwitchTo(arr_context);
> + astate = (ArrayBuildState *) palloc(sizeof(ArrayBuildState));
> + astate->mcontext = arr_context;
> + astate->dvalues = (Datum *)
> + palloc(ARRAY_ELEMS_CHUNKSIZE * sizeof(Datum));
> + astate->nelems = 0;
> + astate->element_type = element_type;
> + get_typlenbyvalalign(element_type,
> + &astate->typlen,
> + &astate->typbyval,
> + &astate->typalign);
> +
> + MemoryContextSwitchTo(oldcontext);
> +
> + return astate;
> + }
> +
> /*
> * accumArrayResult - accumulate one (more) Datum for an array result
> *
> ***************
> *** 3264,3293 ****
> Oid element_type,
> MemoryContext rcontext)
> {
> ! MemoryContext arr_context,
> ! oldcontext;
>
> if (astate == NULL)
> {
> /* First time through --- initialize */
> !
> ! /* Make a temporary context to hold all the junk */
> ! arr_context = AllocSetContextCreate(rcontext,
> ! "accumArrayResult",
> ! ALLOCSET_DEFAULT_MINSIZE,
> ! ALLOCSET_DEFAULT_INITSIZE,
> ! ALLOCSET_DEFAULT_MAXSIZE);
> ! oldcontext = MemoryContextSwitchTo(arr_context);
> ! astate = (ArrayBuildState *) palloc(sizeof(ArrayBuildState));
> ! astate->mcontext = arr_context;
> ! astate->dvalues = (Datum *)
> ! palloc(ARRAY_ELEMS_CHUNKSIZE * sizeof(Datum));
> ! astate->nelems = 0;
> ! astate->element_type = element_type;
> ! get_typlenbyvalalign(element_type,
> ! &astate->typlen,
> ! &astate->typbyval,
> ! &astate->typalign);
> }
> else
> {
> --- 3300,3311 ----
> Oid element_type,
> MemoryContext rcontext)
> {
> ! MemoryContext oldcontext;
>
> if (astate == NULL)
> {
> /* First time through --- initialize */
> ! astate = initArrayResult(element_type, rcontext);
> }
> else
> {
> Index: src/include/utils/array.h
> ===================================================================
> RCS file: /cvsroot/pgsql/src/include/utils/array.h,v
> retrieving revision 1.54
> diff -c -r1.54 array.h
> *** src/include/utils/array.h 29 Mar 2005 00:17:18 -0000 1.54
> --- src/include/utils/array.h 26 May 2005 18:52:16 -0000
> ***************
> *** 176,181 ****
> --- 176,182 ----
> Oid elmtype,
> int elmlen, bool elmbyval, char elmalign,
> Datum **elemsp, int *nelemsp);
> + extern ArrayBuildState *initArrayResult(Oid element_type, MemoryContext rcontext);
> extern ArrayBuildState *accumArrayResult(ArrayBuildState *astate,
> Datum dvalue, bool disnull,
> Oid element_type,
>
>
> ------------------------------------------------------------------------
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: "Patches (PostgreSQL)" <pgsql-patches(at)postgresql(dot)org>, Markus Bertheau <twanger(at)bluetwanger(dot)de>
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-31 21:58:03
Message-ID: 7686.1117576683@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Joe Conway <mail(at)joeconway(dot)com> writes:
>> + Oid element_type = planstate->ps_ResultTupleSlot->tts_tupleDescriptor->attrs[0]->atttypid;

Hmm, that makes me itch ... it seems like unwarranted familiarity with
the innards of the subplan; not only as to where it keeps things, but
when things have been initialized. Perhaps we have no choice, but isn't
the datatype available on the current plan level?

regards, tom lane


From: Joe Conway <mail(at)joeconway(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Patches (PostgreSQL)" <pgsql-patches(at)postgresql(dot)org>, Markus Bertheau <twanger(at)bluetwanger(dot)de>
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp.
Date: 2005-05-31 22:02:00
Message-ID: 429CDED8.8010704@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Tom Lane wrote:
> Joe Conway <mail(at)joeconway(dot)com> writes:
>
>>>+ Oid element_type = planstate->ps_ResultTupleSlot->tts_tupleDescriptor->attrs[0]->atttypid;
>
>
> Hmm, that makes me itch ... it seems like unwarranted familiarity with
> the innards of the subplan; not only as to where it keeps things, but
> when things have been initialized. Perhaps we have no choice, but isn't
> the datatype available on the current plan level?
>

I poked around a bit, and that was the best I could come up with. I'll
take another look.

Joe


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Markus Bertheau <twanger(at)bluetwanger(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-06-04 17:52:16
Message-ID: 200506041752.j54HqGZ12891@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Joe Conway wrote:
> Tom Lane wrote:
> > I think he's got a good point, actually. We document the ARRAY-with-
> > parens-around-a-SELECT syntax as
> >
> > The resulting one-dimensional array will have an element for
> > each row in the subquery result, with an element type matching
> > that of the subquery's output column.
> >
> > To me, that implies that a subquery result of no rows generates a
> > one-dimensional array of no elements, not a null array.
>
> OK, looks like I'm outnumbered.
>
> But as far as I know, we have never had a way to produce a
> one-dimensional empty array. Empty arrays thus far have been dimensionless.
>
> Assuming we really want an empty 1D array, I created the attached patch.
> This works fine, but now leaves a few oddities to be dealt with, e.g.:
>
> regression=# select array_dims(array(select 1 where false));
> array_dims
> ------------
> [1:0]
> (1 row)
>
> Any thoughts on how this should be handled for an empty 1D array?
>
> > The point Markus is complaining about seems like it should
> > be easily fixable.
>
> Well, "easily" is a relative term. My Postgres hacking neurons have
> gotten kind of rusty lately -- but then maybe that was your underlying
> point ;-)

No one responed to this email, so I will try. Is this the one
dimmentional array you were talking about?

test=> select array_dims('{}'::integer[]);
array_dims
------------

(1 row)

Why is [1:0] wrong to return?

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Joe Conway <mail(at)joeconway(dot)com>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Markus Bertheau <twanger(at)bluetwanger(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-06-04 21:01:18
Message-ID: 42A2169E.9020700@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Bruce Momjian wrote:
> Joe Conway wrote:
>>
>>Any thoughts on how this should be handled for an empty 1D array?
>
> No one responed to this email, so I will try. Is this the one
> dimmentional array you were talking about?
>
> test=> select array_dims('{}'::integer[]);
> array_dims
> ------------
>
> (1 row)

In this case, what you get is actually a dimensionless array. Literally,
you get this:

if (nitems == 0)
{
/* Return empty array */
retval = (ArrayType *) palloc0(sizeof(ArrayType));
retval->size = sizeof(ArrayType);
retval->elemtype = element_type;
PG_RETURN_ARRAYTYPE_P(retval);
}

I.e. the array structure is allocated, the size is set (which is
required since arrays are varlena), and the element type is initialized.
There is no initialization of ndim, ARR_DIMS(), or ARR_LBOUND().

In this case, since there are no dimensions, array_dims() probably does
the right thing by returning NULL.

> Why is [1:0] wrong to return?
>

I'm not sure it is wrong -- it just seems a bit strange. The difference
is that in order to return an empty *one-dimensional* array, ndim,
ARR_DIMS(), and ARR_LBOUND() are all appropriately set (by the patched
code). Basically, ndim == 1, ARR_DIMS() is a single element int array (C
array that is) indicating 0 elements for dimension 1, and ARR_LBOUND()
is a single element int array indicating a lower bound of 1. This leads
to the array_dims() return value of [1:0]. The value 1 is unquestionably
correct for the lower bound index, but what should be reported for the
upper bound? We can't return [1:1], because that would indicate that we
have one element.

Joe


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Markus Bertheau <twanger(at)bluetwanger(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-06-04 22:52:31
Message-ID: 200506042252.j54MqVu14936@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Joe Conway wrote:
> Bruce Momjian wrote:
> > Joe Conway wrote:
> >>
> >>Any thoughts on how this should be handled for an empty 1D array?
> >
> > No one responed to this email, so I will try. Is this the one
> > dimmentional array you were talking about?
> >
> > test=> select array_dims('{}'::integer[]);
> > array_dims
> > ------------
> >
> > (1 row)
>
> In this case, what you get is actually a dimensionless array. Literally,
> you get this:
>
> if (nitems == 0)
> {
> /* Return empty array */
> retval = (ArrayType *) palloc0(sizeof(ArrayType));
> retval->size = sizeof(ArrayType);
> retval->elemtype = element_type;
> PG_RETURN_ARRAYTYPE_P(retval);
> }
>
> I.e. the array structure is allocated, the size is set (which is
> required since arrays are varlena), and the element type is initialized.
> There is no initialization of ndim, ARR_DIMS(), or ARR_LBOUND().
>
> In this case, since there are no dimensions, array_dims() probably does
> the right thing by returning NULL.
>
> > Why is [1:0] wrong to return?
> >
>
> I'm not sure it is wrong -- it just seems a bit strange. The difference
> is that in order to return an empty *one-dimensional* array, ndim,
> ARR_DIMS(), and ARR_LBOUND() are all appropriately set (by the patched
> code). Basically, ndim == 1, ARR_DIMS() is a single element int array (C
> array that is) indicating 0 elements for dimension 1, and ARR_LBOUND()
> is a single element int array indicating a lower bound of 1. This leads
> to the array_dims() return value of [1:0]. The value 1 is unquestionably
> correct for the lower bound index, but what should be reported for the
> upper bound? We can't return [1:1], because that would indicate that we
> have one element.

OK, so '[1:0]' seems correct. How would to specify such an array
manually in a string?

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Joe Conway <mail(at)joeconway(dot)com>, pgsql-sql(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-06-06 14:17:24
Message-ID: 1118067445.5506.6.camel@dicaprio.akademie1.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

В Втр, 24/05/2005 в 00:06 -0400, Tom Lane пишет:
> Joe Conway <mail(at)joeconway(dot)com> writes:
> > Markus Bertheau wrote:
> >> why does SELECT ARRAY(SELECT 1 WHERE FALSE) return NULL instead of
> >> ARRAY[] resp. '{}'?
>
> > Why would you expect an empty array instead of a NULL?
>
> I think he's got a good point, actually. We document the ARRAY-with-
> parens-around-a-SELECT syntax as
>
> The resulting one-dimensional array will have an element for
> each row in the subquery result, with an element type matching
> that of the subquery's output column.
>
> To me, that implies that a subquery result of no rows generates a
> one-dimensional array of no elements, not a null array.

By analogy, array_upper('{}'::TEXT[], 1) should return 0 instead of
NULL.

Markus

--
Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>
Cc: Joe Conway <mail(at)joeconway(dot)com>, pgsql-sql(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-06-06 14:44:49
Message-ID: 25974.1118069089@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Markus Bertheau =?UTF-8?Q?=E2=98=AD?= <twanger(at)bluetwanger(dot)de> writes:
> By analogy, array_upper('{}'::TEXT[], 1) should return 0 instead of
> NULL.

No, that doesn't follow ... we've traditionally considered '{}' to
denote a zero-dimensional array. A 1-D array of no elements is
'[1:0]={}', just as Joe shows ... or at least it would be except
for an overenthusiastic error check:

regression=# select '[1:0]={}' :: int[];
ERROR: upper bound cannot be less than lower bound

I think this should be a legal boundary case. In general, it should be
possible to form zero-size arrays of any number of dimensions.

regards, tom lane


From: Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Joe Conway <mail(at)joeconway(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-06-06 14:54:59
Message-ID: 1118069699.5506.9.camel@dicaprio.akademie1.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

В Пнд, 06/06/2005 в 10:44 -0400, Tom Lane пишет:
> Markus Bertheau =?UTF-8?Q?=E2=98=AD?= <twanger(at)bluetwanger(dot)de> writes:
> > By analogy, array_upper('{}'::TEXT[], 1) should return 0 instead of
> > NULL.
>
> No, that doesn't follow ... we've traditionally considered '{}' to
> denote a zero-dimensional array.

But array_upper(ARRAY(SELECT 1 WHERE FALSE), 1) should return 0,
and array_upper(ARRAY[], 1), when the ARRAY[] syntax is supported,
should return 0.

Do I get that right?

Markus

--
Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>


From: Joe Conway <mail(at)joeconway(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>, pgsql-sql(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-06-06 15:39:48
Message-ID: 42A46E44.3060100@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Tom Lane wrote:
> Markus Bertheau =?UTF-8?Q?=E2=98=AD?= <twanger(at)bluetwanger(dot)de> writes:
>
>>By analogy, array_upper('{}'::TEXT[], 1) should return 0 instead of
>>NULL.
>
> No, that doesn't follow ... we've traditionally considered '{}' to
> denote a zero-dimensional array. A 1-D array of no elements is
> '[1:0]={}', just as Joe shows ... or at least it would be except
> for an overenthusiastic error check:
>
> regression=# select '[1:0]={}' :: int[];
> ERROR: upper bound cannot be less than lower bound
>
> I think this should be a legal boundary case. In general, it should be
> possible to form zero-size arrays of any number of dimensions.
>

I've been playing with exactly this over the weekend. Of course, as
usual, the devil is in the details. For instance, using the above
notation, how would I specify a zero-element 1D array starting at a
lower bound index of 0? The answer following the above pattern would be:

select '[0:-1]={}'::int[];

You could not use '[0:0]={}'::int[], because that would be a one-element
array. I propose the following instead:

regression=# select '[1:]={}' :: int[];
int4
------
{}
(1 row)

regression=# select array_dims('[1:]={}' :: int[]);
array_dims
------------
[1:]
(1 row)

In other words, an missing upper bound indicates zero elements.

Now the next question; what does a 2D zero-element array look like? I
think this makes sense:

regression=# select '[1:2][1:]={{},{}}'::int[];
int4
------
{}
(1 row)

Except (I think) array_out() should probably output something closer to
the input literal. Any thoughts on this?

Joe


From: Joe Conway <mail(at)joeconway(dot)com>
To: Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-06-06 15:50:13
Message-ID: 42A470B5.8060007@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Markus Bertheau ☭ wrote:
> В Пнд, 06/06/2005 в 10:44 -0400, Tom Lane пишет:
>>Markus Bertheau =?UTF-8?Q?=E2=98=AD?= <twanger(at)bluetwanger(dot)de> writes:
>>
>>>By analogy, array_upper('{}'::TEXT[], 1) should return 0 instead of
>>>NULL.
>>
>>No, that doesn't follow ... we've traditionally considered '{}' to
>>denote a zero-dimensional array.
>
> But array_upper(ARRAY(SELECT 1 WHERE FALSE), 1) should return 0,
> and array_upper(ARRAY[], 1), when the ARRAY[] syntax is supported,
> should return 0.
>

Actually, consistent with my last post, I think array_upper() on a
zero-element array should return NULL. A zero-element array has a
defined lower bound, but its upper bound is not zero -- it is really
undefined.

Joe


From: Joe Conway <mail(at)joeconway(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp.
Date: 2005-06-06 15:58:28
Message-ID: 42A472A4.6090505@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Joe Conway wrote:
> Actually, consistent with my last post, I think array_upper() on a
> zero-element array should return NULL. A zero-element array has a
> defined lower bound, but its upper bound is not zero -- it is really
> undefined.

Just to clarify my response, this is what I propose:

regression=# select array_upper('[2][1:]={{},{}}'::int[],1);
array_upper
-------------
2
(1 row)

regression=# select array_upper('[2][1:]={{},{}}'::int[],2) IS NULL;
?column?
----------
t
(1 row)

Joe


From: Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[]
Date: 2005-06-06 16:12:46
Message-ID: 1118074367.5506.16.camel@dicaprio.akademie1.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

В Пнд, 06/06/2005 в 08:58 -0700, Joe Conway пишет:
> Joe Conway wrote:
> > Actually, consistent with my last post, I think array_upper() on a
> > zero-element array should return NULL. A zero-element array has a
> > defined lower bound, but its upper bound is not zero -- it is really
> > undefined.
>
> Just to clarify my response, this is what I propose:
>
> regression=# select array_upper('[2][1:]={{},{}}'::int[],1);
> array_upper
> -------------
> 2
> (1 row)
>
> regression=# select array_upper('[2][1:]={{},{}}'::int[],2) IS NULL;
> ?column?
> ----------
> t
> (1 row)

Hmm, this gets really complicated and inconsistent. Complicated means
unusable. What about modifying the dimension syntax such that the second
number means number of elements instead of upper bound? That particular
problem would go away then, and array_upper('[0:0]={}'::int[]) can
return the correct 0 then.

What I'm actually worrying about is that array_upper(array(select 1
where false)) returns 0.

An option would be to drop the possibility to let the array start at
another index than 0. I don't know why it was decided to do that in the
first place. It seems a rather odd feature to me.

Markus
--
Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>


From: Joe Conway <mail(at)joeconway(dot)com>
To: Markus Bertheau ☭ <twanger(at)bluetwanger(dot)de>
Cc: pgsql-hackers(at)postgresql(dot)org, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp.
Date: 2005-06-07 01:24:52
Message-ID: 42A4F764.9010705@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Markus Bertheau ☭ wrote:
> Hmm, this gets really complicated and inconsistent. Complicated means
> unusable. What about modifying the dimension syntax such that the second
> number means number of elements instead of upper bound? That particular
> problem would go away then, and array_upper('[0:0]={}'::int[]) can
> return the correct 0 then.
>
> What I'm actually worrying about is that array_upper(array(select 1
> where false)) returns 0.
>
> An option would be to drop the possibility to let the array start at
> another index than 0. I don't know why it was decided to do that in the
> first place. It seems a rather odd feature to me.
>

Actually I like both of these ideas, and have advocated the second one
myself before. But it isn't backward compatible -- anyone else have an
opinion? SQL2003 actually specifies that an array *should* start at 1:

4.10.2 Arrays
An array is a collection A in which each element is associated with
exactly one ordinal position in A. If n is the cardinality of A, then
the ordinal position p of an element is an integer in the range 1 (one)
≤ p ≤ n.

Joe


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: "Markus Bertheau ?" <twanger(at)bluetwanger(dot)de>
Cc: Joe Conway <mail(at)joeconway(dot)com>, pgsql-hackers(at)postgresql(dot)org, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[]
Date: 2005-06-25 01:50:23
Message-ID: 200506250150.j5P1oNO01032@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql


Is this a TODO item?

---------------------------------------------------------------------------

Markus Bertheau ? wrote:
> ? ???, 06/06/2005 ? 08:58 -0700, Joe Conway ?????:
> > Joe Conway wrote:
> > > Actually, consistent with my last post, I think array_upper() on a
> > > zero-element array should return NULL. A zero-element array has a
> > > defined lower bound, but its upper bound is not zero -- it is really
> > > undefined.
> >
> > Just to clarify my response, this is what I propose:
> >
> > regression=# select array_upper('[2][1:]={{},{}}'::int[],1);
> > array_upper
> > -------------
> > 2
> > (1 row)
> >
> > regression=# select array_upper('[2][1:]={{},{}}'::int[],2) IS NULL;
> > ?column?
> > ----------
> > t
> > (1 row)
>
> Hmm, this gets really complicated and inconsistent. Complicated means
> unusable. What about modifying the dimension syntax such that the second
> number means number of elements instead of upper bound? That particular
> problem would go away then, and array_upper('[0:0]={}'::int[]) can
> return the correct 0 then.
>
> What I'm actually worrying about is that array_upper(array(select 1
> where false)) returns 0.
>
> An option would be to drop the possibility to let the array start at
> another index than 0. I don't know why it was decided to do that in the
> first place. It seems a rather odd feature to me.
>
> Markus
> --
> Markus Bertheau ? <twanger(at)bluetwanger(dot)de>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Joe Conway <mail(at)joeconway(dot)com>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Markus Bertheau ? <twanger(at)bluetwanger(dot)de>, pgsql-hackers(at)postgresql(dot)org, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[]
Date: 2005-06-27 04:08:15
Message-ID: 42BF7BAF.2080401@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql

Bruce Momjian wrote:
> Is this a TODO item?
>

Probably. I posted some questions regarding whether or not to break
backward compatiblity, and received no replies. In the meanwhile, I've
been doing a major system integration in Korea for the last 2 weeks, and
won't get back to home, or to anything like a reasonably normal schedule
until after July 2. I doubt I'll have time to do much between now and
feature freeze.

Joe


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: "Markus Bertheau ?" <twanger(at)bluetwanger(dot)de>, pgsql-hackers(at)postgresql(dot)org, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: [SQL] ARRAY() returning NULL instead of ARRAY[]
Date: 2005-06-27 09:39:17
Message-ID: 200506270939.j5R9dH011936@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches pgsql-sql


OK, what is the TODO item text?

---------------------------------------------------------------------------

Joe Conway wrote:
> Bruce Momjian wrote:
> > Is this a TODO item?
> >
>
> Probably. I posted some questions regarding whether or not to break
> backward compatiblity, and received no replies. In the meanwhile, I've
> been doing a major system integration in Korea for the last 2 weeks, and
> won't get back to home, or to anything like a reasonably normal schedule
> until after July 2. I doubt I'll have time to do much between now and
> feature freeze.
>
> Joe
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org
>

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073