Skip to content

Commit 4ee6e78

Browse files
jianhe-funCommitfest Bot
authored andcommitted
error safe for casting date to other types per pg_cast
select castsource::regtype, casttarget::regtype, castfunc, castcontext,castmethod, pp.prosrc, pp.proname from pg_cast pc join pg_proc pp on pp.oid = pc.castfunc and pc.castfunc > 0 and castsource::regtype = 'date'::regtype order by castsource::regtype; castsource | casttarget | castfunc | castcontext | castmethod | prosrc | proname ------------+-----------------------------+----------+-------------+------------+------------------+------------- date | timestamp without time zone | 2024 | i | f | date_timestamp | timestamp date | timestamp with time zone | 1174 | i | f | date_timestamptz | timestamptz (2 rows) discussion: https://postgr.es/m/CADkLM=fv1JfY4Ufa-jcwwNbjQixNViskQ8jZu3Tz_p656i_4hQ@mail.gmail.com
1 parent 26c4f25 commit 4ee6e78

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/backend/utils/adt/date.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,16 @@ date_timestamp(PG_FUNCTION_ARGS)
13501350
DateADT dateVal = PG_GETARG_DATEADT(0);
13511351
Timestamp result;
13521352

1353-
result = date2timestamp(dateVal);
1353+
if (likely(!fcinfo->context))
1354+
result = date2timestamp(dateVal);
1355+
else
1356+
{
1357+
int overflow;
1358+
1359+
result = date2timestamp_opt_overflow(dateVal, &overflow);
1360+
if (overflow != 0)
1361+
PG_RETURN_NULL();
1362+
}
13541363

13551364
PG_RETURN_TIMESTAMP(result);
13561365
}
@@ -1435,7 +1444,16 @@ date_timestamptz(PG_FUNCTION_ARGS)
14351444
DateADT dateVal = PG_GETARG_DATEADT(0);
14361445
TimestampTz result;
14371446

1438-
result = date2timestamptz(dateVal);
1447+
if (likely(!fcinfo->context))
1448+
result = date2timestamptz(dateVal);
1449+
else
1450+
{
1451+
int overflow;
1452+
result = date2timestamptz_opt_overflow(dateVal, &overflow);
1453+
1454+
if (overflow != 0)
1455+
PG_RETURN_NULL();
1456+
}
14391457

14401458
PG_RETURN_TIMESTAMP(result);
14411459
}

0 commit comments

Comments
 (0)