github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/zone.go (about)

     1  // Copyright 2017 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  // ZoneSpecifier represents a reference to a configurable zone of the keyspace.
    14  type ZoneSpecifier struct {
    15  	// Only one of NamedZone, Database or TableOrIndex may be set.
    16  	NamedZone UnrestrictedName
    17  	Database  Name
    18  	// TODO(radu): TableOrIndex abuses TableIndexName: it allows for the case when
    19  	// an index is not specified, in which case TableOrIndex.Index is empty.
    20  	TableOrIndex TableIndexName
    21  
    22  	// Partition is only respected when Table is set.
    23  	Partition Name
    24  }
    25  
    26  // TelemetryName returns a name fitting for telemetry purposes.
    27  func (node ZoneSpecifier) TelemetryName() string {
    28  	if node.NamedZone != "" {
    29  		return "range"
    30  	}
    31  	if node.Database != "" {
    32  		return "database"
    33  	}
    34  	str := ""
    35  	if node.Partition != "" {
    36  		str = "partition."
    37  	}
    38  	if node.TargetsIndex() {
    39  		str += "index"
    40  	} else {
    41  		str += "table"
    42  	}
    43  	return str
    44  }
    45  
    46  // TargetsTable returns whether the zone specifier targets a table or a subzone
    47  // within a table.
    48  func (node ZoneSpecifier) TargetsTable() bool {
    49  	return node.NamedZone == "" && node.Database == ""
    50  }
    51  
    52  // TargetsIndex returns whether the zone specifier targets an index.
    53  func (node ZoneSpecifier) TargetsIndex() bool {
    54  	return node.TargetsTable() && node.TableOrIndex.Index != ""
    55  }
    56  
    57  // TargetsPartition returns whether the zone specifier targets a partition.
    58  func (node ZoneSpecifier) TargetsPartition() bool {
    59  	return node.TargetsTable() && node.Partition != ""
    60  }
    61  
    62  // Format implements the NodeFormatter interface.
    63  func (node *ZoneSpecifier) Format(ctx *FmtCtx) {
    64  	if node.NamedZone != "" {
    65  		ctx.WriteString("RANGE ")
    66  		ctx.FormatNode(&node.NamedZone)
    67  	} else if node.Database != "" {
    68  		ctx.WriteString("DATABASE ")
    69  		ctx.FormatNode(&node.Database)
    70  	} else {
    71  		if node.Partition != "" {
    72  			ctx.WriteString("PARTITION ")
    73  			ctx.FormatNode(&node.Partition)
    74  			ctx.WriteString(" OF ")
    75  		}
    76  		if node.TargetsIndex() {
    77  			ctx.WriteString("INDEX ")
    78  		} else {
    79  			ctx.WriteString("TABLE ")
    80  		}
    81  		ctx.FormatNode(&node.TableOrIndex)
    82  	}
    83  }
    84  
    85  func (node *ZoneSpecifier) String() string { return AsString(node) }
    86  
    87  // ShowZoneConfig represents a SHOW ZONE CONFIGURATION
    88  // statement.
    89  type ShowZoneConfig struct {
    90  	ZoneSpecifier
    91  }
    92  
    93  // Format implements the NodeFormatter interface.
    94  func (node *ShowZoneConfig) Format(ctx *FmtCtx) {
    95  	if node.ZoneSpecifier == (ZoneSpecifier{}) {
    96  		ctx.WriteString("SHOW ZONE CONFIGURATIONS")
    97  	} else {
    98  		ctx.WriteString("SHOW ZONE CONFIGURATION FOR ")
    99  		ctx.FormatNode(&node.ZoneSpecifier)
   100  	}
   101  }
   102  
   103  // SetZoneConfig represents an ALTER DATABASE/TABLE... CONFIGURE ZONE
   104  // statement.
   105  type SetZoneConfig struct {
   106  	ZoneSpecifier
   107  	// AllIndexes indicates that the zone configuration should be applied across
   108  	// all of a tables indexes. (ALTER PARTITION ... OF INDEX <tablename>@*)
   109  	AllIndexes bool
   110  	SetDefault bool
   111  	YAMLConfig Expr
   112  	Options    KVOptions
   113  }
   114  
   115  // Format implements the NodeFormatter interface.
   116  func (node *SetZoneConfig) Format(ctx *FmtCtx) {
   117  	ctx.WriteString("ALTER ")
   118  	ctx.FormatNode(&node.ZoneSpecifier)
   119  	ctx.WriteString(" CONFIGURE ZONE ")
   120  	if node.SetDefault {
   121  		ctx.WriteString("USING DEFAULT")
   122  	} else if node.YAMLConfig != nil {
   123  		if node.YAMLConfig == DNull {
   124  			ctx.WriteString("DISCARD")
   125  		} else {
   126  			ctx.WriteString("= ")
   127  			ctx.FormatNode(node.YAMLConfig)
   128  		}
   129  	} else {
   130  		ctx.WriteString("USING ")
   131  		kvOptions := node.Options
   132  		comma := ""
   133  		for _, kv := range kvOptions {
   134  			ctx.WriteString(comma)
   135  			comma = ", "
   136  			ctx.FormatNode(&kv.Key)
   137  			if kv.Value != nil {
   138  				ctx.WriteString(` = `)
   139  				ctx.FormatNode(kv.Value)
   140  			} else {
   141  				ctx.WriteString(` = COPY FROM PARENT`)
   142  			}
   143  		}
   144  	}
   145  }