1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/hanchuanchuan-goInception

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
aggregation_test.go 21 КБ
Копировать Редактировать Исходные данные Просмотреть построчно История
hanchuanchuan Отправлено 6 лет назад 27f3c5a
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package aggregation
import (
"math"
"github.com/hanchuanchuan/goInception/ast"
"github.com/hanchuanchuan/goInception/expression"
"github.com/hanchuanchuan/goInception/mysql"
"github.com/hanchuanchuan/goInception/sessionctx"
"github.com/hanchuanchuan/goInception/sessionctx/variable"
"github.com/hanchuanchuan/goInception/types"
"github.com/hanchuanchuan/goInception/util/chunk"
"github.com/hanchuanchuan/goInception/util/mock"
. "github.com/pingcap/check"
)
var _ = Suite(&testAggFuncSuit{})
type testAggFuncSuit struct {
ctx sessionctx.Context
rows []chunk.Row
nullRow chunk.Row
}
func generateRowData() []chunk.Row {
rows := make([]chunk.Row, 0, 5050)
for i := 1; i <= 100; i++ {
for j := 0; j < i; j++ {
rows = append(rows, chunk.MutRowFromDatums(types.MakeDatums(i)).ToRow())
}
}
return rows
}
func (s *testAggFuncSuit) SetUpSuite(c *C) {
s.ctx = mock.NewContext()
s.ctx.GetSessionVars().GlobalVarsAccessor = variable.NewMockGlobalAccessor()
s.rows = generateRowData()
s.nullRow = chunk.MutRowFromDatums([]types.Datum{{}}).ToRow()
}
func (s *testAggFuncSuit) TestAvg(c *C) {
col := &expression.Column{
Index: 0,
RetType: types.NewFieldType(mysql.TypeLonglong),
}
ctx := mock.NewContext()
avgFunc := NewAggFuncDesc(s.ctx, ast.AggFuncAvg, []expression.Expression{col}, false).GetAggFunc(ctx)
evalCtx := avgFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
result := avgFunc.GetResult(evalCtx)
c.Assert(result.IsNull(), IsTrue)
for _, row := range s.rows {
err := avgFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
}
result = avgFunc.GetResult(evalCtx)
needed := types.NewDecFromStringForTest("67.000000000000000000000000000000")
c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue)
err := avgFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, s.nullRow)
c.Assert(err, IsNil)
result = avgFunc.GetResult(evalCtx)
c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue)
distinctAvgFunc := NewAggFuncDesc(s.ctx, ast.AggFuncAvg, []expression.Expression{col}, true).GetAggFunc(ctx)
evalCtx = distinctAvgFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
for _, row := range s.rows {
err := distinctAvgFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
}
result = distinctAvgFunc.GetResult(evalCtx)
needed = types.NewDecFromStringForTest("50.500000000000000000000000000000")
c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue)
partialResult := distinctAvgFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetInt64(), Equals, int64(100))
needed = types.NewDecFromStringForTest("5050")
c.Assert(partialResult[1].GetMysqlDecimal().Compare(needed) == 0, IsTrue, Commentf("%v, %v ", result.GetMysqlDecimal(), needed))
}
func (s *testAggFuncSuit) TestAvgFinalMode(c *C) {
rows := make([][]types.Datum, 0, 100)
for i := 1; i <= 100; i++ {
rows = append(rows, types.MakeDatums(i, types.NewDecFromInt(int64(i*i))))
}
ctx := mock.NewContext()
cntCol := &expression.Column{
Index: 0,
RetType: types.NewFieldType(mysql.TypeLonglong),
}
sumCol := &expression.Column{
Index: 1,
RetType: types.NewFieldType(mysql.TypeNewDecimal),
}
aggFunc := NewAggFuncDesc(s.ctx, ast.AggFuncAvg, []expression.Expression{cntCol, sumCol}, false)
aggFunc.Mode = FinalMode
avgFunc := aggFunc.GetAggFunc(ctx)
evalCtx := avgFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
for _, row := range rows {
err := avgFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, chunk.MutRowFromDatums(row).ToRow())
c.Assert(err, IsNil)
}
result := avgFunc.GetResult(evalCtx)
needed := types.NewDecFromStringForTest("67.000000000000000000000000000000")
c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue)
}
func (s *testAggFuncSuit) TestSum(c *C) {
col := &expression.Column{
Index: 0,
RetType: types.NewFieldType(mysql.TypeLonglong),
}
ctx := mock.NewContext()
sumFunc := NewAggFuncDesc(s.ctx, ast.AggFuncSum, []expression.Expression{col}, false).GetAggFunc(ctx)
evalCtx := sumFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
result := sumFunc.GetResult(evalCtx)
c.Assert(result.IsNull(), IsTrue)
for _, row := range s.rows {
err := sumFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
}
result = sumFunc.GetResult(evalCtx)
needed := types.NewDecFromStringForTest("338350")
c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue)
err := sumFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, s.nullRow)
c.Assert(err, IsNil)
result = sumFunc.GetResult(evalCtx)
c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue)
partialResult := sumFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetMysqlDecimal().Compare(needed) == 0, IsTrue)
distinctSumFunc := NewAggFuncDesc(s.ctx, ast.AggFuncSum, []expression.Expression{col}, true).GetAggFunc(ctx)
evalCtx = distinctSumFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
for _, row := range s.rows {
err := distinctSumFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
}
result = distinctSumFunc.GetResult(evalCtx)
needed = types.NewDecFromStringForTest("5050")
c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue)
}
func (s *testAggFuncSuit) TestBitAnd(c *C) {
col := &expression.Column{
Index: 0,
RetType: types.NewFieldType(mysql.TypeLonglong),
}
ctx := mock.NewContext()
bitAndFunc := NewAggFuncDesc(s.ctx, ast.AggFuncBitAnd, []expression.Expression{col}, false).GetAggFunc(ctx)
evalCtx := bitAndFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
result := bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(math.MaxUint64))
row := chunk.MutRowFromDatums(types.MakeDatums(1)).ToRow()
err := bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
err = bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, s.nullRow)
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
row = chunk.MutRowFromDatums(types.MakeDatums(1)).ToRow()
err = bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
row = chunk.MutRowFromDatums(types.MakeDatums(3)).ToRow()
err = bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
row = chunk.MutRowFromDatums(types.MakeDatums(2)).ToRow()
err = bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))
partialResult := bitAndFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetUint64(), Equals, uint64(0))
// test bit_and( decimal )
col.RetType = types.NewFieldType(mysql.TypeNewDecimal)
bitAndFunc.ResetContext(s.ctx.GetSessionVars().StmtCtx, evalCtx)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(math.MaxUint64))
var dec types.MyDecimal
err = dec.FromString([]byte("1.234"))
c.Assert(err, IsNil)
row = chunk.MutRowFromDatums(types.MakeDatums(&dec)).ToRow()
err = bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
err = dec.FromString([]byte("3.012"))
c.Assert(err, IsNil)
row = chunk.MutRowFromDatums(types.MakeDatums(&dec)).ToRow()
err = bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
err = dec.FromString([]byte("2.12345678"))
c.Assert(err, IsNil)
row = chunk.MutRowFromDatums(types.MakeDatums(&dec)).ToRow()
err = bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))
}
func (s *testAggFuncSuit) TestBitOr(c *C) {
col := &expression.Column{
Index: 0,
RetType: types.NewFieldType(mysql.TypeLonglong),
}
ctx := mock.NewContext()
bitOrFunc := NewAggFuncDesc(s.ctx, ast.AggFuncBitOr, []expression.Expression{col}, false).GetAggFunc(ctx)
evalCtx := bitOrFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
result := bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))
row := chunk.MutRowFromDatums(types.MakeDatums(1)).ToRow()
err := bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, s.nullRow)
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
row = chunk.MutRowFromDatums(types.MakeDatums(1)).ToRow()
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
row = chunk.MutRowFromDatums(types.MakeDatums(3)).ToRow()
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(3))
row = chunk.MutRowFromDatums(types.MakeDatums(2)).ToRow()
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(3))
partialResult := bitOrFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetUint64(), Equals, uint64(3))
// test bit_or( decimal )
col.RetType = types.NewFieldType(mysql.TypeNewDecimal)
bitOrFunc.ResetContext(s.ctx.GetSessionVars().StmtCtx, evalCtx)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))
var dec types.MyDecimal
err = dec.FromString([]byte("12.234"))
c.Assert(err, IsNil)
row = chunk.MutRowFromDatums(types.MakeDatums(&dec)).ToRow()
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(12))
err = dec.FromString([]byte("1.012"))
c.Assert(err, IsNil)
row = chunk.MutRowFromDatums(types.MakeDatums(&dec)).ToRow()
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(13))
err = dec.FromString([]byte("15.12345678"))
c.Assert(err, IsNil)
row = chunk.MutRowFromDatums(types.MakeDatums(&dec)).ToRow()
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(15))
err = dec.FromString([]byte("16.00"))
c.Assert(err, IsNil)
row = chunk.MutRowFromDatums(types.MakeDatums(&dec)).ToRow()
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(31))
}
func (s *testAggFuncSuit) TestBitXor(c *C) {
col := &expression.Column{
Index: 0,
RetType: types.NewFieldType(mysql.TypeLonglong),
}
ctx := mock.NewContext()
bitXorFunc := NewAggFuncDesc(s.ctx, ast.AggFuncBitXor, []expression.Expression{col}, false).GetAggFunc(ctx)
evalCtx := bitXorFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
result := bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))
row := chunk.MutRowFromDatums(types.MakeDatums(1)).ToRow()
err := bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
err = bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, s.nullRow)
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
row = chunk.MutRowFromDatums(types.MakeDatums(1)).ToRow()
err = bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))
row = chunk.MutRowFromDatums(types.MakeDatums(3)).ToRow()
err = bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(3))
row = chunk.MutRowFromDatums(types.MakeDatums(2)).ToRow()
err = bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
partialResult := bitXorFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetUint64(), Equals, uint64(1))
// test bit_xor( decimal )
col.RetType = types.NewFieldType(mysql.TypeNewDecimal)
bitXorFunc.ResetContext(s.ctx.GetSessionVars().StmtCtx, evalCtx)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))
var dec types.MyDecimal
err = dec.FromString([]byte("1.234"))
c.Assert(err, IsNil)
row = chunk.MutRowFromDatums(types.MakeDatums(&dec)).ToRow()
err = bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
err = dec.FromString([]byte("1.012"))
c.Assert(err, IsNil)
row = chunk.MutRowFromDatums(types.MakeDatums(&dec)).ToRow()
err = bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))
err = dec.FromString([]byte("2.12345678"))
c.Assert(err, IsNil)
row = chunk.MutRowFromDatums(types.MakeDatums(&dec)).ToRow()
err = bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(2))
}
func (s *testAggFuncSuit) TestCount(c *C) {
col := &expression.Column{
Index: 0,
RetType: types.NewFieldType(mysql.TypeLonglong),
}
ctx := mock.NewContext()
countFunc := NewAggFuncDesc(s.ctx, ast.AggFuncCount, []expression.Expression{col}, false).GetAggFunc(ctx)
evalCtx := countFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
result := countFunc.GetResult(evalCtx)
c.Assert(result.GetInt64(), Equals, int64(0))
for _, row := range s.rows {
err := countFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
}
result = countFunc.GetResult(evalCtx)
c.Assert(result.GetInt64(), Equals, int64(5050))
err := countFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, s.nullRow)
c.Assert(err, IsNil)
result = countFunc.GetResult(evalCtx)
c.Assert(result.GetInt64(), Equals, int64(5050))
partialResult := countFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetInt64(), Equals, int64(5050))
distinctCountFunc := NewAggFuncDesc(s.ctx, ast.AggFuncCount, []expression.Expression{col}, true).GetAggFunc(ctx)
evalCtx = distinctCountFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
for _, row := range s.rows {
err := distinctCountFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
}
result = distinctCountFunc.GetResult(evalCtx)
c.Assert(result.GetInt64(), Equals, int64(100))
}
func (s *testAggFuncSuit) TestConcat(c *C) {
col := &expression.Column{
Index: 0,
RetType: types.NewFieldType(mysql.TypeLonglong),
}
sep := &expression.Column{
Index: 1,
RetType: types.NewFieldType(mysql.TypeVarchar),
}
ctx := mock.NewContext()
concatFunc := NewAggFuncDesc(s.ctx, ast.AggFuncGroupConcat, []expression.Expression{col, sep}, false).GetAggFunc(ctx)
evalCtx := concatFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
result := concatFunc.GetResult(evalCtx)
c.Assert(result.IsNull(), IsTrue)
row := chunk.MutRowFromDatums(types.MakeDatums(1, "x"))
err := concatFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = concatFunc.GetResult(evalCtx)
c.Assert(result.GetString(), Equals, "1")
row.SetDatum(0, types.NewIntDatum(2))
err = concatFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = concatFunc.GetResult(evalCtx)
c.Assert(result.GetString(), Equals, "1x2")
row.SetDatum(0, types.NewDatum(nil))
err = concatFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = concatFunc.GetResult(evalCtx)
c.Assert(result.GetString(), Equals, "1x2")
partialResult := concatFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetString(), Equals, "1x2")
distinctConcatFunc := NewAggFuncDesc(s.ctx, ast.AggFuncGroupConcat, []expression.Expression{col, sep}, true).GetAggFunc(ctx)
evalCtx = distinctConcatFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
row.SetDatum(0, types.NewIntDatum(1))
err = distinctConcatFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = distinctConcatFunc.GetResult(evalCtx)
c.Assert(result.GetString(), Equals, "1")
row.SetDatum(0, types.NewIntDatum(1))
err = distinctConcatFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = distinctConcatFunc.GetResult(evalCtx)
c.Assert(result.GetString(), Equals, "1")
}
func (s *testAggFuncSuit) TestFirstRow(c *C) {
col := &expression.Column{
Index: 0,
RetType: types.NewFieldType(mysql.TypeLonglong),
}
ctx := mock.NewContext()
firstRowFunc := NewAggFuncDesc(s.ctx, ast.AggFuncFirstRow, []expression.Expression{col}, false).GetAggFunc(ctx)
evalCtx := firstRowFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
row := chunk.MutRowFromDatums(types.MakeDatums(1)).ToRow()
err := firstRowFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result := firstRowFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
row = chunk.MutRowFromDatums(types.MakeDatums(2)).ToRow()
err = firstRowFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, row)
c.Assert(err, IsNil)
result = firstRowFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))
partialResult := firstRowFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetUint64(), Equals, uint64(1))
}
func (s *testAggFuncSuit) TestMaxMin(c *C) {
col := &expression.Column{
Index: 0,
RetType: types.NewFieldType(mysql.TypeLonglong),
}
ctx := mock.NewContext()
maxFunc := NewAggFuncDesc(s.ctx, ast.AggFuncMax, []expression.Expression{col}, false).GetAggFunc(ctx)
minFunc := NewAggFuncDesc(s.ctx, ast.AggFuncMin, []expression.Expression{col}, false).GetAggFunc(ctx)
maxEvalCtx := maxFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
minEvalCtx := minFunc.CreateContext(s.ctx.GetSessionVars().StmtCtx)
result := maxFunc.GetResult(maxEvalCtx)
c.Assert(result.IsNull(), IsTrue)
result = minFunc.GetResult(minEvalCtx)
c.Assert(result.IsNull(), IsTrue)
row := chunk.MutRowFromDatums(types.MakeDatums(2))
err := maxFunc.Update(maxEvalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = maxFunc.GetResult(maxEvalCtx)
c.Assert(result.GetInt64(), Equals, int64(2))
err = minFunc.Update(minEvalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = minFunc.GetResult(minEvalCtx)
c.Assert(result.GetInt64(), Equals, int64(2))
row.SetDatum(0, types.NewIntDatum(3))
err = maxFunc.Update(maxEvalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = maxFunc.GetResult(maxEvalCtx)
c.Assert(result.GetInt64(), Equals, int64(3))
err = minFunc.Update(minEvalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = minFunc.GetResult(minEvalCtx)
c.Assert(result.GetInt64(), Equals, int64(2))
row.SetDatum(0, types.NewIntDatum(1))
err = maxFunc.Update(maxEvalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = maxFunc.GetResult(maxEvalCtx)
c.Assert(result.GetInt64(), Equals, int64(3))
err = minFunc.Update(minEvalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = minFunc.GetResult(minEvalCtx)
c.Assert(result.GetInt64(), Equals, int64(1))
row.SetDatum(0, types.NewDatum(nil))
err = maxFunc.Update(maxEvalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = maxFunc.GetResult(maxEvalCtx)
c.Assert(result.GetInt64(), Equals, int64(3))
err = minFunc.Update(minEvalCtx, s.ctx.GetSessionVars().StmtCtx, row.ToRow())
c.Assert(err, IsNil)
result = minFunc.GetResult(minEvalCtx)
c.Assert(result.GetInt64(), Equals, int64(1))
partialResult := minFunc.GetPartialResult(minEvalCtx)
c.Assert(partialResult[0].GetInt64(), Equals, int64(1))
}

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/hanchuanchuan-goInception.git
git@gitlife.ru:oschina-mirror/hanchuanchuan-goInception.git
oschina-mirror
hanchuanchuan-goInception
hanchuanchuan-goInception
v1.2.2