Categorygithub.com/wind-c/cosqlparser
repository
0.5.1
Repository: https://github.com/wind-c/cosqlparser.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

cosqlparser

Cosqlparser is a powerful and high-performance SQL parser for Go (powered by vitess).

This library inspired by https://github.com/blastrain/vitess-sqlparser, but it uses an older version of vitess and the latest version has vastly improved performance.

(original source : https://github.com/youtube/vitess/tree/master/go/vt/sqlparser)

Installation

[NOTE] Required Go version more than 1.9

go get -u github.com/wind-c/cosqlparser

Examples

package main

import (
	"encoding/json"
	"fmt"
	"github.com/wind-c/cosqlparser/sqlparser"
)

func main() {
	sqlStr := "SELECT t.cola a, t.colb b FROM topic t  WHERE a < 30 and b > 20 and a between 10 and 20 GROUP BY (a, b) HAVING a > c ORDER BY b"

	stmt, err := sqlparser.Parse(sqlStr)
	if err != nil {
		panic(err)
	}
	fmt.Printf("stmt = %+v\n", stmt)

	jn, err := json.Marshal(stmt)
	if err != nil {
		fmt.Printf("unexpected error: %s", err)
		return
	}
	fmt.Println(string(jn))
}

Benchmark

Simple SQL

sql = "SELECT cola, colb FROM topic t WHERE colb > 20"

libraryb.Nns/opB/opallocs/op
wind-c/cosqlparser2434244890 ns/op1208 B/op22 allocs/op
vitess-sqlparser1587487431 ns/op11984 B/op38 allocs/op
pingcap/parser2026216317 ns/op19064 B/op28 allocs/op

Complex SQL

sql = "SELECT cola a, colb b FROM topic t WHERE b > 20 and a between 10 and 20 GROUP BY (a, b) HAVING a > c ORDER BY b"

libraryb.Nns/opB/opallocs/op
wind-c/cosqlparser10750911048 ns/op2305 B/op47 allocs/op
vitess-sqlparser8382114271 ns/op13120 B/op89 allocs/op
pingcap/parser11392510483 ns/op21888 B/op60 allocs/op
package cosqlparser

import (
	btparser "github.com/blastrain/vitess-sqlparser/sqlparser"
	pcparser "github.com/pingcap/parser"
	_ "github.com/pingcap/parser/test_driver"
	coparser "github.com/wind-c/cosqlparser/sqlparser"
	"testing"
)

var sql = "SELECT cola a, colb b FROM topic t  WHERE b > 20 and a between 10 and 20 GROUP BY (a, b) HAVING a > c ORDER BY b"

//var sql = "SELECT cola, colb FROM topic t  WHERE colb > 20"

func BenchmarkBlastrainVitessParse(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		btparser.Parse(sql)
	}
}

func BenchmarkCoVitessParse(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		coparser.Parse(sql)
	}
}

func BenchmarkPingcapParse(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		pcparser.New().Parse(sql, "", "")
	}
}