Skip to content

Commit b7a23bd

Browse files
alixhamitswast
authored andcommitted
BigQuery: Add properties to job config constructors (googleapis#6397)
* add properties to job config constructors * update properties arg * update **properties to **kwargs * fix docstrings
1 parent 6ad3c60 commit b7a23bd

2 files changed

Lines changed: 74 additions & 32 deletions

File tree

bigquery/google/cloud/bigquery/job.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,11 @@ class _JobConfig(object):
719719
job_type (str): The key to use for the job configuration.
720720
"""
721721

722-
def __init__(self, job_type):
722+
def __init__(self, job_type, **kwargs):
723723
self._job_type = job_type
724724
self._properties = {job_type: {}}
725+
for prop, val in kwargs.items():
726+
setattr(self, prop, val)
725727

726728
@property
727729
def labels(self):
@@ -762,7 +764,7 @@ def _get_sub_prop(self, key, default=None):
762764
``self._properties[self._job_type]`` dictionary.
763765
default (object):
764766
(Optional) Default value to return if the key is not found.
765-
Defaults to ``None``.
767+
Defaults to :data:`None`.
766768
767769
Returns:
768770
object: The value if present or the default.
@@ -793,7 +795,7 @@ def _set_sub_prop(self, key, value):
793795
_helpers._set_sub_prop(self._properties, [self._job_type, key], value)
794796

795797
def _del_sub_prop(self, key):
796-
"""Reove ``key`` from the ``self._properties[self._job_type]`` dict.
798+
"""Remove ``key`` from the ``self._properties[self._job_type]`` dict.
797799
798800
Most job properties are inside the dictionary related to the job type
799801
(e.g. 'copy', 'extract', 'load', 'query'). Use this method to clear
@@ -874,12 +876,13 @@ def from_api_repr(cls, resource):
874876
class LoadJobConfig(_JobConfig):
875877
"""Configuration options for load jobs.
876878
877-
All properties in this class are optional. Values which are ``None`` ->
878-
server defaults.
879+
All properties in this class are optional. Values which are :data:`None` ->
880+
server defaults. Set properties on the constructed configuration by using
881+
the property name as the name of a keyword argument.
879882
"""
880883

881-
def __init__(self):
882-
super(LoadJobConfig, self).__init__('load')
884+
def __init__(self, **kwargs):
885+
super(LoadJobConfig, self).__init__('load', **kwargs)
883886

884887
@property
885888
def allow_jagged_rows(self):
@@ -967,7 +970,7 @@ def destination_encryption_configuration(self):
967970
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
968971
encryption configuration for the destination table.
969972
970-
Custom encryption configuration (e.g., Cloud KMS keys) or ``None``
973+
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
971974
if using default encryption.
972975
973976
See
@@ -1338,7 +1341,7 @@ def destination_encryption_configuration(self):
13381341
encryption configuration for the destination table.
13391342
13401343
Custom encryption configuration (e.g., Cloud KMS keys)
1341-
or ``None`` if using default encryption.
1344+
or :data:`None` if using default encryption.
13421345
13431346
See
13441347
:attr:`google.cloud.bigquery.job.LoadJobConfig.destination_encryption_configuration`.
@@ -1469,12 +1472,13 @@ def from_api_repr(cls, resource, client):
14691472
class CopyJobConfig(_JobConfig):
14701473
"""Configuration options for copy jobs.
14711474
1472-
All properties in this class are optional. Values which are ``None`` ->
1473-
server defaults.
1475+
All properties in this class are optional. Values which are :data:`None` ->
1476+
server defaults. Set properties on the constructed configuration by using
1477+
the property name as the name of a keyword argument.
14741478
"""
14751479

1476-
def __init__(self):
1477-
super(CopyJobConfig, self).__init__('copy')
1480+
def __init__(self, **kwargs):
1481+
super(CopyJobConfig, self).__init__('copy', **kwargs)
14781482

14791483
@property
14801484
def create_disposition(self):
@@ -1509,7 +1513,7 @@ def destination_encryption_configuration(self):
15091513
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
15101514
encryption configuration for the destination table.
15111515
1512-
Custom encryption configuration (e.g., Cloud KMS keys) or ``None``
1516+
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
15131517
if using default encryption.
15141518
15151519
See
@@ -1579,7 +1583,7 @@ def destination_encryption_configuration(self):
15791583
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
15801584
encryption configuration for the destination table.
15811585
1582-
Custom encryption configuration (e.g., Cloud KMS keys) or ``None``
1586+
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
15831587
if using default encryption.
15841588
15851589
See
@@ -1662,12 +1666,13 @@ def from_api_repr(cls, resource, client):
16621666
class ExtractJobConfig(_JobConfig):
16631667
"""Configuration options for extract jobs.
16641668
1665-
All properties in this class are optional. Values which are ``None`` ->
1666-
server defaults.
1669+
All properties in this class are optional. Values which are :data:`None` ->
1670+
server defaults. Set properties on the constructed configuration by using
1671+
the property name as the name of a keyword argument.
16671672
"""
16681673

1669-
def __init__(self):
1670-
super(ExtractJobConfig, self).__init__('extract')
1674+
def __init__(self, **kwargs):
1675+
super(ExtractJobConfig, self).__init__('extract', **kwargs)
16711676

16721677
@property
16731678
def compression(self):
@@ -1906,19 +1911,20 @@ def _to_api_repr_table_defs(value):
19061911
class QueryJobConfig(_JobConfig):
19071912
"""Configuration options for query jobs.
19081913
1909-
All properties in this class are optional. Values which are ``None`` ->
1910-
server defaults.
1914+
All properties in this class are optional. Values which are :data:`None` ->
1915+
server defaults. Set properties on the constructed configuration by using
1916+
the property name as the name of a keyword argument.
19111917
"""
19121918

1913-
def __init__(self):
1914-
super(QueryJobConfig, self).__init__('query')
1919+
def __init__(self, **kwargs):
1920+
super(QueryJobConfig, self).__init__('query', **kwargs)
19151921

19161922
@property
19171923
def destination_encryption_configuration(self):
19181924
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
19191925
encryption configuration for the destination table.
19201926
1921-
Custom encryption configuration (e.g., Cloud KMS keys) or ``None``
1927+
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
19221928
if using default encryption.
19231929
19241930
See
@@ -1966,7 +1972,8 @@ def create_disposition(self, value):
19661972
@property
19671973
def default_dataset(self):
19681974
"""google.cloud.bigquery.dataset.DatasetReference: the default dataset
1969-
to use for unqualified table names in the query or ``None`` if not set.
1975+
to use for unqualified table names in the query or :data:`None` if not
1976+
set.
19701977
19711978
See
19721979
https://g.co/cloud/bigquery/docs/reference/v2/jobs#configuration.query.defaultDataset
@@ -1986,7 +1993,7 @@ def default_dataset(self, value):
19861993
@property
19871994
def destination(self):
19881995
"""google.cloud.bigquery.table.TableReference: table where results are
1989-
written or ``None`` if not set.
1996+
written or :data:`None` if not set.
19901997
19911998
See
19921999
https://g.co/cloud/bigquery/docs/reference/rest/v2/jobs#configuration.query.destinationTable
@@ -2005,7 +2012,8 @@ def destination(self, value):
20052012

20062013
@property
20072014
def dry_run(self):
2008-
"""bool: ``True`` if this query should be a dry run to estimate costs.
2015+
"""bool: :data:`True` if this query should be a dry run to estimate
2016+
costs.
20092017
20102018
See
20112019
https://g.co/cloud/bigquery/docs/reference/v2/jobs#configuration.dryRun
@@ -2045,7 +2053,7 @@ def maximum_billing_tier(self, value):
20452053

20462054
@property
20472055
def maximum_bytes_billed(self):
2048-
"""int: Maximum bytes to be billed for this job or ``None`` if not set.
2056+
"""int: Maximum bytes to be billed for this job or :data:`None` if not set.
20492057
20502058
See
20512059
https://g.co/cloud/bigquery/docs/reference/rest/v2/jobs#configuration.query.maximumBytesBilled
@@ -2147,7 +2155,7 @@ def write_disposition(self, value):
21472155
@property
21482156
def table_definitions(self):
21492157
"""Dict[str, google.cloud.bigquery.external_config.ExternalConfig]:
2150-
Definitions for external tables or ``None`` if not set.
2158+
Definitions for external tables or :data:`None` if not set.
21512159
21522160
See
21532161
https://g.co/cloud/bigquery/docs/reference/rest/v2/jobs#configuration.query.tableDefinitions
@@ -2305,7 +2313,7 @@ def destination_encryption_configuration(self):
23052313
"""google.cloud.bigquery.table.EncryptionConfiguration: Custom
23062314
encryption configuration for the destination table.
23072315
2308-
Custom encryption configuration (e.g., Cloud KMS keys) or ``None``
2316+
Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None`
23092317
if using default encryption.
23102318
23112319
See

bigquery/tests/unit/test_job.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,13 @@ def _get_target_class():
11521152
from google.cloud.bigquery.job import LoadJobConfig
11531153
return LoadJobConfig
11541154

1155+
def test_ctor_w_properties(self):
1156+
config = self._get_target_class()(
1157+
allow_jagged_rows=True, allow_quoted_newlines=True)
1158+
1159+
self.assertTrue(config.allow_jagged_rows)
1160+
self.assertTrue(config.allow_quoted_newlines)
1161+
11551162
def test_allow_jagged_rows_missing(self):
11561163
config = self._get_target_class()()
11571164
self.assertIsNone(config.allow_jagged_rows)
@@ -2482,6 +2489,20 @@ def _get_target_class():
24822489
from google.cloud.bigquery.job import CopyJobConfig
24832490
return CopyJobConfig
24842491

2492+
def test_ctor_w_properties(self):
2493+
from google.cloud.bigquery.job import CreateDisposition
2494+
from google.cloud.bigquery.job import WriteDisposition
2495+
2496+
create_disposition = CreateDisposition.CREATE_NEVER
2497+
write_disposition = WriteDisposition.WRITE_TRUNCATE
2498+
config = self._get_target_class()(
2499+
create_disposition=create_disposition,
2500+
write_disposition=write_disposition
2501+
)
2502+
2503+
self.assertEqual(config.create_disposition, create_disposition)
2504+
self.assertEqual(config.write_disposition, write_disposition)
2505+
24852506
def test_to_api_repr_with_encryption(self):
24862507
from google.cloud.bigquery.table import EncryptionConfiguration
24872508

@@ -2916,6 +2937,13 @@ def _get_target_class():
29162937
from google.cloud.bigquery.job import ExtractJobConfig
29172938
return ExtractJobConfig
29182939

2940+
def test_ctor_w_properties(self):
2941+
config = self._get_target_class()(
2942+
field_delimiter='\t', print_header=True)
2943+
2944+
self.assertEqual(config.field_delimiter, '\t')
2945+
self.assertTrue(config.print_header)
2946+
29192947
def test_to_api_repr(self):
29202948
from google.cloud.bigquery import job
29212949
config = self._make_one()
@@ -3299,6 +3327,13 @@ def test_ctor_w_none(self):
32993327
self.assertIsNone(config.default_dataset)
33003328
self.assertIsNone(config.destination)
33013329

3330+
def test_ctor_w_properties(self):
3331+
config = self._get_target_class()(
3332+
use_query_cache=False, use_legacy_sql=True)
3333+
3334+
self.assertFalse(config.use_query_cache)
3335+
self.assertTrue(config.use_legacy_sql)
3336+
33023337
def test_time_partitioning(self):
33033338
from google.cloud.bigquery import table
33043339

@@ -3637,8 +3672,7 @@ def test_ctor_w_query_parameters(self):
36373672

36383673
query_parameters = [ScalarQueryParameter("foo", 'INT64', 123)]
36393674
client = _make_client(project=self.PROJECT)
3640-
config = QueryJobConfig()
3641-
config.query_parameters = query_parameters
3675+
config = QueryJobConfig(query_parameters=query_parameters)
36423676
job = self._make_one(
36433677
self.JOB_ID, self.QUERY, client, job_config=config)
36443678
self.assertEqual(job.query_parameters, query_parameters)

0 commit comments

Comments
 (0)