Skip to content

Commit 4f9e330

Browse files
committed
Return NULL instead of throwing error when desired bound is not available.
Change range_lower and range_upper to return NULL rather than throwing an error when the input range is empty or the relevant bound is infinite. Per discussion, throwing an error seems likely to be unduly hard to work with. Also, this is more consistent with the behavior of the constructors, which treat NULL as meaning an infinite bound.
1 parent 851c83f commit 4f9e330

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

doc/src/sgml/func.sgml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10745,6 +10745,14 @@ SELECT NULLIF(value, '(none)') ...
1074510745
</tbody>
1074610746
</tgroup>
1074710747
</table>
10748+
10749+
<para>
10750+
The <function>lower</> and <function>upper</> functions return null
10751+
if the range is empty or the requested bound is infinite.
10752+
The <function>lower_inc</function>, <function>upper_inc</function>,
10753+
<function>lower_inf</function>, and <function>upper_inf</function>
10754+
functions all return false for an empty range.
10755+
</para>
1074810756
</sect1>
1074910757

1075010758
<sect1 id="functions-aggregate">

src/backend/utils/adt/rangetypes.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -456,14 +456,9 @@ range_lower(PG_FUNCTION_ARGS)
456456

457457
range_deserialize(fcinfo, r1, &lower, &upper, &empty);
458458

459-
if (empty)
460-
ereport(ERROR,
461-
(errcode(ERRCODE_DATA_EXCEPTION),
462-
errmsg("empty range has no lower bound")));
463-
if (lower.infinite)
464-
ereport(ERROR,
465-
(errcode(ERRCODE_DATA_EXCEPTION),
466-
errmsg("range lower bound is infinite")));
459+
/* Return NULL if there's no finite lower bound */
460+
if (empty || lower.infinite)
461+
PG_RETURN_NULL();
467462

468463
PG_RETURN_DATUM(lower.val);
469464
}
@@ -478,14 +473,9 @@ range_upper(PG_FUNCTION_ARGS)
478473

479474
range_deserialize(fcinfo, r1, &lower, &upper, &empty);
480475

481-
if (empty)
482-
ereport(ERROR,
483-
(errcode(ERRCODE_DATA_EXCEPTION),
484-
errmsg("empty range has no upper bound")));
485-
if (upper.infinite)
486-
ereport(ERROR,
487-
(errcode(ERRCODE_DATA_EXCEPTION),
488-
errmsg("range upper bound is infinite")));
476+
/* Return NULL if there's no finite upper bound */
477+
if (empty || upper.infinite)
478+
PG_RETURN_NULL();
489479

490480
PG_RETURN_DATUM(upper.val);
491481
}

0 commit comments

Comments
 (0)