/
main.go
536 lines (459 loc) · 10.7 KB
/
main.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
package main
import (
"encoding/csv"
"errors"
"fmt"
"io"
"log"
"math"
"math/rand"
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"time"
goccy_json "github.com/goccy/go-json"
"github.com/jaswdr/faker"
jsonutil "github.com/multiprocessio/go-json"
"github.com/xitongsys/parquet-go/writer"
"github.com/xuri/excelize/v2"
)
var preferredParallelism = runtime.NumCPU() * 2
// faker.Lorem().Word() only seems to have like 180 unique entries.
// So we brought in our own dictionary so that the words are unique
// but not just completely random collections of letters which are not
// realistic in the real world and destroy sorting algorithms
// unfairly.
func randomWord() string {
return WORDS[rand.Intn(len(WORDS))]
}
var (
yearsAhead50 = time.Now().Add(time.Hour * 8760 * 50)
yearsAgo50 = time.Now().Add(-1 * time.Hour * 8760 * 50)
)
func jsonEncode(out io.Writer, generator chan map[string]any, sep string) error {
isArray := sep == ",\n"
if isArray {
encoder := jsonutil.NewGenericStreamEncoder(out, goccy_json.Marshal, isArray)
defer encoder.Close()
for row := range generator {
err := encoder.EncodeRow(row)
if err != nil {
return err
}
}
return nil
}
w := goccy_json.NewEncoder(out)
first := false
for row := range generator {
if !first {
_, err := out.Write([]byte(sep))
if err != nil {
return err
}
}
err := w.Encode(row)
if err != nil {
return err
}
}
return nil
}
func csvEncode(out io.Writer, generator chan map[string]any, sep rune) error {
w := csv.NewWriter(out)
defer w.Flush()
w.Comma = sep
first := true
var columns []string
var rowStrings []string
for row := range generator {
if first {
for col := range row {
columns = append(columns, col)
rowStrings = append(rowStrings, "")
}
sort.Strings(columns)
err := w.Write(columns)
if err != nil {
return nil
}
first = false
}
for i, col := range columns {
s := ""
switch t := row[col].(type) {
case time.Time:
s = t.Format(time.RFC3339)
default:
s = fmt.Sprintf("%v", t)
}
rowStrings[i] = s
}
err := w.Write(rowStrings)
if err != nil {
return err
}
}
return nil
}
var errOutMustBeSet = errors.New("--out X, -o X output file setting must be set when using this format")
func excelEncode(out string, generator chan map[string]any) error {
if out == "" {
return errOutMustBeSet
}
xlsx := excelize.NewFile()
streamWriter, err := xlsx.NewStreamWriter("Sheet1")
if err != nil {
return err
}
i := 0
var columns []string
var rowValues []any
for row := range generator {
i++
cell, err := excelize.CoordinatesToCellName(1, i)
if err != nil {
return err
}
if i == 1 {
for col := range row {
columns = append(columns, col)
rowValues = append(rowValues, "")
}
sort.Strings(columns)
// Excelize requires an array of any
var colsInt []any
for _, col := range columns {
colsInt = append(colsInt, col)
}
err = streamWriter.SetRow(cell, colsInt)
if err != nil {
return nil
}
// Increment cell for first non-header row
i++
cell, err = excelize.CoordinatesToCellName(1, i)
if err != nil {
return err
}
}
for i, col := range columns {
s := ""
switch t := row[col].(type) {
case time.Time:
s = t.Format(time.RFC3339)
default:
s = fmt.Sprintf("%v", t)
}
rowValues[i] = s
}
err = streamWriter.SetRow(cell, rowValues)
if err != nil {
return err
}
}
err = streamWriter.Flush()
if err != nil {
return err
}
return xlsx.SaveAs(out)
}
func orcEncode(out io.Writer, generator chan map[string]any) error {
return errors.New("ORC unimplemented")
}
func parquetEncode(out io.Writer, schema map[string]columnKind, generator chan map[string]any) error {
var columns []string
for col := range schema {
columns = append(columns, col)
}
sort.Strings(columns)
parquetSchema := "{\"Tag\": \"name=root\", \"Fields\": [\n"
for i, col := range columns {
if i > 0 {
parquetSchema += ",\n"
}
kind := schema[col]
parquetInfo := map[columnKind]string{
boolColumn: "type=BOOLEAN",
stringColumn: "type=BYTE_ARRAY, convertedtype=UTF8",
intColumn: "type=INT64",
floatColumn: "type=FLOAT",
timeColumn: "type=INT64, convertedtype=TIMESTAMP_MILLIS",
}[kind]
col = strings.ReplaceAll(col, "-", "")
parquetSchema += fmt.Sprintf(`{"Tag": "name=%s, %s"}`, col, parquetInfo)
}
parquetSchema += "\n]}"
w, err := writer.NewParquetWriterFromWriter(out, parquetSchema, int64(preferredParallelism))
if err != nil {
return err
}
for row := range generator {
for col, kind := range schema {
if kind == timeColumn {
t := row[col].(time.Time)
row[col] = t.UnixNano() / int64(time.Millisecond)
}
}
err := w.Write(row)
if err != nil {
return nil
}
}
return w.WriteStop()
}
func odsEncode(out io.Writer, generator chan map[string]any) error {
return errors.New("ods unimplemented")
}
type columnKind = uint
const (
boolColumn columnKind = iota
stringColumn
intColumn
floatColumn
timeColumn
)
type columnSchema struct {
generator func() any
kind columnKind
}
var nullFrequency = .01
func timeForNull() bool {
// Don't trust float equality
if nullFrequency < .00001 {
return false
}
times := 1 / nullFrequency * 1000
r := rand.Intn(int(math.Ceil(times)))
n := times - times*nullFrequency
return float64(r) > n
}
func makeGenerator(rows, cols int) (map[string]columnKind, chan map[string]any) {
faker := faker.New()
// Populate all the column names
schema := map[string]func() any{}
types := []columnSchema{
{
func() any {
return faker.Lorem().Paragraph(rand.Intn(3) + 1)
},
stringColumn,
},
{
func() any {
return int(faker.Int32())
},
intColumn,
},
{
func() any {
return faker.Float32(6, -100000, 100000)
},
floatColumn,
},
{
func() any {
return faker.Bool()
},
boolColumn,
},
{
func() any {
return faker.Time().TimeBetween(yearsAgo50, yearsAhead50)
},
timeColumn,
},
{
func() any {
return strings.Join(faker.Lorem().Words(rand.Intn(10)+1), " ")
},
stringColumn,
},
{
func() any {
return int(faker.UInt16())
},
intColumn,
},
{
func() any {
return randomWord()
},
stringColumn,
},
}
failures := 0
schemaTypes := map[string]columnKind{}
for len(schema) < cols {
// TODO: handle when more than 370k columns
// requested. Should probably try to combine words
// instead of reusing them or failing here.
column := randomWord()
_, exists := schema[column]
if exists {
failures += 1
if failures > len(WORDS)/10 {
log.Fatal("Running out of unique entries.")
}
continue
}
columnSchema := types[rand.Intn(len(types))]
schema[column] = columnSchema.generator
schemaTypes[column] = columnSchema.kind
}
out := make(chan map[string]any)
go func() {
defer close(out)
for i := 0; i < rows; i += 1 {
row := map[string]any{}
for col, generator := range schema {
if timeForNull() {
row[col] = nil
} else {
row[col] = generator()
}
}
out <- row
}
}()
return schemaTypes, out
}
func formatOrFromFileExtension(format, out string, formatDefault bool) string {
if out == "" {
return format
}
if !formatDefault {
return format
}
ext := filepath.Ext(out)
if ext[0] == '.' {
ext = ext[1:]
}
return ext
}
var Version = "latest"
var HELP = `fakegen (Version ` + Version + `) - Single binary CLI for generating a random schema of M columns to populate N rows of data
Usage: fakegen --rows N --cols M > testdata.json
fakegen --rows N --cols M --out testdata.json
fakegen --rows N --cols M --format csv > testdata.csv
Flags:
-h, --help Dump help text and exit
-v, --version Dump version and exit
-o, --out Specify file to write to. If empty, writes to stdout
-f, --format Specify output format. Inferred by --out flag if present and --format flag empty
-r, --rows Number of rows to generate
-c, --cols Number of columns to generate
Supported formats:
json Array of JSON objects
jsonl Newline separated JSON objects
cjson Concatenated JSON objects
xlsx
csv
tsv
See the repo for more details: https://github.com/multiprocessio/fakegen.`
func _main() error {
log.SetFlags(0)
rand.Seed(time.Now().UnixNano())
var rows, cols int
format := "json"
formatDefault := true
out := ""
for i, arg := range os.Args {
var err error
if arg == "--rows" || arg == "-r" {
rows, err = strconv.Atoi(os.Args[i+1])
if err != nil {
return errors.New("--rows must be an int. e.g. --rows 10000: " + err.Error())
}
i++
continue
}
if arg == "--cols" || arg == "-c" {
cols, err = strconv.Atoi(os.Args[i+1])
if err != nil {
return errors.New("--cols must be an int. e.g. --cols 10000: " + err.Error())
}
i++
continue
}
if arg == "-h" || arg == "--help" {
log.Println(HELP)
return nil
}
if arg == "-v" || arg == "--version" {
log.Println("fakegen " + Version)
return nil
}
if arg == "-f" || arg == "--format" {
format = os.Args[i+1]
formatDefault = false
i++
continue
}
if arg == "-o" || arg == "--out" {
out = os.Args[i+1]
i++
continue
}
if arg == "-n" || arg == "--null-frequency" {
nullFrequency, err = strconv.ParseFloat(os.Args[i+1], 32)
if err != nil {
return errors.New("-n, --null-frequency must be a float. e.g --null-frequency .001: " + err.Error())
}
i++
continue
}
}
if rows < 1 {
return errors.New("--rows must be at least 1")
}
if cols < 1 {
return errors.New("--cols must be at least 1")
}
schema, generator := makeGenerator(rows, cols)
format = formatOrFromFileExtension(format, out, formatDefault)
w := os.Stdout
if out != "" {
// Truncates the file if it exists, creates directory if it doesn't exist
base := filepath.Dir(out)
err := os.Mkdir(base, os.ModePerm)
// Other comparisons like os.IsNotExist(err) and errors.Is(err, os.ErrNotExist) don't work.
if err != nil && !strings.HasSuffix(err.Error(), ": file exists") {
return err
}
w, err = os.OpenFile(out, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer w.Close()
}
switch format {
case "json":
return jsonEncode(w, generator, ",\n")
case "jsonl":
return jsonEncode(w, generator, "")
case "csv":
return csvEncode(w, generator, rune(','))
case "tsv":
return csvEncode(w, generator, rune('\t'))
case "parquet":
return parquetEncode(w, schema, generator)
case "orc":
return orcEncode(w, generator)
case "xlsx":
w.Close()
return excelEncode(out, generator)
case "ods":
return odsEncode(w, generator)
}
return fmt.Errorf("Unimplemented format: " + format)
}
func main() {
err := _main()
if err != nil {
log.Fatal(err)
}
}