Skip to main content

SQL Data Lineage

Introducing SQL Data Lineage, a powerful package designed to simplify SQL query analysis. This versatile tool parses data lineage from individual SQL queries or builds comprehensive lineage from multiple queries. It offers both an interactive command-line interface and programmatic integration, making it easy to incorporate into your Python projects.

SQL Data Lineage performs detailed column-level analysis, tracing data flows step-by-step through tables, CTEs, subqueries, and more. It generates user-friendly lineage graphs that clearly show how columns move and transform across SQL components.

You can easily enhance your lineage insights by retrieving and customizing metadata to fit your specific requirements.

We welcome and encourage contributions to the SQL Data Lineage project!

Installation

pip install sql-datalineage

Usage

CLI usage

Show help of CLI commands.

datalineage --help

Generate data lineage of a sql file, output type is mermaid.

$> datalineage -i docs/example/test-query.sql --schema-path docs/example/test-schema.json -r mermaid

%%{init: {"flowchart": {"defaultRenderer": "elk"}} }%%
graph LR
subgraph 2420861448752 ["Table: catalog.schema1.customer AS customer"]
2420861447168["id"]
2420861446976["name"]
2420861448464["phone"]
2420860590112["address"]
2420861446304["location"]
end

subgraph 2420861448224 ["CTE: cte1 AS t1"]
2420861448848["id"]
2420861449040["name"]
2420861448272["phone"]
2420861449184["address"]
end
2420861447168 --> 2420861448848
2420861446976 --> 2420861449040
2420861448464 --> 2420861448272
2420860590112 --> 2420861449184

....

[!TIP] The output of the above command is truncated. You can optionally save the command's result to a file using the -o option.

datalineage -i /docs/example/test-query.sql --schema-path docs/example/test-schema.json -o docs/example/output.mermaid -r mermaid

You can preview the above result using Mermaid Live Editor, here is the result:

readme_example

Interactive usage

You can import datalineage into your project and generate the lineage tree directly.

>>> from datalineage.lineage import lineage
>>> sql = """select  
  id, name, phone, address
  from (
    select id, name, phone, address,
    row_number() over(partition by phone order by name) as rn    
    from `catalog.schema1.customer`) data
  where data.rn = 1
"""
>>> schema = None # we will infer the schema of table when no schema are provided
>>> dialect = "bigquery"
>>> tree = lineage(sql, dialect, schema)
>>> tree
Node<{"name": "myroot", "expression": "ANCHOR",...

You can traversal and print out the lineage tree in this way:

>>> def print_node(node):
>>>     print("Node:", node.name)
>>>     list(map(lambda c: print("Column:", c.name), node.children))
>>> for node in tree.walk():
...     print_node(node)
... 
Node: myroot
Node: _output_
Column: id
Column: name
Column: phone
Column: address
Node: data
Column: id
Column: name
Column: phone
Column: address
Column: rn
Node: "catalog"."schema1"."customer" AS "customer"
Column: id
Column: name
Column: phone
Column: address

Or you can render the tree to a format you like, for example, mermaid.

>>> from datalineage.renderer import MermaidRenderer
>>> renderer = MermaidRenderer()
>>> print(renderer.render(tree))
%%{init: {"flowchart": {"defaultRenderer": "elk"}} }%%
graph LR
subgraph 1434247920720 ["Table: catalog.schema1.customer AS customer"]
1434247920624["id"]
1434247921104["name"]
1434247919568["phone"]
1434247921200["address"]
end

subgraph 1434247919280 ["Subquery: data"]
1434247920816["id"]
1434247919856["name"]
1434247917696["phone"]
1434247917744["address"]
1434247918224["rn"]
end
1434247920624 --> 1434247920816
1434247921104 --> 1434247919856
1434247919568 --> 1434247917696
1434247921200 --> 1434247917744
1434247919568 --> 1434247918224
1434247921104 --> 1434247918224

subgraph 1434247918032 ["Select: _output_"]
1434247921392["id"]
1434247921344["name"]
1434247921152["phone"]
1434247920912["address"]
end
1434247920816 --> 1434247921392
1434247919856 --> 1434247921344
1434247917696 --> 1434247921152
1434247917744 --> 1434247920912
>>>

[!TIP] You can render to json format using datalineage.renderer.JsonRenderer class, or customize your own renderer.

If you are in enviroment which support Ipython (for example, jupyter notebook), you can render the mermaid graph directly:

from datalineage.renderer import MermaidRenderer, MermaidType

html_renderer = MermaidRenderer(output_type=MermaidType.HTML)
html_output = html_renderer.render(tree)

Output: readme_mermaid_html_jupyter

Contribution

Setup Environment

We use uv to manage the project. Please follow the official document to install uv to your environment.

[!TIP] Your environment does not require any python or pip installed, but if you already have pip, you can quickly install uv like this:

pip install uv

Install pre-commit to your local git hooks.

make install-pre-commit

Run Lint

make style

Run Tests

make test

Run Lint and Tests

make check

Run from local development

uv run --no-project -- datalineage --help

Release files for sql-datalineage 0.0.16

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sql-datalineage 0.0.16
File Size Uploaded
sql_datalineage-0.0.16.tar.gz 102.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sql-datalineage 0.0.16
File Interpreter ABI Platform
sql_datalineage-0.0.16-py3-none-any.whl Python 3 none any Details

Total release size: 127.0 kB

Release files / sql_datalineage-0.0.16.tar.gz

Download URL sql_datalineage-0.0.16.tar.gz
Size 102.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4c4c5a6ab7343834dfdbe35a22a60166298850094aaafd832320c364a820d904
BLAKE2b-256 checksum
How to use checksums
de144596ed607ef7116f6fa34ae265738232c09879512f944310944d17f920ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.7

Release files / sql_datalineage-0.0.16-py3-none-any.whl

Download URL sql_datalineage-0.0.16-py3-none-any.whl
Size 25.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb1f80e3ed12536754ca4f013b2f624215304209182efdedddbcd4b1976a8559
BLAKE2b-256 checksum
How to use checksums
e2de23b4f791a1eb16a08d332b817e712d19bcb20b5620b782125fae2c2a4292
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.7

Release history Release notifications | RSS feed

This release

0.0.16 This release

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page