Repository navigation
Conversation
`fetch_real()` parsed every value with `atof()`, which stops at the currency symbol of a money value such as "$1,234.56" and returns 0. Plain numbers still go through `strtod()`. Other text is now parsed as money output in any lc_monetary locale: the sign and currency symbol around the digits are recognized, thousands separators are dropped, and the decimal separator is detected from the digit groups. Text that cannot be parsed unambiguously, such as "1,235" in a locale without fractional digits (ja_JP) or "1.500" in one with three (ar_KW), becomes NA with a warning. Closes #654. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
Question: How should
|
… ambiguous `money` values A value that cannot be parsed as a number now counts towards one warning per column and fetch, which names the column, the count, and the first such value, instead of one warning per value. The warning no longer assumes the column is money. Money values whose text is ambiguous become NA: amounts from 1,000 to 999,999 in locales without fractional digits, such as ja_JP, ko_KR, is_IS or vi_VN, and amounts from 1 to 999.999 in locales with three, such as ar_KW. The documentation of `dbGetQuery()` now says so and recommends casting to numeric. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
… first-row peeks The warning passes its message as an argument to a "%s" format, so a `%` in the column name or the value is printed as is. Peeking at the first row, as `dbFetch(n = 0)` and `dbColumnInfo()` do, parses a row it does not return and no longer warns about it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
Superseded: Accepted limitationsSuperseded by #659 (comment). These limitations remain after three rounds of clean review.
Generated by Claude Code |
|
Generated by Claude Code |
|
Install locales in CI to fix coverage. |
The money tests set lc_monetary to locales that a stock Ubuntu runner lacks, so CI skipped all of them but C and en_US, including every test of the warning for ambiguous values. The Linux runners now generate these locales before PostgreSQL starts. Windows names its locales differently, so the tests still skip there. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
|
Can we instruct libpq to return money as decimal? |
|
Not per type, as far as I can tell. libpq chooses text or binary for a whole result ( The server does convert correctly, though. I checked on PostgreSQL 16:
This suggests a robust fix without the ambiguity. When a result has a The cost is one extra round trip, only for results that contain a Workaround today: Should I switch #659 to the probe? I'd do it after #638 merges, so the change sits on the new fetch loop. Generated by Claude Code |
|
Done: CI on Linux now generates the locales the money tests use, before PostgreSQL starts (9e77ac0, in Generated by Claude Code |
Accepted limitationsThese limitations remain after three rounds of clean review.
The same printf-format problem in Generated by Claude Code |
|
Just don't use the "money" type: https://stackoverflow.com/questions/71908140/how-to-robustly-parse-postgresql-money-in-python . Cast in queries. |
Closes #654.
money(OID 790) maps toDT_REALinget_column_type_from_oid(), so its text output reachesPqColumnDataSource::fetch_real(), which calledatof()and returned 0 for every value such as$1,234.56. The fix stays in the parsing logic and reads nothing more from thePGresult. Plain numbers (numeric, float4, float8) still go throughstrtod(), which now has to consume the whole string. Anything else is parsed as money output by a newparse_money()helper that follows PostgreSQL'scash_out(). The sign (a minus anywhere around the digits, or parentheses) and the currency symbol sit outside the digits, thousands separators may be multibyte, and the decimal separator is a single byte before the last digit group. ASCII-and(cover every glibc locale's negative sign.The text alone does not always tell the decimal separator from a thousands separator. With a single single-byte separator followed by three or four digits,
1,235is 1235 inja_JP(no fractional digits) and 1.235 insq_AL(three fractional digits). Such values becomeNAinstead of a silently wrong number. In locales without fractional digits (ja_JP,ko_KR,is_IS,vi_VN, ...), every amount from 1,000 to 999,999 is lost this way. In locales with three (ar_KW, ...), every amount from 1 to 999.999 is. The common locales with two fractional digits are not affected, because every value carries the decimal separator. A new "Money" section on thedbGetQuery()help page (postgres-query) documents this and recommends casting to numeric.Values that cannot be parsed are counted, and after each fetch they raise one warning per column. The warning names the column, the count and the first such value, through a new
warn_parse_failures()hook thatDbColumn::warn_type_conflicts()calls. The message goes through a"%s"format, so a%in a column name or a value is printed as is. Peeking at the first row, asdbFetch(n = 0)anddbColumnInfo()do, does not warn, because that row is not returned. A 200,000-rowja_JPfetch raises one warning and takes about 0.06 s. Withoptions(warn = 2), the fetch errors cleanly and the result can still be fetched from afterwards.The older mixed-type warning in
DbColumn::warn_type_conflicts()(src/DbColumn.cpp) also passes its message tocpp11::warning()as a format string. This PR leaves it alone, and it is being handled in a separate issue and PR.Tests in
tests/testthat/test-money.Rsetlc_monetarytoC,en_US,de_DE,de_CH,fr_FR,sv_SE,nl_NL,pt_BR,ja_JPandar_KWwithSET lc_monetary. They cover negative values, the extremes of the money range,NULLand the ambiguous cases. They also check the warning: its count per column and fetch, a column name containing%s%n, and the first-row peek. Locales the server lacks are skipped. CI on Linux generates all these locales before PostgreSQL starts (.github/workflows/custom/before-install), so the money tests run there without skips; Windows names its locales differently, so they skip there. The warning tests needja_JP, because money in two-digit locales is never ambiguous. Locally the money tests and the full test suite passed against PostgreSQL 16 with all these locales generated.Phase 1 of the ALTREP work (#632) will move this parser into shared functions and should carry this fix with it. How
moneyshould be returned is an open question: #659 (comment)Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
Accepted limitations after three review rounds: #659 (comment)
https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
Generated by Claude Code