示例#1
0
文件: orderby_test.go 项目: npk/tidb
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: &expressions.Ident{CIStr: model.NewCIStr(name)}, Name: name}
	}

	selectList := &plans.SelectList{
		HiddenFieldOffset: len(resultFields),
		ResultFields:      resultFields,
		Fields:            fields,
	}

	expr := &expressions.Ident{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].Name = "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 := expressions.NewCall("count", []expression.Expression{expressions.Value{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 = expressions.NewCall("count", []expression.Expression{&expressions.Ident{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 = &expressions.Ident{model.NewCIStr("xxx")}
	selectList.Fields[1].Name = "name"
	selectList.ResultFields[1].Name = "name"

	err = r.CheckAndUpdateSelectList(selectList, resultFields)
	c.Assert(err, NotNil)
}
示例#2
0
func (s *testHelperSuite) SetUpSuite(c *C) {
	fldx := &field.Field{Expr: &expressions.Ident{CIStr: model.NewCIStr("name")}, Name: "a"}
	expr, err := expressions.NewCall("count", []expression.Expression{expressions.Value{Val: 1}}, false)
	c.Assert(err, IsNil)
	fldy := &field.Field{Expr: expr}

	s.fields = []*field.Field{fldx, fldy}
}
示例#3
0
文件: groupby_test.go 项目: npk/tidb
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] = expressions.Value{"abc"}

	_, err = s.r.Plan(nil)
	c.Assert(err, IsNil)

	// `select id, name from t group by 1`
	s.r.By[0] = expressions.Value{int64(1)}

	_, err = s.r.Plan(nil)
	c.Assert(err, IsNil)

	s.r.By[0] = expressions.Value{uint64(1)}

	_, err = s.r.Plan(nil)
	c.Assert(err, IsNil)

	// `select id, name from t group by 0`
	s.r.By[0] = expressions.Value{int64(0)}

	_, err = s.r.Plan(nil)
	c.Assert(err, NotNil)

	fldExpr, err := expressions.NewCall("count", []expression.Expression{expressions.Value{1}}, false)
	c.Assert(err, IsNil)

	// `select count(1) as a, name from t group by 1`
	fld := &field.Field{Expr: fldExpr, Name: "a"}
	s.r.SelectList.Fields[0] = fld

	s.r.By[0] = expressions.Value{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] = &expressions.Ident{model.NewCIStr("name")}

	fldx := &field.Field{Expr: &expressions.Ident{CIStr: model.NewCIStr("id")}, Name: "name"}
	fldy := &field.Field{Expr: &expressions.Ident{CIStr: model.NewCIStr("name")}, Name: "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 := expressions.NewBinaryOperation(opcode.Plus, &expressions.Ident{model.NewCIStr("a")}, expressions.Value{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)
}
示例#4
0
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: &expressions.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{&expressions.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 := expressions.NewBinaryOperation(opcode.Plus, &expressions.Ident{CIStr: model.NewCIStr("id")}, expressions.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 := expressions.NewCall("count", []expression.Expression{expressions.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 = expressions.NewCall("count", []expression.Expression{&expressions.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 = expressions.NewBinaryOperation(opcode.GT, &expressions.Ident{CIStr: model.NewCIStr("xxx")}, expressions.Value{Val: 1})

	s.r.Expr = expr

	err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields)
	c.Assert(err, NotNil)

	s.r.Expr = &expressions.Row{Values: []expression.Expression{expressions.Value{Val: 1}, expressions.Value{Val: 1}}}
	err = s.r.CheckAndUpdateSelectList(selectList, groupBy, resultFields)
	c.Assert(err, NotNil)
}