Add numeric <-> int8 and numeric <-> int2 conversion functions, as well

as a unary minus operator for numeric.  Now that long numeric constants
will get converted to NUMERIC in early parsing, it's essential to have
numeric->int8 conversion to avoid 'can't convert' errors on undecorated
int8 constants.  Threw in the rest for completeness while I was in the
area.
I did not force an initdb for this, since the system will still run
without the new pg_proc/pg_operator entries.  Possibly I should've.
This commit is contained in:
Tom Lane 2000-02-24 02:05:30 +00:00
parent 512669db9e
commit 9110b33f46
4 changed files with 267 additions and 94 deletions

View File

@ -5,7 +5,7 @@
*
* 1998 Jan Wieck
*
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.24 2000/01/20 02:21:44 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.25 2000/02/24 02:05:30 tgl Exp $
*
* ----------
*/
@ -357,7 +357,7 @@ numeric(Numeric num, int32 typmod)
/* ----------------------------------------------------------------------
*
* Rounding and the like
* Sign manipulation, rounding and the like
*
* ----------------------------------------------------------------------
*/
@ -395,6 +395,51 @@ numeric_abs(Numeric num)
}
Numeric
numeric_uminus(Numeric num)
{
Numeric res;
/* ----------
* Handle NULL
* ----------
*/
if (num == NULL)
return NULL;
/* ----------
* Handle NaN
* ----------
*/
if (NUMERIC_IS_NAN(num))
return make_result(&const_nan);
/* ----------
* Do it the easy way directly on the packed format
* ----------
*/
res = (Numeric) palloc(num->varlen);
memcpy(res, num, num->varlen);
/* ----------
* The packed format is known to be totally zero digit trimmed
* always. So we can identify a ZERO by the fact that there
* are no digits at all. Do nothing to a zero.
* ----------
*/
if (num->varlen != NUMERIC_HDRSZ)
{
/* Else, flip the sign */
if (NUMERIC_SIGN(num) == NUMERIC_POS)
res->n_sign_dscale = NUMERIC_NEG | NUMERIC_DSCALE(num);
else
res->n_sign_dscale = NUMERIC_POS | NUMERIC_DSCALE(num);
}
return res;
}
Numeric
numeric_sign(Numeric num)
{
@ -1465,7 +1510,7 @@ numeric_ln(Numeric num)
/* ----------
* numeric_ln() -
* numeric_log() -
*
* Compute the logarithm of x in a given base
* ----------
@ -1596,6 +1641,8 @@ numeric_power(Numeric num1, Numeric num2)
*
* ----------------------------------------------------------------------
*/
Numeric
int4_numeric(int32 val)
{
@ -1627,7 +1674,7 @@ numeric_int4(Numeric num)
return 0;
if (NUMERIC_IS_NAN(num))
return 0;
elog(ERROR, "Cannot convert NaN to int4");
/* ----------
* Get the number in the variable format so we can round to integer.
@ -1647,6 +1694,108 @@ numeric_int4(Numeric num)
}
Numeric
int8_numeric(int64 *val)
{
Numeric res;
NumericVar result;
char *tmp;
init_var(&result);
tmp = int8out(val);
set_var_from_str(tmp, &result);
res = make_result(&result);
free_var(&result);
pfree(tmp);
return res;
}
int64 *
numeric_int8(Numeric num)
{
NumericVar x;
char *str;
int64 *result;
if (num == NULL)
return NULL;
if (NUMERIC_IS_NAN(num))
elog(ERROR, "Cannot convert NaN to int8");
/* ----------
* Get the number in the variable format so we can round to integer.
* ----------
*/
init_var(&x);
set_var_from_num(num, &x);
str = get_str_from_var(&x, 0); /* dscale = 0 produces rounding */
free_var(&x);
result = int8in(str);
pfree(str);
return result;
}
Numeric
int2_numeric(int16 val)
{
Numeric res;
NumericVar result;
char *tmp;
init_var(&result);
tmp = int2out(val);
set_var_from_str(tmp, &result);
res = make_result(&result);
free_var(&result);
pfree(tmp);
return res;
}
int16
numeric_int2(Numeric num)
{
NumericVar x;
char *str;
int16 result;
if (num == NULL)
return 0;
if (NUMERIC_IS_NAN(num))
elog(ERROR, "Cannot convert NaN to int2");
/* ----------
* Get the number in the variable format so we can round to integer.
* ----------
*/
init_var(&x);
set_var_from_num(num, &x);
str = get_str_from_var(&x, 0); /* dscale = 0 produces rounding */
free_var(&x);
result = int2in(str);
pfree(str);
return result;
}
Numeric
float8_numeric(float64 val)
{

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pg_operator.h,v 1.69 2000/02/17 03:39:48 tgl Exp $
* $Id: pg_operator.h,v 1.70 2000/02/24 02:05:28 tgl Exp $
*
* NOTES
* the genbki.sh script reads this file and generates .bki
@ -682,6 +682,7 @@ DATA(insert OID = 1760 ( "*" PGUID 0 b t f 1700 1700 1700 1760 0 0 0 numeric
DATA(insert OID = 1761 ( "/" PGUID 0 b t f 1700 1700 1700 0 0 0 0 numeric_div - - ));
DATA(insert OID = 1762 ( "%" PGUID 0 b t f 1700 1700 1700 0 0 0 0 numeric_mod - - ));
DATA(insert OID = 1763 ( "@" PGUID 0 l t f 0 1700 1700 0 0 0 0 numeric_abs - - ));
DATA(insert OID = 1788 ( "-" PGUID 0 l t f 0 1700 1700 0 0 0 0 numeric_uminus - - ));
/*

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pg_proc.h,v 1.124 2000/02/21 03:36:55 tgl Exp $
* $Id: pg_proc.h,v 1.125 2000/02/24 02:05:27 tgl Exp $
*
* NOTES
* The script catalog/genbki.sh reads this file and generates .bki
@ -430,7 +430,7 @@ DESCR("addition");
DATA(insert OID = 205 ( float4mi PGUID 11 f t t 2 f 700 "700 700" 100 0 0 100 float4mi - ));
DESCR("subtract");
DATA(insert OID = 206 ( float4um PGUID 11 f t t 1 f 700 "700" 100 0 0 100 float4um - ));
DESCR("subtract");
DESCR("negate");
DATA(insert OID = 207 ( float4abs PGUID 11 f t t 1 f 700 "700" 100 0 0 100 float4abs - ));
DESCR("absolute value");
DATA(insert OID = 208 ( float4inc PGUID 11 f t t 1 f 700 "700" 100 0 0 100 float4inc - ));
@ -441,9 +441,9 @@ DATA(insert OID = 211 ( float4smaller PGUID 11 f t t 2 f 700 "700 700" 100 0
DESCR("smaller of two");
DATA(insert OID = 212 ( int4um PGUID 11 f t t 1 f 23 "23" 100 0 0 100 int4um - ));
DESCR("subtract");
DESCR("negate");
DATA(insert OID = 213 ( int2um PGUID 11 f t t 1 f 21 "21" 100 0 0 100 int2um - ));
DESCR("subtract");
DESCR("negate");
DATA(insert OID = 214 ( float8in PGUID 11 f t t 1 f 701 "0" 100 0 0 100 float8in - ));
DESCR("(internal)");
@ -458,7 +458,7 @@ DESCR("addition");
DATA(insert OID = 219 ( float8mi PGUID 11 f t t 2 f 701 "701 701" 100 0 0 100 float8mi - ));
DESCR("subtract");
DATA(insert OID = 220 ( float8um PGUID 11 f t t 1 f 701 "701" 100 0 0 100 float8um - ));
DESCR("subtract");
DESCR("negate");
DATA(insert OID = 221 ( float8abs PGUID 11 f t t 1 f 701 "701" 100 0 0 100 float8abs - ));
DESCR("absolute value");
DATA(insert OID = 222 ( float8inc PGUID 11 f t t 1 f 701 "701" 100 0 0 100 float8inc - ));
@ -847,7 +847,7 @@ DESCR("(internal)");
DATA(insert OID = 461 ( int8out PGUID 11 f t t 1 f 23 "0" 100 0 0 100 int8out - ));
DESCR("(internal)");
DATA(insert OID = 462 ( int8um PGUID 11 f t t 1 f 20 "20" 100 0 0 100 int8um - ));
DESCR("unary minus");
DESCR("negate");
DATA(insert OID = 463 ( int8pl PGUID 11 f t t 2 f 20 "20 20" 100 0 0 100 int8pl - ));
DESCR("addition");
DATA(insert OID = 464 ( int8mi PGUID 11 f t t 2 f 20 "20 20" 100 0 0 100 int8mi - ));
@ -2333,6 +2333,24 @@ DATA(insert OID = 1767 ( numeric_larger PGUID 11 f t t 2 f 1700 "1700 1700" 10
DESCR("larger of two numbers");
DATA(insert OID = 1769 ( numeric_cmp PGUID 11 f t t 2 f 23 "1700 1700" 100 0 0 100 numeric_cmp - ));
DESCR("compare two numbers");
DATA(insert OID = 1771 ( numeric_uminus PGUID 11 f t t 1 f 1700 "1700" 100 0 0 100 numeric_uminus - ));
DESCR("negate");
DATA(insert OID = 1779 ( int8_numeric PGUID 11 f t t 1 f 1700 "20" 100 0 0 100 int8_numeric - ));
DESCR("(internal)");
DATA(insert OID = 1781 ( numeric PGUID 11 f t t 1 f 1700 "20" 100 0 0 100 int8_numeric - ));
DESCR("(internal)");
DATA(insert OID = 1782 ( numeric_int8 PGUID 11 f t t 1 f 20 "1700" 100 0 0 100 numeric_int8 - ));
DESCR("(internal)");
DATA(insert OID = 1783 ( int8 PGUID 11 f t t 1 f 20 "1700" 100 0 0 100 numeric_int8 - ));
DESCR("(internal)");
DATA(insert OID = 1784 ( int2_numeric PGUID 11 f t t 1 f 1700 "21" 100 0 0 100 int2_numeric - ));
DESCR("(internal)");
DATA(insert OID = 1785 ( numeric PGUID 11 f t t 1 f 1700 "21" 100 0 0 100 int2_numeric - ));
DESCR("(internal)");
DATA(insert OID = 1786 ( numeric_int2 PGUID 11 f t t 1 f 21 "1700" 100 0 0 100 numeric_int2 - ));
DESCR("(internal)");
DATA(insert OID = 1787 ( int2 PGUID 11 f t t 1 f 21 "1700" 100 0 0 100 numeric_int2 - ));
DESCR("(internal)");
/* formatting */
DATA(insert OID = 1770 ( to_char PGUID 11 f t f 2 f 25 "1184 25" 100 0 0 100 timestamp_to_char - ));
@ -2349,7 +2367,7 @@ DATA(insert OID = 1776 ( to_char PGUID 11 f t f 2 f 25 "701 25" 100 0 0 100
DESCR("convert / formatting float8 to text");
DATA(insert OID = 1777 ( to_number PGUID 11 f t f 2 f 1700 "25 25" 100 0 0 100 numeric_to_number - ));
DESCR("convert text to numeric");
DATA(insert OID = 1778 ( to_timestamp PGUID 11 f t f 2 f 1184 "25 25" 100 0 0 100 to_timestamp - ));
DATA(insert OID = 1778 ( to_timestamp PGUID 11 f t f 2 f 1184 "25 25" 100 0 0 100 to_timestamp - ));
DESCR("convert text to timestamp");
DATA(insert OID = 1780 ( to_date PGUID 11 f t f 2 f 1082 "25 25" 100 0 0 100 to_date - ));
DESCR("convert text to date");

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: builtins.h,v 1.103 2000/02/21 03:36:59 tgl Exp $
* $Id: builtins.h,v 1.104 2000/02/24 02:05:24 tgl Exp $
*
* NOTES
* This should normally only be included by fmgr.h.
@ -474,97 +474,102 @@ extern text *translate(text *string, char from, char to);
/* acl.c */
/* inet_net_ntop.c */
char *inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size);
char *inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size);
extern char *inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size);
extern char *inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size);
/* inet_net_pton.c */
int inet_net_pton(int af, const char *src, void *dst, size_t size);
extern int inet_net_pton(int af, const char *src, void *dst, size_t size);
/* network.c */
inet *inet_in(char *str);
char *inet_out(inet *addr);
inet *cidr_in(char *str);
char *cidr_out(inet *addr);
bool network_lt(inet *a1, inet *a2);
bool network_le(inet *a1, inet *a2);
bool network_eq(inet *a1, inet *a2);
bool network_ge(inet *a1, inet *a2);
bool network_gt(inet *a1, inet *a2);
bool network_ne(inet *a1, inet *a2);
bool network_sub(inet *a1, inet *a2);
bool network_subeq(inet *a1, inet *a2);
bool network_sup(inet *a1, inet *a2);
bool network_supeq(inet *a1, inet *a2);
int4 network_cmp(inet *a1, inet *a2);
extern inet *inet_in(char *str);
extern char *inet_out(inet *addr);
extern inet *cidr_in(char *str);
extern char *cidr_out(inet *addr);
extern bool network_lt(inet *a1, inet *a2);
extern bool network_le(inet *a1, inet *a2);
extern bool network_eq(inet *a1, inet *a2);
extern bool network_ge(inet *a1, inet *a2);
extern bool network_gt(inet *a1, inet *a2);
extern bool network_ne(inet *a1, inet *a2);
extern bool network_sub(inet *a1, inet *a2);
extern bool network_subeq(inet *a1, inet *a2);
extern bool network_sup(inet *a1, inet *a2);
extern bool network_supeq(inet *a1, inet *a2);
extern int4 network_cmp(inet *a1, inet *a2);
text *network_network(inet *addr);
text *network_netmask(inet *addr);
int4 network_masklen(inet *addr);
text *network_broadcast(inet *addr);
text *network_host(inet *addr);
extern text *network_network(inet *addr);
extern text *network_netmask(inet *addr);
extern int4 network_masklen(inet *addr);
extern text *network_broadcast(inet *addr);
extern text *network_host(inet *addr);
/* mac.c */
macaddr *macaddr_in(char *str);
char *macaddr_out(macaddr *addr);
bool macaddr_lt(macaddr *a1, macaddr *a2);
bool macaddr_le(macaddr *a1, macaddr *a2);
bool macaddr_eq(macaddr *a1, macaddr *a2);
bool macaddr_ge(macaddr *a1, macaddr *a2);
bool macaddr_gt(macaddr *a1, macaddr *a2);
bool macaddr_ne(macaddr *a1, macaddr *a2);
int4 macaddr_cmp(macaddr *a1, macaddr *a2);
text *macaddr_manuf(macaddr *addr);
extern macaddr *macaddr_in(char *str);
extern char *macaddr_out(macaddr *addr);
extern bool macaddr_lt(macaddr *a1, macaddr *a2);
extern bool macaddr_le(macaddr *a1, macaddr *a2);
extern bool macaddr_eq(macaddr *a1, macaddr *a2);
extern bool macaddr_ge(macaddr *a1, macaddr *a2);
extern bool macaddr_gt(macaddr *a1, macaddr *a2);
extern bool macaddr_ne(macaddr *a1, macaddr *a2);
extern int4 macaddr_cmp(macaddr *a1, macaddr *a2);
extern text *macaddr_manuf(macaddr *addr);
/* numeric.c */
Numeric numeric_in(char *str, int dummy, int32 typmod);
char *numeric_out(Numeric num);
Numeric numeric(Numeric num, int32 typmod);
Numeric numeric_abs(Numeric num);
Numeric numeric_sign(Numeric num);
Numeric numeric_round(Numeric num, int32 scale);
Numeric numeric_trunc(Numeric num, int32 scale);
Numeric numeric_ceil(Numeric num);
Numeric numeric_floor(Numeric num);
int32 numeric_cmp(Numeric num1, Numeric num2);
bool numeric_eq(Numeric num1, Numeric num2);
bool numeric_ne(Numeric num1, Numeric num2);
bool numeric_gt(Numeric num1, Numeric num2);
bool numeric_ge(Numeric num1, Numeric num2);
bool numeric_lt(Numeric num1, Numeric num2);
bool numeric_le(Numeric num1, Numeric num2);
Numeric numeric_add(Numeric num1, Numeric num2);
Numeric numeric_sub(Numeric num1, Numeric num2);
Numeric numeric_mul(Numeric num1, Numeric num2);
Numeric numeric_div(Numeric num1, Numeric num2);
Numeric numeric_mod(Numeric num1, Numeric num2);
Numeric numeric_inc(Numeric num);
Numeric numeric_dec(Numeric num);
Numeric numeric_smaller(Numeric num1, Numeric num2);
Numeric numeric_larger(Numeric num1, Numeric num2);
Numeric numeric_sqrt(Numeric num);
Numeric numeric_exp(Numeric num);
Numeric numeric_ln(Numeric num);
Numeric numeric_log(Numeric num1, Numeric num2);
Numeric numeric_power(Numeric num1, Numeric num2);
Numeric int4_numeric(int32 val);
int32 numeric_int4(Numeric num);
Numeric float4_numeric(float32 val);
float32 numeric_float4(Numeric num);
Numeric float8_numeric(float64 val);
float64 numeric_float8(Numeric num);
extern Numeric numeric_in(char *str, int dummy, int32 typmod);
extern char *numeric_out(Numeric num);
extern Numeric numeric(Numeric num, int32 typmod);
extern Numeric numeric_abs(Numeric num);
extern Numeric numeric_uminus(Numeric num);
extern Numeric numeric_sign(Numeric num);
extern Numeric numeric_round(Numeric num, int32 scale);
extern Numeric numeric_trunc(Numeric num, int32 scale);
extern Numeric numeric_ceil(Numeric num);
extern Numeric numeric_floor(Numeric num);
extern int32 numeric_cmp(Numeric num1, Numeric num2);
extern bool numeric_eq(Numeric num1, Numeric num2);
extern bool numeric_ne(Numeric num1, Numeric num2);
extern bool numeric_gt(Numeric num1, Numeric num2);
extern bool numeric_ge(Numeric num1, Numeric num2);
extern bool numeric_lt(Numeric num1, Numeric num2);
extern bool numeric_le(Numeric num1, Numeric num2);
extern Numeric numeric_add(Numeric num1, Numeric num2);
extern Numeric numeric_sub(Numeric num1, Numeric num2);
extern Numeric numeric_mul(Numeric num1, Numeric num2);
extern Numeric numeric_div(Numeric num1, Numeric num2);
extern Numeric numeric_mod(Numeric num1, Numeric num2);
extern Numeric numeric_inc(Numeric num);
extern Numeric numeric_dec(Numeric num);
extern Numeric numeric_smaller(Numeric num1, Numeric num2);
extern Numeric numeric_larger(Numeric num1, Numeric num2);
extern Numeric numeric_sqrt(Numeric num);
extern Numeric numeric_exp(Numeric num);
extern Numeric numeric_ln(Numeric num);
extern Numeric numeric_log(Numeric num1, Numeric num2);
extern Numeric numeric_power(Numeric num1, Numeric num2);
extern Numeric int4_numeric(int32 val);
extern int32 numeric_int4(Numeric num);
extern Numeric int8_numeric(int64 *val);
extern int64 *numeric_int8(Numeric num);
extern Numeric int2_numeric(int16 val);
extern int16 numeric_int2(Numeric num);
extern Numeric float4_numeric(float32 val);
extern float32 numeric_float4(Numeric num);
extern Numeric float8_numeric(float64 val);
extern float64 numeric_float8(Numeric num);
/* ri_triggers.c */
HeapTuple RI_FKey_check_ins(FmgrInfo *proinfo);
HeapTuple RI_FKey_check_upd(FmgrInfo *proinfo);
HeapTuple RI_FKey_noaction_del(FmgrInfo *proinfo);
HeapTuple RI_FKey_noaction_upd(FmgrInfo *proinfo);
HeapTuple RI_FKey_cascade_del(FmgrInfo *proinfo);
HeapTuple RI_FKey_cascade_upd(FmgrInfo *proinfo);
HeapTuple RI_FKey_restrict_del(FmgrInfo *proinfo);
HeapTuple RI_FKey_restrict_upd(FmgrInfo *proinfo);
HeapTuple RI_FKey_setnull_del(FmgrInfo *proinfo);
HeapTuple RI_FKey_setnull_upd(FmgrInfo *proinfo);
HeapTuple RI_FKey_setdefault_del(FmgrInfo *proinfo);
HeapTuple RI_FKey_setdefault_upd(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_check_ins(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_check_upd(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_noaction_del(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_noaction_upd(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_cascade_del(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_cascade_upd(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_restrict_del(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_restrict_upd(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_setnull_del(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_setnull_upd(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_setdefault_del(FmgrInfo *proinfo);
extern HeapTuple RI_FKey_setdefault_upd(FmgrInfo *proinfo);
#endif /* BUILTINS_H */