~ubuntu-branches/ubuntu/trusty/postgresql-9.3/trusty-proposed

« back to all changes in this revision

Viewing changes to src/backend/utils/adt/numeric.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-07-24 16:13:59 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20140724161359-uk325qfv03euxuuh
Tags: 9.3.5-0ubuntu0.14.04.1
* New upstream bug fix release: (LP: #1348176)
  - pg_upgrade: Users who upgraded to version 9.3 using pg_upgrade may have
    an issue with transaction information which causes VACUUM to eventually
    fail. These users should run the script provided in the release notes to
    determine if their installation is affected, and then take the remedy
    steps outlined there.
  - Various data integrity and other bug fixes.
  - Secure Unix-domain sockets of temporary postmasters started during make
    check.
    Any local user able to access the socket file could connect as the
    server's bootstrap superuser, then proceed to execute arbitrary code as
    the operating-system user running the test, as we previously noted in
    CVE-2014-0067. This change defends against that risk by placing the
    server's socket in a temporary, mode 0700 subdirectory of /tmp.
  - See release notes for details:
    http://www.postgresql.org/about/news/1534/
* Remove pg_regress patches to support --host=/path, obsolete with above
  upstream changes and not applicable any more.
* Drop tcl8.6 patch, applied upstream.
* Add missing logrotate test dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
 * Numeric values are represented in a base-NBASE floating point format.
51
51
 * Each "digit" ranges from 0 to NBASE-1.  The type NumericDigit is signed
52
52
 * and wide enough to store a digit.  We assume that NBASE*NBASE can fit in
53
 
 * an int.      Although the purely calculational routines could handle any even
 
53
 * an int.  Although the purely calculational routines could handle any even
54
54
 * NBASE that's less than sqrt(INT_MAX), in practice we are only interested
55
55
 * in NBASE a power of ten, so that I/O conversions and decimal rounding
56
56
 * are easy.  Also, it's actually more efficient if NBASE is rather less than
95
95
 * If the high bits of the first word of a NumericChoice (n_header, or
96
96
 * n_short.n_header, or n_long.n_sign_dscale) are NUMERIC_SHORT, then the
97
97
 * numeric follows the NumericShort format; if they are NUMERIC_POS or
98
 
 * NUMERIC_NEG, it follows the NumericLong format.      If they are NUMERIC_NAN,
 
98
 * NUMERIC_NEG, it follows the NumericLong format.  If they are NUMERIC_NAN,
99
99
 * it is a NaN.  We currently always store a NaN using just two bytes (i.e.
100
100
 * only n_header), but previous releases used only the NumericLong format,
101
101
 * so we might find 4-byte NaNs on disk if a database has been migrated using
102
 
 * pg_upgrade.  In either case, when the high bits indicate a NaN, the
 
102
 * pg_upgrade.  In either case, when the high bits indicate a NaN, the
103
103
 * remaining bits are never examined.  Currently, we always initialize these
104
104
 * to zero, but it might be possible to use them for some other purpose in
105
105
 * the future.
207
207
        : ((n)->choice.n_long.n_weight))
208
208
 
209
209
/* ----------
210
 
 * NumericVar is the format we use for arithmetic.      The digit-array part
 
210
 * NumericVar is the format we use for arithmetic.  The digit-array part
211
211
 * is the same as the NumericData storage format, but the header is more
212
212
 * complex.
213
213
 *
214
214
 * The value represented by a NumericVar is determined by the sign, weight,
215
215
 * ndigits, and digits[] array.
216
216
 * Note: the first digit of a NumericVar's value is assumed to be multiplied
217
 
 * by NBASE ** weight.  Another way to say it is that there are weight+1
 
217
 * by NBASE ** weight.  Another way to say it is that there are weight+1
218
218
 * digits before the decimal point.  It is possible to have weight < 0.
219
219
 *
220
220
 * buf points at the physical start of the palloc'd digit buffer for the
221
 
 * NumericVar.  digits points at the first digit in actual use (the one
222
 
 * with the specified weight).  We normally leave an unused digit or two
 
221
 * NumericVar.  digits points at the first digit in actual use (the one
 
222
 * with the specified weight).  We normally leave an unused digit or two
223
223
 * (preset to zeroes) between buf and digits, so that there is room to store
224
224
 * a carry out of the top digit without reallocating space.  We just need to
225
225
 * decrement digits (and increment weight) to make room for the carry digit.
596
596
         * In most cases, the size of a numeric will be smaller than the value
597
597
         * computed below, because the varlena header will typically get toasted
598
598
         * down to a single byte before being stored on disk, and it may also be
599
 
         * possible to use a short numeric header.      But our job here is to compute
 
599
         * possible to use a short numeric header.  But our job here is to compute
600
600
         * the worst case.
601
601
         */
602
602
        return NUMERIC_HDRSZ + (numeric_digits * sizeof(NumericDigit));
716
716
 *
717
717
 * Flatten calls to numeric's length coercion function that solely represent
718
718
 * increases in allowable precision.  Scale changes mutate every datum, so
719
 
 * they are unoptimizable.      Some values, e.g. 1E-1001, can only fit into an
 
719
 * they are unoptimizable.  Some values, e.g. 1E-1001, can only fit into an
720
720
 * unconstrained numeric, so a change from an unconstrained numeric to any
721
721
 * constrained numeric is also unoptimizable.
722
722
 */
746
746
                 * If new_typmod < VARHDRSZ, the destination is unconstrained; that's
747
747
                 * always OK.  If old_typmod >= VARHDRSZ, the source is constrained,
748
748
                 * and we're OK if the scale is unchanged and the precision is not
749
 
                 * decreasing.  See further notes in function header comment.
 
749
                 * decreasing.  See further notes in function header comment.
750
750
                 */
751
751
                if (new_typmod < (int32) VARHDRSZ ||
752
752
                        (old_typmod >= (int32) VARHDRSZ &&
958
958
 
959
959
        /*
960
960
         * The packed format is known to be totally zero digit trimmed always. So
961
 
         * we can identify a ZERO by the fact that there are no digits at all.  Do
 
961
         * we can identify a ZERO by the fact that there are no digits at all.  Do
962
962
         * nothing to a zero.
963
963
         */
964
964
        if (NUMERIC_NDIGITS(num) != 0)
1934
1934
                PG_RETURN_NUMERIC(make_result(&const_nan));
1935
1935
 
1936
1936
        /*
1937
 
         * Unpack the argument and determine the result scale.  We choose a scale
 
1937
         * Unpack the argument and determine the result scale.  We choose a scale
1938
1938
         * to give at least NUMERIC_MIN_SIG_DIGITS significant digits; but in any
1939
1939
         * case not less than the input's dscale.
1940
1940
         */
1985
1985
                PG_RETURN_NUMERIC(make_result(&const_nan));
1986
1986
 
1987
1987
        /*
1988
 
         * Unpack the argument and determine the result scale.  We choose a scale
 
1988
         * Unpack the argument and determine the result scale.  We choose a scale
1989
1989
         * to give at least NUMERIC_MIN_SIG_DIGITS significant digits; but in any
1990
1990
         * case not less than the input's dscale.
1991
1991
         */
2568
2568
 
2569
2569
/*
2570
2570
 * Integer data types all use Numeric accumulators to share code and
2571
 
 * avoid risk of overflow.      For int2 and int4 inputs, Numeric accumulation
 
2571
 * avoid risk of overflow.  For int2 and int4 inputs, Numeric accumulation
2572
2572
 * is overkill for the N and sum(X) values, but definitely not overkill
2573
 
 * for the sum(X*X) value.      Hence, we use int2_accum and int4_accum only
 
2573
 * for the sum(X*X) value.  Hence, we use int2_accum and int4_accum only
2574
2574
 * for stddev/variance --- there are faster special-purpose accumulator
2575
2575
 * routines for SUM and AVG of these datatypes.
2576
2576
 */
2828
2828
 * the initial condition of the transition data value needs to be NULL. This
2829
2829
 * means we can't rely on ExecAgg to automatically insert the first non-null
2830
2830
 * data value into the transition data: it doesn't know how to do the type
2831
 
 * conversion.  The upshot is that these routines have to be marked non-strict
 
2831
 * conversion.  The upshot is that these routines have to be marked non-strict
2832
2832
 * and handle substitution of the first non-null input themselves.
2833
2833
 */
2834
2834
 
3226
3226
 
3227
3227
        /*
3228
3228
         * We first parse the string to extract decimal digits and determine the
3229
 
         * correct decimal weight.      Then convert to NBASE representation.
 
3229
         * correct decimal weight.  Then convert to NBASE representation.
3230
3230
         */
3231
3231
        switch (*cp)
3232
3232
        {
3834
3834
/*
3835
3835
 * Convert numeric to int8, rounding if needed.
3836
3836
 *
3837
 
 * If overflow, return FALSE (no error is raised).      Return TRUE if okay.
 
3837
 * If overflow, return FALSE (no error is raised).  Return TRUE if okay.
3838
3838
 */
3839
3839
static bool
3840
3840
numericvar_to_int8(NumericVar *var, int64 *result)
4305
4305
 * mul_var() -
4306
4306
 *
4307
4307
 *      Multiplication on variable level. Product of var1 * var2 is stored
4308
 
 *      in result.      Result is rounded to no more than rscale fractional digits.
 
4308
 *      in result.  Result is rounded to no more than rscale fractional digits.
4309
4309
 */
4310
4310
static void
4311
4311
mul_var(NumericVar *var1, NumericVar *var2, NumericVar *result,
4349
4349
        /*
4350
4350
         * Determine number of result digits to compute.  If the exact result
4351
4351
         * would have more than rscale fractional digits, truncate the computation
4352
 
         * with MUL_GUARD_DIGITS guard digits.  We do that by pretending that one
 
4352
         * with MUL_GUARD_DIGITS guard digits.  We do that by pretending that one
4353
4353
         * or both inputs have fewer digits than they really do.
4354
4354
         */
4355
4355
        res_ndigits = var1ndigits + var2ndigits + 1;
4592
4592
                 *
4593
4593
                 * We need the first divisor digit to be >= NBASE/2.  If it isn't,
4594
4594
                 * make it so by scaling up both the divisor and dividend by the
4595
 
                 * factor "d".  (The reason for allocating dividend[0] above is to
 
4595
                 * factor "d".  (The reason for allocating dividend[0] above is to
4596
4596
                 * leave room for possible carry here.)
4597
4597
                 */
4598
4598
                if (divisor[1] < HALF_NBASE)
4636
4636
 
4637
4637
                        /*
4638
4638
                         * If next2digits are 0, then quotient digit must be 0 and there's
4639
 
                         * no need to adjust the working dividend.      It's worth testing
 
4639
                         * no need to adjust the working dividend.  It's worth testing
4640
4640
                         * here to fall out ASAP when processing trailing zeroes in a
4641
4641
                         * dividend.
4642
4642
                         */
4654
4654
                        /*
4655
4655
                         * Adjust quotient digit if it's too large.  Knuth proves that
4656
4656
                         * after this step, the quotient digit will be either correct or
4657
 
                         * just one too large.  (Note: it's OK to use dividend[j+2] here
 
4657
                         * just one too large.  (Note: it's OK to use dividend[j+2] here
4658
4658
                         * because we know the divisor length is at least 2.)
4659
4659
                         */
4660
4660
                        while (divisor2 * qhat >
4829
4829
         * dividend's digits (plus appended zeroes to reach the desired precision
4830
4830
         * including guard digits).  Each step of the main loop computes an
4831
4831
         * (approximate) quotient digit and stores it into div[], removing one
4832
 
         * position of dividend space.  A final pass of carry propagation takes
 
4832
         * position of dividend space.  A final pass of carry propagation takes
4833
4833
         * care of any mistaken quotient digits.
4834
4834
         */
4835
4835
        div = (int *) palloc0((div_ndigits + 1) * sizeof(int));
5679
5679
 
5680
5680
        /*
5681
5681
         * The general case repeatedly multiplies base according to the bit
5682
 
         * pattern of exp.      We do the multiplications with some extra precision.
 
5682
         * pattern of exp.  We do the multiplications with some extra precision.
5683
5683
         */
5684
5684
        neg = (exp < 0);
5685
5685
        exp = Abs(exp);