@@ -978,6 +978,54 @@ def test_update_table_constraints(self):
978
978
)
979
979
self .assertIsNone (reference_table3 .table_constraints , None )
980
980
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
+
981
1029
@staticmethod
982
1030
def _fetch_single_page (table , selected_fields = None ):
983
1031
iterator = Config .CLIENT .list_rows (table , selected_fields = selected_fields )
0 commit comments