sqlleaf 0.0.1__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. sqlleaf-0.0.3/PKG-INFO +235 -0
  2. sqlleaf-0.0.3/README.md +222 -0
  3. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/pyproject.toml +9 -7
  4. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/__init__.py +1 -0
  5. sqlleaf-0.0.3/sqlleaf/dialects/__init__.py +1 -0
  6. sqlleaf-0.0.3/sqlleaf/dialects/plpgsql/__init__.py +1 -0
  7. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/dialects/plpgsql/p_classes.py +1 -1
  8. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/dialects/plpgsql/p_generator.py +2 -2
  9. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/dialects/plpgsql/p_parser.py +4 -4
  10. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/exception.py +2 -2
  11. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/lineage.py +8 -65
  12. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/mappings.py +105 -34
  13. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/context.py +1 -1
  14. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/base.py +1 -1
  15. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/__init__.py +57 -0
  16. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/base.py +12 -21
  17. sqlleaf-0.0.3/sqlleaf/models/query/block.py +39 -0
  18. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/call.py +4 -4
  19. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/copy.py +2 -2
  20. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/ctas.py +6 -6
  21. sqlleaf-0.0.3/sqlleaf/models/query/declare_item.py +37 -0
  22. sqlleaf-0.0.3/sqlleaf/models/query/do.py +108 -0
  23. sqlleaf-0.0.3/sqlleaf/models/query/exception_.py +31 -0
  24. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/execute.py +2 -2
  25. sqlleaf-0.0.3/sqlleaf/models/query/execute_dynamic.py +75 -0
  26. sqlleaf-0.0.3/sqlleaf/models/query/fetch.py +25 -0
  27. sqlleaf-0.0.3/sqlleaf/models/query/for_in.py +94 -0
  28. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/holder.py +2 -2
  29. sqlleaf-0.0.3/sqlleaf/models/query/if_.py +35 -0
  30. sqlleaf-0.0.3/sqlleaf/models/query/loop.py +31 -0
  31. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/multitable_insert.py +1 -1
  32. sqlleaf-0.0.3/sqlleaf/models/query/open.py +54 -0
  33. sqlleaf-0.0.3/sqlleaf/models/query/perform.py +12 -0
  34. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/prepare.py +1 -1
  35. sqlleaf-0.0.3/sqlleaf/models/query/procedure.py +124 -0
  36. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/set.py +43 -11
  37. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/stage.py +1 -1
  38. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/table.py +10 -10
  39. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/type.py +1 -1
  40. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/unload.py +1 -1
  41. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/user_defined_function.py +20 -28
  42. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/view.py +3 -3
  43. sqlleaf-0.0.3/sqlleaf/models/query/while_.py +30 -0
  44. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/path.py +8 -8
  45. sqlleaf-0.0.3/sqlleaf/processors/collector/classify.py +117 -0
  46. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/collector/collector.py +226 -285
  47. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/generator/dialects/base.py +50 -5
  48. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/generator/generator.py +61 -8
  49. sqlleaf-0.0.3/sqlleaf/processors/substitutor/substitute.py +781 -0
  50. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/__init__.py +8 -2
  51. sqlleaf-0.0.3/sqlleaf/processors/transformer/assignment.py +18 -0
  52. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/base.py +101 -89
  53. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/call.py +2 -1
  54. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/copy.py +2 -0
  55. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/ctas.py +2 -0
  56. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/delete.py +2 -0
  57. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/execute.py +2 -1
  58. sqlleaf-0.0.3/sqlleaf/processors/transformer/execute_dynamic.py +10 -0
  59. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/expressions/__init__.py +5 -1
  60. sqlleaf-0.0.3/sqlleaf/processors/transformer/expressions/format.py +251 -0
  61. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/expressions/row.py +12 -12
  62. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/expressions/values.py +115 -89
  63. sqlleaf-0.0.3/sqlleaf/processors/transformer/for_in.py +30 -0
  64. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/insert.py +3 -1
  65. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/merge.py +2 -0
  66. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/multitable_insert.py +2 -0
  67. sqlleaf-0.0.3/sqlleaf/processors/transformer/open.py +78 -0
  68. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/replace.py +3 -1
  69. sqlleaf-0.0.3/sqlleaf/processors/transformer/transformer.py +186 -0
  70. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/unload.py +2 -0
  71. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/update.py +2 -0
  72. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/values.py +3 -2
  73. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/settings/__init__.py +2 -2
  74. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/settings/athena.py +1 -1
  75. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/settings/base.py +2 -2
  76. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/settings/postgres.py +2 -2
  77. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/util/__init__.py +2 -0
  78. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/util/expression.py +25 -4
  79. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/util/graph.py +3 -3
  80. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/util/helpers.py +2 -2
  81. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/util/iterators.py +17 -6
  82. sqlleaf-0.0.3/sqlleaf.egg-info/PKG-INFO +235 -0
  83. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf.egg-info/SOURCES.txt +20 -3
  84. sqlleaf-0.0.3/sqlleaf.egg-info/requires.txt +2 -0
  85. sqlleaf-0.0.1/PKG-INFO +0 -46
  86. sqlleaf-0.0.1/README.md +0 -32
  87. sqlleaf-0.0.1/sqlleaf/models/query/procedure.py +0 -72
  88. sqlleaf-0.0.1/sqlleaf/processors/collector/substitute.py +0 -396
  89. sqlleaf-0.0.1/sqlleaf/processors/generator/__init__.py +0 -0
  90. sqlleaf-0.0.1/sqlleaf/processors/transformer/transformer.py +0 -141
  91. sqlleaf-0.0.1/sqlleaf/processors/transformer/udf.py +0 -235
  92. sqlleaf-0.0.1/sqlleaf/system_tables/load.py +0 -0
  93. sqlleaf-0.0.1/sqlleaf.egg-info/PKG-INFO +0 -46
  94. sqlleaf-0.0.1/sqlleaf.egg-info/requires.txt +0 -3
  95. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/LICENSE +0 -0
  96. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/setup.cfg +0 -0
  97. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/dialects/plpgsql/p_dialect.py +0 -0
  98. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/dialects/plpgsql/p_tokenizer.py +0 -0
  99. {sqlleaf-0.0.1/sqlleaf/dialects → sqlleaf-0.0.3/sqlleaf/models}/__init__.py +0 -0
  100. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/hook.py +0 -0
  101. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/__init__.py +0 -0
  102. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/column.py +0 -0
  103. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/dynamodb.py +0 -0
  104. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/function.py +0 -0
  105. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/interval.py +0 -0
  106. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/json.py +0 -0
  107. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/literal.py +0 -0
  108. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/null.py +0 -0
  109. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/pivot.py +0 -0
  110. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/program.py +0 -0
  111. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/sequence.py +0 -0
  112. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/star.py +0 -0
  113. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/stream.py +0 -0
  114. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/user_defined_function.py +0 -0
  115. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/var.py +0 -0
  116. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/variable.py +0 -0
  117. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/node/window.py +0 -0
  118. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/database.py +0 -0
  119. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/delete.py +0 -0
  120. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/insert.py +0 -0
  121. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/merge.py +0 -0
  122. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/put.py +0 -0
  123. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/replace.py +0 -0
  124. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/schema.py +0 -0
  125. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/select.py +0 -0
  126. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/sequence.py +0 -0
  127. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/trigger.py +0 -0
  128. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/update.py +0 -0
  129. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/models/query/values.py +0 -0
  130. {sqlleaf-0.0.1/sqlleaf/dialects/plpgsql → sqlleaf-0.0.3/sqlleaf/processors}/__init__.py +0 -0
  131. {sqlleaf-0.0.1/sqlleaf/models → sqlleaf-0.0.3/sqlleaf/processors/collector}/__init__.py +0 -0
  132. {sqlleaf-0.0.1/sqlleaf/processors → sqlleaf-0.0.3/sqlleaf/processors/generator}/__init__.py +0 -0
  133. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/generator/dialects/__init__.py +0 -0
  134. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/generator/dialects/athena.py +0 -0
  135. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/generator/dialects/mysql.py +0 -0
  136. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/generator/dialects/postgres.py +0 -0
  137. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/generator/dialects/redshift.py +0 -0
  138. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/generator/dialects/snowflake.py +0 -0
  139. {sqlleaf-0.0.1/sqlleaf/processors/collector → sqlleaf-0.0.3/sqlleaf/processors/substitutor}/__init__.py +0 -0
  140. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/processors/transformer/expressions/functional_notation.py +0 -0
  141. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf/typing.py +0 -0
  142. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf.egg-info/dependency_links.txt +0 -0
  143. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/sqlleaf.egg-info/top_level.txt +0 -0
  144. {sqlleaf-0.0.1 → sqlleaf-0.0.3}/tests/test_hooks.py +0 -0
sqlleaf-0.0.3/PKG-INFO ADDED
@@ -0,0 +1,235 @@
1
+ Metadata-Version: 2.4
2
+ Name: sqlleaf
3
+ Version: 0.0.3
4
+ Summary: Trace your data
5
+ Author-email: zac <zac@sqlleaf.io>
6
+ Project-URL: Homepage, https://github.com/sqlleaf-io/sqlleaf
7
+ Requires-Python: >=3.14
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: networkx==3.6.1
11
+ Requires-Dist: sqlglot==30.22.0
12
+ Dynamic: license-file
13
+
14
+ # sqlleaf
15
+ ## Trace your data
16
+ sqlleaf is a tool to help you to understand how your data flows across your data systems by producing detailed, column-level lineage across any SQL query and dialect.
17
+
18
+ It extends beyond traditional lineage tools by mapping relationships across all kinds of expressions, such as columns, functions, literals and variables.
19
+
20
+ ## Example
21
+ Add some SQL queries to a file:
22
+ ```sql
23
+ -- File: example.sql
24
+ CREATE TABLE users (name VARCHAR(50), birthday DATE);
25
+ CREATE TABLE logins (name VARCHAR(50));
26
+
27
+ INSERT INTO users (name, birthday)
28
+ SELECT LOWER(name) AS name, CURRENT_TIMESTAMP AS birthday
29
+ FROM logins;
30
+ ```
31
+ Add some Python code:
32
+ ```python
33
+ sql = open("example.sql").read()
34
+
35
+ import sqlleaf
36
+ lineage = sqlleaf.Lineage()
37
+ lineage.generate(sql=sql, dialect="postgres")
38
+ lineage.print_paths()
39
+ ```
40
+ View the lineage printed to the console:
41
+ ```
42
+ column[logins.name] -> function[LOWER] -> column[users.name]
43
+ function[CURRENT_TIMESTAMP] -> column[users.birthday]
44
+ ```
45
+
46
+ ## Installation
47
+ Available on PyPI:
48
+ ```
49
+ pip3 install sqlleaf
50
+ ```
51
+
52
+ ## Applications
53
+ sqlleaf allows you to build a wide range of new applications on top of SQL databases, such as:
54
+ - data tracing and tagging systems, such as personally identifiable information tracking
55
+ - lineage-based SQL validators
56
+ - auditing systems and historical analysis
57
+ - identification of unused columns and tables
58
+ - support for database migration correctness
59
+ - impact analysis of changes to queries
60
+
61
+ ## Features
62
+ ### Types of queries
63
+ sqlleaf traces lineage across a wide range of DML queries, such as `INSERT`, `UPDATE`, `MERGE`, `CREATE TABLE AS`, `COPY`, `UNLOAD`, CTEs, and many more.
64
+
65
+ It parses over 30 dialects, extending functionality from the underlying SQL transpiler `sqlglot`.
66
+
67
+ Here are some basic queries as examples:
68
+ ```sql
69
+ -- Basic CTAS
70
+ CREATE TABLE dst AS SELECT * FROM src;
71
+
72
+ -- Basic UPDATE
73
+ UPDATE dst t SET first_name = 'john', age = s.age FROM src s;
74
+
75
+ -- An INSERT with a CTE (Common Table Expression)
76
+ WITH my_cte AS (SELECT 1 AS num) INSERT INTO dst (age) SELECT c.num FROM my_cte c;
77
+ ```
78
+
79
+ ### Support for dynamic SQL inside PL/pgSQL
80
+ You can trace lineage across variables and complex, dynamically constructed statements. This works by analyzing the procedure's inner statements, substituting any arguments and variables, and transforming the statements into the desired queries.
81
+
82
+ For example, given a query to calculate the row count of an arbitrary table:
83
+ ```sql
84
+ -- Define a PL/pgSQL procedure in Postgres
85
+ CREATE PROCEDURE get_maximum_id(schema_name TEXT, table_name TEXT)
86
+ AS $$
87
+ BEGIN
88
+ EXECUTE format('INSERT INTO target (my_value) SELECT MAX(id) FROM %I.%I', schema_name, table_name);
89
+ END;
90
+ $$ LANGUAGE plpgsql;
91
+
92
+ -- Call it
93
+ CALL get_maximum_id('public', 'users');
94
+ ```
95
+ sqlleaf extracts and transforms this into the statement:
96
+ ```sql
97
+ INSERT INTO target (my_value) SELECT MAX(id) FROM public.users;
98
+ ```
99
+ and then generates its lineage:
100
+ ```
101
+ column[public.users.id] -> function[MAX] -> column[target.my_value]
102
+ ```
103
+
104
+ ### User-defined functions, procedures and types
105
+ Given a user-defined function or a procedure, sqlleaf will extract the statement's inner queries so that they can be referenced by the calling query:
106
+ ```sql
107
+ -- Create the function
108
+ CREATE FUNCTION hello() RETURNS TEXT AS $$
109
+ SELECT 'Hello';
110
+ $$ LANGUAGE SQL;
111
+
112
+ -- Insert its value into a new table
113
+ CREATE TABLE dst (my_name VARCHAR);
114
+ INSERT INTO dst (my_name) SELECT hello();
115
+ ```
116
+ This will perform inline transformations to produce a new query:
117
+ ```sql
118
+ INSERT INTO dst (my_name) SELECT (SELECT 'hello') AS name;
119
+ ```
120
+ with lineage:
121
+ `literal['Hello'] -> column[dst.name]`
122
+
123
+ ### Pivot, Unpivot
124
+ Complex transformations such as pivots, unpivots, laterals and table functions are supported.
125
+
126
+ ```sql
127
+ -- UNPIVOT example
128
+ INSERT INTO dst
129
+ SELECT name, amount
130
+ FROM src
131
+ UNPIVOT (
132
+ amount FOR name IN (john_total AS 'john')
133
+ );
134
+ ```
135
+ produces lineage: \
136
+ `literal["john"] -> unpivot[] -> column[target.name]` \
137
+ `column[source.john_total] -> unpivot[] -> column[target.amount]`
138
+
139
+ ## Customization
140
+ You can bring your own logic for processing SQL expressions and their nodes.
141
+
142
+ For example, let's say you only want to include certain functions inside the lineage graph, such as `SUM()`. You would do this by defining a *user-defined hook* and that excludes everything except `SUM()`:
143
+ ```python
144
+ import sqlleaf
145
+ lineage = sqlleaf.Lineage()
146
+
147
+ from sqlleaf.lineage import Hook
148
+ from sqlleaf.models.node import FunctionNode
149
+
150
+ # Register a hook that only collects certain functions
151
+ hook = Hook(name="only_sum", kind=FunctionNode, func=lambda n: n.name == "SUM")
152
+ lineage.register_hooks(hooks=[hook])
153
+ ```
154
+ When you run `lineage.generate()`, the only functions that are included as nodes are those named `SUM`.
155
+
156
+ ## Searching the lineage
157
+ To map the lineage into a graph, sqlleaf is built on top of the popular library `networkx`.
158
+ If you need to search the graph, you can use any of network'x graph traversal functions.
159
+ There are currently no dedicated search APIs for sqlleaf. These will be developed in an upcoming major release.
160
+
161
+ There are various functions for printing the lineage: `print_paths()`, `print_tree()`, and `print_json()`.
162
+
163
+
164
+ ## Examples
165
+ Below are examples of SQL queries and their corresponding lineages.
166
+
167
+ #### 1. Simple INSERT
168
+ ```sql
169
+ INSERT INTO dst (name) VALUES (3);
170
+ ```
171
+ `literal[3] -> column[dst.name]`
172
+
173
+ #### 2. JSONPath selectors
174
+ ```sql
175
+ INSERT INTO dst SELECT jsonblob ->> 'fruits' -> 'apple' AS name FROM src;
176
+ ```
177
+ `column[src.name] → jsonpath[.fruits.apple] → column[dst.name]`
178
+
179
+ #### 3. Using variables via SET
180
+ ```snowflake
181
+ SET b = 'src';
182
+ CREATE TABLE dst AS SELECT col1 FROM IDENTIFIER($b);
183
+ ```
184
+ `column[SRC.COL1] → column[DST.COL1]`
185
+
186
+ #### 4. COPY queries
187
+ ```sql
188
+ COPY dst TO STDOUT;
189
+ ```
190
+ `column[dst.col1] → stream[stdout]` \
191
+ `column[dst.col2] → stream[stdout] `
192
+
193
+ ```sql
194
+ COPY dst TO PROGRAM 'gzip > /tmp/data.csv.gz';
195
+ ```
196
+ `column[dst.col1] → program[gzip]` \
197
+ `column[dst.col2] → program[gzip] `
198
+
199
+ #### 5. UNLOAD query
200
+ ```redshift
201
+ UNLOAD ('SELECT name FROM src') TO 's3://dst-bucket/name-prefix' IAM_ROLE '...';
202
+ ```
203
+ `column[src.name] → column[name path=s3://dst-bucket/name-prefix]`
204
+
205
+ #### 6. Sequences
206
+ ```sql
207
+ CREATE SEQUENCE my_serial START 101;
208
+ INSERT INTO dst (age) SELECT nextval('my_serial') as age;
209
+ ```
210
+ `sequence[my_serial] → column[dst.age]`
211
+
212
+ #### 7. An UPDATE with inner CTEs containing UPDATEs that return their old and new columns
213
+ ```sql
214
+ WITH first_cte AS (
215
+ UPDATE fruit.raw
216
+ SET name = 'pear'
217
+ RETURNING age, old.age as old_age, new.age as new_age
218
+ ),
219
+ second_cte AS (
220
+ UPDATE fruit.raw AS r
221
+ SET name = 'tomato'
222
+ RETURNING *, OLD.*, NEW.*
223
+ )
224
+ UPDATE fruit.processed
225
+ SET age = first_cte.age
226
+ FROM first_cte;
227
+ ```
228
+ `column[fruit.raw.age] -> column[first_cte.age] -> column[fruit.processed.age]` \
229
+ `literal["pear"] -> column[fruit.raw.name]` \
230
+ `literal["tomato"] -> column[fruit.raw.name`
231
+
232
+ ## Limitations
233
+ - Many of the dialects are incomplete. Postgres and PL/pgSQL have been mapped out extensively, but most of the dialects do not have their own specific features parsed into graphs yet. Dialects that do have features are Athena, Redshift, MySQL and Snowflake; the others have not been implemented at all yet.
234
+
235
+ - sqlleaf does not support data deletion. Statements like `DELETE`, `TRUNCATE` or `DROP TABLE` currently have no effect. They will in the future.
@@ -0,0 +1,222 @@
1
+ # sqlleaf
2
+ ## Trace your data
3
+ sqlleaf is a tool to help you to understand how your data flows across your data systems by producing detailed, column-level lineage across any SQL query and dialect.
4
+
5
+ It extends beyond traditional lineage tools by mapping relationships across all kinds of expressions, such as columns, functions, literals and variables.
6
+
7
+ ## Example
8
+ Add some SQL queries to a file:
9
+ ```sql
10
+ -- File: example.sql
11
+ CREATE TABLE users (name VARCHAR(50), birthday DATE);
12
+ CREATE TABLE logins (name VARCHAR(50));
13
+
14
+ INSERT INTO users (name, birthday)
15
+ SELECT LOWER(name) AS name, CURRENT_TIMESTAMP AS birthday
16
+ FROM logins;
17
+ ```
18
+ Add some Python code:
19
+ ```python
20
+ sql = open("example.sql").read()
21
+
22
+ import sqlleaf
23
+ lineage = sqlleaf.Lineage()
24
+ lineage.generate(sql=sql, dialect="postgres")
25
+ lineage.print_paths()
26
+ ```
27
+ View the lineage printed to the console:
28
+ ```
29
+ column[logins.name] -> function[LOWER] -> column[users.name]
30
+ function[CURRENT_TIMESTAMP] -> column[users.birthday]
31
+ ```
32
+
33
+ ## Installation
34
+ Available on PyPI:
35
+ ```
36
+ pip3 install sqlleaf
37
+ ```
38
+
39
+ ## Applications
40
+ sqlleaf allows you to build a wide range of new applications on top of SQL databases, such as:
41
+ - data tracing and tagging systems, such as personally identifiable information tracking
42
+ - lineage-based SQL validators
43
+ - auditing systems and historical analysis
44
+ - identification of unused columns and tables
45
+ - support for database migration correctness
46
+ - impact analysis of changes to queries
47
+
48
+ ## Features
49
+ ### Types of queries
50
+ sqlleaf traces lineage across a wide range of DML queries, such as `INSERT`, `UPDATE`, `MERGE`, `CREATE TABLE AS`, `COPY`, `UNLOAD`, CTEs, and many more.
51
+
52
+ It parses over 30 dialects, extending functionality from the underlying SQL transpiler `sqlglot`.
53
+
54
+ Here are some basic queries as examples:
55
+ ```sql
56
+ -- Basic CTAS
57
+ CREATE TABLE dst AS SELECT * FROM src;
58
+
59
+ -- Basic UPDATE
60
+ UPDATE dst t SET first_name = 'john', age = s.age FROM src s;
61
+
62
+ -- An INSERT with a CTE (Common Table Expression)
63
+ WITH my_cte AS (SELECT 1 AS num) INSERT INTO dst (age) SELECT c.num FROM my_cte c;
64
+ ```
65
+
66
+ ### Support for dynamic SQL inside PL/pgSQL
67
+ You can trace lineage across variables and complex, dynamically constructed statements. This works by analyzing the procedure's inner statements, substituting any arguments and variables, and transforming the statements into the desired queries.
68
+
69
+ For example, given a query to calculate the row count of an arbitrary table:
70
+ ```sql
71
+ -- Define a PL/pgSQL procedure in Postgres
72
+ CREATE PROCEDURE get_maximum_id(schema_name TEXT, table_name TEXT)
73
+ AS $$
74
+ BEGIN
75
+ EXECUTE format('INSERT INTO target (my_value) SELECT MAX(id) FROM %I.%I', schema_name, table_name);
76
+ END;
77
+ $$ LANGUAGE plpgsql;
78
+
79
+ -- Call it
80
+ CALL get_maximum_id('public', 'users');
81
+ ```
82
+ sqlleaf extracts and transforms this into the statement:
83
+ ```sql
84
+ INSERT INTO target (my_value) SELECT MAX(id) FROM public.users;
85
+ ```
86
+ and then generates its lineage:
87
+ ```
88
+ column[public.users.id] -> function[MAX] -> column[target.my_value]
89
+ ```
90
+
91
+ ### User-defined functions, procedures and types
92
+ Given a user-defined function or a procedure, sqlleaf will extract the statement's inner queries so that they can be referenced by the calling query:
93
+ ```sql
94
+ -- Create the function
95
+ CREATE FUNCTION hello() RETURNS TEXT AS $$
96
+ SELECT 'Hello';
97
+ $$ LANGUAGE SQL;
98
+
99
+ -- Insert its value into a new table
100
+ CREATE TABLE dst (my_name VARCHAR);
101
+ INSERT INTO dst (my_name) SELECT hello();
102
+ ```
103
+ This will perform inline transformations to produce a new query:
104
+ ```sql
105
+ INSERT INTO dst (my_name) SELECT (SELECT 'hello') AS name;
106
+ ```
107
+ with lineage:
108
+ `literal['Hello'] -> column[dst.name]`
109
+
110
+ ### Pivot, Unpivot
111
+ Complex transformations such as pivots, unpivots, laterals and table functions are supported.
112
+
113
+ ```sql
114
+ -- UNPIVOT example
115
+ INSERT INTO dst
116
+ SELECT name, amount
117
+ FROM src
118
+ UNPIVOT (
119
+ amount FOR name IN (john_total AS 'john')
120
+ );
121
+ ```
122
+ produces lineage: \
123
+ `literal["john"] -> unpivot[] -> column[target.name]` \
124
+ `column[source.john_total] -> unpivot[] -> column[target.amount]`
125
+
126
+ ## Customization
127
+ You can bring your own logic for processing SQL expressions and their nodes.
128
+
129
+ For example, let's say you only want to include certain functions inside the lineage graph, such as `SUM()`. You would do this by defining a *user-defined hook* and that excludes everything except `SUM()`:
130
+ ```python
131
+ import sqlleaf
132
+ lineage = sqlleaf.Lineage()
133
+
134
+ from sqlleaf.lineage import Hook
135
+ from sqlleaf.models.node import FunctionNode
136
+
137
+ # Register a hook that only collects certain functions
138
+ hook = Hook(name="only_sum", kind=FunctionNode, func=lambda n: n.name == "SUM")
139
+ lineage.register_hooks(hooks=[hook])
140
+ ```
141
+ When you run `lineage.generate()`, the only functions that are included as nodes are those named `SUM`.
142
+
143
+ ## Searching the lineage
144
+ To map the lineage into a graph, sqlleaf is built on top of the popular library `networkx`.
145
+ If you need to search the graph, you can use any of network'x graph traversal functions.
146
+ There are currently no dedicated search APIs for sqlleaf. These will be developed in an upcoming major release.
147
+
148
+ There are various functions for printing the lineage: `print_paths()`, `print_tree()`, and `print_json()`.
149
+
150
+
151
+ ## Examples
152
+ Below are examples of SQL queries and their corresponding lineages.
153
+
154
+ #### 1. Simple INSERT
155
+ ```sql
156
+ INSERT INTO dst (name) VALUES (3);
157
+ ```
158
+ `literal[3] -> column[dst.name]`
159
+
160
+ #### 2. JSONPath selectors
161
+ ```sql
162
+ INSERT INTO dst SELECT jsonblob ->> 'fruits' -> 'apple' AS name FROM src;
163
+ ```
164
+ `column[src.name] → jsonpath[.fruits.apple] → column[dst.name]`
165
+
166
+ #### 3. Using variables via SET
167
+ ```snowflake
168
+ SET b = 'src';
169
+ CREATE TABLE dst AS SELECT col1 FROM IDENTIFIER($b);
170
+ ```
171
+ `column[SRC.COL1] → column[DST.COL1]`
172
+
173
+ #### 4. COPY queries
174
+ ```sql
175
+ COPY dst TO STDOUT;
176
+ ```
177
+ `column[dst.col1] → stream[stdout]` \
178
+ `column[dst.col2] → stream[stdout] `
179
+
180
+ ```sql
181
+ COPY dst TO PROGRAM 'gzip > /tmp/data.csv.gz';
182
+ ```
183
+ `column[dst.col1] → program[gzip]` \
184
+ `column[dst.col2] → program[gzip] `
185
+
186
+ #### 5. UNLOAD query
187
+ ```redshift
188
+ UNLOAD ('SELECT name FROM src') TO 's3://dst-bucket/name-prefix' IAM_ROLE '...';
189
+ ```
190
+ `column[src.name] → column[name path=s3://dst-bucket/name-prefix]`
191
+
192
+ #### 6. Sequences
193
+ ```sql
194
+ CREATE SEQUENCE my_serial START 101;
195
+ INSERT INTO dst (age) SELECT nextval('my_serial') as age;
196
+ ```
197
+ `sequence[my_serial] → column[dst.age]`
198
+
199
+ #### 7. An UPDATE with inner CTEs containing UPDATEs that return their old and new columns
200
+ ```sql
201
+ WITH first_cte AS (
202
+ UPDATE fruit.raw
203
+ SET name = 'pear'
204
+ RETURNING age, old.age as old_age, new.age as new_age
205
+ ),
206
+ second_cte AS (
207
+ UPDATE fruit.raw AS r
208
+ SET name = 'tomato'
209
+ RETURNING *, OLD.*, NEW.*
210
+ )
211
+ UPDATE fruit.processed
212
+ SET age = first_cte.age
213
+ FROM first_cte;
214
+ ```
215
+ `column[fruit.raw.age] -> column[first_cte.age] -> column[fruit.processed.age]` \
216
+ `literal["pear"] -> column[fruit.raw.name]` \
217
+ `literal["tomato"] -> column[fruit.raw.name`
218
+
219
+ ## Limitations
220
+ - Many of the dialects are incomplete. Postgres and PL/pgSQL have been mapped out extensively, but most of the dialects do not have their own specific features parsed into graphs yet. Dialects that do have features are Athena, Redshift, MySQL and Snowflake; the others have not been implemented at all yet.
221
+
222
+ - sqlleaf does not support data deletion. Statements like `DELETE`, `TRUNCATE` or `DROP TABLE` currently have no effect. They will in the future.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sqlleaf"
3
- version = "0.0.1"
3
+ version = "0.0.3"
4
4
  authors = [
5
5
  {name = "zac", email = "zac@sqlleaf.io"}
6
6
  ]
@@ -8,9 +8,14 @@ description = "Trace your data"
8
8
  readme = "README.md"
9
9
  requires-python = ">=3.14"
10
10
  dependencies = [
11
- "build>=1.6.1",
12
11
  "networkx==3.6.1",
13
- "sqlglot==30.17.0",
12
+ "sqlglot==30.22.0",
13
+ ]
14
+
15
+ [dependency-groups]
16
+ test = [
17
+ "pytest>=9.1.1",
18
+ "pytest-xdist>=3.8.0",
14
19
  ]
15
20
 
16
21
  [project.urls]
@@ -45,7 +50,4 @@ line-ending = "auto"
45
50
 
46
51
  [tool.ruff.lint]
47
52
  select = ["F", "E", "W", "I"]
48
- ignore = ["E501"]
49
-
50
- [tool.vulture]
51
- min_confidence = 100
53
+ ignore = ["E501", "E731"]
@@ -2,3 +2,4 @@ from sqlleaf.lineage import Lineage as Lineage
2
2
  from sqlleaf.processors.collector.collector import collect_queries as collect_queries
3
3
  from sqlleaf.processors.generator.generator import generate_lineage_for_query as generate_lineage_for_query
4
4
  from sqlleaf.processors.transformer.transformer import transform_query as transform_query
5
+ from sqlleaf.dialects import PlPgSQL
@@ -0,0 +1 @@
1
+ from sqlleaf.dialects.plpgsql.p_dialect import PlPgSQL
@@ -0,0 +1 @@
1
+ from sqlleaf.dialects.plpgsql import p_classes as pgexp
@@ -146,7 +146,7 @@ class PGGetDiagnostics(exp.Expression):
146
146
  arg_types = {"current": False, "stacked": False, "expressions": True}
147
147
 
148
148
 
149
- class PGOpenCursor(exp.Expression):
149
+ class PGOpen(exp.Expression):
150
150
  """An OPEN cursor statement."""
151
151
  arg_types = {"this": True, "expression": False, "scroll": False, "expressions": False}
152
152
 
@@ -25,7 +25,7 @@ class PlPgSQLGenerator(PostgresGenerator):
25
25
  PGContinue: lambda self, e: self.pgcontinue_sql(e),
26
26
  PGRaise: lambda self, e: self.pgraise_sql(e),
27
27
  PGSqlState: lambda self, e: self.pgsqlstate_sql(e),
28
- PGOpenCursor: lambda self, e: self.pgopencursor_sql(e),
28
+ PGOpen: lambda self, e: self.pgopen_sql(e),
29
29
  PGFetch: lambda self, e: self.pgfetch_sql(e),
30
30
  PGMove: lambda self, e: self.pgmove_sql(e),
31
31
  PGClose: lambda self, e: self.pgclose_sql(e),
@@ -419,7 +419,7 @@ class PlPgSQLGenerator(PostgresGenerator):
419
419
  return f"{self.sql(dir_expr)} {prep} {self.sql(expression.this)}"
420
420
  return self.sql(expression.this)
421
421
 
422
- def pgopencursor_sql(self, expression: PGOpenCursor) -> str:
422
+ def pgopen_sql(self, expression: PGOpen) -> str:
423
423
  # Unbound form: OPEN c [NO|SCROLL] FOR <query>
424
424
  if expression.args.get("expression") is not None:
425
425
  parts: list[str] = ["OPEN", self.sql(expression.this)]
@@ -1053,7 +1053,7 @@ class PlPgSQLParser(PostgresParser):
1053
1053
 
1054
1054
  return args
1055
1055
 
1056
- def _parse_pgopen(self) -> PGOpenCursor:
1056
+ def _parse_pgopen(self) -> PGOpen:
1057
1057
  # Cursor variable identifier
1058
1058
  cursor = self._parse_id_var()
1059
1059
 
@@ -1062,7 +1062,7 @@ class PlPgSQLParser(PostgresParser):
1062
1062
  if self._match(TokenType.L_PAREN, advance=False):
1063
1063
  # Parse cursor call arguments directly (positional, name := value, name => value)
1064
1064
  args = self._parse_cursor_call_args()
1065
- return self.expression(PGOpenCursor(this=cursor, expressions=args))
1065
+ return self.expression(PGOpen(this=cursor, expressions=args))
1066
1066
 
1067
1067
  # Optional [[NO] SCROLL]
1068
1068
  scroll: bool | None = None
@@ -1075,7 +1075,7 @@ class PlPgSQLParser(PostgresParser):
1075
1075
  # If there's no FOR and no SCROLL/NO SCROLL, treat as bound cursor with zero args: OPEN c;
1076
1076
  if not self._match(TokenType.FOR):
1077
1077
  if scroll is None:
1078
- return self.expression(PGOpenCursor(this=cursor))
1078
+ return self.expression(PGOpen(this=cursor))
1079
1079
  # If SCROLL/NO SCROLL was provided, FOR is required
1080
1080
  self._match_expect(TokenType.FOR)
1081
1081
 
@@ -1088,7 +1088,7 @@ class PlPgSQLParser(PostgresParser):
1088
1088
  if query is None:
1089
1089
  self.raise_error("Expected query after OPEN ... FOR")
1090
1090
 
1091
- return self.expression(PGOpenCursor(this=cursor, expression=query, scroll=scroll))
1091
+ return self.expression(PGOpen(this=cursor, expression=query, scroll=scroll))
1092
1092
 
1093
1093
  def _parse_pgfetch(self) -> PGFetch:
1094
1094
  # Parse optional direction, optional preposition (required when direction given), and cursor
@@ -6,12 +6,12 @@ ERROR_LEVEL = "stop"
6
6
  logger = logging.getLogger("sqlleaf")
7
7
 
8
8
 
9
- def raise_error(clazz: type, message: str):
9
+ def raise_error(cls: type, message: str):
10
10
  """
11
11
  Raise a custom exception based on the error level.
12
12
  """
13
13
  if ERROR_LEVEL == "stop":
14
- raise clazz(message)
14
+ raise cls(message)
15
15
  elif ERROR_LEVEL == "continue":
16
16
  logger.error(message)
17
17