forked from johnkerl/miller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reshape.go
458 lines (402 loc) · 14.8 KB
/
reshape.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
package transformers
// ================================================================
// WIDE:
// time X Y Z
// 1 2009-01-01 0.65473572 2.4520609 -1.46570942
// 2 2009-01-02 -0.89248112 0.2154713 -2.05357735
// 3 2009-01-03 0.98012375 1.3179287 4.64248357
// 4 2009-01-04 0.35397376 3.3765645 -0.25237774
// 5 2009-01-05 2.19357813 1.3477511 0.09719105
// LONG:
// time item price
// 1 2009-01-01 X 0.65473572
// 2 2009-01-02 X -0.89248112
// 3 2009-01-03 X 0.98012375
// 4 2009-01-04 X 0.35397376
// 5 2009-01-05 X 2.19357813
// 6 2009-01-01 Y 2.45206093
// 7 2009-01-02 Y 0.21547134
// 8 2009-01-03 Y 1.31792866
// 9 2009-01-04 Y 3.37656453
// 10 2009-01-05 Y 1.34775108
// 11 2009-01-01 Z -1.46570942
// 12 2009-01-02 Z -2.05357735
// 13 2009-01-03 Z 4.64248357
// 14 2009-01-04 Z -0.25237774
// 15 2009-01-05 Z 0.09719105
import (
"container/list"
"fmt"
"os"
"regexp"
"strings"
"github.com/johnkerl/miller/internal/pkg/cli"
"github.com/johnkerl/miller/internal/pkg/lib"
"github.com/johnkerl/miller/internal/pkg/mlrval"
"github.com/johnkerl/miller/internal/pkg/types"
)
// ----------------------------------------------------------------
const verbNameReshape = "reshape"
var ReshapeSetup = TransformerSetup{
Verb: verbNameReshape,
UsageFunc: transformerReshapeUsage,
ParseCLIFunc: transformerReshapeParseCLI,
IgnoresInput: false,
}
func transformerReshapeUsage(
o *os.File,
) {
argv0 := "mlr"
verb := verbNameReshape
fmt.Fprintf(o, "Usage: %s %s [options]\n", argv0, verb)
fmt.Fprintf(o, "Wide-to-long options:\n")
fmt.Fprintf(o, " -i {input field names} -o {key-field name,value-field name}\n")
fmt.Fprintf(o, " -r {input field regex} -o {key-field name,value-field name}\n")
fmt.Fprintf(o, " These pivot/reshape the input data such that the input fields are removed\n")
fmt.Fprintf(o, " and separate records are emitted for each key/value pair.\n")
fmt.Fprintf(o, " Note: if you have multiple regexes, please specify them using multiple -r,\n")
fmt.Fprintf(o, " since regexes can contain commas within them.\n")
fmt.Fprintf(o, " Note: this works with tail -f and produces output records for each input\n")
fmt.Fprintf(o, " record seen.\n")
fmt.Fprintf(o, "Long-to-wide options:\n")
fmt.Fprintf(o, " -s {key-field name,value-field name}\n")
fmt.Fprintf(o, " These pivot/reshape the input data to undo the wide-to-long operation.\n")
fmt.Fprintf(o, " Note: this does not work with tail -f; it produces output records only after\n")
fmt.Fprintf(o, " all input records have been read.\n")
fmt.Fprintf(o, "\n")
fmt.Fprintf(o, "Examples:\n")
fmt.Fprintf(o, "\n")
fmt.Fprintf(o, " Input file \"wide.txt\":\n")
fmt.Fprintf(o, " time X Y\n")
fmt.Fprintf(o, " 2009-01-01 0.65473572 2.4520609\n")
fmt.Fprintf(o, " 2009-01-02 -0.89248112 0.2154713\n")
fmt.Fprintf(o, " 2009-01-03 0.98012375 1.3179287\n")
fmt.Fprintf(o, "\n")
fmt.Fprintf(o, " %s --pprint %s -i X,Y -o item,value wide.txt\n", argv0, verb)
fmt.Fprintf(o, " time item value\n")
fmt.Fprintf(o, " 2009-01-01 X 0.65473572\n")
fmt.Fprintf(o, " 2009-01-01 Y 2.4520609\n")
fmt.Fprintf(o, " 2009-01-02 X -0.89248112\n")
fmt.Fprintf(o, " 2009-01-02 Y 0.2154713\n")
fmt.Fprintf(o, " 2009-01-03 X 0.98012375\n")
fmt.Fprintf(o, " 2009-01-03 Y 1.3179287\n")
fmt.Fprintf(o, "\n")
fmt.Fprintf(o, " %s --pprint %s -r '[A-Z]' -o item,value wide.txt\n", argv0, verb)
fmt.Fprintf(o, " time item value\n")
fmt.Fprintf(o, " 2009-01-01 X 0.65473572\n")
fmt.Fprintf(o, " 2009-01-01 Y 2.4520609\n")
fmt.Fprintf(o, " 2009-01-02 X -0.89248112\n")
fmt.Fprintf(o, " 2009-01-02 Y 0.2154713\n")
fmt.Fprintf(o, " 2009-01-03 X 0.98012375\n")
fmt.Fprintf(o, " 2009-01-03 Y 1.3179287\n")
fmt.Fprintf(o, "\n")
fmt.Fprintf(o, " Input file \"long.txt\":\n")
fmt.Fprintf(o, " time item value\n")
fmt.Fprintf(o, " 2009-01-01 X 0.65473572\n")
fmt.Fprintf(o, " 2009-01-01 Y 2.4520609\n")
fmt.Fprintf(o, " 2009-01-02 X -0.89248112\n")
fmt.Fprintf(o, " 2009-01-02 Y 0.2154713\n")
fmt.Fprintf(o, " 2009-01-03 X 0.98012375\n")
fmt.Fprintf(o, " 2009-01-03 Y 1.3179287\n")
fmt.Fprintf(o, "\n")
fmt.Fprintf(o, " %s --pprint %s -s item,value long.txt\n", argv0, verb)
fmt.Fprintf(o, " time X Y\n")
fmt.Fprintf(o, " 2009-01-01 0.65473572 2.4520609\n")
fmt.Fprintf(o, " 2009-01-02 -0.89248112 0.2154713\n")
fmt.Fprintf(o, " 2009-01-03 0.98012375 1.3179287\n")
fmt.Fprintf(o, "See also %s nest.\n", argv0)
}
func transformerReshapeParseCLI(
pargi *int,
argc int,
args []string,
_ *cli.TOptions,
doConstruct bool, // false for first pass of CLI-parse, true for second pass
) IRecordTransformer {
// Skip the verb name from the current spot in the mlr command line
argi := *pargi
verb := args[argi]
argi++
// Parse local flags
var inputFieldNames []string = nil
var inputFieldRegexStrings []string = nil
var outputFieldNames []string = nil
var splitOutFieldNames []string = nil
for argi < argc /* variable increment: 1 or 2 depending on flag */ {
opt := args[argi]
if !strings.HasPrefix(opt, "-") {
break // No more flag options to process
}
if args[argi] == "--" {
break // All transformers must do this so main-flags can follow verb-flags
}
argi++
if opt == "-h" || opt == "--help" {
transformerReshapeUsage(os.Stdout)
os.Exit(0)
} else if opt == "-i" {
inputFieldNames = cli.VerbGetStringArrayArgOrDie(verb, opt, args, &argi, argc)
} else if opt == "-r" {
inputFieldRegexString := cli.VerbGetStringArgOrDie(verb, opt, args, &argi, argc)
if inputFieldRegexStrings == nil {
inputFieldRegexStrings = make([]string, 0)
}
inputFieldRegexStrings = append(inputFieldRegexStrings, inputFieldRegexString)
} else if opt == "-o" {
outputFieldNames = cli.VerbGetStringArrayArgOrDie(verb, opt, args, &argi, argc)
} else if opt == "-s" {
splitOutFieldNames = cli.VerbGetStringArrayArgOrDie(verb, opt, args, &argi, argc)
} else {
transformerReshapeUsage(os.Stderr)
os.Exit(1)
}
}
outputKeyFieldName := ""
outputValueFieldName := ""
splitOutKeyFieldName := ""
splitOutValueFieldName := ""
if splitOutFieldNames == nil {
// wide to long
if inputFieldNames == nil && inputFieldRegexStrings == nil {
transformerReshapeUsage(os.Stderr)
os.Exit(1)
}
if outputFieldNames == nil {
transformerReshapeUsage(os.Stderr)
os.Exit(1)
}
if len(outputFieldNames) != 2 {
transformerReshapeUsage(os.Stderr)
os.Exit(1)
}
outputKeyFieldName = outputFieldNames[0]
outputValueFieldName = outputFieldNames[1]
} else {
// long to wide
if len(splitOutFieldNames) != 2 {
transformerReshapeUsage(os.Stderr)
os.Exit(1)
}
splitOutKeyFieldName = splitOutFieldNames[0]
splitOutValueFieldName = splitOutFieldNames[1]
}
*pargi = argi
if !doConstruct { // All transformers must do this for main command-line parsing
return nil
}
transformer, err := NewTransformerReshape(
inputFieldNames,
inputFieldRegexStrings,
outputKeyFieldName,
outputValueFieldName,
splitOutKeyFieldName,
splitOutValueFieldName,
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return transformer
}
// ----------------------------------------------------------------
type TransformerReshape struct {
// for wide-to-long:
inputFieldNames []string
inputFieldRegexes []*regexp.Regexp
outputKeyFieldName string
outputValueFieldName string
// for long-to-wide:
splitOutKeyFieldName string
splitOutValueFieldName string
otherKeysToOtherValuesToBuckets *lib.OrderedMap
recordTransformerFunc RecordTransformerFunc
}
// ----------------------------------------------------------------
func NewTransformerReshape(
inputFieldNames []string,
inputFieldRegexStrings []string,
outputKeyFieldName string,
outputValueFieldName string,
splitOutKeyFieldName string,
splitOutValueFieldName string,
) (*TransformerReshape, error) {
tr := &TransformerReshape{
inputFieldNames: inputFieldNames,
outputKeyFieldName: outputKeyFieldName,
outputValueFieldName: outputValueFieldName,
splitOutKeyFieldName: splitOutKeyFieldName,
splitOutValueFieldName: splitOutValueFieldName,
otherKeysToOtherValuesToBuckets: lib.NewOrderedMap(),
}
if inputFieldRegexStrings != nil {
tr.inputFieldRegexes = make([]*regexp.Regexp, len(inputFieldRegexStrings))
// TODO: make a library function for string-array to regex-array
for i, inputFieldRegexString := range inputFieldRegexStrings {
regex, err := lib.CompileMillerRegex(inputFieldRegexString)
if err != nil {
fmt.Fprintf(
os.Stderr,
"%s %s: cannot compile regex [%s]\n",
"mlr", verbNameReshape, inputFieldRegexString,
)
os.Exit(1)
}
tr.inputFieldRegexes[i] = regex
}
}
if splitOutKeyFieldName == "" {
if tr.inputFieldRegexes == nil {
tr.recordTransformerFunc = tr.wideToLongNoRegex
} else {
tr.recordTransformerFunc = tr.wideToLongRegex
}
} else {
tr.recordTransformerFunc = tr.longToWide
}
return tr, nil
}
// ----------------------------------------------------------------
func (tr *TransformerReshape) Transform(
inrecAndContext *types.RecordAndContext,
outputRecordsAndContexts *list.List, // list of *types.RecordAndContext
inputDownstreamDoneChannel <-chan bool,
outputDownstreamDoneChannel chan<- bool,
) {
HandleDefaultDownstreamDone(inputDownstreamDoneChannel, outputDownstreamDoneChannel)
tr.recordTransformerFunc(inrecAndContext, outputRecordsAndContexts, inputDownstreamDoneChannel, outputDownstreamDoneChannel)
}
// ----------------------------------------------------------------
func (tr *TransformerReshape) wideToLongNoRegex(
inrecAndContext *types.RecordAndContext,
outputRecordsAndContexts *list.List, // list of *types.RecordAndContext
inputDownstreamDoneChannel <-chan bool,
outputDownstreamDoneChannel chan<- bool,
) {
if !inrecAndContext.EndOfStream {
inrec := inrecAndContext.Record
pairs := mlrval.NewMlrmap()
for _, inputFieldName := range tr.inputFieldNames {
value := inrec.Get(inputFieldName)
if value != nil {
// Reference, not copy, since the inrec will be freed here, or everything else will
pairs.PutReference(inputFieldName, value)
}
}
// Unset the record keys after iterating over them, rather than during
for pe := pairs.Head; pe != nil; pe = pe.Next {
inrec.Remove(pe.Key)
}
if pairs.IsEmpty() {
outputRecordsAndContexts.PushBack(inrecAndContext)
} else {
for pf := pairs.Head; pf != nil; pf = pf.Next {
outrec := inrec.Copy()
outrec.PutReference(tr.outputKeyFieldName, mlrval.FromString(pf.Key))
outrec.PutReference(tr.outputValueFieldName, pf.Value)
outputRecordsAndContexts.PushBack(types.NewRecordAndContext(outrec, &inrecAndContext.Context))
}
}
} else {
outputRecordsAndContexts.PushBack(inrecAndContext) // emit end-of-stream marker
}
}
// ----------------------------------------------------------------
func (tr *TransformerReshape) wideToLongRegex(
inrecAndContext *types.RecordAndContext,
outputRecordsAndContexts *list.List, // list of *types.RecordAndContext
inputDownstreamDoneChannel <-chan bool,
outputDownstreamDoneChannel chan<- bool,
) {
if !inrecAndContext.EndOfStream {
inrec := inrecAndContext.Record
pairs := mlrval.NewMlrmap()
for pd := inrec.Head; pd != nil; pd = pd.Next {
for _, inputFieldRegex := range tr.inputFieldRegexes {
if inputFieldRegex.MatchString(pd.Key) {
// Reference, not copy, since the inrec will be freed here, or everything else will
pairs.PutReference(pd.Key, pd.Value)
break
}
}
}
// Unset the record keys after iterating over them, rather than during
for pe := pairs.Head; pe != nil; pe = pe.Next {
inrec.Remove(pe.Key)
}
if pairs.IsEmpty() {
outputRecordsAndContexts.PushBack(inrecAndContext)
} else {
for pf := pairs.Head; pf != nil; pf = pf.Next {
outrec := inrec.Copy()
outrec.PutReference(tr.outputKeyFieldName, mlrval.FromString(pf.Key))
outrec.PutReference(tr.outputValueFieldName, pf.Value)
outputRecordsAndContexts.PushBack(types.NewRecordAndContext(outrec, &inrecAndContext.Context))
}
}
} else {
outputRecordsAndContexts.PushBack(inrecAndContext) // emit end-of-stream marker
}
}
// ----------------------------------------------------------------
func (tr *TransformerReshape) longToWide(
inrecAndContext *types.RecordAndContext,
outputRecordsAndContexts *list.List, // list of *types.RecordAndContext
inputDownstreamDoneChannel <-chan bool,
outputDownstreamDoneChannel chan<- bool,
) {
if !inrecAndContext.EndOfStream {
inrec := inrecAndContext.Record
splitOutKeyFieldValue := inrec.Get(tr.splitOutKeyFieldName)
splitOutValueFieldValue := inrec.Get(tr.splitOutValueFieldName)
if splitOutKeyFieldValue == nil || splitOutValueFieldValue == nil {
outputRecordsAndContexts.PushBack(inrecAndContext)
return
}
inrec.Remove(tr.splitOutKeyFieldName)
inrec.Remove(tr.splitOutValueFieldName)
// Don't unset tr.fieldName in the record, so we can implode in-place at the end.
otherKeysJoined := inrec.GetKeysJoined()
var otherValuesToBuckets *lib.OrderedMap = nil
iOtherValuesToBuckets := tr.otherKeysToOtherValuesToBuckets.Get(otherKeysJoined)
if iOtherValuesToBuckets == nil {
otherValuesToBuckets = lib.NewOrderedMap()
tr.otherKeysToOtherValuesToBuckets.Put(otherKeysJoined, otherValuesToBuckets)
} else {
otherValuesToBuckets = iOtherValuesToBuckets.(*lib.OrderedMap)
}
otherValuesJoined := inrec.GetValuesJoined()
var bucket *tReshapeBucket = nil
iBucket := otherValuesToBuckets.Get(otherValuesJoined)
if iBucket == nil {
bucket = newReshapeBucket(inrec)
otherValuesToBuckets.Put(otherValuesJoined, bucket)
} else {
bucket = iBucket.(*tReshapeBucket)
}
bucket.pairs.PutCopy(splitOutKeyFieldValue.String(), splitOutValueFieldValue)
} else {
for pe := tr.otherKeysToOtherValuesToBuckets.Head; pe != nil; pe = pe.Next {
otherValuesToBuckets := pe.Value.(*lib.OrderedMap)
for pf := otherValuesToBuckets.Head; pf != nil; pf = pf.Next {
bucket := pf.Value.(*tReshapeBucket)
outrec := bucket.representative
bucket.representative = nil // ownership transfer
for pg := bucket.pairs.Head; pg != nil; pg = pg.Next {
outrec.PutReference(pg.Key, pg.Value)
}
outputRecordsAndContexts.PushBack(types.NewRecordAndContext(outrec, &inrecAndContext.Context))
}
}
outputRecordsAndContexts.PushBack(inrecAndContext) // emit end-of-stream marker
}
}
type tReshapeBucket struct {
representative *mlrval.Mlrmap
pairs *mlrval.Mlrmap
}
func newReshapeBucket(representative *mlrval.Mlrmap) *tReshapeBucket {
return &tReshapeBucket{
representative: representative,
pairs: mlrval.NewMlrmap(),
}
}