sqlleaf 0.0.4__tar.gz → 0.0.7__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 (147) hide show
  1. {sqlleaf-0.0.4/sqlleaf.egg-info → sqlleaf-0.0.7}/PKG-INFO +43 -5
  2. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/README.md +40 -2
  3. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/pyproject.toml +3 -3
  4. {sqlleaf-0.0.4/sqlleaf/settings → sqlleaf-0.0.7/sqlleaf/dialects}/__init__.py +5 -4
  5. sqlleaf-0.0.7/sqlleaf/dialects/athena/config.py +84 -0
  6. {sqlleaf-0.0.4/sqlleaf/settings → sqlleaf-0.0.7/sqlleaf/dialects}/base.py +22 -2
  7. sqlleaf-0.0.7/sqlleaf/dialects/loader.py +51 -0
  8. sqlleaf-0.0.4/sqlleaf/settings/postgres.py → sqlleaf-0.0.7/sqlleaf/dialects/postgres/config.py +10 -5
  9. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/lineage.py +48 -3
  10. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/mappings.py +148 -43
  11. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/__init__.py +25 -0
  12. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/column.py +38 -6
  13. sqlleaf-0.0.7/sqlleaf/models/node/counter.py +16 -0
  14. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/dynamodb.py +2 -2
  15. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/function.py +2 -2
  16. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/interval.py +2 -2
  17. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/json.py +2 -2
  18. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/literal.py +2 -2
  19. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/null.py +2 -2
  20. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/pivot.py +3 -3
  21. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/program.py +2 -2
  22. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/sequence.py +2 -2
  23. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/star.py +2 -2
  24. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/stream.py +2 -2
  25. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/user_defined_function.py +2 -2
  26. sqlleaf-0.0.7/sqlleaf/models/node/var.py +7 -0
  27. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/variable.py +2 -2
  28. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/window.py +2 -2
  29. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/__init__.py +2 -0
  30. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/base.py +1 -1
  31. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/copy.py +2 -2
  32. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/ctas.py +3 -0
  33. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/open.py +1 -1
  34. sqlleaf-0.0.7/sqlleaf/models/query/reset.py +30 -0
  35. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/set.py +56 -6
  36. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/table.py +26 -5
  37. sqlleaf-0.0.7/sqlleaf/models/query/view.py +107 -0
  38. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/path.py +21 -1
  39. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/collector/classify.py +22 -0
  40. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/collector/collector.py +95 -6
  41. sqlleaf-0.0.7/sqlleaf/processors/generator/__init__.py +0 -0
  42. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/generator/dialects/base.py +18 -7
  43. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/generator/generator.py +8 -1
  44. sqlleaf-0.0.7/sqlleaf/processors/substitutor/__init__.py +0 -0
  45. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/substitutor/substitute.py +39 -13
  46. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/base.py +112 -47
  47. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/copy.py +4 -4
  48. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/ctas.py +2 -3
  49. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/delete.py +2 -1
  50. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/expressions/__init__.py +3 -0
  51. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/expressions/functional_notation.py +1 -2
  52. sqlleaf-0.0.7/sqlleaf/processors/transformer/expressions/operator.py +32 -0
  53. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/expressions/row.py +1 -2
  54. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/insert.py +3 -4
  55. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/merge.py +2 -1
  56. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/open.py +2 -2
  57. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/transformer.py +9 -9
  58. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/unload.py +1 -1
  59. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/update.py +4 -5
  60. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/typing.py +7 -13
  61. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/util/expression.py +3 -3
  62. {sqlleaf-0.0.4 → sqlleaf-0.0.7/sqlleaf.egg-info}/PKG-INFO +43 -5
  63. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf.egg-info/SOURCES.txt +9 -4
  64. sqlleaf-0.0.7/sqlleaf.egg-info/requires.txt +2 -0
  65. sqlleaf-0.0.4/sqlleaf/dialects/__init__.py +0 -1
  66. sqlleaf-0.0.4/sqlleaf/models/node/var.py +0 -7
  67. sqlleaf-0.0.4/sqlleaf/models/query/view.py +0 -41
  68. sqlleaf-0.0.4/sqlleaf/settings/athena.py +0 -23
  69. sqlleaf-0.0.4/sqlleaf.egg-info/requires.txt +0 -2
  70. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/LICENSE +0 -0
  71. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/setup.cfg +0 -0
  72. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/__init__.py +0 -0
  73. {sqlleaf-0.0.4/sqlleaf/models → sqlleaf-0.0.7/sqlleaf/dialects/athena}/__init__.py +0 -0
  74. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/dialects/plpgsql/__init__.py +0 -0
  75. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/dialects/plpgsql/p_classes.py +0 -0
  76. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/dialects/plpgsql/p_dialect.py +0 -0
  77. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/dialects/plpgsql/p_generator.py +0 -0
  78. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/dialects/plpgsql/p_parser.py +0 -0
  79. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/dialects/plpgsql/p_tokenizer.py +0 -0
  80. {sqlleaf-0.0.4/sqlleaf/processors → sqlleaf-0.0.7/sqlleaf/dialects/postgres}/__init__.py +0 -0
  81. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/exception.py +0 -0
  82. {sqlleaf-0.0.4/sqlleaf/processors/collector → sqlleaf-0.0.7/sqlleaf/models}/__init__.py +0 -0
  83. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/context.py +0 -0
  84. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/hook.py +0 -0
  85. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/node/base.py +0 -0
  86. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/block.py +0 -0
  87. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/call.py +0 -0
  88. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/case.py +0 -0
  89. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/database.py +0 -0
  90. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/declare_item.py +0 -0
  91. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/delete.py +0 -0
  92. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/do.py +0 -0
  93. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/exception_.py +0 -0
  94. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/execute.py +0 -0
  95. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/execute_dynamic.py +0 -0
  96. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/fetch.py +0 -0
  97. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/for_in.py +0 -0
  98. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/holder.py +0 -0
  99. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/if_.py +0 -0
  100. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/insert.py +0 -0
  101. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/loop.py +0 -0
  102. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/merge.py +0 -0
  103. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/multitable_insert.py +0 -0
  104. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/perform.py +0 -0
  105. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/prepare.py +0 -0
  106. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/procedure.py +0 -0
  107. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/put.py +0 -0
  108. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/replace.py +0 -0
  109. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/schema.py +0 -0
  110. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/select.py +0 -0
  111. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/sequence.py +0 -0
  112. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/stage.py +0 -0
  113. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/trigger.py +0 -0
  114. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/type.py +0 -0
  115. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/unload.py +0 -0
  116. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/update.py +0 -0
  117. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/user_defined_function.py +0 -0
  118. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/values.py +0 -0
  119. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/models/query/while_.py +0 -0
  120. {sqlleaf-0.0.4/sqlleaf/processors/generator → sqlleaf-0.0.7/sqlleaf/processors}/__init__.py +0 -0
  121. {sqlleaf-0.0.4/sqlleaf/processors/substitutor → sqlleaf-0.0.7/sqlleaf/processors/collector}/__init__.py +0 -0
  122. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/generator/dialects/__init__.py +0 -0
  123. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/generator/dialects/athena.py +0 -0
  124. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/generator/dialects/mysql.py +0 -0
  125. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/generator/dialects/postgres.py +0 -0
  126. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/generator/dialects/redshift.py +0 -0
  127. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/generator/dialects/snowflake.py +0 -0
  128. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/__init__.py +0 -0
  129. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/assignment.py +0 -0
  130. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/call.py +0 -0
  131. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/case.py +0 -0
  132. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/execute.py +0 -0
  133. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/execute_dynamic.py +0 -0
  134. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/expressions/format.py +0 -0
  135. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/expressions/values.py +0 -0
  136. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/for_in.py +1 -1
  137. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/multitable_insert.py +0 -0
  138. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/prepare.py +0 -0
  139. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/replace.py +0 -0
  140. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/processors/transformer/values.py +0 -0
  141. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/util/__init__.py +0 -0
  142. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/util/graph.py +0 -0
  143. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/util/helpers.py +0 -0
  144. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf/util/iterators.py +0 -0
  145. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf.egg-info/dependency_links.txt +0 -0
  146. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/sqlleaf.egg-info/top_level.txt +0 -0
  147. {sqlleaf-0.0.4 → sqlleaf-0.0.7}/tests/test_hooks.py +0 -0
@@ -1,14 +1,14 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlleaf
3
- Version: 0.0.4
3
+ Version: 0.0.7
4
4
  Summary: Trace your data
5
5
  Author-email: zac <zac@sqlleaf.io>
6
6
  Project-URL: Homepage, https://github.com/sqlleaf-io/sqlleaf
7
7
  Requires-Python: >=3.14
8
8
  Description-Content-Type: text/markdown
9
9
  License-File: LICENSE
10
- Requires-Dist: networkx==3.6.1
11
- Requires-Dist: sqlglot==30.22.0
10
+ Requires-Dist: networkx==3.7
11
+ Requires-Dist: sqlglot==30.23.0
12
12
  Dynamic: license-file
13
13
 
14
14
  # sqlleaf
@@ -210,6 +210,7 @@ INSERT INTO dst (age) SELECT nextval('my_serial') as age;
210
210
  `sequence[my_serial] → column[dst.age]`
211
211
 
212
212
  #### 7. An UPDATE with inner CTEs containing UPDATEs that return their old and new columns
213
+ sqlleaf will expand the `RETURNING *` into the correct columns and connect them together:
213
214
  ```sql
214
215
  WITH first_cte AS (
215
216
  UPDATE fruit.raw
@@ -227,9 +228,46 @@ FROM first_cte;
227
228
  ```
228
229
  `column[fruit.raw.age] -> column[first_cte.age] -> column[fruit.processed.age]` \
229
230
  `literal["pear"] -> column[fruit.raw.name]` \
230
- `literal["tomato"] -> column[fruit.raw.name`
231
+ `literal["tomato"] -> column[fruit.raw.name]`
232
+
233
+ #### 8. PREPARE and EXECUTE
234
+ Set the search path in Postgres and run some queries:
235
+ ```sql
236
+ SET search_path = "$user", public;
237
+
238
+ CREATE TABLE source(name VARCHAR, amount INT);
239
+ CREATE TABLE target(name VARCHAR, amount INT);
240
+
241
+ PREPARE my_plan AS INSERT INTO public.target SELECT * FROM public.source;
242
+ EXECUTE my_plan;
243
+ ```
244
+ `column[public.source.name] -> column[public.target.name]` \
245
+ `column[public.source.amount] -> column[public.target.amount]`
246
+
247
+ ## Graphs, edges and nodes
248
+ Every node that is generated and added to the graph is de-duplicated *except for* columns. This is done by including the position alongside the node's information when calculating its identity.
249
+ In other words, if we have a query:
250
+ ```sql
251
+ SELECT UPPER('a'), UPPER('b')
252
+ ```
253
+ We separate the two function calls `UPPER()` into separate nodes on the graph. This is to keep the graph organised and simple:
254
+ 1) it prevents nodes from having too many edges
255
+ 2) it ensures that paths along the graph are kept separate
256
+ 3) it enables applications such as impact analysis
257
+
258
+ Columns are handled differently from other nodes and are *not* unique per query. This is because they are the most significant source of information and we need to know how information flows across them. Every time a query refers to a column, it is the same column in the graph.
259
+
260
+ ### Node identities
261
+ A node can be represented using two forms:
262
+ - its friendly name: `function[ADD]`
263
+ - its full name: `function[name=ADD type=INT position=[query_depth=0 query_width=0 statement=0 select=0 func_depth=0 func_arg=0]]"`
264
+
265
+ Each of the numbers inside the `position` structure define a coordinate that makes it unique within a single query and across many queries. More information can be found inside the [context class](sqlleaf/models/context.py)
266
+
267
+ ### Node types
268
+ There are currently about 20 node types. These include `ColumnNode`, `LiteralNode`, and `FunctionNode`. For the full list, see [Node types](sqlleaf/models/node/__init__.py)
231
269
 
232
270
  ## 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.
271
+ - Many of the dialects are incomplete. Postgres and PL/pgSQL have been mapped out extensively (see the test cases in [tests/postgres](tests/postgres/__init__.py), 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
272
 
235
273
  - sqlleaf does not support data deletion. Statements like `DELETE`, `TRUNCATE` or `DROP TABLE` currently have no effect. They will in the future.
@@ -197,6 +197,7 @@ INSERT INTO dst (age) SELECT nextval('my_serial') as age;
197
197
  `sequence[my_serial] → column[dst.age]`
198
198
 
199
199
  #### 7. An UPDATE with inner CTEs containing UPDATEs that return their old and new columns
200
+ sqlleaf will expand the `RETURNING *` into the correct columns and connect them together:
200
201
  ```sql
201
202
  WITH first_cte AS (
202
203
  UPDATE fruit.raw
@@ -214,9 +215,46 @@ FROM first_cte;
214
215
  ```
215
216
  `column[fruit.raw.age] -> column[first_cte.age] -> column[fruit.processed.age]` \
216
217
  `literal["pear"] -> column[fruit.raw.name]` \
217
- `literal["tomato"] -> column[fruit.raw.name`
218
+ `literal["tomato"] -> column[fruit.raw.name]`
219
+
220
+ #### 8. PREPARE and EXECUTE
221
+ Set the search path in Postgres and run some queries:
222
+ ```sql
223
+ SET search_path = "$user", public;
224
+
225
+ CREATE TABLE source(name VARCHAR, amount INT);
226
+ CREATE TABLE target(name VARCHAR, amount INT);
227
+
228
+ PREPARE my_plan AS INSERT INTO public.target SELECT * FROM public.source;
229
+ EXECUTE my_plan;
230
+ ```
231
+ `column[public.source.name] -> column[public.target.name]` \
232
+ `column[public.source.amount] -> column[public.target.amount]`
233
+
234
+ ## Graphs, edges and nodes
235
+ Every node that is generated and added to the graph is de-duplicated *except for* columns. This is done by including the position alongside the node's information when calculating its identity.
236
+ In other words, if we have a query:
237
+ ```sql
238
+ SELECT UPPER('a'), UPPER('b')
239
+ ```
240
+ We separate the two function calls `UPPER()` into separate nodes on the graph. This is to keep the graph organised and simple:
241
+ 1) it prevents nodes from having too many edges
242
+ 2) it ensures that paths along the graph are kept separate
243
+ 3) it enables applications such as impact analysis
244
+
245
+ Columns are handled differently from other nodes and are *not* unique per query. This is because they are the most significant source of information and we need to know how information flows across them. Every time a query refers to a column, it is the same column in the graph.
246
+
247
+ ### Node identities
248
+ A node can be represented using two forms:
249
+ - its friendly name: `function[ADD]`
250
+ - its full name: `function[name=ADD type=INT position=[query_depth=0 query_width=0 statement=0 select=0 func_depth=0 func_arg=0]]"`
251
+
252
+ Each of the numbers inside the `position` structure define a coordinate that makes it unique within a single query and across many queries. More information can be found inside the [context class](sqlleaf/models/context.py)
253
+
254
+ ### Node types
255
+ There are currently about 20 node types. These include `ColumnNode`, `LiteralNode`, and `FunctionNode`. For the full list, see [Node types](sqlleaf/models/node/__init__.py)
218
256
 
219
257
  ## 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.
258
+ - Many of the dialects are incomplete. Postgres and PL/pgSQL have been mapped out extensively (see the test cases in [tests/postgres](tests/postgres/__init__.py), 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
259
 
222
260
  - 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.4"
3
+ version = "0.0.7"
4
4
  authors = [
5
5
  {name = "zac", email = "zac@sqlleaf.io"}
6
6
  ]
@@ -8,8 +8,8 @@ description = "Trace your data"
8
8
  readme = "README.md"
9
9
  requires-python = ">=3.14"
10
10
  dependencies = [
11
- "networkx==3.6.1",
12
- "sqlglot==30.22.0",
11
+ "networkx==3.7",
12
+ "sqlglot==30.23.0",
13
13
  ]
14
14
 
15
15
  [dependency-groups]
@@ -1,11 +1,12 @@
1
- import typing as t
1
+ from sqlleaf.dialects.plpgsql.p_dialect import PlPgSQL
2
+
2
3
  from functools import cache
3
4
 
4
5
  from sqlglot import exp
5
6
 
6
- from sqlleaf.settings.athena import AthenaSettings as AthenaSettings
7
- from sqlleaf.settings.base import BaseDialectSettings as BaseDialectSettings
8
- from sqlleaf.settings.postgres import PostgresSettings as PostgresSettings
7
+ from sqlleaf.dialects.athena.config import AthenaSettings as AthenaSettings
8
+ from sqlleaf.dialects.base import BaseDialectSettings as BaseDialectSettings
9
+ from sqlleaf.dialects.postgres.config import PostgresSettings as PostgresSettings
9
10
 
10
11
 
11
12
  @cache
@@ -0,0 +1,84 @@
1
+ from sqlglot import exp
2
+
3
+ from sqlleaf.dialects.base import BaseDialectSettings
4
+
5
+
6
+ class AthenaSettings(BaseDialectSettings):
7
+ DIALECT = "athena"
8
+ DEFAULT_USER_CATALOG = "awsdatacatalog"
9
+ DEFAULT_SYSTEM_CATALOG = ""
10
+ PSEUDOCOLUMNS = {
11
+ "$bucket": "BIGINT",
12
+ "$file_modified_time": "TIMESTAMP",
13
+ "$file_size": "BIGINT",
14
+ "$partition": "VARCHAR",
15
+ "$path": "VARCHAR",
16
+ }
17
+ # Iceberg metadata fields
18
+ METADATA_FIELDS = {
19
+ "$files": {
20
+ "content": "INTEGER",
21
+ "file_path": "VARCHAR",
22
+ "file_format": "VARCHAR",
23
+ "record_count": "BIGINT",
24
+ "file_size_in_bytes": "BIGINT",
25
+ "column_sizes": "MAP(INTEGER, BIGINT)",
26
+ "value_counts": "MAP(INTEGER, BIGINT)",
27
+ "null_value_counts": "MAP(INTEGER, BIGINT)",
28
+ "nan_value_counts": "MAP(INTEGER, BIGINT)",
29
+ "lower_bounds": "MAP(INTEGER, VARCHAR)",
30
+ "upper_bounds": "MAP(INTEGER, VARCHAR)",
31
+ "key_metadata": "VARBINARY",
32
+ "split_offsets": "ARRAY(BIGINT)",
33
+ "equality_ids": "ARRAY(INTEGER)",
34
+ },
35
+ "$manifests": {
36
+ "path": "VARCHAR",
37
+ "length": "BIGINT",
38
+ "partition_spec_id": "INTEGER",
39
+ "added_snapshot_id": "BIGINT",
40
+ "added_data_files_count": "INTEGER",
41
+ "added_rows_count": "BIGINT",
42
+ "existing_data_files_count": "INTEGER",
43
+ "existing_rows_count": "BIGINT",
44
+ "deleted_data_files_count": "INTEGER",
45
+ "deleted_rows_count": "BIGINT",
46
+ "partitions": "ARRAY(STRUCT(contains_null BOOLEAN, contains_nan BOOLEAN, lower_bound VARCHAR, upper_bound VARCHAR))",
47
+ },
48
+ "$history": {
49
+ "made_current_at": "TIMESTAMP WITH TIME ZONE",
50
+ "snapshot_id": "BIGINT",
51
+ "parent_id": "BIGINT",
52
+ "is_current_ancestor": "BOOLEAN",
53
+ },
54
+ "$partitions": {
55
+ "partition": "STRUCT(category VARCHAR)",
56
+ "record_count": "BIGINT",
57
+ "file_count": "BIGINT",
58
+ "total_size": "BIGINT",
59
+ "data": "STRUCT(id STRUCT(min BIGINT, max BIGINT, null_count BIGINT, nan_count BIGINT), data STRUCT(min VARCHAR, max VARCHAR, null_count BIGINT, nan_count BIGINT))",
60
+ },
61
+ "$snapshots": {
62
+ "committed_at": "TIMESTAMP WITH TIME ZONE",
63
+ "snapshot_id": "BIGINT",
64
+ "parent_id": "BIGINT",
65
+ "operation": "VARCHAR",
66
+ "manifest_list": "VARCHAR",
67
+ "summary": "MAP(VARCHAR, VARCHAR)",
68
+ },
69
+ "$refs": {
70
+ "name": "VARCHAR",
71
+ "type": "VARCHAR",
72
+ "snapshot_id": "BIGINT",
73
+ "max_reference_age_in_ms": "BIGINT",
74
+ "min_snapshots_to_keep": "INTEGER",
75
+ "max_snapshot_age_in_ms": "BIGINT",
76
+ },
77
+ }
78
+
79
+ @property
80
+ def system_columns(self) -> list[exp.ColumnDef]:
81
+ return [
82
+ exp.ColumnDef(this=exp.to_identifier(name), kind=exp.DataType.build(kind, self.DIALECT))
83
+ for name, kind in self.PSEUDOCOLUMNS.items()
84
+ ]
@@ -1,5 +1,3 @@
1
- import typing as t
2
-
3
1
  from sqlglot import exp
4
2
 
5
3
 
@@ -7,6 +5,10 @@ class BaseDialectSettings:
7
5
  # A registry to store subclasses
8
6
  _dialects = {}
9
7
  DIALECT = ""
8
+ DEFAULT_USER_CATALOG = ""
9
+ DEFAULT_SYSTEM_CATALOG = ""
10
+ # Metadata fields, keyed by their suffix (e.g. '$files' in 'my_table$files'), mapped to their column names and types
11
+ METADATA_FIELDS: dict[str, dict[str, str]] = {}
10
12
 
11
13
  def __init_subclass__(cls, **kwargs):
12
14
  """Automatically registers subclasses when they are defined."""
@@ -35,3 +37,21 @@ class BaseDialectSettings:
35
37
  Represents system functions.
36
38
  """
37
39
  return {}
40
+
41
+ def get_metadata_field(self, table_name: str) -> str | None:
42
+ """
43
+ Returns the metadata field of a table name (e.g. '$files' for 'my_table$files').
44
+ """
45
+ for suffix in self.METADATA_FIELDS:
46
+ if table_name.lower().endswith(suffix):
47
+ return suffix
48
+ return None
49
+
50
+ def get_metadata_field_column_defs(self, suffix: str) -> list[exp.ColumnDef]:
51
+ """
52
+ Returns the column definitions of the metadata table with the given field (e.g. '$files').
53
+ """
54
+ return [
55
+ exp.ColumnDef(this=exp.to_identifier(name), kind=exp.DataType.build(kind, self.DIALECT))
56
+ for name, kind in self.METADATA_FIELDS[suffix].items()
57
+ ]
@@ -0,0 +1,51 @@
1
+ import re
2
+ from functools import cache
3
+ from pathlib import Path
4
+
5
+ TABLE_HEADER = re.compile(r"^CREATE TABLE (\w+)\.(\w+) \(", re.MULTILINE)
6
+ VIEW_HEADER = re.compile(r"^CREATE VIEW (\w+)\.(\w+) AS", re.MULTILINE)
7
+ SCHEMA_HEADER = re.compile(r"^CREATE SCHEMA (\w+);", re.MULTILINE)
8
+
9
+ KINDS = {
10
+ "tables": (TABLE_HEADER, "\n);"),
11
+ "views": (VIEW_HEADER, ";\n"),
12
+ "schemas": (SCHEMA_HEADER, ";\n"),
13
+ }
14
+
15
+
16
+ @cache
17
+ def _read_system_objects(dialect: str, kind: str) -> tuple[str, dict[tuple[str, ...], tuple[int, int]]]:
18
+ """
19
+ Read the system objects from disk.
20
+ """
21
+ path = Path(__file__).parent / dialect / "system_objects" / f"{kind}.sql"
22
+ if not path.exists():
23
+ return "", {}
24
+
25
+ header, statement_end = KINDS[kind]
26
+ text = path.read_text()
27
+ if kind in ("tables", "views"):
28
+ # sqlglot fails when parsing 'oid[]'
29
+ text = text.replace("oid[]", "array<oid>")
30
+ index = {}
31
+ for match in header.finditer(text):
32
+ end = text.index(statement_end, match.start()) + len(statement_end)
33
+ index[tuple(group.lower() for group in match.groups())] = (match.start(), end)
34
+ return text, index
35
+
36
+
37
+ def _lookup(dialect: str, kind: str, key: tuple[str, ...]) -> str | None:
38
+ text, index = _read_system_objects(dialect, kind)
39
+ span = index.get(key)
40
+ if span is None:
41
+ return None
42
+ return text[span[0] : span[1]]
43
+
44
+
45
+ def get_object_ddl(schema: str, table: str, dialect: str) -> str | None:
46
+ key = (schema.lower(), table.lower())
47
+ return _lookup(dialect, "tables", key) or _lookup(dialect, "views", key)
48
+
49
+
50
+ def get_schema_ddl(schema: str, dialect: str) -> str | None:
51
+ return _lookup(dialect, "schemas", (schema.lower(),))
@@ -1,13 +1,13 @@
1
- import typing as t
2
-
3
1
  from sqlglot import exp
4
2
  from sqlglot.dialects import postgres
5
3
 
6
- from sqlleaf.settings.base import BaseDialectSettings
4
+ from sqlleaf.dialects.base import BaseDialectSettings
7
5
 
8
6
 
9
7
  class PostgresSettings(BaseDialectSettings):
10
8
  DIALECT = "postgres"
9
+ DEFAULT_USER_CATALOG = "public"
10
+ DEFAULT_SYSTEM_CATALOG = "pg_catalog"
11
11
  PSEUDOCOLUMNS = {
12
12
  "cmax": "OID",
13
13
  "cmin": "OID",
@@ -31,7 +31,7 @@ class PostgresSettings(BaseDialectSettings):
31
31
  Map function names to their returned column names.
32
32
  """
33
33
  return {
34
- "aclexplode": ["grantor, grantee, privilege_type, is_grantable"],
34
+ "aclexplode": ["grantor", "grantee", "privilege_type", "is_grantable"],
35
35
  "json_array_elements": ["value"],
36
36
  "json_array_elements_text": ["value"],
37
37
  "json_each": ["key", "value"],
@@ -40,7 +40,7 @@ class PostgresSettings(BaseDialectSettings):
40
40
  "jsonb_array_elements_text": ["value"],
41
41
  "jsonb_each": ["key", "value"],
42
42
  "jsonb_each_text": ["key", "value"],
43
- "ts_debug": ["alias, description, token, dictionaries, dictionary, lexemes"],
43
+ "ts_debug": ["alias", "description", "token", "dictionaries", "dictionary", "lexemes"],
44
44
  "ts_parse": ["tokid", "token"],
45
45
  "ts_stat": ["word", "ndoc", "nentry"],
46
46
  "ts_token_type": ["tokid", "alias", "description"],
@@ -49,4 +49,9 @@ class PostgresSettings(BaseDialectSettings):
49
49
 
50
50
  # sqlglot is missing pseudocolumns for Postgres
51
51
  postgres.Postgres.PSEUDOCOLUMNS = {c.upper() for c in PostgresSettings.PSEUDOCOLUMNS.keys()}
52
+
53
+ # Prevents pseudocolumns from being included in 'SELECT *' expansions
52
54
  postgres.Postgres.EXCLUDES_PSEUDOCOLUMNS_FROM_STAR = True
55
+
56
+ # Prevents sqlglot's optimizer from assigning [n+1] to arrays of [n]
57
+ postgres.Postgres.INDEX_OFFSET = 0
@@ -6,7 +6,7 @@ import networkx as nx
6
6
 
7
7
  from sqlleaf import exception, mappings, path, util
8
8
  from sqlleaf.models.hook import Hook
9
- from sqlleaf.models.node import EdgeAttributes, GraphAttributes, N
9
+ from sqlleaf.models.node import EdgeAttributes, GraphAttributes, N, NodeAttributes
10
10
  from sqlleaf.models.query import (
11
11
  Q,
12
12
  )
@@ -37,8 +37,7 @@ class Lineage:
37
37
  """
38
38
  exception.set_error_action(action=on_error)
39
39
 
40
- if not self.object_mapping:
41
- self.object_mapping = mappings.ObjectMapping(dialect=dialect)
40
+ self.init_object_mapping(dialect)
42
41
 
43
42
  self.collected_queries = collector.collect_queries(
44
43
  text=sql, dialect=dialect, object_mapping=self.object_mapping
@@ -80,6 +79,7 @@ class Lineage:
80
79
  edge_data["attrs"].child = old_node_attrs
81
80
  else:
82
81
  self.graph.add_node(n, **data)
82
+ self.object_mapping.node_index.add(data["attrs"])
83
83
 
84
84
  self.graph.add_edges_from(subgraph.edges(data=True))
85
85
 
@@ -98,6 +98,46 @@ class Lineage:
98
98
  )
99
99
  return nodes
100
100
 
101
+ def find_nodes(self, *nodes: NodeAttributes) -> list[NodeAttributes]:
102
+ """
103
+ Find a set of nodes in the graph matching the given node instances.
104
+
105
+ This allows nodes to be found without knowing their positions.
106
+
107
+ This uses an index/map structure because the keys of the full node names contain
108
+ the positions of each node to identify them uniquely. For example, two literal
109
+ expressions for "abc" are assigned their own positions to distinguish them from each other.
110
+
111
+ Examples:
112
+ # Find all columns whose name is 'price'
113
+ find_node(ColumnNode(column="price"))
114
+
115
+ # Find two columns: one unqualified, another fully qualified
116
+ find_node(ColumnNode(column="age"), ColumnNode(schema="fruit", table="raw", column="name"))
117
+
118
+ # Find all functions
119
+ find_node(FunctionNode())
120
+ """
121
+ if self.object_mapping is None:
122
+ return []
123
+
124
+ found: list[NodeAttributes] = []
125
+ seen: set[str] = set()
126
+
127
+ for query in nodes:
128
+ candidates = self.object_mapping.node_index.get(type(query))
129
+ if query.name != "":
130
+ query_name = query.get_name()
131
+ candidates = [node for node in candidates if node.get_name() == query_name]
132
+
133
+ for node in candidates:
134
+ if node.full_name not in seen:
135
+ seen.add(node.full_name)
136
+ found.append(node)
137
+
138
+ return found
139
+
140
+
101
141
  def get_original_queries(self) -> list[Q]:
102
142
  """
103
143
  Get the original queries from the graph.
@@ -262,6 +302,11 @@ class Lineage:
262
302
  """
263
303
  self.user_defined_hooks = [udh for udh in self.user_defined_hooks if udh not in hooks]
264
304
 
305
+ def init_object_mapping(self, dialect: str):
306
+ if not self.object_mapping:
307
+ self.object_mapping = mappings.ObjectMapping(dialect=dialect, object_loader=collector.SystemObjectLoader(dialect))
308
+
309
+
265
310
  def new_graph() -> nx.MultiDiGraph:
266
311
  """
267
312
  A graph has attributes along with its node and edges.