Exemplo n.º 1
0
func (v *typeInferrer) aggregateFunc(x *ast.AggregateFuncExpr) {
	name := strings.ToLower(x.F)
	switch name {
	case "count":
		ft := types.NewFieldType(mysql.TypeLonglong)
		ft.Flen = 21
		x.SetType(ft)
	}
}
Exemplo n.º 2
0
func (e *Evaluator) evalAggAvg(v *ast.AggregateFuncExpr) {
	ctx := v.GetContext()
	switch x := ctx.Value.(type) {
	case float64:
		ctx.Value = x / float64(ctx.Count)
	case mysql.Decimal:
		ctx.Value = x.Div(mysql.NewDecimalFromUint(uint64(ctx.Count), 0))
	}
	v.SetValue(ctx.Value)
}
Exemplo n.º 3
0
func (e *Evaluator) aggregateFunc(v *ast.AggregateFuncExpr) bool {
	idx := e.aggregateMap[v]
	aggr := e.aggregators[idx]
	if e.aggregateDone {
		v.SetValue(aggr.Output())
		return true
	}
	// TODO: currently only single argument aggregate functions are supported.
	e.err = aggr.Input(v.Args[0].GetValue())
	return e.err == nil
}
Exemplo n.º 4
0
func (v *typeInferrer) aggregateFunc(x *ast.AggregateFuncExpr) {
	name := strings.ToLower(x.F)
	switch name {
	case ast.AggFuncCount:
		ft := types.NewFieldType(mysql.TypeLonglong)
		ft.Flen = 21
		ft.Charset = charset.CharsetBin
		ft.Collate = charset.CollationBin
		x.SetType(ft)
	case ast.AggFuncMax, ast.AggFuncMin:
		x.SetType(x.Args[0].GetType())
	case ast.AggFuncSum, ast.AggFuncAvg:
		ft := types.NewFieldType(mysql.TypeNewDecimal)
		ft.Charset = charset.CharsetBin
		ft.Collate = charset.CollationBin
		x.SetType(ft)
	case ast.AggFuncGroupConcat:
		ft := types.NewFieldType(mysql.TypeVarString)
		ft.Charset = v.defaultCharset
		cln, err := charset.GetDefaultCollation(v.defaultCharset)
		if err != nil {
			v.err = err
		}
		ft.Collate = cln
		x.SetType(ft)
	}
}
Exemplo n.º 5
0
func (v *typeInferrer) aggregateFunc(x *ast.AggregateFuncExpr) {
	name := strings.ToLower(x.F)
	switch name {
	case "count":
		ft := types.NewFieldType(mysql.TypeLonglong)
		ft.Flen = 21
		ft.Charset = charset.CharsetBin
		ft.Collate = charset.CollationBin
		x.SetType(ft)
	case "sum":
		ft := types.NewFieldType(mysql.TypeNewDecimal)
		ft.Charset = charset.CharsetBin
		ft.Collate = charset.CollationBin
		x.SetType(ft)
	}
}
Exemplo n.º 6
0
func (e *Evaluator) evalAggAvg(v *ast.AggregateFuncExpr) {
	ctx := v.GetContext()
	switch ctx.Value.Kind() {
	case types.KindFloat64:
		t := ctx.Value.GetFloat64() / float64(ctx.Count)
		v.SetValue(t)
	case types.KindMysqlDecimal:
		x := ctx.Value.GetMysqlDecimal()
		t := x.Div(mysql.NewDecimalFromUint(uint64(ctx.Count), 0))
		v.SetMysqlDecimal(t)
	}
	ctx.Value = *v.GetDatum()
}
Exemplo n.º 7
0
func (e *Evaluator) evalAggGroupConcat(v *ast.AggregateFuncExpr) {
	ctx := v.GetContext()
	if ctx.Buffer != nil {
		v.SetString(ctx.Buffer.String())
	} else {
		v.SetNull()
	}
}
Exemplo n.º 8
0
func (e *Evaluator) evalAggAvg(v *ast.AggregateFuncExpr) {
	ctx := v.GetContext()
	switch ctx.Value.Kind() {
	case types.KindFloat64:
		t := ctx.Value.GetFloat64() / float64(ctx.Count)
		v.SetValue(t)
	case types.KindMysqlDecimal:
		x := ctx.Value.GetMysqlDecimal()
		var y, to mysql.MyDecimal
		y.FromUint(uint64(ctx.Count))
		mysql.DecimalDiv(x, &y, &to, mysql.DivFracIncr)
		to.Round(&to, ctx.Value.Frac()+mysql.DivFracIncr)
		v.SetMysqlDecimal(&to)
	}
	ctx.Value = *v.GetDatum()
}
Exemplo n.º 9
0
func (e *Evaluator) evalAggSetValue(v *ast.AggregateFuncExpr) {
	ctx := v.GetContext()
	v.SetDatum(ctx.Value)
}
Exemplo n.º 10
0
func (e *Evaluator) evalAggCount(v *ast.AggregateFuncExpr) {
	ctx := v.GetContext()
	v.SetInt64(ctx.Count)
}
Exemplo n.º 11
0
func (e *Evaluator) evalAggFirstRow(v *ast.AggregateFuncExpr) {
	ctx := v.GetContext()
	v.SetValue(ctx.Value)
}