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

Add support for query parameters #2776

Merged
merged 20 commits into from
Dec 2, 2016
Merged

Add support for query parameters #2776

merged 20 commits into from
Dec 2, 2016

Conversation

tseaver
Copy link
Contributor

@tseaver tseaver commented Nov 29, 2016

Closes #2551.

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.
@tseaver tseaver added the api: bigquery Issues related to the BigQuery API. label Nov 29, 2016
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Nov 29, 2016
@@ -28,3 +28,6 @@
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.

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

@classmethod
def positional(cls, type_, value):
"""Factory for positional paramters.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

:param value: the scalar parameter value.

:rtype: :class:`ScalarQueryParameter`
:returns: instance w/o name

This comment was marked as spam.

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


@staticmethod
def _get_target_class():
from google.cloud.bigquery._helpers import AbstractQueryParameter

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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

class ScalarQueryParameter(object):
"""Named / positional query parameters for scalar values.

:type name: str or None

This comment was marked as spam.


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

"""
name = resource.get('name')
type_ = resource['parameterType']['type']
value = resource['parameterValue']['value']

This comment was marked as spam.

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

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.

@@ -1287,12 +1287,31 @@ def _verifyIntegerResourceProperties(self, job, config):
else:
self.assertIsNone(job.maximum_bytes_billed)

def _verifyUDFResources(self, job, config):

This comment was marked as spam.

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.

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.

@@ -490,6 +486,30 @@ def client_run_sync_query(client, _):


@snippet
def client_run_sync_query_w_param(client, _):
"""Run a synchronous query using a query parameter"""
QUERY_W_PARM = (

This comment was marked as spam.


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

This comment was marked as spam.

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.

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.

Copy link
Contributor

@dhermes dhermes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor Author

tseaver commented Dec 2, 2016

especially if no human is every going to use the constructor

I do expect humans to use the constructor, e.g.:

from google.cloud.bigquery import Client
from google.cloud.bigquery import ScalarQueryParameter
from google.cloud.bigquery import StructQueryParameter


birth_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')

@tseaver tseaver merged commit 75c4115 into googleapis:master Dec 2, 2016
@tseaver tseaver deleted the 2551-bigquery-query_parameters branch December 2, 2016 22:52
richkadel pushed a commit to richkadel/google-cloud-python that referenced this pull request 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.
@avitzavi
Copy link

does this mean we can use @parameters in the BigQuery UI ?? I cant seem to figure out how to declare them correctly.

@tseaver
Copy link
Contributor Author

tseaver commented Oct 31, 2018

@avitzavi I'm not sure what you are asking. Does the example in the docs help?

@avitzavi
Copy link

avitzavi commented Oct 31, 2018 via email

@tseaver
Copy link
Contributor Author

tseaver commented Oct 31, 2018

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?

@avitzavi
Copy link

avitzavi commented Oct 31, 2018 via email

@tseaver
Copy link
Contributor Author

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.

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. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants