Skip to content

Commit 8a4dc94

Browse files
committed
Make details of the Numeric representation private to numeric.c.
Review by Tom Lane.
1 parent f223bb7 commit 8a4dc94

File tree

4 files changed

+75
-50
lines changed

4 files changed

+75
-50
lines changed

contrib/btree_gist/btree_numeric.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $PostgreSQL: pgsql/contrib/btree_gist/btree_numeric.c,v 1.13 2009/06/11 14:48:50 momjian Exp $
2+
* $PostgreSQL: pgsql/contrib/btree_gist/btree_numeric.c,v 1.14 2010/07/30 04:30:23 rhaas Exp $
33
*/
44
#include "btree_gist.h"
55

@@ -185,9 +185,9 @@ gbt_numeric_penalty(PG_FUNCTION_ARGS)
185185
NumericGetDatum(os)
186186
));
187187

188-
if (NUMERIC_IS_NAN(us))
188+
if (numeric_is_nan(us))
189189
{
190-
if (NUMERIC_IS_NAN(os))
190+
if (numeric_is_nan(os))
191191
*result = 0.0;
192192
else
193193
*result = 1.0;

src/backend/utils/adt/format_type.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/format_type.c,v 1.53 2010/02/14 18:42:16 rhaas Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/format_type.c,v 1.54 2010/07/30 04:30:23 rhaas Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -387,15 +387,7 @@ type_maximum_size(Oid type_oid, int32 typemod)
387387
+ VARHDRSZ;
388388

389389
case NUMERICOID:
390-
/* precision (ie, max # of digits) is in upper bits of typmod */
391-
if (typemod > VARHDRSZ)
392-
{
393-
int precision = ((typemod - VARHDRSZ) >> 16) & 0xffff;
394-
395-
/* Numeric stores 2 decimal digits/byte, plus header */
396-
return (precision + 1) / 2 + NUMERIC_HDRSZ;
397-
}
398-
break;
390+
return numeric_maximum_size(typemod);
399391

400392
case VARBITOID:
401393
case BITOID:

src/backend/utils/adt/numeric.c

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Copyright (c) 1998-2010, PostgreSQL Global Development Group
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.123 2010/02/26 02:01:09 momjian Exp $
17+
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.124 2010/07/30 04:30:23 rhaas Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -35,6 +35,38 @@
3535
#include "utils/int8.h"
3636
#include "utils/numeric.h"
3737

38+
/*
39+
* Sign values and macros to deal with packing/unpacking n_sign_dscale
40+
*/
41+
#define NUMERIC_SIGN_MASK 0xC000
42+
#define NUMERIC_POS 0x0000
43+
#define NUMERIC_NEG 0x4000
44+
#define NUMERIC_NAN 0xC000
45+
#define NUMERIC_DSCALE_MASK 0x3FFF
46+
#define NUMERIC_SIGN(n) ((n)->n_sign_dscale & NUMERIC_SIGN_MASK)
47+
#define NUMERIC_DSCALE(n) ((n)->n_sign_dscale & NUMERIC_DSCALE_MASK)
48+
#define NUMERIC_IS_NAN(n) (NUMERIC_SIGN(n) != NUMERIC_POS && \
49+
NUMERIC_SIGN(n) != NUMERIC_NEG)
50+
#define NUMERIC_HDRSZ (VARHDRSZ + sizeof(uint16) + sizeof(int16))
51+
52+
53+
/*
54+
* The Numeric data type stored in the database
55+
*
56+
* NOTE: by convention, values in the packed form have been stripped of
57+
* all leading and trailing zero digits (where a "digit" is of base NBASE).
58+
* In particular, if the value is zero, there will be no digits at all!
59+
* The weight is arbitrary in that case, but we normally set it to zero.
60+
*/
61+
struct NumericData
62+
{
63+
int32 vl_len_; /* varlena header (do not touch directly!) */
64+
uint16 n_sign_dscale; /* Sign + display scale */
65+
int16 n_weight; /* Weight of 1st digit */
66+
char n_data[1]; /* Digits (really array of NumericDigit) */
67+
};
68+
69+
3870
/* ----------
3971
* Uncomment the following to enable compilation of dump_numeric()
4072
* and dump_var() and to get a dump of any result produced by make_result().
@@ -427,6 +459,37 @@ numeric_out(PG_FUNCTION_ARGS)
427459
PG_RETURN_CSTRING(str);
428460
}
429461

462+
/*
463+
* numeric_is_nan() -
464+
*
465+
* Is Numeric value a NaN?
466+
*/
467+
bool
468+
numeric_is_nan(Numeric num)
469+
{
470+
return NUMERIC_IS_NAN(num);
471+
}
472+
473+
/*
474+
* numeric_maximum_size() -
475+
*
476+
* Maximum size of a numeric with given typmod, or -1 if unlimited/unknown.
477+
*/
478+
int32
479+
numeric_maximum_size(int32 typemod)
480+
{
481+
int precision;
482+
483+
if (typemod <= VARHDRSZ)
484+
return -1;
485+
486+
/* precision (ie, max # of digits) is in upper bits of typmod */
487+
precision = ((typemod - VARHDRSZ) >> 16) & 0xffff;
488+
489+
/* Numeric stores 2 decimal digits/byte, plus header */
490+
return (precision + 1) / 2 + NUMERIC_HDRSZ;
491+
}
492+
430493
/*
431494
* numeric_out_sci() -
432495
*

src/include/utils/numeric.h

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1998-2010, PostgreSQL Global Development Group
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/numeric.h,v 1.29 2010/01/02 16:58:10 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/numeric.h,v 1.30 2010/07/30 04:30:23 rhaas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -37,41 +37,9 @@
3737
*/
3838
#define NUMERIC_MIN_SIG_DIGITS 16
3939

40-
41-
/*
42-
* Sign values and macros to deal with packing/unpacking n_sign_dscale
43-
*/
44-
#define NUMERIC_SIGN_MASK 0xC000
45-
#define NUMERIC_POS 0x0000
46-
#define NUMERIC_NEG 0x4000
47-
#define NUMERIC_NAN 0xC000
48-
#define NUMERIC_DSCALE_MASK 0x3FFF
49-
#define NUMERIC_SIGN(n) ((n)->n_sign_dscale & NUMERIC_SIGN_MASK)
50-
#define NUMERIC_DSCALE(n) ((n)->n_sign_dscale & NUMERIC_DSCALE_MASK)
51-
#define NUMERIC_IS_NAN(n) (NUMERIC_SIGN(n) != NUMERIC_POS && \
52-
NUMERIC_SIGN(n) != NUMERIC_NEG)
53-
54-
55-
/*
56-
* The Numeric data type stored in the database
57-
*
58-
* NOTE: by convention, values in the packed form have been stripped of
59-
* all leading and trailing zero digits (where a "digit" is of base NBASE).
60-
* In particular, if the value is zero, there will be no digits at all!
61-
* The weight is arbitrary in that case, but we normally set it to zero.
62-
*/
63-
typedef struct NumericData
64-
{
65-
int32 vl_len_; /* varlena header (do not touch directly!) */
66-
uint16 n_sign_dscale; /* Sign + display scale */
67-
int16 n_weight; /* Weight of 1st digit */
68-
char n_data[1]; /* Digits (really array of NumericDigit) */
69-
} NumericData;
70-
71-
typedef NumericData *Numeric;
72-
73-
#define NUMERIC_HDRSZ (VARHDRSZ + sizeof(uint16) + sizeof(int16))
74-
40+
/* The actual contents of Numeric are private to numeric.c */
41+
struct NumericData;
42+
typedef struct NumericData *Numeric;
7543

7644
/*
7745
* fmgr interface macros
@@ -87,6 +55,8 @@ typedef NumericData *Numeric;
8755
/*
8856
* Utility functions in numeric.c
8957
*/
58+
extern bool numeric_is_nan(Numeric num);
59+
int32 numeric_maximum_size(int32 typemod);
9060
extern char *numeric_out_sci(Numeric num, int scale);
9161

9262
#endif /* _PG_NUMERIC_H_ */

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