3232 CommentCreationTable ,
3333 CommentCreationView ,
3434 DataObject ,
35+ DataObjectType ,
3536 EngineRunMode ,
3637 InsertOverwriteStrategy ,
3738 SourceQuery ,
@@ -369,6 +370,9 @@ def replace_query(
369370 kwargs: Optional create table properties.
370371 """
371372 target_table = exp .to_table (table_name )
373+
374+ table_exists = self ._drop_data_object_on_type_mismatch (target_table , DataObjectType .TABLE )
375+
372376 source_queries , columns_to_types = self ._get_source_queries_and_columns_to_types (
373377 query_or_df , columns_to_types , target_table = target_table
374378 )
@@ -390,7 +394,7 @@ def replace_query(
390394 )
391395 # All engines support `CREATE TABLE AS` so we use that if the table doesn't already exist and we
392396 # use `CREATE OR REPLACE TABLE AS` if the engine supports it
393- if self .SUPPORTS_REPLACE_TABLE or not self . table_exists ( target_table ) :
397+ if self .SUPPORTS_REPLACE_TABLE or not table_exists :
394398 return self ._create_table_from_source_queries (
395399 target_table ,
396400 source_queries ,
@@ -930,6 +934,28 @@ def clone_table(
930934 )
931935 )
932936
937+ def drop_data_object (self , data_object : DataObject , ignore_if_not_exists : bool = True ) -> None :
938+ """Drops a data object of arbitrary type.
939+
940+ Args:
941+ data_object: The data object to drop.
942+ ignore_if_not_exists: If True, no error will be raised if the data object does not exist.
943+ """
944+ if data_object .type .is_view :
945+ self .drop_view (data_object .to_table (), ignore_if_not_exists = ignore_if_not_exists )
946+ elif data_object .type .is_materialized_view :
947+ self .drop_view (
948+ data_object .to_table (), ignore_if_not_exists = ignore_if_not_exists , materialized = True
949+ )
950+ elif data_object .type .is_table :
951+ self .drop_table (data_object .to_table (), exists = ignore_if_not_exists )
952+ elif data_object .type .is_managed_table :
953+ self .drop_managed_table (data_object .to_table (), exists = ignore_if_not_exists )
954+ else :
955+ raise SQLMeshError (
956+ f"Can't drop data object '{ data_object .to_table ().sql (dialect = self .dialect )} ' of type '{ data_object .type .value } '"
957+ )
958+
933959 def drop_table (self , table_name : TableName , exists : bool = True ) -> None :
934960 """Drops a table.
935961
@@ -1118,6 +1144,12 @@ def create_view(
11181144 if properties .expressions :
11191145 create_kwargs ["properties" ] = properties
11201146
1147+ if replace :
1148+ self ._drop_data_object_on_type_mismatch (
1149+ view_name ,
1150+ DataObjectType .VIEW if not materialized else DataObjectType .MATERIALIZED_VIEW ,
1151+ )
1152+
11211153 with source_queries [0 ] as query :
11221154 self .execute (
11231155 exp .Create (
@@ -2483,6 +2515,35 @@ def _truncate_table(self, table_name: TableName) -> None:
24832515 table = exp .to_table (table_name )
24842516 self .execute (f"TRUNCATE TABLE { table .sql (dialect = self .dialect , identify = True )} " )
24852517
2518+ def _drop_data_object_on_type_mismatch (
2519+ self , target_name : TableName , expected_type : DataObjectType
2520+ ) -> bool :
2521+ """Drops a data object if it exists and is not of the expected type.
2522+
2523+ Args:
2524+ target_name: The name of the data object to check.
2525+ expected_type: The expected type of the data object.
2526+
2527+ Returns:
2528+ True if the data object exists and is of the expected type, False otherwise.
2529+ """
2530+ target_table = exp .to_table (target_name )
2531+ existing_data_objects = self .get_data_objects (
2532+ schema_ (target_table .db , target_table .catalog ), {target_table .name }
2533+ )
2534+ if existing_data_objects :
2535+ if existing_data_objects [0 ].type == expected_type :
2536+ return True
2537+
2538+ logger .warning (
2539+ "Target data object '%s' is a %s and not a %s, dropping it" ,
2540+ target_table .sql (dialect = self .dialect ),
2541+ existing_data_objects [0 ].type .value ,
2542+ expected_type .value ,
2543+ )
2544+ self .drop_data_object (existing_data_objects [0 ])
2545+ return False
2546+
24862547 def _replace_by_key (
24872548 self ,
24882549 target_table : TableName ,
0 commit comments