forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeinferer.go
351 lines (333 loc) · 10.7 KB
/
typeinferer.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// 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 optimizer
import (
"strings"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/types"
)
// InferType infers result type for ast.ExprNode.
func InferType(node ast.Node) error {
var inferrer typeInferrer
// TODO: get the default charset from ctx
inferrer.defaultCharset = "utf8"
node.Accept(&inferrer)
return inferrer.err
}
type typeInferrer struct {
err error
defaultCharset string
}
func (v *typeInferrer) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
return in, false
}
func (v *typeInferrer) Leave(in ast.Node) (out ast.Node, ok bool) {
switch x := in.(type) {
case *ast.AggregateFuncExpr:
v.aggregateFunc(x)
case *ast.BetweenExpr:
x.SetType(types.NewFieldType(mysql.TypeLonglong))
x.Type.Charset = charset.CharsetBin
x.Type.Collate = charset.CollationBin
case *ast.BinaryOperationExpr:
v.binaryOperation(x)
case *ast.CaseExpr:
v.handleCaseExpr(x)
case *ast.ColumnNameExpr:
x.SetType(&x.Refer.Column.FieldType)
case *ast.CompareSubqueryExpr:
x.SetType(types.NewFieldType(mysql.TypeLonglong))
x.Type.Charset = charset.CharsetBin
x.Type.Collate = charset.CollationBin
case *ast.ExistsSubqueryExpr:
x.SetType(types.NewFieldType(mysql.TypeLonglong))
x.Type.Charset = charset.CharsetBin
x.Type.Collate = charset.CollationBin
case *ast.FuncCallExpr:
v.handleFuncCallExpr(x)
case *ast.FuncCastExpr:
// Copy a new field type.
tp := *x.Tp
x.SetType(&tp)
if len(x.Type.Charset) == 0 {
x.Type.Charset, x.Type.Collate = types.DefaultCharsetForType(x.Type.Tp)
}
case *ast.IsNullExpr:
x.SetType(types.NewFieldType(mysql.TypeLonglong))
x.Type.Charset = charset.CharsetBin
x.Type.Collate = charset.CollationBin
case *ast.IsTruthExpr:
x.SetType(types.NewFieldType(mysql.TypeLonglong))
x.Type.Charset = charset.CharsetBin
x.Type.Collate = charset.CollationBin
case *ast.ParamMarkerExpr:
x.SetType(types.DefaultTypeForValue(x.GetValue()))
case *ast.ParenthesesExpr:
x.SetType(x.Expr.GetType())
case *ast.PatternInExpr:
x.SetType(types.NewFieldType(mysql.TypeLonglong))
x.Type.Charset = charset.CharsetBin
x.Type.Collate = charset.CollationBin
case *ast.PatternLikeExpr:
x.SetType(types.NewFieldType(mysql.TypeLonglong))
x.Type.Charset = charset.CharsetBin
x.Type.Collate = charset.CollationBin
case *ast.PatternRegexpExpr:
x.SetType(types.NewFieldType(mysql.TypeLonglong))
x.Type.Charset = charset.CharsetBin
x.Type.Collate = charset.CollationBin
case *ast.SelectStmt:
v.selectStmt(x)
case *ast.UnaryOperationExpr:
v.unaryOperation(x)
case *ast.ValueExpr:
v.handleValueExpr(x)
case *ast.ValuesExpr:
v.handleValuesExpr(x)
case *ast.VariableExpr:
x.SetType(types.NewFieldType(mysql.TypeVarString))
x.Type.Charset = v.defaultCharset
cln, err := charset.GetDefaultCollation(v.defaultCharset)
if err != nil {
v.err = err
}
x.Type.Collate = cln
// TODO: handle all expression types.
}
return in, true
}
func (v *typeInferrer) selectStmt(x *ast.SelectStmt) {
rf := x.GetResultFields()
for _, val := range rf {
// column ID is 0 means it is not a real column from table, but a temporary column,
// so its type is not pre-defined, we need to set it.
if val.Column.ID == 0 && val.Expr.GetType() != nil {
val.Column.FieldType = *(val.Expr.GetType())
}
}
}
func (v *typeInferrer) aggregateFunc(x *ast.AggregateFuncExpr) {
name := strings.ToLower(x.F)
switch name {
case ast.AggFuncCount:
ft := types.NewFieldType(mysql.TypeLonglong)
ft.Flen = 21
ft.Charset = charset.CharsetBin
ft.Collate = charset.CollationBin
x.SetType(ft)
case ast.AggFuncMax, ast.AggFuncMin:
x.SetType(x.Args[0].GetType())
case ast.AggFuncSum, ast.AggFuncAvg:
ft := types.NewFieldType(mysql.TypeNewDecimal)
ft.Charset = charset.CharsetBin
ft.Collate = charset.CollationBin
x.SetType(ft)
case ast.AggFuncGroupConcat:
ft := types.NewFieldType(mysql.TypeVarString)
ft.Charset = v.defaultCharset
cln, err := charset.GetDefaultCollation(v.defaultCharset)
if err != nil {
v.err = err
}
ft.Collate = cln
x.SetType(ft)
}
}
func (v *typeInferrer) binaryOperation(x *ast.BinaryOperationExpr) {
switch x.Op {
case opcode.AndAnd, opcode.OrOr, opcode.LogicXor:
x.Type = types.NewFieldType(mysql.TypeLonglong)
case opcode.LT, opcode.LE, opcode.GE, opcode.GT, opcode.EQ, opcode.NE, opcode.NullEQ:
x.Type = types.NewFieldType(mysql.TypeLonglong)
case opcode.RightShift, opcode.LeftShift, opcode.And, opcode.Or, opcode.Xor:
x.Type = types.NewFieldType(mysql.TypeLonglong)
x.Type.Flag |= mysql.UnsignedFlag
case opcode.IntDiv:
x.Type = types.NewFieldType(mysql.TypeLonglong)
case opcode.Plus, opcode.Minus, opcode.Mul, opcode.Mod:
if x.L.GetType() != nil && x.R.GetType() != nil {
xTp := mergeArithType(x.L.GetType().Tp, x.R.GetType().Tp)
x.Type = types.NewFieldType(xTp)
leftUnsigned := x.L.GetType().Flag & mysql.UnsignedFlag
rightUnsigned := x.R.GetType().Flag & mysql.UnsignedFlag
// If both operands are unsigned, result is unsigned.
x.Type.Flag |= (leftUnsigned & rightUnsigned)
}
case opcode.Div:
if x.L.GetType() != nil && x.R.GetType() != nil {
xTp := mergeArithType(x.L.GetType().Tp, x.R.GetType().Tp)
if xTp == mysql.TypeLonglong {
xTp = mysql.TypeDecimal
}
x.Type = types.NewFieldType(xTp)
}
}
x.Type.Charset = charset.CharsetBin
x.Type.Collate = charset.CollationBin
}
func mergeArithType(a, b byte) byte {
switch a {
case mysql.TypeString, mysql.TypeVarchar, mysql.TypeVarString, mysql.TypeDouble, mysql.TypeFloat:
return mysql.TypeDouble
}
switch b {
case mysql.TypeString, mysql.TypeVarchar, mysql.TypeVarString, mysql.TypeDouble, mysql.TypeFloat:
return mysql.TypeDouble
}
if a == mysql.TypeNewDecimal || b == mysql.TypeNewDecimal {
return mysql.TypeNewDecimal
}
return mysql.TypeLonglong
}
func (v *typeInferrer) unaryOperation(x *ast.UnaryOperationExpr) {
switch x.Op {
case opcode.Not:
x.Type = types.NewFieldType(mysql.TypeLonglong)
case opcode.BitNeg:
x.Type = types.NewFieldType(mysql.TypeLonglong)
x.Type.Flag |= mysql.UnsignedFlag
case opcode.Plus:
x.Type = x.V.GetType()
case opcode.Minus:
x.Type = types.NewFieldType(mysql.TypeLonglong)
if x.V.GetType() != nil {
switch x.V.GetType().Tp {
case mysql.TypeString, mysql.TypeVarchar, mysql.TypeVarString, mysql.TypeDouble, mysql.TypeFloat:
x.Type.Tp = mysql.TypeDouble
case mysql.TypeNewDecimal:
x.Type.Tp = mysql.TypeNewDecimal
}
}
}
x.Type.Charset = charset.CharsetBin
x.Type.Collate = charset.CollationBin
}
func (v *typeInferrer) handleValueExpr(x *ast.ValueExpr) {
tp := types.DefaultTypeForValue(x.GetValue())
// Set charset and collation
x.SetType(tp)
}
func (v *typeInferrer) handleValuesExpr(x *ast.ValuesExpr) {
x.SetType(x.Column.GetType())
}
func (v *typeInferrer) getFsp(x *ast.FuncCallExpr) int {
if len(x.Args) == 1 {
a := x.Args[0].GetValue()
fsp, err := types.ToInt64(a)
if err != nil {
v.err = err
}
return int(fsp)
}
return 0
}
func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) {
var (
tp *types.FieldType
chs = charset.CharsetBin
)
switch x.FnName.L {
case "abs", "ifnull", "nullif":
tp = x.Args[0].GetType()
case "pow", "power", "rand":
tp = types.NewFieldType(mysql.TypeDouble)
case "curdate", "current_date", "date":
tp = types.NewFieldType(mysql.TypeDate)
case "curtime", "current_time":
tp = types.NewFieldType(mysql.TypeDuration)
tp.Decimal = v.getFsp(x)
case "current_timestamp", "date_arith":
tp = types.NewFieldType(mysql.TypeDatetime)
case "microsecond", "second", "minute", "hour", "day", "week", "month", "year",
"dayofweek", "dayofmonth", "dayofyear", "weekday", "weekofyear", "yearweek",
"found_rows", "length", "extract", "locate":
tp = types.NewFieldType(mysql.TypeLonglong)
case "now", "sysdate":
tp = types.NewFieldType(mysql.TypeDatetime)
tp.Decimal = v.getFsp(x)
case "dayname", "version", "database", "user", "current_user",
"concat", "concat_ws", "left", "lower", "repeat", "replace", "upper", "convert",
"substring", "substring_index", "trim":
tp = types.NewFieldType(mysql.TypeVarString)
chs = v.defaultCharset
case "strcmp":
tp = types.NewFieldType(mysql.TypeLonglong)
case "connection_id":
tp = types.NewFieldType(mysql.TypeLonglong)
tp.Flag |= mysql.UnsignedFlag
case "if":
// TODO: fix this
// See: https://dev.mysql.com/doc/refman/5.5/en/control-flow-functions.html#function_if
// The default return type of IF() (which may matter when it is stored into a temporary table) is calculated as follows.
// Expression Return Value
// expr2 or expr3 returns a string string
// expr2 or expr3 returns a floating-point value floating-point
// expr2 or expr3 returns an integer integer
tp = x.Args[1].GetType()
default:
tp = types.NewFieldType(mysql.TypeUnspecified)
}
// If charset is unspecified.
if len(tp.Charset) == 0 {
tp.Charset = chs
cln := charset.CollationBin
if chs != charset.CharsetBin {
var err error
cln, err = charset.GetDefaultCollation(chs)
if err != nil {
v.err = err
}
}
tp.Collate = cln
}
x.SetType(tp)
}
// The return type of a CASE expression is the compatible aggregated type of all return values,
// but also depends on the context in which it is used.
// If used in a string context, the result is returned as a string.
// If used in a numeric context, the result is returned as a decimal, real, or integer value.
func (v *typeInferrer) handleCaseExpr(x *ast.CaseExpr) {
var currType types.FieldType
for _, w := range x.WhenClauses {
t := w.Result.GetType()
if currType.Tp == mysql.TypeUnspecified {
currType = *t
continue
}
mtp := types.MergeFieldType(currType.Tp, t.Tp)
if mtp == t.Tp && mtp != currType.Tp {
currType.Charset = t.Charset
currType.Collate = t.Collate
}
currType.Tp = mtp
}
if x.ElseClause != nil {
t := x.ElseClause.GetType()
if currType.Tp == mysql.TypeUnspecified {
currType = *t
} else {
mtp := types.MergeFieldType(currType.Tp, t.Tp)
if mtp == t.Tp && mtp != currType.Tp {
currType.Charset = t.Charset
currType.Collate = t.Collate
}
currType.Tp = mtp
}
}
x.SetType(&currType)
// TODO: We need a better way to set charset/collation
x.Type.Charset, x.Type.Collate = types.DefaultCharsetForType(x.Type.Tp)
}