/
merge.go
510 lines (455 loc) · 15.3 KB
/
merge.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
package gotables
/*
Copyright (c) 2017 Malcolm Gorman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import (
"fmt"
"math"
"reflect"
)
/*
Merge table1 and table2 and return merged (table) of table1 and table2 columns and rows.
table1 and table2 must share compatible sort key columns: same names and types.
Sort keys for the key columns must be set in table1 (or table2): table1.SetSortKeys() and optionally table1.SetSortKeysReverse()
For each matching row (or rows -- duplicates will be removed) in each table the non-key cells will be merged
using the following rules:
There are 4 possibilities (based on whether the cells have zero values -- soft-null):
-----------------------------------------------------------------------------------------------
Combination | table1.cell | table2.cell | Action | Remarks
-----------------------------------------------------------------------------------------------
(a) | zero == zero | do nothing
(b) | zero <- non-zero | copy cell2 to cell1 | Assumes zero is a missing value
(c) | non-zero -> zero | copy cell1 to cell2 | Assumes zero is a missing value
(d) | non-zero -> non-zero | copy cell1 to cell2 | (table1 takes precedence)
There are 2 further possibilities with float32 and float64 (based on NaN values -- hard-null):
-----------------------------------------------------------------------------------------------
Combination | table1.cell | table2.cell | Action
-----------------------------------------------------------------------------------------------
(e) | zero -> NaN | copy cell1 to cell2 | Assumes zero is NOT a missing value
(f) | NaN <- zero | copy cell2 to cell1 | Assumes zero is NOT a missing value
-----------------------------------------------------------------------------------------------
*/
func (table1 *Table) Merge(table2 *Table) (merged *Table, err error) {
// Local function.
// Make sort keys of both input tables the same.
setSortKeysBetweenTables := func() error {
if table1.SortKeyCount() > 0 {
// Note: table1 is dominant.
err = table2.SetSortKeysFromTable(table1)
if err != nil {
return err
}
} else if table2.SortKeyCount() > 0 {
err = table1.SetSortKeysFromTable(table2)
if err != nil {
return err
}
} else {
err = fmt.Errorf("[%s].Merge([%s]) needs at least table [%s] or table [%s] to have sort keys",
table1.Name(), table2.Name(), table1.Name(), table2.Name())
return err
}
return nil
}
// Local function.
sortMerged := func(localMerged *Table) (*Table, error) {
if localMerged.SortKeyCount() == 0 {
err = setSortKeysBetweenTables()
if err != nil {
return nil, err
}
}
err = localMerged.Sort()
if err != nil {
return nil, err
}
return localMerged, nil
}
if table1 == nil {
err = fmt.Errorf("func (table1 *Table) %s(table2 *Table): table1 is <nil>", UtilFuncName())
return merged, err
}
if table2 == nil {
err = fmt.Errorf("func (table1 *Table) %s(table2 *Table): table2 is <nil>", UtilFuncName())
return merged, err
}
// TODO: this value of merged is never used (SA4006)
if table1.RowCount() == 0 {
merged, err = sortMerged(table2)
if err != nil {
return nil, err
}
return table2, nil
}
// TODO: this value of merged is never used (SA4006)
if table2.RowCount() == 0 {
merged, err = sortMerged(table1)
if err != nil {
return nil, err
}
return table1, nil
}
// Check that table1 and table2 have the same sort columns.
err = setSortKeysBetweenTables()
if err != nil {
return nil, err
}
// Okay. They're compatible, now set up for merging.
merged, err = NewTable("Merged")
if err != nil {
return nil, err
}
// Add all columns (but not yet rows) from table1 and table2 into merged.
err = merged.AppendColsFromTable(table1)
if err != nil {
return nil, err
}
// fmt.Println(merged)
err = merged.AppendColsFromTable(table2)
if err != nil {
return nil, err
}
// fmt.Println(merged)
err = merged.SetSortKeysFromTable(table1)
if err != nil {
return nil, err
}
// This is purely aesthetic for human readability.
err = merged.OrderColsBySortKeys()
if err != nil {
return nil, err
}
var tempColCount int // To ignore temporary columns.
// Add a column to keep track of which columns came from which table.
const tableNumberColName = "_TABLE_"
err = merged.AppendCol(tableNumberColName, "int")
if err != nil {
return nil, err
}
tempColCount++
// Make space (enough rows) for both tables.
err = merged.AppendRows(table1.RowCount() + table2.RowCount())
if err != nil {
return nil, err
}
// Set floats to NaN values, their default value if not set.
err = merged.SetAllFloatCellsToNaN()
if err != nil {
return nil, err
}
// where(fmt.Sprintf("merged = %s\n", merged))
// Copy table1 into merged.
var beginRow = 0
err = table1.copyTableCells(beginRow, merged)
if err != nil {
return nil, err
}
// Set table number in merged of table1.
for rowIndex := beginRow; rowIndex < table1.RowCount(); rowIndex++ {
err = merged.SetInt(tableNumberColName, rowIndex, 1)
if err != nil {
return nil, err
}
}
// Copy table2 into merged.
beginRow = table1.RowCount()
err = table2.copyTableCells(beginRow, merged)
if err != nil {
return nil, err
}
// Set table number in merged of table2.
for rowIndex := beginRow; rowIndex < merged.RowCount(); rowIndex++ {
err = merged.SetInt(tableNumberColName, rowIndex, 2)
if err != nil {
return nil, err
}
}
// We want the table sorted with the matching (by keys) rows from each table contiguous.
// Use tableNumberColName in sort to put table 1 row before matching table 2 row.
err = merged.AppendSortKey(tableNumberColName)
if err != nil {
return nil, err
}
err = merged.Sort()
if err != nil {
return nil, err
}
// Revert to previous sort keys.
err = merged.DeleteSortKey(tableNumberColName)
if err != nil {
return nil, err
}
// Add a column to keep track of which columns are duplicates, to be deleted later.
const deleteColName = "_DELETE_"
err = merged.AppendCol(deleteColName, "bool")
if err != nil {
return nil, err
}
tempColCount++
// where(fmt.Sprintf("BEFORE Merge()\n%s\n", merged))
// Loop through to the second-last row, comparing each row with the row after it.
for rowIndex := 0; rowIndex < merged.RowCount()-1; rowIndex++ {
// Reduce make it easier to reason about assignments of val1 and val2.
rowIndex1 := rowIndex
rowIndex2 := rowIndex + 1
var comparison int
comparison, err = merged.CompareRows(rowIndex, rowIndex+1)
if err != nil {
return nil, err
}
// where(fmt.Sprintf("[%s].CompareRows(%d, %d) = %d\n", merged.Name(), rowIndex, rowIndex+1, comparison))
if comparison == 0 {
// They are equal.
// Loop through columns, short of the last (temporary) column(s).
for colIndex := 0; colIndex < merged.ColCount()-tempColCount; colIndex++ {
var colType string
colType, err = merged.ColTypeByColIndex(colIndex)
if err != nil {
return nil, err
}
switch colType {
case "string":
var val1 string
var val2 string
const zeroVal = ""
val1, err = merged.GetStringByColIndex(colIndex, rowIndex)
if err != nil {
return nil, err
}
val2, err = merged.GetStringByColIndex(colIndex, rowIndex+1)
if err != nil {
return nil, err
}
if val1 != zeroVal { // Covers combinations (c) and (d)
err = merged.SetStringByColIndex(colIndex, rowIndex+1, val1) // Use val1
if err != nil {
return nil, err
}
} else if val2 != zeroVal { // Covers combination (b)
err = merged.SetStringByColIndex(colIndex, rowIndex, val2) // Use val2
if err != nil {
return nil, err
}
}
// Otherwise both vals must be zero. Do nothing.
case "bool":
var val1 bool
var val2 bool
const zeroVal = false
val1, err = merged.GetBoolByColIndex(colIndex, rowIndex)
if err != nil {
return nil, err
}
val2, err = merged.GetBoolByColIndex(colIndex, rowIndex+1)
if err != nil {
return nil, err
}
if val1 != zeroVal { // Covers combinations (c) and (d)
err = merged.SetBoolByColIndex(colIndex, rowIndex+1, val1) // Use val1
if err != nil {
return nil, err
}
} else if val2 != zeroVal { // Covers combination (b)
err = merged.SetBoolByColIndex(colIndex, rowIndex, val2) // Use val2
if err != nil {
return nil, err
}
}
// Otherwise both vals must be zero. Do nothing.
case "int8", "int16", "int32", "int64", "int":
var tmp1 interface{}
var tmp2 interface{}
var val1 int64
var val2 int64
const zeroVal = 0
tmp1, err = merged.GetValByColIndex(colIndex, rowIndex)
if err != nil {
return nil, err
}
val1 = reflect.ValueOf(tmp1).Int()
tmp2, err = merged.GetValByColIndex(colIndex, rowIndex+1)
if err != nil {
return nil, err
}
val2 = reflect.ValueOf(tmp2).Int()
if val1 != zeroVal { // Covers combinations (c) and (d)
err = merged.SetValByColIndex(colIndex, rowIndex+1, tmp1) // Use val1
if err != nil {
return nil, err
}
} else if val2 != zeroVal { // Covers combination (b)
err = merged.SetValByColIndex(colIndex, rowIndex, tmp2) // Use val2
if err != nil {
return nil, err
}
}
// Otherwise both vals must be zero. Do nothing.
case "uint8", "uint16", "uint32", "uint64", "uint":
var tmp1 interface{}
var tmp2 interface{}
var val1 uint64
var val2 uint64
const zeroVal = 0
tmp1, err = merged.GetValByColIndex(colIndex, rowIndex)
if err != nil {
return nil, err
}
val1 = reflect.ValueOf(tmp1).Uint()
tmp2, err = merged.GetValByColIndex(colIndex, rowIndex+1)
if err != nil {
return nil, err
}
val2 = reflect.ValueOf(tmp2).Uint()
if val1 != zeroVal { // Covers combinations (c) and (d)
err = merged.SetValByColIndex(colIndex, rowIndex+1, tmp1) // Use val1
if err != nil {
return nil, err
}
} else if val2 != zeroVal { // Covers combination (b)
err = merged.SetValByColIndex(colIndex, rowIndex, tmp2) // Use val2
if err != nil {
return nil, err
}
}
// Otherwise both vals must be zero. Do nothing.
case "float32", "float64":
// Note: NaN is more zero than zero, so zero value 0.0 trumps NaN.
var tmp1 interface{}
var tmp2 interface{}
var val1 float64
var val2 float64
const zeroVal = 0.0
tmp1, err = merged.GetValByColIndex(colIndex, rowIndex1)
if err != nil {
return nil, err
}
val1 = reflect.ValueOf(tmp1).Float()
tmp2, err = merged.GetValByColIndex(colIndex, rowIndex2)
if err != nil {
return nil, err
}
val2 = reflect.ValueOf(tmp2).Float()
if val1 != zeroVal && !math.IsNaN(val1) { // Covers combinations (c) and (d)
err = merged.SetValByColIndex(colIndex, rowIndex2, tmp1) // Use val1
if err != nil {
return nil, err
}
} else if val2 != zeroVal && !math.IsNaN(val2) { // Covers combination (b)
err = merged.SetValByColIndex(colIndex, rowIndex1, tmp2) // Use val2
if err != nil {
return nil, err
}
} else if math.IsNaN(val1) { // Maybe one of them is NaN and the other is zero.
err = merged.SetValByColIndex(colIndex, rowIndex1, tmp2) // Use val2
if err != nil {
return nil, err
}
} else if math.IsNaN(val2) { // Maybe one of them is NaN and the other is zero.
err = merged.SetValByColIndex(colIndex, rowIndex2, tmp1) // Use val1
if err != nil {
return nil, err
}
}
// Otherwise both vals must be zero. Do nothing.
default:
// Should never reach here.
var isValid bool
isValid, err = IsValidColType(colType)
if !isValid {
return nil, err
} else {
return nil, fmt.Errorf("what? We seem to have an unlisted type: %s", colType)
}
}
}
/*
Tag one of the (now identical) rows for deletion.
Choose the 1st row (rowIndex1) because rowIndex2 will be compared with the subsequent row.
Each row1 (of row1 and row2) will be marked for deletion after its values have been merged.
This performs a cascading/progressive copy of the least-zero value forward in the matching rows
so that the single remaining matching row contains the least-zero value.
E.g. float 4.4 will trump 0.0 will trump NaN.
*/
err = merged.SetBool(deleteColName, rowIndex1, true)
if err != nil {
return nil, err
}
}
}
// Sort by deleteColName ready to delete as a single block of rows.
err = merged.SetSortKeys(deleteColName)
if err != nil {
return nil, err
}
err = merged.Sort()
if err != nil {
return nil, err
}
// Find the block of rows to delete.
first, last, err := merged.SearchRange(true)
if err != nil {
return nil, err
}
// Delete the block of rows.
err = merged.DeleteRows(first, last)
if err != nil {
return nil, err
}
// Delete the temporary tag columns.
err = merged.DeleteCol(deleteColName)
if err != nil {
return nil, err
}
err = merged.DeleteCol(tableNumberColName)
if err != nil {
return nil, err
}
err = merged.SetSortKeysFromTable(table1)
if err != nil {
return nil, err
}
err = merged.Sort()
if err != nil {
return nil, err
}
return merged, nil
}
// This copies from left (srcTable) to right (targTable) beginning at beginRow in targTable.
func (srcTable *Table) copyTableCells(beginRow int, targTable *Table) error {
for srcCol := 0; srcCol < srcTable.ColCount(); srcCol++ {
colName, err := srcTable.ColName(srcCol)
// where(fmt.Sprintf("srcTable.ColName(%d) = %q\n", srcCol, colName))
if err != nil {
return err
}
// Note: multiple assignment syntax in for loop.
for srcRow, targRow := 0, beginRow; targRow < (beginRow + srcTable.RowCount()); srcRow, targRow = srcRow+1, targRow+1 {
cellVal, err := srcTable.GetValByColIndex(srcCol, srcRow)
// where(fmt.Sprintf("srcTable.GetValByColIndex(%d, %d) = %v\n", srcCol, srcRow, cellVal))
if err != nil {
return err
}
err = targTable.SetVal(colName, targRow, cellVal)
// where(fmt.Sprintf("targTable.SetVal(%q, %d, cellVal=%v)\n", colName, targRow, cellVal))
if err != nil {
return err
}
}
}
return nil
}