-
Notifications
You must be signed in to change notification settings - Fork 1
/
readOutputTable.go
369 lines (325 loc) · 9.08 KB
/
readOutputTable.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
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package db
import (
"database/sql"
"errors"
"strconv"
)
// ReadOutputTableTo read output table page (dimensions and values) from model run results and process each row by cvtTo().
//
// If layout.IsAccum true then select accumulator(s) else output expression value(s)
// If layout.ValueName not empty then select only that expression (accumulator) else all expressions (accumulators)
func ReadOutputTableTo(dbConn *sql.DB, modelDef *ModelMeta, layout *ReadTableLayout, 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) output table name")
}
// find output table id by name
var table *TableMeta
if k, ok := modelDef.OutTableByName(layout.Name); ok {
table = &modelDef.Table[k]
} else {
return nil, errors.New("output table not found: " + layout.Name)
}
// find expression or accumulator id by name
// if this is select from all accumulators view then find db internal column name
valId := -1
valAccCol := ""
if layout.ValueName != "" {
if layout.IsAccum { // find accumulator
for i := range table.Acc {
if table.Acc[i].Name == layout.ValueName {
valId = table.Acc[i].AccId
valAccCol = table.Acc[i].colName
}
}
if valId < 0 || valAccCol == "" {
return nil, errors.New("output table accumulator not found: " + layout.Name + " " + layout.ValueName)
}
} else { // find expression
for i := range table.Expr {
if table.Expr[i].Name == layout.ValueName {
valId = table.Expr[i].ExprId
}
}
if valId < 0 {
return nil, errors.New("output table expression not found: " + layout.Name + " " + layout.ValueName)
}
}
}
// number of accumulator value columns: acc_value or acc0, acc1, acc2...
accCount := 1
if layout.IsAccum && layout.IsAllAccum && layout.ValueName == "" {
accCount = len(table.Acc)
}
// 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))
}
// make sql to select output table expression(s) from model run:
//
// SELECT expr_id, dim0, dim1, expr_value
// FROM salarySex_v2012_820
// WHERE run_id =
// (
// SELECT base_run_id FROM run_table WHERE run_id = 2 AND table_hid = 12345
// )
// AND expr_id = 3
// AND dim1 IN (10, 20, 30, 40)
// ORDER BY 1, 2, 3
//
// or accumulator(s):
//
// SELECT acc_id, sub_id, dim0, dim1, acc_value
// FROM salarySex_a2012_820
// WHERE run_id =
// (
// SELECT base_run_id FROM run_table WHERE run_id = 2 AND table_hid = 12345
// )
// AND acc_id = 4
// AND dim1 IN (10, 20, 30, 40)
// ORDER BY 1, 2, 3, 4
//
// or all accumulators view:
//
// WITH va1 AS
// (
// SELECT
// run_id, sub_id, dim0, dim1, acc_value
// FROM salarySex_a_2012820
// WHERE acc_id = 1
// ),
// v_all_acc AS
// (
// SELECT
// A.run_id,
// A.sub_id,
// A.dim0,
// A.dim1,
// A.acc_value AS acc0,
// A1.acc_value AS acc1,
// (
// A.acc_value / CASE WHEN ABS(A1.acc_value) > 1.0e-37 THEN A1.acc_value ELSE NULL END
// ) AS expr0
// FROM salarySex_a_2012820 A
// INNER JOIN va1 A1 ON (A1.run_id = A.run_id AND A1.sub_id = A.sub_id AND A1.dim0 = A.dim0 AND A1.dim1 = A.dim1)
// WHERE A.acc_id = 0
// )
// SELECT
// run_id, sub_id, dim0, dim1, acc0, acc1, expr0
// FROM va_all_acc
// WHERE run_id =
// (
// SELECT base_run_id FROM run_table WHERE run_id = 2 AND table_hid = 12345
// )
// AND dim1 IN (10, 20, 30, 40)
// ORDER BY 1, 2, 3, 4
//
q := ""
if layout.IsAllAccum {
q = sqlAccAllViewAsWith(table) + " "
}
q += "SELECT"
if layout.IsAccum {
if !layout.IsAllAccum {
q += " acc_id,"
}
q += " sub_id"
} else {
q += " expr_id"
}
for k := range table.Dim {
q += ", " + table.Dim[k].colName
}
if !layout.IsAccum {
q += ", expr_value FROM " + table.DbExprTable
} else {
if !layout.IsAllAccum {
q += ", acc_value FROM " + table.DbAccTable
} else {
if valAccCol != "" {
q += ", " + valAccCol
} else {
for k := range table.Acc {
q += ", " + table.Acc[k].colName
}
}
q += " FROM v_all_acc"
}
}
q += " WHERE run_id =" +
" (SELECT base_run_id FROM run_table" +
" WHERE run_id = " + strconv.Itoa(layout.FromId) +
" AND table_hid = " + strconv.Itoa(table.TableHid) + ")"
if !layout.IsAllAccum && valId >= 0 {
if layout.IsAccum {
q += " AND acc_id = " + strconv.Itoa(valId)
} else {
q += " AND expr_id = " + strconv.Itoa(valId)
}
}
// append dimension enum code filters, if specified
for k := range layout.Filter {
// find dimension index by name
dix := -1
for j := range table.Dim {
if table.Dim[j].Name == layout.Filter[k].Name {
dix = j
break
}
}
if dix < 0 {
return nil, errors.New("output table " + table.Name + " does not have dimension " + layout.Filter[k].Name)
}
f, err := makeWhereFilter(
&layout.Filter[k], "", table.Dim[dix].colName, table.Dim[dix].typeOf, table.Dim[dix].IsTotal, table.Dim[dix].Name, "output table "+table.Name)
if err != nil {
return nil, err
}
q += " AND " + f
}
// append dimension enum id filters, if specified
for k := range layout.FilterById {
// find dimension index by name
dix := -1
for j := range table.Dim {
if table.Dim[j].Name == layout.FilterById[k].Name {
dix = j
break
}
}
if dix < 0 {
return nil, errors.New("output table " + table.Name + " does not have dimension " + layout.FilterById[k].Name)
}
f, err := makeWhereIdFilter(
&layout.FilterById[k], "", table.Dim[dix].colName, table.Dim[dix].typeOf, table.Dim[dix].Name, "output table "+table.Name)
if err != nil {
return nil, err
}
q += " AND " + f
}
// append order by expr_id or acc_id, sub_id or sub_id
nExtraCol := 1
if layout.IsAccum && !layout.IsAllAccum {
nExtraCol = 2 // extra columns: acc_id, sub_id
}
q += makeOrderBy(table.Rank, layout.OrderBy, nExtraCol)
// prepare db-row conversion buffer:
// acc_id, sub_id, expr_id, dimensions, value or []values
var n1, n2 int
d := make([]int, table.Rank)
var vf sql.NullFloat64
fa := make([]sql.NullFloat64, accCount)
var scanBuf []interface{}
scanBuf = append(scanBuf, &n1)
if layout.IsAccum && !layout.IsAllAccum {
scanBuf = append(scanBuf, &n2)
}
for k := 0; k < table.Rank; k++ {
scanBuf = append(scanBuf, &d[k])
}
if !layout.IsAccum || !layout.IsAllAccum {
scanBuf = append(scanBuf, &vf)
} else {
for k := 0; k < accCount; k++ {
scanBuf = append(scanBuf, &fa[k])
}
}
// 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 cells:
// expr_id or or sub_id or acc_id and sub_id, dimension(s) enum ids
// value or all accumulator values and null status
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 err := rows.Scan(scanBuf...); err != nil {
return false, err
}
lt.Size++
// make new cell from conversion buffer
if layout.IsAccum {
if !layout.IsAllAccum {
var ca = CellAcc{cellIdValue: cellIdValue{DimIds: make([]int, table.Rank)}}
ca.AccId = n1
ca.SubId = n2
copy(ca.DimIds, d)
ca.IsNull = !vf.Valid
ca.Value = 0.0
if !ca.IsNull {
ca.Value = vf.Float64
}
return cvtTo(ca) // process cell
}
// else all accumulators
var cl = CellAllAcc{
DimIds: make([]int, table.Rank),
IsNull: make([]bool, accCount),
Value: make([]float64, accCount)}
cl.SubId = n1
copy(cl.DimIds, d)
for k := 0; k < accCount; k++ {
cl.IsNull[k] = !fa[k].Valid
cl.Value[k] = 0.0
if !cl.IsNull[k] {
cl.Value[k] = fa[k].Float64
}
}
return cvtTo(cl) // process cell
}
// else output table expression
var ce = CellExpr{cellIdValue: cellIdValue{DimIds: make([]int, table.Rank)}}
ce.ExprId = n1
copy(ce.DimIds, d)
ce.IsNull = !vf.Valid
ce.Value = 0.0
if !ce.IsNull {
ce.Value = vf.Float64
}
return cvtTo(ce) // process cell
})
if err != nil {
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
}