|
1 |
| -<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.433 2008/04/17 20:56:41 momjian Exp $ --> |
| 1 | +<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.434 2008/04/28 14:48:57 alvherre Exp $ --> |
2 | 2 |
|
3 | 3 | <chapter id="functions">
|
4 | 4 | <title>Functions and Operators</title>
|
@@ -10613,7 +10613,8 @@ AND
|
10613 | 10613 | <para>
|
10614 | 10614 | This section describes functions that possibly return more than one row.
|
10615 | 10615 | Currently the only functions in this class are series generating functions,
|
10616 |
| - as detailed in <xref linkend="functions-srf-series">. |
| 10616 | + as detailed in <xref linkend="functions-srf-series"> and |
| 10617 | + <xref linkend="functions-srf-subscripts">. |
10617 | 10618 | </para>
|
10618 | 10619 |
|
10619 | 10620 | <table id="functions-srf-series">
|
@@ -10691,6 +10692,99 @@ select current_date + s.a as dates from generate_series(0,14,7) as s(a);
|
10691 | 10692 | (3 rows)
|
10692 | 10693 | </programlisting>
|
10693 | 10694 | </para>
|
| 10695 | + |
| 10696 | + <table id="functions-srf-subscripts"> |
| 10697 | + |
| 10698 | + <indexterm> |
| 10699 | + <primary>generate_subscripts</primary> |
| 10700 | + </indexterm> |
| 10701 | + |
| 10702 | + <title>Subscripts Generating Functions</title> |
| 10703 | + <tgroup cols="3"> |
| 10704 | + <thead> |
| 10705 | + <row> |
| 10706 | + <entry>Function</entry> |
| 10707 | + <entry>Return Type</entry> |
| 10708 | + <entry>Description</entry> |
| 10709 | + </row> |
| 10710 | + </thead> |
| 10711 | + |
| 10712 | + <tbody> |
| 10713 | + <row> |
| 10714 | + <entry><literal><function>generate_subscripts</function>(<parameter>array annyarray</parameter>, <parameter>dim int</parameter>)</literal></entry> |
| 10715 | + <entry><type>setof int</type></entry> |
| 10716 | + <entry> |
| 10717 | + Generate a series comprising the given array's subscripts. |
| 10718 | + </entry> |
| 10719 | + </row> |
| 10720 | + |
| 10721 | + <row> |
| 10722 | + <entry><literal><function>generate_subscripts</function>(<parameter>array annyarray</parameter>, <parameter>dim int</parameter>, <parameter>reverse boolean</parameter>)</literal></entry> |
| 10723 | + <entry><type>setof int</type></entry> |
| 10724 | + <entry> |
| 10725 | + Generate a series comprising the given array's subscripts. When |
| 10726 | + <parameter>reverse</parameter> is true, the series is returned in |
| 10727 | + reverse order. |
| 10728 | + </entry> |
| 10729 | + </row> |
| 10730 | + |
| 10731 | + </tbody> |
| 10732 | + </tgroup> |
| 10733 | + </table> |
| 10734 | + |
| 10735 | + <para> |
| 10736 | + Zero rows are returned for arrays that do not have the requested dimension, |
| 10737 | + or for NULL arrays (but valid subscripts are returned for NULL array |
| 10738 | + elements.) Some examples follow: |
| 10739 | +<programlisting> |
| 10740 | +-- basic usage |
| 10741 | +select generate_subscripts('{NULL,1,NULL,2}'::int[], 1) as s; |
| 10742 | + s |
| 10743 | +--- |
| 10744 | + 1 |
| 10745 | + 2 |
| 10746 | + 3 |
| 10747 | + 4 |
| 10748 | +(4 rows) |
| 10749 | + |
| 10750 | +-- presenting an array, the subscript and the subscripted |
| 10751 | +-- value requires a subquery |
| 10752 | +select * from arrays; |
| 10753 | + a |
| 10754 | +-------------------- |
| 10755 | + {-1,-2} |
| 10756 | + {100,200} |
| 10757 | +(2 rows) |
| 10758 | + |
| 10759 | +select a as array, s as subscript, a[s] as value |
| 10760 | +from (select generate_subscripts(a, 1) as s, a from arrays) foo; |
| 10761 | + array | subscript | value |
| 10762 | +-----------+-----------+------- |
| 10763 | + {-1,-2} | 1 | -1 |
| 10764 | + {-1,-2} | 2 | -2 |
| 10765 | + {100,200} | 1 | 100 |
| 10766 | + {100,200} | 2 | 200 |
| 10767 | +(4 rows) |
| 10768 | + |
| 10769 | +-- unnest a 2D array |
| 10770 | +create or replace function unnest2(anyarray) |
| 10771 | +returns setof anyelement as $$ |
| 10772 | +select $1[i][j] |
| 10773 | + from generate_subscripts($1,1) g1(i), |
| 10774 | + generate_subscripts($1,2) g2(j); |
| 10775 | +$$ language sql immutable; |
| 10776 | +CREATE FUNCTION |
| 10777 | +postgres=# select * from unnest2(array[[1,2],[3,4]]); |
| 10778 | + unnest2 |
| 10779 | +--------- |
| 10780 | + 1 |
| 10781 | + 2 |
| 10782 | + 3 |
| 10783 | + 4 |
| 10784 | +(4 rows) |
| 10785 | +</programlisting> |
| 10786 | + </para> |
| 10787 | + |
10694 | 10788 | </sect1>
|
10695 | 10789 |
|
10696 | 10790 | <sect1 id="functions-info">
|
|
0 commit comments