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

BigQuery: Add support for query parameters #2551

Closed
jlowin opened this issue Oct 16, 2016 · 9 comments
Closed

BigQuery: Add support for query parameters #2551

jlowin opened this issue Oct 16, 2016 · 9 comments
Assignees
Labels
api: bigquery Issues related to the BigQuery API. release blocking Required feature/issue must be fixed prior to next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@jlowin
Copy link

jlowin commented Oct 16, 2016

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.

@jlowin jlowin changed the title Add support for query parameters BigQuery: Add support for query parameters Oct 16, 2016
@tseaver tseaver added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. api: bigquery Issues related to the BigQuery API. labels Oct 16, 2016
@tseaver
Copy link
Contributor

tseaver commented Oct 16, 2016

@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?

@jlowin
Copy link
Author

jlowin commented Oct 16, 2016

I wish there were but so far I've just been figuring it out via trial and error. For example:

bq query --use_legacy_sql=False --parameter param1:INTEGER:5 --parameter param2:INTEGER:100 "select @param1 + @param2 as result"

Waiting on bqjob_r665b4bfadeedbee9_00000157ce6674a0_1 ... (0s) Current status: DONE
+--------+
| result |
+--------+
|    105 |
+--------+

Putting @param in the Web UI raises errors about undeclared variables, but I haven't figured out how to declare them...

@tswast
Copy link
Contributor

tswast commented Oct 17, 2016

@tseaver It's on my TODO list to document this feature better.

@tswast
Copy link
Contributor

tswast commented Nov 3, 2016

@tseaver I found the docs on this:

https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query

Search for queryParameters.

Depending on what parameterMode is set to, the parameter can be positional ? or names @somename

@c0b
Copy link

c0b commented Nov 14, 2016

+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 optional because bq tool doesn't send that at all, it still works.

if you give the parameter a name it is named and you may refer it in the SQL by @thename; or if omit the name, it is positional, refer it by ?.

you may continue research by turning on --apilog - to see all the REST API interaction to bigquery server, that way to know how queryParameters are constructed, have fun 😄

$ ./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

@tseaver
Copy link
Contributor

tseaver commented Nov 28, 2016

@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.

@danoscarmike
Copy link
Contributor

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!

@tseaver
Copy link
Contributor

tseaver commented Nov 30, 2016

@danoscarmike PR #2776 implements the feature.

tseaver added a commit that referenced this issue Dec 2, 2016
* 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.
@danoscarmike
Copy link
Contributor

Great stuff. Thanks Tres!

richkadel pushed a commit to richkadel/google-cloud-python that referenced this issue May 6, 2017
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the BigQuery API. release blocking Required feature/issue must be fixed prior to next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

5 participants