Show HN: cq – Query CSVs using SQL
github.com
github.com
For those that deal with this sort of thing often, here are some similarly useful tools:
⠀
Really useful for generating table definitions and/or importing data from a csv to a variety of databases (bunch of other useful utilities in the repo as well): https://csvkit.readthedocs.io/en/latest/scripts/csvsql.html
Similar to cq for querying a CSV file directly: http://harelba.github.io/q/
Not used often, but really handy when you come across a csv file it's designed for: http://colin.maudry.com/csvtool-manual-page/
Diff csv files: http://paulfitz.github.io/daff/
Can convert flatfiles between a bunch of different formats. Useful blunt tool to get things to and from csv files (among others): https://github.com/dflemstr/rq
And for searching JSON: jq: https://stedolan.github.io/jq/ gron: https://github.com/tomnomnom/gron
Or use in2csv from csvkit or rq to get the JSON over to CSV and go from there.
The big advantage of sqlite is the lack of static types for columns. I can just add numbers without explicitely typing a column as being numeric.
One could in fact template SQL scripts expecting tables a,b,c as input and just substitute inputs for premade operations.
In fact, now that I remember, it has happened that I'll be provided a CSV, so I write this big query. Then, from analyzing the results, they'll tell me they made a mistake with the CSV they gave me and provide me with a corrected one. They'll use a different name to avoid confusion in not knowing which is the bad one and which is the corrected one, and I just change the file in the assignment and rerun the same SQL.
1. 'create table' should create a new .csv and new .schema.csv;
2. 'insert into' should insert records into .csv;
3. 'commit' should do a git commit
That way we get a human readable database + all the transaction semantics and data versioning + all the query goodness. Would like to know if there's a project doing exactly this.
$ cq t:=- -q '
insert into t values (2,3);
select * from t;
' <<EOF
foo,bar
1,2
EOF
foo,bar
1,2
2,3
I can't think of a good use-case to do create table from this tool, though. It would always be simpler to just create the CSV file.Also, I can't see the point of interpreting "commit" in the SQL to do a git commit. This tool just passes the SQL to an established SQL engine (only SQLite for now). I don't think I want to get into interpreting my own version of SQL. It doesn't sound KISS. In the same vein, interpreting "create table" to create a schema.csv also sounds out of scope just from having to interpret that instead of passing it to an SQL engine, and I can't see the point either.
Ultimately, my aim is for this tool to be more analogous to jq than to sqlite. It's not so much an RDBMS, although you can query a directory of CSVs by globbing like it's a database. It's mainly an ad-hoc querying tool.
EDIT: I don't know anymore. I'm growing to like your idea, and thinking of how it can be supported. I'll keep it in consideration.
Does anyone have a concrete use-case for a human readable database with automatic git commits like that? It wouldn't be fast enough for anything intensive. I can only see it as a CLI version of Excel.
It may also be nice to be able to use that to export a DB to SQL and then use cq to turn that into a set of CSV files. Would anyone really find something like that useful, though? Maybe that's pointless.
Some related tools:
https://github.com/BurntSushi/xsv/ http://ebay.github.io/tsv-utils/
You can also join those steps in a single command with Postgres, something like:
psql << EOF
create table ...
copy tablename from filename csv ...
copy (select ...) to stdout csv ...
drop table ...
EOF
However, this tool is more concise to do the above and facilitates combining with brace-expansion, file tab-completion, and <() process substitution.