Skip to content

Commit aa6b9eb

Browse files
committed
Add ability to set autodetect_schema query_param
1 parent 7301667 commit aa6b9eb

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

google/cloud/bigquery/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,7 @@ def update_table(
13891389
self,
13901390
table: Table,
13911391
fields: Sequence[str],
1392+
autodetect_schema: bool = False,
13921393
retry: retries.Retry = DEFAULT_RETRY,
13931394
timeout: TimeoutType = DEFAULT_TIMEOUT,
13941395
) -> Table:
@@ -1419,6 +1420,10 @@ def update_table(
14191420
fields (Sequence[str]):
14201421
The fields of ``table`` to change, spelled as the
14211422
:class:`~google.cloud.bigquery.table.Table` properties.
1423+
autodetect_schema (bool):
1424+
Specifies if the schema of the table should be autodetected when
1425+
updating the table from the underlying source. Only applicable
1426+
for external tables.
14221427
retry (Optional[google.api_core.retry.Retry]):
14231428
A description of how to retry the API call.
14241429
timeout (Optional[float]):
@@ -1438,12 +1443,18 @@ def update_table(
14381443
path = table.path
14391444
span_attributes = {"path": path, "fields": fields}
14401445

1446+
if autodetect_schema:
1447+
query_params = {"autodetect_schema": True}
1448+
else:
1449+
query_params = None
1450+
14411451
api_response = self._call_api(
14421452
retry,
14431453
span_name="BigQuery.updateTable",
14441454
span_attributes=span_attributes,
14451455
method="PATCH",
14461456
path=path,
1457+
query_params=query_params,
14471458
data=partial,
14481459
headers=headers,
14491460
timeout=timeout,

tests/system/test_client.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,54 @@ def test_update_table_constraints(self):
978978
)
979979
self.assertIsNone(reference_table3.table_constraints, None)
980980

981+
def test_update_table_autodetect_schema(self):
982+
dataset = self.temp_dataset(_make_dataset_id("bq_update_table_test"))
983+
984+
# Create an external table, restrict schema to one field
985+
TABLE_NAME = "test_table"
986+
set_schema = [bigquery.SchemaField("username", "STRING", mode="NULLABLE")]
987+
table_arg = Table(dataset.table(TABLE_NAME))
988+
external_config = bigquery.ExternalConfig(bigquery.ExternalSourceFormat.AVRO)
989+
external_config.source_uris = SOURCE_URIS_AVRO
990+
external_config.reference_file_schema_uri = REFERENCE_FILE_SCHEMA_URI_AVRO
991+
external_config.schema = set_schema
992+
table_arg.external_data_configuration = external_config
993+
994+
self.assertFalse(_table_exists(table_arg))
995+
996+
table = helpers.retry_403(Config.CLIENT.create_table)(table_arg)
997+
self.to_delete.insert(0, table)
998+
self.assertTrue(_table_exists(table))
999+
1000+
self.assertEqual(table.schema, set_schema)
1001+
1002+
# Update table with schema autodetection
1003+
updated_table_arg = Table(dataset.table(TABLE_NAME))
1004+
updated_external_config = bigquery.ExternalConfig(
1005+
bigquery.ExternalSourceFormat.AVRO
1006+
)
1007+
updated_external_config.source_uris = SOURCE_URIS_AVRO
1008+
updated_external_config.reference_file_schema_uri = (
1009+
REFERENCE_FILE_SCHEMA_URI_AVRO
1010+
)
1011+
updated_external_config.autodetect = True
1012+
updated_external_config.schema = None
1013+
updated_table_arg.external_data_configuration = updated_external_config
1014+
1015+
updated_table = Config.CLIENT.update_table(
1016+
updated_table_arg, ["external_data_configuration"], autodetect_schema=True
1017+
)
1018+
1019+
# The updated table shlould have a schema inferred from the reference
1020+
# file, which has all four fields.
1021+
expected_schema = [
1022+
bigquery.SchemaField("username", "STRING", mode="NULLABLE"),
1023+
bigquery.SchemaField("tweet", "STRING", mode="NULLABLE"),
1024+
bigquery.SchemaField("timestamp", "STRING", mode="NULLABLE"),
1025+
bigquery.SchemaField("likes", "INTEGER", mode="NULLABLE"),
1026+
]
1027+
self.assertEqual(updated_table.schema, expected_schema)
1028+
9811029
@staticmethod
9821030
def _fetch_single_page(table, selected_fields=None):
9831031
iterator = Config.CLIENT.list_rows(table, selected_fields=selected_fields)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy