Coder Social home page Coder Social logo

cosqlparser's Introduction

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"

library b.N ns/op B/op allocs/op
wind-c/cosqlparser 243424 4890 ns/op 1208 B/op 22 allocs/op
vitess-sqlparser 158748 7431 ns/op 11984 B/op 38 allocs/op
pingcap/parser 202621 6317 ns/op 19064 B/op 28 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"

library b.N ns/op B/op allocs/op
wind-c/cosqlparser 107509 11048 ns/op 2305 B/op 47 allocs/op
vitess-sqlparser 83821 14271 ns/op 13120 B/op 89 allocs/op
pingcap/parser 113925 10483 ns/op 21888 B/op 60 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, "", "")
	}
}

cosqlparser's People

Contributors

wind-c avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

xwinie

cosqlparser's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.