-
-
Notifications
You must be signed in to change notification settings - Fork 558
/
chunk_executor.go
265 lines (249 loc) · 8.36 KB
/
chunk_executor.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
// Copyright 2017 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 expression
import (
"strconv"
"github.com/hanchuanchuan/goInception/ast"
"github.com/hanchuanchuan/goInception/mysql"
"github.com/hanchuanchuan/goInception/sessionctx"
"github.com/hanchuanchuan/goInception/types"
"github.com/hanchuanchuan/goInception/util/chunk"
"github.com/pingcap/errors"
)
// Vectorizable checks whether a list of expressions can employ vectorized execution.
func Vectorizable(exprs []Expression) bool {
for _, expr := range exprs {
if hasUnVectorizableFunc(expr) {
return false
}
}
return true
}
// hasUnVectorizableFunc checks whether an expression contains functions that can not utilize the vectorized execution.
func hasUnVectorizableFunc(expr Expression) bool {
scalaFunc, ok := expr.(*ScalarFunction)
if !ok {
return false
}
if scalaFunc.FuncName.L == ast.SetVar {
return true
}
if scalaFunc.FuncName.L == ast.GetVar {
return true
}
for _, arg := range scalaFunc.GetArgs() {
if hasUnVectorizableFunc(arg) {
return true
}
}
return false
}
// VectorizedExecute evaluates a list of expressions column by column and append their results to "output" Chunk.
func VectorizedExecute(ctx sessionctx.Context, exprs []Expression, iterator *chunk.Iterator4Chunk, output *chunk.Chunk) error {
for colID, expr := range exprs {
err := evalOneColumn(ctx, expr, iterator, output, colID)
if err != nil {
return errors.Trace(err)
}
}
return nil
}
func evalOneColumn(ctx sessionctx.Context, expr Expression, iterator *chunk.Iterator4Chunk, output *chunk.Chunk, colID int) (err error) {
switch fieldType, evalType := expr.GetType(), expr.GetType().EvalType(); evalType {
case types.ETInt:
for row := iterator.Begin(); err == nil && row != iterator.End(); row = iterator.Next() {
err = executeToInt(ctx, expr, fieldType, row, output, colID)
}
case types.ETReal:
for row := iterator.Begin(); err == nil && row != iterator.End(); row = iterator.Next() {
err = executeToReal(ctx, expr, fieldType, row, output, colID)
}
case types.ETDecimal:
for row := iterator.Begin(); err == nil && row != iterator.End(); row = iterator.Next() {
err = executeToDecimal(ctx, expr, fieldType, row, output, colID)
}
case types.ETDatetime, types.ETTimestamp:
for row := iterator.Begin(); err == nil && row != iterator.End(); row = iterator.Next() {
err = executeToDatetime(ctx, expr, fieldType, row, output, colID)
}
case types.ETDuration:
for row := iterator.Begin(); err == nil && row != iterator.End(); row = iterator.Next() {
err = executeToDuration(ctx, expr, fieldType, row, output, colID)
}
case types.ETJson:
for row := iterator.Begin(); err == nil && row != iterator.End(); row = iterator.Next() {
err = executeToJSON(ctx, expr, fieldType, row, output, colID)
}
case types.ETString:
for row := iterator.Begin(); err == nil && row != iterator.End(); row = iterator.Next() {
err = executeToString(ctx, expr, fieldType, row, output, colID)
}
}
return errors.Trace(err)
}
func evalOneCell(ctx sessionctx.Context, expr Expression, row chunk.Row, output *chunk.Chunk, colID int) (err error) {
switch fieldType, evalType := expr.GetType(), expr.GetType().EvalType(); evalType {
case types.ETInt:
err = executeToInt(ctx, expr, fieldType, row, output, colID)
case types.ETReal:
err = executeToReal(ctx, expr, fieldType, row, output, colID)
case types.ETDecimal:
err = executeToDecimal(ctx, expr, fieldType, row, output, colID)
case types.ETDatetime, types.ETTimestamp:
err = executeToDatetime(ctx, expr, fieldType, row, output, colID)
case types.ETDuration:
err = executeToDuration(ctx, expr, fieldType, row, output, colID)
case types.ETJson:
err = executeToJSON(ctx, expr, fieldType, row, output, colID)
case types.ETString:
err = executeToString(ctx, expr, fieldType, row, output, colID)
}
return errors.Trace(err)
}
func executeToInt(ctx sessionctx.Context, expr Expression, fieldType *types.FieldType, row chunk.Row, output *chunk.Chunk, colID int) error {
res, isNull, err := expr.EvalInt(ctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
output.AppendNull(colID)
return nil
}
if fieldType.Tp == mysql.TypeBit {
output.AppendBytes(colID, strconv.AppendUint(make([]byte, 0, 8), uint64(res), 10))
return nil
}
if mysql.HasUnsignedFlag(fieldType.Flag) {
output.AppendUint64(colID, uint64(res))
return nil
}
output.AppendInt64(colID, res)
return nil
}
func executeToReal(ctx sessionctx.Context, expr Expression, fieldType *types.FieldType, row chunk.Row, output *chunk.Chunk, colID int) error {
res, isNull, err := expr.EvalReal(ctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
output.AppendNull(colID)
return nil
}
if fieldType.Tp == mysql.TypeFloat {
output.AppendFloat32(colID, float32(res))
return nil
}
output.AppendFloat64(colID, res)
return nil
}
func executeToDecimal(ctx sessionctx.Context, expr Expression, fieldType *types.FieldType, row chunk.Row, output *chunk.Chunk, colID int) error {
res, isNull, err := expr.EvalDecimal(ctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
output.AppendNull(colID)
return nil
}
output.AppendMyDecimal(colID, res)
return nil
}
func executeToDatetime(ctx sessionctx.Context, expr Expression, fieldType *types.FieldType, row chunk.Row, output *chunk.Chunk, colID int) error {
res, isNull, err := expr.EvalTime(ctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
output.AppendNull(colID)
} else {
output.AppendTime(colID, res)
}
return nil
}
func executeToDuration(ctx sessionctx.Context, expr Expression, fieldType *types.FieldType, row chunk.Row, output *chunk.Chunk, colID int) error {
res, isNull, err := expr.EvalDuration(ctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
output.AppendNull(colID)
} else {
output.AppendDuration(colID, res)
}
return nil
}
func executeToJSON(ctx sessionctx.Context, expr Expression, fieldType *types.FieldType, row chunk.Row, output *chunk.Chunk, colID int) error {
res, isNull, err := expr.EvalJSON(ctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
output.AppendNull(colID)
} else {
output.AppendJSON(colID, res)
}
return nil
}
func executeToString(ctx sessionctx.Context, expr Expression, fieldType *types.FieldType, row chunk.Row, output *chunk.Chunk, colID int) error {
res, isNull, err := expr.EvalString(ctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
output.AppendNull(colID)
} else if fieldType.Tp == mysql.TypeEnum {
val := types.Enum{Value: uint64(0), Name: res}
output.AppendEnum(colID, val)
} else if fieldType.Tp == mysql.TypeSet {
val := types.Set{Value: uint64(0), Name: res}
output.AppendSet(colID, val)
} else {
output.AppendString(colID, res)
}
return nil
}
// VectorizedFilter applies a list of filters to a Chunk and
// returns a bool slice, which indicates whether a row is passed the filters.
// Filters is executed vectorized.
func VectorizedFilter(ctx sessionctx.Context, filters []Expression, iterator *chunk.Iterator4Chunk, selected []bool) ([]bool, error) {
selected = selected[:0]
for i, numRows := 0, iterator.Len(); i < numRows; i++ {
selected = append(selected, true)
}
for _, filter := range filters {
isIntType := true
if filter.GetType().EvalType() != types.ETInt {
isIntType = false
}
for row := iterator.Begin(); row != iterator.End(); row = iterator.Next() {
if !selected[row.Idx()] {
continue
}
if isIntType {
filterResult, isNull, err := filter.EvalInt(ctx, row)
if err != nil {
return nil, errors.Trace(err)
}
selected[row.Idx()] = selected[row.Idx()] && !isNull && (filterResult != 0)
} else {
// TODO: should rewrite the filter to `cast(expr as SIGNED) != 0` and always use `EvalInt`.
bVal, err := EvalBool(ctx, []Expression{filter}, row)
if err != nil {
return nil, errors.Trace(err)
}
selected[row.Idx()] = selected[row.Idx()] && bVal
}
}
}
return selected, nil
}