11from __future__ import annotations
22
33import typing as t
4- import uuid
54
65import pandas as pd
76from sqlglot import exp
1211 GetCurrentCatalogFromFunctionMixin ,
1312 LogicalMergeMixin ,
1413 LogicalReplaceQueryMixin ,
14+ NonTransactionalTruncateMixin ,
1515)
1616from sqlmesh .core .engine_adapter .shared import DataObject , DataObjectType , set_catalog
1717
@@ -25,6 +25,7 @@ class RedshiftEngineAdapter(
2525 LogicalReplaceQueryMixin ,
2626 LogicalMergeMixin ,
2727 GetCurrentCatalogFromFunctionMixin ,
28+ NonTransactionalTruncateMixin ,
2829):
2930 DIALECT = "redshift"
3031 ESCAPE_JSON = True
@@ -180,12 +181,8 @@ def replace_query(
180181 columns_to_types = columns_to_types or self .columns (table_name )
181182 target_table = exp .to_table (table_name )
182183 with self .transaction ():
183- temp_table_name = f"{ target_table .alias_or_name } _temp_{ self ._short_hash ()} "
184- temp_table = target_table .copy ()
185- temp_table .set ("this" , exp .to_identifier (temp_table_name ))
186- old_table_name = f"{ target_table .alias_or_name } _old_{ self ._short_hash ()} "
187- old_table = target_table .copy ()
188- old_table .set ("this" , exp .to_identifier (old_table_name ))
184+ temp_table = self ._get_temp_table (target_table )
185+ old_table = self ._get_temp_table (target_table )
189186 self .create_table (temp_table , columns_to_types , exists = False , ** kwargs )
190187 self ._insert_append_source_queries (temp_table , source_queries , columns_to_types )
191188 self .rename_table (target_table , old_table )
@@ -200,17 +197,18 @@ def _get_data_objects(self, schema_name: SchemaName) -> t.List[DataObject]:
200197 """
201198 Returns all the data objects that exist in the given schema and optionally catalog.
202199 """
200+ catalog_name = self .get_current_catalog ()
203201 query = f"""
204202 SELECT
205- null AS catalog_name,
203+ ' { catalog_name } ' AS catalog_name,
206204 tablename AS name,
207205 schemaname AS schema_name,
208206 'TABLE' AS type
209207 FROM pg_tables
210208 WHERE schemaname ILIKE '{ schema_name } '
211209 UNION ALL
212210 SELECT
213- null AS catalog_name,
211+ ' { catalog_name } ' AS catalog_name,
214212 viewname AS name,
215213 schemaname AS schema_name,
216214 'VIEW' AS type
@@ -219,7 +217,7 @@ def _get_data_objects(self, schema_name: SchemaName) -> t.List[DataObject]:
219217 AND definition not ilike '%create materialized view%'
220218 UNION ALL
221219 SELECT
222- null AS catalog_name,
220+ ' { catalog_name } ' AS catalog_name,
223221 viewname AS name,
224222 schemaname AS schema_name,
225223 'MATERIALIZED_VIEW' AS type
@@ -236,9 +234,6 @@ def _get_data_objects(self, schema_name: SchemaName) -> t.List[DataObject]:
236234 for row in df .itertuples ()
237235 ]
238236
239- def _short_hash (self ) -> str :
240- return uuid .uuid4 ().hex [:8 ]
241-
242237
243238def parse_plan (plan : str ) -> t .Optional [t .Dict ]:
244239 """Parse the output of a redshift explain verbose query plan into a Python dict."""
0 commit comments