|
| 1 | +# |
| 2 | +# Copyright (c) 2012-2025 Snowflake Computing Inc. All rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +from functools import cached_property |
| 6 | +import os |
| 7 | +import sys |
| 8 | +from typing import Dict, List, Optional |
| 9 | +import itertools |
| 10 | + |
| 11 | +from snowflake.snowpark._internal.ast.batch import get_dependent_bind_ids |
| 12 | +from snowflake.snowpark._internal.ast.utils import __STRING_INTERNING_MAP__ |
| 13 | +import snowflake.snowpark._internal.proto.generated.ast_pb2 as proto |
| 14 | + |
| 15 | +UNKNOWN_FILE = "__UNKNOWN_FILE__" |
| 16 | +SNOWPARK_PYTHON_DATAFRAME_TRANSFORM_TRACE_LENGTH = ( |
| 17 | + "SNOWPARK_PYTHON_DATAFRAME_TRANSFORM_TRACE_LENGTH" |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class DataFrameTraceNode: |
| 22 | + """A node representing a dataframe operation in the DAG that represents the lineage of a DataFrame.""" |
| 23 | + |
| 24 | + def __init__(self, batch_id: int, stmt_cache: Dict[int, proto.Stmt]) -> None: |
| 25 | + self.batch_id = batch_id |
| 26 | + self.stmt_cache = stmt_cache |
| 27 | + |
| 28 | + @cached_property |
| 29 | + def children(self) -> set[int]: |
| 30 | + """Returns the batch_ids of the children of this node.""" |
| 31 | + return get_dependent_bind_ids(self.stmt_cache[self.batch_id]) |
| 32 | + |
| 33 | + def get_src(self) -> Optional[proto.SrcPosition]: |
| 34 | + """The source Stmt of the DataFrame described by the batch_id.""" |
| 35 | + stmt = self.stmt_cache[self.batch_id] |
| 36 | + api_call = stmt.bind.expr.WhichOneof("variant") |
| 37 | + return ( |
| 38 | + getattr(stmt.bind.expr, api_call).src |
| 39 | + if api_call and getattr(stmt.bind.expr, api_call).HasField("src") |
| 40 | + else None |
| 41 | + ) |
| 42 | + |
| 43 | + def _read_file( |
| 44 | + self, filename, start_line, end_line, start_column, end_column |
| 45 | + ) -> str: |
| 46 | + """Read the relevant code snippets of where the DataFrame was created. The filename given here |
| 47 | + must have read permissions for the executing user.""" |
| 48 | + with open(filename) as f: |
| 49 | + code_lines = [] |
| 50 | + if sys.version_info >= (3, 11): |
| 51 | + # Skip to start_line and read only the required lines |
| 52 | + lines = itertools.islice(f, start_line - 1, end_line) |
| 53 | + code_lines = list(lines) |
| 54 | + if start_line == end_line: |
| 55 | + code_lines[0] = code_lines[0][start_column:end_column] |
| 56 | + else: |
| 57 | + code_lines[0] = code_lines[0][start_column:] |
| 58 | + code_lines[-1] = code_lines[-1][:end_column] |
| 59 | + else: |
| 60 | + # For python 3.9/3.10, we do not extract the end line from the source code |
| 61 | + # so we just read the start line and return. |
| 62 | + for line in itertools.islice(f, start_line - 1, start_line): |
| 63 | + code_lines.append(line) |
| 64 | + |
| 65 | + code_lines = [line.rstrip() for line in code_lines] |
| 66 | + return "\n".join(code_lines) |
| 67 | + |
| 68 | + @cached_property |
| 69 | + def source_id(self) -> str: |
| 70 | + """Unique identifier of the location of the DataFrame creation in the source code.""" |
| 71 | + src = self.get_src() |
| 72 | + if src is None: # pragma: no cover |
| 73 | + return "" |
| 74 | + |
| 75 | + fileno = src.file |
| 76 | + start_line = src.start_line |
| 77 | + start_column = src.start_column |
| 78 | + end_line = src.end_line |
| 79 | + end_column = src.end_column |
| 80 | + return f"{fileno}:{start_line}:{start_column}-{end_line}:{end_column}" |
| 81 | + |
| 82 | + def get_source_snippet(self) -> str: |
| 83 | + """Read the source file and extract the snippet where the dataframe is created.""" |
| 84 | + src = self.get_src() |
| 85 | + if src is None: # pragma: no cover |
| 86 | + return "No source" |
| 87 | + |
| 88 | + # get the latest mapping of fileno to filename |
| 89 | + _fileno_to_filename_map = {v: k for k, v in __STRING_INTERNING_MAP__.items()} |
| 90 | + fileno = src.file |
| 91 | + filename = _fileno_to_filename_map.get(fileno, UNKNOWN_FILE) |
| 92 | + |
| 93 | + start_line = src.start_line |
| 94 | + end_line = src.end_line |
| 95 | + start_column = src.start_column |
| 96 | + end_column = src.end_column |
| 97 | + |
| 98 | + # Build the code identifier to find the operations where the DataFrame was created |
| 99 | + if sys.version_info >= (3, 11): |
| 100 | + code_identifier = ( |
| 101 | + f"{filename}|{start_line}:{start_column}-{end_line}:{end_column}" |
| 102 | + ) |
| 103 | + else: |
| 104 | + code_identifier = f"{filename}|{start_line}" |
| 105 | + |
| 106 | + if filename != UNKNOWN_FILE and os.access(filename, os.R_OK): |
| 107 | + # If the file is readable, read the code snippet |
| 108 | + code = self._read_file( |
| 109 | + filename, start_line, end_line, start_column, end_column |
| 110 | + ) |
| 111 | + return f"{code_identifier}: {code}" |
| 112 | + return code_identifier # pragma: no cover |
| 113 | + |
| 114 | + |
| 115 | +def _get_df_transform_trace( |
| 116 | + batch_id: int, |
| 117 | + stmt_cache: Dict[int, proto.Stmt], |
| 118 | +) -> List[DataFrameTraceNode]: |
| 119 | + """Helper function to get the transform trace of the dataframe involved in the exception. |
| 120 | + It gathers the lineage in the following way: |
| 121 | +
|
| 122 | + 1. Start by creating a DataFrameTraceNode for the given batch_id. |
| 123 | + 2. We use BFS to traverse the lineage using the node created in 1. as the first layer. |
| 124 | + 3. During each iteration, we check if the node's source_id has been visited. If not, |
| 125 | + we add it to the visited set and append its source format to the trace. This step |
| 126 | + is needed to avoid source_id added multiple times in lineage due to loops. |
| 127 | + 4. We then explore the next layer by adding the children of the current node to the |
| 128 | + next layer. We check if the child ID has been visited and if not, we add it to the |
| 129 | + visited set and append the DataFrameTraceNode for it to the next layer. |
| 130 | + 5. We repeat this process until there are no more nodes to explore. |
| 131 | +
|
| 132 | + Args: |
| 133 | + batch_id: The batch ID of the dataframe involved in the exception. |
| 134 | + stmt_cache: The statement cache of the session. |
| 135 | +
|
| 136 | + Returns: |
| 137 | + A list of DataFrameTraceNode objects representing the transform trace of the dataframe. |
| 138 | + """ |
| 139 | + visited_batch_id = set() |
| 140 | + visited_source_id = set() |
| 141 | + |
| 142 | + visited_batch_id.add(batch_id) |
| 143 | + curr = [DataFrameTraceNode(batch_id, stmt_cache)] |
| 144 | + lineage = [] |
| 145 | + |
| 146 | + while curr: |
| 147 | + next: List[DataFrameTraceNode] = [] |
| 148 | + for node in curr: |
| 149 | + # tracing updates |
| 150 | + source_id = node.source_id |
| 151 | + if source_id not in visited_source_id: |
| 152 | + visited_source_id.add(source_id) |
| 153 | + lineage.append(node) |
| 154 | + |
| 155 | + # explore next layer |
| 156 | + for child_id in node.children: |
| 157 | + if child_id in visited_batch_id: |
| 158 | + continue |
| 159 | + visited_batch_id.add(child_id) |
| 160 | + next.append(DataFrameTraceNode(child_id, stmt_cache)) |
| 161 | + |
| 162 | + curr = next |
| 163 | + |
| 164 | + return lineage |
| 165 | + |
| 166 | + |
| 167 | +def get_df_transform_trace_message( |
| 168 | + df_ast_id: int, stmt_cache: Dict[int, proto.Stmt] |
| 169 | +) -> Optional[str]: |
| 170 | + """Get the transform trace message for the dataframe involved in the exception. |
| 171 | +
|
| 172 | + Args: |
| 173 | + df_ast_id: The AST ID of the dataframe involved in the exception. |
| 174 | + stmt_cache: The statement cache of the session. |
| 175 | +
|
| 176 | + Returns: |
| 177 | + A string representing the transform trace message. |
| 178 | + """ |
| 179 | + df_transform_trace_nodes = _get_df_transform_trace(df_ast_id, stmt_cache) |
| 180 | + if len(df_transform_trace_nodes) == 0: # pragma: no cover |
| 181 | + return None |
| 182 | + |
| 183 | + df_transform_trace_length = len(df_transform_trace_nodes) |
| 184 | + show_trace_length = int( |
| 185 | + os.environ.get(SNOWPARK_PYTHON_DATAFRAME_TRANSFORM_TRACE_LENGTH, 5) |
| 186 | + ) |
| 187 | + |
| 188 | + debug_info_lines = [ |
| 189 | + "\n\n--- Additional Debug Information ---\n", |
| 190 | + f"Trace of the most recent dataframe operations associated with the error (total {df_transform_trace_length}):\n", |
| 191 | + ] |
| 192 | + for node in df_transform_trace_nodes[:show_trace_length]: |
| 193 | + debug_info_lines.append(node.get_source_snippet()) |
| 194 | + if df_transform_trace_length > show_trace_length: |
| 195 | + debug_info_lines.append( |
| 196 | + f"... and {df_transform_trace_length - show_trace_length} more.\nYou can increase " |
| 197 | + f"the lineage length by setting {SNOWPARK_PYTHON_DATAFRAME_TRANSFORM_TRACE_LENGTH} " |
| 198 | + "environment variable." |
| 199 | + ) |
| 200 | + return "\n".join(debug_info_lines) |
0 commit comments