forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdescriptor.go
335 lines (318 loc) · 12.3 KB
/
descriptor.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aggregation
import (
"bytes"
"fmt"
"math"
"strconv"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/planner/util"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
)
// AggFuncDesc describes an aggregation function signature, only used in planner.
type AggFuncDesc struct {
baseFuncDesc
// Mode represents the execution mode of the aggregation function.
Mode AggFunctionMode
// HasDistinct represents whether the aggregation function contains distinct attribute.
HasDistinct bool
// OrderByItems represents the order by clause used in GROUP_CONCAT
OrderByItems []*util.ByItems
}
// NewAggFuncDesc creates an aggregation function signature descriptor.
// this func cannot be called twice as the TypeInfer has changed the type of args in the first time.
func NewAggFuncDesc(ctx sessionctx.Context, name string, args []expression.Expression, hasDistinct bool) (*AggFuncDesc, error) {
b, err := newBaseFuncDesc(ctx, name, args)
if err != nil {
return nil, err
}
return &AggFuncDesc{baseFuncDesc: b, HasDistinct: hasDistinct}, nil
}
// NewAggFuncDescForWindowFunc creates an aggregation function from window functions, where baseFuncDesc may be ready.
func NewAggFuncDescForWindowFunc(ctx sessionctx.Context, Desc *WindowFuncDesc, hasDistinct bool) (*AggFuncDesc, error) {
if Desc.RetTp == nil { // safety check
return NewAggFuncDesc(ctx, Desc.Name, Desc.Args, hasDistinct)
}
return &AggFuncDesc{baseFuncDesc: baseFuncDesc{Desc.Name, Desc.Args, Desc.RetTp}, HasDistinct: hasDistinct}, nil
}
// String implements the fmt.Stringer interface.
func (a *AggFuncDesc) String() string {
buffer := bytes.NewBufferString(a.Name)
buffer.WriteString("(")
if a.HasDistinct {
buffer.WriteString("distinct ")
}
for i, arg := range a.Args {
buffer.WriteString(arg.String())
if i+1 != len(a.Args) {
buffer.WriteString(", ")
}
}
if len(a.OrderByItems) > 0 {
buffer.WriteString(" order by ")
}
for i, arg := range a.OrderByItems {
buffer.WriteString(arg.String())
if i+1 != len(a.OrderByItems) {
buffer.WriteString(", ")
}
}
buffer.WriteString(")")
return buffer.String()
}
// Equal checks whether two aggregation function signatures are equal.
func (a *AggFuncDesc) Equal(ctx sessionctx.Context, other *AggFuncDesc) bool {
if a.HasDistinct != other.HasDistinct {
return false
}
if len(a.OrderByItems) != len(other.OrderByItems) {
return false
}
for i := range a.OrderByItems {
if !a.OrderByItems[i].Equal(ctx, other.OrderByItems[i]) {
return false
}
}
return a.baseFuncDesc.equal(ctx, &other.baseFuncDesc)
}
// Clone copies an aggregation function signature totally.
func (a *AggFuncDesc) Clone() *AggFuncDesc {
clone := *a
clone.baseFuncDesc = *a.baseFuncDesc.clone()
clone.OrderByItems = make([]*util.ByItems, len(a.OrderByItems))
for i, byItem := range a.OrderByItems {
clone.OrderByItems[i] = byItem.Clone()
}
return &clone
}
// Split splits `a` into two aggregate descriptors for partial phase and
// final phase individually.
// This function is only used when executing aggregate function parallelly.
// ordinal indicates the column ordinal of the intermediate result.
func (a *AggFuncDesc) Split(ordinal []int) (partialAggDesc, finalAggDesc *AggFuncDesc) {
partialAggDesc = a.Clone()
if a.Mode == CompleteMode {
partialAggDesc.Mode = Partial1Mode
} else if a.Mode == FinalMode {
partialAggDesc.Mode = Partial2Mode
} else {
panic("Error happened during AggFuncDesc.Split, the AggFunctionMode is not CompleteMode or FinalMode.")
}
finalAggDesc = &AggFuncDesc{
Mode: FinalMode, // We only support FinalMode now in final phase.
HasDistinct: a.HasDistinct,
}
finalAggDesc.Name = a.Name
finalAggDesc.RetTp = a.RetTp
switch a.Name {
case ast.AggFuncAvg:
args := make([]expression.Expression, 0, 2)
args = append(args, &expression.Column{
Index: ordinal[0],
RetType: types.NewFieldType(mysql.TypeLonglong),
})
args = append(args, &expression.Column{
Index: ordinal[1],
RetType: a.RetTp,
})
finalAggDesc.Args = args
case ast.AggFuncApproxCountDistinct:
args := make([]expression.Expression, 0, 1)
args = append(args, &expression.Column{
Index: ordinal[0],
RetType: types.NewFieldType(mysql.TypeString),
})
finalAggDesc.Args = args
default:
args := make([]expression.Expression, 0, 1)
args = append(args, &expression.Column{
Index: ordinal[0],
RetType: a.RetTp,
})
finalAggDesc.Args = args
if finalAggDesc.Name == ast.AggFuncGroupConcat || finalAggDesc.Name == ast.AggFuncApproxPercentile {
finalAggDesc.Args = append(finalAggDesc.Args, a.Args[len(a.Args)-1]) // separator
}
}
return
}
// EvalNullValueInOuterJoin gets the null value when the aggregation is upon an outer join,
// and the aggregation function's input is null.
// If there is no matching row for the inner table of an outer join,
// an aggregation function only involves constant and/or columns belongs to the inner table
// will be set to the null value.
// The input stands for the schema of Aggregation's child. If the function can't produce a null value, the second
// return value will be false.
// e.g.
// Table t with only one row:
// +-------+---------+---------+
// | Table | Field | Type |
// +-------+---------+---------+
// | t | a | int(11) |
// +-------+---------+---------+
// +------+
// | a |
// +------+
// | 1 |
// +------+
//
// Table s which is empty:
// +-------+---------+---------+
// | Table | Field | Type |
// +-------+---------+---------+
// | s | a | int(11) |
// +-------+---------+---------+
//
// Query: `select t.a as `t.a`, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), s.a as `s.a`, avg(95) from t left join s on t.a = s.a;`
// +------+-----------+---------+---------+------------+-------------+------------+---------+---------+------+----------+
// | t.a | count(95) | sum(95) | avg(95) | bit_or(95) | bit_and(95) | bit_or(95) | max(95) | min(95) | s.a | avg(s.a) |
// +------+-----------+---------+---------+------------+-------------+------------+---------+---------+------+----------+
// | 1 | 1 | 95 | 95.0000 | 95 | 95 | 95 | 95 | 95 | NULL | NULL |
// +------+-----------+---------+---------+------------+-------------+------------+---------+---------+------+----------+
func (a *AggFuncDesc) EvalNullValueInOuterJoin(ctx sessionctx.Context, schema *expression.Schema) (types.Datum, bool) {
switch a.Name {
case ast.AggFuncCount:
return a.evalNullValueInOuterJoin4Count(ctx, schema)
case ast.AggFuncSum, ast.AggFuncMax, ast.AggFuncMin,
ast.AggFuncFirstRow:
return a.evalNullValueInOuterJoin4Sum(ctx, schema)
case ast.AggFuncAvg, ast.AggFuncGroupConcat:
return types.Datum{}, false
case ast.AggFuncBitAnd:
return a.evalNullValueInOuterJoin4BitAnd(ctx, schema)
case ast.AggFuncBitOr, ast.AggFuncBitXor:
return a.evalNullValueInOuterJoin4BitOr(ctx, schema)
default:
panic("unsupported agg function")
}
}
// GetAggFunc gets an evaluator according to the aggregation function signature.
func (a *AggFuncDesc) GetAggFunc(ctx sessionctx.Context) Aggregation {
aggFunc := aggFunction{AggFuncDesc: a}
switch a.Name {
case ast.AggFuncSum:
return &sumFunction{aggFunction: aggFunc}
case ast.AggFuncCount:
return &countFunction{aggFunction: aggFunc}
case ast.AggFuncAvg:
return &avgFunction{aggFunction: aggFunc}
case ast.AggFuncGroupConcat:
var s string
var err error
var maxLen uint64
s, err = variable.GetSessionOrGlobalSystemVar(ctx.GetSessionVars(), variable.GroupConcatMaxLen)
if err != nil {
panic(fmt.Sprintf("Error happened when GetAggFunc: no system variable named '%s'", variable.GroupConcatMaxLen))
}
maxLen, err = strconv.ParseUint(s, 10, 64)
if err != nil {
panic(fmt.Sprintf("Error happened when GetAggFunc: illegal value for system variable named '%s'", variable.GroupConcatMaxLen))
}
return &concatFunction{aggFunction: aggFunc, maxLen: maxLen}
case ast.AggFuncMax:
return &maxMinFunction{aggFunction: aggFunc, isMax: true, ctor: collate.GetCollator(a.Args[0].GetType().Collate)}
case ast.AggFuncMin:
return &maxMinFunction{aggFunction: aggFunc, isMax: false, ctor: collate.GetCollator(a.Args[0].GetType().Collate)}
case ast.AggFuncFirstRow:
return &firstRowFunction{aggFunction: aggFunc}
case ast.AggFuncBitOr:
return &bitOrFunction{aggFunction: aggFunc}
case ast.AggFuncBitXor:
return &bitXorFunction{aggFunction: aggFunc}
case ast.AggFuncBitAnd:
return &bitAndFunction{aggFunction: aggFunc}
default:
panic("unsupported agg function")
}
}
func (a *AggFuncDesc) evalNullValueInOuterJoin4Count(ctx sessionctx.Context, schema *expression.Schema) (types.Datum, bool) {
for _, arg := range a.Args {
result := expression.EvaluateExprWithNull(ctx, schema, arg)
con, ok := result.(*expression.Constant)
if !ok || con.Value.IsNull() {
return types.Datum{}, ok
}
}
return types.NewDatum(1), true
}
func (a *AggFuncDesc) evalNullValueInOuterJoin4Sum(ctx sessionctx.Context, schema *expression.Schema) (types.Datum, bool) {
result := expression.EvaluateExprWithNull(ctx, schema, a.Args[0])
con, ok := result.(*expression.Constant)
if !ok || con.Value.IsNull() {
return types.Datum{}, ok
}
return con.Value, true
}
func (a *AggFuncDesc) evalNullValueInOuterJoin4BitAnd(ctx sessionctx.Context, schema *expression.Schema) (types.Datum, bool) {
result := expression.EvaluateExprWithNull(ctx, schema, a.Args[0])
con, ok := result.(*expression.Constant)
if !ok || con.Value.IsNull() {
return types.NewDatum(uint64(math.MaxUint64)), true
}
return con.Value, true
}
func (a *AggFuncDesc) evalNullValueInOuterJoin4BitOr(ctx sessionctx.Context, schema *expression.Schema) (types.Datum, bool) {
result := expression.EvaluateExprWithNull(ctx, schema, a.Args[0])
con, ok := result.(*expression.Constant)
if !ok || con.Value.IsNull() {
return types.NewDatum(0), true
}
return con.Value, true
}
// UpdateNotNullFlag4RetType checks if we should remove the NotNull flag for the return type of the agg.
func (a *AggFuncDesc) UpdateNotNullFlag4RetType(hasGroupBy, allAggsFirstRow bool) error {
var removeNotNull bool
switch a.Name {
case ast.AggFuncCount, ast.AggFuncApproxCountDistinct, ast.AggFuncApproxPercentile,
ast.AggFuncBitAnd, ast.AggFuncBitOr, ast.AggFuncBitXor,
ast.WindowFuncFirstValue, ast.WindowFuncLastValue, ast.WindowFuncNthValue, ast.WindowFuncRowNumber,
ast.WindowFuncRank, ast.WindowFuncDenseRank, ast.WindowFuncCumeDist, ast.WindowFuncNtile, ast.WindowFuncPercentRank,
ast.WindowFuncLead, ast.WindowFuncLag, ast.AggFuncJsonObjectAgg, ast.AggFuncJsonArrayagg,
ast.AggFuncVarSamp, ast.AggFuncVarPop, ast.AggFuncStddevPop, ast.AggFuncStddevSamp:
removeNotNull = false
case ast.AggFuncSum, ast.AggFuncAvg, ast.AggFuncGroupConcat:
if !hasGroupBy {
removeNotNull = true
}
// `select max(a) from empty_tbl` returns `null`, while `select max(a) from empty_tbl group by b` returns empty.
case ast.AggFuncMax, ast.AggFuncMin:
if !hasGroupBy && a.RetTp.Tp != mysql.TypeBit {
removeNotNull = true
}
// `select distinct a from empty_tbl` returns empty
// `select a from empty_tbl group by b` returns empty
// `select a, max(a) from empty_tbl` returns `(null, null)`
// `select a, max(a) from empty_tbl group by b` returns empty
// `select a, count(a) from empty_tbl` returns `(null, 0)`
// `select a, count(a) from empty_tbl group by b` returns empty
case ast.AggFuncFirstRow:
if !allAggsFirstRow && !hasGroupBy {
removeNotNull = true
}
default:
return errors.Errorf("unsupported agg function: %s", a.Name)
}
if removeNotNull {
a.RetTp = a.RetTp.Clone()
a.RetTp.Flag &^= mysql.NotNullFlag
}
return nil
}