| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
type PlanValue struct { |
| 60 |
Key string |
| 61 |
Value Value |
| 62 |
ListKey string |
| 63 |
Values []PlanValue |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func (pv PlanValue) IsNull() bool { |
| 68 |
return pv.Key == "" && pv.Value.IsNull() && pv.ListKey == "" && pv.Values == nil |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
func (pv PlanValue) IsList() bool { |
| 73 |
return pv.ListKey != "" || pv.Values != nil |
| 74 |
} |
| 75 |
|
| 76 |
|
| 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 |
|
| 89 |
|
| 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 |
|
| 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 |
|
| 131 |
|
| 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 |
|
| 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 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
return 1, nil |
| 207 |
} |
| 208 |
return count, nil |
| 209 |
} |
| 210 |
|
| 211 |
|
| 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 |
|
| 219 |
rows := make([][]Value, count) |
| 220 |
for i := range rows { |
| 221 |
rows[i] = make([]Value, len(pvs)) |
| 222 |
} |
| 223 |
|
| 224 |
|
| 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 |
|
| 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 |
|
| 256 |
} |
| 257 |
} |
| 258 |
return rows, nil |
| 259 |
} |