@@ -2039,6 +2039,118 @@ def execute(self, context) -> None:
20392039 )
20402040
20412041
2042+ class BigQueryUpdateTableSchemaOperator (BaseOperator ):
2043+ """
2044+ Update BigQuery Table Schema
2045+ Updates fields on a table schema based on contents of the supplied schema_fields_updates
2046+ parameter. The supplied schema does not need to be complete, if the field
2047+ already exists in the schema you only need to supply keys & values for the
2048+ items you want to patch, just ensure the "name" key is set.
2049+
2050+ .. seealso::
2051+ For more information on how to use this operator, take a look at the guide:
2052+ :ref:`howto/operator:BigQueryUpdateTableSchemaOperator`
2053+
2054+ :param schema_fields_updates: a partial schema resource. see
2055+ https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#TableSchema
2056+
2057+ **Example**: ::
2058+
2059+ schema_fields_updates=[
2060+ {"name": "emp_name", "description": "Some New Description"},
2061+ {"name": "salary", "policyTags": {'names': ['some_new_policy_tag']},},
2062+ {"name": "departments", "fields": [
2063+ {"name": "name", "description": "Some New Description"},
2064+ {"name": "type", "description": "Some New Description"}
2065+ ]},
2066+ ]
2067+
2068+ :type schema_fields_updates: List[dict]
2069+ :param include_policy_tags: (Optional) If set to True policy tags will be included in
2070+ the update request which requires special permissions even if unchanged (default False)
2071+ see https://cloud.google.com/bigquery/docs/column-level-security#roles
2072+ :type include_policy_tags: bool
2073+ :param dataset_id: A dotted
2074+ ``(<project>.|<project>:)<dataset>`` that indicates which dataset
2075+ will be updated. (templated)
2076+ :type dataset_id: str
2077+ :param table_id: The table ID of the requested table. (templated)
2078+ :type table_id: str
2079+ :param project_id: The name of the project where we want to update the dataset.
2080+ Don't need to provide, if projectId in dataset_reference.
2081+ :type project_id: str
2082+ :param gcp_conn_id: (Optional) The connection ID used to connect to Google Cloud.
2083+ :type gcp_conn_id: str
2084+ :param bigquery_conn_id: (Deprecated) The connection ID used to connect to Google Cloud.
2085+ This parameter has been deprecated. You should pass the gcp_conn_id parameter instead.
2086+ :type bigquery_conn_id: str
2087+ :param delegate_to: The account to impersonate, if any.
2088+ For this to work, the service account making the request must have domain-wide
2089+ delegation enabled.
2090+ :type delegate_to: str
2091+ :param location: The location used for the operation.
2092+ :type location: str
2093+ :param impersonation_chain: Optional service account to impersonate using short-term
2094+ credentials, or chained list of accounts required to get the access_token
2095+ of the last account in the list, which will be impersonated in the request.
2096+ If set as a string, the account must grant the originating account
2097+ the Service Account Token Creator IAM role.
2098+ If set as a sequence, the identities from the list must grant
2099+ Service Account Token Creator IAM role to the directly preceding identity, with first
2100+ account from the list granting this role to the originating account (templated).
2101+ :type impersonation_chain: Union[str, Sequence[str]]
2102+ """
2103+
2104+ template_fields = (
2105+ 'schema_fields_updates' ,
2106+ 'dataset_id' ,
2107+ 'table_id' ,
2108+ 'project_id' ,
2109+ 'impersonation_chain' ,
2110+ )
2111+ template_fields_renderers = {"schema_fields_updates" : "json" }
2112+ ui_color = BigQueryUIColors .TABLE .value
2113+
2114+ @apply_defaults
2115+ def __init__ (
2116+ self ,
2117+ * ,
2118+ schema_fields_updates : List [Dict [str , Any ]],
2119+ include_policy_tags : Optional [bool ] = False ,
2120+ dataset_id : Optional [str ] = None ,
2121+ table_id : Optional [str ] = None ,
2122+ project_id : Optional [str ] = None ,
2123+ gcp_conn_id : str = 'google_cloud_default' ,
2124+ delegate_to : Optional [str ] = None ,
2125+ impersonation_chain : Optional [Union [str , Sequence [str ]]] = None ,
2126+ ** kwargs ,
2127+ ) -> None :
2128+ self .schema_fields_updates = schema_fields_updates
2129+ self .include_policy_tags = include_policy_tags
2130+ self .table_id = table_id
2131+ self .dataset_id = dataset_id
2132+ self .project_id = project_id
2133+ self .gcp_conn_id = gcp_conn_id
2134+ self .delegate_to = delegate_to
2135+ self .impersonation_chain = impersonation_chain
2136+ super ().__init__ (** kwargs )
2137+
2138+ def execute (self , context ):
2139+ bq_hook = BigQueryHook (
2140+ gcp_conn_id = self .gcp_conn_id ,
2141+ delegate_to = self .delegate_to ,
2142+ impersonation_chain = self .impersonation_chain ,
2143+ )
2144+
2145+ return bq_hook .update_table_schema (
2146+ schema_fields_updates = self .schema_fields_updates ,
2147+ include_policy_tags = self .include_policy_tags ,
2148+ dataset_id = self .dataset_id ,
2149+ table_id = self .table_id ,
2150+ project_id = self .project_id ,
2151+ )
2152+
2153+
20422154# pylint: disable=too-many-arguments
20432155class BigQueryInsertJobOperator (BaseOperator ):
20442156 """
0 commit comments