aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils')
-rw-r--r--src/backend/utils/adt/float.c332
-rw-r--r--src/backend/utils/adt/oracle_compat.c101
-rw-r--r--src/backend/utils/adt/timestamp.c111
3 files changed, 423 insertions, 121 deletions
diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c
index 973e9cd12e9..82c7e191d53 100644
--- a/src/backend/utils/adt/float.c
+++ b/src/backend/utils/adt/float.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.55 2000/03/23 07:40:00 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.56 2000/04/07 13:39:40 thomas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1220,7 +1220,7 @@ dlog1(float64 arg1)
float64 result;
double tmp;
- if (!arg1)
+ if (!PointerIsValid(arg1))
return (float64) NULL;
result = (float64) palloc(sizeof(float64data));
@@ -1234,7 +1234,8 @@ dlog1(float64 arg1)
CheckFloat8Val(*result);
return result;
-}
+} /* dlog1() */
+
/*
* dlog10 - returns a pointer to the base 10 logarithm of arg1
@@ -1263,6 +1264,331 @@ dlog10(float64 arg1)
/*
+ * dacos - returns a pointer to the arccos of arg1 (radians)
+ */
+float64
+dacos(float64 arg1)
+{
+ float64 result;
+ double tmp;
+
+ if (!PointerIsValid(arg1))
+ return (float64) NULL;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ tmp = *arg1;
+ errno = 0;
+ *result = (float64data) acos(tmp);
+ if (errno != 0
+#ifdef HAVE_FINITE
+ || !finite(*result)
+#endif
+ )
+ elog(ERROR, "dacos(%f) input is out of range", *arg1);
+
+ CheckFloat8Val(*result);
+ return result;
+} /* dacos() */
+
+
+/*
+ * dasin - returns a pointer to the arcsin of arg1 (radians)
+ */
+float64
+dasin(float64 arg1)
+{
+ float64 result;
+ double tmp;
+
+ if (!PointerIsValid(arg1))
+ return (float64) NULL;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ tmp = *arg1;
+ errno = 0;
+ *result = (float64data) asin(tmp);
+ if (errno != 0
+#ifdef HAVE_FINITE
+ || !finite(*result)
+#endif
+ )
+ elog(ERROR, "dasin(%f) input is out of range", *arg1);
+
+ CheckFloat8Val(*result);
+ return result;
+} /* dasin() */
+
+
+/*
+ * datan - returns a pointer to the arctan of arg1 (radians)
+ */
+float64
+datan(float64 arg1)
+{
+ float64 result;
+ double tmp;
+
+ if (!PointerIsValid(arg1))
+ return (float64) NULL;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ tmp = *arg1;
+ errno = 0;
+ *result = (float64data) atan(tmp);
+ if (errno != 0
+#ifdef HAVE_FINITE
+ || !finite(*result)
+#endif
+ )
+ elog(ERROR, "atan(%f) input is out of range", *arg1);
+
+ CheckFloat8Val(*result);
+ return result;
+} /* datan() */
+
+
+/*
+ * atan2 - returns a pointer to the arctan2 of arg1 (radians)
+ */
+float64
+datan2(float64 arg1, float64 arg2)
+{
+ float64 result;
+
+ if (!PointerIsValid(arg1) || !PointerIsValid(arg1))
+ return (float64) NULL;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ errno = 0;
+ *result = (float64data) atan2(*arg1, *arg2);
+ if (errno != 0
+#ifdef HAVE_FINITE
+ || !finite(*result)
+#endif
+ )
+ elog(ERROR, "atan2(%f,%f) input is out of range", *arg1, *arg2);
+
+ CheckFloat8Val(*result);
+ return result;
+} /* datan2() */
+
+
+/*
+ * dcos - returns a pointer to the cosine of arg1 (radians)
+ */
+float64
+dcos(float64 arg1)
+{
+ float64 result;
+ double tmp;
+
+ if (!PointerIsValid(arg1))
+ return (float64) NULL;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ tmp = *arg1;
+ errno = 0;
+ *result = (float64data) cos(tmp);
+ if (errno != 0
+#ifdef HAVE_FINITE
+ || !finite(*result)
+#endif
+ )
+ elog(ERROR, "dcos(%f) input is out of range", *arg1);
+
+ CheckFloat8Val(*result);
+ return result;
+} /* dcos() */
+
+
+/*
+ * dcot - returns a pointer to the cotangent of arg1 (radians)
+ */
+float64
+dcot(float64 arg1)
+{
+ float64 result;
+ double tmp;
+
+ if (!PointerIsValid(arg1))
+ return (float64) NULL;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ tmp = *arg1;
+ errno = 0;
+ *result = (float64data) tan(tmp);
+ if ((errno != 0) || (*result == 0.0)
+#ifdef HAVE_FINITE
+ || !finite(*result)
+#endif
+ )
+ elog(ERROR, "dcot(%f) input is out of range", *arg1);
+
+ *result = 1.0/(*result);
+ CheckFloat8Val(*result);
+ return result;
+} /* dcot() */
+
+
+/*
+ * dsin - returns a pointer to the sine of arg1 (radians)
+ */
+float64
+dsin(float64 arg1)
+{
+ float64 result;
+ double tmp;
+
+ if (!PointerIsValid(arg1))
+ return (float64) NULL;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ tmp = *arg1;
+ errno = 0;
+ *result = (float64data) sin(tmp);
+ if (errno != 0
+#ifdef HAVE_FINITE
+ || !finite(*result)
+#endif
+ )
+ elog(ERROR, "dsin(%f) input is out of range", *arg1);
+
+ CheckFloat8Val(*result);
+ return result;
+} /* dsin() */
+
+
+/*
+ * dtan - returns a pointer to the tangent of arg1 (radians)
+ */
+float64
+dtan(float64 arg1)
+{
+ float64 result;
+ double tmp;
+
+ if (!PointerIsValid(arg1))
+ return (float64) NULL;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ tmp = *arg1;
+ errno = 0;
+ *result = (float64data) tan(tmp);
+ if (errno != 0
+#ifdef HAVE_FINITE
+ || !finite(*result)
+#endif
+ )
+ elog(ERROR, "dtan(%f) input is out of range", *arg1);
+
+ CheckFloat8Val(*result);
+ return result;
+} /* dtan() */
+
+
+#ifndef M_PI
+/* from my RH5.2 gcc math.h file - thomas 2000-04-03 */
+#define M_PI 3.14159265358979323846
+#endif
+
+
+/*
+ * degrees - returns a pointer to degrees converted from radians
+ */
+float64
+degrees(float64 arg1)
+{
+ float64 result;
+
+ if (!arg1)
+ return (float64) NULL;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ *result = ((*arg1) * (180.0 / M_PI));
+
+ CheckFloat8Val(*result);
+ return result;
+} /* degrees() */
+
+
+/*
+ * dpi - returns a pointer to degrees converted to radians
+ */
+float64
+dpi(void)
+{
+ float64 result;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ *result = (M_PI);
+
+ return result;
+} /* dpi() */
+
+
+/*
+ * radians - returns a pointer to radians converted from degrees
+ */
+float64
+radians(float64 arg1)
+{
+ float64 result;
+
+ if (!arg1)
+ return (float64) NULL;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ *result = ((*arg1) * (M_PI / 180.0));
+
+ CheckFloat8Val(*result);
+ return result;
+} /* radians() */
+
+
+/*
+ * drandom - returns a random number
+ */
+float64
+drandom(void)
+{
+ float64 result;
+
+ result = (float64) palloc(sizeof(float64data));
+
+ /* result 0.0-1.0 */
+ *result = (((double) random()) / RAND_MAX);
+
+ CheckFloat8Val(*result);
+ return result;
+} /* drandom() */
+
+
+/*
+ * setseed - set seed for the random number generator
+ */
+int32
+setseed(float64 seed)
+{
+ int iseed = ((*seed) * RAND_MAX);
+
+ srandom((unsigned int) ((*seed) * RAND_MAX));
+
+ return iseed;
+} /* setseed() */
+
+
+/*
* ====================
* ARITHMETIC OPERATORS
* ====================
diff --git a/src/backend/utils/adt/oracle_compat.c b/src/backend/utils/adt/oracle_compat.c
index 36524798823..41e2563f3b0 100644
--- a/src/backend/utils/adt/oracle_compat.c
+++ b/src/backend/utils/adt/oracle_compat.c
@@ -1,7 +1,7 @@
/*
* Edmund Mergl <E.Mergl@bawue.de>
*
- * $Id: oracle_compat.c,v 1.22 2000/03/15 17:24:18 tgl Exp $
+ * $Id: oracle_compat.c,v 1.23 2000/04/07 13:39:41 thomas Exp $
*
*/
@@ -448,50 +448,6 @@ rtrim(text *string, text *set)
/********************************************************************
*
- * substr
- *
- * Syntax:
- *
- * text *substr(text *string, int4 m, int4 n)
- *
- * Purpose:
- *
- * Returns a portion of string, beginning at character m, n
- * characters long. The first position of string is 1.
- *
- ********************************************************************/
-
-#ifdef NOT_USED
-text *
-substr(text *string, int4 m, int4 n)
-{
- text *ret;
- char *ptr,
- *ptr_ret;
- int len;
-
- if ((string == (text *) NULL) ||
- (m <= 0) || (n <= 0) ||
- ((len = VARSIZE(string) - VARHDRSZ - m + 1) <= 0))
- return string;
-
- len = len + 1 < n ? len + 1 : n;
-
- ret = (text *) palloc(VARHDRSZ + len);
- VARSIZE(ret) = VARHDRSZ + len;
-
- ptr = VARDATA(string) + m - 1;
- ptr_ret = VARDATA(ret);
-
- while (len--)
- *ptr_ret++ = *ptr++;
-
- return ret;
-}
-#endif
-
-/********************************************************************
- *
* translate
*
* Syntax:
@@ -573,3 +529,58 @@ translate(text *string, text *from, text *to)
return result;
}
+
+
+int4
+ascii(text *string)
+{
+ if (!PointerIsValid(string))
+ return 0;
+
+ if (VARSIZE(string) <= VARHDRSZ)
+ return 0;
+
+ return ((int) *(VARDATA(string)));
+} /* ascii() */
+
+
+text *
+ichar(int4 cvalue)
+{
+ text *result;
+
+ result = (text *) palloc(VARHDRSZ + 1);
+ VARSIZE(result) = VARHDRSZ + 1;
+ *VARDATA(result) = (char) cvalue;
+
+ return result;
+} /* ichar() */
+
+
+text *
+repeat(text *string, int4 count)
+{
+ text *result;
+ int slen, tlen;
+ int i;
+ char *cp;
+
+ if (count < 0)
+ count = 0;
+
+ slen = (VARSIZE(string)-VARHDRSZ);
+ tlen = (VARHDRSZ + (count * slen));
+
+ result = (text *) palloc(tlen);
+
+ VARSIZE(result) = tlen;
+ cp = VARDATA(result);
+ for (i = 0; i < count; i++)
+ {
+ memcpy(cp, VARDATA(string), slen);
+ cp += slen;
+ }
+
+ return result;
+} /* repeat() */
+
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index 4086a803dd7..7a94ddd804d 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.23 2000/03/14 23:06:37 thomas Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.24 2000/04/07 13:39:41 thomas Exp $
*
*-------------------------------------------------------------------------
*/
@@ -32,72 +32,6 @@
#include "utils/builtins.h"
-#if 0
-
-
-static int DecodeDate(char *str, int fmask, int *tmask, struct tm * tm);
-static int DecodeNumber(int flen, char *field,
- int fmask, int *tmask, struct tm * tm, double *fsec, int *is2digits);
-static int DecodeNumberField(int len, char *str,
- int fmask, int *tmask, struct tm * tm, double *fsec, int *is2digits);
-static int DecodeSpecial(int field, char *lowtoken, int *val);
-static int DecodeTime(char *str, int fmask, int *tmask,
- struct tm * tm, double *fsec);
-static int DecodeTimezone(char *str, int *tzp);
-static int DecodeUnits(int field, char *lowtoken, int *val);
-static int EncodeSpecialTimestamp(Timestamp dt, char *str);
-static datetkn *datebsearch(char *key, datetkn *base, unsigned int nel);
-static Timestamp dt2local(Timestamp dt, int timezone);
-static int j2day(int jd);
-static double time2t(const int hour, const int min, const double sec);
-static int interval2tm(Interval span, struct tm * tm, float8 *fsec);
-static int tm2interval(struct tm * tm, double fsec, Interval *span);
-
-
-#define USE_DATE_CACHE 1
-#define ROUND_ALL 0
-
-int day_tab[2][13] = {
- {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0},
-{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}};
-
-
-char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
-"Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL};
-
-char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
-"Thursday", "Friday", "Saturday", NULL};
-
-/* TMODULO()
- * Macro to replace modf(), which is broken on some platforms.
- */
-#define TMODULO(t,q,u) \
-do { \
- q = ((t < 0)? ceil(t / u): floor(t / u)); \
- if (q != 0) \
- t -= rint(q * u); \
-} while(0)
-
-static void GetEpochTime(struct tm * tm);
-
-#define UTIME_MINYEAR (1901)
-#define UTIME_MINMONTH (12)
-#define UTIME_MINDAY (14)
-#define UTIME_MAXYEAR (2038)
-#define UTIME_MAXMONTH (01)
-#define UTIME_MAXDAY (18)
-
-#define IS_VALID_UTIME(y,m,d) (((y > UTIME_MINYEAR) \
- || ((y == UTIME_MINYEAR) && ((m > UTIME_MINMONTH) \
- || ((m == UTIME_MINMONTH) && (d >= UTIME_MINDAY))))) \
- && ((y < UTIME_MAXYEAR) \
- || ((y == UTIME_MAXYEAR) && ((m < UTIME_MAXMONTH) \
- || ((m == UTIME_MAXMONTH) && (d <= UTIME_MAXDAY))))))
-
-
-#endif
-
-
static double time2t(const int hour, const int min, const double sec);
@@ -1334,21 +1268,52 @@ interval_mi(Interval *span1, Interval *span2)
} /* interval_mi() */
Interval *
-interval_div(Interval *span1, float8 *arg2)
+interval_mul(Interval *span1, float8 *factor)
+{
+ Interval *result;
+ double months;
+
+ if ((!PointerIsValid(span1)) || (!PointerIsValid(factor)))
+ return NULL;
+
+ if (!PointerIsValid(result = palloc(sizeof(Interval))))
+ elog(ERROR, "Memory allocation failed, can't multiply interval");
+
+ months = (span1->month * *factor);
+ result->month = rint(months);
+ result->time = JROUND(span1->time * *factor);
+ /* evaluate fractional months as 30 days */
+ result->time += JROUND((months - result->month) * 30 * 86400);
+
+ return result;
+} /* interval_mul() */
+
+Interval *
+mul_d_interval(float8 *factor, Interval *span1)
+{
+ return interval_mul(span1, factor);
+} /* mul_d_interval() */
+
+Interval *
+interval_div(Interval *span1, float8 *factor)
{
Interval *result;
+ double months;
- if ((!PointerIsValid(span1)) || (!PointerIsValid(arg2)))
+ if ((!PointerIsValid(span1)) || (!PointerIsValid(factor)))
return NULL;
if (!PointerIsValid(result = palloc(sizeof(Interval))))
- elog(ERROR, "Memory allocation failed, can't divide intervals");
+ elog(ERROR, "Memory allocation failed, can't divide interval");
- if (*arg2 == 0.0)
+ if (*factor == 0.0)
elog(ERROR, "interval_div: divide by 0.0 error");
- result->month = rint(span1->month / *arg2);
- result->time = JROUND(span1->time / *arg2);
+ months = (span1->month / *factor);
+ result->month = rint(months);
+ result->time = JROUND(span1->time / *factor);
+ /* evaluate fractional months as 30 days */
+ result->time += JROUND((months - result->month) * 30 * 86400);
return result;
} /* interval_div() */