Skip to content

Commit b03ae8e

Browse files
committed
more fixes
1 parent c8166cd commit b03ae8e

5 files changed

Lines changed: 43 additions & 34 deletions

File tree

sdks/python/apache_beam/io/gcp/bigquery.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def chain_after(result):
423423
from google.cloud import bigquery as gcp_bigquery
424424
DatasetReference = gcp_bigquery.DatasetReference
425425
TableReference = gcp_bigquery.TableReference
426-
JobReference = gcp_bigquery.JobReference
426+
SchemaField = gcp_bigquery.SchemaField
427427
except ImportError:
428428

429429
class DatasetReference(object):
@@ -432,9 +432,11 @@ class DatasetReference(object):
432432
class TableReference(object):
433433
pass
434434

435-
class JobReference(object):
435+
class SchemaField(object):
436436
pass
437437

438+
JobReference = bigquery_tools.BeamJobReference
439+
438440

439441
_LOGGER = logging.getLogger(__name__)
440442

@@ -2235,7 +2237,8 @@ def expand(self, pcoll):
22352237
self.table_reference.project == bigquery_tools.FALLBACK_PROJECT):
22362238
self.table_reference = TableReference(
22372239
DatasetReference(
2238-
pcoll.pipeline.options.view_as(GoogleCloudOptions).project,
2240+
(pcoll.pipeline.options.view_as(GoogleCloudOptions).project or
2241+
bigquery_tools.FALLBACK_PROJECT),
22392242
self.table_reference.dataset_id),
22402243
self.table_reference.table_id)
22412244

@@ -2766,7 +2769,7 @@ def __init__(self, schema):
27662769
self._value = schema
27672770

27682771
def __enter__(self):
2769-
if not isinstance(self._value, (gcp_bigquery.SchemaField, )):
2772+
if not isinstance(self._value, (SchemaField, )):
27702773
return bigquery_tools.get_bq_tableschema(self._value)
27712774

27722775
return self._value
@@ -3047,8 +3050,8 @@ def _expand_direct_read(self, pcoll):
30473050
project_id = None
30483051
temp_table_ref = None
30493052
if 'temp_dataset' in self._kwargs:
3050-
temp_table_ref = gcp_bigquery.TableReference(
3051-
gcp_bigquery.DatasetReference(
3053+
temp_table_ref = TableReference(
3054+
DatasetReference(
30523055
self._kwargs['temp_dataset'].project,
30533056
self._kwargs['temp_dataset'].dataset_id),
30543057
'beam_temp_table_' + uuid.uuid4().hex)

sdks/python/apache_beam/io/gcp/bigquery_read_internal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from google.cloud import bigquery as gcp_bigquery
5656
DatasetReference = gcp_bigquery.DatasetReference
5757
TableReference = gcp_bigquery.TableReference
58+
SchemaField = gcp_bigquery.SchemaField
5859
except ImportError:
5960

6061
class DatasetReference(object):
@@ -63,6 +64,9 @@ class DatasetReference(object):
6364
class TableReference(object):
6465
pass
6566

67+
class SchemaField(object):
68+
pass
69+
6670

6771
_LOGGER = logging.getLogger(__name__)
6872

sdks/python/apache_beam/io/gcp/bigquery_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ class DummyTable:
413413
class DummySchema:
414414
fields = []
415415

416-
numBytes = 5
416+
num_bytes = 5
417417
schema = DummySchema()
418418

419419
# TODO(https://github.com/apache/beam/issues/34549): This test relies on
@@ -528,7 +528,7 @@ class DummyTable:
528528
class DummySchema:
529529
fields = []
530530

531-
numBytes = 5
531+
num_bytes = 5
532532
schema = DummySchema()
533533

534534
with mock.patch('time.sleep'), \
@@ -683,7 +683,7 @@ def test_table_spec_display_data(self):
683683
sink = beam.io.BigQuerySink('dataset.table')
684684
dd = DisplayData.create_from(sink)
685685
expected_items = [
686-
DisplayDataItemMatcher('table', 'apache-beam-testing:dataset.table'),
686+
DisplayDataItemMatcher('table', 'dataset.table'),
687687
DisplayDataItemMatcher('validation', False)
688688
]
689689
hc.assert_that(dd.items, hc.contains_inanyorder(*expected_items))

sdks/python/apache_beam/io/gcp/bigquery_tools.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,19 @@ def __repr__(self):
125125
try:
126126
from google.cloud import bigquery as gcp_bigquery
127127
TableReference = gcp_bigquery.TableReference
128+
DatasetReference = gcp_bigquery.DatasetReference
129+
SchemaField = gcp_bigquery.SchemaField
128130
except ImportError:
129131

130132
class TableReference(object):
131133
pass
132134

135+
class DatasetReference(object):
136+
pass
137+
138+
class SchemaField(object):
139+
pass
140+
133141

134142
# pylint: enable=wrong-import-order, wrong-import-position, ungrouped-imports
135143

@@ -216,8 +224,7 @@ def get_hashable_destination(destination):
216224

217225

218226
def to_hashable_table_ref(
219-
table_ref_elem_kv: tuple[Union[str, gcp_bigquery.TableReference], V]
220-
) -> tuple[str, V]:
227+
table_ref_elem_kv: tuple[Union[str, TableReference], V]) -> tuple[str, V]:
221228
"""Turns the key of the input tuple to its string representation. The key
222229
should be either a string or a TableReference.
223230
@@ -243,7 +250,7 @@ def _parse_schema_field(field):
243250
mode = field.get('mode', 'NULLABLE')
244251
description = field.get('description')
245252
fields = tuple([_parse_schema_field(x) for x in field.get('fields', [])])
246-
return gcp_bigquery.SchemaField(
253+
return SchemaField(
247254
field['name'],
248255
field['type'],
249256
mode=mode,
@@ -281,10 +288,9 @@ def parse_table_reference(table, dataset=None, project=None):
281288
format.
282289
"""
283290

284-
if isinstance(table, gcp_bigquery.TableReference):
285-
return gcp_bigquery.TableReference(
286-
gcp_bigquery.DatasetReference(table.project, table.dataset_id),
287-
table.table_id)
291+
if isinstance(table, TableReference):
292+
return TableReference(
293+
DatasetReference(table.project, table.dataset_id), table.table_id)
288294
elif callable(table):
289295
return table
290296
elif isinstance(table, value_provider.ValueProvider):
@@ -307,8 +313,7 @@ def parse_table_reference(table, dataset=None, project=None):
307313
# A dummy project is used. It's often overridden by the pipeline options
308314
project = FALLBACK_PROJECT
309315

310-
return gcp_bigquery.TableReference(
311-
gcp_bigquery.DatasetReference(project, dataset), table)
316+
return TableReference(DatasetReference(project, dataset), table)
312317

313318

314319
# -----------------------------------------------------------------------------
@@ -784,8 +789,8 @@ def _create_table(
784789

785790
additional_parameters = additional_parameters or {}
786791
table = gcp_bigquery.Table(
787-
table_ref=gcp_bigquery.TableReference(
788-
gcp_bigquery.DatasetReference(project_id, dataset_id), table_id),
792+
table_ref=TableReference(
793+
DatasetReference(project_id, dataset_id), table_id),
789794
schema=schema,
790795
**additional_parameters)
791796
response = self.client.create_table(table)
@@ -1327,7 +1332,7 @@ def convert_row_to_dict(self, row, schema):
13271332
if isinstance(schema, (tuple, list)):
13281333
cell = row.f[index]
13291334
value = from_json_value(cell.v) if cell.v is not None else None
1330-
elif isinstance(schema, gcp_bigquery.SchemaField):
1335+
elif isinstance(schema, SchemaField):
13311336
cell = row['f'][index]
13321337
value = cell['v'] if 'v' in cell else None
13331338
if field.mode == 'REPEATED':
@@ -1577,11 +1582,11 @@ def beam_row_from_dict(row: dict, schema):
15771582
Returns:
15781583
~apache_beam.pvalue.Row: The converted row.
15791584
"""
1580-
if not isinstance(schema, (tuple, list, gcp_bigquery.SchemaField)):
1585+
if not isinstance(schema, (tuple, list, SchemaField)):
15811586
schema = get_bq_tableschema(schema)
15821587
beam_row = {}
15831588
fields_to_iterate = schema.fields if isinstance(
1584-
schema, gcp_bigquery.SchemaField) else schema
1589+
schema, SchemaField) else schema
15851590
for field in fields_to_iterate:
15861591
name = field.name
15871592
mode = field.mode.upper()
@@ -1632,7 +1637,7 @@ def get_table_schema_from_string(schema):
16321637
schema_list = [s.strip() for s in schema.split(',')]
16331638
for field_and_type in schema_list:
16341639
field_name, field_type = field_and_type.split(':')
1635-
field_schema = gcp_bigquery.SchemaField(
1640+
field_schema = SchemaField(
16361641
name=field_name, field_type=field_type, mode='NULLABLE')
16371642
table_schema.append(field_schema)
16381643
return table_schema
@@ -1748,11 +1753,11 @@ def get_beam_typehints_from_tableschema(schema, type_overrides=None):
17481753
Nested and repeated fields are supported.
17491754
"""
17501755
effective_types = {**BIGQUERY_TYPE_TO_PYTHON_TYPE, **(type_overrides or {})}
1751-
if not isinstance(schema, (tuple, list, gcp_bigquery.SchemaField)):
1756+
if not isinstance(schema, (tuple, list, SchemaField)):
17521757
schema = get_bq_tableschema(schema)
17531758
typehints = []
17541759
fields_to_iterate = schema.fields if isinstance(
1755-
schema, gcp_bigquery.SchemaField) else schema
1760+
schema, SchemaField) else schema
17561761
for field in fields_to_iterate:
17571762
name, field_type, mode = field.name, field.field_type.upper(), field.mode.upper()
17581763

@@ -1823,11 +1828,10 @@ def check_schema_equal(
18231828
Returns:
18241829
bool: True if the schemas are equivalent, False otherwise.
18251830
"""
1826-
if type(left) != type(right) or not isinstance(
1827-
left, (tuple, gcp_bigquery.SchemaField)):
1831+
if type(left) != type(right) or not isinstance(left, (tuple, SchemaField)):
18281832
return False
18291833

1830-
if isinstance(left, gcp_bigquery.SchemaField):
1834+
if isinstance(left, SchemaField):
18311835
if left.name != right.name:
18321836
return False
18331837

sdks/python/apache_beam/io/gcp/bigquery_tools_test.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@
5656
# Protect against environments where bigquery library is not available.
5757
# pylint: disable=wrong-import-order, wrong-import-position
5858
try:
59-
from apitools.base.py.exceptions import GoogleAPICallError
60-
from apitools.base.py.exceptions import HttpForbiddenError
6159
from google.api_core import exceptions as google_api_core_exceptions
6260
from google.api_core.exceptions import ClientError
6361
from google.api_core.exceptions import DeadlineExceeded
@@ -155,7 +153,7 @@ def test_calling_with_partially_qualified_table_ref(self):
155153
parsed_ref = parse_table_reference(partially_qualified_table)
156154
self.assertEqual(parsed_ref.dataset_id, datasetId)
157155
self.assertEqual(parsed_ref.table_id, tableId)
158-
self.assertEqual(parsed_ref.project, 'apache-beam-testing')
156+
self.assertEqual(parsed_ref.project, 'beam_fallback_project')
159157

160158
def test_calling_with_insufficient_table_ref(self):
161159
table = 'test_table'
@@ -581,7 +579,7 @@ def test_start_query_job_priority_configuration(self):
581579
priority=beam.io.BigQueryQueryPriority.BATCH)
582580

583581
self.assertEqual(
584-
client.query.call_args[0][0].job.configuration.query.priority, 'BATCH')
582+
client.query.call_args[1]['job_config'].priority, 'BATCH')
585583

586584
wrapper._start_query_job(
587585
"my_project",
@@ -592,7 +590,7 @@ def test_start_query_job_priority_configuration(self):
592590
priority=beam.io.BigQueryQueryPriority.INTERACTIVE)
593591

594592
self.assertEqual(
595-
client.query.call_args[0][0].job.configuration.query.priority,
593+
client.query.call_args[1]['job_config'].priority,
596594
'INTERACTIVE')
597595

598596
def test_get_temp_table_project_with_temp_table_ref(self):

0 commit comments

Comments
 (0)