github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/limit.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package plan 16 17 import ( 18 "github.com/dolthub/go-mysql-server/sql" 19 ) 20 21 // Limit is a node that only allows up to N rows to be retrieved. 22 type Limit struct { 23 UnaryNode 24 Limit sql.Expression 25 CalcFoundRows bool 26 } 27 28 var _ sql.Node = (*Limit)(nil) 29 var _ sql.CollationCoercible = (*Limit)(nil) 30 31 // NewLimit creates a new Limit node with the given size. 32 func NewLimit(size sql.Expression, child sql.Node) *Limit { 33 return &Limit{ 34 UnaryNode: UnaryNode{Child: child}, 35 Limit: size, 36 } 37 } 38 39 // Expressions implements sql.Expressioner 40 func (l *Limit) Expressions() []sql.Expression { 41 return []sql.Expression{l.Limit} 42 } 43 44 // WithExpressions implements sql.Expressioner 45 func (l Limit) WithExpressions(exprs ...sql.Expression) (sql.Node, error) { 46 if len(exprs) != 1 { 47 return nil, sql.ErrInvalidChildrenNumber.New(l, len(exprs), 1) 48 } 49 nl := &l 50 nl.Limit = exprs[0] 51 return nl, nil 52 } 53 54 // Resolved implements the Resolvable interface. 55 func (l *Limit) Resolved() bool { 56 return l.UnaryNode.Child.Resolved() && l.Limit.Resolved() 57 } 58 59 func (l Limit) WithCalcFoundRows(v bool) *Limit { 60 l.CalcFoundRows = v 61 return &l 62 } 63 64 func (l Limit) IsReadOnly() bool { 65 return l.Child.IsReadOnly() 66 } 67 68 // WithChildren implements the Node interface. 69 func (l *Limit) WithChildren(children ...sql.Node) (sql.Node, error) { 70 if len(children) != 1 { 71 return nil, sql.ErrInvalidChildrenNumber.New(l, len(children), 1) 72 } 73 74 nl := *l 75 nl.Child = children[0] 76 return &nl, nil 77 } 78 79 // CheckPrivileges implements the interface sql.Node. 80 func (l *Limit) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool { 81 return l.Child.CheckPrivileges(ctx, opChecker) 82 } 83 84 // CollationCoercibility implements the interface sql.CollationCoercible. 85 func (l *Limit) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 86 return sql.GetCoercibility(ctx, l.Child) 87 } 88 89 func (l Limit) String() string { 90 pr := sql.NewTreePrinter() 91 _ = pr.WriteNode("Limit(%s)", l.Limit) 92 _ = pr.WriteChildren(l.Child.String()) 93 return pr.String() 94 } 95 96 func (l Limit) DebugString() string { 97 pr := sql.NewTreePrinter() 98 _ = pr.WriteNode("Limit(%s)", l.Limit) 99 _ = pr.WriteChildren(sql.DebugString(l.Child)) 100 return pr.String() 101 }