1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-12 21:01:52 +03:00

Change array_offset to return subscripts, not offsets

... and rename it and its sibling array_offsets to array_position and
array_positions, to account for the changed behavior.

Having the functions return subscripts better matches existing practice,
and is better suited to using the result value as a subscript into the
array directly.  For one-based arrays, the new definition is identical
to what was originally committed.

(We use the term "subscript" in the documentation, which is what we use
whenever we talk about arrays; but the functions themselves are named
using the word "position" to match the standard-defined POSITION()
functions.)

Author: Pavel Stěhule
Behavioral problem noted by Dean Rasheed.
This commit is contained in:
Alvaro Herrera
2015-03-30 16:13:21 -03:00
parent 0853630159
commit 97690ea6e8
8 changed files with 136 additions and 117 deletions

View File

@ -601,20 +601,20 @@ SELECT * FROM sal_emp WHERE pay_by_quarter && ARRAY[10000];
</para>
<para>
You can also search for specific values in an array using the <function>array_offset</>
and <function>array_offsets</> functions. The former returns the position of
You can also search for specific values in an array using the <function>array_position</>
and <function>array_positions</> functions. The former returns the subscript of
the first occurrence of a value in an array; the latter returns an array with the
positions of all occurrences of the value in the array. For example:
subscripts of all occurrences of the value in the array. For example:
<programlisting>
SELECT array_offset(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon');
array_offset
--------------
SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon');
array_positions
-----------------
2
SELECT array_offsets(ARRAY[1, 4, 3, 1, 3, 4, 2, 1], 1);
array_offsets
---------------
SELECT array_positions(ARRAY[1, 4, 3, 1, 3, 4, 2, 1], 1);
array_positions
-----------------
{1,4,8}
</programlisting>
</para>