Skip to content

Commit ccfe3db

Browse files
committed
Doc: improve documentation about width_bucket().
Specify whether the bucket bounds are inclusive or exclusive, and improve some other vague language. Explain the behavior that occurs when the "low" bound is greater than the "high" bound. Make width_bucket_numeric's comment more like that for width_bucket_float8, in particular noting that infinite bounds are rejected (since they became possible in v14). Reported-by: Ben Peachey Higdon <bpeacheyhigdon@gmail.com> Author: Robert Treat <rob@xzilla.net> Co-authored-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com> Discussion: https://postgr.es/m/2BD74F86-5B89-4AC1-8F13-23CED3546AC1@gmail.com Backpatch-through: 13
1 parent 75b8982 commit ccfe3db

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

doc/src/sgml/func.sgml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,13 +1739,23 @@ repeat('Pg', 4) <returnvalue>PgPgPgPg</returnvalue>
17391739
which <parameter>operand</parameter> falls in a histogram
17401740
having <parameter>count</parameter> equal-width buckets spanning the
17411741
range <parameter>low</parameter> to <parameter>high</parameter>.
1742-
Returns <literal>0</literal>
1742+
The buckets have inclusive lower bounds and exclusive upper bounds.
1743+
Returns <literal>0</literal> for an input less
1744+
than <parameter>low</parameter>,
17431745
or <literal><parameter>count</parameter>+1</literal> for an input
1744-
outside that range.
1746+
greater than or equal to <parameter>high</parameter>.
1747+
If <parameter>low</parameter> &gt; <parameter>high</parameter>,
1748+
the behavior is mirror-reversed, with bucket <literal>1</literal>
1749+
now being the one just below <parameter>low</parameter>, and the
1750+
inclusive bounds now being on the upper side.
17451751
</para>
17461752
<para>
17471753
<literal>width_bucket(5.35, 0.024, 10.06, 5)</literal>
17481754
<returnvalue>3</returnvalue>
1755+
</para>
1756+
<para>
1757+
<literal>width_bucket(9, 10, 0, 10)</literal>
1758+
<returnvalue>2</returnvalue>
17491759
</para></entry>
17501760
</row>
17511761

@@ -1757,8 +1767,8 @@ repeat('Pg', 4) <returnvalue>PgPgPgPg</returnvalue>
17571767
<para>
17581768
Returns the number of the bucket in
17591769
which <parameter>operand</parameter> falls given an array listing the
1760-
lower bounds of the buckets. Returns <literal>0</literal> for an
1761-
input less than the first lower
1770+
inclusive lower bounds of the buckets.
1771+
Returns <literal>0</literal> for an input less than the first lower
17621772
bound. <parameter>operand</parameter> and the array elements can be
17631773
of any type having standard comparison operators.
17641774
The <parameter>thresholds</parameter> array <emphasis>must be

src/backend/utils/adt/float.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4004,8 +4004,8 @@ float84ge(PG_FUNCTION_ARGS)
40044004
* in the histogram. width_bucket() returns an integer indicating the
40054005
* bucket number that 'operand' belongs to in an equiwidth histogram
40064006
* with the specified characteristics. An operand smaller than the
4007-
* lower bound is assigned to bucket 0. An operand greater than the
4008-
* upper bound is assigned to an additional bucket (with number
4007+
* lower bound is assigned to bucket 0. An operand greater than or equal
4008+
* to the upper bound is assigned to an additional bucket (with number
40094009
* count+1). We don't allow "NaN" for any of the float8 inputs, and we
40104010
* don't allow either of the histogram bounds to be +/- infinity.
40114011
*/

src/backend/utils/adt/numeric.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,9 +1764,10 @@ generate_series_step_numeric(PG_FUNCTION_ARGS)
17641764
* in the histogram. width_bucket() returns an integer indicating the
17651765
* bucket number that 'operand' belongs to in an equiwidth histogram
17661766
* with the specified characteristics. An operand smaller than the
1767-
* lower bound is assigned to bucket 0. An operand greater than the
1768-
* upper bound is assigned to an additional bucket (with number
1769-
* count+1). We don't allow "NaN" for any of the numeric arguments.
1767+
* lower bound is assigned to bucket 0. An operand greater than or equal
1768+
* to the upper bound is assigned to an additional bucket (with number
1769+
* count+1). We don't allow "NaN" for any of the numeric inputs, and we
1770+
* don't allow either of the histogram bounds to be +/- infinity.
17701771
*/
17711772
Datum
17721773
width_bucket_numeric(PG_FUNCTION_ARGS)

0 commit comments

Comments
 (0)