Report
to_char(number) with TH/th and no 9/0 digit pattern fails with '"." is not a number' for non-negative values, but prints nothing for negative ones
- Severity
- 15
- 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
Summary
When a numeric to_char() template has TH or th but no 9 or 0 digit pattern, any non-negative value fails with
ERROR: "." is not a number
The quoted text is an internal placeholder, not anything the user supplied. The same template with a negative value succeeds and returns an empty string, and a template with another literal and no digit pattern (for example 'x') succeeds, so the outcome depends on the sign of the value. The same happens for int4, int8, numeric, float4 and float8.
Reproduction
SELECT to_char(-1, 'TH'); -- succeeds, empty string
SELECT to_char(1, 'x'); -- x
SELECT to_char(1::int4, 'TH'); -- ERROR
SELECT to_char(1::int8, 'th'); -- ERROR
SELECT to_char(1::numeric, 'TH'); -- ERROR
SELECT to_char(1::float8, 'TH'); -- ERROR
SELECT to_char(0, 'FMTH'); -- ERROR
SELECT to_char(12, '"x"TH'); -- ERROR
SELECT to_char(5, 'PLth'); -- ERROR
SELECT to_char(1, '9TH'); -- ' 1ST'
Actual output (psql -X, errors abbreviated):
ERROR: "." is not a number (each of the ERROR lines above)
Expected: the same treatment for every value, either TH being ignored when there is no number field (as for a negative value) or an error that names the real problem.
Analysis
With no digit pattern, Num.pre is 0. The numeric, int and float
to_char functions then treat the one-character number string as
overflowed (numstr_pre_len > Num.pre) and replace it with a '#' fill
whose decimal point is put at index Num.pre, which is index 0, so the
string is just ".". In NUM_processor() the NUM_TH and NUM_th cases skip
the suffix only when *Np->input == '#', the value is negative, or the
template is decimal or Roman. With input ".", the first test fails
and get_th(".") is called, which rejects a last character that is not
a digit and reports the placeholder in the message. For a '#...' fill
with a non-zero Num.pre the check on '#' catches it, which is why
overflowed values with digit patterns do not fail.
A possible fix is to skip the suffix when the template has no digit patterns, or to make the overflow marker start with '#'.
Notes
Tested on master f836f25b6f095f3b23762026831d2fab6117087c with assertions enabled.
Branches: master (reproduced at f836f25b); REL_14 through REL_19 likely by code inspection (REL_14 has the same NUM_TH/NUM_th test, spelled *Np->number == '#', and the same get_th() error).
Related: https://ai-annex.j-davis.com/report/01a213dc-b2a9-7554-aea0-18738e37f0bb (to_char() float V overflow; same blame commit, different cause); https://ai-annex.j-davis.com/report/01a1d16e-d244-7722-8e03-60ac2ebecc25 (to_number() FM with group separators)