-
Notifications
You must be signed in to change notification settings - Fork 1
Expand PostgreSQL grammar and AST APIs; make IN-list construction linear #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
renecannao
wants to merge
11
commits into
main
Choose a base branch
from
feat/postgresql-coverage-and-ast-apis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eeee5ef
feat: expand PostgreSQL TABLE and VALUES query coverage
renecannao cf67898
perf: build IN-list AST children in linear time
renecannao 0b73e2c
feat: add PostgreSQL query extensions and structured batch parsing
renecannao fdb77d3
feat: expose owned AST transforms and context-aware parameterization
renecannao 363f641
test: refresh PostgreSQL compatibility baselines after grammar expansion
renecannao f414da5
docs: describe PostgreSQL analysis syntax and AST ownership contracts
renecannao b06aa50
feat: parse PostgreSQL casts, symbolic operators and named arguments
renecannao 24cd1f3
test: record PostgreSQL expression coverage gains and API contracts
renecannao 4f601d2
perf: reuse cached tokens when probing PostgreSQL typed literals
renecannao 06058a7
feat: expand PostgreSQL aggregate calls and CTE query composition
renecannao de06d72
test: refresh PostgreSQL coverage after aggregate and CTE expansion
renecannao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| # AST traversal, transformation, and parameterization | ||
|
|
||
| These header-only C++17 APIs use the existing 48-byte `AstNode` and `Arena`. | ||
| Include `sql_parser/ast_walk.h`, `sql_parser/ast_transform.h`, or | ||
| `sql_parser/parameterize.h` as needed. | ||
|
|
||
| ## Traverse one subtree | ||
|
|
||
| ```cpp | ||
| auto result = sql_parser::walk_ast(root, | ||
| [](const sql_parser::AstNode& node, | ||
| const sql_parser::AstVisitContext& context) { | ||
| // context.parent, context.depth, context.child_index | ||
| return sql_parser::AstVisitAction::Continue; | ||
| }); | ||
| ``` | ||
|
|
||
| Traversal is preorder, children in sibling order. `SkipChildren` skips only the | ||
| current node's descendants; `Stop` terminates the whole walk. The root's own | ||
| `next_sibling` is outside the traversal. A null root is a completed empty walk. | ||
| Do not change links during a walk. | ||
|
|
||
| The iterative implementation uses O(depth) auxiliary storage. `AstWalkLimits` | ||
| defaults to 1,000,000 visited nodes and a maximum depth of 4,096 (root depth zero). | ||
| `AstWalkResult` contains the visit count and `Completed`, `Stopped`, or | ||
| `LimitExceeded`. Bounds also prevent unbounded traversal of cyclic input. | ||
|
|
||
| ## Copy and replace | ||
|
|
||
| ```cpp | ||
| sql_parser::Arena destination; | ||
| auto copied = sql_parser::clone_ast(root, destination); | ||
| if (!copied.ok()) { /* inspect copied.error */ } | ||
| ``` | ||
|
|
||
| `clone_ast` copies one subtree and every value/source string into `destination`. | ||
| The input SQL and original arena may then be destroyed. The clone's root has no | ||
| sibling; internal child and sibling order and flags are retained. Cycles and | ||
| shared nodes are rejected with `InvalidTree`; exhaustion and traversal bounds | ||
| return explicit errors. Cloning null succeeds with a null result. | ||
|
|
||
| `make_owned_node(arena, type, value, flags, source)` is the owning counterpart of | ||
| `make_node`: it copies text and returns null on arena allocation failure. | ||
|
|
||
| `replace_ast_subtree(root, target, replacement)` changes the caller's root pointer | ||
| when necessary, preserving the target's sibling position. Pass null replacement | ||
| to remove the target. On success the old target is detached from its siblings. | ||
| The input root must be a standalone tree, with no root sibling. Replacement must | ||
| be a detached subtree with no root sibling and share no nodes with the input | ||
| tree. Validation happens before mutation; errors leave input links unchanged. | ||
| Nodes are never freed individually, and callers retain responsibility for the | ||
| lifetimes of every participating arena. | ||
|
|
||
| Failed operations can consume arena space; they do not reset or roll back an | ||
| arena that might contain other live nodes. Temporary STL allocation failures use | ||
| the usual C++ exception behavior. | ||
|
|
||
| ## Parameterize complete statements | ||
|
|
||
| ```cpp | ||
| sql_parser::Parser<sql_parser::Dialect::PostgreSQL> parser; | ||
| auto parsed = parser.parse(sql.data(), sql.size()); | ||
| sql_parser::Arena output; | ||
| auto rewritten = sql_parser::parameterize_ast<sql_parser::Dialect::PostgreSQL>( | ||
| parsed, output); | ||
| if (rewritten.ok()) { | ||
| sql_parser::Emitter<sql_parser::Dialect::PostgreSQL> emitter(output); | ||
| emitter.emit(rewritten.ast); | ||
| auto sql_with_binds = emitter.result(); | ||
| } | ||
| ``` | ||
|
|
||
| Prefer the `ParseResult` overload: it rejects non-OK results, unconsumed input, | ||
| and results without `full_input`. The raw `AstNode*` overload requires a complete, | ||
| valid AST in the specified dialect. Neither overload performs catalog lookup, | ||
| type inference, or a second SQL grammar validation. | ||
|
|
||
| The original tree is unchanged. The result contains a copied AST and an ordered | ||
| `parameters` vector of `ExtractedParameter { index, literal_type, value, source }`. | ||
| Indices are one-based. Values retain their lexical spelling, including any | ||
| escape sequences; they are not decoded strings or converted numeric values. | ||
| Source retains the original literal spelling when recorded by the parser, and | ||
| may be empty for manually built nodes or keywords without source metadata. | ||
| Mapping text and AST nodes live in the output arena; the result owns its vectors. | ||
| Repeated literals produce separate parameters. | ||
|
|
||
| PostgreSQL preserves existing `$n` spellings and indices and generates new | ||
| indices after the largest existing index. For `SELECT $3, 7, $1, 'a'`, the result | ||
| is `SELECT $3, $4, $1, $5`, with new mapping indices 4 and 5 and | ||
| `existing_parameters` equal to `[3, 1]`. Invalid indices and signed 32-bit index | ||
| overflow fail explicitly. | ||
|
|
||
| MySQL emits `?` and counts positions in the resulting SQL. For | ||
| `SELECT ?, 7, ?, 'a'`, new mapping indices are 2 and 4 and | ||
| `existing_parameters` is `[1, 3]`. The latter identifies the positions where | ||
| callers should interleave their original bindings. MySQL comma LIMIT syntax is | ||
| normalized from `LIMIT offset, count` to `LIMIT count OFFSET offset`, so extracted | ||
| literals follow the normalized emission order. If that comma LIMIT contains an | ||
| existing `?`, parameterization returns `UnsupportedContext` rather than silently | ||
| reordering the caller's original anonymous bindings. Existing binds elsewhere in | ||
| the statement and `LIMIT count OFFSET offset` remain supported. | ||
|
|
||
| Supported roots are SELECT, INSERT, UPDATE, DELETE, compound queries, and TABLE | ||
| queries. Bindable literals include integers, floats, strings, hex/bit literals, | ||
| and standalone TRUE/FALSE. NULL remains literal. Direct integer ordinals in | ||
| query GROUP BY, ORDER BY, and DISTINCT ON remain unchanged, including | ||
| parenthesized integers. Integers inside larger expressions and window ORDER BY | ||
| are values. `IS [NOT] TRUE/FALSE` keywords and PostgreSQL binary `::` type | ||
| subtrees remain unchanged. The precision argument of CURRENT_TIMESTAMP, | ||
| CURRENT_TIME, LOCALTIME, and LOCALTIMESTAMP remains a syntax constant. MySQL also | ||
| preserves precision arguments of NOW, CURTIME, SYSDATE, UTC_TIME, and | ||
| UTC_TIMESTAMP; these ordinary function names retain bindable arguments in | ||
| PostgreSQL. Quoted function names retain bindable arguments in both dialects. | ||
| Aggregate FILTER predicates and window frame offsets are bindable. PostgreSQL | ||
| casts and type-prefixed string literals now produce `NODE_TYPE_CAST` with an | ||
| expression child and a `NODE_TYPE_NAME` leaf. Cast values are bindable; type | ||
| modifiers and array bounds remain intact. Named function argument values are | ||
| bindable while their names remain intact. Manually constructed binary `::` | ||
| ASTs retain their existing support. | ||
|
|
||
| Unsupported roots and contexts return `UnsupportedRoot` or `UnsupportedContext` | ||
| with no AST or partial mapping. These include SET/transaction/COPY/DDL/other | ||
| utility statements, SELECT INTO, CTEs, generic function-shaped CAST nodes, | ||
| opaque subquery/interval fragments, and unknown future node types. PostgreSQL | ||
| string-shaped aliases are rejected. Supported type-prefixed literals such as | ||
| `SELECT TIMESTAMP '2020-01-01'` now parameterize as casts instead of being | ||
| misparsed as aliases. These | ||
| restrictions reflect contexts where replacing literals or the current emitter | ||
| would not produce reliable executable SQL. Parameterization does not change the | ||
| existing digest API. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Update the architecture diagram.
Line 329 states that
AstNodeis 48 bytes. The diagram at README.md line 267 still states 32 B nodes. Change the diagram to 48 B so the layout contract is consistent.🤖 Prompt for AI Agents