Q: Run SQL Directly on CSV Files
harelba.github.io
harelba.github.io
https://twitter.com/neilkod/status/914217352564137984
Furthermore, no thread on CSV files can be complete without mentioning this infamous bit of fact-trolling: the ASCII standard has had unit- and record-delimiters baked into it from the beginning.
https://ronaldduncan.wordpress.com/2009/10/31/text-file-form...
Never underestimate ergonomics like screen printability!
We just need an rcat to print them clearly.
That's mad that I've never heard of that before.
I'm amazed this isn't used more often!
So for example at a Python prompt:
>>> ord('^^') # typed ctrl-v ctrl-^ here
30
(the low unprintable ascii codes correspond to certain ctrl combinations because ctrl-x is just the ascii code of x with the high bits masked off (i.e. & 0x1f) )[1] custimizable in your terminal settings, see stty lnext ("literal next")
1: https://dev.mysql.com/doc/refman/8.0/en/csv-storage-engine.h...
This may looks like a dirty hack compared to a clean API, but if you have performance considerations or need to interface with some legacy thing that happens to understand CSV, this can be a good choice.
$ sqlite3
sqlite3> .mode csv
sqlite3> .import foo.csv foo
sqlite3> SELECT * FROM foo WHERE bar = 'baz'; -- you get the gist...
(a bunch of rows)
Q seems to be much easier to use (and certainly easier to remember), but I've always found it handy to have the full power of SQL at my fingertips when needing to do a bunch of CSV manipulations.- some strings enclosed in speechmarks, but some not
- empty fields
- speechmarks within strings
- commas within strings
- carriage returns within strings
How does Q do up against a CSV file with those traits?
Unless you’re trying to parse csv files with regexes, none of those should be difficult, or even unexpected, to handls with a PEG parser, or any equivalent device
Ofc if you’re accepting ambiguity then its just arbitrary how you handle it, but none of your examples afaict present any ambiguity (I’m assuming strings are either quoted or unquoted, with the former primarily allowing commas/newlines in strings; escaping exists as well; comma delimited columns, newline delimited rows)
Many people are not in the position to just return the file to the client/boss and tell them they have a "broken csv file". (They'll tell you they saved it in Excel and it reads back fine, so the problem must be on your end. E.g.: https://stackoverflow.com/questions/43273976/escaping-quotes...)
Import-CSV .\file.csv | Export-CSV .\file.csv -NoTypeInformation -Encoding UTF8
Any kind of input/output delimiter is supported (-d <delim> and -D <delim>), and also multiple encodings (-e <encoding>). Also, q performs automatic type inference over the actual data.
Encoding autodetection and fixed width files are not supported though.
harelba (creator of q)
After you're done preparing your data in PowerQuery you can run PowerPivot on it for aggregations.
[0]: https://cdn-5a6cb102f911c811e474f1cd.closte.com/wp-content/u...
https://wiki.postgresql.org/wiki/Foreign_data_wrappers
In fact, seems it has several.
And there are drivers for everything from PostgreSQL and Oracle to text files and the Azure WMI.
Veeeery nice for data munging :)
http://fsharp.github.io/FSharp.Data/library/CsvProvider.html
https://en.m.wikipedia.org/wiki/Q_%28programming_language_fr...
q is available i'm thinking
Calling this command-line tool Q, which happens to be used for the same purpose as the Q programming language (i.e. querying data) begs for confusion and misunderstanding...
Meanwhile for all your streaming, filtering and aggregating need there is awk.
I’ve never been board enough to write my own little sql library for awk, but I’d be surprised if it doesn’t exist.
https://github.com/dkogan/vnlog
This is effectively a set of wrappers around awk, sort, join, etc that knows about field names, and lets you use the vanilla shell tools in a much nicer way. It's exactly what you want for the 99% use case. If your problem is complicated enough that you feel like sql is what you want, maybe leaving the shell is ok at that point.Ya know how some people want everything to be vim after they use it? I'm surprised more people haven't done that with other tools.
So... "fuck you"?
Not really. I could see this being helpful for debugging, but at that point you can get by with some simple bash string operations. Doing that same operation w/SQL seems like overkill.
Since I still had the file, I ran some queries on the same file via Q to test it out. And it works great! (With some handholding to get quoting and delimiters right). The major downside was it's pretty slow, presumably because it reloads the file into a SQLite database each time you run a command. So, I'll probably stick with loading CSVs into SQLite myself, but I could see this being a useful tool for running one-off analysis on data from stdin.
Coding bash seems an overkill for just so many people. Bash is scary while SQL is intuitive by design.