parser: support built-in function pow

This commit is contained in:
sllt
2015-12-19 23:51:04 +08:00
parent 60ec9fccff
commit c01879ba2e
5 changed files with 77 additions and 2 deletions

View File

@ -55,8 +55,10 @@ var Funcs = map[string]Func{
"coalesce": {builtinCoalesce, 1, -1, true, false},
// math functions
"abs": {builtinAbs, 1, 1, true, false},
"rand": {builtinRand, 0, 1, true, false},
"abs": {builtinAbs, 1, 1, true, false},
"rand": {builtinRand, 0, 1, true, false},
"pow": {builtinPow, 2, 2, true, false},
"power": {builtinPower, 2, 2, true, false},
// group by functions
"avg": {builtinAvg, 1, 1, false, true},

View File

@ -63,3 +63,22 @@ func builtinRand(args []interface{}, ctx map[interface{}]interface{}) (v interfa
return rand.Float64(), nil
}
func builtinPow(args []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
x, err := types.ToFloat64(args[0])
if err != nil {
return nil, errors.Trace(err)
}
y, err := types.ToFloat64(args[1])
if err != nil {
return nil, errors.Trace(err)
}
return math.Pow(x, y), nil
}
func builtinPower(args []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
return builtinPow(args, ctx)
}

View File

@ -43,3 +43,39 @@ func (s *testBuiltinSuite) TestRand(c *C) {
c.Assert(v, Less, float64(1))
c.Assert(v, GreaterEqual, float64(0))
}
func (s *testBuiltinSuite) TestPow(c *C) {
tbl := []struct {
Arg []interface{}
Ret float64
}{
{[]interface{}{1, 3}, 1},
{[]interface{}{2, 2}, 4},
{[]interface{}{4, 0.5}, 2},
{[]interface{}{4, -2}, 0.0625},
}
for _, t := range tbl {
v, err := builtinPow(t.Arg, nil)
c.Assert(err, IsNil)
c.Assert(v, DeepEquals, t.Ret)
}
}
func (s *testBuiltinSuite) TestPower(c *C) {
tbl := []struct {
Arg []interface{}
Ret float64
}{
{[]interface{}{1, 3}, 1},
{[]interface{}{2, 2}, 4},
{[]interface{}{4, 0.5}, 2},
{[]interface{}{4, -2}, 0.0625},
}
for _, t := range tbl {
v, err := builtinPower(t.Arg, nil)
c.Assert(err, IsNil)
c.Assert(v, DeepEquals, t.Ret)
}
}

View File

@ -210,6 +210,8 @@ import (
outer "OUTER"
password "PASSWORD"
placeholder "PLACEHOLDER"
pow "POW"
power "POWER"
prepare "PREPARE"
primary "PRIMARY"
quarter "QUARTER"
@ -2257,6 +2259,16 @@ FunctionCallNonKeyword:
{
$$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1.(string)), Args: $3.([]ast.ExprNode)}
}
| "POW" '(' Expression ',' Expression ')'
{
args := []ast.ExprNode{$3.(ast.ExprNode), $5.(ast.ExprNode)}
$$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1.(string)), Args: args}
}
| "POWER" '(' Expression ',' Expression ')'
{
args := []ast.ExprNode{$3.(ast.ExprNode), $5.(ast.ExprNode)}
$$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1.(string)), Args: args}
}
| "RAND" '(' ExpressionOpt ')'
{

View File

@ -414,6 +414,8 @@ or {o}{r}
order {o}{r}{d}{e}{r}
outer {o}{u}{t}{e}{r}
password {p}{a}{s}{s}{w}{o}{r}{d}
pow {p}{o}{w}
power {p}{o}{w}{e}{r}
prepare {p}{r}{e}{p}{a}{r}{e}
primary {p}{r}{i}{m}{a}{r}{y}
quarter {q}{u}{a}{r}{t}{e}{r}
@ -850,6 +852,10 @@ year_month {y}{e}{a}{r}_{m}{o}{n}{t}{h}
{outer} return outer
{password} lval.item = string(l.val)
return password
{pow} lval.item = string(l.val)
return pow
{power} lval.item = string(l.val)
return power
{prepare} lval.item = string(l.val)
return prepare
{primary} return primary