noborus / trdsql

CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN. Can output to various formats.
https://noborus.github.io/trdsql/
MIT License
1.94k stars 73 forks source link

Specifying column types #183

Open agguser opened 2 years ago

agguser commented 2 years ago

Please add an option to auto-detect column types (e.g. -itype), or specify numeric column types (e.g. -inum c2,c3).

Currently, it seems that column types are not auto-detected, which may cause wrong order, e.g.

$ d='a,1
b,2
c,11'

$ <<<$d trdsql -a -
The table name is -.
The file type is CSV.

Data types:
+-------------+------+
| column name | type |
+-------------+------+
| c1          | text |
| c2          | text |
+-------------+------+
…

$ <<<$d trdsql 'select * from - order by c2'
a,1
c,11
b,2

$ <<<$d trdsql 'select * from - order by cast(c2 as number)'
a,1
b,2
c,11
noborus commented 2 years ago

Thank you for the issue. I think it should be cast explicitly. The mistake of automatically converting a number to a number type is repeated many times. This is either already the wrong value or will be the wrong value.

agguser commented 2 years ago

Then, please add an option to specify column types (e.g. -types integer:c2,c3).

noborus commented 2 years ago

I don't think it's a good idea to just add options so far.