forked from alibaba/nquery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaggregation.js
79 lines (69 loc) · 1.76 KB
/
aggregation.js
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
// (C) 2011-2013 Alibaba Group Holding Limited.
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
// Author :windyrobin <[email protected]>
var Tool = require('../lib/tool');
var Engine = require('../lib/engine');
var AstHelper = require('../lib/ast_helper');
var runExpr = Engine.run;
var getSelRefCols = AstHelper.getSelRefCols;
var getRefColInfo = AstHelper.getRefColInfo;
var backupRefCols = AstHelper.backupRefCols;
var restoreRefCols = AstHelper.restoreRefCols;
function debug(str) {
//console.log(str);
}
function inspect(obj) {
//console.log(require('util').inspect(obj, false, 10));
}
module.exports = doAggregation;
function doAggregation(dc, sels, info) {
// {
// id1 : {
// cols : [val1, val2, val3,...]
// pos : 2
// } ,
// id2 : {
// cols : [,,]
// pos :
// }
// }
if (!info) {
var refCols = getSelRefCols(sels);
info = getRefColInfo(refCols, dc.columns);
}
//inspect(refCols);
var i, key, pos, vals;
var valMap = {};
var rows = dc.data;
for (key in info) {
vals = [];
pos = info[key].pos;
for (i = 0; i < rows.length; i++) {
vals.push(rows[i][pos]);
}
valMap[key] = vals;
}
//inspect(valMap);
var rc;
for (key in info) {
rc = info[key].cols;
vals = valMap[key];
for (i = 0; i < rc.length; i++) {
rc[i].type = 'literal_list';
rc[i].value = vals;
}
}
var val;
var row = [];
for (i = 0; i < sels.length; i++) {
val = runExpr(sels[i].expr);
//for no aggr fuctions
if (Array.isArray(val)) {
val = val[0];
}
row.push(val);
}
return row;
}