1010from metaflow .cards import Artifact , Markdown , Table
1111from snowflake .connector .cursor import SnowflakeCursor
1212
13+ from ds_platform_utils ._snowflake .run_query import _execute_sql
1314from ds_platform_utils .metaflow .get_snowflake_connection import get_snowflake_connection
1415
1516if TYPE_CHECKING :
@@ -97,7 +98,7 @@ def get_select_dev_query_tags() -> Dict[str, str]:
9798 stacklevel = 2 ,
9899 )
99100
100- def extract (prefix : str , default : str = "unknown" ) -> str :
101+ def _extract (prefix : str , default : str = "unknown" ) -> str :
101102 for tag in fetched_tags :
102103 if tag .startswith (prefix + ":" ):
103104 return tag .split (":" , 1 )[1 ]
@@ -106,19 +107,19 @@ def extract(prefix: str, default: str = "unknown") -> str:
106107 # most of these will be unknown if no tags are set on the flow
107108 # (most likely for the flow runs which are triggered manually locally)
108109 return {
109- "app" : extract (
110+ "app" : _extract (
110111 "ds.domain"
111112 ), # first tag after 'app:', is the domain of the flow, fetched from current tags of the flow
112- "workload_id" : extract (
113+ "workload_id" : _extract (
113114 "ds.project"
114115 ), # second tag after 'workload_id:', is the project of the flow which it belongs to
115- "flow_name" : current .flow_name , # name of the metaflow flow
116+ "flow_name" : current .flow_name ,
116117 "project" : current .project_name , # Project name from the @project decorator, lets us
117118 # identify the flow’s project without relying on user tags (added via --tag).
118119 "step_name" : current .step_name , # name of the current step
119120 "run_id" : current .run_id , # run_id: unique id of the current run
120121 "user" : current .username , # username of user who triggered the run (argo-workflows if its a deployed flow)
121- "domain" : extract ("ds.domain" ), # business unit (domain) of the flow, same as app
122+ "domain" : _extract ("ds.domain" ), # business unit (domain) of the flow, same as app
122123 "namespace" : current .namespace , # namespace of the flow
123124 "perimeter" : str (os .environ .get ("OB_CURRENT_PERIMETER" ) or os .environ .get ("OBP_PERIMETER" )),
124125 "is_production" : str (
@@ -216,7 +217,7 @@ def publish( # noqa: PLR0913, D417
216217
217218 with conn .cursor () as cur :
218219 if warehouse is not None :
219- cur . execute ( f"USE WAREHOUSE { warehouse } " )
220+ _execute_sql ( conn , f"USE WAREHOUSE { warehouse } " )
220221
221222 last_op_was_write = False
222223 for operation in write_audit_publish (
@@ -334,20 +335,28 @@ def fetch_table_preview(
334335 :param table_name: Table name
335336 :param cursor: Snowflake cursor
336337 """
337- cursor .execute (f"""
338- SELECT *
339- FROM { database } .{ schema } .{ table_name }
340- LIMIT { n_rows } ;
341- """ )
342- columns = [col [0 ] for col in cursor .description ]
343- rows = cursor .fetchall ()
344-
345- # Create header row plus data rows
346- table_rows = [[Artifact (col ) for col in columns ]] # Header row
347- for row in rows :
348- table_rows .append ([Artifact (val ) for val in row ]) # Data rows
349-
350- return [
351- Markdown (f"### Table Preview: ({ database } .{ schema } .{ table_name } )" ),
352- Table (table_rows ),
353- ]
338+ if cursor is None :
339+ return []
340+ else :
341+ result_cursor = _execute_sql (
342+ cursor .connection ,
343+ f"""
344+ SELECT *
345+ FROM { database } .{ schema } .{ table_name }
346+ LIMIT { n_rows } ;
347+ """ ,
348+ )
349+ if result_cursor is None :
350+ return []
351+ columns = [col [0 ] for col in result_cursor .description ]
352+ rows = result_cursor .fetchall ()
353+
354+ # Create header row plus data rows
355+ table_rows = [[Artifact (col ) for col in columns ]] # Header row
356+ for row in rows :
357+ table_rows .append ([Artifact (val ) for val in row ]) # Data rows
358+
359+ return [
360+ Markdown (f"### Table Preview: ({ database } .{ schema } .{ table_name } )" ),
361+ Table (table_rows ),
362+ ]
0 commit comments