Change ln(), log(), power(), and sqrt() to emit the correct SQLSTATE
authorNeil Conway
Sun, 16 May 2004 23:18:55 +0000 (23:18 +0000)
committerNeil Conway
Sun, 16 May 2004 23:18:55 +0000 (23:18 +0000)
error codes for certain error conditions, as specified by SQL2003.

doc/src/sgml/errcodes.sgml
src/backend/utils/adt/float.c
src/backend/utils/adt/numeric.c
src/include/utils/errcodes.h

index 295c41ffad8031f69be1a1e390c0f719aed76e62..5d1a770bf87f1c47e7e91d3640c641dcab947387 100644 (file)
@@ -1,4 +1,4 @@
-
+
 
 
  <productname>PostgreSQL</productname> Error Codes
 INTERVAL FIELD OVERFLOW
 
 
+
+2201E
+INVALID ARGUMENT FOR LOGARITHM
+
+
+
+2201F
+INVALID ARGUMENT FOR POWER FUNCTION
+
+
 
 2201G
 INVALID ARGUMENT FOR WIDTH BUCKET FUNCTION
index c48af109e3ea25f914b9819972721e1fa97414e2..bef89f38c7078096b27f2848215df2bcba126c79 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.104 2004/05/07 00:24:58 tgl Exp $
+ *   $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.105 2004/05/16 23:18:55 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1415,7 +1415,7 @@ dsqrt(PG_FUNCTION_ARGS)
 
    if (arg1 < 0)
        ereport(ERROR,
-               (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
+               (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
                 errmsg("cannot take square root of a negative number")));
 
    result = sqrt(arg1);
@@ -1449,6 +1449,16 @@ dpow(PG_FUNCTION_ARGS)
    float8      arg2 = PG_GETARG_FLOAT8(1);
    float8      result;
 
+   /*
+    * The SQL spec requires that we emit a particular SQLSTATE error
+    * code for certain error conditions.
+    */
+   if ((arg1 == 0 && arg2 < 0) ||
+       (arg1 < 0 && floor(arg2) != arg2))
+       ereport(ERROR,
+               (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
+                errmsg("invalid argument for power function")));
+
    /*
     * We must check both for errno getting set and for a NaN result, in
     * order to deal with the vagaries of different platforms...
@@ -1501,7 +1511,6 @@ dexp(PG_FUNCTION_ARGS)
 
 /*
  *     dlog1           - returns the natural logarithm of arg1
- *                       ("dlog" is already a logging routine...)
  */
 Datum
 dlog1(PG_FUNCTION_ARGS)
@@ -1509,14 +1518,17 @@ dlog1(PG_FUNCTION_ARGS)
    float8      arg1 = PG_GETARG_FLOAT8(0);
    float8      result;
 
+   /*
+    * Emit particular SQLSTATE error codes for ln(). This is required
+    * by the SQL standard.
+    */
    if (arg1 == 0.0)
        ereport(ERROR,
-               (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
+               (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
                 errmsg("cannot take logarithm of zero")));
-
    if (arg1 < 0)
        ereport(ERROR,
-               (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
+               (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
                 errmsg("cannot take logarithm of a negative number")));
 
    result = log(arg1);
@@ -1535,14 +1547,19 @@ dlog10(PG_FUNCTION_ARGS)
    float8      arg1 = PG_GETARG_FLOAT8(0);
    float8      result;
 
+   /*
+    * Emit particular SQLSTATE error codes for log(). The SQL spec
+    * doesn't define log(), but it does define ln(), so it makes
+    * sense to emit the same error code for an analogous error
+    * condition.
+    */
    if (arg1 == 0.0)
        ereport(ERROR,
-               (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
+               (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
                 errmsg("cannot take logarithm of zero")));
-
    if (arg1 < 0)
        ereport(ERROR,
-               (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
+               (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
                 errmsg("cannot take logarithm of a negative number")));
 
    result = log10(arg1);
index 4214d7af0079955fff661ebaee631d9030370662..9307ba5e6ecfaca4b0b89b8c1333818be80e3834 100644 (file)
@@ -14,7 +14,7 @@
  * Copyright (c) 1998-2003, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.74 2004/05/14 21:42:28 neilc Exp $
+ *   $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.75 2004/05/16 23:18:55 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1668,6 +1668,7 @@ numeric_power(PG_FUNCTION_ARGS)
    Numeric     res;
    NumericVar  arg1;
    NumericVar  arg2;
+   NumericVar  arg2_trunc;
    NumericVar  result;
 
    /*
@@ -1681,10 +1682,26 @@ numeric_power(PG_FUNCTION_ARGS)
     */
    init_var(&arg1);
    init_var(&arg2);
+   init_var(&arg2_trunc);
    init_var(&result);
 
    set_var_from_num(num1, &arg1);
    set_var_from_num(num2, &arg2);
+   set_var_from_var(&arg2, &arg2_trunc);
+
+   trunc_var(&arg2_trunc, 0);
+
+   /*
+    * Return special SQLSTATE error codes for a few conditions
+    * mandated by the standard.
+    */
+   if ((cmp_var(&arg1, &const_zero) == 0 &&
+        cmp_var(&arg2, &const_zero) < 0) ||
+       (cmp_var(&arg1, &const_zero) < 0 &&
+        cmp_var(&arg2, &arg2_trunc) != 0))
+       ereport(ERROR,
+               (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
+                errmsg("invalid argument for power function")));
 
    /*
     * Call power_var() to compute and return the result; note it handles
@@ -1696,6 +1713,7 @@ numeric_power(PG_FUNCTION_ARGS)
 
    free_var(&result);
    free_var(&arg2);
+   free_var(&arg2_trunc);
    free_var(&arg1);
 
    PG_RETURN_NUMERIC(res);
@@ -4408,10 +4426,16 @@ ln_var(NumericVar *arg, NumericVar *result, int rscale)
    NumericVar  elem;
    NumericVar  fact;
    int         local_rscale;
+   int         cmp;
 
-   if (cmp_var(arg, &const_zero) <= 0)
+   cmp = cmp_var(arg, &const_zero);
+   if (cmp == 0)
        ereport(ERROR,
-               (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
+               (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
+                errmsg("cannot take logarithm of zero")));
+   else if (cmp < 0)
+       ereport(ERROR,
+               (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
                 errmsg("cannot take logarithm of a negative number")));
 
    local_rscale = rscale + 8;
index ed1b4e413d8c05dd88d32c6345e54c62e5dfc278..cdee2737f0ddd1d8071c9f44eacd81cd31c9a06d 100644 (file)
@@ -11,7 +11,7 @@
  *
  * Copyright (c) 2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/include/utils/errcodes.h,v 1.10 2004/05/14 21:42:30 neilc Exp $
+ * $PostgreSQL: pgsql/src/include/utils/errcodes.h,v 1.11 2004/05/16 23:18:55 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
 #define ERRCODE_ESCAPE_CHARACTER_CONFLICT  MAKE_SQLSTATE('2','2', '0','0','B')
 #define ERRCODE_INDICATOR_OVERFLOW         MAKE_SQLSTATE('2','2', '0','2','2')
 #define ERRCODE_INTERVAL_FIELD_OVERFLOW        MAKE_SQLSTATE('2','2', '0','1','5')
-#define ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION     MAKE_SQLSTATE('2','2', '0', '1', 'G')
+#define ERRCODE_INVALID_ARGUMENT_FOR_LOG   MAKE_SQLSTATE('2','2', '0','1','E')
+#define ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION    MAKE_SQLSTATE('2','2', '0', '1', 'F')
+#define ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION MAKE_SQLSTATE('2','2', '0', '1', 'G')
 #define ERRCODE_INVALID_CHARACTER_VALUE_FOR_CAST       MAKE_SQLSTATE('2','2', '0','1','8')
 #define ERRCODE_INVALID_DATETIME_FORMAT        MAKE_SQLSTATE('2','2', '0','0','7')
 #define ERRCODE_INVALID_ESCAPE_CHARACTER   MAKE_SQLSTATE('2','2', '0','1','9')