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

builtin: add any_value built-in function #2866

Merged
merged 4 commits into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion expression/builtin_miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ type builtinAnyValueSig struct {

// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value
func (b *builtinAnyValueSig) eval(row []types.Datum) (d types.Datum, err error) {
return d, errFunctionNotExists.GenByArgs("ANY_VALUE")
args, err := b.evalArgs(row)
if err != nil {
return types.Datum{}, errors.Trace(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Return d, errors.Trace(err) is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, will fix.

}
d = args[0]
return d, nil
}

type defaultFunctionClass struct {
Expand Down
30 changes: 30 additions & 0 deletions expression/builtin_miscellaneous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,33 @@
// limitations under the License.

package expression

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/util/testleak"
"github.com/pingcap/tidb/util/testutil"
"github.com/pingcap/tidb/util/types"
)

func (s *testEvaluatorSuite) TestAnyValue(c *C) {
defer testleak.AfterTest(c)()

tbl := []struct {
arg interface{}
ret interface{}
}{
{nil, nil},
{1234, 1234},
{-0x99, -0x99},
{3.1415926, 3.1415926},
{"Hello, World", "Hello, World"},
}
for _, t := range tbl {
fc := funcs[ast.AnyValue]
f, err := fc.getFunction(datumsToConstants(types.MakeDatums(t.arg)), s.ctx)
c.Assert(err, IsNil)
r, err := f.eval(nil)
c.Assert(r, testutil.DatumEquals, types.NewDatum(t.ret))
}
}
2 changes: 2 additions & 0 deletions plan/typeinferer.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) {
tp.Flen = 40
case ast.Coalesce:
tp = aggArgsType(x.Args)
case ast.AnyValue:
tp = x.Args[0].GetType()
default:
tp = types.NewFieldType(mysql.TypeUnspecified)
}
Expand Down
3 changes: 3 additions & 0 deletions plan/typeinferer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ func (ts *testTypeInferrerSuite) TestInferType(c *C) {
{`coalesce(null, 0.1)`, mysql.TypeNewDecimal, charset.CharsetBin},
{`coalesce(1, "1" + 1)`, mysql.TypeDouble, charset.CharsetBin},
{`coalesce(1, "abc")`, mysql.TypeVarString, charset.CharsetUTF8},
{`any_value("abc")`, mysql.TypeVarString, charset.CharsetUTF8},
{`any_value(1)`, mysql.TypeLonglong, charset.CharsetBin},
{`any_value(1.234)`, mysql.TypeNewDecimal, charset.CharsetBin},
{`make_set(1 | 3, "hello", "nice", null, "world")`, mysql.TypeVarString, charset.CharsetUTF8},
{`exp(1)`, mysql.TypeDouble, charset.CharsetBin},
{`exp(1.23)`, mysql.TypeDouble, charset.CharsetBin},
Expand Down