-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbgen.go
1188 lines (1081 loc) · 29.4 KB
/
dbgen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2032 The Sqlite Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"database/sql"
"fmt"
"math"
"math/big"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"modernc.org/mathutil"
"modernc.org/sqlite/tpch/driver"
)
var (
// .2.2.12 All dates must be computed using the following values:
//
// STARTDATE = 1992-01-01 CURRENTDATE = 1995-06-17 ENDDATE = 1998-12-31
StartDate = time.Date(1992, 1, 1, 0, 0, 0, 0, time.UTC)
CurrentDate = time.Date(1995, 6, 17, 12, 12, 0, 0, time.UTC)
EndDate = time.Date(1998, 12, 31, 23, 59, 59, 999999999, time.UTC)
seed, _ = mathutil.NewFCBig(big.NewInt(0), big.NewInt(math.MaxInt64), true)
prices []int32
maxRecs = -1
)
func ns2time(ns int64) time.Time { return time.Unix(ns/1e9, ns%1e9).UTC() }
// 1.3 Datatype Definitions
//
// 1.3.1 The following datatype definitions apply to the list of columns of
// each table:
//
// - Identifier means that the column must be able to hold any key value
// generated for that column and be able to support at least 2,147,483,647
// unique values;
//
// Comment: A common implementation of this datatype will be an integer.
// However, for SF greater than 300 some column values will exceed the range of
// integer values supported by a 4-byte integer. A test sponsor may use some
// other datatype such as 8-byte integer, decimal or character string to
// implement the identifier datatype;
//
// - Integer means that the column must be able to exactly represent integer
// values (i.e., values in increments of 1) in the range of at least
// -2,147,483,646 to 2,147,483,647.
//
// - Decimal means that the column must be able to represent values in the
// range -9,999,999,999.99 to +9,999,999,999.99 in increments of 0.01; the
// values can be either represented exactly or interpreted to be in this range;
//
// - Big Decimal is of the Decimal datatype as defined above, with the
// additional property that it must be large enough to represent the aggregated
// values stored in temporary tables created within query variants;
//
// - Fixed text, size N means that the column must be able to hold any string
// of characters of a fixed length of N.
//
// Comment: If the string it holds is shorter than N characters, then trailing
// spaces must be stored in the database or the database must automatically pad
// with spaces upon retrieval such that a CHAR_LENGTH() function will return N.
//
// - Variable text, size N means that the column must be able to hold any
// string of characters of a variable length with a maximum length of N.
// Columns defined as "variable text, size N" may optionally be implemented as
// "fixed text, size N";
//
// - Date is a value whose external representation can be expressed as
// YYYY-MM-DD, where all characters are numeric. A date must be able to express
// any day within at least 14 consecutive years. There is no requirement
// specific to the internal representation of a date.
//
// Comment: The implementation datatype chosen by the test sponsor for a
// particular datatype definition must be applied consistently to all the
// instances of that datatype definition in the schema, except for identifier
// columns, whose datatype may be selected to satisfy database scaling
// requirements.
//
// 1.3.2 The symbol SF is used in this document to represent the scale factor
// for the database (see Clause 4: ).
type rng struct {
r *mathutil.FCBig
}
func newRng(lo, hi int64) *rng {
r, err := mathutil.NewFCBig(big.NewInt(lo), big.NewInt(hi), true)
if err != nil {
panic("internal error")
}
r.Seed(seed.Next().Int64())
return &rng{r}
}
func (r *rng) n() int64 {
return r.r.Next().Int64()
}
// 4.2.2.2 The term unique within [x] represents any one value within a set of
// x values between 1 and x, unique within the scope of rows being populated.
func uniqueWithin(x int64) *rng { return newRng(1, x) }
// 4.2.2.3 The notation random value [x .. y] represents a random value between
// x and y inclusively, with a mean of (x+y)/2, and with the same number of
// digits of precision as shown. For example, [0.01 .. 100.00] has 10,000
// unique values, whereas [1..100] has only 100 unique values.
func (r *rng) randomValue(min, max int64) int64 {
return min + r.n()%(max-min+1)
}
// 4.2.2.7 The notation random v-string [min, max] represents a string
// comprised of randomly generated alphanumeric characters within a character
// set of at least 64 symbols. The length of the string is a random value
// between min and max inclusive.
func (r *rng) vString(min, max int) string {
l := min + int(r.n())%(max-min+1)
b := make([]byte, l)
for i := range b {
b[i] = '0' + byte(r.n()%64)
}
return string(b)
}
// 4.2.2.9 The term phone number represents a string of numeric characters
// separated by hyphens and generated as follows:
//
// Let i be an index into the list of strings Nations (i.e., ALGERIA is 0,
// ARGENTINA is 1, etc., see Clause 4.2.3),
//
// Let country_code be the sub-string representation of the number (i + 10),
//
// Let local_number1 be random [100 .. 999],
//
// Let local_number2 be random [100 .. 999],
//
// Let local_number3 be random [1000 .. 9999],
//
// The phone number string is obtained by concatenating the following
// sub-strings:
//
// country_code, "-", local_number1, "-", local_number2, "-", local_number3
func (r *rng) phoneNumber(i int) string {
return fmt.Sprintf("%v-%v-%v-%v", i+10, 100+r.n()%900, 100+r.n()%900, 1000+r.n()%9000)
}
// 4.2.2.10 The term text string[min, max] represents a substring of a 300 MB
// string populated according to the pseudo text grammar defined in Clause
// 4.2.2.14. The length of the substring is a random number between min and max
// inclusive. The substring offset is randomly chosen.
func (r *rng) textString(min, max int64) string {
off := r.n() % (int64(len(pseudotext)) - max)
l := min + r.n()%(max-min+1)
return string(pseudotext[off : off+l])
}
var (
types1 = []string{
"STANDARD",
"SMALL",
"MEDIUM",
"LARGE",
"ECONOMY",
"PROMO",
}
types2 = []string{
"ANODIZED",
"BURNISHED",
"PLATED",
"POLISHED",
"BRUSHED",
}
types3 = []string{
"TIN",
"NICKEL",
"BRASS",
"STELL",
"COPPER",
}
)
// 4.2.2.13
func (r *rng) types() string {
return types1[int(r.n())%len(types1)] + " " + types2[int(r.n())%len(types2)] + " " + types3[int(r.n())%len(types3)]
}
var (
containers1 = []string{
"SM",
"LG",
"MED",
"JUMBO",
"WRAP",
}
containers2 = []string{
"CASE",
"BOX",
"BAG",
"JAR",
"PKG",
"PACK",
"CAN",
"DRUM",
}
)
func (r *rng) containers() string {
return containers1[int(r.n())%len(containers1)] + " " + containers2[int(r.n())%len(containers2)]
}
var segments1 = []string{
"AUTOMOBILE",
"BUILDING",
"FURNITURE",
"MACHINERY",
"HOUSEHOLD",
}
func (r *rng) segments() string {
return segments1[int(r.n())%len(segments1)]
}
var priorities1 = []string{
"1-URGENT",
"2-HIGH",
"3-MEDIUM",
"4-NOT SPECIFIED",
"5-LOW",
}
func (r *rng) priorities() string {
return priorities1[int(r.n())%len(priorities1)]
}
var instructions1 = []string{
"DELIVER IN PERSON",
"COLLECT COD",
"NONE",
"TAKE BACK RETURN",
}
func (r *rng) instructions() string {
return instructions1[int(r.n())%len(instructions1)]
}
var modes1 = []string{
"REG AIR",
"AIR",
"RAIL",
"SHIP",
"TRUCK",
"MAIL",
"FOB",
}
func (r *rng) modes() string {
return modes1[int(r.n())%len(modes1)]
}
var nouns1 = []string{
"foxes",
"ideas",
"theodolites",
"pinto",
"beans",
"instructions",
"dependencies",
"excuses",
"platelets",
"asymptotes",
"courts",
"dolphins",
"multipliers",
"sauternes",
"warthogs",
"frets",
"dinos",
"attainments",
"somas",
"Tiresias'",
"patterns",
"forges",
"braids",
"hockey",
"players",
"frays",
"warhorses",
"dugouts",
"notornis",
"epitaphs",
"pearls",
"tithes",
"waters",
"orbits",
"gifts",
"sheaves",
"depths",
"sentiments",
"decoys",
"realms",
"pains",
"grouches",
"escapades",
}
func (r *rng) nouns() string {
return nouns1[int(r.n())%len(nouns1)]
}
var verbs1 = []string{
"sleep",
"wake",
"are",
"cajole",
"haggle",
"nag",
"use",
"boost",
"affix",
"detect",
"integrate",
"maintain",
"nod",
"was",
"lose",
"sublate",
"solve",
"thrash",
"promise",
"engage",
"hinder",
"print",
"x-ray",
"breach",
"eat",
"grow",
"impress",
"mold",
"poach",
"serve",
"run",
"dazzle",
"snooze",
"doze",
"unwind",
"kindle",
"play",
"hang",
"believe",
"doubt",
}
func (r *rng) verbs() string {
return verbs1[int(r.n())%len(verbs1)]
}
var adjectives1 = []string{
"furious",
"sly",
"careful",
"blithe",
"quick",
"fluffy",
"slow",
"quiet",
"ruthless",
"thin",
"close",
"dogged",
"daring",
"brave",
"stealthy",
"permanent",
"enticing",
"idle",
"busy",
"regular",
"final",
"ironic",
"even",
"bold",
"silent",
}
func (r *rng) adjectives() string {
return adjectives1[int(r.n())%len(adjectives1)]
}
var adverbs1 = []string{
"sometimes",
"always",
"never",
"furiously",
"slyly",
"carefully",
"blithely",
"quickly",
"fluffily",
"slowly",
"quietly",
"ruthlessly",
"thinly",
"closely",
"doggedly",
"daringly",
"bravely",
"stealthily",
"permanently",
"enticingly",
"idly",
"busily",
"regularly",
"finally",
"ironically",
"evenly",
"boldly",
"silently",
}
func (r *rng) adverbs() string {
return adverbs1[int(r.n())%len(adverbs1)]
}
var prepositions1 = []string{
"about",
"above",
"according to",
"across",
"after",
"against",
"along",
"alongside of",
"among",
"around",
"at",
"atop",
"before",
"behind",
"beneath",
"beside",
"besides",
"between",
"beyond",
"by",
"despite",
"during",
"except",
"for",
"from",
"in place of",
"inside",
"instead of",
"into",
"near",
"of",
"on",
"outside",
"over",
"past",
"since",
"through",
"throughout",
"to",
"toward",
"under",
"until",
"up",
"upon",
"without",
"with",
"within",
}
func (r *rng) prepositions() string {
return prepositions1[int(r.n())%len(prepositions1)]
}
var auxiliaries1 = []string{
"do",
"may",
"might",
"shall",
"will",
"would",
"can",
"could",
"should",
"ought to",
"must",
"will have to",
"shall have to",
"could have to",
"should have to",
"must have to",
"need to",
"try to",
}
func (r *rng) auxiliaries() string {
return auxiliaries1[int(r.n())%len(auxiliaries1)]
}
var terminators1 = []string{
".",
";",
"!",
":",
"?",
"--",
}
func (r *rng) terminators() string {
return terminators1[int(r.n())%len(terminators1)]
}
var pseudotext []byte
func genPseudotext() (err error) {
pth := filepath.Join("testdata", "pseudotext")
if _, err = os.Stat(pth); err == nil {
return fmt.Errorf("file already exists: %s", pth)
}
if !os.IsNotExist(err) {
return err
}
if err = os.MkdirAll("testdata", 0766); err != nil {
return err
}
f, err := os.Create(pth)
if err != nil {
return err
}
defer func() {
if cerr := f.Close(); cerr != nil && err == nil {
err = cerr
}
}()
w := bufio.NewWriter(f)
defer func() {
if ferr := w.Flush(); ferr != nil && err == nil {
err = ferr
}
}()
const sz = 300 * 1e6
r := newRng(0, math.MaxInt64)
nounPhrase := func() string {
switch r.n() % 4 {
case 0: // noun phrase:<noun>
return r.nouns()
case 1: // |<adjective> <noun>
return r.adjectives() + " " + r.nouns()
case 2: // |<adjective>, <adjective> <noun>
return r.adjectives() + ", " + r.adjectives() + " " + r.nouns()
case 3: // |<adverb> <adjective> <noun>
return r.adverbs() + " " + r.adjectives() + " " + r.nouns()
}
panic("internal error")
}
verbPhrase := func() string {
switch r.n() % 4 {
case 0: // verb phrase:<verb>
return r.verbs()
case 1: // |<auxiliary> <verb>
return r.auxiliaries() + " " + r.verbs()
case 2: // |<verb> <adverb>
return r.verbs() + " " + r.adverbs()
case 3: // |<auxiliary> <verb> <adverb>
return r.auxiliaries() + " " + r.verbs() + " " + r.adverbs()
}
panic("internal error")
}
prepositionalPhrase := func() string {
// prepositional phrase: <preposition> the <noun phrase>
return r.prepositions() + " the " + nounPhrase()
}
sentence := func() string {
switch r.n() % 5 {
case 0: // sentence:<noun phrase> <verb phrase> <terminator>
return nounPhrase() + " " + verbPhrase() + r.terminators()
case 1: // |<noun phrase> <verb phrase> <prepositional phrase> <terminator>
return nounPhrase() + " " + verbPhrase() + " " + prepositionalPhrase() + r.terminators()
case 2: // |<noun phrase> <verb phrase> <noun phrase> <terminator>
return nounPhrase() + " " + verbPhrase() + " " + nounPhrase() + r.terminators()
case 3: // |<noun phrase> <prepositional phrase> <verb phrase> <noun phrase> <terminator>
return nounPhrase() + " " + prepositionalPhrase() + " " + verbPhrase() + " " + nounPhrase() + r.terminators()
case 4: // |<noun phrase> <prepositional phrase> <verb phrase> <prepositional phrase> <terminator>
return nounPhrase() + " " + prepositionalPhrase() + " " + verbPhrase() + " " + prepositionalPhrase() + r.terminators()
}
panic("internal error")
}
n := 0
for n < sz {
s := sentence() + " "
if _, err = w.WriteString(s); err != nil {
return err
}
n += len(s)
}
return nil
}
func pthForSUT(sut driver.SUT, sf int) string {
return filepath.Join("testdata", sut.Name(), "sf"+strconv.Itoa(sf))
}
func dbGen(sut driver.SUT, sf int) (err error) {
if pseudotext, err = os.ReadFile(filepath.Join("testdata", "pseudotext")); err != nil {
return fmt.Errorf("Run this program with -pseudotext: %v", err)
}
pth := pthForSUT(sut, sf)
if err = os.MkdirAll(pth, 0766); err != nil {
return err
}
if err = sut.SetWD(pth); err != nil {
return err
}
db, err := sut.OpenDB()
if err != nil {
return err
}
defer func() {
if cerr := db.Close(); cerr != nil && err == nil {
err = cerr
}
}()
if err = sut.CreateTables(); err != nil {
return err
}
if err = genSupplier(db, sf, sut); err != nil {
return err
}
if err = genPartAndPartSupp(db, sf, sut); err != nil {
return err
}
if err = genCustomerAndOrders(db, sf, sut); err != nil {
return err
}
if err = genNation(db, sf, sut); err != nil {
return err
}
return genRegion(db, sf, sut)
}
func genSupplier(db *sql.DB, sf int, sut driver.SUT) (err error) {
recs := 10000
if n := maxRecs; n >= 0 {
recs = n
}
keyrng := uniqueWithin(int64(sf) * int64(recs))
rng5 := uniqueWithin(int64(sf) * int64(recs))
sf5rows := make(map[int64]bool)
for i := 0; i < sf*5; i++ {
sf5rows[rng5.n()] = true
sf5rows[rng5.n()] = false
}
rng := newRng(0, math.MaxInt64)
tx, err := db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare(sut.InsertSupplier())
if err != nil {
return err
}
// SF * 10,000 rows in the SUPPLIER table with:
// S_SUPPKEY unique within [SF * 10,000].
// S_NAME text appended with minimum 9 digits with leading zeros ["Supplie#r", S_SUPPKEY].
// S_ADDRESS random v-string[10,40].
// S_NATIONKEY random value [0 .. 24].
// S_PHONE generated according to Clause 4.2.2.9.
// S_ACCTBAL random value [-999.99 .. 9,999.99].
// S_COMMENT text string [25,100].
// SF * 5 rows are randomly selected to hold at a random position
// a string matching "Customer%Complaints". Another SF * 5 rows
// are randomly selected to hold at a random position a string
// matching "Customer%Recommends", where % is a wildcard that
// denotes zero or more characters.
for i := 0; i < sf*recs; i++ {
sSuppKey := keyrng.n()
nk := int(rng.n() % 25)
sComment := rng.textString(25, 100)
if b, ok := sf5rows[sSuppKey]; ok {
s := "Complaints"
if b {
s = "Recommends"
}
s = "Customer" + rng.vString(0, 4) + s
off := int(rng.randomValue(0, int64(len(sComment)-len(s))))
sComment = sComment[:off] + s + sComment[off+len(s):]
}
if _, err := stmt.Exec(
sSuppKey,
fmt.Sprintf("Supplier#%09d", sSuppKey),
rng.vString(10, 40),
nk,
rng.phoneNumber(nk),
rng.randomValue(-99999, 999999),
sComment,
); err != nil {
return err
}
}
return tx.Commit()
}
var pnames1 = []string{
"almond", "antique", "aquamarine", "azure", "beige", "bisque", "black", "blanched", "blue",
"blush", "brown", "burlywood", "burnished", "chartreuse", "chiffon", "chocolate", "coral",
"cornflower", "cornsilk", "cream", "cyan", "dark", "deep", "dim", "dodger", "drab", "firebrick",
"floral", "forest", "frosted", "gainsboro", "ghost", "goldenrod", "green", "grey", "honeydew",
"hot", "indian", "ivory", "khaki", "lace", "lavender", "lawn", "lemon", "light", "lime", "linen",
"magenta", "maroon", "medium", "metallic", "midnight", "mint", "misty", "moccasin", "navajo",
"navy", "olive", "orange", "orchid", "pale", "papaya", "peach", "peru", "pink", "plum", "powder",
"puff", "purple", "red", "rose", "rosy", "royal", "saddle", "salmon", "sandy", "seashell", "sienna",
"sky", "slate", "smoke", "snow", "spring", "steel", "tan", "thistle", "tomato", "turquoise", "violet",
"wheat", "white", "yellow",
}
func genPartAndPartSupp(db *sql.DB, sf int, sut driver.SUT) (err error) {
recs := 200000
if n := maxRecs; n >= 0 {
recs = n
}
prices = make([]int32, sf*recs)
a := make([]string, 5)
var tx *sql.Tx
var stmt, stmt2 *sql.Stmt
// SF * 200,000 rows in the PART table with:
// P_PARTKEY unique within [SF * 200,000].
// P_NAME generated by concatenating five unique randomly selected strings from the following list,
// separated by a single space:
// {"almond", "antique", "aquamarine", "azure", "beige", "bisque", "black", "blanched", "blue",
// "blush", "brown", "burlywood", "burnished", "chartreuse", "chiffon", "chocolate", "coral",
// "cornflower", "cornsilk", "cream", "cyan", "dark", "deep", "dim", "dodger", "drab", "firebrick",
// "floral", "forest", "frosted", "gainsboro", "ghost", "goldenrod", "green", "grey", "honeydew",
// "hot", "indian", "ivory", "khaki", "lace", "lavender", "lawn", "lemon", "light", "lime", "linen",
// "magenta", "maroon", "medium", "metallic", "midnight", "mint", "misty", "moccasin", "navajo",
// "navy", "olive", "orange", "orchid", "pale", "papaya", "peach", "peru", "pink", "plum", "powder",
// "puff", "purple", "red", "rose", "rosy", "royal", "saddle", "salmon", "sandy", "seashell", "sienna",
// "sky", "slate", "smoke", "snow", "spring", "steel", "tan", "thistle", "tomato", "turquoise", "violet",
// "wheat", "white", "yellow"}.
// P_MFGR text appended with digit ["Manufacturer#",M], where M = random value [1,5].
// P_BRAND text appended with digits ["Brand#",MN], where N = random value [1,5] and M is defined
// while generating P_MFGR.
// P_TYPE random string [Types].
// P_SIZE random value [1 .. 50].
// P_CONTAINER random string [Containers].
// P_RETAILPRICE = (90000 + ((P_PARTKEY/10) modulo 20001 ) + 100 * (P_PARTKEY modulo 1000))/100
// P_COMMENT text string [5,22].
keyrng := uniqueWithin(int64(sf) * int64(recs))
rng := newRng(0, math.MaxInt64)
for i := 0; i < sf*recs; i++ {
if i%1000 == 0 {
if i != 0 {
if err = tx.Commit(); err != nil {
return err
}
}
if tx, err = db.Begin(); err != nil {
return err
}
if stmt, err = tx.Prepare(sut.InsertPart()); err != nil {
return err
}
if stmt2, err = tx.Prepare(sut.InsertPartSupp()); err != nil {
return err
}
}
pPartKey := keyrng.n()
a = a[:0]
for len(a) < 5 {
again:
s := pnames1[rng.n()%int64(len(pnames1))]
for _, v := range a {
if s == v {
goto again
}
}
a = append(a, s)
}
m := rng.randomValue(1, 5)
pRetailPrice := 90000 + ((pPartKey / 10) % 20001) + 100*(pPartKey%1000)
prices[pPartKey-1] = int32(pRetailPrice)
if _, err := stmt.Exec(
pPartKey,
strings.Join(a, " "),
fmt.Sprintf("Manufacturer#%d", m),
fmt.Sprintf("Brand#%d%d", m, rng.randomValue(1, 5)),
rng.types(),
rng.randomValue(1, 50),
rng.containers(),
pRetailPrice,
rng.textString(5, 22),
); err != nil {
return err
}
// For each row in the PART table, four rows in PartSupp table with:
// PS_PARTKEY = P_PARTKEY.
// PS_SUPPKEY = (ps_partkey + (i * (( S/4 ) + (int)(ps_partkey-1 )/S)))) modulo S + 1
// where i is the ith supplier within [0 .. 3] and S = SF * 10,000.
// PS_AVAILQTY random value [1 .. 9,999].
// PS_SUPPLYCOST random value [1.00 .. 1,000.00].
// PS_COMMENT text string [49,198].
s := int64(sf) * 10000
psPartKey := pPartKey
for i := 0; i < 4; i++ {
if _, err := stmt2.Exec(
psPartKey,
(psPartKey+(int64(i)*((s/4)+(psPartKey-1)/s)))%(s+1),
rng.randomValue(1, 9999),
rng.randomValue(100, 100000),
rng.textString(49, 198),
); err != nil {
return err
}
}
}
return tx.Commit()
}
func genCustomerAndOrders(db *sql.DB, sf int, sut driver.SUT) (err error) {
recs := 150000
if n := maxRecs; n >= 0 {
recs = n
}
var tx *sql.Tx
var stmt, stmt2, stmt3 *sql.Stmt
s := int64(sf) * 10000
minDate := StartDate.UnixNano()
maxDate := EndDate.UnixNano() - 151*24*int64(time.Hour)
// SF * 150,000 rows in CUSTOMER table with:
// C_CUSTKEY unique within [SF * 150,000].
// C_NAME text appended with minimum 9 digits with leading zeros ["Customer#", C_CUSTKEY].
// C_ADDRESS random v-string [10,40].
// C_NATIONKEY random value [0 .. 24].
// C_PHONE generated according to Clause 4.2.2.9.
// C_ACCTBAL random value [-999.99 .. 9,999.99].
// C_MKTSEGMENT random string [Segments].
// C_COMMENT text string [29,116].
keyrng := uniqueWithin(int64(sf) * int64(recs))
keyrng2 := uniqueWithin(int64(sf) * 10 * int64(recs))
rng := newRng(0, math.MaxInt64)
for i := 0; i < sf*recs; i++ {
if i%1000 == 0 {
if i != 0 {
if err = tx.Commit(); err != nil {
return err
}
}
if tx, err = db.Begin(); err != nil {
return err
}
if stmt, err = tx.Prepare(sut.InsertCustomer()); err != nil {
return err
}
if stmt2, err = tx.Prepare(sut.InsertOrders()); err != nil {
return err
}
if stmt3, err = tx.Prepare(sut.InsertLineItem()); err != nil {
return err
}
}
cCustKey := keyrng.n()
nk := rng.randomValue(0, 24)
if _, err := stmt.Exec(
cCustKey,
fmt.Sprintf("Customer#%09d", cCustKey),
rng.vString(10, 40),
nk,
rng.phoneNumber(int(nk)),
rng.randomValue(-99999, 999999),
rng.segments(),
rng.textString(29, 116),
); err != nil {
return err
}
// For each row in the CUSTOMER table, ten rows in the ORDERS
// table with: O_ORDERKEY unique within [SF * 1,500,000 * 4].
//
// Comment: The ORDERS and LINEITEM tables are sparsely
// populated by generating a key value that causes the first 8
// keys of each 32 to be populated, yielding a 25% use of the
// key range. Test sponsors must not take advantage of this
// aspect of the benchmark. For example, horizontally
// partitioning the test database onto different devices in
// order to place unused areas onto separate peripherals is
// prohibited.
//
// O_CUSTKEY = random value [1 .. (SF * 150,000)].
// The generation of this random value must be such that
// O_CUSTKEY modulo 3 is not zero.
//
// Comment: Orders are not present for all customers. Every
// third customer (in C_CUSTKEY order) is not assigned any
// order.
//
// O_ORDERSTATUS set to the following value:
// "F" if all lineitems of this order have L_LINESTATUS set to "F".
// "O" if all lineitems of this order have L_LINESTATUS set to "O".
// "P" otherwise.
// O_TOTALPRICE computed as:
// sum (L_EXTENDEDPRICE * (1+L_TAX) * (1-L_DISCOUNT)) for all LINEITEM of this order.
// O_ORDERDATE uniformly distributed between STARTDATE and (ENDDATE - 151 days).
// O_ORDERPRIORITY random string [Priorities].
// O_CLERK text appended with minimum 9 digits with leading zeros ["Clerk#", C] where C = random value [000000001 .. (SF * 1000)].
// O_SHIPPRIORITY set to 0.
// O_COMMENT text string [19,78].
for i := 0; i < 10; i++ {
var oCustKey int64
for {
oCustKey = rng.randomValue(1, int64(sf)*int64(recs))
if oCustKey%3 != 0 {
break
}
}
oOrderKey := keyrng2.n() - 1 // Zero base.
oOrderKey = oOrderKey/8*32 + oOrderKey%8 + 1 // 1 based, sparseness as specified above.
oOrderDate := rng.randomValue(minDate, maxDate) // unix ns
oOrderStatus := "X"
var oTotalPrice int64
{
// For each row in the ORDERS table, a random number of rows within [1 .. 7] in the LINEITEM table with:
// L_ORDERKEY = O_ORDERKEY.
// L_PARTKEY random value [1 .. (SF * 200,000)].
// L_SUPPKEY = (L_PARTKEY + (i * (( S/4 ) + (int)(L_partkey-1 )/S)))) modulo S + 1
// where i is the corresponding supplier within [0 .. 3] and S = SF * 10,000.
// L_LINENUMBER unique within [7].
// L_QUANTITY random value [1 .. 50].
// L_EXTENDEDPRICE = L_QUANTITY * P_RETAILPRICE
// Where P_RETAILPRICE is from the part with P_PARTKEY = L_PARTKEY.
// L_DISCOUNT random value [0.00 .. 0.10].
// L_TAX random value [0.00 .. 0.08].
// L_RETURNFLAG set to a value selected as follows:
// If L_RECEIPTDATE <= CURRENTDATE
// then either "R" or "A" is selected at random
// else "N" is selected.
// L_LINESTATUS set the following value:
// "O" if L_SHIPDATE > CURRENTDATE
// "F" otherwise.
// L_SHIPDATE = O_ORDERDATE + random value [1 .. 121].
// L_COMMITDATE = O_ORDERDATE + random value [30 .. 90].
// L_RECEIPTDATE = L_SHIPDATE + random value [1 .. 30].
// L_SHIPINSTRUCT random string [Instructions].
// L_SHIPMODE random string [Modes].
// L_COMMENT text string [10,43].
n := int(rng.randomValue(1, 7))