postgres.git / summary / log / commit / refs
commit dcb2b356b01a2c7192f507c3570e6b5bbad93c24
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Wed Dec 29 03:49:57 2010 +0000
First released in: 9.1.0, 8.2.20, 8.3.14, 8.4.7, 9.0.3
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 +++++++++++++++++++++++++++++++++
src/backend/utils/adt/selfuncs.c | 3 +--
src/include/utils/date.h | 2 ++
3 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c
index d29f95846e9..60bf4ad5b21 100644
--- a/src/backend/utils/adt/date.c
+++ b/src/backend/utils/adt/date.c
@@ -388,6 +388,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
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 76df407a203..256a0725728 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -3473,8 +3473,7 @@ convert_timevalue_to_scalar(Datum value, Oid typid)
return DatumGetTimestamp(DirectFunctionCall1(abstime_timestamp,
value));
case DATEOID:
- return DatumGetTimestamp(DirectFunctionCall1(date_timestamp,
- value));
+ return date2timestamp_no_overflow(DatumGetDateADT(value));
case INTERVALOID:
{
Interval *interval = DatumGetIntervalP(value);
diff --git a/src/include/utils/date.h b/src/include/utils/date.h
index eca2700ed17..58a1ca680fa 100644
--- a/src/include/utils/date.h
+++ b/src/include/utils/date.h
@@ -81,6 +81,8 @@ typedef struct
/* date.c */
+extern double date2timestamp_no_overflow(DateADT dateVal);
+
extern Datum date_in(PG_FUNCTION_ARGS);
extern Datum date_out(PG_FUNCTION_ARGS);
extern Datum date_recv(PG_FUNCTION_ARGS);
[parent: a6a77ae407be]