Report
to_number() with FM and a group separator drops one digit per separator when every digit position is filled
- Severity
- 80
- Blamed commit
- b866d2e2d79416f8497e4dffa7e800298d018f6c
On 27 branches
REL7_1_STABLE, REL7_2_STABLE, REL7_3_STABLE, REL7_4_STABLE, REL8_0_STABLE, REL8_1_STABLE, REL8_2_STABLE, REL8_3_STABLE, REL8_4_STABLE, REL9_0_STABLE, REL9_1_STABLE, REL9_2_STABLE, REL9_3_STABLE, REL9_4_STABLE, REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Problem
to_number() drops one digit for each group separator when the template uses fill mode (FM) and the input fills every digit position. The function returns that truncated numeric value with no error.
FM99G999 applied to 12,345 returns 1234. FM9G999D9 applied to 1,234.5 returns 1234. FM9G999G999D999 applied to 1,234,567.891 returns 1234567.8. FM9,999 applied to 1,234 returns 123, and FM9,999.99 applied to 1,234.56 returns 1234.5. The same templates without FM, and an FM template with a spare digit position, return the full value. A missing separator is already accepted.
to_char(12345.675, 'FM99G999D99') produces 12,345.68. Passing that text back through to_number() with the same template returns 12345.6, so an FM round trip does not preserve the formatted value.
The lost digits are the function result. A later statement that stores the result persists the truncated number. Omitting FM, leaving one unused digit position, or stripping separators before the call avoids the loss.
Introducing commit
b866d2e2d79416f8497e4dffa7e800298d018f6c (2000-01-25) added to_number(). On input, a comma or G node continued without reading the separator when the in-number flag was false and fill mode was set. That flag was set only while the output path copied a digit, so input never consumed a group separator under FM. The next digit node then advanced over the separator. When every digit position was filled, the final digit was never read.
394af52795f6bb02ae26af2ddca6cec0f255ee33 renamed the flag to num_in. e87d4965bd39e4d0d56346c1bbe9361d3eb9ff0a stopped to_number() from swallowing a digit when the template does not match, and kept the fill-mode skip. 28c5d0724875591ff55dd85a065207badc2fa8ad split the processor and left the same skip in the input path. The current continue is blamed on e87d4965bd39 because that commit moved the line. The skip itself comes from b866d2e2d794.
Affected branches
master and REL_14_STABLE through REL_19_STABLE. With lc_numeric set to C, G is a comma. A literal comma in the template has the same fault. D is unaffected: the decimal separator has its own node.
Reproduction
SET lc_numeric = 'C';
SELECT to_number('12,345', 'FM99G999');
SELECT to_number('1,234.5', 'FM9G999D9');
SELECT to_number('1,234,567.891', 'FM9G999G999D999');
SELECT to_number('1,234', 'FM9,999');
SELECT to_number('1,234.56', 'FM9,999.99');
SELECT to_number(to_char(12345.675, 'FM99G999D99'), 'FM99G999D99');
SELECT to_number('12,345', '99G999');
SELECT to_number('1,234', 'FM999G999');
SELECT to_number('12345', 'FM99G999');
SELECT to_number(',123', 'FMG999');
Expected:
12345
1234.5
1234567.891
1234
1234.56
12345.68
12345
1234
12345
123
Actual:
1234
1234
1234567.8
123
1234.5
12345.6
12345
1234
12345
12
The last three statements are the cases that already behave as expected: no FM, a spare digit position, and a missing separator. The leading-separator call returns 12.
Diagnosis
num_in stays false for the whole of to_number(). It is set only in the output digit-copy path, and each output digit pattern clears it again. Under FM, the input NUM_COMMA and NUM_G nodes therefore continue without consuming the separator. The following digit slot increments the input pointer and spends itself on the comma or on the locale thousands separator. Each separator costs one digit, so a fully occupied template drops its last digits. A spare 9 happens to absorb the separator and the value survives.
Fill mode suppresses padding blanks and trailing zeros on output. It is not a rule about whether an input separator is optional.
Fix
On input only, delete the fill-mode early continue. Consume ',' or L_thousands_sep when the input matches, and otherwise leave the character for a later digit. That is what the non-FM path already does, so a missing separator still does not swallow a digit. Leave the to_char() nodes unchanged.
to_number(',123', 'FMG999') and to_number(',123', 'FM,999') then return 123. Today each returns 12, which is the same lost digit before any digit has been seen.
Gating the old skip on read_pre + read_post > 0 would fix a separator that follows a digit and would leave the leading-separator case truncated. Matching a separator whenever it is present covers both.
The change is confined to the input arm of the numeric processor (NUM_processor_from_char on master, the FROM_CHAR side of NUM_processor on the stable branches) plus regression queries in numeric.sql. There is no catalog or WAL change, so the same patch can ship in a minor release. Existing non-FM results, including to_number('34,50', '999,99') and to_number('123456', '999G999'), stay as they are. Callers that depended on the truncated FM result will see the full number on new calls. Values already stored are unchanged.
Severity
- A silent wrong numeric result sits at the bottom of the 80-100 anchor. to_number() itself does not write a table, and the template must combine FM with a group separator that the input actually contains and with no spare digit position. Those limits keep the score at 80.