Skip to content

Commit f6db150

Browse files
committed
bigquery: modify CopyJob
Update CopyJob and CopyJobConfig to conform to the new design for jobs.
1 parent 8a65026 commit f6db150

File tree

7 files changed

+247
-127
lines changed

7 files changed

+247
-127
lines changed

bigquery/google/cloud/bigquery/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from google.cloud.bigquery.client import Client
3333
from google.cloud.bigquery.dataset import AccessEntry
3434
from google.cloud.bigquery.dataset import Dataset
35+
from google.cloud.bigquery.job import CopyJobConfig
3536
from google.cloud.bigquery.job import ExtractJobConfig
3637
from google.cloud.bigquery.schema import SchemaField
3738
from google.cloud.bigquery.table import Table
@@ -42,6 +43,7 @@
4243
'ArrayQueryParameter',
4344
'Client',
4445
'Dataset',
46+
'CopyJobConfig',
4547
'ExtractJobConfig',
4648
'ScalarQueryParameter',
4749
'SchemaField',

bigquery/google/cloud/bigquery/_helpers.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,6 @@ def __init__(self, name, type_, value):
527527
self.type_ = type_
528528
self.value = value
529529

530-
def __eq__(self, other):
531-
if not isinstance(other, ScalarQueryParameter):
532-
return NotImplemented
533-
return(
534-
self.name == other.name and
535-
self.type_ == other.type_ and
536-
self.value == other.value)
537-
538530
@classmethod
539531
def positional(cls, type_, value):
540532
"""Factory for positional paramater.
@@ -637,14 +629,6 @@ def __init__(self, name, array_type, values):
637629
self.array_type = array_type
638630
self.values = values
639631

640-
def __eq__(self, other):
641-
if not isinstance(other, ArrayQueryParameter):
642-
return NotImplemented
643-
return(
644-
self.name == other.name and
645-
self.array_type == other.array_type and
646-
self.values == other.values)
647-
648632
@classmethod
649633
def positional(cls, array_type, values):
650634
"""Factory for positional parameters.
@@ -789,14 +773,6 @@ def __init__(self, name, *sub_params):
789773
types[sub.name] = sub.type_
790774
values[sub.name] = sub.value
791775

792-
def __eq__(self, other):
793-
if not isinstance(other, StructQueryParameter):
794-
return NotImplemented
795-
return(
796-
self.name == other.name and
797-
self.struct_types == other.struct_types and
798-
self.struct_values == other.struct_values)
799-
800776
@classmethod
801777
def positional(cls, *sub_params):
802778
"""Factory for positional parameters.

bigquery/google/cloud/bigquery/client.py

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from __future__ import absolute_import
1818

19+
import collections
1920
import uuid
2021

2122
from google.api.core import page_iterator
@@ -492,25 +493,39 @@ def load_table_from_storage(self, job_id, destination, *source_uris):
492493
"""
493494
return LoadJob(job_id, destination, source_uris, client=self)
494495

495-
def copy_table(self, job_id, destination, *sources):
496-
"""Construct a job for copying one or more tables into another table.
496+
def copy_table(self, sources, destination, job_id=None, job_config=None):
497+
"""Start a job for copying one or more tables into another table.
497498
498499
See
499500
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.copy
500501
501-
:type job_id: str
502-
:param job_id: Name of the job.
502+
:type sources: One of:
503+
:class:`~google.cloud.bigquery.table.TableReference`
504+
sequence of
505+
:class:`~google.cloud.bigquery.table.TableReference`
506+
:param sources: Table or tables to be copied.
503507
504-
:type destination: :class:`google.cloud.bigquery.table.Table`
508+
509+
:type destination: :class:`google.cloud.bigquery.table.TableReference`
505510
:param destination: Table into which data is to be copied.
506511
507-
:type sources: sequence of :class:`google.cloud.bigquery.table.Table`
508-
:param sources: tables to be copied.
512+
:type job_id: str
513+
:param job_id: (Optional) The ID of the job.
514+
515+
:type job_config: :class:`google.cloud.bigquery.job.CopyJobConfig`
516+
:param job_config: (Optional) Extra configuration options for the job.
509517
510518
:rtype: :class:`google.cloud.bigquery.job.CopyJob`
511519
:returns: a new ``CopyJob`` instance
512520
"""
513-
return CopyJob(job_id, destination, sources, client=self)
521+
job_id = _make_job_id(job_id)
522+
523+
if not isinstance(sources, collections.Sequence):
524+
sources = [sources]
525+
job = CopyJob(job_id, sources, destination, client=self,
526+
job_config=job_config)
527+
job.begin()
528+
return job
514529

515530
def extract_table(self, source, *destination_uris, **kwargs):
516531
"""Start a job to extract a table into Cloud Storage files.
@@ -541,9 +556,7 @@ def extract_table(self, source, *destination_uris, **kwargs):
541556
:returns: a new ``ExtractJob`` instance
542557
"""
543558
job_config = kwargs.get('job_config')
544-
job_id = kwargs.get('job_id')
545-
if job_id is None:
546-
job_id = str(uuid.uuid4())
559+
job_id = _make_job_id(kwargs.get('job_id'))
547560

548561
job = ExtractJob(
549562
job_id, source, list(destination_uris), client=self,
@@ -667,3 +680,17 @@ def _item_to_table(iterator, resource):
667680
:returns: The next table in the page.
668681
"""
669682
return Table.from_api_repr(resource, iterator.client)
683+
684+
685+
def _make_job_id(job_id):
686+
"""Construct an ID for a new job.
687+
688+
:type job_id: str or ``NoneType``
689+
:param job_id: the user-provided job ID
690+
691+
:rtype: str
692+
:returns: A job ID
693+
"""
694+
if job_id is None:
695+
return str(uuid.uuid4())
696+
return job_id

0 commit comments

Comments
 (0)