1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-07 00:36:50 +03:00

Allow numeric scale to be negative or greater than precision.

Formerly, when specifying NUMERIC(precision, scale), the scale had to
be in the range [0, precision], which was per SQL spec. This commit
extends the range of allowed scales to [-1000, 1000], independent of
the precision (whose valid range remains [1, 1000]).

A negative scale implies rounding before the decimal point. For
example, a column might be declared with a scale of -3 to round values
to the nearest thousand. Note that the display scale remains
non-negative, so in this case the display scale will be zero, and all
digits before the decimal point will be displayed.

A scale greater than the precision supports fractional values with
zeros immediately after the decimal point.

Take the opportunity to tidy up the code that packs, unpacks and
validates the contents of a typmod integer, encapsulating it in a
small set of new inline functions.

Bump the catversion because the allowed contents of atttypmod have
changed for numeric columns. This isn't a change that requires a
re-initdb, but negative scale values in the typmod would confuse old
backends.

Dean Rasheed, with additional improvements by Tom Lane. Reviewed by
Tom Lane.

Discussion: https://postgr.es/m/CAEZATCWdNLgpKihmURF8nfofP0RFtAKJ7ktY6GcZOPnMfUoRqA@mail.gmail.com
This commit is contained in:
Dean Rasheed
2021-07-26 14:13:47 +01:00
parent efe0802209
commit 085f931f52
7 changed files with 251 additions and 43 deletions

View File

@ -17,12 +17,22 @@
#include "fmgr.h"
/*
* Limit on the precision (and hence scale) specifiable in a NUMERIC typmod.
* Note that the implementation limit on the length of a numeric value is
* much larger --- beware of what you use this for!
* Limits on the precision and scale specifiable in a NUMERIC typmod. The
* precision is strictly positive, but the scale may be positive or negative.
* A negative scale implies rounding before the decimal point.
*
* Note that the minimum display scale defined below is zero --- we always
* display all digits before the decimal point, even when the scale is
* negative.
*
* Note that the implementation limits on the precision and display scale of a
* numeric value are much larger --- beware of what you use these for!
*/
#define NUMERIC_MAX_PRECISION 1000
#define NUMERIC_MIN_SCALE (-1000)
#define NUMERIC_MAX_SCALE 1000
/*
* Internal limits on the scales chosen for calculation results
*/