Uh oh!
There was an error while loading. Please reload this page.
Add support for query parameters - #2776
Conversation
Holds name, type, and value for scalar query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. Toward #2551.
Holds name, type, and value for array query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. Toward #2551.
Holds name, types, and values for Struct query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. Toward #2551.
It is just a list comprehension, really.
| from google.cloud.bigquery.dataset import Dataset | ||
| from google.cloud.bigquery.schema import SchemaField | ||
| from google.cloud.bigquery.table import Table | ||
| from google.cloud.bigquery._helpers import ArrayQueryParameter |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| """Named / positional query parameters for scalar values. | ||
| :type name: str or None | ||
| :param name: Parameter name, used via `@foo` syntax. If None, the |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| @classmethod | ||
| def positional(cls, type_, value): | ||
| """Factory for positional paramters. | ||
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| :param value: the scalar parameter value. | ||
| :rtype: :class:`ScalarQueryParameter` | ||
| :returns: instance w/o name |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| """Factory for positional paramters. | ||
| :type sub_parms: tuple of :class:`ScalarQueryParameter` | ||
| :param sub_parms:s the sub-parameters for the struct |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| @staticmethod | ||
| def _get_target_class(): | ||
| from google.cloud.bigquery._helpers import AbstractQueryParameter |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| """Named / positional query parameters for scalar values. | ||
| :type name: str or None | ||
| :param name: Parameter name, used via `@foo` syntax. If None, the |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| class ScalarQueryParameter(object): | ||
| """Named / positional query parameters for scalar values. | ||
| :type name: str or None |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| :type name: str or None | ||
| :param name: Parameter name, used via `@foo` syntax. If None, the | ||
| paramter can only be addressed via position (`?`). |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| """ | ||
| name = resource.get('name') | ||
| type_ = resource['parameterType']['type'] | ||
| value = resource['parameterValue']['value'] |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| :class:`datetime.date`. | ||
| :param value: the scalar parameter value. | ||
| """ | ||
| def __init__(self, name, type_, value): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| query_parameter.to_api_repr() | ||
| for query_parameter in self._query_parameters | ||
| ] | ||
| if self._query_parameters[0].name is None: |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| else: | ||
| self.assertIsNone(job.maximum_bytes_billed) | ||
| def _verifyUDFResources(self, job, config): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| def test_begin_w_named_query_parameter(self): | ||
| from google.cloud.bigquery._helpers import ScalarQueryParameter | ||
| query_parameters = [ScalarQueryParameter('foo', 'INT64', 123)] | ||
| PATH = 'projects/%s/jobs' % self.PROJECT |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| if token is None: | ||
| break | ||
| jobs, token = client.list_jobs(page_token=token) # API request | ||
| job_iterator = client.list_jobs() # API request |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| @snippet | ||
| def client_run_sync_query_w_param(client, _): | ||
| """Run a synchronous query using a query parameter""" |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Addresses: #2776 (comment).
Addresses: #2776 (comment)
| :type type_: str | ||
| :param type_: name of parameter type. One of `'STRING'`, `'INT64'`, | ||
| `'FLOAT64'`, `'BOOLEAN'`, `'TIMESTAMP'`, or `'DATE'`. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
The back-end raises an exception when passed the 'BOOLEAN' type to define a query parameter. Addresses: #2776 (comment)
| {'name': name, 'type': self.struct_types[name]} | ||
| for name in self._order | ||
| {'name': key, 'type': value} | ||
| for key, value in self.struct_types.items() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| the class). | ||
| :type value: list of instances of classes derived from | ||
| :class:`AbstractQueryParameter`. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
dhermes
left a comment
There was a problem hiding this comment.
LGTM, though I don't understand why you'd have the constructor and the API rep so far apart, especially if no human is every going to use the constructor
tseaver
commented
Dec 2, 2016
I do expect humans to use the constructor, e.g.: fromgoogle.cloud.bigqueryimportClientfromgoogle.cloud.bigqueryimportScalarQueryParameterfromgoogle.cloud.bigqueryimportStructQueryParameterbirth_state=ScalarQueryParameter('birth_state', 'STRING', 'VA')
death_state=ScalarQueryParameter('death_state', 'STRING', 'MS')
birth_death=StructQueryParameter(
'birth_death', birth_state, death_state)
client=Client()
query=client.run_sync_query(
'SELECT * FROM mortality WHERE birth_death = @birth_death') |
* 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. Closesgoogleapis#2551.
avitzavi
commented
Oct 31, 2018
does this mean we can use @parameters in the BigQuery UI ?? I cant seem to figure out how to declare them correctly. |
tseaver
commented
Oct 31, 2018
@avitzavi I'm not sure what you are asking. Does the example in the docs help? |
avitzavi
commented
Oct 31, 2018
via email
I’m trying to use variables like I do in sql server:
“Declare @variable INT = 10”
Can I do this in the BigQuery UI? … On Oct 31, 2018, at 2:25 PM, Tres Seaver ***@***.***> wrote:
@avitzavi I'm not sure what you are asking. Does the example in the docs help?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread. |
tseaver
commented
Oct 31, 2018
The Also, I don't know what you mean by "BigQuery UI" -- is that the https://console.cloud.google.com/ interface? |
avitzavi
commented
Oct 31, 2018
via email
Yes it is the console.
The Declare/Set methods from SQL are very valuable for me as a data analyst.
Seems like a really big missing feature...
I create many reports (based off scheduled queries) and not having these parameters makes it much more challenging to do.
Especially given the Console's lack of basic editor features like find/replace. … Oct 31, 2018, at 5:30 PM, Tres Seaver ***@***.***> wrote:
The @parameter placeholders added by this PR are used for passing in values from the client to be substituted into a query: they aren't meant to be used that way.
Also, I don't know what you mean by "BigQuery UI" -- is that the https://console.cloud.google.com/ interface?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread. |
tseaver
commented
Oct 31, 2018
@avitzavi This repository is solely about the Python client libraries which wrap the various Google Cloud APIs. Please check out the BigQuery support page for ways to get support for the back-end API itself. |
Closes#2551.