コード例 #1
0
ファイル: select_list.go プロジェクト: no2key/tidb
// UpdateAggFields adds aggregate function resultfield to select result field list.
func (s *SelectList) UpdateAggFields(expr expression.Expression, tableFields []*field.ResultField) (expression.Expression, error) {
	// For aggregate function, the name can be in table or select list.
	names := expressions.MentionedColumns(expr)

	for _, name := range names {
		if field.ContainFieldName(name, tableFields, field.DefaultFieldFlag) {
			continue
		}

		if field.ContainFieldName(name, s.ResultFields, field.DefaultFieldFlag) {
			continue
		}

		return nil, errors.Errorf("Unknown column '%s'", name)
	}

	// We must add aggregate function to hidden select list
	// and use a position expression to fetch its value later.
	exprName := expr.String()
	if !field.ContainFieldName(exprName, s.ResultFields, field.CheckFieldFlag) {
		f := &field.Field{Expr: expr, Name: exprName}
		resultField := &field.ResultField{Name: exprName}
		s.AddField(f, resultField)

		return &expressions.Position{N: len(s.Fields), Name: exprName}, nil
	}

	return nil, nil
}
コード例 #2
0
ファイル: having.go プロジェクト: no2key/tidb
// CheckAndUpdateSelectList checks having fields validity and set hidden fields to selectList.
func (r *HavingRset) CheckAndUpdateSelectList(selectList *plans.SelectList, groupBy []expression.Expression, tableFields []*field.ResultField) error {
	if err := expressions.CheckOneColumn(r.Expr); err != nil {
		return errors.Trace(err)
	}

	if expressions.ContainAggregateFunc(r.Expr) {
		expr, err := selectList.UpdateAggFields(r.Expr, tableFields)
		if err != nil {
			return errors.Errorf("%s in 'having clause'", err.Error())
		}

		r.Expr = expr
	} else {
		// having can only contain group by column and select list, e.g,
		// `select c1 from t group by c2 having c3 > 0` is invalid,
		// because c3 is not in group by and select list.
		names := expressions.MentionedColumns(r.Expr)
		for _, name := range names {
			found := false

			// check name whether in select list.
			// notice that `select c1 as c2 from t group by c1, c2, c3 having c2 > c3`,
			// will use t.c2 not t.c1 here.
			if field.ContainFieldName(name, selectList.ResultFields, field.OrgFieldNameFlag) {
				continue
			}
			if field.ContainFieldName(name, selectList.ResultFields, field.FieldNameFlag) {
				if field.ContainFieldName(name, tableFields, field.OrgFieldNameFlag) {
					selectList.CloneHiddenField(name, tableFields)
				}
				continue
			}

			// check name whether in group by.
			// group by must only have column name, e.g,
			// `select c1 from t group by c2 having c2 > 0` is valid,
			// but `select c1 from t group by c2 + 1 having c2 > 0` is invalid.
			for _, by := range groupBy {
				if !field.CheckFieldsEqual(name, by.String()) {
					continue
				}

				// if name is not in table fields, it will get an unknown field error in GroupByRset,
				// so no need to check return value.
				selectList.CloneHiddenField(name, tableFields)

				found = true
				break
			}

			if !found {
				return errors.Errorf("Unknown column '%s' in 'having clause'", name)
			}
		}
	}

	return nil
}
コード例 #3
0
ファイル: orderby.go プロジェクト: szctop/tidb
// CheckAndUpdateSelectList checks order by fields validity and set hidden fields to selectList.
func (r *OrderByRset) CheckAndUpdateSelectList(selectList *plans.SelectList, tableFields []*field.ResultField) error {
	for i, v := range r.By {
		if expressions.ContainAggregateFunc(v.Expr) {
			expr, err := selectList.UpdateAggFields(v.Expr, tableFields)
			if err != nil {
				return errors.Errorf("%s in 'order clause'", err.Error())
			}

			r.By[i].Expr = expr
		} else {
			names := expressions.MentionedColumns(v.Expr)
			for _, name := range names {
				// try to find in select list
				// TODO: mysql has confused result for this, see #555.
				// now we use select list then order by, later we should make it easier.
				if field.ContainFieldName(name, selectList.ResultFields, field.CheckFieldFlag) {
					// check ambiguous fields, like `select c1 as c2, c2 from t order by c2`.
					if err := field.CheckAmbiguousField(name, selectList.ResultFields, field.DefaultFieldFlag); err != nil {
						return errors.Errorf("Column '%s' in order statement is ambiguous", name)
					}
					continue
				}

				if !selectList.CloneHiddenField(name, tableFields) {
					return errors.Errorf("Unknown column '%s' in 'order clause'", name)
				}
			}
		}
	}

	return nil
}
コード例 #4
0
ファイル: select_list.go プロジェクト: gaohf/tidb
// CloneHiddenField checks and clones field and result field from table fields,
// and adds them to hidden field of select list.
func (s *SelectList) CloneHiddenField(name string, tableFields []*field.ResultField) bool {
	// Check and add hidden field.
	if field.ContainFieldName(name, tableFields, field.CheckFieldFlag) {
		resultField, _ := field.CloneFieldByName(name, tableFields, field.CheckFieldFlag)
		f := &field.Field{
			Expr: &expression.Ident{
				CIStr: resultField.ColumnInfo.Name,
			},
		}
		s.AddField(f, resultField)
		return true
	}

	return false
}
コード例 #5
0
ファイル: orderby.go プロジェクト: gaohf/tidb
// CheckAndUpdateSelectList checks order by fields validity and set hidden fields to selectList.
func (r *OrderByRset) CheckAndUpdateSelectList(selectList *plans.SelectList, tableFields []*field.ResultField) error {
	for i, v := range r.By {
		if expression.ContainAggregateFunc(v.Expr) {
			expr, err := selectList.UpdateAggFields(v.Expr, tableFields)
			if err != nil {
				return errors.Errorf("%s in 'order clause'", err.Error())
			}

			r.By[i].Expr = expr
		} else {
			if _, err := selectList.CheckReferAmbiguous(v.Expr); err != nil {
				return errors.Errorf("Column '%s' in order statement is ambiguous", v.Expr)
			}

			// TODO: check more ambiguous case
			// Order by ambiguous rule:
			//	select c1 as a, c2 as a from t order by a is ambiguous
			//	select c1 as a, c2 as a from t order by a + 1 is ambiguous
			//	select c1 as c2, c2 from t order by c2 is ambiguous
			//	select c1 as c2, c2 from t order by c2 + 1 is ambiguous

			// TODO: use vistor to refactor all and combine following plan check.
			names := expression.MentionedColumns(v.Expr)
			for _, name := range names {
				// try to find in select list
				// TODO: mysql has confused result for this, see #555.
				// now we use select list then order by, later we should make it easier.
				if field.ContainFieldName(name, selectList.ResultFields, field.CheckFieldFlag) {
					continue
				}

				if !selectList.CloneHiddenField(name, tableFields) {
					return errors.Errorf("Unknown column '%s' in 'order clause'", name)
				}
			}
		}
	}

	return nil
}
コード例 #6
0
ファイル: groupby.go プロジェクト: ninefive/tidb
// Plan gets GroupByDefaultPlan.
func (r *GroupByRset) Plan(ctx context.Context) (plan.Plan, error) {
	fields := r.SelectList.Fields
	resultfields := r.SelectList.ResultFields
	srcFields := r.Src.GetFields()

	r.SelectList.AggFields = GetAggFields(fields)
	aggFields := r.SelectList.AggFields

	for i, e := range r.By {
		if v, ok := e.(expressions.Value); ok {
			var position int
			switch u := v.Val.(type) {
			case int64:
				position = int(u)
			case uint64:
				position = int(u)
			default:
				continue
			}

			if position < 1 || position > len(fields) {
				return nil, errors.Errorf("Unknown column '%d' in 'group statement'", position)
			}

			index := position - 1
			if _, ok := aggFields[index]; ok {
				return nil, errors.Errorf("Can't group on '%s'", fields[index].Name)
			}

			// use Position expression for the associated field.
			r.By[i] = &expressions.Position{N: position}
		} else {
			names := expressions.MentionedColumns(e)
			for _, name := range names {
				if field.ContainFieldName(name, srcFields, field.DefaultFieldFlag) {
					// check whether column is qualified, like `select t.c1 c1, t.c2 from t group by t.c1, t.c2`
					// no need to check ambiguous field.
					if expressions.IsQualified(name) {
						continue
					}

					// check ambiguous fields, like `select c1 as c2, c2 from t group by c2`.
					if err := field.CheckAmbiguousField(name, resultfields, field.DefaultFieldFlag); err == nil {
						continue
					}
				}

				// check reference to group function name
				indices := field.GetFieldIndex(name, fields[0:r.SelectList.HiddenFieldOffset], field.CheckFieldFlag)
				if len(indices) > 1 {
					// check ambiguous fields, like `select c1 as a, c2 as a from t group by a`,
					// notice that `select c2 as c2, c2 as c2 from t group by c2;` is valid.
					if r.HasAmbiguousField(indices, fields[0:r.SelectList.HiddenFieldOffset]) {
						return nil, errors.Errorf("Column '%s' in group statement is ambiguous", name)
					}
				} else if len(indices) == 1 {
					// check reference to aggregate function, like `select c1, count(c1) as b from t group by b + 1`.
					index := indices[0]
					if _, ok := aggFields[index]; ok {
						return nil, errors.Errorf("Reference '%s' not supported (reference to group function)", name)
					}
				}
			}

			// group by should be an expression, a qualified field name or a select field position,
			// but can not contain any aggregate function.
			if e := r.By[i]; expressions.ContainAggregateFunc(e) {
				return nil, errors.Errorf("group by cannot contain aggregate function %s", e.String())
			}
		}
	}

	return &plans.GroupByDefaultPlan{By: r.By, Src: r.Src, SelectList: r.SelectList}, nil
}