Skip to content

add external_udf_uris support to query() #120

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

Merged
merged 1 commit into from
Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 13 additions & 10 deletions bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def _insert_job(self, body_object):
body=body_object
).execute()

def query(self, query, max_results=None, timeout=0, dry_run=False, use_legacy_sql=None):
def query(self, query, max_results=None, timeout=0, dry_run=False, use_legacy_sql=None, external_udf_uris=None):
"""Submit a query to BigQuery.

Parameters
Expand All @@ -294,6 +294,9 @@ def query(self, query, max_results=None, timeout=0, dry_run=False, use_legacy_sq
message it would if it wasn't a dry run.
use_legacy_sql : bool, optional. Default True.
If False, the query will use BigQuery's standard SQL (https://cloud.google.com/bigquery/sql-reference/)
external_udf_uris : list, optional
Contains external UDF URIs. If given, URIs must be Google Cloud
Storage and have .js extensions.


Returns
Expand Down Expand Up @@ -321,6 +324,10 @@ def query(self, query, max_results=None, timeout=0, dry_run=False, use_legacy_sq
if use_legacy_sql is not None:
query_data['useLegacySql'] = use_legacy_sql

if external_udf_uris:
query_data['userDefinedFunctionResources'] = \
[ {'resourceUri': u} for u in external_udf_uris ]

return self._submit_query_job(query_data)

def get_query_schema(self, job_id):
Expand Down Expand Up @@ -1048,7 +1055,7 @@ def write_to_table(
query,
dataset=None,
table=None,
external_udf_uris=[],
external_udf_uris=None,
allow_large_results=None,
use_query_cache=None,
priority=None,
Expand All @@ -1073,7 +1080,7 @@ def write_to_table(
table : str, optional
String id of the table
external_udf_uris : list, optional
Contains extternal UDF URIs. If given, URIs must be Google Cloud
Contains external UDF URIs. If given, URIs must be Google Cloud
Storage and have .js extensions.
allow_large_results : bool, optional
Whether or not to allow large results
Expand Down Expand Up @@ -1144,13 +1151,9 @@ def write_to_table(
if write_disposition:
configuration['writeDisposition'] = write_disposition

configuration['userDefinedFunctionResources'] = []
for external_udf_uri in external_udf_uris:
configuration['userDefinedFunctionResources'].append(
{
"resourceUri": external_udf_uri
}
)
if external_udf_uris:
configuration['userDefinedFunctionResources'] = \
[ {'resourceUri': u} for u in external_udf_uris ]

body = {
"configuration": {
Expand Down
12 changes: 9 additions & 3 deletions bigquery/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ def setUp(self):

self.query = 'foo'
self.project_id = 'project'
self.external_udf_uris = ['gs://bucket/external_udf.js']
self.client = client.BigQueryClient(self.mock_bq_service,
self.project_id)

Expand All @@ -276,12 +277,17 @@ def test_query(self):

self.mock_job_collection.query.return_value = mock_query_job

job_id, results = self.client.query(self.query)
job_id, results = self.client.query(self.query, external_udf_uris=self.external_udf_uris)

self.mock_job_collection.query.assert_called_once_with(
projectId=self.project_id,
body={'query': self.query, 'timeoutMs': 0, 'dryRun': False,
'maxResults': None}
body={
'query': self.query,
'userDefinedFunctionResources': [ {'resourceUri': u} for u in self.external_udf_uris ],
'timeoutMs': 0,
'dryRun': False,
'maxResults': None
}
)
self.assertEquals(job_id, 'spiderman')
self.assertEquals(results, [])
Expand Down