Skip to content

Commit 643c7be

Browse files
author
Thomas G. Lockhart
committed
Add text<->float8 and text<->float4 conversion functions.
This will fix the problem reported by Jose' Soares when trying to cast a float to text.
1 parent 8d507c2 commit 643c7be

File tree

3 files changed

+130
-17
lines changed

3 files changed

+130
-17
lines changed

src/backend/utils/adt/float.c

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.33 1998/09/01 04:32:32 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.34 1998/11/17 14:36:44 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -926,6 +926,96 @@ i2tof(int16 num)
926926
}
927927

928928

929+
/*
930+
* float8_text - converts a float8 number to a text string
931+
*/
932+
text *
933+
float8_text(float64 num)
934+
{
935+
text *result;
936+
int len;
937+
char *str;
938+
939+
str = float8out(num);
940+
len = (strlen(str)+VARHDRSZ);
941+
942+
result = palloc(len);
943+
944+
VARSIZE(result) = len;
945+
memmove(VARDATA(result), str, (len - VARHDRSZ));
946+
947+
pfree(str);
948+
return result;
949+
} /* float8_text() */
950+
951+
952+
/*
953+
* text_float8 - converts a text string to a float8 number
954+
*/
955+
float64
956+
text_float8(text *string)
957+
{
958+
float64 result;
959+
int len;
960+
char *str;
961+
962+
len = (VARSIZE(string) - VARHDRSZ);
963+
str = palloc(len + 1);
964+
memmove(str, VARDATA(string), len);
965+
*(str + len) = '\0';
966+
967+
result = float8in(str);
968+
pfree(str);
969+
970+
return result;
971+
} /* text_float8() */
972+
973+
974+
/*
975+
* float4_text - converts a float4 number to a text string
976+
*/
977+
text *
978+
float4_text(float32 num)
979+
{
980+
text *result;
981+
int len;
982+
char *str;
983+
984+
str = float4out(num);
985+
len = (strlen(str)+VARHDRSZ);
986+
987+
result = palloc(len);
988+
989+
VARSIZE(result) = len;
990+
memmove(VARDATA(result), str, (len - VARHDRSZ));
991+
992+
pfree(str);
993+
return result;
994+
} /* float4_text() */
995+
996+
997+
/*
998+
* text_float4 - converts a text string to a float4 number
999+
*/
1000+
float32
1001+
text_float4(text *string)
1002+
{
1003+
float32 result;
1004+
int len;
1005+
char *str;
1006+
1007+
len = (VARSIZE(string) - VARHDRSZ);
1008+
str = palloc(len + 1);
1009+
memmove(str, VARDATA(string), len);
1010+
*(str + len) = '\0';
1011+
1012+
result = float4in(str);
1013+
pfree(str);
1014+
1015+
return result;
1016+
} /* text_float4() */
1017+
1018+
9291019
/*
9301020
* =======================
9311021
* RANDOM FLOAT8 OPERATORS

src/include/catalog/pg_proc.h

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: pg_proc.h,v 1.83 1998/10/29 19:03:50 tgl Exp $
9+
* $Id: pg_proc.h,v 1.84 1998/11/17 14:36:51 thomas Exp $
1010
*
1111
* NOTES
1212
* The script catalog/genbki.sh reads this file and generates .bki
@@ -1067,16 +1067,14 @@ DESCR("convert");
10671067
DATA(insert OID = 819 ( text_int4 PGUID 11 f t f 1 f 23 "25" 100 0 0 100 foo bar));
10681068
DESCR("convert");
10691069

1070-
DATA(insert OID = 849 ( textpos PGUID 11 f t f 2 f 23 "25 25" 100 0 1 0 foo bar ));
1071-
DESCR("return position of substring");
1072-
DATA(insert OID = 850 ( textlike PGUID 11 f t f 2 f 16 "25 25" 100 0 1 0 foo bar ));
1073-
DESCR("matches LIKE expression");
1074-
DATA(insert OID = 851 ( textnlike PGUID 11 f t f 2 f 16 "25 25" 100 0 1 0 foo bar ));
1075-
DESCR("does not match LIKE expression");
1076-
DATA(insert OID = 858 ( namelike PGUID 11 f t f 2 f 16 "19 25" 100 0 0 100 foo bar ));
1077-
DESCR("matches LIKE expression");
1078-
DATA(insert OID = 859 ( namenlike PGUID 11 f t f 2 f 16 "19 25" 100 0 0 100 foo bar ));
1079-
DESCR("does not match LIKE expression");
1070+
DATA(insert OID = 838 ( text_float8 PGUID 11 f t f 1 f 701 "25" 100 0 0 100 foo bar));
1071+
DESCR("convert");
1072+
DATA(insert OID = 839 ( text_float4 PGUID 11 f t f 1 f 700 "25" 100 0 0 100 foo bar));
1073+
DESCR("convert");
1074+
DATA(insert OID = 840 ( float8_text PGUID 11 f t f 1 f 25 "701" 100 0 0 100 foo bar));
1075+
DESCR("convert");
1076+
DATA(insert OID = 841 ( float4_text PGUID 11 f t f 1 f 25 "700" 100 0 0 100 foo bar));
1077+
DESCR("convert");
10801078

10811079
DATA(insert OID = 846 ( cash_mul_flt4 PGUID 11 f t f 2 f 790 "790 700" 100 0 0 100 foo bar ));
10821080
DESCR("multiply");
@@ -1085,6 +1083,13 @@ DESCR("divide");
10851083
DATA(insert OID = 848 ( flt4_mul_cash PGUID 11 f t f 2 f 790 "700 790" 100 0 0 100 foo bar ));
10861084
DESCR("multiply");
10871085

1086+
DATA(insert OID = 849 ( textpos PGUID 11 f t f 2 f 23 "25 25" 100 0 1 0 foo bar ));
1087+
DESCR("return position of substring");
1088+
DATA(insert OID = 850 ( textlike PGUID 11 f t f 2 f 16 "25 25" 100 0 1 0 foo bar ));
1089+
DESCR("matches LIKE expression");
1090+
DATA(insert OID = 851 ( textnlike PGUID 11 f t f 2 f 16 "25 25" 100 0 1 0 foo bar ));
1091+
DESCR("does not match LIKE expression");
1092+
10881093
DATA(insert OID = 852 ( int48eq PGUID 11 f t f 2 f 16 "21 20" 100 0 0 100 foo bar ));
10891094
DESCR("equal");
10901095
DATA(insert OID = 853 ( int48ne PGUID 11 f t f 2 f 16 "21 20" 100 0 0 100 foo bar ));
@@ -1098,6 +1103,11 @@ DESCR("less-than-or-equal");
10981103
DATA(insert OID = 857 ( int48ge PGUID 11 f t f 2 f 16 "21 20" 100 0 0 100 foo bar ));
10991104
DESCR("greater-than-or-equal");
11001105

1106+
DATA(insert OID = 858 ( namelike PGUID 11 f t f 2 f 16 "19 25" 100 0 0 100 foo bar ));
1107+
DESCR("matches LIKE expression");
1108+
DATA(insert OID = 859 ( namenlike PGUID 11 f t f 2 f 16 "19 25" 100 0 0 100 foo bar ));
1109+
DESCR("does not match LIKE expression");
1110+
11011111
DATA(insert OID = 860 ( char_bpchar PGUID 11 f t f 1 f 1042 "18" 100 0 0 100 foo bar ));
11021112
DESCR("convert to bpchar");
11031113
DATA(insert OID = 861 ( bpchar_char PGUID 11 f t f 1 f 18 "1042" 100 0 0 100 foo bar ));
@@ -1981,13 +1991,22 @@ DESCR("convert int8 to float8");
19811991
/* OIDS 1600 - 1699 */
19821992

19831993
DATA(insert OID = 1600 ( line PGUID 14 f t f 2 f 628 "600 600" 100 0 0 100 "select line_construct_pp($1, $2)" - ));
1984-
DESCR("");
1994+
DESCR("points to line");
19851995
DATA(insert OID = 1601 ( ishorizontal PGUID 14 f t f 1 f 16 "628" 100 0 0 100 "select line_horizontal($1)" - ));
1986-
DESCR("");
1996+
DESCR("is line horizontal?");
19871997
DATA(insert OID = 1602 ( isvertical PGUID 14 f t f 1 f 16 "628" 100 0 0 100 "select line_vertical($1)" - ));
1988-
DESCR("");
1998+
DESCR("is line vertical?");
19891999
DATA(insert OID = 1603 ( isparallel PGUID 14 f t f 2 f 16 "628 628" 100 0 0 100 "select line_parallel($1, $2)" - ));
1990-
DESCR("");
2000+
DESCR("are lines parallel?");
2001+
2002+
DATA(insert OID = 1604 ( float8 PGUID 14 f t f 1 f 701 "25" 100 0 0 100 "select text_float8($1)" - ));
2003+
DESCR("convert");
2004+
DATA(insert OID = 1605 ( float4 PGUID 14 f t f 1 f 700 "25" 100 0 0 100 "select text_float4($1)" - ));
2005+
DESCR("convert");
2006+
DATA(insert OID = 1606 ( text PGUID 14 f t f 1 f 25 "701" 100 0 0 100 "select float8_text($1)" - ));
2007+
DESCR("convert");
2008+
DATA(insert OID = 1607 ( text PGUID 14 f t f 1 f 25 "700" 100 0 0 100 "select float4_text($1)" - ));
2009+
DESCR("convert");
19912010

19922011
/* Oracle Compatibility Related Functions - By Edmund Mergl <E.Mergl@bawue.de> */
19932012
DATA(insert OID = 868 ( strpos PGUID 14 f t f 2 f 23 "25 25" 100 0 0 100 "select textpos($1, $2)" - ));

src/include/utils/builtins.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: builtins.h,v 1.69 1998/10/29 18:07:09 momjian Exp $
9+
* $Id: builtins.h,v 1.70 1998/11/17 14:36:37 thomas Exp $
1010
*
1111
* NOTES
1212
* This should normally only be included by fmgr.h.
@@ -280,6 +280,10 @@ extern float32 i4tof(int32 num);
280280
extern float32 i2tof(int16 num);
281281
extern int32 ftoi4(float32 num);
282282
extern int16 ftoi2(float32 num);
283+
extern float64 text_float8(text *str);
284+
extern float32 text_float4(text *str);
285+
extern text *float8_text(float64 num);
286+
extern text *float4_text(float32 num);
283287
extern float64 dround(float64 arg1);
284288
extern float64 dtrunc(float64 arg1);
285289
extern float64 dsqrt(float64 arg1);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy