forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate_test.go
119 lines (107 loc) · 2.55 KB
/
aggregate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2015 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 executor
import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/optimizer/evaluator"
"github.com/pingcap/tidb/util/mock"
)
var _ = Suite(&testAggFuncSuite{})
type testAggFuncSuite struct {
}
func (s *testAggFuncSuite) SetUpSuite(c *C) {
}
func (s *testAggFuncSuite) TearDownSuite(c *C) {
}
type mockExec struct {
fields []*ast.ResultField
rows []*Row
curRowIdx int
}
func (m *mockExec) Fields() []*ast.ResultField {
return m.fields
}
func (m *mockExec) Next() (*Row, error) {
if m.curRowIdx >= len(m.rows) {
return nil, nil
}
r := m.rows[m.curRowIdx]
m.curRowIdx++
for i, d := range r.Data {
m.fields[i].Expr.SetValue(d)
}
return r, nil
}
func (m *mockExec) Close() error {
return nil
}
func (s *testAggFuncSuite) TestCount(c *C) {
// Compose aggregate exec for "select c1, count(c2) from t";
// c1 c2
// 1 1
// 2 1
// 3 nil
c1 := ast.NewValueExpr(0)
rf1 := &ast.ResultField{Expr: c1}
col1 := &ast.ColumnNameExpr{Refer: rf1}
fc1 := &ast.AggregateFuncExpr{
F: ast.AggFuncFirstRow,
Args: []ast.ExprNode{col1},
}
c2 := ast.NewValueExpr(0)
rf2 := &ast.ResultField{Expr: c2}
col2 := &ast.ColumnNameExpr{Refer: rf2}
fc2 := &ast.AggregateFuncExpr{
F: ast.AggFuncCount,
Args: []ast.ExprNode{col2},
}
row1 := []interface{}{1, 1}
row2 := []interface{}{2, 1}
row3 := []interface{}{3, nil}
data := []([]interface{}){row1, row2, row3}
rows := make([]*Row, 0, 3)
for _, d := range data {
rows = append(rows, &Row{Data: d})
}
src := &mockExec{
rows: rows,
fields: []*ast.ResultField{rf1, rf2},
}
agg := &AggregateExec{
AggFuncs: []*ast.AggregateFuncExpr{fc1, fc2},
Src: src,
}
var (
row *Row
cnt int
)
for {
r, err := agg.Next()
c.Assert(err, IsNil)
if r == nil {
break
}
row = r
cnt++
}
c.Assert(cnt, Equals, 1)
c.Assert(row, NotNil)
ctx := mock.NewContext()
val, err := evaluator.Eval(ctx, fc1)
c.Assert(err, IsNil)
c.Assert(val, Equals, 1)
val, err = evaluator.Eval(ctx, fc2)
c.Assert(err, IsNil)
c.Assert(val, Equals, int64(2))
}