Skip to main content

Data Lineage for Python

Project description

PyPI - Python PyPI - PyPi PyPI - License

pylineage

This package provides simple tools for parsing and visualizing your .sql scripts.

Installation

The package is distributed through PyPI, and can be installed with

pip install pylineage

SQL Parser

Individual basic .sql scripts can be parsed by running

from pylineage import SqlParser

parser = SqlParser()
query = """

SELECT column1
     , column2 AS c2
FROM my_table t
WHERE column1 = 1

"""

parsed = parser.parse(query)

The output looks as follows

>>> parsed

{
  "select": [{ "content": "column1" }, { "content": "column2", "alias": "c2" }],
  "from": { "content": "my_table", "alias": "t" },
  "where": ["column1 = 1"]
}

Currently the parser supports the clauses

Clause
SELECT
FROM
LEFT JOIN
LEFT OUTER JOIN
RIGHT JOIN
RIGHT OUTER JOIN
FULL JOIN
FULL OUTER JOIN
INNER JOIN
WHERE
QUALIFY

Lineage Graph

Based on the SQL parser, a lineage graph can be constructed. We start off with the main constructor:

from pylineage import LineageGraph

lineage_graph = LineageGraph()

Subsequently, there are two options of adding SQL scripts: as individual input strings or as directory.

lineage_graph.extend_graph_from_input_string("CREATE TABLE my_view AS SELECT column1 FROM my_table")

lineage_graph.extend_graph_from_directory("/data")

The graph can be cleared at any time by running

lineage_graph.clear_graph()

One purpose of parsing and visualizing is to obtain execution order. This can be obtained by running

lineage_graph.get_execution_order()

NOTE
Any inner query that is not a part of a source clause (FROM / JOIN) is not included as a node in the graph. As such, statements to the one below are not taken into account.

...
WHERE column not in (
  SELECT column
  FROM table2
)

Finally the Lineage Graph can be accessed by directly checking

lineage_graph.graph

or by running it in interactive mode:

lineage_graph.serve_graph()

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

pylineage-0.1.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

pylineage-0.1.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file pylineage-0.1.1.tar.gz.

File metadata

  • Download URL: pylineage-0.1.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.8.10 Windows/10

File hashes

Hashes for pylineage-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e8c56dd1ab691eaf8b1602212583f800cd66510552b1018ef8ee61e9c227a5f9
MD5 d610624d51c53b5ae3607a38be7adf80
BLAKE2b-256 17a2646747347d921e26a68834870fb6cfd6426d78258f31c25c91342bbb6e66

See more details on using hashes here.

File details

Details for the file pylineage-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pylineage-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.8.10 Windows/10

File hashes

Hashes for pylineage-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6c3f68f171f5e5a7ea9173841dcbbe15936e6c8c9a5c63d63eb6114cab9e4906
MD5 fdafe682e78ed7301eea4eb84054cf19
BLAKE2b-256 7601074b649260962f898c3ee2000b843ae31e148be02cedf82ed67991eb367a

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