cube2222 / octosql

OctoSQL is a query tool that allows you to join, analyse and transform data from multiple databases and file formats using SQL.
Mozilla Public License 2.0
4.74k stars 201 forks source link

Allow using double-quotes for escaping. #303

Closed codekitchen closed 1 year ago

codekitchen commented 1 year ago

I have a csv file that has a column named key, which seems to be a reserved word in octosql? select key from myfile.csv errors with

Error: couldn't parse query: invalid argument syntax error at position 11 near 'key'

I tried escaping the identifier with double-quotes following the SQL standard, but that seems to actually create a string literal in octosql, same as with single-quotes?

❯ octosql 'select "key" from myfile.csv limit 3'
+-------+
| col_0 |
+-------+
| 'key' |
| 'key' |
| 'key' |
+-------+

I also tried brackets (select [key] from) but that errors as well. Is there a way to escape identifiers that I am missing in the docs? Thanks!

cube2222 commented 1 year ago

Hey @codekitchen!

You can escape keywords (and also complex file paths) using backticks. So in this case that would be:

SELECT `key` FROM myfile.csv

However, keep in mind that if you're using bash, you need to escape backticks, so the above becomes:

octosql 'SELECT \`key\` FROM myfile.csv'

That said, using double quotes for escaping is a good idea (following the SQL standard, as you said). Thanks for it! It would also partially solve https://github.com/cube2222/octosql/issues/295.

I'll update the issue title to track adding this (changing from the current behaviour of treating double-quotes as string literals).

codekitchen commented 1 year ago

aha, thank you. and 👍 on allowing double-quotes as is standard.

one note: escaping the backticks isn't necessary in bash (and zsh) if you wrap the whole query in single-quotes, e.g. this works fine:

octosql 'select `key` from myfile.csv'

but if wrapping in double-quotes, you do need to escape the backticks:

octosql "select \`key\` from myfile.csv"
cube2222 commented 1 year ago

@codekitchen FYI double quotes for literals have been released as part of v0.12.0.