Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/pocket-pull-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: pocket-test
on: [pull_request]
jobs:
pocket-test:
name: Pocket test
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Get dependencies
working-directory: ./pocket
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi

- name: Test
working-directory: ./pocket
run: make test

- name: Build
working-directory: ./pocket
run: make
22 changes: 22 additions & 0 deletions go-sqlsmith/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Binaries for programs and plugins
.DS_Store
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/

# IDEs
.idea
.vscode
*.swap
*.swp
37 changes: 37 additions & 0 deletions go-sqlsmith/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# SQLsmith-GO

Go version of [SQLsmith](https://github.com/anse1/sqlsmith).

## Usage

```go
import (
sqlsmith_go "github.com/pingcap/tipocket/go-sqlsmith"
)

func gosmith() {
ss := sqlsmith_go.New()

// load schema
ss.LoadSchema([][5]string{
// members table
[5]string{"games", "members", "BASE TABLE", "id", "int(11)"},
[5]string{"games", "members", "BASE TABLE", "name", "varchar(255)"},
[5]string{"games", "members", "BASE TABLE", "age", "int(11)"},
[5]string{"games", "members", "BASE TABLE", "team_id", "int(11)"},
// teams table
[5]string{"games", "teams", "BASE TABLE", "id", "int(11)"},
[5]string{"games", "teams", "BASE TABLE", "team_name", "varchar(255)"},
[5]string{"games", "teams", "BASE TABLE", "created_at", "timestamp"},
})

// use games database
ss.SetDB("games")

// generate select statement AST without scema information
node := ss.SelectStmt(5)

// fill the tree with selected schema and get SQL string
sql, err := ss.Walk(node)
}
```
61 changes: 61 additions & 0 deletions go-sqlsmith/builtin/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package builtin

import (
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/tipocket/go-sqlsmith/types"
"github.com/pingcap/tipocket/go-sqlsmith/util"
)

// GenerateFuncCallExpr generate random builtin chain
func GenerateFuncCallExpr(table *types.Table, args int, stable bool) ast.ExprNode {
if args == 0 && util.Rd(2) == 0 {
return ast.NewValueExpr(util.GenerateRandDataItem())
}

funcCallExpr := ast.FuncCallExpr{}

fns := getValidArgsFunc(args, stable)
fn := copyFunc(fns[util.Rd(len(fns))])
funcCallExpr.FnName = model.NewCIStr(fn.name)
for i := 0; i < args; i++ {
r := util.Rd(100)
if r > 80 {
funcCallExpr.Args = append(funcCallExpr.Args, GenerateFuncCallExpr(table, 1 + util.Rd(3), stable))
}
funcCallExpr.Args = append(funcCallExpr.Args, GenerateFuncCallExpr(table, 0, stable))
}
// if args != 0 {
// log.Println(funcCallExpr)
// for _, arg := range funcCallExpr.Args {
// log.Println(arg)
// }
// }
return &funcCallExpr
}

func getValidArgsFunc(args int, stable bool) []*functionClass {
var fns []*functionClass
for _, fn := range getFuncMap() {
if fn.minArg > args {
continue
}
if fn.stable != stable {
continue
}
if fn.maxArg == -1 || fn.maxArg >= args {
fns = append(fns, fn)
}
}
return fns
}

func copyFunc(fn *functionClass) *functionClass {
return &functionClass{
name: fn.name,
minArg: fn.minArg,
maxArg: fn.maxArg,
constArg: fn.constArg,
mysql: fn.mysql,
}
}
12 changes: 12 additions & 0 deletions go-sqlsmith/builtin/function_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package builtin


import "github.com/pingcap/parser/ast"

var commonFunctions = []*functionClass{
{ast.Coalesce, 1, -1, false, true, false},
{ast.IsNull, 1, 1, false, true, false},
{ast.Greatest, 2, -1, false, true, false},
{ast.Least, 2, -1, false, true, false},
{ast.Interval, 2, -1, false, true, false},
}
9 changes: 9 additions & 0 deletions go-sqlsmith/builtin/function_control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package builtin


import "github.com/pingcap/parser/ast"

var controlFunctions = []*functionClass{
{ast.If, 3, 3, false, true, false},
{ast.Ifnull, 3, 3, false, true, false},
}
71 changes: 71 additions & 0 deletions go-sqlsmith/builtin/function_datetime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package builtin


import "github.com/pingcap/parser/ast"

var datetimeFunctions = []*functionClass{
{ast.AddDate, 3, 3, false, true, false},
{ast.DateAdd, 3, 3, false, true, false},
{ast.SubDate, 3, 3, false, true, false},
{ast.DateSub, 3, 3, false, true, false},
{ast.AddTime, 2, 2, false, true, false},
{ast.ConvertTz, 3, 3, false, true, false},
// {ast.Curdate, 0, 0, false, true, false},
// {ast.CurrentDate, 0, 0, false, true, false},
// {ast.CurrentTime, 0, 1, false, true, false},
// {ast.CurrentTimestamp, 0, 1, false, true, false},
// {ast.Curtime, 0, 1, false, true, false},
{ast.Date, 1, 1, false, true, false},
{ast.DateLiteral, 1, 1, false, true, false},
{ast.DateFormat, 2, 2, false, true, false},
{ast.DateDiff, 2, 2, false, true, false},
{ast.Day, 1, 1, false, true, false},
{ast.DayName, 1, 1, false, true, false},
{ast.DayOfMonth, 1, 1, false, true, false},
{ast.DayOfWeek, 1, 1, false, true, false},
{ast.DayOfYear, 1, 1, false, true, false},
{ast.Extract, 2, 2, false, true, false},
{ast.FromDays, 1, 1, false, true, false},
{ast.FromUnixTime, 1, 2, false, true, false},
{ast.GetFormat, 2, 2, false, true, false},
{ast.Hour, 1, 1, false, true, false},
// {ast.LocalTime, 0, 1, false, true, false},
// {ast.LocalTimestamp, 0, 1, true, true, false},
{ast.MakeDate, 2, 2, false, true, false},
{ast.MakeTime, 3, 3, false, true, false},
{ast.MicroSecond, 1, 1, false, true, false},
{ast.Minute, 1, 1, false, true, false},
{ast.Month, 1, 1, false, true, false},
{ast.MonthName, 1, 1, false, true, false},
// {ast.Now, 0, 1, false, true, false},
{ast.PeriodAdd, 2, 2, false, true, false},
// {ast.PeriodDiff, 2, 2, false, true, false},
{ast.Quarter, 1, 1, false, true, false},
{ast.SecToTime, 1, 1, false, true, false},
{ast.Second, 1, 1, false, true, false},
{ast.StrToDate, 2, 2, false, true, false},
{ast.SubTime, 2, 2, false, true, false},
// will make diff
// {ast.Sysdate, 0, 1, false, true, false},
{ast.Time, 1, 1, false, true, false},
{ast.TimeLiteral, 1, 1, false, true, false},
{ast.TimeFormat, 2, 2, false, true, false},
{ast.TimeToSec, 1, 1, false, true, false},
{ast.TimeDiff, 2, 2, false, true, false},
{ast.Timestamp, 1, 2, false, true, false},
{ast.TimestampLiteral, 1, 2, false, true, false},
{ast.TimestampAdd, 3, 3, false, true, false},
{ast.TimestampDiff, 3, 3, false, true, false},
{ast.ToDays, 1, 1, false, true, false},
{ast.ToSeconds, 1, 1, false, true, false},
// {ast.UnixTimestamp, 0, 1, false, true, false},
// {ast.UTCDate, 0, 0, false, true, false},
// {ast.UTCTime, 0, 1, false, true, false},
// {ast.UTCTimestamp, 0, 1, false, true, false},
{ast.Week, 1, 2, false, true, false},
{ast.Weekday, 1, 1, false, true, false},
{ast.WeekOfYear, 1, 1, false, true, false},
{ast.Year, 1, 1, false, true, false},
{ast.YearWeek, 1, 2, false, true, false},
{ast.LastDay, 1, 1, false, true, false},
}
27 changes: 27 additions & 0 deletions go-sqlsmith/builtin/function_encryption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package builtin


import "github.com/pingcap/parser/ast"

// compress and uncompress function make diff
var encryptionFunctions = []*functionClass{
{ast.AesDecrypt, 2, 3, false, true, false},
{ast.AesEncrypt, 2, 3, false, true, false},
// {ast.Compress, 1, 1, false, true, false},
// removed in MySQL 8.0.3
// {ast.Decode, 2, 2, false, true, false},
{ast.DesDecrypt, 1, 2, false, true, false},
{ast.DesEncrypt, 1, 2, false, true, false},
// {ast.Encode, 2, 2, false, true, false},
{ast.Encrypt, 1, 2, false, true, false},
{ast.MD5, 1, 1, false, true, false},
{ast.OldPassword, 1, 1, false, true, false},
// {ast.PasswordFunc, 1, 1, false, true, false},
{ast.RandomBytes, 1, 1, false, true, false},
{ast.SHA1, 1, 1, false, true, false},
{ast.SHA, 1, 1, false, true, false},
{ast.SHA2, 2, 2, false, true, false},
// {ast.Uncompress, 1, 1, false, true, false},
// {ast.UncompressedLength, 1, 1, false, true, false},
// {ast.ValidatePasswordStrength, 1, 1, false, true, false},
}
28 changes: 28 additions & 0 deletions go-sqlsmith/builtin/function_information.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package builtin


import "github.com/pingcap/parser/ast"

var informationFunctions = []*functionClass{
// will make diff
// {ast.ConnectionID, 0, 0, false, true, false},
{ast.CurrentUser, 0, 0, false, true, true},
// should be fix
// {ast.CurrentRole, 0, 0, false, true, false},
{ast.Database, 0, 0, false, true, false},
// This function is a synonym for DATABASE().
// See http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_schema
{ast.Schema, 0, 0, false, true, false},
{ast.FoundRows, 0, 0, false, true, false},
{ast.LastInsertId, 0, 1, false, true, false},
{ast.User, 0, 0, false, true, true},
{ast.Version, 0, 0, false, true, true},
{ast.Benchmark, 2, 2, false, true, false},
// {ast.Charset, 1, 1, false, true, false},
// {ast.Coercibility, 1, 1, false, true, false},
// {ast.Collation, 1, 1, false, true, false},
{ast.RowCount, 0, 0, false, true, false},
// Will make difference in abtest
// {ast.SessionUser, 0, 0, false, true, false},
// {ast.SystemUser, 0, 0, false, true, false},
}
31 changes: 31 additions & 0 deletions go-sqlsmith/builtin/function_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package builtin


// import "github.com/pingcap/parser/ast"

var jsonFunctions = []*functionClass{
// {ast.JSONType, 1, 1, false, true, false},
// {ast.JSONExtract, 2, -1, false, true, false},
// {ast.JSONUnquote, 1, 1, false, true, false},
// {ast.JSONSet, 3, -1, false, true, false},
// {ast.JSONInsert, 3, -1, false, true, false},
// {ast.JSONReplace, 3, -1, false, true, false},
// // {ast.JSONRemove, 2, -1, false, true, false},
// // {ast.JSONMerge, 2, -1, false, true, false},
// {ast.JSONObject, 0, -1, false, true, false},
// {ast.JSONArray, 0, -1, false, true, false},
// {ast.JSONContains, 2, 3, false, true, false},
// {ast.JSONContainsPath, 3, -1, false, true, false},
// {ast.JSONValid, 1, 1, false, true, false},
// {ast.JSONArrayAppend, 3, -1, false, true, false},
// {ast.JSONArrayInsert, 3, -1, false, true, false},
// // {ast.JSONMergePatch, 2, -1, false, true, false},
// {ast.JSONMergePreserve, 2, -1, false, true, false},
// // {ast.JSONPretty, 1, 1, false, true, false},
// {ast.JSONQuote, 1, 1, false, true, false},
// {ast.JSONSearch, 3, -1, false, true, false},
// // {ast.JSONStorageSize, 1, 1, false, true, false},
// {ast.JSONDepth, 1, 1, false, true, false},
// {ast.JSONKeys, 1, 2, false, true, false},
// {ast.JSONLength, 1, 2, false, true, false},
}
31 changes: 31 additions & 0 deletions go-sqlsmith/builtin/function_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package builtin


import "log"

var funcLists = [][]*functionClass{
commonFunctions,
mathFunctions,
datetimeFunctions,
stringFunctions,
informationFunctions,
controlFunctions,
miscellaneousFunctions,
encryptionFunctions,
jsonFunctions,
tidbFunctions,
}

func getFuncMap() map[string]*functionClass {
funcs := make(map[string]*functionClass)
for _, funcSet := range funcLists {
for _, fn := range funcSet {
if _, ok := funcs[fn.name]; ok {
log.Fatalf("duplicated func name %s", fn.name)
} else {
funcs[fn.name] = fn
}
}
}
return funcs
}
Loading