0

I'm trying to run parametrized queries from a shell script and I need to be able to provide parameters.

This is the command I'm trying to run :

bq --location=US query --use_legacy_sql=False \
--parameter='gender::M' \
--parameter='states:ARRAY<STRING>:["WA", "WI", "WV", "WY"]' \
'SELECT name, sum(number) as count
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE gender = @gender
AND state IN UNNEST(@states)
GROUP BY name
ORDER BY count DESC
LIMIT 10;'

If you copy/paste as it is in a shell, it works. Now, this is what I'm trying to run :

In a file script.sh

runQuery(){

    states="'$1'"

    bq --location=US query --use_legacy_sql=False --parameter='gender::M' --parameter=$states \
        'SELECT name, sum(number) as count
        FROM `bigquery-public-data.usa_names.usa_1910_2013`
        WHERE gender = @gender
        AND state IN UNNEST(@states)
        GROUP BY name
        ORDER BY count DESC
        LIMIT 10;'
    }

runQuery 'states:ARRAY<STRING>:["WA", "WI", "WV", "WY"]'

But this doesn't work.

Here's the error log :

enter image description here

I am pretty sure it has to do with the fact that the parameter has both single quotes and double quotes and at some points something goes wrong when processing 'states:ARRAY:["WA", "WI", "WV", "WY"]' but I just can't find how to solve this.

Thanks in advance !

1 Answer 1

1

Found the error :

Changed runQuery 'states:ARRAY<STRING>:["WA", "WI", "WV", "WY"]'

To runQuery 'states:ARRAY<STRING>:["WA","WI","WV","WY"]'

Note that blank spaces were removed from the array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.