github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/ordering/mutation.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"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
    18  )
    19  
    20  func mutationCanProvideOrdering(expr memo.RelExpr, required *physical.OrderingChoice) bool {
    21  	// The mutation operator can always pass through ordering to its input.
    22  	return true
    23  }
    24  
    25  func mutationBuildChildReqOrdering(
    26  	parent memo.RelExpr, required *physical.OrderingChoice, childIdx int,
    27  ) physical.OrderingChoice {
    28  	if childIdx != 0 {
    29  		return physical.OrderingChoice{}
    30  	}
    31  
    32  	// Remap each of the required columns to corresponding input columns.
    33  	private := parent.Private().(*memo.MutationPrivate)
    34  
    35  	optional := private.MapToInputCols(required.Optional)
    36  	columns := make([]physical.OrderingColumnChoice, len(required.Columns))
    37  	for i := range required.Columns {
    38  		colChoice := &required.Columns[i]
    39  		columns[i] = physical.OrderingColumnChoice{
    40  			Group:      private.MapToInputCols(colChoice.Group),
    41  			Descending: colChoice.Descending,
    42  		}
    43  	}
    44  	return physical.OrderingChoice{Optional: optional, Columns: columns}
    45  }
    46  
    47  func mutationBuildProvided(expr memo.RelExpr, required *physical.OrderingChoice) opt.Ordering {
    48  	private := expr.Private().(*memo.MutationPrivate)
    49  	input := expr.Child(0).(memo.RelExpr)
    50  	provided := input.ProvidedPhysical().Ordering
    51  
    52  	// Construct FD set that includes mapping to/from input columns. This will
    53  	// be used by remapProvided.
    54  	var fdset props.FuncDepSet
    55  	fdset.CopyFrom(&input.Relational().FuncDeps)
    56  	private.AddEquivTableCols(expr.Memo().Metadata(), &fdset)
    57  
    58  	// Ensure that provided ordering only uses projected columns.
    59  	return remapProvided(provided, &fdset, expr.Relational().OutputCols)
    60  }