Skip to main content

A lightweight SQL lineage analysis library for tracking table dependencies in data pipelines

Project description

mini-sqllineage

PyPI version Python version License Tests

A lightweight Python library for analyzing SQL lineage and tracking table dependencies in data pipelines.

Features

  • 📊 SQL Lineage Analysis: Parse SQL statements and extract table dependencies
  • 🔄 DAG Visualization: Visualize data lineage as Directed Acyclic Graph
  • 🔍 Dependency Search: Find upstream/downstream tables and related dependencies
  • 🎯 Root/Leaf Detection: Identify source tables (ODS) and target tables (ADS)
  • 🖥️ CLI Tool: Command-line interface for quick analysis
  • 🌐 Web Visualization: Interactive web UI for exploring lineage
  • Fast: Token-based parsing, lightweight and efficient

Installation

pip install mini-sqllineage

Quick Start

Python API

from sqllineage import (
    get_all_tables,
    get_all_root_tables,
    search_related_upstream_tables,
)

# Get all tables from SQL
sql = """
INSERT INTO dwd.user_dim SELECT * FROM ods.user;
INSERT INTO ads.user_report SELECT * FROM dwd.user_dim;
"""
tables = get_all_tables(sql)
print(tables)  # ['ods.user', 'dwd.user_dim', 'ads.user_report']

# Get root tables (no upstream dependencies)
root_tables = get_all_root_tables(sql)
print(root_tables)  # ['ods.user']

# Search upstream dependencies
upstream = search_related_upstream_tables(sql, 'ads.user_report')
print(upstream[0])  # ['dwd.user_dim', 'ods.user']

CLI Usage

# List all tables
sqlh list --all -p /path/to/sql/files

# List root tables
sqlh list --root -p /path/to/sql/files

# Search upstream tables
sqlh search --upstream -t ads.user_report -p /path/to/sql/files

# Open web visualization
sqlh web -p /path/to/sql/files

API Reference

Core Functions

Function Description
get_all_tables(sql) Get all tables from SQL statements
get_all_root_tables(sql) Get tables with no upstream dependencies
get_all_leaf_tables(sql) Get tables with no downstream dependencies
search_related_tables(sql, table) Search all related tables (upstream + downstream)
search_related_upstream_tables(sql, table) Search upstream dependencies
search_related_downstream_tables(sql, table) Search downstream dependents
search_related_root_tables(sql, table) Search root tables in the dependency path
read_sql_from_directory(path) Read SQL files from directory

DagGraph Class

from sqllineage import DagGraph

dag = DagGraph()
dag.add_edge("table_a", "table_b")
dag.add_edge("table_b", "table_c")

# Export to Mermaid
mermaid_str = dag.to_mermaid()

# Export to HTML
html_content = dag.to_html()

# Find upstream/downstream
upstream = dag.find_upstream("table_c")
downstream = dag.find_downstream("table_a")

CLI Commands

List Tables

# Get all tables
sqlh list --all -p /path/to/sql/files

# Get root tables
sqlh list --root -p /path/to/sql/files

# Get leaf tables
sqlh list --leaf -p /path/to/sql/files

# Output formats
sqlh list --all -p /path/to/sql/files --output-format json
sqlh list --all -p /path/to/sql/files --output-format text

Search Tables

# Search upstream tables
sqlh search --upstream -t table_name -p /path/to/sql/files

# Search downstream tables
sqlh search --downstream -t table_name -p /path/to/sql/files

# Search all related tables
sqlh search --all -t table_name -p /path/to/sql/files

# Search root tables
sqlh search --root -t table_name -p /path/to/sql/files

Web Visualization

# Open web server
sqlh web -p /path/to/sql/files

# Specify HTML output path
sqlh web -p /path/to/sql/files --html-path ./custom.html

Output Formats

JSON Format

{
    "status": "ok",
    "command": "list-tables",
    "tables": ["table1", "table2"],
    "meta": {
        "table_count": 2
    }
}

Text Format

table1
table2

Mermaid Format

graph LR
    table1 --> table2
    table2 --> table3

Supported SQL Statements

  • SELECT queries
  • INSERT INTO ... SELECT statements
  • CREATE TABLE AS SELECT (CTAS)
  • WITH ... AS (CTE)
  • JOIN operations
  • Subqueries

Development

Setup

# Clone the repository
git clone https://github.com/yourusername/mini-sqllineage.git
cd mini-sqllineage

# Install in development mode
pip install -e ".[dev]"

# Run tests
pytest

# Run linting
ruff check .

# Run type checking
mypy sqllineage

Project Structure

mini-sqllineage/
├── sqllineage/
│   ├── __init__.py
│   ├── cli.py              # Command-line interface
│   ├── utils.py            # Utility functions
│   └── core/
│       ├── graph.py         # DAG implementation
│       ├── helper.py       # SQL parser
│       └── keywords.py     # SQL keywords
├── tests/                  # Test suite
├── static/                 # Web visualization templates
└── README.md

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

See CHANGELOG.md for a list of changes.

TODO

  • Fuzzy search for table names with suggestions
  • Support for more SQL dialects (PostgreSQL, MySQL, etc.)
  • Database schema import (DESCRIBE TABLE)
  • Column-level lineage tracking
  • CI/CD pipeline configuration

Acknowledgments


TODO

  • [] 搜索指定表时考虑模糊匹配,当表名不存在时,返回提示或者给出可能的表名(相似度)

CLI

output 格式:

  • json: 默认格式, 输出 json 格式
  1. 搜索命令的输出
{
    "status": "ok",
    "command": "search-table",
    "data": {
        "nodes": [
            {"id": "table1", "label": "ods"},
            {"id": "table2", "label": "dwd"},
        ],
        "edges": [
            {"source": "table1", "target": "table2"}
        ]
    },
    "mermaid": "graph LR\n table1 --> table2",
    "meta": {
        "node_count": 2
    }
}
  1. 列举命令的输出
{
    "status": "ok",
    "command": "list-tables",
    "tables": [
        "table1",
        "table2"
    ],
    "meta": {
        "table_count": 2
    }
}

CLI 参数

# 获取所有表名
sqlh list --all --path </path/to/sql-files> --output-format <json|text>

# 获取所有 root 表名
sqlh list --root --path </path/to/sql-files> --output-format <json|text>

# 获取所有 leaf 表名
sqlh list --leaf --path </path/to/sql-files> --output-format <json|text>

# 搜索指定表的root 表名
sqlh search --root --path </path/to/sql-files> --table <table-name> --output-format <json|text>

# 搜索指定表的所有上游表名
sqlh search --upstream --path </path/to/sql-files> --table <table-name> --output-format <json|web|text>

# 搜索指定表的所有下游表名
sqlh search --downstream --path </path/to/sql-files> --table <table-name> --output-format <json|web|text>


# 搜索指定表的所有相关表
sqlh search --all --path </path/to/sql-files> --table <table-name> --output-format <json|web|text>

# 打开全部血缘关系图 web
sqlh web --path </path/to/sql-files>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlh-0.2.3.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlh-0.2.3-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

Details for the file sqlh-0.2.3.tar.gz.

File metadata

  • Download URL: sqlh-0.2.3.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sqlh-0.2.3.tar.gz
Algorithm Hash digest
SHA256 5eed09c4de24c420c614e02cd0882779af61c3d8c9770ee9ba1e3147f51fa0fb
MD5 b65c0ecb14a56e03ba706e452031bf31
BLAKE2b-256 47cb31eef34c8f36d084282141a196bf5b769740fd3db54f180372c4f2120180

See more details on using hashes here.

File details

Details for the file sqlh-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: sqlh-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 27.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sqlh-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 479adb1912ee7f7051e17d3e79c75fc3e2fce808346263f02fde79346089ec96
MD5 a4d911b8f0e26bbdc114a21468923598
BLAKE2b-256 966bcb5a74bd54dd64e759f22f1ddf251fd043355b9baaf4fd3bfe895fc3d95c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page