@@ -102,24 +102,14 @@ func RandCreateCompositeType(rng *rand.Rand, name, alphabet string) tree.Stateme
102
102
}
103
103
}
104
104
105
- type TableOpt uint8
106
-
107
- const (
108
- TableOptNone TableOpt = 0
109
- TableOptPrimaryIndexRequired TableOpt = 1 << (iota - 1 )
110
- TableOptSkipColumnFamilyMutations
111
- TableOptMultiRegion
112
- TableOptAllowPartiallyVisibleIndex
113
- TableOptCrazyNames
114
- )
115
-
116
- func (t TableOpt ) IsSet (o TableOpt ) bool {
117
- return t & o == o
118
- }
119
-
120
105
// RandCreateTables creates random table definitions.
121
106
func RandCreateTables (
122
- ctx context.Context , rng * rand.Rand , prefix string , num int , opt TableOpt , mutators ... Mutator ,
107
+ ctx context.Context ,
108
+ rng * rand.Rand ,
109
+ prefix string ,
110
+ num int ,
111
+ opts []TableOption ,
112
+ mutators ... Mutator ,
123
113
) []tree.Statement {
124
114
if num < 1 {
125
115
panic ("at least one table required" )
@@ -128,7 +118,7 @@ func RandCreateTables(
128
118
// Make some random tables.
129
119
tables := make ([]tree.Statement , num )
130
120
for i := 0 ; i < num ; i ++ {
131
- t := RandCreateTable (ctx , rng , prefix , i + 1 , opt )
121
+ t := RandCreateTable (ctx , rng , prefix , i + 1 , opts )
132
122
tables [i ] = t
133
123
}
134
124
@@ -141,10 +131,10 @@ func RandCreateTables(
141
131
142
132
// RandCreateTable creates a random CreateTable definition.
143
133
func RandCreateTable (
144
- ctx context.Context , rng * rand.Rand , prefix string , tableIdx int , opt TableOpt ,
134
+ ctx context.Context , rng * rand.Rand , prefix string , tableIdx int , opts [] TableOption ,
145
135
) * tree.CreateTable {
146
136
return RandCreateTableWithColumnIndexNumberGenerator (
147
- ctx , rng , prefix , tableIdx , opt , nil , /* generateColumnIndexNumber */
137
+ ctx , rng , prefix , tableIdx , opts , nil , /* generateColumnIndexNumber */
148
138
)
149
139
}
150
140
@@ -161,26 +151,27 @@ func RandCreateTableWithColumnIndexNumberGenerator(
161
151
rng * rand.Rand ,
162
152
prefix string ,
163
153
tableIdx int ,
164
- opt TableOpt ,
154
+ opts [] TableOption ,
165
155
generateColumnIndexSuffix func () string ,
166
156
) * tree.CreateTable {
157
+ options := applyOptions (opts )
167
158
var name string
168
- if opt . IsSet ( TableOptCrazyNames ) {
159
+ if options . crazyNames {
169
160
g := randident .NewNameGenerator (& nameGenCfg , rng , prefix )
170
161
name = g .GenerateOne (strconv .Itoa (tableIdx ))
171
162
} else {
172
163
name = fmt .Sprintf ("%s%d" , prefix , tableIdx )
173
164
}
174
165
return randCreateTableWithColumnIndexNumberGeneratorAndName (
175
- ctx , rng , name , tableIdx , opt , generateColumnIndexSuffix ,
166
+ ctx , rng , name , tableIdx , opts , generateColumnIndexSuffix ,
176
167
)
177
168
}
178
169
179
170
func RandCreateTableWithName (
180
- ctx context.Context , rng * rand.Rand , tableName string , tableIdx int , opt TableOpt ,
171
+ ctx context.Context , rng * rand.Rand , tableName string , tableIdx int , opts [] TableOption ,
181
172
) * tree.CreateTable {
182
173
return randCreateTableWithColumnIndexNumberGeneratorAndName (
183
- ctx , rng , tableName , tableIdx , opt , nil , /* generateColumnIndexSuffix */
174
+ ctx , rng , tableName , tableIdx , opts , nil , /* generateColumnIndexSuffix */
184
175
)
185
176
}
186
177
@@ -189,9 +180,10 @@ func randCreateTableWithColumnIndexNumberGeneratorAndName(
189
180
rng * rand.Rand ,
190
181
tableName string ,
191
182
tableIdx int ,
192
- opt TableOpt ,
183
+ opts [] TableOption ,
193
184
generateColumnIndexSuffix func () string ,
194
185
) * tree.CreateTable {
186
+ options := applyOptions (opts )
195
187
// columnDefs contains the list of Columns we'll add to our table.
196
188
nColumns := randutil .RandIntInRange (rng , 1 , 20 )
197
189
columnDefs := make ([]* tree.ColumnTableDef , 0 , nColumns )
@@ -211,24 +203,24 @@ func randCreateTableWithColumnIndexNumberGeneratorAndName(
211
203
nComputedColumns := randutil .RandIntInRange (rng , 0 , (nColumns + 1 )/ 2 )
212
204
nNormalColumns := nColumns - nComputedColumns
213
205
for i := 0 ; i < nNormalColumns ; i ++ {
214
- columnDef := randColumnTableDef (rng , tableIdx , colSuffix (i ), opt )
206
+ columnDef := randColumnTableDef (rng , tableIdx , colSuffix (i ), opts )
215
207
columnDefs = append (columnDefs , columnDef )
216
208
defs = append (defs , columnDef )
217
209
}
218
210
219
211
// Make defs for computed columns.
220
212
normalColDefs := columnDefs
221
213
for i := nNormalColumns ; i < nColumns ; i ++ {
222
- columnDef := randComputedColumnTableDef (rng , normalColDefs , tableIdx , colSuffix (i ), opt )
214
+ columnDef := randComputedColumnTableDef (rng , normalColDefs , tableIdx , colSuffix (i ), opts )
223
215
columnDefs = append (columnDefs , columnDef )
224
216
defs = append (defs , columnDef )
225
217
}
226
218
227
219
// Make a random primary key with high likelihood.
228
220
var pk * tree.IndexTableDef
229
- if opt . IsSet ( TableOptPrimaryIndexRequired ) || (rng .Intn (8 ) != 0 ) {
221
+ if options . primaryIndexRequired || (rng .Intn (8 ) != 0 ) {
230
222
for {
231
- indexDef , ok := randIndexTableDefFromCols (ctx , rng , columnDefs , tableName , true /* isPrimaryIndex */ , opt )
223
+ indexDef , ok := randIndexTableDefFromCols (ctx , rng , columnDefs , tableName , true /* isPrimaryIndex */ , opts )
232
224
canUseIndex := ok && indexDef .Type .CanBePrimary ()
233
225
if canUseIndex {
234
226
// Although not necessary for Cockroach to function correctly,
@@ -248,7 +240,7 @@ func randCreateTableWithColumnIndexNumberGeneratorAndName(
248
240
IndexTableDef : indexDef ,
249
241
})
250
242
}
251
- if canUseIndex || ! opt . IsSet ( TableOptPrimaryIndexRequired ) {
243
+ if canUseIndex || ! options . primaryIndexRequired {
252
244
break
253
245
}
254
246
}
@@ -257,7 +249,7 @@ func randCreateTableWithColumnIndexNumberGeneratorAndName(
257
249
// Make indexes.
258
250
nIdxs := rng .Intn (10 )
259
251
for i := 0 ; i < nIdxs ; i ++ {
260
- indexDef , ok := randIndexTableDefFromCols (ctx , rng , columnDefs , tableName , false /* isPrimaryIndex */ , opt )
252
+ indexDef , ok := randIndexTableDefFromCols (ctx , rng , columnDefs , tableName , false /* isPrimaryIndex */ , opts )
261
253
if ! ok {
262
254
continue
263
255
}
@@ -291,7 +283,7 @@ func randCreateTableWithColumnIndexNumberGeneratorAndName(
291
283
indexDef .Invisibility .Value = 0.0
292
284
if notvisible := rng .Intn (6 ) == 0 ; notvisible {
293
285
indexDef .Invisibility .Value = 1.0
294
- if opt . IsSet ( TableOptAllowPartiallyVisibleIndex ) {
286
+ if options . allowPartiallyVisibleIndex {
295
287
if rng .Intn (2 ) == 0 {
296
288
indexDef .Invisibility .Value = 1 - rng .Float64 ()
297
289
indexDef .Invisibility .FloatProvided = true
@@ -309,7 +301,7 @@ func randCreateTableWithColumnIndexNumberGeneratorAndName(
309
301
}
310
302
311
303
// Create some random column families.
312
- if ! opt . IsSet ( TableOptSkipColumnFamilyMutations ) && rng .Intn (2 ) == 0 {
304
+ if ! options . skipColumnFamilyMutations && rng .Intn (2 ) == 0 {
313
305
ColumnFamilyMutator (rng , ret )
314
306
}
315
307
@@ -463,10 +455,11 @@ func PopulateTableWithRandData(
463
455
// randColumnTableDef produces a random ColumnTableDef for a non-computed
464
456
// column, with a random type and nullability.
465
457
func randColumnTableDef (
466
- rng * rand.Rand , tableIdx int , colSuffix string , opt TableOpt ,
458
+ rng * rand.Rand , tableIdx int , colSuffix string , opts [] TableOption ,
467
459
) * tree.ColumnTableDef {
460
+ options := applyOptions (opts )
468
461
var colName tree.Name
469
- if opt . IsSet ( TableOptCrazyNames ) {
462
+ if options . crazyNames {
470
463
g := randident .NewNameGenerator (& nameGenCfg , rng , fmt .Sprintf ("col%d" , tableIdx ))
471
464
colName = tree .Name (g .GenerateOne (colSuffix ))
472
465
} else {
@@ -501,9 +494,9 @@ func randComputedColumnTableDef(
501
494
normalColDefs []* tree.ColumnTableDef ,
502
495
tableIdx int ,
503
496
colSuffix string ,
504
- opt TableOpt ,
497
+ opts [] TableOption ,
505
498
) * tree.ColumnTableDef {
506
- newDef := randColumnTableDef (rng , tableIdx , colSuffix , opt )
499
+ newDef := randColumnTableDef (rng , tableIdx , colSuffix , opts )
507
500
newDef .Computed .Computed = true
508
501
newDef .Computed .Virtual = rng .Intn (2 ) == 0
509
502
@@ -524,8 +517,9 @@ func randIndexTableDefFromCols(
524
517
columnTableDefs []* tree.ColumnTableDef ,
525
518
tableName string ,
526
519
isPrimaryIndex bool ,
527
- opt TableOpt ,
520
+ opts [] TableOption ,
528
521
) (def tree.IndexTableDef , ok bool ) {
522
+ options := applyOptions (opts )
529
523
cpy := make ([]* tree.ColumnTableDef , len (columnTableDefs ))
530
524
copy (cpy , columnTableDefs )
531
525
rng .Shuffle (len (cpy ), func (i , j int ) { cpy [i ], cpy [j ] = cpy [j ], cpy [i ] })
@@ -655,7 +649,7 @@ func randIndexTableDefFromCols(
655
649
// not support partitioning.
656
650
// TODO(harding): Allow partitioning the primary index. This will require
657
651
// massaging the syntax.
658
- if ! opt . IsSet ( TableOptMultiRegion ) && ! isPrimaryIndex && ! partitioningNotSupported && len (prefix ) > 0 && rng .Intn (10 ) == 0 {
652
+ if ! options . multiRegion && ! isPrimaryIndex && ! partitioningNotSupported && len (prefix ) > 0 && rng .Intn (10 ) == 0 {
659
653
def .PartitionByIndex = & tree.PartitionByIndex {PartitionBy : & tree.PartitionBy {}}
660
654
prefixLen := 1 + rng .Intn (len (prefix ))
661
655
def .PartitionByIndex .Fields = prefix [:prefixLen ]
@@ -666,7 +660,7 @@ func randIndexTableDefFromCols(
666
660
numExpressions := rng .Intn (10 ) + 1
667
661
for i := 0 ; i < numPartitions ; i ++ {
668
662
var partition tree.ListPartition
669
- if opt . IsSet ( TableOptCrazyNames ) {
663
+ if options . crazyNames {
670
664
partition .Name = tree .Name (g .GenerateOne (strconv .Itoa (i )))
671
665
} else {
672
666
partition .Name = tree .Name (fmt .Sprintf ("%s_part_%d" , tableName , i ))
0 commit comments