/
rename_fields.go
127 lines (108 loc) · 3.59 KB
/
rename_fields.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
/*
Copyright 2021 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"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/vterrors"
)
var _ Primitive = (*RenameFields)(nil)
// RenameFields is a primitive that renames the fields
type RenameFields struct {
noTxNeeded
Cols []string
Indices []int
Input Primitive
}
// NewRenameField creates a new rename field
func NewRenameField(cols []string, indices []int, input Primitive) (*RenameFields, error) {
if len(cols) != len(indices) {
return nil, vterrors.VT13001("number of columns does not match number of indices in RenameField primitive")
}
return &RenameFields{
Cols: cols,
Indices: indices,
Input: input,
}, nil
}
// RouteType implements the primitive interface
func (r *RenameFields) RouteType() string {
return r.Input.RouteType()
}
// GetKeyspaceName implements the primitive interface
func (r *RenameFields) GetKeyspaceName() string {
return r.Input.GetKeyspaceName()
}
// GetTableName implements the primitive interface
func (r *RenameFields) GetTableName() string {
return r.Input.GetTableName()
}
// TryExecute implements the Primitive interface
func (r *RenameFields) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
qr, err := vcursor.ExecutePrimitive(ctx, r.Input, bindVars, wantfields)
if err != nil {
return nil, err
}
if wantfields {
r.renameFields(qr)
}
return qr, nil
}
func (r *RenameFields) renameFields(qr *sqltypes.Result) {
for ind, index := range r.Indices {
if index >= len(qr.Fields) {
continue
}
colName := r.Cols[ind]
qr.Fields[index].Name = colName
}
}
// TryStreamExecute implements the Primitive interface
func (r *RenameFields) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
if wantfields {
innerCallback := callback
callback = func(result *sqltypes.Result) error {
// Only the first callback will contain the fields.
// This check is to avoid going over the RenameFields indices when no fields are present in the result set.
if len(result.Fields) != 0 {
r.renameFields(result)
}
return innerCallback(result)
}
}
return vcursor.StreamExecutePrimitive(ctx, r.Input, bindVars, wantfields, callback)
}
// GetFields implements the primitive interface
func (r *RenameFields) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
qr, err := r.Input.GetFields(ctx, vcursor, bindVars)
if err != nil {
return nil, err
}
r.renameFields(qr)
return qr, nil
}
// Inputs implements the primitive interface
func (r *RenameFields) Inputs() ([]Primitive, []map[string]any) {
return []Primitive{r.Input}, nil
}
// description implements the primitive interface
func (r *RenameFields) description() PrimitiveDescription {
return PrimitiveDescription{
OperatorType: "RenameFields",
Other: map[string]any{
"Indices": r.Indices,
"Columns": r.Cols,
},
}
}