From 356e4dbf9afbcdd61a35581c5bb43404d75fc305 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Thu, 3 Jun 2010 02:06:10 +0000 Subject: [PATCH] Clarify array generate_subscripts() documentation example. Tim Landscheidt --- doc/src/sgml/func.sgml | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index e1c0bf32d9..30b3419937 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,4 +1,4 @@ - + Functions and Operators @@ -11419,7 +11419,7 @@ SELECT * FROM generate_series('2008-03-01 00:00'::timestamp, elements). Some examples follow: -- basic usage -select generate_subscripts('{NULL,1,NULL,2}'::int[], 1) as s; +SELECT generate_subscripts('{NULL,1,NULL,2}'::int[], 1) AS s; s --- 1 @@ -11430,32 +11430,33 @@ select generate_subscripts('{NULL,1,NULL,2}'::int[], 1) as s; -- presenting an array, the subscript and the subscripted -- value requires a subquery -select * from arrays; +SELECT * FROM arrays; a -------------------- {-1,-2} - {100,200} + {100,200,300} (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) +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,300} | 1 | 100 + {100,200,300} | 2 | 200 + {100,200,300} | 3 | 300 +(5 rows) -- unnest a 2D array -create or replace function unnest2(anyarray) -returns setof anyelement as $$ +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; +$$ LANGUAGE sql IMMUTABLE; CREATE FUNCTION -postgres=# select * from unnest2(array[[1,2],[3,4]]); +postgres=# SELECT * FROM unnest2(ARRAY[[1,2],[3,4]]); unnest2 --------- 1 -- 2.40.0