-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
limit.go
60 lines (52 loc) · 1.47 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
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package colexec
import (
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/sql/colexecop"
)
// limitOp is an operator that implements limit, returning only the first n
// tuples from its input.
type limitOp struct {
colexecop.OneInputInitCloserHelper
limit uint64
// seen is the number of tuples seen so far.
seen uint64
// done is true if the limit has been reached.
done bool
}
var _ colexecop.Operator = &limitOp{}
var _ colexecop.ClosableOperator = &limitOp{}
// NewLimitOp returns a new limit operator with the given limit.
func NewLimitOp(input colexecop.Operator, limit uint64) colexecop.Operator {
c := &limitOp{
OneInputInitCloserHelper: colexecop.MakeOneInputInitCloserHelper(input),
limit: limit,
}
return c
}
func (c *limitOp) Next() coldata.Batch {
if c.done {
return coldata.ZeroBatch
}
bat := c.Input.Next()
length := bat.Length()
if length == 0 {
return bat
}
newSeen := c.seen + uint64(length)
if newSeen >= c.limit {
c.done = true
bat.SetLength(int(c.limit - c.seen))
return bat
}
c.seen = newSeen
return bat
}