/
filtering.go
121 lines (106 loc) · 4.13 KB
/
filtering.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
/*
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 (
"errors"
"fmt"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/vtgate/semantics"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
)
// planFilter solves this particular expression, either by pushing it down to a child or changing this logicalPlan
func planFilter(pb *primitiveBuilder, input logicalPlan, filter sqlparser.Expr, whereType string, origin logicalPlan) (logicalPlan, error) {
switch node := input.(type) {
case *join:
isLeft := true
var in logicalPlan
if node.isOnLeft(origin.Order()) {
in = node.Left
} else {
if node.ejoin.Opcode == engine.LeftJoin {
return nil, errors.New("unsupported: cross-shard left join and where clause")
}
isLeft = false
in = node.Right
}
filtered, err := planFilter(pb, in, filter, whereType, origin)
if err != nil {
return nil, err
}
if isLeft {
node.Left = filtered
} else {
node.Right = filtered
}
return node, nil
case *route:
sel := node.Select.(*sqlparser.Select)
switch whereType {
case sqlparser.WhereStr:
sel.AddWhere(filter)
case sqlparser.HavingStr:
sel.AddHaving(filter)
}
node.UpdatePlan(pb, filter)
return node, nil
case *pulloutSubquery:
plan, err := planFilter(pb, node.underlying, filter, whereType, origin)
if err != nil {
return nil, err
}
node.underlying = plan
return node, nil
case *vindexFunc:
return filterVindexFunc(node, filter)
case *simpleProjection:
return nil, errors.New("unsupported: filtering on results of cross-shard subquery")
case *orderedAggregate:
return nil, errors.New("unsupported: filtering on results of aggregates")
}
return nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "[BUG] unreachable %T.filtering", input)
}
func filterVindexFunc(node *vindexFunc, filter sqlparser.Expr) (logicalPlan, error) {
if node.eVindexFunc.Opcode != engine.VindexNone {
return nil, errors.New("unsupported: where clause for vindex function must be of the form id = <val> or id in(<val>,...) (multiple filters)")
}
// Check LHS.
comparison, ok := filter.(*sqlparser.ComparisonExpr)
if !ok {
return nil, errors.New("unsupported: where clause for vindex function must be of the form id = <val> or id in(<val>,...) (not a comparison)")
}
if comparison.Operator != sqlparser.EqualOp && comparison.Operator != sqlparser.InOp {
return nil, errors.New("unsupported: where clause for vindex function must be of the form id = <val> or id in(<val>,...) (not equality)")
}
colname, ok := comparison.Left.(*sqlparser.ColName)
if !ok {
return nil, errors.New("unsupported: where clause for vindex function must be of the form id = <val> or id in(<val>,...) (lhs is not a column)")
}
if !colname.Name.EqualString("id") {
return nil, errors.New("unsupported: where clause for vindex function must be of the form id = <val> or id in(<val>,...) (lhs is not id)")
}
// Check RHS.
// We have to check before calling NewPlanValue because NewPlanValue allows lists also.
if !sqlparser.IsValue(comparison.Right) && !sqlparser.IsSimpleTuple(comparison.Right) {
return nil, errors.New("unsupported: where clause for vindex function must be of the form id = <val> or id in(<val>,...) (rhs is not a value)")
}
var err error
node.eVindexFunc.Value, err = evalengine.Translate(comparison.Right, semantics.EmptySemTable())
if err != nil {
return nil, fmt.Errorf("unsupported: where clause for vindex function must be of the form id = <val> or id in(<val>,...): %v", err)
}
node.eVindexFunc.Opcode = engine.VindexMap
return node, nil
}