sqlleaf 0.0.5__tar.gz → 0.0.8__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.
- {sqlleaf-0.0.5/sqlleaf.egg-info → sqlleaf-0.0.8}/PKG-INFO +43 -5
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/README.md +40 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/pyproject.toml +3 -3
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/__init__.py +0 -1
- sqlleaf-0.0.8/sqlleaf/dialects/__init__.py +8 -0
- sqlleaf-0.0.8/sqlleaf/dialects/athena/config.py +92 -0
- sqlleaf-0.0.8/sqlleaf/dialects/base.py +86 -0
- sqlleaf-0.0.8/sqlleaf/dialects/loader.py +51 -0
- sqlleaf-0.0.8/sqlleaf/dialects/mysql/config.py +33 -0
- sqlleaf-0.0.5/sqlleaf/settings/postgres.py → sqlleaf-0.0.8/sqlleaf/dialects/postgres/config.py +14 -9
- sqlleaf-0.0.8/sqlleaf/dialects/redshift/config.py +12 -0
- sqlleaf-0.0.8/sqlleaf/exception.py +94 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/lineage.py +55 -10
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/mappings.py +92 -16
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/context.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/__init__.py +25 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/column.py +38 -6
- sqlleaf-0.0.8/sqlleaf/models/node/counter.py +16 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/dynamodb.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/function.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/interval.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/json.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/literal.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/null.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/pivot.py +3 -3
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/program.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/sequence.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/star.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/stream.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/user_defined_function.py +2 -2
- sqlleaf-0.0.8/sqlleaf/models/node/var.py +7 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/variable.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/window.py +2 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/__init__.py +4 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/base.py +9 -3
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/ctas.py +6 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/declare_item.py +6 -2
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/execute.py +6 -0
- sqlleaf-0.0.8/sqlleaf/models/query/prepare.py +108 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/procedure.py +9 -8
- sqlleaf-0.0.8/sqlleaf/models/query/reset.py +30 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/select.py +13 -1
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/set.py +61 -11
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/table.py +30 -5
- sqlleaf-0.0.8/sqlleaf/models/query/use.py +26 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/user_defined_function.py +19 -1
- sqlleaf-0.0.8/sqlleaf/models/query/view.py +107 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/path.py +27 -7
- sqlleaf-0.0.8/sqlleaf/processors/__init__.py +0 -0
- sqlleaf-0.0.8/sqlleaf/processors/collector/__init__.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/collector/classify.py +84 -8
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/collector/collector.py +104 -23
- sqlleaf-0.0.8/sqlleaf/processors/generator/__init__.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/generator/dialects/base.py +16 -5
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/generator/generator.py +19 -9
- sqlleaf-0.0.8/sqlleaf/processors/substitutor/__init__.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/substitutor/substitute.py +37 -7
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/base.py +108 -4
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/expressions/__init__.py +3 -0
- sqlleaf-0.0.8/sqlleaf/processors/transformer/expressions/operator.py +32 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/transformer.py +8 -1
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/typing.py +5 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/util/__init__.py +2 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/util/expression.py +82 -5
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/util/graph.py +11 -10
- {sqlleaf-0.0.5 → sqlleaf-0.0.8/sqlleaf.egg-info}/PKG-INFO +43 -5
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf.egg-info/SOURCES.txt +15 -6
- sqlleaf-0.0.8/sqlleaf.egg-info/requires.txt +2 -0
- sqlleaf-0.0.5/sqlleaf/dialects/__init__.py +0 -1
- sqlleaf-0.0.5/sqlleaf/exception.py +0 -67
- sqlleaf-0.0.5/sqlleaf/models/node/var.py +0 -7
- sqlleaf-0.0.5/sqlleaf/models/query/prepare.py +0 -66
- sqlleaf-0.0.5/sqlleaf/models/query/view.py +0 -41
- sqlleaf-0.0.5/sqlleaf/settings/__init__.py +0 -19
- sqlleaf-0.0.5/sqlleaf/settings/athena.py +0 -23
- sqlleaf-0.0.5/sqlleaf/settings/base.py +0 -37
- sqlleaf-0.0.5/sqlleaf.egg-info/requires.txt +0 -2
- sqlleaf-0.0.5/tests/test_hooks.py +0 -64
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/LICENSE +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/setup.cfg +0 -0
- {sqlleaf-0.0.5/sqlleaf/models → sqlleaf-0.0.8/sqlleaf/dialects/athena}/__init__.py +0 -0
- {sqlleaf-0.0.5/sqlleaf/processors → sqlleaf-0.0.8/sqlleaf/dialects/mysql}/__init__.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/dialects/plpgsql/__init__.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/dialects/plpgsql/p_classes.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/dialects/plpgsql/p_dialect.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/dialects/plpgsql/p_generator.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/dialects/plpgsql/p_parser.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/dialects/plpgsql/p_tokenizer.py +0 -0
- {sqlleaf-0.0.5/sqlleaf/processors/collector → sqlleaf-0.0.8/sqlleaf/dialects/postgres}/__init__.py +0 -0
- {sqlleaf-0.0.5/sqlleaf/processors/generator → sqlleaf-0.0.8/sqlleaf/dialects/redshift}/__init__.py +0 -0
- {sqlleaf-0.0.5/sqlleaf/processors/substitutor → sqlleaf-0.0.8/sqlleaf/models}/__init__.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/hook.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/node/base.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/block.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/call.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/case.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/copy.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/database.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/delete.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/do.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/exception_.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/execute_dynamic.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/fetch.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/for_in.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/holder.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/if_.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/insert.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/loop.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/merge.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/multitable_insert.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/open.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/perform.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/put.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/replace.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/schema.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/sequence.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/stage.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/trigger.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/type.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/unload.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/update.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/values.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/models/query/while_.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/generator/dialects/__init__.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/generator/dialects/athena.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/generator/dialects/mysql.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/generator/dialects/postgres.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/generator/dialects/redshift.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/generator/dialects/snowflake.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/__init__.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/assignment.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/call.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/case.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/copy.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/ctas.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/delete.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/execute.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/execute_dynamic.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/expressions/format.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/expressions/functional_notation.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/expressions/row.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/expressions/values.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/for_in.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/insert.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/merge.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/multitable_insert.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/open.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/prepare.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/replace.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/unload.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/update.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/processors/transformer/values.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/util/helpers.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf/util/iterators.py +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf.egg-info/dependency_links.txt +0 -0
- {sqlleaf-0.0.5 → sqlleaf-0.0.8}/sqlleaf.egg-info/top_level.txt +0 -0
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sqlleaf
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.8
|
|
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.
|
|
11
|
-
Requires-Dist: sqlglot==30.
|
|
10
|
+
Requires-Dist: networkx==3.7
|
|
11
|
+
Requires-Dist: sqlglot==30.30.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.
|
|
3
|
+
version = "0.0.8"
|
|
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.
|
|
12
|
-
"sqlglot==30.
|
|
11
|
+
"networkx==3.7",
|
|
12
|
+
"sqlglot==30.30.0",
|
|
13
13
|
]
|
|
14
14
|
|
|
15
15
|
[dependency-groups]
|
|
@@ -2,4 +2,3 @@ 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,8 @@
|
|
|
1
|
+
# Maps each dialect name to the module that registers its config or parser. Lazily imported for performance.
|
|
2
|
+
DIALECT_MODULES = {
|
|
3
|
+
"athena": "sqlleaf.dialects.athena.config",
|
|
4
|
+
"mysql": "sqlleaf.dialects.mysql.config",
|
|
5
|
+
"plpgsql": "sqlleaf.dialects.plpgsql.p_dialect",
|
|
6
|
+
"postgres": "sqlleaf.dialects.postgres.config",
|
|
7
|
+
"redshift": "sqlleaf.dialects.redshift.config",
|
|
8
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from sqlglot import exp
|
|
2
|
+
from sqlglot.dialects import athena
|
|
3
|
+
|
|
4
|
+
from sqlleaf.dialects.base import DialectConfig
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class AthenaConfig(DialectConfig):
|
|
8
|
+
DIALECT = "athena"
|
|
9
|
+
DEFAULT_USER_CATALOG = "awsdatacatalog"
|
|
10
|
+
DEFAULT_SYSTEM_CATALOG = ""
|
|
11
|
+
PSEUDOCOLUMNS = {
|
|
12
|
+
"$bucket": "BIGINT",
|
|
13
|
+
"$file_modified_time": "TIMESTAMP",
|
|
14
|
+
"$file_size": "BIGINT",
|
|
15
|
+
"$partition": "VARCHAR",
|
|
16
|
+
"$path": "VARCHAR",
|
|
17
|
+
}
|
|
18
|
+
# Iceberg metadata fields
|
|
19
|
+
METADATA_FIELDS = {
|
|
20
|
+
"$files": {
|
|
21
|
+
"content": "INTEGER",
|
|
22
|
+
"file_path": "VARCHAR",
|
|
23
|
+
"file_format": "VARCHAR",
|
|
24
|
+
"record_count": "BIGINT",
|
|
25
|
+
"file_size_in_bytes": "BIGINT",
|
|
26
|
+
"column_sizes": "MAP(INTEGER, BIGINT)",
|
|
27
|
+
"value_counts": "MAP(INTEGER, BIGINT)",
|
|
28
|
+
"null_value_counts": "MAP(INTEGER, BIGINT)",
|
|
29
|
+
"nan_value_counts": "MAP(INTEGER, BIGINT)",
|
|
30
|
+
"lower_bounds": "MAP(INTEGER, VARCHAR)",
|
|
31
|
+
"upper_bounds": "MAP(INTEGER, VARCHAR)",
|
|
32
|
+
"key_metadata": "VARBINARY",
|
|
33
|
+
"split_offsets": "ARRAY(BIGINT)",
|
|
34
|
+
"equality_ids": "ARRAY(INTEGER)",
|
|
35
|
+
},
|
|
36
|
+
"$manifests": {
|
|
37
|
+
"path": "VARCHAR",
|
|
38
|
+
"length": "BIGINT",
|
|
39
|
+
"partition_spec_id": "INTEGER",
|
|
40
|
+
"added_snapshot_id": "BIGINT",
|
|
41
|
+
"added_data_files_count": "INTEGER",
|
|
42
|
+
"added_rows_count": "BIGINT",
|
|
43
|
+
"existing_data_files_count": "INTEGER",
|
|
44
|
+
"existing_rows_count": "BIGINT",
|
|
45
|
+
"deleted_data_files_count": "INTEGER",
|
|
46
|
+
"deleted_rows_count": "BIGINT",
|
|
47
|
+
"partitions": "ARRAY(STRUCT(contains_null BOOLEAN, contains_nan BOOLEAN, lower_bound VARCHAR, upper_bound VARCHAR))",
|
|
48
|
+
},
|
|
49
|
+
"$history": {
|
|
50
|
+
"made_current_at": "TIMESTAMP WITH TIME ZONE",
|
|
51
|
+
"snapshot_id": "BIGINT",
|
|
52
|
+
"parent_id": "BIGINT",
|
|
53
|
+
"is_current_ancestor": "BOOLEAN",
|
|
54
|
+
},
|
|
55
|
+
"$partitions": {
|
|
56
|
+
"partition": "STRUCT(category VARCHAR)",
|
|
57
|
+
"record_count": "BIGINT",
|
|
58
|
+
"file_count": "BIGINT",
|
|
59
|
+
"total_size": "BIGINT",
|
|
60
|
+
"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))",
|
|
61
|
+
},
|
|
62
|
+
"$snapshots": {
|
|
63
|
+
"committed_at": "TIMESTAMP WITH TIME ZONE",
|
|
64
|
+
"snapshot_id": "BIGINT",
|
|
65
|
+
"parent_id": "BIGINT",
|
|
66
|
+
"operation": "VARCHAR",
|
|
67
|
+
"manifest_list": "VARCHAR",
|
|
68
|
+
"summary": "MAP(VARCHAR, VARCHAR)",
|
|
69
|
+
},
|
|
70
|
+
"$refs": {
|
|
71
|
+
"name": "VARCHAR",
|
|
72
|
+
"type": "VARCHAR",
|
|
73
|
+
"snapshot_id": "BIGINT",
|
|
74
|
+
"max_reference_age_in_ms": "BIGINT",
|
|
75
|
+
"min_snapshots_to_keep": "INTEGER",
|
|
76
|
+
"max_snapshot_age_in_ms": "BIGINT",
|
|
77
|
+
},
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def SYSTEM_COLUMNS(self) -> list[exp.ColumnDef]:
|
|
82
|
+
return [
|
|
83
|
+
exp.ColumnDef(this=exp.to_identifier(name), kind=exp.DataType.build(kind, self.DIALECT))
|
|
84
|
+
for name, kind in self.PSEUDOCOLUMNS.items()
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# sqlglot is missing pseudocolumns for Athena
|
|
89
|
+
athena.Athena.PSEUDOCOLUMNS = {c.upper() for c in AthenaConfig.PSEUDOCOLUMNS.keys()}
|
|
90
|
+
|
|
91
|
+
# Prevents pseudocolumns from being included in 'SELECT *' expansions
|
|
92
|
+
athena.Athena.EXCLUDES_PSEUDOCOLUMNS_FROM_STAR = True
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import importlib
|
|
2
|
+
import typing as t
|
|
3
|
+
|
|
4
|
+
import sqlglot
|
|
5
|
+
from sqlglot import Dialect, exp
|
|
6
|
+
|
|
7
|
+
from sqlleaf.dialects import DIALECT_MODULES
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# Wrap a function parameter parser so that it also accepts IN, OUT, and INOUT parameter modes
|
|
11
|
+
def with_parameter_modes(parse_function_parameter: t.Callable) -> t.Callable:
|
|
12
|
+
def _parse_function_parameter(self) -> exp.Expr | None:
|
|
13
|
+
mode = self._match_texts(("INOUT", "IN", "OUT")) and self._prev.text.upper()
|
|
14
|
+
parameter = parse_function_parameter(self)
|
|
15
|
+
if mode and parameter:
|
|
16
|
+
parameter.append(
|
|
17
|
+
"constraints",
|
|
18
|
+
exp.InOutColumnConstraint(input_=mode in ("IN", "INOUT"), output=mode in ("OUT", "INOUT")),
|
|
19
|
+
)
|
|
20
|
+
return parameter
|
|
21
|
+
|
|
22
|
+
return _parse_function_parameter
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class DialectConfig:
|
|
26
|
+
# A registry to store subclasses
|
|
27
|
+
_dialects = {}
|
|
28
|
+
DIALECT = ""
|
|
29
|
+
DEFAULT_USER_CATALOG = ""
|
|
30
|
+
DEFAULT_SYSTEM_CATALOG = ""
|
|
31
|
+
# The language of procedures that do not declare one explicitly
|
|
32
|
+
DEFAULT_PROCEDURE_LANGUAGE: str | None = None
|
|
33
|
+
# Metadata fields, keyed by their suffix (e.g. '$files' in 'my_table$files'), mapped to their column names and types
|
|
34
|
+
METADATA_FIELDS: dict[str, dict[str, str]] = {}
|
|
35
|
+
|
|
36
|
+
def __init_subclass__(cls, **kwargs):
|
|
37
|
+
"""Automatically registers subclasses when they are defined."""
|
|
38
|
+
super().__init_subclass__(**kwargs)
|
|
39
|
+
if cls.DIALECT:
|
|
40
|
+
DialectConfig._dialects[cls.DIALECT] = cls
|
|
41
|
+
|
|
42
|
+
def __new__(cls, dialect: str) -> DialectConfig:
|
|
43
|
+
"""Returns the config registered for the dialect name, or the base config if none is registered."""
|
|
44
|
+
if dialect in DIALECT_MODULES:
|
|
45
|
+
importlib.import_module(DIALECT_MODULES[dialect])
|
|
46
|
+
return super().__new__(DialectConfig._dialects.get(dialect, DialectConfig))
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def SYSTEM_COLUMNS(self) -> list[exp.ColumnDef]:
|
|
50
|
+
"""
|
|
51
|
+
Represents system columns.
|
|
52
|
+
"""
|
|
53
|
+
return []
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def SYSTEM_FUNCTIONS(self) -> dict[str, list[str]]:
|
|
57
|
+
"""
|
|
58
|
+
Represents system functions.
|
|
59
|
+
"""
|
|
60
|
+
return {}
|
|
61
|
+
|
|
62
|
+
def get_metadata_field(self, table_name: str) -> str | None:
|
|
63
|
+
"""
|
|
64
|
+
Returns the metadata field of a table name (e.g. '$files' for 'my_table$files').
|
|
65
|
+
"""
|
|
66
|
+
for suffix in self.METADATA_FIELDS:
|
|
67
|
+
if table_name.lower().endswith(suffix):
|
|
68
|
+
return suffix
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
def get_metadata_field_column_defs(self, suffix: str) -> list[exp.ColumnDef]:
|
|
72
|
+
"""
|
|
73
|
+
Returns the column definitions of the metadata table with the given field (e.g. '$files').
|
|
74
|
+
"""
|
|
75
|
+
return [
|
|
76
|
+
exp.ColumnDef(this=exp.to_identifier(name), kind=exp.DataType.build(kind, self.DIALECT))
|
|
77
|
+
for name, kind in self.METADATA_FIELDS[suffix].items()
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
class ParameterModeDialect(Dialect):
|
|
81
|
+
"""
|
|
82
|
+
The generic dialect, which also accepts IN, OUT, and INOUT parameter modes in procedure parameters.
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
class Parser(sqlglot.Parser):
|
|
86
|
+
_parse_function_parameter = with_parameter_modes(sqlglot.Parser._parse_function_parameter)
|
|
@@ -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(),))
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from sqlglot import TokenType
|
|
2
|
+
from sqlglot.dialects.mysql import MySQL
|
|
3
|
+
|
|
4
|
+
from sqlleaf.dialects.base import DialectConfig, with_parameter_modes
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MySQLConfig(DialectConfig):
|
|
8
|
+
DIALECT = "mysql"
|
|
9
|
+
DEFAULT_PROCEDURE_LANGUAGE = "SQL"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Monkeypatch sqlglot's MySQL parser so that MySQL procedure parameters also accept parameter modes
|
|
13
|
+
MySQL.Parser._parse_function_parameter = with_parameter_modes(MySQL.Parser._parse_function_parameter)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _parse_user_defined_function_expression(self):
|
|
17
|
+
"""
|
|
18
|
+
Parse a MySQL function body that starts with BEGIN as a block, so that every statement is kept inside the function
|
|
19
|
+
rather than only the first one.
|
|
20
|
+
"""
|
|
21
|
+
if self._prev.token_type == TokenType.BEGIN:
|
|
22
|
+
return self._parse_block()
|
|
23
|
+
return _parse_user_defined_function_expression_default(self)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
_parse_user_defined_function_expression_default = MySQL.Parser._parse_user_defined_function_expression
|
|
27
|
+
MySQL.Parser._parse_user_defined_function_expression = _parse_user_defined_function_expression
|
|
28
|
+
|
|
29
|
+
# Tokenize 'DECLARE' as a keyword so procedure variable declarations are parsed by sqlglot's declare parser rather than an exp.Column
|
|
30
|
+
MySQL.Tokenizer.KEYWORDS["DECLARE"] = TokenType.DECLARE
|
|
31
|
+
|
|
32
|
+
# MySQL supports 'SELECT ... INTO @var', so keep it as a SELECT rather than rewriting it to 'CREATE TABLE AS'
|
|
33
|
+
MySQL.Generator.SUPPORTS_SELECT_INTO = True
|
sqlleaf-0.0.5/sqlleaf/settings/postgres.py → sqlleaf-0.0.8/sqlleaf/dialects/postgres/config.py
RENAMED
|
@@ -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.
|
|
4
|
+
from sqlleaf.dialects.base import DialectConfig
|
|
7
5
|
|
|
8
6
|
|
|
9
|
-
class
|
|
7
|
+
class PostgresConfig(DialectConfig):
|
|
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",
|
|
@@ -19,19 +19,19 @@ class PostgresSettings(BaseDialectSettings):
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
@property
|
|
22
|
-
def
|
|
22
|
+
def SYSTEM_COLUMNS(self) -> list[exp.ColumnDef]:
|
|
23
23
|
return [
|
|
24
24
|
exp.ColumnDef(this=exp.to_identifier(name), kind=exp.DataType.build(kind, self.DIALECT))
|
|
25
25
|
for name, kind in self.PSEUDOCOLUMNS.items()
|
|
26
26
|
]
|
|
27
27
|
|
|
28
28
|
@property
|
|
29
|
-
def
|
|
29
|
+
def SYSTEM_FUNCTIONS(self) -> dict[str, list[str]]:
|
|
30
30
|
"""
|
|
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"],
|
|
@@ -48,5 +48,10 @@ class PostgresSettings(BaseDialectSettings):
|
|
|
48
48
|
|
|
49
49
|
|
|
50
50
|
# sqlglot is missing pseudocolumns for Postgres
|
|
51
|
-
postgres.Postgres.PSEUDOCOLUMNS = {c.upper() for c in
|
|
51
|
+
postgres.Postgres.PSEUDOCOLUMNS = {c.upper() for c in PostgresConfig.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
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from sqlglot.dialects.redshift import Redshift
|
|
2
|
+
|
|
3
|
+
from sqlleaf.dialects.base import DialectConfig
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class RedshiftConfig(DialectConfig):
|
|
7
|
+
DIALECT = "redshift"
|
|
8
|
+
DEFAULT_PROCEDURE_LANGUAGE = "PLPGSQL"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# Redshift supports 'SELECT ... INTO', so keep it as a SELECT rather than rewriting it to 'CREATE TABLE AS'
|
|
12
|
+
Redshift.Generator.SUPPORTS_SELECT_INTO = True
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import sys
|
|
3
|
+
from enum import StrEnum, auto
|
|
4
|
+
|
|
5
|
+
logger = logging.getLogger("sqlleaf")
|
|
6
|
+
|
|
7
|
+
class ErrorLevel(StrEnum):
|
|
8
|
+
ERROR = auto()
|
|
9
|
+
WARN = auto()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
ERROR_LEVEL = ErrorLevel.ERROR
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def raise_error(cls: type, message: str):
|
|
16
|
+
"""
|
|
17
|
+
Raise a custom exception based on the error level.
|
|
18
|
+
"""
|
|
19
|
+
if ERROR_LEVEL == ErrorLevel.ERROR:
|
|
20
|
+
raise cls(message).with_traceback(None)
|
|
21
|
+
elif ERROR_LEVEL == ErrorLevel.WARN:
|
|
22
|
+
logger.error(message)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def set_error_action(action: str) -> None:
|
|
26
|
+
"""
|
|
27
|
+
Set the error level for exceptions ('error' or 'warn').
|
|
28
|
+
"""
|
|
29
|
+
if action not in {level.value for level in ErrorLevel}:
|
|
30
|
+
raise ValueError("The error action must be 'error' or 'warn'.")
|
|
31
|
+
|
|
32
|
+
global ERROR_LEVEL
|
|
33
|
+
ERROR_LEVEL = ErrorLevel(action)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class SqlGlotException(Exception):
|
|
37
|
+
def __init__(self, message, table=""):
|
|
38
|
+
super().__init__(message)
|
|
39
|
+
|
|
40
|
+
self.message = message
|
|
41
|
+
|
|
42
|
+
def __str__(self):
|
|
43
|
+
return "%s" % (self.message,)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class SqlLeafException(Exception):
|
|
47
|
+
def __init__(self, message):
|
|
48
|
+
super().__init__(message)
|
|
49
|
+
|
|
50
|
+
self.message = message
|
|
51
|
+
|
|
52
|
+
def __str__(self):
|
|
53
|
+
return "%s" % (self.message,)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class InvalidQueryError(SqlLeafException):
|
|
57
|
+
pass
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class UnsupportedFeatureError(SqlLeafException):
|
|
61
|
+
pass
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class MappingError(SqlLeafException):
|
|
65
|
+
pass
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class GeneratorError(SqlLeafException):
|
|
69
|
+
pass
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class GraphError(SqlLeafException):
|
|
73
|
+
pass
|
|
74
|
+
|
|
75
|
+
PRINT_FULL_EXCEPTION = False
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def set_print_full_exception(enabled: bool) -> None:
|
|
79
|
+
"""
|
|
80
|
+
Set whether uncaught exceptions print the full traceback instead of only the error type and message.
|
|
81
|
+
"""
|
|
82
|
+
global PRINT_FULL_EXCEPTION
|
|
83
|
+
PRINT_FULL_EXCEPTION = enabled
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def suppress_exception_message(kind, message, traceback):
|
|
87
|
+
if not PRINT_FULL_EXCEPTION and SqlLeafException in kind.__bases__:
|
|
88
|
+
# Short form
|
|
89
|
+
print('{0}: {1}'.format(kind.__name__, message))
|
|
90
|
+
else:
|
|
91
|
+
# Regular form
|
|
92
|
+
sys.__excepthook__(kind, message, traceback)
|
|
93
|
+
|
|
94
|
+
sys.excepthook = suppress_exception_message
|