From fbdf9712afbbee4c213905f28019114e79b4e139 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 24 Feb 2010 18:02:36 +0000 Subject: [PATCH] Allow zero-dimensional (ie, empty) arrays in contrib/ltree operations. The main motivation for changing this is bug #4921, in which it's pointed out that it's no longer safe to apply ltree operations to the result of ARRAY(SELECT ...) if the sub-select might return no rows. Before 8.3, the ARRAY() construct would return NULL, which might or might not be helpful but at least it wouldn't result in an error. Now it returns an empty array which results in a failure for no good reason, since the ltree operations are all perfectly capable of dealing with zero-element arrays. As far as I can find, these ltree functions are the only places where zero array dimensionality is rejected unnecessarily. Back-patch to 8.3 to prevent behavioral regression of queries that worked in older releases. --- contrib/ltree/_ltree_gist.c | 4 ++-- contrib/ltree/_ltree_op.c | 6 +++--- contrib/ltree/lquery_op.c | 4 ++-- contrib/ltree/ltree_gist.c | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/contrib/ltree/_ltree_gist.c b/contrib/ltree/_ltree_gist.c index 34ec2552ca4..3a0e7769fff 100644 --- a/contrib/ltree/_ltree_gist.c +++ b/contrib/ltree/_ltree_gist.c @@ -84,7 +84,7 @@ _ltree_compress(PG_FUNCTION_ARGS) int num = ArrayGetNItems(ARR_NDIM(val), ARR_DIMS(val)); ltree *item = (ltree *) ARR_DATA_PTR(val); - if (ARR_NDIM(val) != 1) + if (ARR_NDIM(val) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must be one-dimensional"))); @@ -530,7 +530,7 @@ _arrq_cons(ltree_gist * key, ArrayType *_query) lquery *query = (lquery *) ARR_DATA_PTR(_query); int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query)); - if (ARR_NDIM(_query) != 1) + if (ARR_NDIM(_query) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must be one-dimensional"))); diff --git a/contrib/ltree/_ltree_op.c b/contrib/ltree/_ltree_op.c index f93d2bb8889..de5cb90a51e 100644 --- a/contrib/ltree/_ltree_op.c +++ b/contrib/ltree/_ltree_op.c @@ -43,7 +43,7 @@ array_iterator(ArrayType *la, PGCALL2 callback, void *param, ltree ** found) int num = ArrayGetNItems(ARR_NDIM(la), ARR_DIMS(la)); ltree *item = (ltree *) ARR_DATA_PTR(la); - if (ARR_NDIM(la) != 1) + if (ARR_NDIM(la) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must be one-dimensional"))); @@ -143,7 +143,7 @@ _lt_q_regex(PG_FUNCTION_ARGS) bool res = false; int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query)); - if (ARR_NDIM(_query) != 1) + if (ARR_NDIM(_query) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must be one-dimensional"))); @@ -301,7 +301,7 @@ _lca(PG_FUNCTION_ARGS) ltree **a, *res; - if (ARR_NDIM(la) != 1) + if (ARR_NDIM(la) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must be one-dimensional"))); diff --git a/contrib/ltree/lquery_op.c b/contrib/ltree/lquery_op.c index 8522c5e0538..c0f4309b78b 100644 --- a/contrib/ltree/lquery_op.c +++ b/contrib/ltree/lquery_op.c @@ -1,7 +1,7 @@ /* * op function for ltree and lquery * Teodor Sigaev - * $PostgreSQL: pgsql/contrib/ltree/lquery_op.c,v 1.11 2006/10/04 00:29:45 momjian Exp $ + * $PostgreSQL: pgsql/contrib/ltree/lquery_op.c,v 1.11.4.1 2010/02/24 18:02:36 tgl Exp $ */ #include "ltree.h" @@ -325,7 +325,7 @@ lt_q_regex(PG_FUNCTION_ARGS) bool res = false; int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query)); - if (ARR_NDIM(_query) != 1) + if (ARR_NDIM(_query) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must be one-dimensional"))); diff --git a/contrib/ltree/ltree_gist.c b/contrib/ltree/ltree_gist.c index dea0b9c3863..921643d2129 100644 --- a/contrib/ltree/ltree_gist.c +++ b/contrib/ltree/ltree_gist.c @@ -1,7 +1,7 @@ /* * GiST support for ltree * Teodor Sigaev - * $PostgreSQL: pgsql/contrib/ltree/ltree_gist.c,v 1.22 2007/11/16 01:12:24 momjian Exp $ + * $PostgreSQL: pgsql/contrib/ltree/ltree_gist.c,v 1.22.2.1 2010/02/24 18:02:36 tgl Exp $ */ #include "ltree.h" @@ -601,7 +601,7 @@ arrq_cons(ltree_gist * key, ArrayType *_query) lquery *query = (lquery *) ARR_DATA_PTR(_query); int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query)); - if (ARR_NDIM(_query) != 1) + if (ARR_NDIM(_query) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must be one-dimensional")));