-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
limit.go
161 lines (134 loc) · 4.4 KB
/
limit.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Copyright 2017 Google Inc.
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/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/engine"
)
// limit is the builder for engine.Limit.
// This gets built if a limit needs to be applied
// after rows are returned from an underlying
// operation. Since a limit is the final operation
// of a SELECT, most pushes are not applicable.
type limit struct {
order int
resultColumns []*resultColumn
input builder
elimit *engine.Limit
}
// newLimit builds a new limit.
func newLimit(bldr builder) *limit {
return &limit{
order: bldr.Order() + 1,
resultColumns: bldr.ResultColumns(),
input: bldr,
elimit: &engine.Limit{},
}
}
// Order satisfies the builder interface.
func (l *limit) Order() int {
return l.order
}
// Reorder satisfies the builder interface.
func (l *limit) Reorder(order int) {
l.input.Reorder(order)
l.order = l.input.Order() + 1
}
// Primitive satisfies the builder interface.
func (l *limit) Primitive() engine.Primitive {
l.elimit.Input = l.input.Primitive()
return l.elimit
}
// First satisfies the builder interface.
func (l *limit) First() builder {
return l.input.First()
}
// ResultColumns satisfies the builder interface.
func (l *limit) ResultColumns() []*resultColumn {
return l.resultColumns
}
// PushFilter satisfies the builder interface.
func (l *limit) PushFilter(_ *primitiveBuilder, _ sqlparser.Expr, whereType string, _ builder) error {
panic("BUG: unreachable")
}
// PushSelect satisfies the builder interface.
func (l *limit) PushSelect(expr *sqlparser.AliasedExpr, origin builder) (rc *resultColumn, colnum int, err error) {
panic("BUG: unreachable")
}
// MakeDistinct satisfies the builder interface.
func (l *limit) MakeDistinct() error {
panic("BUG: unreachable")
}
// SetGroupBy satisfies the builder interface.
func (l *limit) SetGroupBy(groupBy sqlparser.GroupBy) (builder, error) {
panic("BUG: unreachable")
}
// PushOrderByNull satisfies the builder interface.
func (l *limit) PushOrderByNull() {
panic("BUG: unreachable")
}
// PushOrderByRand satisfies the builder interface.
func (l *limit) PushOrderByRand() {
panic("BUG: unreachable")
}
// SetLimit sets the limit for the primitive. It calls the underlying
// primitive's SetUpperLimit, which is an optimization hint that informs
// the underlying primitive that it doesn't need to return more rows than
// specified.
func (l *limit) SetLimit(limit *sqlparser.Limit) error {
count, ok := limit.Rowcount.(*sqlparser.SQLVal)
if !ok {
return fmt.Errorf("unexpected expression in LIMIT: %v", sqlparser.String(limit))
}
pv, err := sqlparser.NewPlanValue(count)
if err != nil {
return err
}
l.elimit.Count = pv
switch offset := limit.Offset.(type) {
case *sqlparser.SQLVal:
pv, err = sqlparser.NewPlanValue(offset)
if err != nil {
return err
}
l.elimit.Offset = pv
case nil:
// NOOP
default:
return fmt.Errorf("unexpected expression in LIMIT: %v", sqlparser.String(limit))
}
l.input.SetUpperLimit(sqlparser.NewValArg([]byte(":__upper_limit")))
return nil
}
// SetUpperLimit satisfies the builder interface.
// This is a no-op because we actually call SetLimit for this primitive.
// In the future, we may have to honor this call for subqueries.
func (l *limit) SetUpperLimit(count *sqlparser.SQLVal) {
}
// PushMisc satisfies the builder interface.
func (l *limit) PushMisc(sel *sqlparser.Select) {
l.input.PushMisc(sel)
}
// Wireup satisfies the builder interface.
func (l *limit) Wireup(bldr builder, jt *jointab) error {
return l.input.Wireup(bldr, jt)
}
// SupplyVar satisfies the builder interface.
func (l *limit) SupplyVar(from, to int, col *sqlparser.ColName, varname string) {
l.input.SupplyVar(from, to, col, varname)
}
// SupplyCol satisfies the builder interface.
func (l *limit) SupplyCol(col *sqlparser.ColName) (rc *resultColumn, colnum int) {
panic("BUG: nothing should depend on LIMIT")
}