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 properties to job config constructors #6397

Merged
merged 4 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 12 additions & 10 deletions bigquery/google/cloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,11 @@ class _JobConfig(object):
job_type (str): The key to use for the job configuration.
"""

def __init__(self, job_type):
def __init__(self, job_type, properties={}):
alixhami marked this conversation as resolved.
Show resolved Hide resolved
self._job_type = job_type
self._properties = {job_type: {}}
for prop, val in properties.items():
setattr(self, prop, val)

@property
def labels(self):
Expand Down Expand Up @@ -793,7 +795,7 @@ def _set_sub_prop(self, key, value):
_helpers._set_sub_prop(self._properties, [self._job_type, key], value)

def _del_sub_prop(self, key):
"""Reove ``key`` from the ``self._properties[self._job_type]`` dict.
"""Remove ``key`` from the ``self._properties[self._job_type]`` dict.

Most job properties are inside the dictionary related to the job type
(e.g. 'copy', 'extract', 'load', 'query'). Use this method to clear
Expand Down Expand Up @@ -878,8 +880,8 @@ class LoadJobConfig(_JobConfig):
server defaults.
"""

def __init__(self):
super(LoadJobConfig, self).__init__('load')
def __init__(self, **properties):
super(LoadJobConfig, self).__init__('load', properties)
alixhami marked this conversation as resolved.
Show resolved Hide resolved

@property
def allow_jagged_rows(self):
Expand Down Expand Up @@ -1473,8 +1475,8 @@ class CopyJobConfig(_JobConfig):
server defaults.
"""

def __init__(self):
super(CopyJobConfig, self).__init__('copy')
def __init__(self, **properties):
super(CopyJobConfig, self).__init__('copy', properties)

@property
def create_disposition(self):
Expand Down Expand Up @@ -1666,8 +1668,8 @@ class ExtractJobConfig(_JobConfig):
server defaults.
"""

def __init__(self):
super(ExtractJobConfig, self).__init__('extract')
def __init__(self, **properties):
super(ExtractJobConfig, self).__init__('extract', properties)

@property
def compression(self):
Expand Down Expand Up @@ -1910,8 +1912,8 @@ class QueryJobConfig(_JobConfig):
server defaults.
"""

def __init__(self):
super(QueryJobConfig, self).__init__('query')
def __init__(self, **properties):
super(QueryJobConfig, self).__init__('query', properties)

@property
def destination_encryption_configuration(self):
Expand Down
38 changes: 36 additions & 2 deletions bigquery/tests/unit/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,13 @@ def _get_target_class():
from google.cloud.bigquery.job import LoadJobConfig
return LoadJobConfig

def test_ctor_w_properties(self):
config = self._get_target_class()(
allow_jagged_rows=True, allow_quoted_newlines=True)

self.assertTrue(config.allow_jagged_rows)
self.assertTrue(config.allow_quoted_newlines)

def test_allow_jagged_rows_missing(self):
config = self._get_target_class()()
self.assertIsNone(config.allow_jagged_rows)
Expand Down Expand Up @@ -2482,6 +2489,20 @@ def _get_target_class():
from google.cloud.bigquery.job import CopyJobConfig
return CopyJobConfig

def test_ctor_w_properties(self):
from google.cloud.bigquery.job import CreateDisposition
from google.cloud.bigquery.job import WriteDisposition

create_disposition = CreateDisposition.CREATE_NEVER
write_disposition = WriteDisposition.WRITE_TRUNCATE
config = self._get_target_class()(
create_disposition=create_disposition,
write_disposition=write_disposition
)

self.assertEqual(config.create_disposition, create_disposition)
self.assertEqual(config.write_disposition, write_disposition)

def test_to_api_repr_with_encryption(self):
from google.cloud.bigquery.table import EncryptionConfiguration

Expand Down Expand Up @@ -2916,6 +2937,13 @@ def _get_target_class():
from google.cloud.bigquery.job import ExtractJobConfig
return ExtractJobConfig

def test_ctor_w_properties(self):
config = self._get_target_class()(
field_delimiter='\t', print_header=True)

self.assertEqual(config.field_delimiter, '\t')
self.assertTrue(config.print_header)

def test_to_api_repr(self):
from google.cloud.bigquery import job
config = self._make_one()
Expand Down Expand Up @@ -3299,6 +3327,13 @@ def test_ctor_w_none(self):
self.assertIsNone(config.default_dataset)
self.assertIsNone(config.destination)

def test_ctor_w_properties(self):
config = self._get_target_class()(
use_query_cache=False, use_legacy_sql=True)

self.assertFalse(config.use_query_cache)
self.assertTrue(config.use_legacy_sql)

def test_time_partitioning(self):
from google.cloud.bigquery import table

Expand Down Expand Up @@ -3637,8 +3672,7 @@ def test_ctor_w_query_parameters(self):

query_parameters = [ScalarQueryParameter("foo", 'INT64', 123)]
client = _make_client(project=self.PROJECT)
config = QueryJobConfig()
config.query_parameters = query_parameters
config = QueryJobConfig(query_parameters=query_parameters)
job = self._make_one(
self.JOB_ID, self.QUERY, client, job_config=config)
self.assertEqual(job.query_parameters, query_parameters)
Expand Down