postgresql-parser

module
v0.0.0-...-13a6da0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2024 License: Apache-2.0

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

Who is using this

Features

  • Pure golang implementation
  • Almost full support of PostgreSQL (cockroachdb style PostgreSQL)
  • For SQL lineage analysis see go-sql-lineage

SQL Standard Compliance

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

How to use

package main

import (
	"log"
	
	"github.com/forhsd/postgresql-parser/pkg/sql/parser"
	"github.com/forhsd/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

Todo

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

Directories

Path Synopsis
example
pkg
sql/pgwire/pgcode
Package pgcode defines the PostgreSQL 5-character support codes used throughout the CockroachDB source tree.
Package pgcode defines the PostgreSQL 5-character support codes used throughout the CockroachDB source tree.
util/encoding/csv
Package csv reads and writes comma-separated values (CSV) files.
Package csv reads and writes comma-separated values (CSV) files.
util/hlc
Package hlc implements the Hybrid Logical Clock outlined in "Logical Physical Clocks and Consistent Snapshots in Globally Distributed Databases", available online at http://www.cse.buffalo.edu/tech-reports/2014-04.pdf.
Package hlc implements the Hybrid Logical Clock outlined in "Logical Physical Clocks and Consistent Snapshots in Globally Distributed Databases", available online at http://www.cse.buffalo.edu/tech-reports/2014-04.pdf.
util/leaktest
Package leaktest provides tools to detect leaked goroutines in tests.
Package leaktest provides tools to detect leaked goroutines in tests.
util/pretty
Package pretty prints documents based on a target line width.
Package pretty prints documents based on a target line width.
util/timeutil/pgdate
Package pgdate contains parsing functions and types for dates and times in a manner that is compatible with PostgreSQL.
Package pgdate contains parsing functions and types for dates and times in a manner that is compatible with PostgreSQL.

Jump to

Keyboard shortcuts

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