index b5dfe08c9bb7ab76c9173c29a55f388d7d2fe085..c97ecf4a1add9adcd14bdc682e70a41c160cbc88 100644 (file)
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
index 5925a91373989c16e7c0bade2028f7bb003ac7d0..c08310ba41561275c2756558a074330e8213afe9 100644 (file)
@@ -3865,8 +3865,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);
index 98570e663c04b41a615f6bfd7b264ff5c3d20f51..227a7c897e0dc138f0d7da46bd8326e574c77fca 100644 (file)
/* 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);