88from sqllineage .exceptions import SQLLineageException
99from pyvis .network import Network
1010import webbrowser
11+
12+ from lineage .query_context import QueryContext
1113from lineage .utils import get_logger
1214from sqllineage .models import Schema , Table
1315from tqdm import tqdm
@@ -118,7 +120,9 @@ def _name_qualification(self, table: Table, database_name: str, schema_name: str
118120
119121 return str (table ).rsplit ('.' , 1 )[- 1 ]
120122
121- def _update_lineage_graph (self , analyzed_statements : [LineageResult ], database_name : str , schema_name : str ) -> None :
123+ def _update_lineage_graph (self , analyzed_statements : [LineageResult ], query_context : QueryContext ) -> None :
124+ database_name = query_context .queried_database
125+ schema_name = query_context .queried_schema
122126 for analyzed_statement in analyzed_statements :
123127 # Handle drop tables, if they exist in the statement
124128 dropped_tables = analyzed_statement .drop
@@ -139,9 +143,9 @@ def _update_lineage_graph(self, analyzed_statements: [LineageResult], database_n
139143 targets = {self ._name_qualification (target , database_name , schema_name )
140144 for target in analyzed_statement .write }
141145
142- self ._add_nodes_and_edges (sources , targets )
146+ self ._add_nodes_and_edges (sources , targets , query_context )
143147
144- def _add_nodes_and_edges (self , sources : {str }, targets : {str }) -> None :
148+ def _add_nodes_and_edges (self , sources : {str }, targets : {str }, query_context : QueryContext ) -> None :
145149 if None in sources :
146150 sources .remove (None )
147151 if None in targets :
@@ -155,10 +159,10 @@ def _add_nodes_and_edges(self, sources: {str}, targets: {str}) -> None:
155159 self ._lineage_graph .add_nodes_from (sources )
156160 elif len (targets ) > 0 and len (sources ) == 0 :
157161 if self ._show_isolated_nodes :
158- self ._lineage_graph .add_nodes_from (targets )
162+ self ._lineage_graph .add_nodes_from (targets , title = query_context . to_html () )
159163 else :
160164 self ._lineage_graph .add_nodes_from (sources )
161- self ._lineage_graph .add_nodes_from (targets )
165+ self ._lineage_graph .add_nodes_from (targets , title = query_context . to_html () )
162166 for source , target in itertools .product (sources , targets ):
163167 self ._lineage_graph .add_edge (source , target )
164168
@@ -191,15 +195,15 @@ def _remove_node(self, node: str) -> None:
191195
192196 def init_graph_from_query_list (self , queries : [tuple ]) -> None :
193197 logger .debug (f'Loading { len (queries )} queries into the lineage graph' )
194- for query , database_name , schema_name in tqdm (queries , desc = "Updating lineage graph" , colour = 'green' ):
198+ for query , query_context in tqdm (queries , desc = "Updating lineage graph" , colour = 'green' ):
195199 try :
196200 analyzed_statements = self ._parse_query (query )
197201 except SQLLineageException as exc :
198202 logger .debug (f'SQLLineageException was raised while parsing this query -\n { query } \n '
199203 f'Error was -\n { exc } .' )
200204 continue
201205
202- self ._update_lineage_graph (analyzed_statements , database_name , schema_name )
206+ self ._update_lineage_graph (analyzed_statements , query_context )
203207
204208 logger .debug (f'Finished updating lineage graph!' )
205209
0 commit comments