github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/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 FROM ") 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 ZoneConfigSettings 111 } 112 113 // Format implements the NodeFormatter interface. 114 func (node *SetZoneConfig) Format(ctx *FmtCtx) { 115 ctx.WriteString("ALTER ") 116 ctx.FormatNode(&node.ZoneSpecifier) 117 ctx.WriteString(" CONFIGURE ZONE ") 118 node.ZoneConfigSettings.Format(ctx) 119 } 120 121 // ZoneConfigSettings represents info needed for zone config setting. 122 type ZoneConfigSettings struct { 123 SetDefault bool 124 YAMLConfig Expr 125 Options KVOptions 126 } 127 128 // Format implements the NodeFormatter interface. 129 func (node *ZoneConfigSettings) Format(ctx *FmtCtx) { 130 if node.SetDefault { 131 ctx.WriteString("USING DEFAULT") 132 } else if node.YAMLConfig != nil { 133 if node.YAMLConfig == DNull { 134 ctx.WriteString("DISCARD") 135 } else { 136 ctx.WriteString("= ") 137 ctx.FormatNode(node.YAMLConfig) 138 } 139 } else { 140 ctx.WriteString("USING ") 141 kvOptions := node.Options 142 comma := "" 143 for _, kv := range kvOptions { 144 ctx.WriteString(comma) 145 comma = ", " 146 ctx.FormatNode(&kv.Key) 147 if kv.Value != nil { 148 ctx.WriteString(` = `) 149 ctx.FormatNode(kv.Value) 150 } else { 151 ctx.WriteString(` = COPY FROM PARENT`) 152 } 153 } 154 } 155 }