PostgreSQL Languages AST and statements prettifier
Project description
- Contact:
- lele@metapensiero.it
- License:
- Status:
- Version:
- 3
This is a Python 3 module that exposes the parse tree of a PostgreSQL statement (extracted by the almost standard PG parser repackaged as a standalone static library by libpg_query) as set of interconnected nodes, usually called an abstract syntax tree.
See a more detailed introduction in the documentation.
Installation
As usual, the easiest way is with pip:
$ pip install pglast
Alternatively you can clone the repository:
$ git clone https://github.com/lelit/pglast.git --recursive
and install from there:
$ pip install ./pglast
Development
There is a set of makefiles implementing the most common operations, a make help will show a brief table of contents. A comprehensive test suite, based on pytest, covers nearly 99% of the source lines.
Documentation
Latest documentation is hosted by Read the Docs at http://pglast.readthedocs.io/
Changes
Version 3
3.0.dev2 (2021-05-22)
Fix bug in CreateStmt printer (issue #79)
Make it possible to pass also concrete ast.Nodes to RawStream`
Breaking change
To reduce confusion, the printer module has been removed: print-specific stuff is now directly exposed by the printers subpackage while serialization classes are now in the new stream module
The default value for the safety_belt option of the printify() function is now False
3.0.dev1 (2021-05-16)
Fix AT_SetIdentity, AT_EnableReplicaTrig and AlterSubscriptionStmt printers
Improve AlterTSConfigType and IntoClause printers
New generic “visitor pattern” (issue #51) exemplified by a new referenced_relations() function (issue #66)
Refine printing of SQL comments
Implement AlterExtensionStmt printer
3.0.dev0 (2021-05-03)
Expose the new pg_query_scan() function as parser.scan()
Expose the pg_query_parse() function as parser.parse_sql_json()
Expose the new pg_query_parse_protobuf() function as parser.parse_sql_protobuf()
Expose the new pg_query_deparse_protobuf() function as parser.deparse_protobuf()
Honor the catalogname of a RangeVar if present (issue #71)
Cover almost all SQL statements, testing against the whole PostgreSQL regression suite (issue #68, PR #72 and PR #77), thanks to Ronan Dunklau and Hong Cheng
New rudimentary support for the preserve comments feature (issue #23)
Breaking changes
Target PostgreSQL 13
The pglast.parser module exposes all libpg_query entry points, even the new pg_query_deparse_protobuf() function that is basically equivalent to RawStream-based printer
The split() function is now based on the lower level pg_query_split_with_xxx() functions
The parse_sql() function returns native Python objects, not a JSON string as before: all PG nodes are now represented by subclasses of pglast.ast.Node, without exception, even Expr and Value are there. The latter impacts on pglast.node.Scalar: for example it now may contains a ast.Integer instance instead of a Python int
The pgpp --parse-tree output is a pprint represention of the AST, not a JSON string as before
The ParseError exception does not expose the location as an instance member anymore, although its still there, as the second argument (ie .args[1]); furthermore, its value now corresponds to the index in the original Unicode string, instead of the offset in the UTF-8 representation passed to the underlying C function
Version 2
2.0.dev3 (2021-02-20)
Handle INCLUDE clause in IndexStmt (PR #67), thanks to Ronan Dunklau
2.0.dev2 (2020-10-24)
Merge new fingerprint functionality from v1 (i.e. master) branch
2.0.dev1 (2020-09-26)
Require Python 3.6 or greater
Handle ALTER TYPE .. RENAME VALUE in AlterEnumStmt (PR #52), thanks to Ronan Dunklau
Add support for Create / Alter / Drop PROCEDURE (PR #48), thanks to Ronan Dunklau
Use Ronan’s fork of libpg_query, targeting PostgreSQL 12.1 (PR #36)
Change get_postgresql_version() to return a (major, minor) tuple (issue #38)
Handle ALTER TABLE ... ALTER COLUMN ... SET STORAGE ...
Handle PG12 materialized CTEs (issue #57)
Support column numbers in ALTER INDEX (PR #58), thanks to Ronan Dunklau
Handle SET LOGGED and SET UNLOGGED in ALTER TABLE (PR #59), thanks to Ronan Dunklau
Handle ALTER TYPE ... RENAME (PR #62), , thanks to Ronan Dunklau
Version 1
1.17 (2021-02-20)
Fix the generic case in the RenameStmt printer
1.16 (2021-02-20)
Promote to the stable state
Move the job of building and uploading binary wheels from TravisCI to GitHub Actions
1.15 (2021-02-19)
Fix IF EXISTS variant of RenameStmt printer (PR #70), thanks to Jonathan Mortensen
Update libpg_query to 10-1.0.5
1.14 (2020-10-24)
Produce Python 3.9 wheels, thanks to cibuildwheel 1.6.3
Expose the libpg_query’s fingerprint functionality (PR #64), thanks to Yiming Wang
1.13 (2020-09-26)
Handle SELECT FROM foo
1.12 (2020-06-08)
1.11 (2020-05-08)
1.10 (2020-01-25)
Fix collation name printer (PR #44), thanks to Ronan Dunklau
Implement CreatePLangStmt printer (PR #42), thanks to Bennie Swart
Fix privileges printer (PR #41), thanks to Bennie Swart
Handle TRUNCATE event in CreateTrigStmt printer (PR #40), thanks to Bennie Swart
Fix function body dollar quoting (PR #39), thanks to Bennie Swart
1.9 (2019-12-20)
Prettier INSERT representation
1.8 (2019-12-07)
Prettier CASE representation
New option to emit a semicolon after the last statement (issue #24)
1.7 (2019-12-01)
Implement NotifyStmt printer
Implement RuleStmt printer, thanks to Gavin M. Roy for his PR #28
Fix RenameStmt, properly handling object name
Produce Python 3.8 wheels, thanks to cibuildwheel 1.0.0
Support ALTER TABLE RENAME CONSTRAINT (PR #35), thanks to Ronan Dunklau
1.6 (2019-09-04)
1.5 (2019-06-04)
1.4 (2019-04-06)
1.3 (2019-03-28)
1.2 (2019-02-13)
1.1 (2018-07-20)
No visible changes, but now PyPI carries binary wheels for Python 3.7.
1.0 (2018-06-16)
0.28 (2018-06-06)
Update libpg_query to 10-1.0.2
Support the ‘?’-style parameter placeholder variant allowed by libpg_query (details)
0.27 (2018-04-15)
Prettier JOINs representation, aligning them with the starting relation
0.26 (2018-04-03)
Fix cosmetic issue with ANY() and ALL()
0.25 (2018-03-31)
Fix issue in the safety belt check performed by pgpp (issue #4)
0.24 (2018-03-02)
Implement Null printer
0.23 (2017-12-28)
Implement some other DDL statements printers
New alternative style to print comma-separated-values lists, activated by a new --comma-at-eoln option on pgpp
0.22 (2017-12-03)
Implement TransactionStmt and almost all DROP xxx printers
0.21 (2017-11-22)
Implement NamedArgExpr printer
New alternative printers for a set of special functions, activated by a new --special-functions option on pgpp (issue #2)
0.20 (2017-11-21)
Handle special de-reference (A_Indirection) cases
0.19 (2017-11-16)
Fix serialization of column labels containing double quotes
Fix corner issues surfaced implementing some more DDL statement printers
0.18 (2017-11-14)
Fix endless loop due to sloppy conversion of command line option
Install the command line tool as pgpp
0.17 (2017-11-12)
Rename printers.sql to printers.dml (backward incompatibility)
List printer functions in the documentation, referencing the definition of related node type
Fix inconsistent spacing in JOIN condition inside a nested expression
Fix representation of unbound arrays
Fix representation of interval data type
Initial support for DDL statements
Fix representation of string literals containing single quotes
0.16 (2017-10-31)
Update libpg_query to 10-1.0.0
0.15 (2017-10-12)
Fix indentation of boolean expressions in SELECT’s targets (issue #3)
0.14 (2017-10-09)
Update to latest libpg_query’s 10-latest branch, targeting PostgreSQL 10.0 final
0.13 (2017-09-17)
Fix representation of subselects requiring surrounding parens
0.12 (2017-08-22)
New option --version on the command line tool
Better enums documentation
Release the GIL while calling libpg_query functions
0.11 (2017-08-11)
Nicer indentation for JOINs, making OUTER JOINs stand out
Minor tweaks to lists rendering, with less spurious whitespaces
New option --no-location on the command line tool
0.10 (2017-08-11)
Support Python 3.4 and Python 3.5 as well as Python 3.6
0.9 (2017-08-10)
Fix spacing before the $ character
Handle type modifiers
New option --plpgsql on the command line tool, just for fun
0.8 (2017-08-10)
Add enums subpackages to the documentation with references to their related headers
New compact_lists_margin option to produce a more compact representation when possible (see issue #1)
0.7 (2017-08-10)
Fix sdist including the Sphinx documentation
0.6 (2017-08-10)
New option --parse-tree on the command line tool to show just the parse tree
Sphinx documentation, available online
0.5 (2017-08-09)
Handle some more cases when a name must be double-quoted
Complete the serialization of the WindowDef node, handling its frame options
0.4 (2017-08-09)
Expose the actual PostgreSQL version the underlying libpg_query libray is built on thru a new get_postgresql_version() function
New option safety_belt for the prettify() function, to protect the innocents
Handle serialization of CoalesceExpr and MinMaxExpr
0.3 (2017-08-07)
Handle serialization of ParamRef nodes
Expose a prettify() helper function
0.2 (2017-08-07)
Test coverage at 99%
First attempt at automatic wheel upload to PyPI, let’s see…
0.1 (2017-08-07)
First release (“Hi daddy!”, as my soul would tag it)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pglast-3.0.dev2-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1aa312e9674dccbca4fa792f610818e72eb27ae57d6b99102643b69559e3906d |
|
MD5 | 4c1d1d8a6d6c87b30c94d1a9cdae2f25 |
|
BLAKE2b-256 | 44081e190126e8e6c19bb16c176939a7ab5ebba2985f283a1eebb3aa7ddabd62 |
Hashes for pglast-3.0.dev2-cp39-cp39-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e1b83e9a5593e41e0025dd74aed3bbb5f8a91765aab19b3305caf492679c664 |
|
MD5 | 94e206a3d6639c4a3cfa4979ae62b45c |
|
BLAKE2b-256 | 39a669120850ff98e609161b72f9c61aa4f6de687475b8ff377f8f233fb4d5fd |
Hashes for pglast-3.0.dev2-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8416afc7930f65c1373689ca662c6ec8190daae6d8a5d92ac27de14f631a63b7 |
|
MD5 | b837f173621885330b056a3aef59a55a |
|
BLAKE2b-256 | 7aea642c09fa1f469c2b3263259f7fc444278cbeccd1e5f3fabbc5c9dfea32a2 |
Hashes for pglast-3.0.dev2-cp39-cp39-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2881d21b7c9bbe3cbe49e914d57ce56c1f2e35d2e5deb4a96e6f3e990f6e54fe |
|
MD5 | ddc61ff2ac7fee6eb29bd0caf698b1b5 |
|
BLAKE2b-256 | 38ea8e3052bbace99114248dd2990ce4b5f2a580e7bdfbea6639374f8d04b752 |
Hashes for pglast-3.0.dev2-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6e59a8af72c0e976dd25b0dea76053045c42ce2807c65df50dd0dd4c6371b30 |
|
MD5 | 2eabbc30bf79b75d0bddfe3a8a473296 |
|
BLAKE2b-256 | bdba9d7f2b1715b45654eb4f3e32bcbedd1ebc0b7a756c1334701a0d6c6b8e5a |
Hashes for pglast-3.0.dev2-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f4eb7b62a9dcd7dff94cbd35b34bdd899abc8a06b0b3ab95afbbb7fcd8afe82 |
|
MD5 | a17dd00ac10b3de092acba2d69eda493 |
|
BLAKE2b-256 | bd789dcec3cceec9c35d2f85fff0bf67d21a1a45b50b788f632fe3614e8056c1 |
Hashes for pglast-3.0.dev2-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30fb493c5c15e6d253347b04261d821130170e599243fe5ba8e2e2f1c88eb16c |
|
MD5 | 9e00ce143524af78e338f44b60e48e1b |
|
BLAKE2b-256 | 75a37673399cd32705c4a9fd360e5ac334f75eae4f20dbc09d316ab7b2c7e3b6 |
Hashes for pglast-3.0.dev2-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db4a0663e826e39b1d3ea92698fe56e9ccde76a1d83bf27b39d7bb878c95c622 |
|
MD5 | a3c7ffdb7a05b350e05dd046ac289643 |
|
BLAKE2b-256 | 128440238ee57223f54330ad61d805afa1ebae5047abdd2ca99a08a5fa67ab5e |
Hashes for pglast-3.0.dev2-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e643fd31d7c12808cbdf4e910f74ead19634aef0fd74031daf703b3deab7f0df |
|
MD5 | 2ceb32783b64c4b9b0bbbcba3da2bfbd |
|
BLAKE2b-256 | 392bc730e2fb1177c5d6dc8e48cef49c046c5856e339435db029763da8cc2ed5 |
Hashes for pglast-3.0.dev2-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62974e39c1351f122bd19d575ad50fe4a8c956d208a59b5495f4869e70f66c17 |
|
MD5 | 802a1469b2382e2968fc3eecb0294aec |
|
BLAKE2b-256 | ef02791b242d590176bc52e0201329d8176eec810bff861324f791a5a8a3ac70 |
Hashes for pglast-3.0.dev2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 544ebb56238656d4852f50c239423af856e27d3e6915c0e97386941c522ec5f1 |
|
MD5 | 51f87c8643d5280c465f874726f814c6 |
|
BLAKE2b-256 | 7b992425c47c6117712794ed54ab83cc7d60b2e019b97b321d5837f29a27502a |
Hashes for pglast-3.0.dev2-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a34aa8e592b42f566e245567412538d889d0afc6a84a823abf2599d2c7d2cb14 |
|
MD5 | 9e592e3c43539d93242dd8119f866abe |
|
BLAKE2b-256 | 6d818161fe04b77d3e61bc6c857b53ee88980168918ac9b67bf1c5e890d3735f |
Hashes for pglast-3.0.dev2-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a60654f00af23e4386761b6cf8888f5cd8da490bb3e2e4e080427f12b7eb3a5b |
|
MD5 | d14f2e171fa439cce87658e921d00b73 |
|
BLAKE2b-256 | a55c8e7e558176f509916b7ed0c6b2c0444e5fff57c451cc61ec6a29eb24e55b |
Hashes for pglast-3.0.dev2-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ee807730c5be9fd5cb1c1ffe965bb6a92ea1b7d6ca7cc318bfe179e97ab91a2 |
|
MD5 | d0521dd52481ca944e7fcd31ca56c099 |
|
BLAKE2b-256 | 488455f7de1d72e8965617a27df243aa718af1511122da025596436bce7ef7e1 |
Hashes for pglast-3.0.dev2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb0122afc7ea02014c454649cc93bdaf8bd57366a98a71eedbe2862b87c902ae |
|
MD5 | 61120838fab849869e945db2bf40dc84 |
|
BLAKE2b-256 | 946351d5438e6f76ee0fdde5de45a1f536d53fff31cd58285be56f09bab8adab |
Hashes for pglast-3.0.dev2-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ba5495746615f94fab784f8b36f9b6d4f0da78e861ba00dabdf89e99ebb9228 |
|
MD5 | 58b9e6b02b1c975ad9672e66bf38b96b |
|
BLAKE2b-256 | ab92c144aaca8e14d1b80b16f8386b20652b2eeb845c3a86258568e47a4ac709 |