Debian Codegraph @

dependency/sqltypes/plan_value.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package sqltypes
18
19 import (
20 "encoding/json"
21 "errors"
22 "fmt"
23
24 "github.com/xwb1989/sqlparser/dependency/querypb"
25 )
26
27 // PlanValue represents a value or a list of values for
28 // a column that will later be resolved using bind vars and used
29 // to perform plan actions like generating the final query or
30 // deciding on a route.
31 //
32 // Plan values are typically used as a slice ([]planValue)
33 // where each entry is for one column. For situations where
34 // the required output is a list of rows (like in the case
35 // of multi-value inserts), the representation is pivoted.
36 // For example, a statement like this:
37 // INSERT INTO t VALUES (1, 2), (3, 4)
38 // will be represented as follows:
39 // []PlanValue{
40 // Values: {1, 3},
41 // Values: {2, 4},
42 // }
43 //
44 // For WHERE clause items that contain a combination of
45 // equality expressions and IN clauses like this:
46 // WHERE pk1 = 1 AND pk2 IN (2, 3, 4)
47 // The plan values will be represented as follows:
48 // []PlanValue{
49 // Value: 1,
50 // Values: {2, 3, 4},
51 // }
52 // When converted into rows, columns with single values
53 // are replicated as the same for all rows:
54 // [][]Value{
55 // {1, 2},
56 // {1, 3},
57 // {1, 4},
58 // }
59 type PlanValue struct {
60 Key string
61 Value Value
62 ListKey string
63 Values []PlanValue
64 }
65
66 // IsNull returns true if the PlanValue is NULL.
67 func (pv PlanValue) IsNull() bool {
68 return pv.Key == "" && pv.Value.IsNull() && pv.ListKey == "" && pv.Values == nil
69 }
70
71 // IsList returns true if the PlanValue is a list.
72 func (pv PlanValue) IsList() bool {
73 return pv.ListKey != "" || pv.Values != nil
74 }
75
76 // ResolveValue resolves a PlanValue as a single value based on the supplied bindvars.
77 func (pv PlanValue) ResolveValue(bindVars map[string]*querypb.BindVariable) (Value, error) {
78 switch {
79 case pv.Key != "":
80 bv, err := pv.lookupValue(bindVars)
81 if err != nil {
82 return NULL, err
83 }
84 return MakeTrusted(bv.Type, bv.Value), nil
85 case !pv.Value.IsNull():
86 return pv.Value, nil
87 case pv.ListKey != "" || pv.Values != nil:
88 // This code is unreachable because the parser does not allow
89 // multi-value constructs where a single value is expected.
90 return NULL, errors.New("a list was supplied where a single value was expected")
91 }
92 return NULL, nil
93 }
94
95 func (pv PlanValue) lookupValue(bindVars map[string]*querypb.BindVariable) (*querypb.BindVariable, error) {
96 bv, ok := bindVars[pv.Key]
97 if !ok {
98 return nil, fmt.Errorf("missing bind var %s", pv.Key)
99 }
100 if bv.Type == querypb.Type_TUPLE {
101 return nil, fmt.Errorf("TUPLE was supplied for single value bind var %s", pv.ListKey)
102 }
103 return bv, nil
104 }
105
106 // ResolveList resolves a PlanValue as a list of values based on the supplied bindvars.
107 func (pv PlanValue) ResolveList(bindVars map[string]*querypb.BindVariable) ([]Value, error) {
108 switch {
109 case pv.ListKey != "":
110 bv, err := pv.lookupList(bindVars)
111 if err != nil {
112 return nil, err
113 }
114 values := make([]Value, 0, len(bv.Values))
115 for _, val := range bv.Values {
116 values = append(values, MakeTrusted(val.Type, val.Value))
117 }
118 return values, nil
119 case pv.Values != nil:
120 values := make([]Value, 0, len(pv.Values))
121 for _, val := range pv.Values {
122 v, err := val.ResolveValue(bindVars)
123 if err != nil {
124 return nil, err
125 }
126 values = append(values, v)
127 }
128 return values, nil
129 }
130 // This code is unreachable because the parser does not allow
131 // single value constructs where multiple values are expected.
132 return nil, errors.New("a single value was supplied where a list was expected")
133 }
134
135 func (pv PlanValue) lookupList(bindVars map[string]*querypb.BindVariable) (*querypb.BindVariable, error) {
136 bv, ok := bindVars[pv.ListKey]
137 if !ok {
138 return nil, fmt.Errorf("missing bind var %s", pv.ListKey)
139 }
140 if bv.Type != querypb.Type_TUPLE {
141 return nil, fmt.Errorf("single value was supplied for TUPLE bind var %s", pv.ListKey)
142 }
143 return bv, nil
144 }
145
146 // MarshalJSON should be used only for testing.
147 func (pv PlanValue) MarshalJSON() ([]byte, error) {
148 switch {
149 case pv.Key != "":
150 return json.Marshal(":" + pv.Key)
151 case !pv.Value.IsNull():
152 if pv.Value.IsIntegral() {
153 return pv.Value.ToBytes(), nil
154 }
155 return json.Marshal(pv.Value.ToString())
156 case pv.ListKey != "":
157 return json.Marshal("::" + pv.ListKey)
158 case pv.Values != nil:
159 return json.Marshal(pv.Values)
160 }
161 return []byte("null"), nil
162 }
163
164 func rowCount(pvs []PlanValue, bindVars map[string]*querypb.BindVariable) (int, error) {
165 count := -1
166 setCount := func(l int) error {
167 switch count {
168 case -1:
169 count = l
170 return nil
171 case l:
172 return nil
173 default:
174 return errors.New("mismatch in number of column values")
175 }
176 }
177
178 for _, pv := range pvs {
179 switch {
180 case pv.Key != "" || !pv.Value.IsNull():
181 continue
182 case pv.Values != nil:
183 if err := setCount(len(pv.Values)); err != nil {
184 return 0, err
185 }
186 case pv.ListKey != "":
187 bv, err := pv.lookupList(bindVars)
188 if err != nil {
189 return 0, err
190 }
191 if err := setCount(len(bv.Values)); err != nil {
192 return 0, err
193 }
194 }
195 }
196
197 if count == -1 {
198 // If there were no lists inside, it was a single row.
199 // Note that count can never be 0 because there is enough
200 // protection at the top level: list bind vars must have
201 // at least one value (enforced by vtgate), and AST lists
202 // must have at least one value (enforced by the parser).
203 // Also lists created internally after vtgate validation
204 // ensure at least one value.
205 // TODO(sougou): verify and change API to enforce this.
206 return 1, nil
207 }
208 return count, nil
209 }
210
211 // ResolveRows resolves a []PlanValue as rows based on the supplied bindvars.
212 func ResolveRows(pvs []PlanValue, bindVars map[string]*querypb.BindVariable) ([][]Value, error) {
213 count, err := rowCount(pvs, bindVars)
214 if err != nil {
215 return nil, err
216 }
217
218 // Allocate the rows.
219 rows := make([][]Value, count)
220 for i := range rows {
221 rows[i] = make([]Value, len(pvs))
222 }
223
224 // Using j becasue we're resolving by columns.
225 for j, pv := range pvs {
226 switch {
227 case pv.Key != "":
228 bv, err := pv.lookupValue(bindVars)
229 if err != nil {
230 return nil, err
231 }
232 for i := range rows {
233 rows[i][j] = MakeTrusted(bv.Type, bv.Value)
234 }
235 case !pv.Value.IsNull():
236 for i := range rows {
237 rows[i][j] = pv.Value
238 }
239 case pv.ListKey != "":
240 bv, err := pv.lookupList(bindVars)
241 if err != nil {
242 // This code is unreachable because pvRowCount already checks this.
243 return nil, err
244 }
245 for i := range rows {
246 rows[i][j] = MakeTrusted(bv.Values[i].Type, bv.Values[i].Value)
247 }
248 case pv.Values != nil:
249 for i := range rows {
250 rows[i][j], err = pv.Values[i].ResolveValue(bindVars)
251 if err != nil {
252 return nil, err
253 }
254 }
255 // default case is a NULL value, which the row values are already initialized to.
256 }
257 }
258 return rows, nil
259 }