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:
- Copy all the
pkg/sql/parser
,pkg/sql/lex
and simplify the dependencies - Simplify the Makefile to just generate the goyacc stuff
- Add the goyacc generated files in parser and lex to make
go get
work easily, see the.gitignore
files - Trim the
etcd
dependency, see thego.mod
- Rename all test file except some
pkg/sql/parser
tests - Add all necessary imports to vendor
- Remove the
panic
of meeting unregistried functions, see the WrapFunction - Other nasty things make the parser just work that I forgot :p
- Pure golang implementation
- Almost full support of PostgreSQL (
cockroachdb
style PostgreSQL)
The code is derived from CockroachDB v20.1.11 which supports most of the major features of SQL:2011. See:
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
}
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
- 2021-02-16
github.com/auxten/postgresql-parser/pkg/sql/parser
Unit tests works now! - 2021-03-08 Add walker package.
- Fix more unit tests
- Make built-in function parsing work