Skip to content

Commit

Permalink
ARRAY aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
agershun committed Dec 22, 2014
1 parent 1a980a9 commit faff4c0
Show file tree
Hide file tree
Showing 14 changed files with 1,144 additions and 1,082 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

### 0.0.35 (14.12.2014 - 20.12.2014)

* Added [?] array conversion
* ARRAY Aggregator (http://stackoverflow.com/questions/15887900/group-objects-by-property-in-javascript?rq=1)


### 0.0.34 (14.12.2014 - 20.12.2014)

* New User Manual written
Expand Down
716 changes: 363 additions & 353 deletions alasql.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion alasql.js.map

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions alasql.min.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions console/alasql.min.js

Large diffs are not rendered by default.

716 changes: 363 additions & 353 deletions dist/alasql.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/alasql.js.map

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions dist/alasql.min.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/42compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ yy.Select.prototype.compileGroup = function(query) {
// || col.aggregatorid == 'AVG'
// ) { return '\''+col.as+'\':r[\''+col.as+'\'],'; }//f.field.arguments[0].toJavaScript();
) { return '\''+col.as+'\':r[\''+col.as+'\'],'; }//f.field.arguments[0].toJavaScript();
else if(col.aggregatorid == 'COUNT') { return '\''+col.as+'\':1,'; }
else if(col.aggregatorid == 'ARRAY') {
return '\''+col.as+'\':[r[\''+col.as+'\']],';
} else if(col.aggregatorid == 'COUNT') { return '\''+col.as+'\':1,'; }
// else if(col.aggregatorid == 'MIN') { return '\''+col.as+'\':r[\''+col.as+'\'],'; }
// else if(col.aggregatorid == 'MAX') { return '\''+col.as+'\':r[\''+col.as+'\'],'; }
else if(col.aggregatorid == 'AVG') {
Expand Down Expand Up @@ -505,6 +507,7 @@ yy.Select.prototype.compileGroup = function(query) {
if (col instanceof yy.AggrValue) {
if (col.aggregatorid == 'SUM') { return 'g[\''+col.as+'\']+=r[\''+col.as+'\'];'; }//f.field.arguments[0].toJavaScript();
else if(col.aggregatorid == 'COUNT') { return 'g[\''+col.as+'\']++;'; }
else if(col.aggregatorid == 'ARRAY') { return 'g[\''+col.as+'\'].push(r[\''+col.as+'\']);'; }
else if(col.aggregatorid == 'MIN') { return 'g[\''+col.as+'\']=Math.min(g[\''+col.as+'\'],r[\''+col.as+'\']);'; }
else if(col.aggregatorid == 'MAX') { return 'g[\''+col.as+'\']=Math.max(g[\''+col.as+'\'],r[\''+col.as+'\']);'; }
else if(col.aggregatorid == 'FIRST') { return ''; }
Expand Down Expand Up @@ -825,7 +828,8 @@ yy.Select.prototype.compileSelect1 = function(query) {
}
if(!col.as) col.as = escapeq(col.toString());
if (col.aggregatorid == 'SUM' || col.aggregatorid == 'MAX' || col.aggregatorid == 'MIN' ||
col.aggregatorid == 'FIRST' || col.aggregatorid == 'LAST' || col.aggregatorid == 'AVG'
col.aggregatorid == 'FIRST' || col.aggregatorid == 'LAST' ||
col.aggregatorid == 'AVG' || col.aggregatorid == 'ARRAY'
) {
ss.push("'"+escapeq(col.as)+'\':'+col.expression.toJavaScript("p",query.defaultTableid,query.defcols))
} else if (col.aggregatorid == 'COUNT') {
Expand Down
1 change: 1 addition & 0 deletions src/50expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ yy.AggrValue.prototype.toString = function() {

yy.AggrValue.prototype.toType = function() {
if(['SUM','COUNT','AVG','MIN', 'MAX','AGGR'].indexOf(this.aggregatorid)>-1) return 'number';
if(['ARRAY'].indexOf(this.aggregatorid)>-1) return 'array';
if(['FIRST','LAST' ].indexOf(this.aggregatorid)>-1) return this.expression.toType();
}
yy.AggrValue.prototype.toJavaScript = function(context, tableid, defcols) {
Expand Down
2 changes: 2 additions & 0 deletions src/alasqlparser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
'AND' return 'AND'
'ANTI' return 'ANTI'
'ANY' return 'ANY'
'ARRAY' return 'ARRAY'
'AS' return 'AS'
'ASSERT' return 'ASSERT'
'ASC' return 'DIRECTION'
Expand Down Expand Up @@ -767,6 +768,7 @@ Aggregator
| FIRST { $$ = "FIRST"; }
| LAST { $$ = "LAST"; }
| AGGR { $$ = "AGGR"; }
| ARRAY { $$ = "ARRAY"; }
;

FuncValue
Expand Down
707 changes: 356 additions & 351 deletions src/alasqlparser.js

Large diffs are not rendered by default.

Binary file modified test/test169a.xlsx
Binary file not shown.
24 changes: 24 additions & 0 deletions test/test181.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
if(typeof exports === 'object') {
var assert = require("assert");
var alasql = require('../alasql.js');
} else {
__dirname = '.';
};

//if(typeof exports != 'object') {

describe('Test 181 - ARRAY aggregator', function() {

it("1. ARRAY()", function(done) {
var food = [
{food: 'apple', type: 'fruit'},
{food: 'potato', type: 'vegetable'},
{food: 'banana', type: 'fruit'},
];
var res = alasql('SELECT ARRAY(food) AS foods FROM ? GROUP BY type',[food]);
console.log(res);
// assert.deepEqual(res,[1,2,3,4,5,6,7,8,9,10]);
done();
});

});

0 comments on commit faff4c0

Please sign in to comment.