Skip to content

Commit 5bc4506

Browse files
committed
Error suppression support for upcoming jsonpath .datetime() method
Add support of error suppression in some date and time manipulation functions as it's required for jsonpath .datetime() method support. This commit doesn't use PG_TRY()/PG_CATCH() in order to implement that. Instead, it provides internal versions of date and time functions used, which support error suppression. Discussion: https://postgr.es/m/CAPpHfdsZgYEra_PeCLGNoXOWYx6iU-S3wF8aX0ObQUcZU%2B4XTw%40mail.gmail.com Author: Alexander Korotkov, Nikita Glukhov Reviewed-by: Anastasia Lubennikova, Peter Eisentraut
1 parent 66c74f8 commit 5bc4506

File tree

7 files changed

+479
-220
lines changed

7 files changed

+479
-220
lines changed

src/backend/utils/adt/date.c

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,15 @@ date_mii(PG_FUNCTION_ARGS)
550550
PG_RETURN_DATEADT(result);
551551
}
552552

553+
553554
/*
554-
* Internal routines for promoting date to timestamp and timestamp with
555-
* time zone
555+
* Promote date to timestamp.
556+
*
557+
* If 'have_error' is NULL, then errors are thrown, else '*have_error' is set
558+
* and zero is returned.
556559
*/
557-
558-
static Timestamp
559-
date2timestamp(DateADT dateVal)
560+
Timestamp
561+
date2timestamp_opt_error(DateADT dateVal, bool *have_error)
560562
{
561563
Timestamp result;
562564

@@ -572,9 +574,19 @@ date2timestamp(DateADT dateVal)
572574
* boundary need be checked for overflow.
573575
*/
574576
if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
575-
ereport(ERROR,
576-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
577-
errmsg("date out of range for timestamp")));
577+
{
578+
if (have_error)
579+
{
580+
*have_error = true;
581+
return (Timestamp) 0;
582+
}
583+
else
584+
{
585+
ereport(ERROR,
586+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
587+
errmsg("date out of range for timestamp")));
588+
}
589+
}
578590

579591
/* date is days since 2000, timestamp is microseconds since same... */
580592
result = dateVal * USECS_PER_DAY;
@@ -583,8 +595,23 @@ date2timestamp(DateADT dateVal)
583595
return result;
584596
}
585597

598+
/*
599+
* Single-argument version of date2timestamp_opt_error().
600+
*/
586601
static TimestampTz
587-
date2timestamptz(DateADT dateVal)
602+
date2timestamp(DateADT dateVal)
603+
{
604+
return date2timestamp_opt_error(dateVal, NULL);
605+
}
606+
607+
/*
608+
* Promote date to timestamp with time zone.
609+
*
610+
* If 'have_error' is NULL, then errors are thrown, else '*have_error' is set
611+
* and zero is returned.
612+
*/
613+
TimestampTz
614+
date2timestamptz_opt_error(DateADT dateVal, bool *have_error)
588615
{
589616
TimestampTz result;
590617
struct pg_tm tt,
@@ -603,9 +630,19 @@ date2timestamptz(DateADT dateVal)
603630
* boundary need be checked for overflow.
604631
*/
605632
if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
606-
ereport(ERROR,
607-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
608-
errmsg("date out of range for timestamp")));
633+
{
634+
if (have_error)
635+
{
636+
*have_error = true;
637+
return (TimestampTz) 0;
638+
}
639+
else
640+
{
641+
ereport(ERROR,
642+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
643+
errmsg("date out of range for timestamp")));
644+
}
645+
}
609646

610647
j2date(dateVal + POSTGRES_EPOCH_JDATE,
611648
&(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
@@ -621,14 +658,33 @@ date2timestamptz(DateADT dateVal)
621658
* of time zone, check for allowed timestamp range after adding tz.
622659
*/
623660
if (!IS_VALID_TIMESTAMP(result))
624-
ereport(ERROR,
625-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
626-
errmsg("date out of range for timestamp")));
661+
{
662+
if (have_error)
663+
{
664+
*have_error = true;
665+
return (TimestampTz) 0;
666+
}
667+
else
668+
{
669+
ereport(ERROR,
670+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
671+
errmsg("date out of range for timestamp")));
672+
}
673+
}
627674
}
628675

629676
return result;
630677
}
631678

679+
/*
680+
* Single-argument version of date2timestamptz_opt_error().
681+
*/
682+
static TimestampTz
683+
date2timestamptz(DateADT dateVal)
684+
{
685+
return date2timestamptz_opt_error(dateVal, NULL);
686+
}
687+
632688
/*
633689
* date2timestamp_no_overflow
634690
*

0 commit comments

Comments
 (0)