-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
projection.go
189 lines (170 loc) · 5.06 KB
/
projection.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
/*
Copyright 2022 The Vitess Authors.
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 engine
import (
"context"
"sync"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/vtgate/evalengine"
)
var _ Primitive = (*Projection)(nil)
// Projection can evaluate expressions and project the results
type Projection struct {
Cols []string
Exprs []evalengine.Expr
Input Primitive
noTxNeeded
}
// RouteType implements the Primitive interface
func (p *Projection) RouteType() string {
return p.Input.RouteType()
}
// GetKeyspaceName implements the Primitive interface
func (p *Projection) GetKeyspaceName() string {
return p.Input.GetKeyspaceName()
}
// GetTableName implements the Primitive interface
func (p *Projection) GetTableName() string {
return p.Input.GetTableName()
}
// TryExecute implements the Primitive interface
func (p *Projection) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
result, err := vcursor.ExecutePrimitive(ctx, p.Input, bindVars, wantfields)
if err != nil {
return nil, err
}
env := evalengine.NewExpressionEnv(ctx, bindVars, vcursor)
var resultRows []sqltypes.Row
for _, row := range result.Rows {
resultRow := make(sqltypes.Row, 0, len(p.Exprs))
env.Row = row
for _, exp := range p.Exprs {
c, err := env.Evaluate(exp)
if err != nil {
return nil, err
}
resultRow = append(resultRow, c.Value(vcursor.ConnCollation()))
}
resultRows = append(resultRows, resultRow)
}
if wantfields {
result.Fields, err = p.evalFields(env, result.Fields, vcursor)
if err != nil {
return nil, err
}
}
result.Rows = resultRows
return result, nil
}
// TryStreamExecute implements the Primitive interface
func (p *Projection) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
env := evalengine.NewExpressionEnv(ctx, bindVars, vcursor)
var once sync.Once
var fields []*querypb.Field
return vcursor.StreamExecutePrimitive(ctx, p.Input, bindVars, wantfields, func(qr *sqltypes.Result) error {
var err error
if wantfields {
once.Do(func() {
fields, err = p.evalFields(env, qr.Fields, vcursor)
if err != nil {
return
}
err = callback(&sqltypes.Result{Fields: fields})
if err != nil {
return
}
})
qr.Fields = fields
}
if err != nil {
return err
}
resultRows := make([]sqltypes.Row, 0, len(qr.Rows))
for _, r := range qr.Rows {
resultRow := make(sqltypes.Row, 0, len(p.Exprs))
env.Row = r
for _, exp := range p.Exprs {
c, err := env.Evaluate(exp)
if err != nil {
return err
}
resultRow = append(resultRow, c.Value(vcursor.ConnCollation()))
}
resultRows = append(resultRows, resultRow)
}
qr.Rows = resultRows
return callback(qr)
})
}
// GetFields implements the Primitive interface
func (p *Projection) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
qr, err := p.Input.GetFields(ctx, vcursor, bindVars)
if err != nil {
return nil, err
}
env := evalengine.NewExpressionEnv(ctx, bindVars, vcursor)
qr.Fields, err = p.evalFields(env, qr.Fields, vcursor)
if err != nil {
return nil, err
}
return qr, nil
}
func (p *Projection) evalFields(env *evalengine.ExpressionEnv, infields []*querypb.Field, vcursor VCursor) ([]*querypb.Field, error) {
var fields []*querypb.Field
for i, col := range p.Cols {
q, f, err := env.TypeOf(p.Exprs[i], infields)
if err != nil {
return nil, err
}
var cs collations.ID = collations.CollationBinaryID
if sqltypes.IsText(q) {
cs = vcursor.ConnCollation()
}
fl := mysql.FlagsForColumn(q, cs)
if !sqltypes.IsNull(q) && !f.Nullable() {
fl |= uint32(querypb.MySqlFlag_NOT_NULL_FLAG)
}
fields = append(fields, &querypb.Field{
Name: col,
Type: q,
Charset: uint32(cs),
Flags: fl,
})
}
return fields, nil
}
// Inputs implements the Primitive interface
func (p *Projection) Inputs() []Primitive {
return []Primitive{p.Input}
}
// description implements the Primitive interface
func (p *Projection) description() PrimitiveDescription {
var exprs []string
for idx, e := range p.Exprs {
expr := evalengine.FormatExpr(e)
alias := p.Cols[idx]
if alias != "" {
expr += " as " + alias
}
exprs = append(exprs, expr)
}
return PrimitiveDescription{
OperatorType: "Projection",
Other: map[string]any{
"Expressions": exprs,
},
}
}