3333from dbt_common .events .functions import fire_event
3434import dbt_common .exceptions
3535import dbt_common .exceptions .base
36+ from dbt_common .exceptions import DbtInternalError
3637from dbt_common .utils import filter_null_values
3738from dbt .adapters .base import (
3839 AdapterConfig ,
4748from dbt .adapters .base .impl import FreshnessResponse
4849from dbt .adapters .cache import _make_ref_key_dict
4950from dbt .adapters .capability import Capability , CapabilityDict , CapabilitySupport , Support
51+ from dbt .adapters .catalogs import CatalogRelation
5052from dbt .adapters .contracts .connection import AdapterResponse
5153from dbt .adapters .contracts .macros import MacroResolverProtocol
5254from dbt .adapters .contracts .relation import RelationConfig
5355from dbt .adapters .events .logging import AdapterLogger
5456from dbt .adapters .events .types import SchemaCreation , SchemaDrop
5557
58+ from dbt .adapters .bigquery import constants , parse_model
59+ from dbt .adapters .bigquery .catalogs import (
60+ BigQueryCatalogIntegration ,
61+ BigQueryCatalogRelation ,
62+ )
5663from dbt .adapters .bigquery .column import BigQueryColumn , get_nested_column_data_types
5764from dbt .adapters .bigquery .connections import BigQueryAdapterResponse , BigQueryConnectionManager
5865from dbt .adapters .bigquery .dataset import add_access_entry_to_dataset , is_access_entry_in_dataset
@@ -129,6 +136,7 @@ class BigQueryAdapter(BaseAdapter):
129136
130137 AdapterSpecificConfigs = BigqueryConfig
131138
139+ CATALOG_INTEGRATIONS = [BigQueryCatalogIntegration ]
132140 CONSTRAINT_SUPPORT = {
133141 ConstraintType .check : ConstraintSupport .NOT_SUPPORTED ,
134142 ConstraintType .not_null : ConstraintSupport .ENFORCED ,
@@ -147,6 +155,8 @@ class BigQueryAdapter(BaseAdapter):
147155 def __init__ (self , config , mp_context : SpawnContext ) -> None :
148156 super ().__init__ (config , mp_context )
149157 self .connections : BigQueryConnectionManager = self .connections
158+ self .add_catalog_integration (constants .DEFAULT_INFO_SCHEMA_CATALOG )
159+ self .add_catalog_integration (constants .DEFAULT_ICEBERG_CATALOG )
150160
151161 ###
152162 # Implementations of abstract methods
@@ -797,6 +807,17 @@ def get_table_options(
797807 if config .get ("partition_expiration_days" ) is not None :
798808 opts ["partition_expiration_days" ] = config .get ("partition_expiration_days" )
799809
810+ relation_config = getattr (config , "model" , None )
811+ if not temporary and (
812+ catalog_relation := self .build_catalog_relation (relation_config )
813+ ):
814+ if not isinstance (catalog_relation , BigQueryCatalogRelation ):
815+ raise DbtInternalError ("Unexpected catalog relation" )
816+ if catalog_relation .table_format == constants .ICEBERG_TABLE_FORMAT :
817+ opts ["table_format" ] = f"'{ catalog_relation .table_format } '"
818+ opts ["file_format" ] = f"'{ catalog_relation .file_format } '"
819+ opts ["storage_uri" ] = f"'{ catalog_relation .external_volume } '"
820+
800821 return opts
801822
802823 @available .parse (lambda * a , ** k : {})
@@ -986,3 +1007,23 @@ def validate_sql(self, sql: str) -> AdapterResponse:
9861007 :param str sql: The sql to validate
9871008 """
9881009 return self .connections .dry_run (sql )
1010+
1011+ @available
1012+ def build_catalog_relation (self , model : RelationConfig ) -> Optional [CatalogRelation ]:
1013+ """
1014+ Builds a relation for a given configuration.
1015+
1016+ This method uses the provided configuration to determine the appropriate catalog
1017+ integration and config parser for building the relation. It defaults to the information schema
1018+ catalog if none is provided in the configuration for backward compatibility.
1019+
1020+ Args:
1021+ model (RelationConfig): `config.model` (not `model`) from the jinja context
1022+
1023+ Returns:
1024+ Any: The constructed relation object generated through the catalog integration and parser
1025+ """
1026+ if catalog := parse_model .catalog_name (model ):
1027+ catalog_integration = self .get_catalog_integration (catalog )
1028+ return catalog_integration .build_relation (model )
1029+ return None
0 commit comments