From 1fcb977a13d70f8746ec86488fd9afc0569e7784 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Mon, 28 Apr 2008 14:48:58 +0000 Subject: Add generate_subscripts, a series-generation function which generates an array's subscripts. Pavel Stehule, some editorialization by me. --- doc/src/sgml/array.sgml | 17 ++++++++- doc/src/sgml/func.sgml | 98 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 112 insertions(+), 3 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/array.sgml b/doc/src/sgml/array.sgml index 87df7e556a2..b0d6e19abf7 100644 --- a/doc/src/sgml/array.sgml +++ b/doc/src/sgml/array.sgml @@ -1,4 +1,4 @@ - + Arrays @@ -542,6 +542,21 @@ SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter); + + Alternatively, the generate_subscripts function can be used. + For example: + + +SELECT * FROM + (SELECT pay_by_quarter, + generate_subscripts(pay_by_quarter, 1) AS s + FROM sal_emp) AS foo + WHERE pay_by_quarter[s] = 10000; + + + This function is described in . + + Arrays are not sets; searching for specific array elements diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 7e120bc8621..d0171798675 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,4 +1,4 @@ - + Functions and Operators @@ -10613,7 +10613,8 @@ AND This section describes functions that possibly return more than one row. Currently the only functions in this class are series generating functions, - as detailed in . + as detailed in and + . @@ -10691,6 +10692,99 @@ select current_date + s.a as dates from generate_series(0,14,7) as s(a); (3 rows) + +
+ + + generate_subscripts + + + Subscripts Generating Functions + + + + Function + Return Type + Description + + + + + + generate_subscripts(array annyarray, dim int) + setof int + + Generate a series comprising the given array's subscripts. + + + + + generate_subscripts(array annyarray, dim int, reverse boolean) + setof int + + Generate a series comprising the given array's subscripts. When + reverse is true, the series is returned in + reverse order. + + + + + +
+ + + Zero rows are returned for arrays that do not have the requested dimension, + or for NULL arrays (but valid subscripts are returned for NULL array + elements.) Some examples follow: + +-- basic usage +select generate_subscripts('{NULL,1,NULL,2}'::int[], 1) as s; + s +--- + 1 + 2 + 3 + 4 +(4 rows) + +-- presenting an array, the subscript and the subscripted +-- value requires a subquery +select * from arrays; + a +-------------------- + {-1,-2} + {100,200} +(2 rows) + +select a as array, s as subscript, a[s] as value +from (select generate_subscripts(a, 1) as s, a from arrays) foo; + array | subscript | value +-----------+-----------+------- + {-1,-2} | 1 | -1 + {-1,-2} | 2 | -2 + {100,200} | 1 | 100 + {100,200} | 2 | 200 +(4 rows) + +-- unnest a 2D array +create or replace function unnest2(anyarray) +returns setof anyelement as $$ +select $1[i][j] + from generate_subscripts($1,1) g1(i), + generate_subscripts($1,2) g2(j); +$$ language sql immutable; +CREATE FUNCTION +postgres=# select * from unnest2(array[[1,2],[3,4]]); + unnest2 +--------- + 1 + 2 + 3 + 4 +(4 rows) + + +
-- cgit v1.2.3