Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1e9a1a7

Browse files
committed
Change array_push and array_cat so that they retain the lower bound of
the array (for array_push) or higher-dimensional array (for array_cat) rather than decrementing it as before. This avoids generating lower bounds other than one for any array operation within the SQL spec. Per recent discussion. Interestingly, this seems to have been the original behavior, because while updating the docs I noticed that a large fraction of relevant examples were *wrong* for the old behavior and are now right. Is it worth correcting this in the back-branch docs?
1 parent 8685c47 commit 1e9a1a7

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

doc/src/sgml/array.sgml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/array.sgml,v 1.47 2005/11/17 22:14:50 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/array.sgml,v 1.48 2005/11/19 01:50:08 tgl Exp $ -->
22

33
<sect1 id="arrays">
44
<title>Arrays</title>
@@ -391,13 +391,11 @@ SELECT ARRAY[5,6] || ARRAY[[1,2],[3,4]];
391391
</para>
392392

393393
<para>
394-
When a single element is pushed on to the beginning of a one-dimensional
395-
array, the result is an array with a lower bound subscript equal to
396-
the right-hand operand's lower bound subscript, minus one. When a single
397-
element is pushed on to the end of a one-dimensional array, the result is
398-
an array retaining the lower bound of the left-hand operand. For example:
394+
When a single element is pushed on to either the beginning or end of a
395+
one-dimensional array, the result is an array with the same lower bound
396+
subscript as the array operand. For example:
399397
<programlisting>
400-
SELECT array_dims(1 || ARRAY[2,3]);
398+
SELECT array_dims(1 || '[0:1]={2,3}'::int[]);
401399
array_dims
402400
------------
403401
[0:2]
@@ -441,7 +439,7 @@ SELECT array_dims(ARRAY[[1,2],[3,4]] || ARRAY[[5,6],[7,8],[9,0]]);
441439
SELECT array_dims(ARRAY[1,2] || ARRAY[[3,4],[5,6]]);
442440
array_dims
443441
------------
444-
[0:2][1:2]
442+
[1:3][1:2]
445443
(1 row)
446444
</programlisting>
447445
</para>

doc/src/sgml/func.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.293 2005/11/17 22:14:50 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.294 2005/11/19 01:50:08 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -7484,7 +7484,7 @@ SELECT NULLIF(value, '(none)') ...
74847484
</entry>
74857485
<entry><type>int</type></entry>
74867486
<entry>returns lower bound of the requested array dimension</entry>
7487-
<entry><literal>array_lower(array_prepend(0, ARRAY[1,2,3]), 1)</literal></entry>
7487+
<entry><literal>array_lower('[0:2]={1,2,3}'::int[], 1)</literal></entry>
74887488
<entry><literal>0</literal></entry>
74897489
</row>
74907490
<row>

src/backend/utils/adt/array_userfuncs.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 2003-2005, PostgreSQL Global Development Group
77
*
88
* IDENTIFICATION
9-
* $PostgreSQL: pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.17 2005/11/17 22:14:52 tgl Exp $
9+
* $PostgreSQL: pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.18 2005/11/19 01:50:08 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -152,6 +152,13 @@ array_push(PG_FUNCTION_ARGS)
152152
result = array_set(v, 1, &indx, newelem, isNull,
153153
-1, typlen, typbyval, typalign);
154154

155+
/*
156+
* Readjust result's LB to match the input's. This does nothing in the
157+
* append case, but it's the simplest way to implement the prepend case.
158+
*/
159+
if (ARR_NDIM(v) == 1)
160+
ARR_LBOUND(result)[0] = ARR_LBOUND(v)[0];
161+
155162
PG_RETURN_ARRAYTYPE_P(result);
156163
}
157164

@@ -305,7 +312,7 @@ array_cat(PG_FUNCTION_ARGS)
305312
{
306313
/*
307314
* resulting array has the second argument as the outer array, with
308-
* the first argument appended to the front of the outer dimension
315+
* the first argument inserted at the front of the outer dimension
309316
*/
310317
ndims = ndims2;
311318
dims = (int *) palloc(ndims * sizeof(int));
@@ -316,9 +323,6 @@ array_cat(PG_FUNCTION_ARGS)
316323
/* increment number of elements in outer array */
317324
dims[0] += 1;
318325

319-
/* decrement outer array lower bound */
320-
lbs[0] -= 1;
321-
322326
/* make sure the added element matches our existing elements */
323327
for (i = 0; i < ndims1; i++)
324328
{

src/test/regress/expected/arrays.out

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ SELECT array_append(array[42], 6) AS "{42,6}";
224224
(1 row)
225225

226226
SELECT array_prepend(6, array[42]) AS "{6,42}";
227-
{6,42}
228-
--------------
229-
[0:1]={6,42}
227+
{6,42}
228+
--------
229+
{6,42}
230230
(1 row)
231231

232232
SELECT array_cat(ARRAY[1,2], ARRAY[3,4]) AS "{1,2,3,4}";
@@ -236,9 +236,9 @@ SELECT array_cat(ARRAY[1,2], ARRAY[3,4]) AS "{1,2,3,4}";
236236
(1 row)
237237

238238
SELECT array_cat(ARRAY[1,2], ARRAY[[3,4],[5,6]]) AS "{{1,2},{3,4},{5,6}}";
239-
{{1,2},{3,4},{5,6}}
240-
--------------------------------
241-
[0:2][1:2]={{1,2},{3,4},{5,6}}
239+
{{1,2},{3,4},{5,6}}
240+
---------------------
241+
{{1,2},{3,4},{5,6}}
242242
(1 row)
243243

244244
SELECT array_cat(ARRAY[[3,4],[5,6]], ARRAY[1,2]) AS "{{3,4},{5,6},{1,2}}";
@@ -267,9 +267,9 @@ SELECT ARRAY[1,2] || 3 AS "{1,2,3}";
267267
(1 row)
268268

269269
SELECT 0 || ARRAY[1,2] AS "{0,1,2}";
270-
{0,1,2}
271-
---------------
272-
[0:2]={0,1,2}
270+
{0,1,2}
271+
---------
272+
{0,1,2}
273273
(1 row)
274274

275275
SELECT ARRAY[1,2] || ARRAY[3,4] AS "{1,2,3,4}";
@@ -297,9 +297,9 @@ SELECT ARRAY[0,0] || ARRAY[1,1] || ARRAY[2,2] AS "{0,0,1,1,2,2}";
297297
(1 row)
298298

299299
SELECT 0 || ARRAY[1,2] || 3 AS "{0,1,2,3}";
300-
{0,1,2,3}
301-
-----------------
302-
[0:3]={0,1,2,3}
300+
{0,1,2,3}
301+
-----------
302+
{0,1,2,3}
303303
(1 row)
304304

305305
-- array casts

0 commit comments

Comments
 (0)