Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: support builtin function "benchmark" #8313

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
214 changes: 213 additions & 1 deletion expression/builtin_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ var (
_ builtinFunc = &builtinVersionSig{}
_ builtinFunc = &builtinTiDBVersionSig{}
_ builtinFunc = &builtinRowCountSig{}
_ builtinFunc = &builtinBenchmarkStringSig{}
_ builtinFunc = &builtinBenchmarkIntSig{}
_ builtinFunc = &builtinBenchmarkDecimalSig{}
_ builtinFunc = &builtinBenchmarkJSONSig{}
_ builtinFunc = &builtinBenchmarkRealSig{}
_ builtinFunc = &builtinBenchmarkDurationSig{}
_ builtinFunc = &builtinBenchmarkTimeSig{}
)

type databaseFunctionClass struct {
Expand Down Expand Up @@ -384,7 +391,212 @@ type benchmarkFunctionClass struct {
}

func (c *benchmarkFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "BENCHMARK")
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
argFieldTp := args[1].GetType()
argTp := argFieldTp.EvalType()

bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETInt, types.ETInt, argTp)
var sig builtinFunc
switch argTp {
case types.ETString:
sig = &builtinBenchmarkStringSig{bf}
case types.ETInt:
sig = &builtinBenchmarkIntSig{bf}
case types.ETJson:
sig = &builtinBenchmarkJSONSig{bf}
case types.ETReal:
sig = &builtinBenchmarkRealSig{bf}
case types.ETDecimal:
sig = &builtinBenchmarkDecimalSig{bf}
case types.ETDuration:
sig = &builtinBenchmarkIntSig{bf}
case types.ETDatetime:
sig = &builtinBenchmarkTimeSig{bf}
case types.ETTimestamp:
sig = &builtinBenchmarkTimeSig{bf}
default:
return nil, errors.New("FUNCTION benchmark has wrong arg2")
}

return sig, nil

zz-jason marked this conversation as resolved.
Show resolved Hide resolved
}

type builtinBenchmarkStringSig struct {
baseBuiltinFunc
}

func (b *builtinBenchmarkStringSig) evalInt(row chunk.Row) (int64, bool, error) {
x, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
var i int64 = 0
for ; i < x; i++ {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
_, _, err := b.args[1].EvalString(b.ctx, row)
if err != nil {
return 0, isNull, errors.Trace(err)
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
}
}
return 0, false, nil
}

func (b *builtinBenchmarkStringSig) Clone() builtinFunc {
newSig := &builtinBenchmarkStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

type builtinBenchmarkJSONSig struct {
baseBuiltinFunc
}

func (b *builtinBenchmarkJSONSig) evalInt(row chunk.Row) (int64, bool, error) {
x, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
var i int64 = 0
for ; i < x; i++ {
_, _, err := b.args[1].EvalJSON(b.ctx, row)
if err != nil {
return 0, false, errors.Trace(err)
}
}
return 0, false, nil
}

func (b *builtinBenchmarkJSONSig) Clone() builtinFunc {
newSig := &builtinBenchmarkJSONSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

type builtinBenchmarkIntSig struct {
baseBuiltinFunc
}

func (b *builtinBenchmarkIntSig) evalInt(row chunk.Row) (int64, bool, error) {
x, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
var i int64 = 0
for ; i < x; i++ {
_, _, err := b.args[1].EvalInt(b.ctx, row)
if err != nil {
return 0, false, errors.Trace(err)
}
}
return 0, false, nil
}

func (b *builtinBenchmarkIntSig) Clone() builtinFunc {
newSig := &builtinBenchmarkIntSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

type builtinBenchmarkDecimalSig struct {
baseBuiltinFunc
}

func (b *builtinBenchmarkDecimalSig) evalInt(row chunk.Row) (int64, bool, error) {
x, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
var i int64 = 0
for ; i < x; i++ {
_, _, err := b.args[1].EvalDecimal(b.ctx, row)
if err != nil {
return 0, false, errors.Trace(err)
}
}
return 0, false, nil
}

func (b *builtinBenchmarkDecimalSig) Clone() builtinFunc {
newSig := &builtinBenchmarkDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

type builtinBenchmarkRealSig struct {
baseBuiltinFunc
}

func (b *builtinBenchmarkRealSig) evalInt(row chunk.Row) (int64, bool, error) {
x, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
var i int64 = 0
for ; i < x; i++ {
_, _, err := b.args[1].EvalReal(b.ctx, row)
if err != nil {
return 0, false, errors.Trace(err)
}
}
return 0, false, nil
}

func (b *builtinBenchmarkRealSig) Clone() builtinFunc {
newSig := &builtinBenchmarkRealSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

type builtinBenchmarkDurationSig struct {
baseBuiltinFunc
}

func (b *builtinBenchmarkDurationSig) evalInt(row chunk.Row) (int64, bool, error) {
x, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
var i int64 = 0
for ; i < x; i++ {
_, _, err := b.args[1].EvalDuration(b.ctx, row)
if err != nil {
return 0, false, errors.Trace(err)
}
}
return 0, false, nil
}

func (b *builtinBenchmarkDurationSig) Clone() builtinFunc {
newSig := &builtinBenchmarkDurationSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

type builtinBenchmarkTimeSig struct {
baseBuiltinFunc
}

func (b *builtinBenchmarkTimeSig) evalInt(row chunk.Row) (int64, bool, error) {
x, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
var i int64 = 0
for ; i < x; i++ {
_, _, err := b.args[1].EvalTime(b.ctx, row)
if err != nil {
return 0, false, errors.Trace(err)
}
}
return 0, false, nil
}

func (b *builtinBenchmarkTimeSig) Clone() builtinFunc {
newSig := &builtinBenchmarkTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

type charsetFunctionClass struct {
Expand Down
37 changes: 32 additions & 5 deletions expression/builtin_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
package expression

import (
"math"

. "github.com/pingcap/check"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/auth"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/printer"
"github.com/pingcap/tidb/util/testleak"
"math"
"time"
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
)

func (s *testEvaluatorSuite) TestDatabase(c *C) {
Expand Down Expand Up @@ -121,9 +122,35 @@ func (s *testEvaluatorSuite) TestVersion(c *C) {
func (s *testEvaluatorSuite) TestBenchMark(c *C) {
defer testleak.AfterTest(c)()
fc := funcs[ast.Benchmark]
f, err := fc.getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(nil, nil)))
c.Assert(f, IsNil)
c.Assert(err, ErrorMatches, "*FUNCTION BENCHMARK does not exist")

mapping := make(map[string]interface{}, 1)
mapping["test"] = "test"
jsonObj := json.CreateBinary(mapping)

cases := []struct {
args []interface{}
expected int64
isNil bool
getErr bool
}{
{[]interface{}{1000, 2}, 0, false, false},
{[]interface{}{1000, 2.0}, 0, false, false},
{[]interface{}{1000, "test"}, 0, false, false},
{[]interface{}{1000, types.TimeFromDays(20171010123456)}, 0, false, false},
{[]interface{}{1000, types.NewDecFromFloatForTest(1.1)}, 0, false, false},
{[]interface{}{1000, types.Duration{Duration: time.Duration(0)}}, 0, false, false},
{[]interface{}{1000, jsonObj}, 0, false, false},
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add some cases that generate some errors or nil results~?

for _, test := range cases {

zz-jason marked this conversation as resolved.
Show resolved Hide resolved
f, err := fc.getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(test.args...)))

c.Assert(err, IsNil)
v, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(v.GetInt64(), Equals, test.expected)
}

zz-jason marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *testEvaluatorSuite) TestCharset(c *C) {
Expand Down
6 changes: 6 additions & 0 deletions expression/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package expression

import (
"log"
"testing"
"time"

Expand Down Expand Up @@ -103,6 +104,10 @@ func (s *testEvaluatorSuite) kindToFieldType(kind byte) types.FieldType {
ft.Collate = charset.CollationBin
case types.KindMysqlBit:
ft.Tp = mysql.TypeBit
case types.KindMysqlJSON:
ft.Tp = mysql.TypeJSON
ft.Charset = charset.CharsetUTF8MB4
ft.Collate = charset.CollationUTF8MB4
}
return ft
}
Expand All @@ -111,6 +116,7 @@ func (s *testEvaluatorSuite) datumsToConstants(datums []types.Datum) []Expressio
constants := make([]Expression, 0, len(datums))
for _, d := range datums {
ft := s.kindToFieldType(d.Kind())
log.Print(d.Kind())
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
ft.Flen, ft.Decimal = types.UnspecifiedLength, types.UnspecifiedLength
constants = append(constants, &Constant{Value: d, RetType: &ft})
}
Expand Down