-
Notifications
You must be signed in to change notification settings - Fork 1
/
limit.go
173 lines (147 loc) · 4.13 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
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2015 The Cockroach 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 sql
import (
"fmt"
"math"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
)
// limitNode represents a node that limits the number of rows
// returned or only return them past a given number (offset).
type limitNode struct {
p *planner
plan planNode
countExpr parser.TypedExpr
offsetExpr parser.TypedExpr
evaluated bool
count int64
offset int64
rowIndex int64
}
// limit constructs a limitNode based on the LIMIT and OFFSET clauses.
func (p *planner) Limit(ctx context.Context, n *parser.Limit) (*limitNode, error) {
if n == nil || (n.Count == nil && n.Offset == nil) {
// No LIMIT nor OFFSET; there is nothing special to do.
return nil, nil
}
res := limitNode{p: p}
data := []struct {
name string
src parser.Expr
dst *parser.TypedExpr
}{
{"LIMIT", n.Count, &res.countExpr},
{"OFFSET", n.Offset, &res.offsetExpr},
}
for _, datum := range data {
if datum.src != nil {
if err := p.parser.AssertNoAggregationOrWindowing(
datum.src, datum.name, p.session.SearchPath,
); err != nil {
return nil, err
}
normalized, err := p.analyzeExpr(ctx, datum.src, nil, parser.IndexedVarHelper{}, parser.TypeInt, true, datum.name)
if err != nil {
return nil, err
}
*datum.dst = normalized
}
}
return &res, nil
}
func (n *limitNode) Start(params runParams) error {
if err := n.plan.Start(params); err != nil {
return err
}
return n.evalLimit()
}
// estimateLimit pre-computes the count and offset fields if they are constants,
// otherwise predefines them to be MaxInt64, 0. Used by index selection.
// This must be called after type checking and constant folding.
func (n *limitNode) estimateLimit() {
n.count = math.MaxInt64
n.offset = 0
// Use simple integer datum if available.
// The limit can be a simple DInt here either because it was
// entered as such in the query, or as a result of constant
// folding prior to type checking.
if n.countExpr != nil {
if i, ok := parser.AsDInt(n.countExpr); ok {
n.count = int64(i)
}
}
if n.offsetExpr != nil {
if i, ok := parser.AsDInt(n.offsetExpr); ok {
n.offset = int64(i)
}
}
}
// evalLimit evaluates the Count and Offset fields. If Count is missing, the
// value is MaxInt64. If Offset is missing, the value is 0
func (n *limitNode) evalLimit() error {
n.count = math.MaxInt64
n.offset = 0
data := []struct {
name string
src parser.TypedExpr
dst *int64
}{
{"LIMIT", n.countExpr, &n.count},
{"OFFSET", n.offsetExpr, &n.offset},
}
for _, datum := range data {
if datum.src != nil {
dstDatum, err := datum.src.Eval(&n.p.evalCtx)
if err != nil {
return err
}
if dstDatum == parser.DNull {
// Use the default value.
continue
}
dstDInt := parser.MustBeDInt(dstDatum)
val := int64(dstDInt)
if val < 0 {
return fmt.Errorf("negative value for %s", datum.name)
}
*datum.dst = val
}
}
n.evaluated = true
return nil
}
func (n *limitNode) Values() parser.Datums { return n.plan.Values() }
func (n *limitNode) Next(params runParams) (bool, error) {
// n.rowIndex is the 0-based index of the next row.
// We don't do (n.rowIndex >= n.offset + n.count) to avoid overflow (count can be MaxInt64).
if n.rowIndex-n.offset >= n.count {
return false, nil
}
for {
if next, err := n.plan.Next(params); !next {
return false, err
}
n.rowIndex++
if n.rowIndex > n.offset {
// Row within limits, return it.
break
}
// Fetch the next row.
}
return true, nil
}
func (n *limitNode) Close(ctx context.Context) {
n.plan.Close(ctx)
}