github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/alter_range.go (about)

     1  // Copyright 2021 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 tree
    12  
    13  import "github.com/cockroachdb/errors"
    14  
    15  // RelocateRange represents an `ALTER RANGE .. RELOCATE ..`
    16  // statement.
    17  type RelocateRange struct {
    18  	Rows            *Select
    19  	ToStoreID       Expr
    20  	FromStoreID     Expr
    21  	SubjectReplicas RelocateSubject
    22  }
    23  
    24  // RelocateSubject indicates what replicas of a range should be relocated.
    25  type RelocateSubject int8
    26  
    27  const (
    28  	// RelocateLease indicates that leases should be relocated.
    29  	RelocateLease RelocateSubject = iota
    30  	// RelocateVoters indicates what voter replicas should be relocated.
    31  	RelocateVoters
    32  	// RelocateNonVoters indicates that non-voter replicas should be relocated.
    33  	RelocateNonVoters
    34  )
    35  
    36  // Format implementsthe NodeFormatter interface.
    37  func (n *RelocateSubject) Format(ctx *FmtCtx) {
    38  	ctx.WriteString(n.String())
    39  }
    40  
    41  func (n RelocateSubject) String() string {
    42  	switch n {
    43  	case RelocateLease:
    44  		return "LEASE"
    45  	case RelocateVoters:
    46  		return "VOTERS"
    47  	case RelocateNonVoters:
    48  		return "NONVOTERS"
    49  	default:
    50  		panic(errors.AssertionFailedf("programming error: unhandled case %d", int(n)))
    51  	}
    52  }
    53  
    54  // Format implements the NodeFormatter interface.
    55  func (n *RelocateRange) Format(ctx *FmtCtx) {
    56  	ctx.WriteString("ALTER RANGE RELOCATE ")
    57  	ctx.FormatNode(&n.SubjectReplicas)
    58  	// When relocating leases, the origin store is implicit.
    59  	if n.SubjectReplicas != RelocateLease {
    60  		ctx.WriteString(" FROM ")
    61  		ctx.FormatNode(n.FromStoreID)
    62  	}
    63  	ctx.WriteString(" TO ")
    64  	ctx.FormatNode(n.ToStoreID)
    65  	ctx.WriteString(" FOR ")
    66  	ctx.FormatNode(n.Rows)
    67  }