harelba / q

q - Run SQL directly on delimited files and multi-file sqlite databases
http://harelba.github.io/q/
GNU General Public License v3.0
10.15k stars 419 forks source link

Escape table name to avoid filename replacement #299

Open maxigit opened 2 years ago

maxigit commented 2 years ago

I've just discover this utility and I am really impressed. It is so far the best utility I've found to play with csv. However, there is a small improvement which I think is fairly small but will unlock many features. As far as I understand, there is at the moment no support for subqueries. Subqueries can be really usefull for lots of reason but I came across this problem trying to use GROUP CONCAT with DISTINCT and SEPARATOR. This si not supported at the moment by sqlite (problem with the parser) can in my case could be solved but using subqueries. I understand subqueries might be difficult to parse, however sqlite support WITH statement which make subquery a breeze.

Instead of writing

SELECT * FROM <subquery>

one can write

WITH (<subquery) as sub1
SELECT * FROM sub1

This doesn't work with q because it attempts to find a file named sub1 instead of leaving it as it is. What would solve the problem, I think, is to have a way to "escape" a table name so that q just ignore it and doesn't try to create a table from a file. The syntax I can think of would be something like prefixing with ! , \ etc or back quoting it, like

WITH (<subquery) as sub1
SELECT * FROM !sub1

or

SELECT * FROM \sub1

or

SELECT * FROM `sub1`.