[docs]defvalidate_column_in_bq_schema(columns:dict,column_name:str,expected_types:list,expected_modes:list)->None:"""Validates a column within a BigQuery schema. Args: columns: A dictionary of BigQuery SchemaField objects representing the table schema. column_name: The name of the column to validate. expected_types: A list of acceptable data types for the column. expected_modes: A list of acceptable modes for the column. Raises: ValueError: If the column doesn't exist, has an unacceptable type, or has an unacceptable mode. """ifcolumn_namenotincolumns:raiseValueError(f"Column {column_name} is missing from the schema.")column=columns[column_name]ifcolumn.field_typenotinexpected_types:raiseValueError(f"Column {column_name} must be one of the following types: {expected_types}")ifcolumn.modenotinexpected_modes:raiseValueError(f"Column {column_name} must be one of the following modes: {expected_modes}")
[docs]defdoc_match_filter(document:Dict[str,Any],filter:Dict[str,Any])->bool:forcolumn,valueinfilter.items():# ignore fields that are not part of the documentifdocument.get(column,value)!=value:returnFalsereturnTrue
[docs]defcheck_bq_dataset_exists(client:Any,dataset_id:str)->bool:fromgoogle.cloudimportbigquery# type: ignore[attr-defined]ifnotisinstance(client,bigquery.Client):raiseTypeError("client must be an instance of bigquery.Client")try:client.get_dataset(dataset_id)# Make an API request.returnTrueexceptNotFound:returnFalse