github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/optbuilder/limit.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package optbuilder
    12  
    13  import (
    14  	"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
    15  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    16  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    17  )
    18  
    19  // buildLimit adds Limit and Offset operators according to the Limit clause.
    20  //
    21  // parentScope is the scope for the LIMIT/OFFSET expressions; this is not the
    22  // same as inScope, because statements like:
    23  //   SELECT k FROM kv LIMIT k
    24  // are not valid.
    25  func (b *Builder) buildLimit(limit *tree.Limit, parentScope, inScope *scope) {
    26  	if limit.Offset != nil {
    27  		input := inScope.expr.(memo.RelExpr)
    28  		offset := b.resolveAndBuildScalar(
    29  			limit.Offset, types.Int, exprKindOffset, tree.RejectSpecial, parentScope,
    30  		)
    31  		inScope.expr = b.factory.ConstructOffset(input, offset, inScope.makeOrderingChoice())
    32  	}
    33  	if limit.Count != nil {
    34  		input := inScope.expr.(memo.RelExpr)
    35  		limit := b.resolveAndBuildScalar(
    36  			limit.Count, types.Int, exprKindLimit, tree.RejectSpecial, parentScope,
    37  		)
    38  		inScope.expr = b.factory.ConstructLimit(input, limit, inScope.makeOrderingChoice())
    39  	}
    40  }