-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
limit.go
166 lines (141 loc) · 3.92 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
// 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.
//
// Author: Tamir Duberstein ([email protected])
package sql
import (
"fmt"
"math"
"strconv"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/util"
)
// evalLimit evaluates the Count and Offset fields. If Count is missing, the
// value is MaxInt64. If Offset is missing, the value is 0
func (p *planner) evalLimit(limit *parser.Limit) (count, offset int64, err error) {
count = math.MaxInt64
offset = 0
if limit == nil {
return count, offset, nil
}
data := []struct {
name string
src parser.Expr
dst *int64
}{
{"LIMIT", limit.Count, &count},
{"OFFSET", limit.Offset, &offset},
}
for _, datum := range data {
if datum.src != nil {
if parser.ContainsVars(datum.src) {
return 0, 0, util.Errorf("argument of %s must not contain variables", datum.name)
}
normalized, err := p.parser.NormalizeExpr(p.evalCtx, datum.src)
if err != nil {
return 0, 0, err
}
dstDatum, err := normalized.Eval(p.evalCtx)
if err != nil {
return 0, 0, err
}
if dstDatum == parser.DNull {
// Use the default value.
continue
}
if dstDInt, ok := dstDatum.(parser.DInt); ok {
val := int64(dstDInt)
if val < 0 {
return 0, 0, fmt.Errorf("negative value for %s", datum.name)
}
*datum.dst = val
continue
}
return 0, 0, fmt.Errorf("argument of %s must be type %s, not type %s",
datum.name, parser.DummyInt.Type(), dstDatum.Type())
}
}
return count, offset, nil
}
// limit constructs a limitNode based on the LIMIT and OFFSET clauses.
func (p *planner) limit(count, offset int64, plan planNode) planNode {
if count == math.MaxInt64 && offset == 0 {
return plan
}
if count != math.MaxInt64 {
plan.SetLimitHint(offset+count, false /* hard */)
}
return &limitNode{planNode: plan, count: count, offset: offset}
}
type limitNode struct {
planNode
count int64
offset int64
rowIndex int64
explain explainMode
debugVals debugValues
}
func (n *limitNode) MarkDebug(mode explainMode) {
if mode != explainDebug {
panic(fmt.Sprintf("unknown debug mode %d", mode))
}
n.explain = mode
n.planNode.MarkDebug(mode)
}
func (n *limitNode) DebugValues() debugValues {
if n.explain != explainDebug {
panic(fmt.Sprintf("node not in debug mode (mode %d)", n.explain))
}
return n.debugVals
}
func (n *limitNode) Next() bool {
// 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
}
for {
if !n.planNode.Next() {
return false
}
if n.explain == explainDebug {
n.debugVals = n.planNode.DebugValues()
if n.debugVals.output != debugValueRow {
// Let the non-row debug values pass through.
return true
}
}
n.rowIndex++
if n.rowIndex > n.offset {
// Row within limits, return it.
return true
}
if n.explain == explainDebug {
// Return as a filtered row.
n.debugVals.output = debugValueFiltered
return true
}
// Fetch the next row.
}
}
func (n *limitNode) ExplainPlan() (string, string, []planNode) {
var count string
if n.count == math.MaxInt64 {
count = "ALL"
} else {
count = strconv.FormatInt(n.count, 10)
}
return "limit", fmt.Sprintf("count: %s, offset: %d", count, n.offset), []planNode{n.planNode}
}
func (*limitNode) SetLimitHint(_ int64, _ bool) {}