Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example requested: how to escape where #80

Open
mooreniemi opened this issue Aug 14, 2023 · 2 comments
Open

Example requested: how to escape where #80

mooreniemi opened this issue Aug 14, 2023 · 2 comments

Comments

@mooreniemi
Copy link

I'd like to be able to execute the following query with -e, without writing a sql file. What's the best way to do this? Because I need both " and ' I'm unsure how to escape in the context of -e 'my query string here'.

select *
from "AwsDataCatalog"."database"."table"
where "column" = 'value'

Naively executing this just returns a confusing error (it's mistaking my value for a column):

COLUMN_NOT_FOUND: line 1:190: Column 'value' cannot be resolved or requester is not authorized to access requested resources
@iainelder
Copy link

iainelder commented Sep 1, 2023

What is the complete command you execute? What is your operating system? What is your shell?

I'm going to assume Linux and Bash because that's what I use.

I would use a command like this:

athenacli -e /dev/stdin <<"EOF"
SELECT "table_name"
FROM "awsdatacatalog"."information_schema"."tables"
WHERE "table_schema" = 'information_schema';
EOF

It gives me a CSV result.

table_name
columns
tables
views
schemata
table_privileges
roles
applicable_roles
enabled_roles

The -e option takes a file or a string. On Linux the file /dev/stdin means the standard input. The <<"EOF" part marks the start of a here document and the second EOF marks the end. Bash writes everything between the markers to standard input and athenacli reads it.

The Bash manual explains here documents and the Linux Documentation Project gives many examples.


Why didn't your command work?

I'm going to assume your complete command looked like this:

athenacli -e '
SELECT "table_name"
FROM "awsdatacatalog"."information_schema"."tables"
WHERE "table_schema" = 'information_schema';
'

That gives the following error:

COLUMN_NOT_FOUND: line 3:24: Column 'information_schema' cannot be resolved or requester is not authorized to access requested resources

To see why, echo the argument to -e:

echo '
SELECT "table_name"
FROM "awsdatacatalog"."information_schema"."tables"
WHERE "table_schema" = 'information_schema';
'

Bash does not preserve the single quotes inside the the main single quotes.

SELECT "table_name"
FROM "awsdatacatalog"."information_schema"."tables"
WHERE "table_schema" = information_schema;

From Bash's point of view the "inside" single quotes are not inside, but they end a string. Then Bash appends the next string, quoted or not.

A simple example that shows the same effect:

echo 'a'b'c'
abc

@iainelder
Copy link

iainelder commented Sep 1, 2023

There's an even simpler solution if you don't need to quote any column names: use double quotes around your query.

athenacli -e "
SELECT table_name
FROM awsdatacatalog.information_schema.tables
WHERE table_schema = 'information_schema';
"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants