Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit57f940d

Browse files
feat: add ability to set autodetect_schema query param in update_table (#2171)
* Add ability to set autodetect_schema query_param* fixup! Add ability to set autodetect_schema query_param* fixup! Add ability to set autodetect_schema query_param* fixup! Add ability to set autodetect_schema query_param---------Co-authored-by: Chalmer Lowe <chalmerlowe@google.com>
1 parent156e518 commit57f940d

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
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+
ifautodetect_schema:
1447+
query_params= {"autodetect_schema":True}
1448+
else:
1449+
query_params= {}
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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,53 @@ def test_update_table_constraints(self):
978978
)
979979
self.assertIsNone(reference_table3.table_constraints,None)
980980

981+
deftest_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+
989+
# Create an external_config and include it in the table arguments
990+
external_config=bigquery.ExternalConfig(bigquery.ExternalSourceFormat.AVRO)
991+
external_config.source_uris=SOURCE_URIS_AVRO
992+
external_config.reference_file_schema_uri=REFERENCE_FILE_SCHEMA_URI_AVRO
993+
external_config.schema=set_schema
994+
table_arg.external_data_configuration=external_config
995+
996+
self.assertFalse(_table_exists(table_arg))
997+
998+
table=helpers.retry_403(Config.CLIENT.create_table)(table_arg)
999+
self.to_delete.insert(0,table)
1000+
self.assertTrue(_table_exists(table))
1001+
1002+
self.assertEqual(table.schema,set_schema)
1003+
1004+
# Update table with schema autodetection
1005+
updated_table_arg=Table(dataset.table(TABLE_NAME))
1006+
1007+
# Update the external_config and include it in the table arguments
1008+
updated_external_config=copy.deepcopy(external_config)
1009+
updated_external_config.autodetect=True
1010+
updated_external_config.schema=None
1011+
updated_table_arg.external_data_configuration=updated_external_config
1012+
1013+
# PATCH call with autodetect_schema=True to trigger schema inference
1014+
updated_table=Config.CLIENT.update_table(
1015+
updated_table_arg, ["external_data_configuration"],autodetect_schema=True
1016+
)
1017+
1018+
# The updated table should have a schema inferred from the reference
1019+
# file, which has all four fields.
1020+
expected_schema= [
1021+
bigquery.SchemaField("username","STRING",mode="NULLABLE"),
1022+
bigquery.SchemaField("tweet","STRING",mode="NULLABLE"),
1023+
bigquery.SchemaField("timestamp","STRING",mode="NULLABLE"),
1024+
bigquery.SchemaField("likes","INTEGER",mode="NULLABLE"),
1025+
]
1026+
self.assertEqual(updated_table.schema,expected_schema)
1027+
9811028
@staticmethod
9821029
def_fetch_single_page(table,selected_fields=None):
9831030
iterator=Config.CLIENT.list_rows(table,selected_fields=selected_fields)

‎tests/unit/test_client.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,7 +2385,7 @@ def test_update_table(self):
23852385
"resourceTags": {"123456789012/key":"value"},
23862386
}
23872387
conn.api_request.assert_called_once_with(
2388-
method="PATCH",data=sent,path="/"+path,timeout=7.5
2388+
method="PATCH",data=sent,path="/"+path,timeout=7.5,query_params={}
23892389
)
23902390
self.assertEqual(updated_table.description,table.description)
23912391
self.assertEqual(updated_table.friendly_name,table.friendly_name)
@@ -2439,6 +2439,7 @@ def test_update_table_w_custom_property(self):
24392439
path="/%s"%path,
24402440
data={"newAlphaProperty":"unreleased property"},
24412441
timeout=DEFAULT_TIMEOUT,
2442+
query_params={},
24422443
)
24432444
self.assertEqual(
24442445
updated_table._properties["newAlphaProperty"],"unreleased property"
@@ -2475,6 +2476,7 @@ def test_update_table_only_use_legacy_sql(self):
24752476
path="/%s"%path,
24762477
data={"view": {"useLegacySql":True}},
24772478
timeout=DEFAULT_TIMEOUT,
2479+
query_params={},
24782480
)
24792481
self.assertEqual(updated_table.view_use_legacy_sql,table.view_use_legacy_sql)
24802482

@@ -2567,9 +2569,10 @@ def test_update_table_w_query(self):
25672569
"schema":schema_resource,
25682570
},
25692571
timeout=DEFAULT_TIMEOUT,
2572+
query_params={},
25702573
)
25712574

2572-
deftest_update_table_w_schema_None(self):
2575+
deftest_update_table_w_schema_None_autodetect_schema(self):
25732576
# Simulate deleting schema: not sure if back-end will actually
25742577
# allow this operation, but the spec says it is optional.
25752578
path="projects/%s/datasets/%s/tables/%s"% (
@@ -2611,7 +2614,9 @@ def test_update_table_w_schema_None(self):
26112614
withmock.patch(
26122615
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
26132616
)asfinal_attributes:
2614-
updated_table=client.update_table(table, ["schema"])
2617+
updated_table=client.update_table(
2618+
table, ["schema"],autodetect_schema=True
2619+
)
26152620

26162621
final_attributes.assert_called_once_with(
26172622
{"path":"/%s"%path,"fields": ["schema"]},client,None
@@ -2623,6 +2628,7 @@ def test_update_table_w_schema_None(self):
26232628
sent= {"schema": {"fields":None}}
26242629
self.assertEqual(req[1]["data"],sent)
26252630
self.assertEqual(req[1]["path"],"/%s"%path)
2631+
self.assertEqual(req[1]["query_params"], {"autodetect_schema":True})
26262632
self.assertEqual(len(updated_table.schema),0)
26272633

26282634
deftest_update_table_delete_property(self):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp