Skip to content

Commit

Permalink
adding sql into json
Browse files Browse the repository at this point in the history
  • Loading branch information
zshandy committed Feb 5, 2024
1 parent 4f96dbf commit c51f08e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lineagex/ColumnLineageNoConn.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,15 @@ def _sub_shared_col_conds(self, sql_ast: expressions = None) -> None:

def _sub_shared_col_conds_cte(
self, sql_ast: expressions = None
) -> Tuple[List, List, List]:
) -> Tuple[List, List, List, str]:
"""
Run the subquery inside the cte first that is with the shared conditions(WHERE, GROUP BY, etc)
:param sql_ast: the ast for the cte
"""
all_cte_sub_table = []
potential_cte_sub_table = []
all_cte_sub_cols = []
sub_name = ""
# add in more conditions, including FROM/JOIN
for cond in shared_conditions_with_table:
for cond_sql in sql_ast.find_all(cond):
Expand Down Expand Up @@ -309,7 +310,7 @@ def _sub_shared_col_conds_cte(
self.cte_dict[sub_name] = temp_sub_dict
sub_ast.replace(exp.Table(this=sub_name))
sub_ast.pop()
return all_cte_sub_table, all_cte_sub_cols, potential_cte_sub_table
return all_cte_sub_table, all_cte_sub_cols, potential_cte_sub_table, sub_name

def _run_cte_lineage(self):
"""
Expand All @@ -320,13 +321,16 @@ def _run_cte_lineage(self):
all_cte_sub_table,
all_cte_sub_cols,
potential_cte_sub_table,
sub_name
) = self._sub_shared_col_conds_cte(sql_ast=cte)
self.all_used_col = []
temp_cte_dict = {}
temp_cte_table = self._resolve_table(part_ast=cte)
if len(temp_cte_table) == 0:
temp_cte_table = potential_cte_sub_table
cte_name = cte.find(exp.TableAlias).alias_or_name
if sub_name in temp_cte_table:
temp_cte_table.remove(sub_name)
self.cte_table_dict[cte_name] = list(
set(
self._find_all_tables(temp_table_list=temp_cte_table)
Expand Down
1 change: 1 addition & 0 deletions lineagex/LineageXNoConn.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def _run_lineage_no_conn(self, name: Optional[str] = "", sql: Optional[str] = ""
"tables": col_lineage.table_list,
"columns": col_lineage.column_dict,
"table_name": name,
"sql": sql,
}
# add to the dict with the already parsed tables
self.input_table_dict[self.target_schema + "." + name] = list(
Expand Down
2 changes: 2 additions & 0 deletions lineagex/LineageXWithConn.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def _run_table_lineage(self) -> None:
"tables": col_lineage.table_list,
"columns": col_lineage.column_dict,
"table_name": name,
"sql": sql,
}
self.parsed += 1
except Exception as e:
Expand Down Expand Up @@ -268,6 +269,7 @@ def _explain_sql(self, name: Optional[str] = "", sql: Optional[str] = "") -> Non
"tables": col_lineage.table_list,
"columns": col_lineage.column_dict,
"table_name": table_name,
"sql": sql,
}
self.finished_list.append(name)
self.parsed += 1
Expand Down
8 changes: 5 additions & 3 deletions lineagex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ def produce_json(
for i in cols:
base_table_dict[t]["columns"][i] = [""]
base_table_dict[t]["table_name"] = str(t)
base_table_dict[t]["sql"] = "this is a base table"
base_table_dict.update(output_dict)
with open("output.json", "w") as outfile:
json.dump(base_table_dict, outfile)
_produce_html(output_json=str(base_table_dict).replace("'", '"'))
#_produce_html(output_json=str(base_table_dict).replace("'", '"'))
_produce_html(output_json=base_table_dict)
return base_table_dict


Expand All @@ -189,7 +191,7 @@ def _guess_base_table(output_dict: Optional[dict] = None) -> dict:
return base_table_noconn_dict


def _produce_html(output_json: Optional[str] = "") -> None:
def _produce_html(output_json: Optional[dict] = "") -> None:
"""
Produce the html file for viewing
:param output_json: the final output.json file
Expand All @@ -212,7 +214,7 @@ def _produce_html(output_json: Optional[str] = "") -> None:
<div id="main"></div>
<script type="text/javascript" src="vendor.js"></script><script type="text/javascript" src="app.js"></script></body>
</html>""".format(
output_json
json.dumps(output_json)
)
)

Expand Down

0 comments on commit c51f08e

Please sign in to comment.