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

Adding UDF Resources to Queries #2015

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
22186d1
started addding udf resources
dwmclary Jul 22, 2016
bdeac5c
added UDFResources to query
dwmclary Jul 22, 2016
5ad14a2
lint and systemtests pass
dwmclary Jul 22, 2016
dafe007
all tests pass
dwmclary Jul 22, 2016
e2a0805
final lint before pr
dwmclary Jul 22, 2016
c4ee959
updated lint warnings
dwmclary Jul 22, 2016
e500987
fixed last linter warning
dwmclary Jul 22, 2016
4a4941c
First pass at Python doc generator...(originally JJ's commit)
daspecster Feb 11, 2016
c2ad035
Initial update to generate json docs
daspecster May 4, 2016
bc584bf
Update site and fix typos.
daspecster Jul 20, 2016
11e897f
Add RST snippet captions.
daspecster Jul 20, 2016
fbd411b
Remove old gh-pages doc build script.
daspecster Jul 22, 2016
a6b2751
Switch to using tox for calling json docs update script.
daspecster Jul 22, 2016
35678f6
Add Sphinx as a dependency for linting.
daspecster Jul 22, 2016
6ee3826
Exclude verify_included_modules.py from lint.
daspecster Jul 23, 2016
b9f8a2f
Add Error Reporting Client
Jun 30, 2016
6aa1c57
Update gcloud references.
daspecster Jul 26, 2016
897f8c0
response to code review, better tests, no backslashes
dwmclary Jul 26, 2016
1b29095
started addding udf resources
dwmclary Jul 22, 2016
79b38e5
coverage fixed
dwmclary Jul 26, 2016
e239baa
fixed linter error, again
dwmclary Jul 26, 2016
95ad07a
re-fixed broken tests
dwmclary Jul 26, 2016
caed23a
Merge branch 'master' of https://github.com/GoogleCloudPlatform/gclou…
dwmclary Jul 26, 2016
6fc3322
fixed author address
dwmclary Jul 27, 2016
a105efc
Merge branch 'master' into fix_author_address
dwmclary Jul 27, 2016
d9a3b2c
fixed classname in docstring
dwmclary Jul 27, 2016
9725f3b
fixed tox.ini to include ordereddict ro dep for py26
dwmclary Jul 27, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion gcloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,42 @@
from gcloud.bigquery._helpers import _TypedProperty


class UDFResource(object):
"""Describe a single user-defined function (UDF) resource.
:type udf_type: str
:param udf_type: the type of the resource ('inlineCode' or 'resourceUri')

:type value: str
:param value: the inline code or resource URI

See
https://cloud.google.com/bigquery/user-defined-functions#api
"""
def __init__(self, udf_type, value):
self.udf_type = udf_type
self.value = value

def __eq__(self, other):
return(
self.udf_type == other.udf_type and
self.value == other.value)


def _build_udf_resources(resources):
"""
:type resources: sequence of :class:`UDFResource`
:param resources: fields to be appended

:rtype: mapping
:returns: a mapping describing userDefinedFunctionResources for the query.
"""
udfs = []
for resource in resources:
udf = {resource.udf_type: resource.value}
udfs.append(udf)
return udfs


class Compression(_EnumProperty):
"""Pseudo-enum for ``compression`` properties."""
GZIP = 'GZIP'
Expand Down Expand Up @@ -888,14 +924,44 @@ class QueryJob(_AsyncJob):
:type client: :class:`gcloud.bigquery.client.Client`
:param client: A client which holds credentials and project configuration
for the dataset (which requires a project).

:type udf_resources: tuple
:param udf_resources: An iterable of
:class:`gcloud.bigquery.job.UDFResource`
(empty by default)
"""
_JOB_TYPE = 'query'
_UDF_KEY = 'userDefinedFunctionResources'
_udf_resources = None

def __init__(self, name, query, client):
def __init__(self, name, query, client, udf_resources=()):
super(QueryJob, self).__init__(name, client)
self.query = query
self.udf_resources = udf_resources
self._configuration = _AsyncQueryConfiguration()

@property
def udf_resources(self):
"""Property for list of UDF resources attached to a query
See
https://cloud.google.com/bigquery/user-defined-functions#api
"""
return list(self._udf_resources)

@udf_resources.setter
def udf_resources(self, value):
"""Update queries UDF resources

:type value: list of :class:`UDFResource`
:param value: an object which defines the type and value of a resource

:raises: TypeError if 'value' is not a sequence, or ValueError if
any item in the sequence is not a UDFResource
"""
if not all(isinstance(u, UDFResource) for u in value):
raise ValueError("udf items must be UDFResource")
self._udf_resources = tuple(value)

allow_large_results = _TypedProperty('allow_large_results', bool)
"""See:
https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query.allowLargeResults
Expand Down Expand Up @@ -979,6 +1045,9 @@ def _populate_config_resource(self, configuration):
configuration['useLegacySql'] = self.use_legacy_sql
if self.write_disposition is not None:
configuration['writeDisposition'] = self.write_disposition
if len(self._udf_resources) > 0:
configuration[self._UDF_KEY] = _build_udf_resources(
self._udf_resources)

def _build_resource(self):
"""Generate a resource for :meth:`begin`."""
Expand Down
Loading