github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/internal/sqlsmith/sampler.go (about) 1 // This file was automatically generated by genny. 2 // Any changes will be lost if this file is regenerated. 3 // see https://github.com/cheekybits/genny 4 5 // Copyright 2019 The Cockroach Authors. 6 // 7 // Use of this software is governed by the Business Source License 8 // included in the file licenses/BSL.txt. 9 // 10 // As of the Change Date specified in that file, in accordance with 11 // the Business Source License, use of this software will be governed 12 // by the Apache License, Version 2.0, included in the file 13 // licenses/APL.txt. 14 15 package sqlsmith 16 17 import ( 18 "math/rand" 19 20 "github.com/cockroachdb/cockroach/pkg/util/syncutil" 21 ) 22 23 // statementWeight is the generic weight type. 24 type statementWeight struct { 25 weight int 26 elem statement 27 } 28 29 // newWeightedStatementSampler creates a statementSampler that produces 30 // statements. They are returned at the relative frequency of the values of 31 // weights. All weights must be >= 1. 32 func newWeightedStatementSampler(weights []statementWeight, seed int64) *statementSampler { 33 sum := 0 34 for _, w := range weights { 35 if w.weight < 1 { 36 panic("expected weight >= 1") 37 } 38 sum += w.weight 39 } 40 if sum == 0 { 41 panic("expected weights") 42 } 43 samples := make([]statement, sum) 44 pos := 0 45 for _, w := range weights { 46 for count := 0; count < w.weight; count++ { 47 samples[pos] = w.elem 48 pos++ 49 } 50 } 51 return &statementSampler{ 52 rnd: rand.New(rand.NewSource(seed)), 53 samples: samples, 54 } 55 } 56 57 type statementSampler struct { 58 mu syncutil.Mutex 59 rnd *rand.Rand 60 samples []statement 61 } 62 63 func (w *statementSampler) Next() statement { 64 w.mu.Lock() 65 v := w.samples[w.rnd.Intn(len(w.samples))] 66 w.mu.Unlock() 67 return v 68 } 69 70 // Copyright 2019 The Cockroach Authors. 71 // 72 // Use of this software is governed by the Business Source License 73 // included in the file licenses/BSL.txt. 74 // 75 // As of the Change Date specified in that file, in accordance with 76 // the Business Source License, use of this software will be governed 77 // by the Apache License, Version 2.0, included in the file 78 // licenses/APL.txt. 79 80 // tableExprWeight is the generic weight type. 81 type tableExprWeight struct { 82 weight int 83 elem tableExpr 84 } 85 86 // newWeightedTableExprSampler creates a tableExprSampler that produces 87 // tableExprs. They are returned at the relative frequency of the values of 88 // weights. All weights must be >= 1. 89 func newWeightedTableExprSampler(weights []tableExprWeight, seed int64) *tableExprSampler { 90 sum := 0 91 for _, w := range weights { 92 if w.weight < 1 { 93 panic("expected weight >= 1") 94 } 95 sum += w.weight 96 } 97 if sum == 0 { 98 panic("expected weights") 99 } 100 samples := make([]tableExpr, sum) 101 pos := 0 102 for _, w := range weights { 103 for count := 0; count < w.weight; count++ { 104 samples[pos] = w.elem 105 pos++ 106 } 107 } 108 return &tableExprSampler{ 109 rnd: rand.New(rand.NewSource(seed)), 110 samples: samples, 111 } 112 } 113 114 type tableExprSampler struct { 115 mu syncutil.Mutex 116 rnd *rand.Rand 117 samples []tableExpr 118 } 119 120 func (w *tableExprSampler) Next() tableExpr { 121 w.mu.Lock() 122 v := w.samples[w.rnd.Intn(len(w.samples))] 123 w.mu.Unlock() 124 return v 125 } 126 127 // Copyright 2019 The Cockroach Authors. 128 // 129 // Use of this software is governed by the Business Source License 130 // included in the file licenses/BSL.txt. 131 // 132 // As of the Change Date specified in that file, in accordance with 133 // the Business Source License, use of this software will be governed 134 // by the Apache License, Version 2.0, included in the file 135 // licenses/APL.txt. 136 137 // selectStatementWeight is the generic weight type. 138 type selectStatementWeight struct { 139 weight int 140 elem selectStatement 141 } 142 143 // newWeightedSelectStatementSampler creates a selectStatementSampler that produces 144 // selectStatements. They are returned at the relative frequency of the values of 145 // weights. All weights must be >= 1. 146 func newWeightedSelectStatementSampler( 147 weights []selectStatementWeight, seed int64, 148 ) *selectStatementSampler { 149 sum := 0 150 for _, w := range weights { 151 if w.weight < 1 { 152 panic("expected weight >= 1") 153 } 154 sum += w.weight 155 } 156 if sum == 0 { 157 panic("expected weights") 158 } 159 samples := make([]selectStatement, sum) 160 pos := 0 161 for _, w := range weights { 162 for count := 0; count < w.weight; count++ { 163 samples[pos] = w.elem 164 pos++ 165 } 166 } 167 return &selectStatementSampler{ 168 rnd: rand.New(rand.NewSource(seed)), 169 samples: samples, 170 } 171 } 172 173 type selectStatementSampler struct { 174 mu syncutil.Mutex 175 rnd *rand.Rand 176 samples []selectStatement 177 } 178 179 func (w *selectStatementSampler) Next() selectStatement { 180 w.mu.Lock() 181 v := w.samples[w.rnd.Intn(len(w.samples))] 182 w.mu.Unlock() 183 return v 184 } 185 186 // Copyright 2019 The Cockroach Authors. 187 // 188 // Use of this software is governed by the Business Source License 189 // included in the file licenses/BSL.txt. 190 // 191 // As of the Change Date specified in that file, in accordance with 192 // the Business Source License, use of this software will be governed 193 // by the Apache License, Version 2.0, included in the file 194 // licenses/APL.txt. 195 196 // scalarExprWeight is the generic weight type. 197 type scalarExprWeight struct { 198 weight int 199 elem scalarExpr 200 } 201 202 // newWeightedScalarExprSampler creates a scalarExprSampler that produces 203 // scalarExprs. They are returned at the relative frequency of the values of 204 // weights. All weights must be >= 1. 205 func newWeightedScalarExprSampler(weights []scalarExprWeight, seed int64) *scalarExprSampler { 206 sum := 0 207 for _, w := range weights { 208 if w.weight < 1 { 209 panic("expected weight >= 1") 210 } 211 sum += w.weight 212 } 213 if sum == 0 { 214 panic("expected weights") 215 } 216 samples := make([]scalarExpr, sum) 217 pos := 0 218 for _, w := range weights { 219 for count := 0; count < w.weight; count++ { 220 samples[pos] = w.elem 221 pos++ 222 } 223 } 224 return &scalarExprSampler{ 225 rnd: rand.New(rand.NewSource(seed)), 226 samples: samples, 227 } 228 } 229 230 type scalarExprSampler struct { 231 mu syncutil.Mutex 232 rnd *rand.Rand 233 samples []scalarExpr 234 } 235 236 func (w *scalarExprSampler) Next() scalarExpr { 237 w.mu.Lock() 238 v := w.samples[w.rnd.Intn(len(w.samples))] 239 w.mu.Unlock() 240 return v 241 }