-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
BigQuery: Add support for query parameters #2551
Comments
@jlowin Thanks for the report! Except for the API reference page you linked, I can't find any other docs (especially narrative ones with samples) which explain how query parameters should be used. Do you know of any? |
I wish there were but so far I've just been figuring it out via trial and error. For example:
Putting |
@tseaver It's on my TODO list to document this feature better. |
@tseaver I found the docs on this: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query Search for Depending on what |
+1 on request of this, even reading that reference didn't help a lot, because it didn't give a working example I was researching on how to pass an array and struct value, but see googleapis/google-cloud-go#390 (comment) what I found from reading /opt/google-cloud-sdk/platform/bq/bq.py source code. @jlowin what I found parameterMode is if you give the parameter a name it is you may continue research by turning on $ ./opt/google-cloud-sdk/platform/bq/bq.py --apilog - query --use_legacy_sql=false \
--parameter arr:'ARRAY<INTEGER>':'[2,3,4]' 'SELECT 3 IN UNNEST(@arr) AS res'
[...]
+------+
| res |
+------+
| true |
+------+
$ ./opt/google-cloud-sdk/platform/bq/bq.py --apilog - query --use_legacy_sql=false \
--parameter :INTEGER:3 'SELECT ? + 2 AS res'
+-----+
| res |
+-----+
| 5 |
+-----+
############ the bq's default parameter type is STRING
$ ./opt/google-cloud-sdk/platform/bq/bq.py --apilog - query --use_legacy_sql=false \
--parameter name::'Mr Smith' 'SELECT CONCAT("Hello, ", @name) AS msg'
+-----------------+
| msg |
+-----------------+
| Hello, Mr Smith |
+-----------------+
################# other BigQuery supported data types also work
$ ./opt/google-cloud-sdk/platform/bq/bq.py --apilog - query --use_legacy_sql=false \
--parameter pt:TIMESTAMP:'2016-11-13 23:23:59' 'SELECT TIMESTAMP_TRUNC(@pt, DAY) AS dt'
+---------------------+
| dt |
+---------------------+
| 2016-11-13 00:00:00 |
+---------------------+ but the WebUI at https://bigquery.cloud.google.com/ doesn't understand these parameters at all. You'll have to write a program to call the REST APIs |
@tswast Working on this now. I note that the feature is still documented as "experimental," which makes it a bit odd to have as "blocking beta" for us. |
Hi @tseaver, are you actively working this one? If you have any blockers or need input from BQ team please let me know ASAP. Otherwise, do you have a sense for remaining effort, ETC? Thanks! |
@danoscarmike PR #2776 implements the feature. |
* Add 'ScalarQueryParameter' class. Holds name, type, and value for scalar query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. * Factor out 'AbstractQueryParameter. * Add 'ArrayQueryParameter' class. Holds name, type, and value for array query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. * Add 'StructQueryParameter' class. Holds name, types, and values for Struct query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. * Add 'QueryParametersProperty' descriptor class. * Add 'query_parameters' property to 'QueryResults' and 'QueryJob'. * Plumb 'udf_resources'/'query_parameters' through client query factories. * Expose concrete query parameter classes as package APIs. Closes #2551.
Great stuff. Thanks Tres! |
* Add 'ScalarQueryParameter' class. Holds name, type, and value for scalar query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. * Factor out 'AbstractQueryParameter. * Add 'ArrayQueryParameter' class. Holds name, type, and value for array query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. * Add 'StructQueryParameter' class. Holds name, types, and values for Struct query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. * Add 'QueryParametersProperty' descriptor class. * Add 'query_parameters' property to 'QueryResults' and 'QueryJob'. * Plumb 'udf_resources'/'query_parameters' through client query factories. * Expose concrete query parameter classes as package APIs. Closes googleapis#2551.
The CLI command
bq query
now supports query parameters for Standard SQL queries and the API does as well (https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#request-body). It would be great to be able to provide these via google.cloud.The text was updated successfully, but these errors were encountered: