MCPcopy Index your code
hub / github.com/auxten/postgresql-parser

github.com/auxten/postgresql-parser @v1.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0.1 ↗ · + Follow
4,437 symbols 10,544 edges 199 files 3,040 documented · 69% 1 cross-repo links updated 39d agov1.0.1 · 2022-08-03★ 3136 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

What's this

PostgreSQL style Parser splitted from CockroachDB

See: Complex SQL format example

I tried to import github.com/cockroachdb/cockroach/pkg/sql/parser, but the dependencies is too complex to make it work.

To make things easy, I did these things:

  1. Copy all the pkg/sql/parser, pkg/sql/lex and simplify the dependencies
  2. Simplify the Makefile to just generate the goyacc stuff
  3. Add the goyacc generated files in parser and lex to make go get work easily, see the .gitignore files
  4. Trim the etcd dependency, see the go.mod
  5. Rename all test file except some pkg/sql/parser tests
  6. Add all necessary imports to vendor
  7. Remove the panic of meeting unregistried functions, see the WrapFunction
  8. Other nasty things make the parser just work that I forgot :p

Features

  • Pure golang implementation
  • Almost full support of PostgreSQL (cockroachdb style PostgreSQL)

SQL Standard Compliance

The code is derived from CockroachDB v20.1.11 which supports most of the major features of SQL:2011. See:

  • https://www.cockroachlabs.com/docs/v20.1/postgresql-compatibility

  • https://www.postgresql.org/docs/9.5/features.html

How to use

package main

import (
    "log"

    "github.com/auxten/postgresql-parser/pkg/sql/parser"
    "github.com/auxten/postgresql-parser/pkg/walk"
)

func main() {
    sql := `select marr
            from (select marr_stat_cd AS marr, label AS l
                  from root_loan_mock_v4
                  order by root_loan_mock_v4.age desc, l desc
                  limit 5) as v4
            LIMIT 1;`
    w := &walk.AstWalker{
        Fn: func(ctx interface{}, node interface{}) (stop bool) {
            log.Printf("node type %T", node)
            return false
        },
    }

    stmts, err := parser.Parse(sql)
    if err != nil {
        return
    }

    _, _ = w.Walk(stmts, nil)
    return
}

SQL parser

This project contains code that is automatically generated using goyacc. goyacc reads the SQL expressions (sql.y) and generates a parser which could be used to tokenize a given input. You could update the generated code using the generate target inside the project's Makefile.

$ make generate

Progress

  • 2021-02-16 github.com/auxten/postgresql-parser/pkg/sql/parser Unit tests works now!
  • 2021-03-08 Add walker package.

Todo

  • Fix more unit tests
  • Make built-in function parsing work

Extension points exported contracts — how you extend this code

Datum (Interface)
Datum represents a SQL value. [17 implementers]
pkg/sql/sem/tree/datum.go
Message (Interface)
Message extends the proto.Message interface with the MarshalTo and Size methods we tell gogoproto to generate for us. [9 …
pkg/util/protoutil/marshal.go
AlterTableCmd (Interface)
AlterTableCmd represents a table modification operation. [18 implementers]
pkg/sql/sem/tree/alter_table.go
Doc (Interface)
Doc represents a document as described by the type "DOC" in the referenced paper. This is the abstract representation co [10 …
pkg/util/pretty/document.go
Expr (Interface)
Expr represents an expression. [53 implementers]
pkg/sql/sem/tree/expr.go
JSON (Interface)
JSON represents a JSON value. [4 implementers]
pkg/util/json/json.go
TypedExpr (Interface)
TypedExpr represents a well-typed expression. [26 implementers]
pkg/sql/sem/tree/expr.go
Generator (Interface)
Generator provides an interface for generating UUIDs. [1 implementers]
pkg/util/uuid/generator.go

Core symbols most depended-on inside this repo

FormatNode
called by 406
pkg/sql/sem/tree/format.go
expr
called by 366
pkg/sql/parser/sql.go
helpWith
called by 202
pkg/sql/parser/help.go
AsString
called by 196
pkg/sql/sem/tree/format.go
Doc
called by 188
pkg/sql/sem/tree/pretty.go
String
called by 143
pkg/sql/sem/tree/overload.go
Family
called by 109
pkg/sql/types/types.go
Newf
called by 109
pkg/sql/pgwire/pgerror/errors.go

Shape

Method 2,839
Function 994
Struct 402
TypeAlias 128
Interface 69
FuncType 5

Languages

Go100%

Modules by API surface

pkg/sql/sem/tree/datum.go426 symbols
pkg/sql/sem/tree/stmt.go361 symbols
pkg/util/json/json.go266 symbols
pkg/sql/sem/tree/expr.go236 symbols
pkg/sql/sem/tree/eval.go172 symbols
pkg/util/encoding/encoding.go153 symbols
pkg/sql/parser/sql.go142 symbols
pkg/sql/sem/tree/pretty.go138 symbols
pkg/sql/sem/tree/walk.go129 symbols
pkg/sql/sem/tree/create.go123 symbols
pkg/sql/sem/tree/type_check.go114 symbols
pkg/sql/sem/tree/select.go112 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add postgresql-parser \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page