本文整理汇总了Golang中github.com/pingcap/tidb/ast.NewValueExpr函数的典型用法代码示例。如果您正苦于以下问题:Golang NewValueExpr函数的具体用法?Golang NewValueExpr怎么用?Golang NewValueExpr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewValueExpr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestCast
func (s *testEvaluatorSuite) TestCast(c *C) {
f := types.NewFieldType(mysql.TypeLonglong)
expr := &ast.FuncCastExpr{
Expr: ast.NewValueExpr(1),
Tp: f,
}
ctx := mock.NewContext()
v, err := Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, Equals, int64(1))
f.Flag |= mysql.UnsignedFlag
v, err = Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, Equals, uint64(1))
f.Tp = mysql.TypeString
f.Charset = charset.CharsetBin
v, err = Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, DeepEquals, []byte("1"))
f.Tp = mysql.TypeString
f.Charset = "utf8"
v, err = Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, DeepEquals, "1")
expr.Expr = ast.NewValueExpr(nil)
v, err = Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, IsNil)
}
示例2: TestBinopBitop
func (s *testEvaluatorSuite) TestBinopBitop(c *C) {
ctx := mock.NewContext()
tbl := []struct {
lhs interface{}
op opcode.Op
rhs interface{}
ret interface{}
}{
{1, opcode.And, 1, 1},
{1, opcode.Or, 1, 1},
{1, opcode.Xor, 1, 0},
{1, opcode.LeftShift, 1, 2},
{2, opcode.RightShift, 1, 1},
{nil, opcode.And, 1, nil},
{1, opcode.And, nil, nil},
{nil, opcode.Or, 1, nil},
{nil, opcode.Xor, 1, nil},
{nil, opcode.LeftShift, 1, nil},
{nil, opcode.RightShift, 1, nil},
}
for _, t := range tbl {
expr := &ast.BinaryOperationExpr{Op: t.op, L: ast.NewValueExpr(t.lhs), R: ast.NewValueExpr(t.rhs)}
v, err := Eval(ctx, expr)
c.Assert(err, IsNil)
switch x := t.ret.(type) {
case nil:
c.Assert(v, IsNil)
case int:
c.Assert(v, DeepEquals, uint64(x))
}
}
}
示例3: TestCount
func (s *testAggFuncSuite) TestCount(c *C) {
// Compose aggregate exec for "select c1, count(c2) from t";
// c1 c2
// 1 1
// 2 1
// 3 nil
c1 := ast.NewValueExpr(0)
rf1 := &ast.ResultField{Expr: c1}
col1 := &ast.ColumnNameExpr{Refer: rf1}
fc1 := &ast.AggregateFuncExpr{
F: ast.AggFuncFirstRow,
Args: []ast.ExprNode{col1},
}
c2 := ast.NewValueExpr(0)
rf2 := &ast.ResultField{Expr: c2}
col2 := &ast.ColumnNameExpr{Refer: rf2}
fc2 := &ast.AggregateFuncExpr{
F: ast.AggFuncCount,
Args: []ast.ExprNode{col2},
}
row1 := []interface{}{1, 1}
row2 := []interface{}{2, 1}
row3 := []interface{}{3, nil}
data := []([]interface{}){row1, row2, row3}
rows := make([]*Row, 0, 3)
for _, d := range data {
rows = append(rows, &Row{Data: d})
}
src := &mockExec{
rows: rows,
fields: []*ast.ResultField{rf1, rf2},
}
agg := &AggregateExec{
AggFuncs: []*ast.AggregateFuncExpr{fc1, fc2},
Src: src,
}
var (
row *Row
cnt int
)
for {
r, err := agg.Next()
c.Assert(err, IsNil)
if r == nil {
break
}
row = r
cnt++
}
c.Assert(cnt, Equals, 1)
c.Assert(row, NotNil)
ctx := mock.NewContext()
val, err := evaluator.Eval(ctx, fc1)
c.Assert(err, IsNil)
c.Assert(val, Equals, 1)
val, err = evaluator.Eval(ctx, fc2)
c.Assert(err, IsNil)
c.Assert(val, Equals, int64(2))
}
示例4: TestRegexp
func (s *testEvaluatorSuite) TestRegexp(c *C) {
defer testleak.AfterTest(c)()
tbl := []struct {
pattern string
input string
match int64
}{
{"^$", "a", 0},
{"a", "a", 1},
{"a", "b", 0},
{"aA", "aA", 1},
{".", "a", 1},
{"^.$", "ab", 0},
{"..", "b", 0},
{".ab", "aab", 1},
{".*", "abcd", 1},
}
for _, v := range tbl {
pattern := &ast.PatternRegexpExpr{
Pattern: ast.NewValueExpr(v.pattern),
Expr: ast.NewValueExpr(v.input),
}
match, err := Eval(s.ctx, pattern)
c.Assert(err, IsNil)
c.Assert(match, testutil.DatumEquals, types.NewDatum(v.match), Commentf("%v", v))
}
}
示例5: TestRegexp
func (s *testEvaluatorSuite) TestRegexp(c *C) {
tbl := []struct {
pattern string
input string
match int64
}{
{"^$", "a", 0},
{"a", "a", 1},
{"a", "b", 0},
{"aA", "aA", 1},
{".", "a", 1},
{"^.$", "ab", 0},
{"..", "b", 0},
{".ab", "aab", 1},
{".*", "abcd", 1},
}
ctx := mock.NewContext()
for _, v := range tbl {
pattern := &ast.PatternRegexpExpr{
Pattern: ast.NewValueExpr(v.pattern),
Expr: ast.NewValueExpr(v.input),
}
match, err := Eval(ctx, pattern)
c.Assert(err, IsNil)
c.Assert(match, Equals, v.match, Commentf("%v", v))
}
}
示例6: TestTrim
func (s *testEvaluatorSuite) TestTrim(c *C) {
tbl := []struct {
str interface{}
remstr interface{}
dir ast.TrimDirectionType
result interface{}
}{
{" bar ", nil, ast.TrimBothDefault, "bar"},
{"xxxbarxxx", "x", ast.TrimLeading, "barxxx"},
{"xxxbarxxx", "x", ast.TrimBoth, "bar"},
{"barxxyz", "xyz", ast.TrimTrailing, "barx"},
{nil, "xyz", ast.TrimBoth, nil},
{1, 2, ast.TrimBoth, "1"},
{" \t\rbar\n ", nil, ast.TrimBothDefault, "bar"},
}
ctx := mock.NewContext()
for _, v := range tbl {
f := &ast.FuncTrimExpr{
Str: ast.NewValueExpr(v.str),
Direction: v.dir,
}
if v.remstr != nil {
f.RemStr = ast.NewValueExpr(v.remstr)
}
r, err := Eval(ctx, f)
c.Assert(err, IsNil)
c.Assert(r, Equals, v.result)
}
}
示例7: TestCast
func (s *testEvaluatorSuite) TestCast(c *C) {
defer testleak.AfterTest(c)()
f := types.NewFieldType(mysql.TypeLonglong)
expr := &ast.FuncCastExpr{
Expr: ast.NewValueExpr(1),
Tp: f,
}
ast.SetFlag(expr)
v, err := Eval(s.ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, testutil.DatumEquals, types.NewDatum(int64(1)))
f.Flag |= mysql.UnsignedFlag
v, err = Eval(s.ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, testutil.DatumEquals, types.NewDatum(uint64(1)))
f.Tp = mysql.TypeString
f.Charset = charset.CharsetBin
v, err = Eval(s.ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, testutil.DatumEquals, types.NewDatum([]byte("1")))
f.Tp = mysql.TypeString
f.Charset = "utf8"
v, err = Eval(s.ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, testutil.DatumEquals, types.NewDatum("1"))
expr.Expr = ast.NewValueExpr(nil)
v, err = Eval(s.ctx, expr)
c.Assert(err, IsNil)
c.Assert(v.Kind(), Equals, types.KindNull)
}
示例8: TestBinopBitop
func (s *testEvaluatorSuite) TestBinopBitop(c *C) {
defer testleak.AfterTest(c)()
tbl := []struct {
lhs interface{}
op opcode.Op
rhs interface{}
ret interface{}
}{
{1, opcode.And, 1, 1},
{1, opcode.Or, 1, 1},
{1, opcode.Xor, 1, 0},
{1, opcode.LeftShift, 1, 2},
{2, opcode.RightShift, 1, 1},
{nil, opcode.And, 1, nil},
{1, opcode.And, nil, nil},
{nil, opcode.Or, 1, nil},
{nil, opcode.Xor, 1, nil},
{nil, opcode.LeftShift, 1, nil},
{nil, opcode.RightShift, 1, nil},
}
for _, t := range tbl {
expr := &ast.BinaryOperationExpr{Op: t.op, L: ast.NewValueExpr(t.lhs), R: ast.NewValueExpr(t.rhs)}
v, err := Eval(s.ctx, expr)
c.Assert(err, IsNil)
switch x := t.ret.(type) {
case nil:
c.Assert(v.Kind(), Equals, types.KindNull)
case int:
c.Assert(v, testutil.DatumEquals, types.NewDatum(uint64(x)))
}
}
}
示例9: TestEvaluatedFlag
func (s *testEvaluatorSuite) TestEvaluatedFlag(c *C) {
l := ast.NewValueExpr(int64(1))
r := ast.NewValueExpr(int64(2))
b := &ast.BinaryOperationExpr{L: l, R: r, Op: opcode.Plus}
ast.SetFlag(b)
c.Assert(ast.IsPreEvaluable(b), Equals, true)
d, err := Eval(s.ctx, b)
c.Assert(ast.IsEvaluated(b), Equals, true)
c.Assert(err, IsNil)
c.Assert(d, testutil.DatumEquals, types.NewIntDatum(3))
funcCall := &ast.FuncCallExpr{
FnName: model.NewCIStr("abs"),
Args: []ast.ExprNode{ast.NewValueExpr(int(-1))},
}
b = &ast.BinaryOperationExpr{L: funcCall, R: r, Op: opcode.Plus}
ast.ResetEvaluatedFlag(b)
ast.SetFlag(b)
c.Assert(ast.IsPreEvaluable(b), Equals, true)
d, err = Eval(s.ctx, b)
c.Assert(ast.IsEvaluated(b), Equals, false)
c.Assert(err, IsNil)
c.Assert(d, testutil.DatumEquals, types.NewIntDatum(3))
rf := &ast.ResultField{Expr: ast.NewValueExpr(int64(1))}
colExpr := &ast.ColumnNameExpr{Refer: rf}
b = &ast.BinaryOperationExpr{L: colExpr, R: r, Op: opcode.Plus}
ast.ResetEvaluatedFlag(b)
ast.SetFlag(b)
c.Assert(ast.IsPreEvaluable(b), Equals, false)
d, err = Eval(s.ctx, b)
c.Assert(ast.IsEvaluated(b), Equals, false)
c.Assert(err, IsNil)
c.Assert(d, testutil.DatumEquals, types.NewIntDatum(3))
}
示例10: TestTrim
func (s *testEvaluatorSuite) TestTrim(c *C) {
defer testleak.AfterTest(c)()
tbl := []struct {
str interface{}
remstr interface{}
dir ast.TrimDirectionType
result interface{}
}{
{" bar ", nil, ast.TrimBothDefault, "bar"},
{"xxxbarxxx", "x", ast.TrimLeading, "barxxx"},
{"xxxbarxxx", "x", ast.TrimBoth, "bar"},
{"barxxyz", "xyz", ast.TrimTrailing, "barx"},
{nil, "xyz", ast.TrimBoth, nil},
{1, 2, ast.TrimBoth, "1"},
{" \t\rbar\n ", nil, ast.TrimBothDefault, "bar"},
}
ctx := mock.NewContext()
for _, v := range tbl {
f := &ast.FuncCallExpr{
FnName: model.NewCIStr("TRIM"),
Args: []ast.ExprNode{
ast.NewValueExpr(v.str),
ast.NewValueExpr(v.remstr),
ast.NewValueExpr(v.dir),
},
}
r, err := Eval(ctx, f)
c.Assert(err, IsNil)
c.Assert(r, testutil.DatumEquals, types.NewDatum(v.result))
}
for _, v := range []struct {
str, result interface{}
fn string
}{
{" ", "", "LTRIM"},
{" ", "", "RTRIM"},
{"foo0", "foo0", "LTRIM"},
{"bar0", "bar0", "RTRIM"},
{" foo1", "foo1", "LTRIM"},
{"bar1 ", "bar1", "RTRIM"},
{spaceChars + "foo2 ", "foo2 ", "LTRIM"},
{" bar2" + spaceChars, " bar2", "RTRIM"},
{nil, nil, "LTRIM"},
{nil, nil, "RTRIM"},
} {
f := &ast.FuncCallExpr{
FnName: model.NewCIStr(v.fn),
Args: []ast.ExprNode{ast.NewValueExpr(v.str)},
}
r, err := Eval(ctx, f)
c.Assert(err, IsNil)
c.Assert(r, testutil.DatumEquals, types.NewDatum(v.result))
}
}
示例11: TestExtract
func (s *testEvaluatorSuite) TestExtract(c *C) {
defer testleak.AfterTest(c)()
str := "2011-11-11 10:10:10.123456"
tbl := []struct {
Unit string
Expect int64
}{
{"MICROSECOND", 123456},
{"SECOND", 10},
{"MINUTE", 10},
{"HOUR", 10},
{"DAY", 11},
{"WEEK", 45},
{"MONTH", 11},
{"QUARTER", 4},
{"YEAR", 2011},
{"SECOND_MICROSECOND", 10123456},
{"MINUTE_MICROSECOND", 1010123456},
{"MINUTE_SECOND", 1010},
{"HOUR_MICROSECOND", 101010123456},
{"HOUR_SECOND", 101010},
{"HOUR_MINUTE", 1010},
{"DAY_MICROSECOND", 11101010123456},
{"DAY_SECOND", 11101010},
{"DAY_MINUTE", 111010},
{"DAY_HOUR", 1110},
{"YEAR_MONTH", 201111},
}
ctx := mock.NewContext()
for _, t := range tbl {
e := &ast.FuncCallExpr{
FnName: model.NewCIStr("EXTRACT"),
Args: []ast.ExprNode{ast.NewValueExpr(t.Unit), ast.NewValueExpr(str)},
}
v, err := Eval(ctx, e)
c.Assert(err, IsNil)
c.Assert(v, testutil.DatumEquals, types.NewDatum(t.Expect))
}
// Test nil
e := &ast.FuncCallExpr{
FnName: model.NewCIStr("EXTRACT"),
Args: []ast.ExprNode{ast.NewValueExpr("SECOND"), ast.NewValueExpr(nil)},
}
v, err := Eval(ctx, e)
c.Assert(err, IsNil)
c.Assert(v.Kind(), Equals, types.KindNull)
}
示例12: TestExtract
func (s *testEvaluatorSuite) TestExtract(c *C) {
str := "2011-11-11 10:10:10.123456"
tbl := []struct {
Unit string
Expect int64
}{
{"MICROSECOND", 123456},
{"SECOND", 10},
{"MINUTE", 10},
{"HOUR", 10},
{"DAY", 11},
{"WEEK", 45},
{"MONTH", 11},
{"QUARTER", 4},
{"YEAR", 2011},
{"SECOND_MICROSECOND", 10123456},
{"MINUTE_MICROSECOND", 1010123456},
{"MINUTE_SECOND", 1010},
{"HOUR_MICROSECOND", 101010123456},
{"HOUR_SECOND", 101010},
{"HOUR_MINUTE", 1010},
{"DAY_MICROSECOND", 11101010123456},
{"DAY_SECOND", 11101010},
{"DAY_MINUTE", 111010},
{"DAY_HOUR", 1110},
{"YEAR_MONTH", 201111},
}
ctx := mock.NewContext()
for _, t := range tbl {
e := &ast.FuncExtractExpr{
Unit: t.Unit,
Date: ast.NewValueExpr(str),
}
v, err := Eval(ctx, e)
c.Assert(err, IsNil)
c.Assert(v, Equals, t.Expect)
}
// Test nil
e := &ast.FuncExtractExpr{
Unit: "SECOND",
Date: ast.NewValueExpr(nil),
}
v, err := Eval(ctx, e)
c.Assert(err, IsNil)
c.Assert(v, IsNil)
}
示例13: TestColumnNameExpr
func (s *testEvaluatorSuite) TestColumnNameExpr(c *C) {
ctx := mock.NewContext()
value1 := ast.NewValueExpr(1)
rf := &ast.ResultField{Expr: value1}
expr := &ast.ColumnNameExpr{Refer: rf}
result, err := Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(result, Equals, 1)
value2 := ast.NewValueExpr(2)
rf.Expr = value2
result, err = Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(result, Equals, 2)
}
示例14: TestIsCurrentTimeExpr
func (s *testEvaluatorSuite) TestIsCurrentTimeExpr(c *C) {
v := IsCurrentTimeExpr(ast.NewValueExpr("abc"))
c.Assert(v, IsFalse)
v = IsCurrentTimeExpr(&ast.FuncCallExpr{FnName: model.NewCIStr("CURRENT_TIMESTAMP")})
c.Assert(v, IsTrue)
}
示例15: TestConvert
func (s *testEvaluatorSuite) TestConvert(c *C) {
defer testleak.AfterTest(c)()
ctx := mock.NewContext()
tbl := []struct {
str string
cs string
result string
}{
{"haha", "utf8", "haha"},
{"haha", "ascii", "haha"},
}
for _, v := range tbl {
f := &ast.FuncCallExpr{
FnName: model.NewCIStr("CONVERT"),
Args: []ast.ExprNode{
ast.NewValueExpr(v.str),
ast.NewValueExpr(v.cs),
},
}
r, err := Eval(ctx, f)
c.Assert(err, IsNil)
c.Assert(r.Kind(), Equals, types.KindString)
c.Assert(r.GetString(), Equals, v.result)
}
// Test case for error
errTbl := []struct {
str interface{}
cs string
result string
}{
{"haha", "wrongcharset", "haha"},
}
for _, v := range errTbl {
f := &ast.FuncCallExpr{
FnName: model.NewCIStr("CONVERT"),
Args: []ast.ExprNode{
ast.NewValueExpr(v.str),
ast.NewValueExpr(v.cs),
},
}
_, err := Eval(ctx, f)
c.Assert(err, NotNil)
}
}