From d5b2587c20854044277c9616b8582b5f1f19d197 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 28 Dec 2010 22:49:57 -0500 Subject: Avoid unexpected conversion overflow in planner for distant date values. The "date" type supports a wider range of dates than int64 timestamps do. However, there is pre-int64-timestamp code in the planner that assumes that all date values can be converted to timestamp with impunity. Fortunately, what we really need out of the conversion is always a double (float8) value; so even when the date is out of timestamp's range it's possible to produce a sane answer. All we need is a code path that doesn't try to force the result into int64. Per trouble report from David Rericha. Back-patch to all supported versions. Although this is surely a corner case, there's not much point in advertising a date range wider than timestamp's if we will choke on such values in unexpected places. --- src/backend/utils/adt/date.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/backend/utils/adt/date.c') diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 99448375ecf..2f6e19be372 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -447,6 +447,39 @@ date2timestamptz(DateADT dateVal) return result; } +/* + * date2timestamp_no_overflow + * + * This is chartered to produce a double value that is numerically + * equivalent to the corresponding Timestamp value, if the date is in the + * valid range of Timestamps, but in any case not throw an overflow error. + * We can do this since the numerical range of double is greater than + * that of non-erroneous timestamps. The results are currently only + * used for statistical estimation purposes. + */ +double +date2timestamp_no_overflow(DateADT dateVal) +{ + double result; + + if (DATE_IS_NOBEGIN(dateVal)) + result = -DBL_MAX; + else if (DATE_IS_NOEND(dateVal)) + result = DBL_MAX; + else + { +#ifdef HAVE_INT64_TIMESTAMP + /* date is days since 2000, timestamp is microseconds since same... */ + result = dateVal * (double) USECS_PER_DAY; +#else + /* date is days since 2000, timestamp is seconds since same... */ + result = dateVal * (double) SECS_PER_DAY; +#endif + } + + return result; +} + /* * Crosstype comparison functions for dates -- cgit v1.2.3