-
Notifications
You must be signed in to change notification settings - Fork 2k
/
expression_converter.go
119 lines (105 loc) · 2.98 KB
/
expression_converter.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
/*
Copyright 2020 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"
"strings"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/evalengine"
)
type expressionConverter struct {
tabletExpressions []sqlparser.Expr
}
func booleanValues(astExpr sqlparser.Expr) evalengine.Expr {
var (
ON = evalengine.NewLiteralInt(1)
OFF = evalengine.NewLiteralInt(0)
)
switch node := astExpr.(type) {
case *sqlparser.Literal:
//set autocommit = 'on'
if node.Type == sqlparser.StrVal {
switch strings.ToLower(node.Val) {
case "on":
return ON
case "off":
return OFF
}
}
case *sqlparser.ColName:
//set autocommit = on
switch node.Name.Lowered() {
case "on":
return ON
case "off":
return OFF
}
}
return nil
}
func identifierAsStringValue(astExpr sqlparser.Expr) evalengine.Expr {
colName, isColName := astExpr.(*sqlparser.ColName)
if isColName {
// TODO@collations: proper collation for column name
return evalengine.NewLiteralString([]byte(colName.Name.Lowered()), collations.TypedCollation{})
}
return nil
}
func (ec *expressionConverter) convert(astExpr sqlparser.Expr, boolean, identifierAsString bool) (evalengine.Expr, error) {
if boolean {
evalExpr := booleanValues(astExpr)
if evalExpr != nil {
return evalExpr, nil
}
}
if identifierAsString {
evalExpr := identifierAsStringValue(astExpr)
if evalExpr != nil {
return evalExpr, nil
}
}
evalExpr, err := evalengine.Translate(astExpr, nil)
if err != nil {
if !strings.Contains(err.Error(), evalengine.ErrTranslateExprNotSupported) {
return nil, err
}
evalExpr = &evalengine.Column{Offset: len(ec.tabletExpressions)}
ec.tabletExpressions = append(ec.tabletExpressions, astExpr)
}
return evalExpr, nil
}
func (ec *expressionConverter) source(vschema plancontext.VSchema) (engine.Primitive, error) {
if len(ec.tabletExpressions) == 0 {
return &engine.SingleRow{}, nil
}
ks, dest, err := resolveDestination(vschema)
if err != nil {
return nil, err
}
var expr []string
for _, e := range ec.tabletExpressions {
expr = append(expr, sqlparser.String(e))
}
query := fmt.Sprintf("select %s from dual", strings.Join(expr, ","))
primitive := &engine.Send{
Keyspace: ks,
TargetDestination: dest,
Query: query,
IsDML: false,
SingleShardOnly: true,
}
return primitive, nil
}