Skip to content

Commit a90932d

Browse files
committed
address comments
1 parent 8fe9b65 commit a90932d

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

sqlmesh/core/engine_adapter/base.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
)
4141
from sqlmesh.core.model.kind import TimeColumn
4242
from sqlmesh.core.schema_diff import SchemaDiffer
43-
from sqlmesh.utils import columns_to_types_all_known, random_id, CorrelationId
44-
from sqlmesh.utils.connection_pool import create_connection_pool, ConnectionPool
43+
from sqlmesh.utils import CorrelationId, columns_to_types_all_known, random_id
44+
from sqlmesh.utils.connection_pool import ConnectionPool, create_connection_pool
4545
from sqlmesh.utils.date import TimeLike, make_inclusive, to_time_column
4646
from sqlmesh.utils.errors import (
47+
MissingDefaultCatalogError,
4748
SQLMeshError,
4849
UnsupportedCatalogOperationError,
49-
MissingDefaultCatalogError,
5050
)
5151
from sqlmesh.utils.pandas import columns_to_types_from_df
5252

@@ -55,8 +55,8 @@
5555

5656
from sqlmesh.core._typing import SchemaName, SessionProperties, TableName
5757
from sqlmesh.core.engine_adapter._typing import (
58-
BigframeSession,
5958
DF,
59+
BigframeSession,
6060
PySparkDataFrame,
6161
PySparkSession,
6262
Query,
@@ -371,7 +371,9 @@ def replace_query(
371371
"""
372372
target_table = exp.to_table(table_name)
373373

374-
table_exists = self._drop_data_object_on_type_mismatch(target_table, DataObjectType.TABLE)
374+
target_data_object = self._get_data_object(target_table)
375+
table_exists = target_data_object is not None
376+
self._drop_data_object_on_type_mismatch(target_data_object, DataObjectType.TABLE)
375377

376378
source_queries, columns_to_types = self._get_source_queries_and_columns_to_types(
377379
query_or_df, columns_to_types, target_table=target_table
@@ -1146,7 +1148,7 @@ def create_view(
11461148

11471149
if replace:
11481150
self._drop_data_object_on_type_mismatch(
1149-
view_name,
1151+
self._get_data_object(view_name),
11501152
DataObjectType.VIEW if not materialized else DataObjectType.MATERIALIZED_VIEW,
11511153
)
11521154

@@ -2515,34 +2517,34 @@ def _truncate_table(self, table_name: TableName) -> None:
25152517
table = exp.to_table(table_name)
25162518
self.execute(f"TRUNCATE TABLE {table.sql(dialect=self.dialect, identify=True)}")
25172519

2520+
def _get_data_object(self, target_name: TableName) -> t.Optional[DataObject]:
2521+
target_table = exp.to_table(target_name)
2522+
existing_data_objects = self.get_data_objects(
2523+
schema_(target_table.db, target_table.catalog), {target_table.name}
2524+
)
2525+
if existing_data_objects:
2526+
return existing_data_objects[0]
2527+
return None
2528+
25182529
def _drop_data_object_on_type_mismatch(
2519-
self, target_name: TableName, expected_type: DataObjectType
2520-
) -> bool:
2530+
self, data_object: t.Optional[DataObject], expected_type: DataObjectType
2531+
) -> None:
25212532
"""Drops a data object if it exists and is not of the expected type.
25222533
25232534
Args:
2524-
target_name: The name of the data object to check.
2535+
data_object: The data object to check.
25252536
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.
25292537
"""
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
2538+
if data_object is None or data_object.type == expected_type:
2539+
return
25372540

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
2541+
logger.warning(
2542+
"Target data object '%s' is a %s and not a %s, dropping it",
2543+
data_object.to_table().sql(dialect=self.dialect),
2544+
data_object.type.value,
2545+
expected_type.value,
2546+
)
2547+
self.drop_data_object(data_object)
25462548

25472549
def _replace_by_key(
25482550
self,

sqlmesh/core/engine_adapter/redshift.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ def replace_query(
262262
"""
263263
import pandas as pd
264264

265-
table_exists = self._drop_data_object_on_type_mismatch(table_name, DataObjectType.TABLE)
265+
target_data_object = self._get_data_object(table_name)
266+
table_exists = target_data_object is not None
267+
self._drop_data_object_on_type_mismatch(target_data_object, DataObjectType.TABLE)
266268

267269
if not isinstance(query_or_df, pd.DataFrame) or not table_exists:
268270
return super().replace_query(

0 commit comments

Comments
 (0)