forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
column.go
242 lines (205 loc) · 7.69 KB
/
column.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
// Copyright 2016 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 (
"bytes"
"fmt"
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/types"
)
// CorrelatedColumn stands for a column in a correlated sub query.
type CorrelatedColumn struct {
Column
Data *types.Datum
}
// Clone implements Expression interface.
func (col *CorrelatedColumn) Clone() Expression {
return col
}
// Eval implements Expression interface.
func (col *CorrelatedColumn) Eval(row []types.Datum) (types.Datum, error) {
return *col.Data, nil
}
// EvalInt returns int representation of CorrelatedColumn.
func (col *CorrelatedColumn) EvalInt(row []types.Datum, sc *variable.StatementContext) (int64, bool, error) {
val, isNull, err := evalExprToInt(col, row, sc)
return val, isNull, errors.Trace(err)
}
// EvalReal returns real representation of CorrelatedColumn.
func (col *CorrelatedColumn) EvalReal(row []types.Datum, sc *variable.StatementContext) (float64, bool, error) {
val, isNull, err := evalExprToReal(col, row, sc)
return val, isNull, errors.Trace(err)
}
// EvalString returns string representation of CorrelatedColumn.
func (col *CorrelatedColumn) EvalString(row []types.Datum, sc *variable.StatementContext) (string, bool, error) {
val, isNull, err := evalExprToString(col, row, sc)
return val, isNull, errors.Trace(err)
}
// EvalDecimal returns decimal representation of CorrelatedColumn.
func (col *CorrelatedColumn) EvalDecimal(row []types.Datum, sc *variable.StatementContext) (*types.MyDecimal, bool, error) {
val, isNull, err := evalExprToDecimal(col, row, sc)
return val, isNull, errors.Trace(err)
}
// EvalTime returns DATE/DATETIME/TIMESTAMP representation of CorrelatedColumn.
func (col *CorrelatedColumn) EvalTime(row []types.Datum, sc *variable.StatementContext) (types.Time, bool, error) {
val, isNull, err := evalExprToTime(col, row, sc)
return val, isNull, errors.Trace(err)
}
// EvalDuration returns Duration representation of CorrelatedColumn.
func (col *CorrelatedColumn) EvalDuration(row []types.Datum, sc *variable.StatementContext) (types.Duration, bool, error) {
val, isNull, err := evalExprToDuration(col, row, sc)
return val, isNull, errors.Trace(err)
}
// Equal implements Expression interface.
func (col *CorrelatedColumn) Equal(expr Expression, ctx context.Context) bool {
if cc, ok := expr.(*CorrelatedColumn); ok {
return col.Column.Equal(&cc.Column, ctx)
}
return false
}
// IsCorrelated implements Expression interface.
func (col *CorrelatedColumn) IsCorrelated() bool {
return true
}
// Decorrelate implements Expression interface.
func (col *CorrelatedColumn) Decorrelate(schema *Schema) Expression {
if !schema.Contains(&col.Column) {
return col
}
return &col.Column
}
// ResolveIndices implements Expression interface.
func (col *CorrelatedColumn) ResolveIndices(_ *Schema) {
}
// Column represents a column.
type Column struct {
FromID string
ColName model.CIStr
DBName model.CIStr
TblName model.CIStr
RetType *types.FieldType
ID int64
// Position means the position of this column that appears in the select fields.
// e.g. SELECT name as id , 1 - id as id , 1 + name as id, name as id from src having id = 1;
// There are four ids in the same schema, so you can't identify the column through the FromID and ColName.
Position int
// IsAggOrSubq means if this column is referenced to a Aggregation column or a Subquery column.
// If so, this column's name will be the plain sql text.
IsAggOrSubq bool
// Index is only used for execution.
Index int
hashcode []byte
}
// Equal implements Expression interface.
func (col *Column) Equal(expr Expression, _ context.Context) bool {
if newCol, ok := expr.(*Column); ok {
return newCol.FromID == col.FromID && newCol.Position == col.Position
}
return false
}
// String implements Stringer interface.
func (col *Column) String() string {
result := col.ColName.L
if col.TblName.L != "" {
result = col.TblName.L + "." + result
}
if col.DBName.L != "" {
result = col.DBName.L + "." + result
}
return result
}
// MarshalJSON implements json.Marshaler interface.
func (col *Column) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(fmt.Sprintf("\"%s\"", col))
return buffer.Bytes(), nil
}
// GetType implements Expression interface.
func (col *Column) GetType() *types.FieldType {
return col.RetType
}
// Eval implements Expression interface.
func (col *Column) Eval(row []types.Datum) (types.Datum, error) {
return row[col.Index], nil
}
// EvalInt returns int representation of Column.
func (col *Column) EvalInt(row []types.Datum, sc *variable.StatementContext) (int64, bool, error) {
val, isNull, err := evalExprToInt(col, row, sc)
return val, isNull, errors.Trace(err)
}
// EvalReal returns real representation of Column.
func (col *Column) EvalReal(row []types.Datum, sc *variable.StatementContext) (float64, bool, error) {
val, isNull, err := evalExprToReal(col, row, sc)
return val, isNull, errors.Trace(err)
}
// EvalString returns string representation of Column.
func (col *Column) EvalString(row []types.Datum, sc *variable.StatementContext) (string, bool, error) {
val, isNull, err := evalExprToString(col, row, sc)
return val, isNull, errors.Trace(err)
}
// EvalDecimal returns decimal representation of Column.
func (col *Column) EvalDecimal(row []types.Datum, sc *variable.StatementContext) (*types.MyDecimal, bool, error) {
val, isNull, err := evalExprToDecimal(col, row, sc)
return val, isNull, errors.Trace(err)
}
// EvalTime returns DATE/DATETIME/TIMESTAMP representation of Column.
func (col *Column) EvalTime(row []types.Datum, sc *variable.StatementContext) (types.Time, bool, error) {
val, isNull, err := evalExprToTime(col, row, sc)
return val, isNull, errors.Trace(err)
}
// EvalDuration returns Duration representation of Column.
func (col *Column) EvalDuration(row []types.Datum, sc *variable.StatementContext) (types.Duration, bool, error) {
val, isNull, err := evalExprToDuration(col, row, sc)
return val, isNull, errors.Trace(err)
}
// Clone implements Expression interface.
func (col *Column) Clone() Expression {
newCol := *col
return &newCol
}
// IsCorrelated implements Expression interface.
func (col *Column) IsCorrelated() bool {
return false
}
// Decorrelate implements Expression interface.
func (col *Column) Decorrelate(_ *Schema) Expression {
return col
}
// HashCode implements Expression interface.
func (col *Column) HashCode() []byte {
if len(col.hashcode) != 0 {
return col.hashcode
}
col.hashcode, _ = codec.EncodeValue(col.hashcode, types.NewStringDatum(col.FromID), types.NewIntDatum(int64(col.Position)))
return col.hashcode
}
// ResolveIndices implements Expression interface.
func (col *Column) ResolveIndices(schema *Schema) {
col.Index = schema.ColumnIndex(col)
// If col's index equals to -1, it means a internal logic error happens.
if col.Index == -1 {
log.Errorf("Can't find column %s in schema %s", col, schema)
}
}
// Column2Exprs will transfer column slice to expression slice.
func Column2Exprs(cols []*Column) []Expression {
result := make([]Expression, 0, len(cols))
for _, col := range cols {
result = append(result, col.Clone())
}
return result
}