github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/ordering/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 ordering
    12  
    13  import (
    14  	"github.com/cockroachdb/cockroach/pkg/sql/opt"
    15  	"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
    16  	"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
    17  )
    18  
    19  func limitOrOffsetCanProvideOrdering(expr memo.RelExpr, required *physical.OrderingChoice) bool {
    20  	// Limit/Offset require a certain ordering of their input, but can also pass
    21  	// through a stronger ordering. For example:
    22  	//
    23  	//   SELECT * FROM (SELECT x, y FROM t ORDER BY x LIMIT 10) ORDER BY x,y
    24  	//
    25  	// In this case the internal ordering is x+, but we can pass through x+,y+
    26  	// to satisfy both orderings.
    27  	return required.Intersects(expr.Private().(*physical.OrderingChoice))
    28  }
    29  
    30  func limitOrOffsetBuildChildReqOrdering(
    31  	parent memo.RelExpr, required *physical.OrderingChoice, childIdx int,
    32  ) physical.OrderingChoice {
    33  	if childIdx != 0 {
    34  		return physical.OrderingChoice{}
    35  	}
    36  	return required.Intersection(parent.Private().(*physical.OrderingChoice))
    37  }
    38  
    39  func limitOrOffsetBuildProvided(expr memo.RelExpr, required *physical.OrderingChoice) opt.Ordering {
    40  	childProvided := expr.Child(0).(memo.RelExpr).ProvidedPhysical().Ordering
    41  	// The child's provided ordering satisfies both <required> and the
    42  	// Limit/Offset internal ordering; it may need to be trimmed.
    43  	return trimProvided(childProvided, required, &expr.Relational().FuncDeps)
    44  }