pgsql: Prevent to_number() from losing data when template doesn't match

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Prevent to_number() from losing data when template doesn't match
Date: 2017-11-17 17:04:26
Message-ID: E1eFk42-0000Mh-7q@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Prevent to_number() from losing data when template doesn't match exactly.

Non-data template patterns would consume characters whether or not those
characters were what the pattern expected, for example
SELECT TO_NUMBER('1234', '9,999');
produced 134 because the '2' got eaten by the comma pattern. This seems
undesirable, not least because it doesn't happen in Oracle. For the ','
and 'G' template patterns, we can fix this by consuming characters only
if they match what the pattern would output. For non-data patterns such
as 'L' and 'TH', it seems impractical to tighten things up to the point of
consuming only exact matches to what the pattern would output; but we can
improve matters quite a lot by redefining the behavior as "consume only
characters that aren't digits, signs, decimal point, or comma".

Also, fix it so that the behavior is to consume the number of *characters*
the pattern would output, not the number of *bytes*. The old coding would
do surprising things with non-ASCII currency symbols, for example. (It
would be good to apply that rule for literal text as well, but this commit
only fixes it for non-data patterns.)

Oliver Ford, reviewed by Thomas Munro and Nathan Wagner, and whacked around
a bit more by me

Discussion: https://postgr.es/m/CAGMVOdvpbMqPf9XWNzOwBpzJfErkydr_fEGhmuDGa015z97mwg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/e87d4965bd39e4d0d56346c1bbe9361d3eb9ff0a

Modified Files
--------------
doc/src/sgml/func.sgml | 28 +++++--
src/backend/utils/adt/formatting.c | 134 +++++++++++++++++++++++++++-------
src/test/regress/expected/numeric.out | 56 ++++++++++++++
src/test/regress/sql/numeric.sql | 11 +++
4 files changed, 197 insertions(+), 32 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2017-11-17 17:48:02 pgsql: Provide modern examples of how to auto-start Postgres on macOS.
Previous Message Robert Haas 2017-11-17 16:53:06 pgsql: Set proargmodes for satisfies_hash_partition.