github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/aggregation/aggregation_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package aggregation 15 16 import ( 17 "math" 18 19 . "github.com/whtcorpsinc/check" 20 "github.com/whtcorpsinc/BerolinaSQL/ast" 21 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 22 "github.com/whtcorpsinc/milevadb/memex" 23 "github.com/whtcorpsinc/milevadb/stochastikctx" 24 "github.com/whtcorpsinc/milevadb/stochastikctx/variable" 25 "github.com/whtcorpsinc/milevadb/types" 26 "github.com/whtcorpsinc/milevadb/soliton/chunk" 27 "github.com/whtcorpsinc/milevadb/soliton/mock" 28 ) 29 30 var _ = Suite(&testAggFuncSuit{}) 31 32 type testAggFuncSuit struct { 33 ctx stochastikctx.Context 34 rows []chunk.Event 35 nullEvent chunk.Event 36 } 37 38 func generateEventData() []chunk.Event { 39 rows := make([]chunk.Event, 0, 5050) 40 for i := 1; i <= 100; i++ { 41 for j := 0; j < i; j++ { 42 rows = append(rows, chunk.MutEventFromCausets(types.MakeCausets(i)).ToEvent()) 43 } 44 } 45 return rows 46 } 47 48 func (s *testAggFuncSuit) SetUpSuite(c *C) { 49 s.ctx = mock.NewContext() 50 s.ctx.GetStochastikVars().GlobalVarsAccessor = variable.NewMockGlobalAccessor() 51 s.rows = generateEventData() 52 s.nullEvent = chunk.MutEventFromCausets([]types.Causet{{}}).ToEvent() 53 } 54 55 func (s *testAggFuncSuit) TestAvg(c *C) { 56 defCaus := &memex.DeferredCauset{ 57 Index: 0, 58 RetType: types.NewFieldType(allegrosql.TypeLonglong), 59 } 60 ctx := mock.NewContext() 61 desc, err := NewAggFuncDesc(s.ctx, ast.AggFuncAvg, []memex.Expression{defCaus}, false) 62 c.Assert(err, IsNil) 63 avgFunc := desc.GetAggFunc(ctx) 64 evalCtx := avgFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 65 66 result := avgFunc.GetResult(evalCtx) 67 c.Assert(result.IsNull(), IsTrue) 68 69 for _, event := range s.rows { 70 err := avgFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 71 c.Assert(err, IsNil) 72 } 73 result = avgFunc.GetResult(evalCtx) 74 needed := types.NewDecFromStringForTest("67.000000000000000000000000000000") 75 c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue) 76 err = avgFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, s.nullEvent) 77 c.Assert(err, IsNil) 78 result = avgFunc.GetResult(evalCtx) 79 c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue) 80 81 desc, err = NewAggFuncDesc(s.ctx, ast.AggFuncAvg, []memex.Expression{defCaus}, true) 82 c.Assert(err, IsNil) 83 distinctAvgFunc := desc.GetAggFunc(ctx) 84 evalCtx = distinctAvgFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 85 for _, event := range s.rows { 86 err := distinctAvgFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 87 c.Assert(err, IsNil) 88 } 89 result = distinctAvgFunc.GetResult(evalCtx) 90 needed = types.NewDecFromStringForTest("50.500000000000000000000000000000") 91 c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue) 92 partialResult := distinctAvgFunc.GetPartialResult(evalCtx) 93 c.Assert(partialResult[0].GetInt64(), Equals, int64(100)) 94 needed = types.NewDecFromStringForTest("5050") 95 c.Assert(partialResult[1].GetMysqlDecimal().Compare(needed) == 0, IsTrue, Commentf("%v, %v ", result.GetMysqlDecimal(), needed)) 96 } 97 98 func (s *testAggFuncSuit) TestAvgFinalMode(c *C) { 99 rows := make([][]types.Causet, 0, 100) 100 for i := 1; i <= 100; i++ { 101 rows = append(rows, types.MakeCausets(i, types.NewDecFromInt(int64(i*i)))) 102 } 103 ctx := mock.NewContext() 104 cntDefCaus := &memex.DeferredCauset{ 105 Index: 0, 106 RetType: types.NewFieldType(allegrosql.TypeLonglong), 107 } 108 sumDefCaus := &memex.DeferredCauset{ 109 Index: 1, 110 RetType: types.NewFieldType(allegrosql.TypeNewDecimal), 111 } 112 aggFunc, err := NewAggFuncDesc(s.ctx, ast.AggFuncAvg, []memex.Expression{cntDefCaus, sumDefCaus}, false) 113 c.Assert(err, IsNil) 114 aggFunc.Mode = FinalMode 115 avgFunc := aggFunc.GetAggFunc(ctx) 116 evalCtx := avgFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 117 118 for _, event := range rows { 119 err := avgFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, chunk.MutEventFromCausets(event).ToEvent()) 120 c.Assert(err, IsNil) 121 } 122 result := avgFunc.GetResult(evalCtx) 123 needed := types.NewDecFromStringForTest("67.000000000000000000000000000000") 124 c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue) 125 } 126 127 func (s *testAggFuncSuit) TestSum(c *C) { 128 defCaus := &memex.DeferredCauset{ 129 Index: 0, 130 RetType: types.NewFieldType(allegrosql.TypeLonglong), 131 } 132 ctx := mock.NewContext() 133 desc, err := NewAggFuncDesc(s.ctx, ast.AggFuncSum, []memex.Expression{defCaus}, false) 134 c.Assert(err, IsNil) 135 sumFunc := desc.GetAggFunc(ctx) 136 evalCtx := sumFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 137 138 result := sumFunc.GetResult(evalCtx) 139 c.Assert(result.IsNull(), IsTrue) 140 141 for _, event := range s.rows { 142 err := sumFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 143 c.Assert(err, IsNil) 144 } 145 result = sumFunc.GetResult(evalCtx) 146 needed := types.NewDecFromStringForTest("338350") 147 c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue) 148 err = sumFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, s.nullEvent) 149 c.Assert(err, IsNil) 150 result = sumFunc.GetResult(evalCtx) 151 c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue) 152 partialResult := sumFunc.GetPartialResult(evalCtx) 153 c.Assert(partialResult[0].GetMysqlDecimal().Compare(needed) == 0, IsTrue) 154 155 desc, err = NewAggFuncDesc(s.ctx, ast.AggFuncSum, []memex.Expression{defCaus}, true) 156 c.Assert(err, IsNil) 157 distinctSumFunc := desc.GetAggFunc(ctx) 158 evalCtx = distinctSumFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 159 for _, event := range s.rows { 160 err := distinctSumFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 161 c.Assert(err, IsNil) 162 } 163 result = distinctSumFunc.GetResult(evalCtx) 164 needed = types.NewDecFromStringForTest("5050") 165 c.Assert(result.GetMysqlDecimal().Compare(needed) == 0, IsTrue) 166 } 167 168 func (s *testAggFuncSuit) TestBitAnd(c *C) { 169 defCaus := &memex.DeferredCauset{ 170 Index: 0, 171 RetType: types.NewFieldType(allegrosql.TypeLonglong), 172 } 173 ctx := mock.NewContext() 174 desc, err := NewAggFuncDesc(s.ctx, ast.AggFuncBitAnd, []memex.Expression{defCaus}, false) 175 c.Assert(err, IsNil) 176 bitAndFunc := desc.GetAggFunc(ctx) 177 evalCtx := bitAndFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 178 179 result := bitAndFunc.GetResult(evalCtx) 180 c.Assert(result.GetUint64(), Equals, uint64(math.MaxUint64)) 181 182 event := chunk.MutEventFromCausets(types.MakeCausets(1)).ToEvent() 183 err = bitAndFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 184 c.Assert(err, IsNil) 185 result = bitAndFunc.GetResult(evalCtx) 186 c.Assert(result.GetUint64(), Equals, uint64(1)) 187 188 err = bitAndFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, s.nullEvent) 189 c.Assert(err, IsNil) 190 result = bitAndFunc.GetResult(evalCtx) 191 c.Assert(result.GetUint64(), Equals, uint64(1)) 192 193 event = chunk.MutEventFromCausets(types.MakeCausets(1)).ToEvent() 194 err = bitAndFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 195 c.Assert(err, IsNil) 196 result = bitAndFunc.GetResult(evalCtx) 197 c.Assert(result.GetUint64(), Equals, uint64(1)) 198 199 event = chunk.MutEventFromCausets(types.MakeCausets(3)).ToEvent() 200 err = bitAndFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 201 c.Assert(err, IsNil) 202 result = bitAndFunc.GetResult(evalCtx) 203 c.Assert(result.GetUint64(), Equals, uint64(1)) 204 205 event = chunk.MutEventFromCausets(types.MakeCausets(2)).ToEvent() 206 err = bitAndFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 207 c.Assert(err, IsNil) 208 result = bitAndFunc.GetResult(evalCtx) 209 c.Assert(result.GetUint64(), Equals, uint64(0)) 210 partialResult := bitAndFunc.GetPartialResult(evalCtx) 211 c.Assert(partialResult[0].GetUint64(), Equals, uint64(0)) 212 213 // test bit_and( decimal ) 214 defCaus.RetType = types.NewFieldType(allegrosql.TypeNewDecimal) 215 bitAndFunc.ResetContext(s.ctx.GetStochastikVars().StmtCtx, evalCtx) 216 217 result = bitAndFunc.GetResult(evalCtx) 218 c.Assert(result.GetUint64(), Equals, uint64(math.MaxUint64)) 219 220 var dec types.MyDecimal 221 err = dec.FromString([]byte("1.234")) 222 c.Assert(err, IsNil) 223 event = chunk.MutEventFromCausets(types.MakeCausets(&dec)).ToEvent() 224 err = bitAndFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 225 c.Assert(err, IsNil) 226 result = bitAndFunc.GetResult(evalCtx) 227 c.Assert(result.GetUint64(), Equals, uint64(1)) 228 229 err = dec.FromString([]byte("3.012")) 230 c.Assert(err, IsNil) 231 event = chunk.MutEventFromCausets(types.MakeCausets(&dec)).ToEvent() 232 err = bitAndFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 233 c.Assert(err, IsNil) 234 result = bitAndFunc.GetResult(evalCtx) 235 c.Assert(result.GetUint64(), Equals, uint64(1)) 236 237 err = dec.FromString([]byte("2.12345678")) 238 c.Assert(err, IsNil) 239 event = chunk.MutEventFromCausets(types.MakeCausets(&dec)).ToEvent() 240 err = bitAndFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 241 c.Assert(err, IsNil) 242 result = bitAndFunc.GetResult(evalCtx) 243 c.Assert(result.GetUint64(), Equals, uint64(0)) 244 } 245 246 func (s *testAggFuncSuit) TestBitOr(c *C) { 247 defCaus := &memex.DeferredCauset{ 248 Index: 0, 249 RetType: types.NewFieldType(allegrosql.TypeLonglong), 250 } 251 ctx := mock.NewContext() 252 desc, err := NewAggFuncDesc(s.ctx, ast.AggFuncBitOr, []memex.Expression{defCaus}, false) 253 c.Assert(err, IsNil) 254 bitOrFunc := desc.GetAggFunc(ctx) 255 evalCtx := bitOrFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 256 257 result := bitOrFunc.GetResult(evalCtx) 258 c.Assert(result.GetUint64(), Equals, uint64(0)) 259 260 event := chunk.MutEventFromCausets(types.MakeCausets(1)).ToEvent() 261 err = bitOrFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 262 c.Assert(err, IsNil) 263 result = bitOrFunc.GetResult(evalCtx) 264 c.Assert(result.GetUint64(), Equals, uint64(1)) 265 266 err = bitOrFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, s.nullEvent) 267 c.Assert(err, IsNil) 268 result = bitOrFunc.GetResult(evalCtx) 269 c.Assert(result.GetUint64(), Equals, uint64(1)) 270 271 event = chunk.MutEventFromCausets(types.MakeCausets(1)).ToEvent() 272 err = bitOrFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 273 c.Assert(err, IsNil) 274 result = bitOrFunc.GetResult(evalCtx) 275 c.Assert(result.GetUint64(), Equals, uint64(1)) 276 277 event = chunk.MutEventFromCausets(types.MakeCausets(3)).ToEvent() 278 err = bitOrFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 279 c.Assert(err, IsNil) 280 result = bitOrFunc.GetResult(evalCtx) 281 c.Assert(result.GetUint64(), Equals, uint64(3)) 282 283 event = chunk.MutEventFromCausets(types.MakeCausets(2)).ToEvent() 284 err = bitOrFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 285 c.Assert(err, IsNil) 286 result = bitOrFunc.GetResult(evalCtx) 287 c.Assert(result.GetUint64(), Equals, uint64(3)) 288 partialResult := bitOrFunc.GetPartialResult(evalCtx) 289 c.Assert(partialResult[0].GetUint64(), Equals, uint64(3)) 290 291 // test bit_or( decimal ) 292 defCaus.RetType = types.NewFieldType(allegrosql.TypeNewDecimal) 293 bitOrFunc.ResetContext(s.ctx.GetStochastikVars().StmtCtx, evalCtx) 294 295 result = bitOrFunc.GetResult(evalCtx) 296 c.Assert(result.GetUint64(), Equals, uint64(0)) 297 298 var dec types.MyDecimal 299 err = dec.FromString([]byte("12.234")) 300 c.Assert(err, IsNil) 301 event = chunk.MutEventFromCausets(types.MakeCausets(&dec)).ToEvent() 302 err = bitOrFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 303 c.Assert(err, IsNil) 304 result = bitOrFunc.GetResult(evalCtx) 305 c.Assert(result.GetUint64(), Equals, uint64(12)) 306 307 err = dec.FromString([]byte("1.012")) 308 c.Assert(err, IsNil) 309 event = chunk.MutEventFromCausets(types.MakeCausets(&dec)).ToEvent() 310 err = bitOrFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 311 c.Assert(err, IsNil) 312 result = bitOrFunc.GetResult(evalCtx) 313 c.Assert(result.GetUint64(), Equals, uint64(13)) 314 err = dec.FromString([]byte("15.12345678")) 315 c.Assert(err, IsNil) 316 317 event = chunk.MutEventFromCausets(types.MakeCausets(&dec)).ToEvent() 318 err = bitOrFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 319 c.Assert(err, IsNil) 320 result = bitOrFunc.GetResult(evalCtx) 321 c.Assert(result.GetUint64(), Equals, uint64(15)) 322 323 err = dec.FromString([]byte("16.00")) 324 c.Assert(err, IsNil) 325 event = chunk.MutEventFromCausets(types.MakeCausets(&dec)).ToEvent() 326 err = bitOrFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 327 c.Assert(err, IsNil) 328 result = bitOrFunc.GetResult(evalCtx) 329 c.Assert(result.GetUint64(), Equals, uint64(31)) 330 } 331 332 func (s *testAggFuncSuit) TestBitXor(c *C) { 333 defCaus := &memex.DeferredCauset{ 334 Index: 0, 335 RetType: types.NewFieldType(allegrosql.TypeLonglong), 336 } 337 ctx := mock.NewContext() 338 desc, err := NewAggFuncDesc(s.ctx, ast.AggFuncBitXor, []memex.Expression{defCaus}, false) 339 c.Assert(err, IsNil) 340 bitXorFunc := desc.GetAggFunc(ctx) 341 evalCtx := bitXorFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 342 343 result := bitXorFunc.GetResult(evalCtx) 344 c.Assert(result.GetUint64(), Equals, uint64(0)) 345 346 event := chunk.MutEventFromCausets(types.MakeCausets(1)).ToEvent() 347 err = bitXorFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 348 c.Assert(err, IsNil) 349 result = bitXorFunc.GetResult(evalCtx) 350 c.Assert(result.GetUint64(), Equals, uint64(1)) 351 352 err = bitXorFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, s.nullEvent) 353 c.Assert(err, IsNil) 354 result = bitXorFunc.GetResult(evalCtx) 355 c.Assert(result.GetUint64(), Equals, uint64(1)) 356 357 event = chunk.MutEventFromCausets(types.MakeCausets(1)).ToEvent() 358 err = bitXorFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 359 c.Assert(err, IsNil) 360 result = bitXorFunc.GetResult(evalCtx) 361 c.Assert(result.GetUint64(), Equals, uint64(0)) 362 363 event = chunk.MutEventFromCausets(types.MakeCausets(3)).ToEvent() 364 err = bitXorFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 365 c.Assert(err, IsNil) 366 result = bitXorFunc.GetResult(evalCtx) 367 c.Assert(result.GetUint64(), Equals, uint64(3)) 368 369 event = chunk.MutEventFromCausets(types.MakeCausets(2)).ToEvent() 370 err = bitXorFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 371 c.Assert(err, IsNil) 372 result = bitXorFunc.GetResult(evalCtx) 373 c.Assert(result.GetUint64(), Equals, uint64(1)) 374 partialResult := bitXorFunc.GetPartialResult(evalCtx) 375 c.Assert(partialResult[0].GetUint64(), Equals, uint64(1)) 376 377 // test bit_xor( decimal ) 378 defCaus.RetType = types.NewFieldType(allegrosql.TypeNewDecimal) 379 bitXorFunc.ResetContext(s.ctx.GetStochastikVars().StmtCtx, evalCtx) 380 381 result = bitXorFunc.GetResult(evalCtx) 382 c.Assert(result.GetUint64(), Equals, uint64(0)) 383 384 var dec types.MyDecimal 385 err = dec.FromString([]byte("1.234")) 386 c.Assert(err, IsNil) 387 event = chunk.MutEventFromCausets(types.MakeCausets(&dec)).ToEvent() 388 err = bitXorFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 389 c.Assert(err, IsNil) 390 result = bitXorFunc.GetResult(evalCtx) 391 c.Assert(result.GetUint64(), Equals, uint64(1)) 392 393 err = dec.FromString([]byte("1.012")) 394 c.Assert(err, IsNil) 395 event = chunk.MutEventFromCausets(types.MakeCausets(&dec)).ToEvent() 396 err = bitXorFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 397 c.Assert(err, IsNil) 398 result = bitXorFunc.GetResult(evalCtx) 399 c.Assert(result.GetUint64(), Equals, uint64(0)) 400 401 err = dec.FromString([]byte("2.12345678")) 402 c.Assert(err, IsNil) 403 event = chunk.MutEventFromCausets(types.MakeCausets(&dec)).ToEvent() 404 err = bitXorFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 405 c.Assert(err, IsNil) 406 result = bitXorFunc.GetResult(evalCtx) 407 c.Assert(result.GetUint64(), Equals, uint64(2)) 408 } 409 410 func (s *testAggFuncSuit) TestCount(c *C) { 411 defCaus := &memex.DeferredCauset{ 412 Index: 0, 413 RetType: types.NewFieldType(allegrosql.TypeLonglong), 414 } 415 ctx := mock.NewContext() 416 desc, err := NewAggFuncDesc(s.ctx, ast.AggFuncCount, []memex.Expression{defCaus}, false) 417 c.Assert(err, IsNil) 418 countFunc := desc.GetAggFunc(ctx) 419 evalCtx := countFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 420 421 result := countFunc.GetResult(evalCtx) 422 c.Assert(result.GetInt64(), Equals, int64(0)) 423 424 for _, event := range s.rows { 425 err := countFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 426 c.Assert(err, IsNil) 427 } 428 result = countFunc.GetResult(evalCtx) 429 c.Assert(result.GetInt64(), Equals, int64(5050)) 430 err = countFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, s.nullEvent) 431 c.Assert(err, IsNil) 432 result = countFunc.GetResult(evalCtx) 433 c.Assert(result.GetInt64(), Equals, int64(5050)) 434 partialResult := countFunc.GetPartialResult(evalCtx) 435 c.Assert(partialResult[0].GetInt64(), Equals, int64(5050)) 436 437 desc, err = NewAggFuncDesc(s.ctx, ast.AggFuncCount, []memex.Expression{defCaus}, true) 438 c.Assert(err, IsNil) 439 distinctCountFunc := desc.GetAggFunc(ctx) 440 evalCtx = distinctCountFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 441 442 for _, event := range s.rows { 443 err := distinctCountFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 444 c.Assert(err, IsNil) 445 } 446 result = distinctCountFunc.GetResult(evalCtx) 447 c.Assert(result.GetInt64(), Equals, int64(100)) 448 } 449 450 func (s *testAggFuncSuit) TestConcat(c *C) { 451 defCaus := &memex.DeferredCauset{ 452 Index: 0, 453 RetType: types.NewFieldType(allegrosql.TypeLonglong), 454 } 455 sep := &memex.DeferredCauset{ 456 Index: 1, 457 RetType: types.NewFieldType(allegrosql.TypeVarchar), 458 } 459 ctx := mock.NewContext() 460 desc, err := NewAggFuncDesc(s.ctx, ast.AggFuncGroupConcat, []memex.Expression{defCaus, sep}, false) 461 c.Assert(err, IsNil) 462 concatFunc := desc.GetAggFunc(ctx) 463 evalCtx := concatFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 464 465 result := concatFunc.GetResult(evalCtx) 466 c.Assert(result.IsNull(), IsTrue) 467 468 event := chunk.MutEventFromCausets(types.MakeCausets(1, "x")) 469 err = concatFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 470 c.Assert(err, IsNil) 471 result = concatFunc.GetResult(evalCtx) 472 c.Assert(result.GetString(), Equals, "1") 473 474 event.SetCauset(0, types.NewIntCauset(2)) 475 err = concatFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 476 c.Assert(err, IsNil) 477 result = concatFunc.GetResult(evalCtx) 478 c.Assert(result.GetString(), Equals, "1x2") 479 480 event.SetCauset(0, types.NewCauset(nil)) 481 err = concatFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 482 c.Assert(err, IsNil) 483 result = concatFunc.GetResult(evalCtx) 484 c.Assert(result.GetString(), Equals, "1x2") 485 partialResult := concatFunc.GetPartialResult(evalCtx) 486 c.Assert(partialResult[0].GetString(), Equals, "1x2") 487 488 desc, err = NewAggFuncDesc(s.ctx, ast.AggFuncGroupConcat, []memex.Expression{defCaus, sep}, true) 489 c.Assert(err, IsNil) 490 distinctConcatFunc := desc.GetAggFunc(ctx) 491 evalCtx = distinctConcatFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 492 493 event.SetCauset(0, types.NewIntCauset(1)) 494 err = distinctConcatFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 495 c.Assert(err, IsNil) 496 result = distinctConcatFunc.GetResult(evalCtx) 497 c.Assert(result.GetString(), Equals, "1") 498 499 event.SetCauset(0, types.NewIntCauset(1)) 500 err = distinctConcatFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 501 c.Assert(err, IsNil) 502 result = distinctConcatFunc.GetResult(evalCtx) 503 c.Assert(result.GetString(), Equals, "1") 504 } 505 506 func (s *testAggFuncSuit) TestFirstEvent(c *C) { 507 defCaus := &memex.DeferredCauset{ 508 Index: 0, 509 RetType: types.NewFieldType(allegrosql.TypeLonglong), 510 } 511 512 ctx := mock.NewContext() 513 desc, err := NewAggFuncDesc(s.ctx, ast.AggFuncFirstEvent, []memex.Expression{defCaus}, false) 514 c.Assert(err, IsNil) 515 firstEventFunc := desc.GetAggFunc(ctx) 516 evalCtx := firstEventFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 517 518 event := chunk.MutEventFromCausets(types.MakeCausets(1)).ToEvent() 519 err = firstEventFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 520 c.Assert(err, IsNil) 521 result := firstEventFunc.GetResult(evalCtx) 522 c.Assert(result.GetUint64(), Equals, uint64(1)) 523 524 event = chunk.MutEventFromCausets(types.MakeCausets(2)).ToEvent() 525 err = firstEventFunc.UFIDelate(evalCtx, s.ctx.GetStochastikVars().StmtCtx, event) 526 c.Assert(err, IsNil) 527 result = firstEventFunc.GetResult(evalCtx) 528 c.Assert(result.GetUint64(), Equals, uint64(1)) 529 partialResult := firstEventFunc.GetPartialResult(evalCtx) 530 c.Assert(partialResult[0].GetUint64(), Equals, uint64(1)) 531 } 532 533 func (s *testAggFuncSuit) TestMaxMin(c *C) { 534 defCaus := &memex.DeferredCauset{ 535 Index: 0, 536 RetType: types.NewFieldType(allegrosql.TypeLonglong), 537 } 538 539 ctx := mock.NewContext() 540 desc, err := NewAggFuncDesc(s.ctx, ast.AggFuncMax, []memex.Expression{defCaus}, false) 541 c.Assert(err, IsNil) 542 maxFunc := desc.GetAggFunc(ctx) 543 desc, err = NewAggFuncDesc(s.ctx, ast.AggFuncMin, []memex.Expression{defCaus}, false) 544 c.Assert(err, IsNil) 545 minFunc := desc.GetAggFunc(ctx) 546 maxEvalCtx := maxFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 547 minEvalCtx := minFunc.CreateContext(s.ctx.GetStochastikVars().StmtCtx) 548 549 result := maxFunc.GetResult(maxEvalCtx) 550 c.Assert(result.IsNull(), IsTrue) 551 result = minFunc.GetResult(minEvalCtx) 552 c.Assert(result.IsNull(), IsTrue) 553 554 event := chunk.MutEventFromCausets(types.MakeCausets(2)) 555 err = maxFunc.UFIDelate(maxEvalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 556 c.Assert(err, IsNil) 557 result = maxFunc.GetResult(maxEvalCtx) 558 c.Assert(result.GetInt64(), Equals, int64(2)) 559 err = minFunc.UFIDelate(minEvalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 560 c.Assert(err, IsNil) 561 result = minFunc.GetResult(minEvalCtx) 562 c.Assert(result.GetInt64(), Equals, int64(2)) 563 564 event.SetCauset(0, types.NewIntCauset(3)) 565 err = maxFunc.UFIDelate(maxEvalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 566 c.Assert(err, IsNil) 567 result = maxFunc.GetResult(maxEvalCtx) 568 c.Assert(result.GetInt64(), Equals, int64(3)) 569 err = minFunc.UFIDelate(minEvalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 570 c.Assert(err, IsNil) 571 result = minFunc.GetResult(minEvalCtx) 572 c.Assert(result.GetInt64(), Equals, int64(2)) 573 574 event.SetCauset(0, types.NewIntCauset(1)) 575 err = maxFunc.UFIDelate(maxEvalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 576 c.Assert(err, IsNil) 577 result = maxFunc.GetResult(maxEvalCtx) 578 c.Assert(result.GetInt64(), Equals, int64(3)) 579 err = minFunc.UFIDelate(minEvalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 580 c.Assert(err, IsNil) 581 result = minFunc.GetResult(minEvalCtx) 582 c.Assert(result.GetInt64(), Equals, int64(1)) 583 584 event.SetCauset(0, types.NewCauset(nil)) 585 err = maxFunc.UFIDelate(maxEvalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 586 c.Assert(err, IsNil) 587 result = maxFunc.GetResult(maxEvalCtx) 588 c.Assert(result.GetInt64(), Equals, int64(3)) 589 err = minFunc.UFIDelate(minEvalCtx, s.ctx.GetStochastikVars().StmtCtx, event.ToEvent()) 590 c.Assert(err, IsNil) 591 result = minFunc.GetResult(minEvalCtx) 592 c.Assert(result.GetInt64(), Equals, int64(1)) 593 partialResult := minFunc.GetPartialResult(minEvalCtx) 594 c.Assert(partialResult[0].GetInt64(), Equals, int64(1)) 595 }