func (s *testOrderByRsetSuite) TestOrderByRsetCheckAndUpdateSelectList(c *C) { resultFields := s.r.Src.GetFields() fields := make([]*field.Field, len(resultFields)) for i, resultField := range resultFields { name := resultField.Name fields[i] = &field.Field{Expr: &expression.Ident{CIStr: model.NewCIStr(name)}} } selectList := &plans.SelectList{ HiddenFieldOffset: len(resultFields), ResultFields: resultFields, Fields: fields, } expr := &expression.Ident{CIStr: model.NewCIStr("id")} orderByItem := OrderByItem{Expr: expr, Asc: true} by := []OrderByItem{orderByItem} r := &OrderByRset{By: by, SelectList: selectList} // `select id, name from t order by id` err := r.CheckAndUpdateSelectList(selectList, resultFields) c.Assert(err, IsNil) // `select id, name as id from t order by id` selectList.Fields[1].AsName = "id" selectList.ResultFields[1].Name = "id" err = r.CheckAndUpdateSelectList(selectList, resultFields) c.Assert(err, NotNil) // `select id, name from t order by count(1) > 1` aggExpr, err := expression.NewCall("count", []expression.Expression{expression.Value{Val: 1}}, false) c.Assert(err, IsNil) r.By[0].Expr = aggExpr err = r.CheckAndUpdateSelectList(selectList, resultFields) c.Assert(err, IsNil) // `select id, name from t order by count(xxx) > 1` aggExpr, err = expression.NewCall("count", []expression.Expression{&expression.Ident{CIStr: model.NewCIStr("xxx")}}, false) c.Assert(err, IsNil) r.By[0].Expr = aggExpr err = r.CheckAndUpdateSelectList(selectList, resultFields) c.Assert(err, NotNil) // `select id, name from t order by xxx` r.By[0].Expr = &expression.Ident{CIStr: model.NewCIStr("xxx")} selectList.Fields[1].AsName = "name" selectList.ResultFields[1].Name = "name" err = r.CheckAndUpdateSelectList(selectList, resultFields) c.Assert(err, NotNil) }
func (s *testHavingRsetSuite) TestHavingRsetCheckAndUpdateSelectList(c *C) { resultFields := s.r.Src.GetFields() selectList := s.r.SelectList groupBy := []expression.Expression{} // `select id, name from t having id > 1` err := s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, IsNil) // `select name from t group by id having id > 1` selectList.ResultFields = selectList.ResultFields[1:] selectList.Fields = selectList.Fields[1:] groupBy = []expression.Expression{&expression.Ident{CIStr: model.NewCIStr("id")}} err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, IsNil) // `select name from t group by id + 1 having id > 1` expr := expression.NewBinaryOperation(opcode.Plus, &expression.Ident{CIStr: model.NewCIStr("id")}, expression.Value{Val: 1}) groupBy = []expression.Expression{expr} err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, IsNil) // `select name from t group by id + 1 having count(1) > 1` aggExpr, err := expression.NewCall("count", []expression.Expression{expression.Value{Val: 1}}, false) c.Assert(err, IsNil) s.r.Expr = aggExpr err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, IsNil) // `select name from t group by id + 1 having count(xxx) > 1` aggExpr, err = expression.NewCall("count", []expression.Expression{&expression.Ident{CIStr: model.NewCIStr("xxx")}}, false) c.Assert(err, IsNil) s.r.Expr = aggExpr err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, NotNil) // `select name from t group by id having xxx > 1` expr = expression.NewBinaryOperation(opcode.GT, &expression.Ident{CIStr: model.NewCIStr("xxx")}, expression.Value{Val: 1}) s.r.Expr = expr err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, NotNil) }
func (s *testHelperSuite) SetUpSuite(c *C) { fldx := &field.Field{Expr: &expression.Ident{CIStr: model.NewCIStr("name")}, AsName: "a"} expr, err := expression.NewCall("count", []expression.Expression{expression.Value{Val: 1}}, false) c.Assert(err, IsNil) fldy := &field.Field{Expr: expr} s.fields = []*field.Field{fldx, fldy} }
func (s *testVisitorSuite) TestBase(c *C) { val := expression.Value{Val: 1} visitor := expression.NewIdentEvalVisitor() var exp expression.Expression exp = &expression.Between{Expr: val, Left: val, Right: val} exp.Accept(visitor) exp = expression.NewBinaryOperation(opcode.And, val, val) exp.Accept(visitor) exp, _ = expression.NewCall("avg", []expression.Expression{val}, true) exp.Accept(visitor) rows := [][]interface{}{{1}} sq := newMockSubQuery(rows, []string{"a"}) exp = expression.NewCompareSubQuery(opcode.EQ, val, sq, true) exp.Accept(visitor) exp = &expression.Default{Name: "a"} exp.Accept(visitor) exp = expression.NewExistsSubQuery(sq) exp.Accept(visitor) when := &expression.WhenClause{Expr: val, Result: val} exp = &expression.FunctionCase{ WhenClauses: []*expression.WhenClause{when}, Value: val, ElseClause: val, } exp.Accept(visitor) exp = &expression.FunctionCast{Expr: val, Tp: types.NewFieldType(mysql.TypeLong), FunctionType: expression.ConvertFunction} exp.Accept(visitor) exp = &expression.FunctionConvert{Expr: val, Charset: "utf8"} exp.Accept(visitor) exp = &expression.FunctionSubstring{StrExpr: expression.Value{Val: "string"}, Pos: expression.Value{Val: 0}, Len: val} exp.Accept(visitor) exp = &expression.IsNull{Expr: val} exp.Accept(visitor) exp = &expression.IsTruth{Expr: val} exp.Accept(visitor) exp = &expression.ParamMarker{Expr: val} exp.Accept(visitor) exp = &expression.PatternIn{Expr: val, List: []expression.Expression{val}} exp.Accept(visitor) exp = &expression.PatternLike{Expr: val, Pattern: val} exp.Accept(visitor) exp = &expression.PatternRegexp{Expr: val, Pattern: val} exp.Accept(visitor) exp = &expression.PExpr{Expr: val} exp.Accept(visitor) exp = &expression.Position{Name: "a"} exp.Accept(visitor) exp = &expression.Row{Values: []expression.Expression{val}} exp.Accept(visitor) exp = &expression.UnaryOperation{V: val} exp.Accept(visitor) exp = &expression.Values{CIStr: model.NewCIStr("a")} exp.Accept(visitor) exp = &expression.Variable{Name: "a"} exp.Accept(visitor) }
func (s *testGroupByRsetSuite) TestGroupByRsetPlan(c *C) { // `select id, name from t group by name` p, err := s.r.Plan(nil) c.Assert(err, IsNil) _, ok := p.(*plans.GroupByDefaultPlan) c.Assert(ok, IsTrue) // `select id, name from t group by abc` s.r.By[0] = expression.Value{Val: "abc"} _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t group by 1` s.r.By[0] = expression.Value{Val: int64(1)} _, err = s.r.Plan(nil) c.Assert(err, IsNil) s.r.By[0] = expression.Value{Val: uint64(1)} _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t group by 0` s.r.By[0] = expression.Value{Val: int64(0)} _, err = s.r.Plan(nil) c.Assert(err, NotNil) fldExpr, err := expression.NewCall("count", []expression.Expression{expression.Value{Val: 1}}, false) c.Assert(err, IsNil) // `select count(1) as a, name from t group by 1` fld := &field.Field{Expr: fldExpr, AsName: "a"} s.r.SelectList.Fields[0] = fld s.r.By[0] = expression.Value{Val: int64(1)} _, err = s.r.Plan(nil) c.Assert(err, NotNil) // check ambiguous field, like `select id as name, name from t group by name` s.r.By[0] = &expression.Ident{CIStr: model.NewCIStr("name")} fldx := &field.Field{Expr: &expression.Ident{CIStr: model.NewCIStr("id")}, AsName: "name"} fldy := &field.Field{Expr: &expression.Ident{CIStr: model.NewCIStr("name")}, AsName: "name"} s.r.SelectList.Fields = []*field.Field{fldx, fldy} s.r.SelectList.ResultFields[0].Name = "name" s.r.SelectList.ResultFields[1].Name = "name" p, err = s.r.Plan(nil) c.Assert(err, NotNil) // check aggregate function reference, like `select count(1) as a, name from t group by a + 1` expr := expression.NewBinaryOperation(opcode.Plus, &expression.Ident{CIStr: model.NewCIStr("a")}, expression.Value{Val: 1}) s.r.By[0] = expr s.r.SelectList.Fields[0] = fld s.r.SelectList.ResultFields[0].Col.Name = model.NewCIStr("count(id)") s.r.SelectList.ResultFields[0].Name = "a" _, err = s.r.Plan(nil) c.Assert(err, NotNil) // check contain aggregate function, like `select count(1) as a, name from t group by count(1)` s.r.By[0] = fldExpr _, err = s.r.Plan(nil) c.Assert(err, NotNil) }
func (s *testHavingRsetSuite) TestHavingRsetCheckAndUpdateSelectList(c *C) { resultFields := s.r.Src.GetFields() fields := make([]*field.Field, len(resultFields)) for i, resultField := range resultFields { name := resultField.Name fields[i] = &field.Field{Expr: &expression.Ident{CIStr: model.NewCIStr(name)}, Name: name} } selectList := &plans.SelectList{ HiddenFieldOffset: len(resultFields), ResultFields: resultFields, Fields: fields, } groupBy := []expression.Expression{} // `select id, name from t having id > 1` err := s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, IsNil) // `select name from t group by id having id > 1` selectList.ResultFields = selectList.ResultFields[1:] selectList.Fields = selectList.Fields[1:] groupBy = []expression.Expression{&expression.Ident{CIStr: model.NewCIStr("id")}} err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, IsNil) // `select name from t group by id + 1 having id > 1` expr := expression.NewBinaryOperation(opcode.Plus, &expression.Ident{CIStr: model.NewCIStr("id")}, expression.Value{Val: 1}) groupBy = []expression.Expression{expr} err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, IsNil) // `select name from t group by id + 1 having count(1) > 1` aggExpr, err := expression.NewCall("count", []expression.Expression{expression.Value{Val: 1}}, false) c.Assert(err, IsNil) s.r.Expr = aggExpr err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, IsNil) // `select name from t group by id + 1 having count(xxx) > 1` aggExpr, err = expression.NewCall("count", []expression.Expression{&expression.Ident{CIStr: model.NewCIStr("xxx")}}, false) c.Assert(err, IsNil) s.r.Expr = aggExpr err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, NotNil) // `select name from t group by id having xxx > 1` expr = expression.NewBinaryOperation(opcode.GT, &expression.Ident{CIStr: model.NewCIStr("xxx")}, expression.Value{Val: 1}) s.r.Expr = expr err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields) c.Assert(err, NotNil) }