1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-07 00:36:50 +03:00

Break out xxx2yyy_opt_overflow APIs for more datetime conversions.

Previous commits invented timestamp2timestamptz_opt_overflow,
date2timestamp_opt_overflow, and date2timestamptz_opt_overflow
functions to perform non-error-throwing conversions between
datetime types.  This patch completes the set by adding
timestamp2date_opt_overflow, timestamptz2date_opt_overflow,
and timestamptz2timestamp_opt_overflow.

In addition, adjust timestamp2timestamptz_opt_overflow so that it
doesn't throw error if timestamp2tm fails, but treats that as an
overflow case.  The situation probably can't arise except with an
invalid timestamp value, and I can't think of a way that that would
happen except data corruption.  However, it's pretty silly to have a
function whose entire reason for existence is to not throw errors for
out-of-range inputs nonetheless throw an error for out-of-range input.

The new APIs are not used in this patch, but will be needed in
upcoming btree_gin changes.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Arseniy Mukhin <arseniy.mukhin.dev@gmail.com>
Discussion: https://postgr.es/m/262624.1738460652@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2025-07-03 16:17:08 -04:00
parent a10f21e6ce
commit 0059bbe1ec
4 changed files with 157 additions and 17 deletions

View File

@ -1363,10 +1363,35 @@ timestamp_date(PG_FUNCTION_ARGS)
{ {
Timestamp timestamp = PG_GETARG_TIMESTAMP(0); Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
DateADT result; DateADT result;
result = timestamp2date_opt_overflow(timestamp, NULL);
PG_RETURN_DATEADT(result);
}
/*
* Convert timestamp to date.
*
* On successful conversion, *overflow is set to zero if it's not NULL.
*
* If the timestamp is finite but out of the valid range for date, then:
* if overflow is NULL, we throw an out-of-range error.
* if overflow is not NULL, we store +1 or -1 there to indicate the sign
* of the overflow, and return the appropriate date infinity.
*
* Note: given the ranges of the types, overflow is only possible at
* the minimum end of the range, but we don't assume that in this code.
*/
DateADT
timestamp2date_opt_overflow(Timestamp timestamp, int *overflow)
{
DateADT result;
struct pg_tm tt, struct pg_tm tt,
*tm = &tt; *tm = &tt;
fsec_t fsec; fsec_t fsec;
if (overflow)
*overflow = 0;
if (TIMESTAMP_IS_NOBEGIN(timestamp)) if (TIMESTAMP_IS_NOBEGIN(timestamp))
DATE_NOBEGIN(result); DATE_NOBEGIN(result);
else if (TIMESTAMP_IS_NOEND(timestamp)) else if (TIMESTAMP_IS_NOEND(timestamp))
@ -1374,14 +1399,30 @@ timestamp_date(PG_FUNCTION_ARGS)
else else
{ {
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
{
if (overflow)
{
if (timestamp < 0)
{
*overflow = -1;
DATE_NOBEGIN(result);
}
else
{
*overflow = 1; /* not actually reachable */
DATE_NOEND(result);
}
return result;
}
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range"))); errmsg("timestamp out of range")));
}
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE; result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
} }
PG_RETURN_DATEADT(result); return result;
} }
@ -1408,11 +1449,36 @@ timestamptz_date(PG_FUNCTION_ARGS)
{ {
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0); TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
DateADT result; DateADT result;
result = timestamptz2date_opt_overflow(timestamp, NULL);
PG_RETURN_DATEADT(result);
}
/*
* Convert timestamptz to date.
*
* On successful conversion, *overflow is set to zero if it's not NULL.
*
* If the timestamptz is finite but out of the valid range for date, then:
* if overflow is NULL, we throw an out-of-range error.
* if overflow is not NULL, we store +1 or -1 there to indicate the sign
* of the overflow, and return the appropriate date infinity.
*
* Note: given the ranges of the types, overflow is only possible at
* the minimum end of the range, but we don't assume that in this code.
*/
DateADT
timestamptz2date_opt_overflow(TimestampTz timestamp, int *overflow)
{
DateADT result;
struct pg_tm tt, struct pg_tm tt,
*tm = &tt; *tm = &tt;
fsec_t fsec; fsec_t fsec;
int tz; int tz;
if (overflow)
*overflow = 0;
if (TIMESTAMP_IS_NOBEGIN(timestamp)) if (TIMESTAMP_IS_NOBEGIN(timestamp))
DATE_NOBEGIN(result); DATE_NOBEGIN(result);
else if (TIMESTAMP_IS_NOEND(timestamp)) else if (TIMESTAMP_IS_NOEND(timestamp))
@ -1420,14 +1486,30 @@ timestamptz_date(PG_FUNCTION_ARGS)
else else
{ {
if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0) if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
{
if (overflow)
{
if (timestamp < 0)
{
*overflow = -1;
DATE_NOBEGIN(result);
}
else
{
*overflow = 1; /* not actually reachable */
DATE_NOEND(result);
}
return result;
}
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range"))); errmsg("timestamp out of range")));
}
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE; result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
} }
PG_RETURN_DATEADT(result); return result;
} }

View File

@ -6477,7 +6477,7 @@ timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
if (TIMESTAMP_NOT_FINITE(timestamp)) if (TIMESTAMP_NOT_FINITE(timestamp))
return timestamp; return timestamp;
/* We don't expect this to fail, but check it pro forma */ /* timestamp2tm should not fail on valid timestamps, but cope */
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0) if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
{ {
tz = DetermineTimeZoneOffset(tm, session_timezone); tz = DetermineTimeZoneOffset(tm, session_timezone);
@ -6485,23 +6485,22 @@ timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
result = dt2local(timestamp, -tz); result = dt2local(timestamp, -tz);
if (IS_VALID_TIMESTAMP(result)) if (IS_VALID_TIMESTAMP(result))
{
return result; return result;
} }
else if (overflow)
if (overflow)
{
if (timestamp < 0)
{ {
if (result < MIN_TIMESTAMP) *overflow = -1;
{ TIMESTAMP_NOBEGIN(result);
*overflow = -1;
TIMESTAMP_NOBEGIN(result);
}
else
{
*overflow = 1;
TIMESTAMP_NOEND(result);
}
return result;
} }
else
{
*overflow = 1;
TIMESTAMP_NOEND(result);
}
return result;
} }
ereport(ERROR, ereport(ERROR,
@ -6531,8 +6530,27 @@ timestamptz_timestamp(PG_FUNCTION_ARGS)
PG_RETURN_TIMESTAMP(timestamptz2timestamp(timestamp)); PG_RETURN_TIMESTAMP(timestamptz2timestamp(timestamp));
} }
/*
* Convert timestamptz to timestamp, throwing error for overflow.
*/
static Timestamp static Timestamp
timestamptz2timestamp(TimestampTz timestamp) timestamptz2timestamp(TimestampTz timestamp)
{
return timestamptz2timestamp_opt_overflow(timestamp, NULL);
}
/*
* Convert timestamp with time zone to timestamp.
*
* On successful conversion, *overflow is set to zero if it's not NULL.
*
* If the timestamptz is finite but out of the valid range for timestamp, then:
* if overflow is NULL, we throw an out-of-range error.
* if overflow is not NULL, we store +1 or -1 there to indicate the sign
* of the overflow, and return the appropriate timestamp infinity.
*/
Timestamp
timestamptz2timestamp_opt_overflow(TimestampTz timestamp, int *overflow)
{ {
Timestamp result; Timestamp result;
struct pg_tm tt, struct pg_tm tt,
@ -6540,18 +6558,53 @@ timestamptz2timestamp(TimestampTz timestamp)
fsec_t fsec; fsec_t fsec;
int tz; int tz;
if (overflow)
*overflow = 0;
if (TIMESTAMP_NOT_FINITE(timestamp)) if (TIMESTAMP_NOT_FINITE(timestamp))
result = timestamp; result = timestamp;
else else
{ {
if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0) if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
{
if (overflow)
{
if (timestamp < 0)
{
*overflow = -1;
TIMESTAMP_NOBEGIN(result);
}
else
{
*overflow = 1;
TIMESTAMP_NOEND(result);
}
return result;
}
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range"))); errmsg("timestamp out of range")));
}
if (tm2timestamp(tm, fsec, NULL, &result) != 0) if (tm2timestamp(tm, fsec, NULL, &result) != 0)
{
if (overflow)
{
if (timestamp < 0)
{
*overflow = -1;
TIMESTAMP_NOBEGIN(result);
}
else
{
*overflow = 1;
TIMESTAMP_NOEND(result);
}
return result;
}
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range"))); errmsg("timestamp out of range")));
}
} }
return result; return result;
} }

View File

@ -100,6 +100,8 @@ extern int32 anytime_typmod_check(bool istz, int32 typmod);
extern double date2timestamp_no_overflow(DateADT dateVal); extern double date2timestamp_no_overflow(DateADT dateVal);
extern Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow); extern Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow);
extern TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow); extern TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow);
extern DateADT timestamp2date_opt_overflow(Timestamp timestamp, int *overflow);
extern DateADT timestamptz2date_opt_overflow(TimestampTz timestamp, int *overflow);
extern int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2); extern int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2);
extern int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2); extern int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2);

View File

@ -144,6 +144,9 @@ extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
extern TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, extern TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp,
int *overflow); int *overflow);
extern Timestamp timestamptz2timestamp_opt_overflow(TimestampTz timestamp,
int *overflow);
extern int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal, extern int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal,
TimestampTz dt2); TimestampTz dt2);