Skip to main content

TOC

TraceSQL Python Package

The tracesql Python is client for TraceSQL. It allows you to easily analyze SQL code for data lineage.

You can currently use this client and the API it wraps without any limitations or tokens. This might be changed in the future.

Features

  • Connects to TraceSQL API.
  • Analyzes SQL code to generate data lineage.
  • Outputs the lineage in JSON format.
  • Generates an SVG image of the lineage.

Installation

You can install the tracesql package via pip:

pip install tracesql

Usage

Simple example

from tracesql import analyze_lineage

code = """
CREATE TABLE active_customers AS
SELECT customer_id, first_name || ' ' || last_name as fullname, email
FROM customers
WHERE status = 'active';
"""
response = analyze_lineage(code)

# Save the SVG image of the lineage
with open("image.svg", "w") as fw:
    fw.write(response.svg)

# Save the lineage data in JSON format
with open("lineage.json", "w") as fw:
    fw.write(response.lineage.model_dump_json(indent=2))

print("Lineage successfully saved in files.")

Here is output for this example: simple

Optionally, you can provide DB model, which will help resolving ambiguous queries:

db_model = DbModel(
    tables=[
        DbModelTable(
            name="ACTIVE_CUSTOMERS",
            columns=["customer_id", "full_name", "email", "status"]
        ),
        DbModelTable(
            name="CUSTOMERS",
            columns=["customer_id", "full_name", "email", "status"]
        )
    ]
)
response = analyze_lineage(code, db_model=db_model)

When submitting database model, please ensure you use the correct case (upper/lower). The system is case-sensitive, and mismatches in casing (e.g., "Customers" vs "CUSTOMERS") may result in processing errors or unexpected behavior.

analyze_lineage method

It provides the most basic interface for analyzing lineage. Check the underlaying code if you want to build something more capable.

Parameters

  • query (str): The SQL query whose lineage you want to analyze.
  • db_model (Optional[DbModel]): The database model containing the tables and columns used in the SQL query.

Response - ApiResponse

  • svg: A string representing the SVG image of the lineage.
  • lineage: An object containing the lineage data in a pydantic class.

Each relation includes an attribute named source_positions, which provides detailed information about code, that is relevant for this relation:

"source_positions": [
    {
        "start_idx": 109,
        "end_idx": 118
    }
    ...
]

The start_idx and end_idx represent character indices in the input SQL code. Together, they define a range that pinpoints the specific section of the code corresponding to the relation. These indices effectively serve as "pointers" to the relevant portion of the analyzed SQL query.

Data lineage

Lineage traditionally refers to a person’s or group’s ancestry, tracing their origins and heritage through generations. It embodies the historical path that defines their roots and connections.

Similarly, data lineage traces the lifecycle of data, mapping its origins, transformations, and usage. It answers critical questions such as:

  • Where did this data originate?
  • How is this data used across systems or processes?
  • What steps were involved in constructing this data?

More specific examples include:

  • Can I safely delete this column?
  • Are there scripts or processes that depend on this table?
  • Who created or modified this data?

In a world where data is often more valuable than gold, it’s crucial to answer these questions quickly and accurately. This is why data lineage plays a vital role in effective data governance.

Data lineage in SQL (technical view)

How is lineage created in SQL? It is as simple as creating a table:

CREATE TABLE NEW_TABLE
AS SELECT first_name, last_name from OLD_TABLE;

With this query, we have create lineage from OLD_TABLE to NEW_TABLE. On column level, this would look like this:

OLD_TABLE.first_name -> NEW_TABLE.first_name
OLD_TABLE.last_name -> NEW_TABLE.last_name

Lineage is created whenever data is moved or transformed. In SQL, this typically involves a SELECT statement to retrieve data before moving it, making SELECT the cornerstone of lineage analysis.

  1. Analyze targets – Identify the destination of the SELECT statement, usually a single table.
  2. Analyze sources – Identify the data sources, typically multiple tables referenced in the FROM clause.
  3. Connect sources and targets – Establish relationships between sources and their corresponding targets.

Simple SELECT

What happens when a SELECT statement has no explicit target?

SELECT name FROM accounts;

In most IDEs, this query simply displays the results. To model this behavior in lineage, we create an artificial target called SELECT-RESULT:

accounts.name -> SELECT-RESULT.name

This approach ensures the lineage remains consistent, even without a defined target.

Wildcard

SELECT * FROM events;

This case is impossible to analyze without any extra info. We need to check the database model - view the columns of table events. You can either do this by providing the CREATE TABLE statement or you can provide the database model in JSON format directly to the API.

Ambiguous Queries

Consider the following query:

SELECT price, name FROM products NATURAL JOIN suppliers;

Analyzing lineage in this case is challenging due to the absence of table aliases and fully qualified column names. Without these, it becomes unclear which table each column originates from.

While providing the database model to the lineage analyzer can help resolve this ambiguity, the best practice is to use explicit column references and table aliases to avoid confusion

SELECT p.price, s.name FROM products p
NATURAL JOIN suppliers s

This approach ensures clearer lineage analysis and reduces the risk of misinterpreting the data's origins.

Metadata

Release files for tracesql 0.1.10

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

Source distribution (sdist)

Source distribution for tracesql 0.1.10
File Size Uploaded Provenance Actions
tracesql-0.1.10.tar.gz 19.9 kB Without provenance Details for tracesql-0.1.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tracesql 0.1.10
File Interpreter ABI Platform Actions
tracesql-0.1.10-py3-none-any.whl Python 3 none any Details for tracesql-0.1.10-py3-none-any.whl

Total release size: 35.7 kB

Release files / tracesql-0.1.10.tar.gz

Download URL tracesql-0.1.10.tar.gz
Size 19.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7dd7f83cbd9c4d95a934355a73f351d7a7e1264f11ec73432f7d827830bbcdc7
BLAKE2b-256 checksum
How to use checksums
0d33e60761fb0fa4766e4ef3bec3363ef15eea79095f3b995da28c51c81f3dc7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.15

Release files / tracesql-0.1.10-py3-none-any.whl

Download URL tracesql-0.1.10-py3-none-any.whl
Size 15.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4add6801ae254debc93491f6073fecf450304226198d19164938abe59d0be1ef
BLAKE2b-256 checksum
How to use checksums
b35283c01294c07a891b5a529f89ed6241748d8c2a4a47b8b49dc5f56c79d828
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.15

Security for tracesql 0.1.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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