usql icon indicating copy to clipboard operation
usql copied to clipboard

bug: `numericlocale` does not seem to be respected

Open mmisiewicz opened this issue 2 years ago • 3 comments

In psql, the numericlocale option uses the system locale to format numbers:

ubuntu=# \pset numericlocale on
ubuntu=# select 1234;
 ?column?
----------
    1,234
(1 row)

In usql this appears not to be respected:

pg:[email protected]/ubuntu=> \pset numericlocale on
Locale-adjusted numeric output is on.
pg:[email protected]/ubuntu=> select 1234;
 ?column?
----------
     1234
(1 row)

mmisiewicz avatar Jul 08 '22 23:07 mmisiewicz

While we try to copy psql's functionality as much as possible, this is not going to be changed anytime soon. The issue here is that it's part of C's underlying formatting functionality, and this can't be changed easily with in the usql source code. I'd suggest using the database's own formatting functionality if you really need to present numbers in a format different from what usql does by default.

kenshaw avatar Jul 08 '22 23:07 kenshaw

Interesting, thanks for the context. Updating the output might be helpful for confusion reduction.

mmisiewicz avatar Jul 08 '22 23:07 mmisiewicz

@kenshaw Should we remove it from https://github.com/xo/usql/blob/d5382e9c4de30b3f82d545e17d6636bd0af61eae/env/types.go#L83 then?

nineinchnick avatar Jul 09 '22 07:07 nineinchnick

Just pushed a change for this, which will go out in the next tagged release. You can now \pset numericlocale which will use the new \pset locale setting to format numbers. This value by default is read from your operating system.

The locale variable needs to be a BCP-47 format, and understood by golang.org/x/text/language.Parse:

$ usql pg://booktest:booktest@localhost
Connected with driver postgres (PostgreSQL 14.5 (Debian 14.5-1.pgdg110+1))
Type "help" for help.

pg:booktest@localhost=> select * from test;
     a     |   b   
-----------+-------
        15 |    15 
 179544.15 | 90218 
(2 rows)

pg:booktest@localhost=> \pset numericlocale
Locale-adjusted numeric output is on.
pg:booktest@localhost=> select * from test;
     a      |   b    
------------+--------
       15.0 |     15 
 179,544.15 | 90,218 
(2 rows)

pg:booktest@localhost=> \pset locale ta-IN
Locale is "ta-IN".
pg:booktest@localhost=> select * from test;
      a      |   b    
-------------+--------
        15.0 |     15 
 1,79,544.15 | 90,218 
(2 rows)

pg:booktest@localhost=>

kenshaw avatar Sep 18 '22 06:09 kenshaw

Amazing news! Confirmed it looks good with the latest commit!

mmisiewicz avatar Sep 18 '22 16:09 mmisiewicz