/
readMicrodata.go
702 lines (569 loc) · 18.1 KB
/
readMicrodata.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
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package db
import (
"database/sql"
"errors"
"strconv"
)
// ReadMicrodataTo read entity microdata rows (microdata key, attributes) from model run results and process each row by cvtTo().
func ReadMicrodataTo(dbConn *sql.DB, modelDef *ModelMeta, layout *ReadMicroLayout, cvtTo func(src interface{}) (bool, error)) (*ReadPageLayout, error) {
// validate parameters
if modelDef == nil {
return nil, errors.New("invalid (empty) model metadata, look like model not found")
}
if layout == nil {
return nil, errors.New("invalid (empty) page layout")
}
if layout.Name == "" {
return nil, errors.New("invalid (empty) parameter name")
}
// find entity by name
eIdx, ok := modelDef.EntityByName(layout.Name)
if !ok {
return nil, errors.New("entity not found: " + layout.Name)
}
entity := &modelDef.Entity[eIdx]
// check if model run exist and model run completed
runRow, err := GetRun(dbConn, layout.FromId)
if err != nil {
return nil, err
}
if runRow == nil {
return nil, errors.New("model run not found, id: " + strconv.Itoa(layout.FromId))
}
if runRow.Status != DoneRunStatus {
return nil, errors.New("model run not completed successfully, id: " + strconv.Itoa(layout.FromId))
}
// find entity generation and generation attributes
egLst, err := GetEntityGenList(dbConn, layout.FromId)
if err != nil {
return nil, err
}
var entGen *EntityGenMeta
for k := range egLst {
if egLst[k].GenDigest == layout.GenDigest {
entGen = &egLst[k]
break
}
}
if entGen == nil {
return nil, errors.New("model run does not contain entity generation: " + layout.GenDigest + " " + entity.Name + " in run, id: " + strconv.Itoa(layout.FromId))
}
entityAttrs := make([]EntityAttrRow, len(entGen.GenAttr))
for k, ga := range entGen.GenAttr {
aIdx, isOk := entity.AttrByKey(ga.AttrId)
if !isOk {
return nil, errors.New("entity attribute id not found: " + strconv.Itoa(ga.AttrId) + " " + entity.Name)
}
entityAttrs[k] = entity.Attr[aIdx]
}
// make sql to select microdata from model run:
// SELECT entity_key, attr4, attr7
// FROM Person_g87abcdef
// WHERE run_id = (SELECT base_run_id FROM run_entity WHERE run_id = 1234 AND entity_gen_hid = 1)
// ORDER BY 1, 2
//
q := "SELECT entity_key "
for _, ea := range entityAttrs {
q += ", " + ea.colName
}
q += " FROM " + entGen.DbEntityTable +
" WHERE run_id =" +
" (SELECT base_run_id FROM run_entity" +
" WHERE run_id = " + strconv.Itoa(layout.FromId) +
" AND entity_gen_hid = " + strconv.Itoa(entGen.GenHid) + ")"
// append attribute enum code filters, if specified
for k := range layout.Filter {
// find attribute index by name
aIdx := -1
for j := range entityAttrs {
if entityAttrs[j].Name == layout.Filter[k].Name {
aIdx = j
break
}
}
if aIdx < 0 {
return nil, errors.New("entity " + entity.Name + " does not have attribute " + layout.Filter[k].Name)
}
f, err := makeWhereFilter(
&layout.Filter[k], "", entityAttrs[aIdx].colName, entityAttrs[aIdx].typeOf, false, entityAttrs[aIdx].Name, "entity "+entity.Name)
if err != nil {
return nil, err
}
q += " AND " + f
}
// append attribute enum id filters, if specified
for k := range layout.FilterById {
// find attribute index by name
aIdx := -1
for j := range entityAttrs {
if entityAttrs[j].Name == layout.FilterById[k].Name {
aIdx = j
break
}
}
if aIdx < 0 {
return nil, errors.New("entity " + entity.Name + " does not have attribute " + layout.FilterById[k].Name)
}
f, err := makeWhereIdFilter(
&layout.FilterById[k], "", entityAttrs[aIdx].colName, entityAttrs[aIdx].typeOf, entityAttrs[aIdx].Name, "entity "+entity.Name)
if err != nil {
return nil, err
}
q += " AND " + f
}
// append order by
q += makeOrderBy(0, layout.OrderBy, 1)
// prepare db-row scan conversion buffer: entity key, attributes value
// and define conversion function to make new cell from scan buffer
scanBuf, fc := scanSqlRowToCellMicro(entity, entityAttrs)
// if full page requested:
// select rows into the list buffer and write rows from the list into output stream
if layout.IsFullPage {
// make a list of output cells
cLst, lt, e := SelectToList(dbConn, q, layout.ReadPageLayout,
func(rows *sql.Rows) (interface{}, error) {
if e := rows.Scan(scanBuf...); e != nil {
return nil, e
}
// make new cell from conversion buffer
c := CellMicro{Attr: make([]attrValue, len(entityAttrs))}
if e := fc(&c); e != nil {
return nil, e
}
return c, nil
})
if e != nil {
return nil, e
}
// write page into output stream
for c := cLst.Front(); c != nil; c = c.Next() {
if _, e := cvtTo(c.Value); e != nil {
return nil, e
}
}
return lt, nil // done: return output page layout
}
// else: select rows and write it into output stream without buffering
// adjust page layout: starting offset and page size
nStart := layout.Offset
if nStart < 0 {
nStart = 0
}
nSize := layout.Size
if nSize < 0 {
nSize = 0
}
var nRow int64
lt := ReadPageLayout{
Offset: nStart,
Size: 0,
IsLastPage: false,
}
// select microdata cells: (entity key, attributes value)
err = SelectRowsTo(dbConn, q,
func(rows *sql.Rows) (bool, error) {
// if page size is limited then select only a page of rows
nRow++
if nSize > 0 && nRow > nStart+nSize {
return false, nil
}
if nRow <= nStart {
return true, nil
}
// select next row
if e := rows.Scan(scanBuf...); e != nil {
return false, e
}
lt.Size++
// make new cell from conversion buffer
c := CellMicro{Attr: make([]attrValue, len(entityAttrs))}
if e := fc(&c); e != nil {
return false, e
}
return cvtTo(c) // process cell
})
if err != nil && err != sql.ErrNoRows { // microdata not found is not an error
return nil, err
}
// check for the empty result page or last page
if lt.Size <= 0 {
lt.Offset = nRow
}
lt.IsLastPage = nSize <= 0 || nSize > 0 && nRow <= nStart+nSize
return <, nil
}
// trxReadMicrodataTo read entity microdata rows (microdata key, attributes) from model run results and process each row by cvtTo().
func trxReadMicrodataTo(trx *sql.Tx, entity *EntityMeta, entityAttrs []EntityAttrRow, query string, cvtTo func(src interface{}) error) error {
// select microdata cells: (microdata key, attributes)
scanBuf, fc := scanSqlRowToCellMicro(entity, entityAttrs)
err := TrxSelectRows(trx, query,
func(rows *sql.Rows) error {
// select next row
if e := rows.Scan(scanBuf...); e != nil {
return e
}
// make new cell from conversion buffer
c := CellMicro{Attr: make([]attrValue, len(entityAttrs))}
if e := fc(&c); e != nil {
return e
}
return cvtTo(c) // process cell
})
return err
}
// prepare to scan sql rows and convert each row to CellMicro
// retun scan buffer to be popualted by rows.Scan() and closure to that buffer into CellMicro
func scanSqlRowToCellMicro(entity *EntityMeta, entityAttrs []EntityAttrRow) ([]interface{}, func(*CellMicro) error) {
nAttr := len(entityAttrs)
scanBuf := make([]interface{}, 1+nAttr) // entity key and attributes
var eKeySrc int64
scanBuf[0] = &eKeySrc // first column is entity key
fd := make([]func(interface{}) (attrValue, error), nAttr) // conversion functions for all attributes
// for each attribute create conversion function by type
for na, ea := range entityAttrs {
switch {
case ea.typeOf.IsBool(): // logical attribute
var v interface{}
scanBuf[1+na] = &v
fd[na] = func(src interface{}) (attrValue, error) {
av := attrValue{}
av.IsNull = false // logical attribute expected to be NOT NULL
is := false
switch vn := v.(type) {
case nil: // 2018: unexpected today, may be in the future
is = false
av.IsNull = true
case bool:
is = vn
case int64:
is = vn != 0
case uint64:
is = vn != 0
case int32:
is = vn != 0
case uint32:
is = vn != 0
case int16:
is = vn != 0
case uint16:
is = vn != 0
case int8:
is = vn != 0
case uint8:
is = vn != 0
case uint:
is = vn != 0
case float32: // oracle (very unlikely)
is = vn != 0.0
case float64: // oracle (often)
is = vn != 0.0
case int:
is = vn != 0
default:
return av, errors.New("invalid attribute value type, integer expected: " + entity.Name + "." + ea.Name)
}
av.Value = is
return av, nil
}
case ea.typeOf.IsString(): // string attribute
var vs string
scanBuf[1+na] = &vs
fd[na] = func(src interface{}) (attrValue, error) {
if src == nil {
return attrValue{IsNull: true}, nil
}
return attrValue{IsNull: false, Value: vs}, nil
}
case ea.typeOf.IsFloat(): // float attribute, can be NULL
var vf sql.NullFloat64
scanBuf[1+na] = &vf
fd[na] = func(src interface{}) (attrValue, error) {
if src == nil {
return attrValue{IsNull: true}, nil
}
if vf.Valid {
return attrValue{IsNull: false, Value: vf.Float64}, nil
}
return attrValue{IsNull: true, Value: 0.0}, nil
}
default:
var v interface{}
scanBuf[1+na] = &v
fd[na] = func(src interface{}) (attrValue, error) { return attrValue{IsNull: src == nil, Value: v}, nil }
}
}
// sql row conevrsion function: convert entity key and each attribute value from scan buffer
cvt := func(c *CellMicro) error {
c.Key = uint64(eKeySrc)
for k := 0; k < nAttr; k++ {
v, e := fd[k](scanBuf[1+k])
if e != nil {
return e
}
c.Attr[k] = v
}
return nil
}
return scanBuf, cvt
}
// ReadMicrodataCalculateTo read aggregated microdata rows (run_id, calc_id, group by attributes, calc_value) and process each row by cvtTo().
//
// It can calculate multiple measure values using simple aggregation of attributes, ex.: OM_AVG(Income)
// or on aggregate for run comparison, ex.: OM_AVG(Income[variant] - Income[base]).
// Optional list of run id's can be supplied to read more than one run from microdata table.
func ReadMicrodataCalculateTo(
dbConn *sql.DB, modelDef *ModelMeta, layout *ReadMicroLayout, calcLt *CalculateMicroLayout, runIds []int, cvtTo func(src interface{}) (bool, error),
) (*ReadPageLayout, error) {
// validate parameters
if modelDef == nil {
return nil, errors.New("invalid (empty) model metadata, look like model not found")
}
if layout == nil {
return nil, errors.New("invalid (empty) page layout")
}
if layout.Name == "" {
return nil, errors.New("invalid (empty) parameter name")
}
if calcLt == nil {
return nil, errors.New("invalid (empty) microdata calculation expression(s): " + layout.Name)
}
// find entity by name
eIdx, ok := modelDef.EntityByName(layout.Name)
if !ok {
return nil, errors.New("entity not found: " + layout.Name)
}
entity := &modelDef.Entity[eIdx]
// check if model run exist and model run completed
runRow, err := GetRun(dbConn, layout.FromId)
if err != nil {
return nil, err
}
if runRow == nil {
return nil, errors.New("model run not found, id: " + strconv.Itoa(layout.FromId))
}
if runRow.Status != DoneRunStatus {
return nil, errors.New("model run not completed successfully, id: " + strconv.Itoa(layout.FromId))
}
// find entity generation and generation attributes
egLst, err := GetEntityGenList(dbConn, layout.FromId)
if err != nil {
return nil, err
}
var entityGen *EntityGenMeta
for k := range egLst {
if egLst[k].GenDigest == layout.GenDigest {
entityGen = &egLst[k]
break
}
}
if entityGen == nil {
return nil, errors.New("model run does not contain entity generation: " + layout.GenDigest + " " + entity.Name + " in run, id: " + strconv.Itoa(layout.FromId))
}
// find group by microdata attributes by name
aGroupBy := []EntityAttrRow{}
for _, ga := range entityGen.GenAttr {
aIdx, ok := entity.AttrByKey(ga.AttrId)
if !ok {
return nil, errors.New("entity attribute not found by id: " + strconv.Itoa(ga.AttrId) + " " + entity.Name)
}
isFound := false
for j := 0; !isFound && j < len(calcLt.GroupBy); j++ {
if calcLt.GroupBy[j] != entity.Attr[aIdx].Name {
continue
}
aGroupBy = append(aGroupBy, entity.Attr[aIdx])
// group by attributes must boolean or not built-in
if entity.Attr[aIdx].typeOf.IsBuiltIn() && !entity.Attr[aIdx].typeOf.IsBool() {
return nil, errors.New("invalid type of entity group by attribute not found by: " + entity.Name + "." + calcLt.GroupBy[j] + " : " + entity.Attr[aIdx].typeOf.Name)
}
}
}
// check: all group by attributes must be found
for _, name := range calcLt.GroupBy {
isFound := false
for k := 0; !isFound && k < len(aGroupBy); k++ {
isFound = aGroupBy[k].Name == name
}
if !isFound {
return nil, errors.New("entity group by attribute not found by: " + entity.Name + "." + name)
}
}
// translate calculation to sql
q, err := translateMicroToSql(entity, entityGen, modelDef.Param, &layout.ReadLayout, calcLt, runIds)
if err != nil {
return nil, err
}
// prepare db-row scan conversion buffer: run_id, calculation id, group by attributes, value
// and define conversion function to make new cell from scan buffer
scanBuf, fc, err := scanSqlRowToCellMicroCalc(entity, aGroupBy)
if err != nil {
return nil, err
}
// if full page requested:
// select rows into the list buffer and write rows from the list into output stream
if layout.IsFullPage {
// make a list of output cells: run_id, calculation id, group by attributes, value and null status
cLst, lt, err := SelectToList(dbConn, q, layout.ReadPageLayout,
func(rows *sql.Rows) (interface{}, error) {
if e := rows.Scan(scanBuf...); e != nil {
return nil, e
}
c := CellMicroCalc{Attr: make([]attrValue, len(aGroupBy)+1)}
if e := fc(&c); e != nil { // conversion from scan bubbfer into cell
return nil, e
}
return c, nil
})
if err != nil {
return nil, err
}
// write page into output stream
for c := cLst.Front(); c != nil; c = c.Next() {
if _, e := cvtTo(c.Value); e != nil {
return nil, e
}
}
return lt, nil // done: return output page layout
}
// else: select rows and write it into output stream without buffering
// adjust page layout: starting offset and page size
nStart := layout.Offset
if nStart < 0 {
nStart = 0
}
nSize := layout.Size
if nSize < 0 {
nSize = 0
}
var nRow int64
lt := ReadPageLayout{
Offset: nStart,
Size: 0,
IsLastPage: false,
}
// select microdata cells: (entity key, attributes value)
err = SelectRowsTo(dbConn, q,
func(rows *sql.Rows) (bool, error) {
// if page size is limited then select only a page of rows
nRow++
if nSize > 0 && nRow > nStart+nSize {
return false, nil
}
if nRow <= nStart {
return true, nil
}
// select next row
if e := rows.Scan(scanBuf...); e != nil {
return false, e
}
lt.Size++
c := CellMicroCalc{Attr: make([]attrValue, len(aGroupBy)+1)}
if e := fc(&c); e != nil {
return false, e
}
return cvtTo(c) // process cell
})
if err != nil && err != sql.ErrNoRows { // microdata not found is not an error
return nil, err
}
// check for the empty result page or last page
if lt.Size <= 0 {
lt.Offset = nRow
}
lt.IsLastPage = nSize <= 0 || nSize > 0 && nRow <= nStart+nSize
return <, nil
}
// prepare to scan sql rows and convert each row to CellMicroCalc
// retun scan buffer to be popualted by rows.Scan() and closure to that buffer into CellMicroCalc
func scanSqlRowToCellMicroCalc(entity *EntityMeta, aGroupBy []EntityAttrRow) ([]interface{}, func(*CellMicroCalc) error, error) {
nGrp := len(aGroupBy)
scanBuf := make([]interface{}, 3+nGrp) // run id, calculation id, group by attributes, calculated value
var runId, calcId int
scanBuf[0] = &runId
scanBuf[1] = &calcId
fd := make([]func(interface{}) (attrValue, error), nGrp+1) // conversion functions for group by attributes
// for each attribute create conversion function by type
for na, ga := range aGroupBy {
switch {
case ga.typeOf.IsBool(): // logical attribute
var v interface{}
scanBuf[2+na] = &v
fd[na] = func(src interface{}) (attrValue, error) {
av := attrValue{}
av.IsNull = false // logical attribute expected to be NOT NULL
is := false
switch vn := v.(type) {
case nil: // 2018: unexpected today, may be in the future
is = false
av.IsNull = true
case bool:
is = vn
case int64:
is = vn != 0
case uint64:
is = vn != 0
case int32:
is = vn != 0
case uint32:
is = vn != 0
case int16:
is = vn != 0
case uint16:
is = vn != 0
case int8:
is = vn != 0
case uint8:
is = vn != 0
case uint:
is = vn != 0
case float32: // oracle (very unlikely)
is = vn != 0.0
case float64: // oracle (often)
is = vn != 0.0
case int:
is = vn != 0
default:
return av, errors.New("invalid attribute value type, integer expected: " + entity.Name + "." + ga.Name)
}
av.Value = is
return av, nil
}
case ga.typeOf.IsString(): // string attribute, as it is today strings are not microdata dimenions
return nil, nil, errors.New("invalid group by attribute type: " + ga.typeOf.Name + " : " + entity.Name + "." + ga.Name)
case ga.typeOf.IsFloat(): // float attribute, can be NULL, as it is today floats cannot are not microdata dimenions
return nil, nil, errors.New("invalid group by attribute type: " + ga.typeOf.Name + " : " + entity.Name + "." + ga.Name)
default:
var v interface{}
scanBuf[2+na] = &v
fd[na] = func(src interface{}) (attrValue, error) { return attrValue{IsNull: src == nil, Value: v}, nil }
}
}
// calculated value expected to be float
var vf sql.NullFloat64
scanBuf[2+nGrp] = &vf
fd[nGrp] = func(src interface{}) (attrValue, error) {
if src == nil {
return attrValue{IsNull: true}, nil
}
if vf.Valid {
return attrValue{IsNull: false, Value: vf.Float64}, nil
}
return attrValue{IsNull: true, Value: 0.0}, nil
}
// sql row conevrsion function: convert (run_id, calc_id, group by attributes, calc_value) from scan buffer to cell
cvt := func(c *CellMicroCalc) error {
c.RunId = runId
c.CalcId = calcId
for k := 0; k < nGrp+1; k++ {
v, e := fd[k](scanBuf[2+k])
if e != nil {
return e
}
c.Attr[k] = v
}
return nil
}
return scanBuf, cvt, nil
}