pg

package
v0.0.0-...-3108594 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 6 Imported by: 0

README

pg -- PostgreSQL Parser

Recursive descent parser for PostgreSQL 17, producing a full AST with position tracking.

Public API

import "github.com/bytebase/omni/pg"

stmts, err := pg.Parse(sql)
Parse(sql string) ([]Statement, error)

Splits and parses a SQL string into individual statements.

Statement
type Statement struct {
    Text      string       // SQL text including trailing semicolon
    AST       ast.Node     // Inner statement node (e.g. *ast.SelectStmt)
    ByteStart int          // Inclusive start byte offset
    ByteEnd   int          // Exclusive end byte offset
    Start     Position     // Start line:column (1-based)
    End       Position     // End line:column (1-based)
}

Packages

Package Description
pg/ast 210+ AST node types matching PostgreSQL internals
pg/parser Recursive descent parser (~29,000 lines, 39 files)
pg/catalog In-memory catalog simulation, DDL semantic analysis, type resolution
pg/analysis Bytebase's query analysis: query span (column lineage, predicate columns, accessed tables), classification (read-only validation, statement types), changed resources, statement ranges, the result limit rewrite, EXPLAIN and its affected-rows estimate, editor diagnose, prior backup and restore SQL, and the system object lists
pg/parsertest 746 test cases organized by SQL feature
pg/pgregress PostgreSQL official regression test compatibility

Documentation

Index

Constants

View Source
const (
	RangeReferenceRelation  = parser.RangeReferenceRelation
	RangeReferenceSubquery  = parser.RangeReferenceSubquery
	RangeReferenceFunction  = parser.RangeReferenceFunction
	RangeReferenceJoinAlias = parser.RangeReferenceJoinAlias
	RangeReferenceCTE       = parser.RangeReferenceCTE
)

Variables

This section is empty.

Functions

func StripTopLevelMetacommands

func StripTopLevelMetacommands(text string) string

StripTopLevelMetacommands returns text with psql metacommand lines removed, but only where a backslash sits in top-level scan state — not inside a string, comment, dollar-quote, or COPY data. It walks with the same construct-skipping logic as Split, so a backslash that is really string content (e.g. 'a \restrict b') is preserved. Used to derive the SQL a segment would actually send to the server, since the database has no notion of psql backslash commands.

Types

type CompletionContext

type CompletionContext = parser.CompletionContext

CompletionContext is the parser-native context for SQL completion.

func CollectCompletion

func CollectCompletion(sql string, cursorOffset int) *CompletionContext

CollectCompletion returns completion candidates plus a best-effort visible relation scope at cursorOffset. Ordinary Parse remains strict.

type Position

type Position struct {
	// Line is 1-based line number.
	Line int
	// Column is 1-based column in code points.
	Column int
}

Position represents a location in source text.

type RangeReference

type RangeReference = parser.RangeReference

RangeReference is a syntax-level FROM/JOIN reference.

type RangeReferenceKind

type RangeReferenceKind = parser.RangeReferenceKind

RangeReferenceKind classifies a range-table entry visible to completion.

type ScopeSnapshot

type ScopeSnapshot = parser.ScopeSnapshot

ScopeSnapshot describes the SELECT-level relation scope visible at a cursor.

type Segment

type Segment struct {
	Text      string // the raw text of this segment
	ByteStart int    // byte offset of start in original sql
	ByteEnd   int    // byte offset of end (exclusive) in original sql
}

Segment represents a portion of SQL text delimited by top-level semicolons.

func Split

func Split(sql string) []Segment

Split splits SQL text into segments at top-level semicolons. It is a pure lexical scanner that does not parse SQL, so it works on both valid and invalid SQL. Each returned segment includes the terminating semicolon (if present). Segments are returned with their byte offsets in the original string.

func (Segment) Empty

func (s Segment) Empty() bool

Empty returns true if the segment contains only whitespace, comments, semicolons, and psql metacommand lines (which are not SQL statements).

type Statement

type Statement struct {
	// Text is the SQL text including trailing semicolon if present.
	Text string
	// AST is the inner statement node (e.g. *ast.SelectStmt). Nil for empty statements.
	AST ast.Node

	// ByteStart is the inclusive start byte offset in the original SQL.
	ByteStart int
	// ByteEnd is the exclusive end byte offset in the original SQL.
	ByteEnd int

	// Start is the start position (line:column) in the original SQL.
	Start Position
	// End is the exclusive end position (line:column) in the original SQL.
	End Position
}

Statement is the result of parsing a single SQL statement.

func Parse

func Parse(sql string) ([]Statement, error)

Parse splits and parses a SQL string into statements. Each statement includes the text, AST, and byte/line positions. Text boundaries are derived from RawStmt.Loc: each statement's text spans from the end of the previous statement to just past the semicolon (or end of input) following the current statement.

func (*Statement) Empty

func (s *Statement) Empty() bool

Empty returns true if this statement has no meaningful content.

Directories

Path Synopsis
Package analysis holds Bytebase's query analysis for PostgreSQL: query span (column lineage, predicate columns and the tables a statement reads) and the system object lists it relies on.
Package analysis holds Bytebase's query analysis for PostgreSQL: query span (column lineage, predicate columns and the tables a statement reads) and the system object lists it relies on.
ast
Package nodes defines PostgreSQL parse tree node types.
Package nodes defines PostgreSQL parse tree node types.
cmd/genwalker command
Command genwalker generates walk_generated.go from parsenodes.go.
Command genwalker generates walk_generated.go from parsenodes.go.
Package completion provides parser-native C3-style SQL completion for PostgreSQL.
Package completion provides parser-native C3-style SQL completion for PostgreSQL.
Package driver is PostgreSQL's implementation of the driver contract in github.com/bytebase/omni/driver: it connects through pgx, syncs a server's metadata into omni/metadata, dumps it as DDL, and runs interactive queries and change scripts.
Package driver is PostgreSQL's implementation of the driver contract in github.com/bytebase/omni/driver: it connects through pgx, syncs a server's metadata into omni/metadata, dumps it as DDL, and runs interactive queries and change scripts.
internal
copyscan
Package copyscan locates the inline data block that follows a COPY ...
Package copyscan locates the inline data block that follows a COPY ...
metacmd
Package metacmd recognizes psql metacommand lines in SQL scripts.
Package metacmd recognizes psql metacommand lines in SQL scripts.
testcontainer
Package testcontainer is how pg's tests reach PostgreSQL: through a tape (enginetest/tape) that replays the pinned server's answers, or, in a recording (OMNI_RECORD=1), through the pinned server itself.
Package testcontainer is how pg's tests reach PostgreSQL: through a tape (enginetest/tape) that replays the pinned server's answers, or, in a recording (OMNI_RECORD=1), through the pinned server itself.
Package parser implements a PostgreSQL-compatible SQL lexer and parser.
Package parser implements a PostgreSQL-compatible SQL lexer and parser.
Package pgregress provides tools for running PostgreSQL regression test SQL files against the pgparser parser to verify parse compatibility.
Package pgregress provides tools for running PostgreSQL regression test SQL files against the pgparser parser to verify parse compatibility.
plpgsql
ast
Package ast defines PL/pgSQL AST node types.
Package ast defines PL/pgSQL AST node types.
parser
Package parser implements a recursive descent PL/pgSQL parser.
Package parser implements a recursive descent PL/pgSQL parser.
Package review implements Bytebase SQL Review V2 for PostgreSQL: the engine side of the contract in github.com/bytebase/omni/review.
Package review implements Bytebase SQL Review V2 for PostgreSQL: the engine side of the contract in github.com/bytebase/omni/review.
Package splittest is the PG splitter test framework: enumeration fences, a constructive generator whose cases carry their own expected boundaries, and invariant checks that need no ground truth at all.
Package splittest is the PG splitter test framework: enumeration fences, a constructive generator whose cases carry their own expected boundaries, and invariant checks that need no ground truth at all.
Package v1review holds the SQL Review V1 judgments ported from Bytebase's advisor for PostgreSQL.
Package v1review holds the SQL Review V1 judgments ported from Bytebase's advisor for PostgreSQL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL