github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/cat/table.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 cat 12 13 import ( 14 "time" 15 16 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 17 ) 18 19 // Table is an interface to a database table, exposing only the information 20 // needed by the query optimizer. 21 // 22 // Both columns and indexes are grouped into three sets: public, write-only, and 23 // delete-only. When a column or index is added or dropped, it proceeds through 24 // each of the three states as that schema change is incrementally rolled out to 25 // the cluster without blocking ongoing queries. In the public state, reads, 26 // writes, and deletes are allowed. In the write-only state, only writes and 27 // deletes are allowed. Finally, in the delete-only state, only deletes are 28 // allowed. Further details about "online schema change" can be found in: 29 // 30 // docs/RFCS/20151014_online_schema_change.md 31 // 32 // Calling code must take care to use the right collection of columns or 33 // indexes. Usually this should be the public collections, since most usages are 34 // read-only, but mutation operators generally need to consider non-public 35 // columns and indexes. 36 type Table interface { 37 DataSource 38 39 // IsVirtualTable returns true if this table is a special system table that 40 // constructs its rows "on the fly" when it's queried. An example is the 41 // information_schema tables. 42 IsVirtualTable() bool 43 44 // ColumnCount returns the number of public columns in the table. Public 45 // columns are not currently being added or dropped from the table. This 46 // method should be used when mutation columns can be ignored (the common 47 // case). 48 ColumnCount() int 49 50 // WritableColumnCount returns the number of public and write-only columns in 51 // the table. Although write-only columns are not visible, any inserts and 52 // updates must still set them. WritableColumnCount is always >= ColumnCount. 53 WritableColumnCount() int 54 55 // DeletableColumnCount returns the number of public, write-only, and 56 // delete- only columns in the table. DeletableColumnCount is always >= 57 // WritableColumnCount. 58 DeletableColumnCount() int 59 60 // Column returns a Column interface to the column at the ith ordinal 61 // position within the table, where i < ColumnCount. Note that the Columns 62 // collection includes mutation columns, if present. Mutation columns are in 63 // the process of being added or dropped from the table, and may need to have 64 // default or computed values set when inserting or updating rows. See this 65 // RFC for more details: 66 // 67 // cockroachdb/cockroach/docs/RFCS/20151014_online_schema_change.md 68 // 69 // Writable columns are always situated after public columns, and are followed 70 // by deletable columns. 71 Column(i int) Column 72 73 // IndexCount returns the number of public indexes defined on this table. 74 // Public indexes are not currently being added or dropped from the table. 75 // This method should be used when mutation columns can be ignored (the common 76 // case). The returned indexes include the primary index, so the count is 77 // always >= 1 (except for virtual tables, which have no indexes). 78 IndexCount() int 79 80 // WritableIndexCount returns the number of public and write-only indexes 81 // defined on this table. Although write-only indexes are not visible, any 82 // table mutation operations must still be applied to them. WritableIndexCount 83 // is always >= IndexCount. 84 WritableIndexCount() int 85 86 // DeletableIndexCount returns the number of public, write-only, and 87 // delete-only indexes defined on this table. DeletableIndexCount is always 88 // >= WritableIndexCount. 89 DeletableIndexCount() int 90 91 // Index returns the ith index, where i < DeletableIndexCount. Except for 92 // virtual tables, the table's primary index is always the 0th index, and is 93 // always present (use cat.PrimaryIndex to select it). The primary index 94 // corresponds to the table's primary key. If a primary key was not 95 // explicitly specified, then the system implicitly creates one based on a 96 // hidden rowid column. 97 Index(i IndexOrdinal) Index 98 99 // StatisticCount returns the number of statistics available for the table. 100 StatisticCount() int 101 102 // Statistic returns the ith statistic, where i < StatisticCount. 103 Statistic(i int) TableStatistic 104 105 // CheckCount returns the number of check constraints present on the table. 106 CheckCount() int 107 108 // Check returns the ith check constraint, where i < CheckCount. 109 Check(i int) CheckConstraint 110 111 // FamilyCount returns the number of column families present on the table. 112 // There is always at least one primary family (always family 0) where columns 113 // go if they are not explicitly assigned to another family. The primary 114 // family is the first family that was explicitly specified by the user, or 115 // is a synthesized family if no families were explicitly specified. 116 FamilyCount() int 117 118 // Family returns the interface for the ith column family, where 119 // i < FamilyCount. 120 Family(i int) Family 121 122 // OutboundForeignKeyCount returns the number of outbound foreign key 123 // references (where this is the origin table). 124 OutboundForeignKeyCount() int 125 126 // OutboundForeignKeyCount returns the ith outbound foreign key reference. 127 OutboundForeignKey(i int) ForeignKeyConstraint 128 129 // InboundForeignKeyCount returns the number of inbound foreign key references 130 // (where this is the referenced table). 131 InboundForeignKeyCount() int 132 133 // InboundForeignKey returns the ith inbound foreign key reference. 134 InboundForeignKey(i int) ForeignKeyConstraint 135 } 136 137 // CheckConstraint contains the SQL text and the validity status for a check 138 // constraint on a table. Check constraints are user-defined restrictions 139 // on the content of each row in a table. For example, this check constraint 140 // ensures that only values greater than zero can be inserted into the table: 141 // 142 // CREATE TABLE a (a INT CHECK (a > 0)) 143 // 144 type CheckConstraint struct { 145 Constraint string 146 Validated bool 147 } 148 149 // TableStatistic is an interface to a table statistic. Each statistic is 150 // associated with a set of columns. 151 type TableStatistic interface { 152 // CreatedAt indicates when the statistic was generated. 153 CreatedAt() time.Time 154 155 // ColumnCount is the number of columns the statistic pertains to. 156 ColumnCount() int 157 158 // ColumnOrdinal returns the column ordinal (see Table.Column) of the ith 159 // column in this statistic, with 0 <= i < ColumnCount. 160 ColumnOrdinal(i int) int 161 162 // RowCount returns the estimated number of rows in the table. 163 RowCount() uint64 164 165 // DistinctCount returns the estimated number of distinct values on the 166 // columns of the statistic. If there are multiple columns, each "value" is a 167 // tuple with the values on each column. 168 DistinctCount() uint64 169 170 // NullCount returns the estimated number of rows which have a NULL value on 171 // any column in the statistic. 172 NullCount() uint64 173 174 // Histogram returns a slice of histogram buckets, sorted by UpperBound. 175 // It is only used for single-column stats (i.e., when ColumnCount() = 1), 176 // and it represents the distribution of values for that column. 177 // See HistogramBucket for more details. 178 Histogram() []HistogramBucket 179 } 180 181 // HistogramBucket contains the data for a single histogram bucket. Note 182 // that NumEq, NumRange, and DistinctRange are floats so the statisticsBuilder 183 // can apply filters to the histogram. 184 type HistogramBucket struct { 185 // NumEq is the estimated number of values equal to UpperBound. 186 NumEq float64 187 188 // NumRange is the estimated number of values between the upper bound of the 189 // previous bucket and UpperBound (both boundaries are exclusive). 190 // The first bucket should always have NumRange=0. 191 NumRange float64 192 193 // DistinctRange is the estimated number of distinct values between the upper 194 // bound of the previous bucket and UpperBound (both boundaries are 195 // exclusive). 196 DistinctRange float64 197 198 // UpperBound is the upper bound of the bucket. 199 UpperBound tree.Datum 200 } 201 202 // ForeignKeyConstraint represents a foreign key constraint. A foreign key 203 // constraint has an origin (or referencing) side and a referenced side. For 204 // example: 205 // ALTER TABLE o ADD CONSTRAINT fk FOREIGN KEY (a,b) REFERENCES r(a,b) 206 // Here o is the origin table, r is the referenced table, and we have two pairs 207 // of columns: (o.a,r.a) and (o.b,r.b). 208 type ForeignKeyConstraint interface { 209 // Name of the foreign key constraint. 210 Name() string 211 212 // OriginTableID returns the referencing table's stable identifier. 213 OriginTableID() StableID 214 215 // ReferencedTableID returns the referenced table's stable identifier. 216 ReferencedTableID() StableID 217 218 // ColumnCount returns the number of column pairs in this FK reference. 219 ColumnCount() int 220 221 // OriginColumnOrdinal returns the ith column ordinal (see Table.Column) in 222 // the origin (referencing) table. The ID() of originTable must equal 223 // OriginTable(). 224 OriginColumnOrdinal(originTable Table, i int) int 225 226 // ReferencedColumnOrdinal returns the ith column ordinal (see Table.Column) 227 // in the referenced table. The ID() of referencedTable must equal 228 // ReferencedTable(). 229 ReferencedColumnOrdinal(referencedTable Table, i int) int 230 231 // Validated is true if the reference is validated (i.e. we know that the 232 // existing data satisfies the constraint). It is possible to set up a foreign 233 // key constraint on existing tables without validating it, in which case we 234 // cannot make any assumptions about the data. An unvalidated constraint still 235 // needs to be enforced on new mutations. 236 Validated() bool 237 238 // MatchMethod returns the method used for comparing composite foreign keys. 239 MatchMethod() tree.CompositeKeyMatchMethod 240 241 // DeleteReferenceAction returns the action to be performed if the foreign key 242 // constraint would be violated by a delete. 243 DeleteReferenceAction() tree.ReferenceAction 244 245 // UpdateReferenceAction returns the action to be performed if the foreign key 246 // constraint would be violated by an update. 247 UpdateReferenceAction() tree.ReferenceAction 248 }