-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vindex_func.go
118 lines (97 loc) · 2.95 KB
/
vindex_func.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
/*
Copyright 2019 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 planbuilder
import (
"fmt"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/vt/vtgate/semantics"
"vitess.io/vitess/go/vt/vterrors"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/engine"
)
var _ logicalPlan = (*vindexFunc)(nil)
// vindexFunc is used to build a VindexFunc primitive.
type vindexFunc struct {
order int
// the tableID field is only used by the gen4 planner
tableID semantics.TableSet
// eVindexFunc is the primitive being built.
eVindexFunc *engine.VindexFunc
}
var colnames = []string{
"id",
"keyspace_id",
"range_start",
"range_end",
"hex_keyspace_id",
"shard",
}
// Primitive implements the logicalPlan interface
func (vf *vindexFunc) Primitive() engine.Primitive {
return vf.eVindexFunc
}
// SupplyProjection pushes the given aliased expression into the fields and cols slices of the
// vindexFunc engine primitive. The method returns the offset of the new expression in the columns
// list.
func (vf *vindexFunc) SupplyProjection(expr *sqlparser.AliasedExpr, reuse bool) (int, error) {
colName, isColName := expr.Expr.(*sqlparser.ColName)
if !isColName {
return 0, vterrors.VT12001("expression on results of a vindex function")
}
enum := vindexColumnToIndex(colName)
if enum == -1 {
return 0, vterrors.VT03016(colName.Name.String())
}
if reuse {
for i, col := range vf.eVindexFunc.Cols {
if col == enum {
return i, nil
}
}
}
vf.eVindexFunc.Fields = append(vf.eVindexFunc.Fields, &querypb.Field{
Name: expr.ColumnName(),
Type: querypb.Type_VARBINARY,
Charset: collations.CollationBinaryID,
Flags: uint32(querypb.MySqlFlag_BINARY_FLAG),
})
vf.eVindexFunc.Cols = append(vf.eVindexFunc.Cols, enum)
return len(vf.eVindexFunc.Cols) - 1, nil
}
// UnsupportedSupplyWeightString represents the error where the supplying a weight string is not supported
type UnsupportedSupplyWeightString struct {
Type string
}
// Error function implements the error interface
func (err UnsupportedSupplyWeightString) Error() string {
return fmt.Sprintf("cannot do collation on %s", err.Type)
}
func vindexColumnToIndex(column *sqlparser.ColName) int {
switch column.Name.String() {
case "id":
return 0
case "keyspace_id":
return 1
case "range_start":
return 2
case "range_end":
return 3
case "hex_keyspace_id":
return 4
case "shard":
return 5
default:
return -1
}
}