xo / usql

Universal command-line interface for SQL databases
MIT License
8.94k stars 352 forks source link

bug: `numericlocale` does not seem to be respected #341

Closed mmisiewicz closed 2 years ago

mmisiewicz commented 2 years ago

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:ubuntu@10.204.96.18/ubuntu=> \pset numericlocale on
Locale-adjusted numeric output is on.
pg:ubuntu@10.204.96.18/ubuntu=> select 1234;
 ?column?
----------
     1234
(1 row)
kenshaw commented 2 years ago

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.

mmisiewicz commented 2 years ago

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

nineinchnick commented 2 years ago

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

kenshaw commented 2 years ago

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=>
mmisiewicz commented 2 years ago

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