Repository navigation
2.1: classification, references, node builders, rewrites, typed visitors, Format - #25
Merged
Merged
Conversation
…endering; bump to 2.2.0 - Parser.Classify and StatementInfo: kind, read-only, and facts such as a data-modifying CTE, SELECT INTO, a locking clause, EXPLAIN ANALYZE, COPY PROGRAM and the wrapped statement - GetReferences: tables with role and CTE detection, functions, and columns matched to their table where the query alone settles it - Parser.Format: pretty print a query or script, keeping comments, one statement per paragraph - Error.Format and Error.GetLineAndColumn: psql-style error rendering
…g under 2.1.0 - Ast builds nodes that match what the parser produces: columns, constants, operators, AND/OR/NOT, IN, calls, casts, tables, select list and ORDER BY items, and Ast.Expression from SQL text - QueryRewrites: AddWhere, SetLimit, CapLimit, SetOffset, ToCount, QualifyTables, RenameSchema, RenameTable, ToDropStatement, EnsureIfNotExists, EnsureOrReplace - NodeVisitor: a handler per node type, several visitors in one walk, each skipping or stopping on its own - EqualsIgnoringLocations now also ignores the start and end positions of parenthesized lists, which made IN lists compare unequal - Version back to 2.1.0, since 2.1.0 was never published; one What's new in 2.1 section in the README
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
More general-purpose capabilities for others to build on. All additive. Everything stays under 2.1.0, since 2.1.0 was merged but never published; the README has a single "What's new in 2.1" section.
Understanding a statement
Statement classification.
Parser.Classify(query), alsotree.Classify()andrawStmt.Classify(), returns aStatementInfoper statement.Kind: Select, Insert, Update, Delete, Merge, Truncate, Copy, Ddl, Transaction, Session, Cursor, Prepared, Explain, Call, Maintenance, Lock, Other. All 117 statement node types are mapped.IsUtility,HasDataModifyingCte,HasSelectInto,HasLockingClause,ExecutesInner(EXPLAIN ANALYZE),RunsProgram(COPY PROGRAM), andInner, the statement wrapped by EXPLAIN, PREPARE, DECLARE CURSOR, CREATE TABLE AS or COPY (query).IsReadOnly, derived from those facts and erring on the side of false.These are facts, not a verdict: a function called from a SELECT can still write, which no parser can see.
Resolved references.
GetReferences(), from any node, returns tables (with roleRead,WriteorDdl, alias, CTE detection), functions, and columns matched to their table when the query alone settles it. Unsettled columns are left unmatched, not guessed.Changing a statement
Node builders.
Ast.Column,Star,Const,Null,Param,Op,Eqand friends,And,Or,Not,IsNull,In,Call,CountStar,Cast,Table,Target,OrderBy,Select, andAst.Expression(sql). Every builder is tested to produce a tree structurally equal to what the parser produces for the same SQL (41 cases).Common rewrites (
QueryRewrites, on the tree, in place):AddWhere: for SELECT, UPDATE, DELETE; combines with an existing WHERE using AND. ThrowsNotSupportedExceptionwith a clear message for a UNION, a VALUES list or other statements.SetLimit,CapLimit,SetOffset.CapLimitadds a limit, lowers a larger constant one, and wraps any other expression inLEAST(..., max).ToCount: a newSELECT count(*) FROM (...) q, leaving the original untouched.QualifyTables,RenameSchema,RenameTable: CTE names are left alone.ToDropStatement: the DROP that undoes a CREATE, for 16 object kinds, withifExistsandcascade.EnsureIfNotExists,EnsureOrReplace.Typed visitors.
NodeVisitorholds a handler per node type;tree.Walk(visitorA, visitorB, ...)runs several in one pass, and each skips a subtree or stops for itself only.Working with text
Parser.Format: pretty prints a query or script and keeps its comments, one statement per paragraph.FormatOptionsfor indent, line length, comma placement, trailing newline and whether to keep comments.Error.FormatandError.GetLineAndColumn: psql-style error rendering with a caret.A fix to 2.1's comparison
EqualsIgnoringLocationsdid not ignore the start and end positions of parenthesized lists, soa IN (1, 2)anda IN ( 1,2 )compared unequal. Found while testing the builders against the parser; fixed, with tests.Verification
AnalysisTests,AstTestsandRewriteAndVisitorTests; 387 pass on .NET 8, 9 and 10 locally.IsUtilityis checked against libpg_query's own answer for every classified statement in the tests; everyAddWhereandCapLimitresult is checked to be a tree the parser itself builds.Limits
RenameSchemachanges table references only, not schema-qualified functions or types.ToDropStatementneeds a named index, and for functions uses the argument types as written.Deparse:Formatputs each statement in its own paragraph.Deparseis unchanged.