Skip to content

Commit

Permalink
Fix for aggregate functions applied to empty sets (AlaSQL#964)
Browse files Browse the repository at this point in the history
* Return undefined from aggregate functions when the accumulator is undefined (fixes AlaSQL#917)

* Build from src
  • Loading branch information
alexpeattie authored and mathiasrw committed Jan 10, 2018
1 parent b3e6f13 commit dbcbedc
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 15 deletions.
4 changes: 2 additions & 2 deletions dist/alasql-worker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! AlaSQL v0.4.4 | © 2014-2016 Andrey Gershun & Mathias Rangel Wulff | License: MIT
//! AlaSQL v0.4.4-fix-aggregate-empty-sets-1566 | © 2014-2016 Andrey Gershun & Mathias Rangel Wulff | License: MIT
/*
@module alasql
@version 0.4.4
@version 0.4.4-fix-aggregate-empty-sets-1566
AlaSQL - JavaScript SQL database
© 2014-2016 Andrey Gershun & Mathias Rangel Wulff
Expand Down
2 changes: 1 addition & 1 deletion dist/alasql-worker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions dist/alasql.fs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! AlaSQL v0.4.4 | © 2014-2016 Andrey Gershun & Mathias Rangel Wulff | License: MIT
//! AlaSQL v0.4.4-fix-aggregate-empty-sets-1566 | © 2014-2016 Andrey Gershun & Mathias Rangel Wulff | License: MIT
/*
@module alasql
@version 0.4.4
@version 0.4.4-fix-aggregate-empty-sets-1566

AlaSQL - JavaScript SQL database
© 2014-2016 Andrey Gershun & Mathias Rangel Wulff
Expand Down Expand Up @@ -137,7 +137,7 @@ var alasql = function(sql, params, cb, scope) {
Current version of alasql
@constant {string}
*/
alasql.version = '0.4.4';
alasql.version = '0.4.4-fix-aggregate-empty-sets-1566';

/**
Debug flag
Expand Down Expand Up @@ -10832,6 +10832,15 @@ alasql.aggr.STD = alasql.aggr.STDDEV = alasql.aggr.STDEVP = function(v,s,stage){
}
};

alasql._aggrOriginal = alasql.aggr
alasql.aggr = {}
Object.keys(alasql._aggrOriginal).forEach(function(k) {
alasql.aggr[k] = function(v, s, stage) {
if(stage === 3 && typeof(s) === 'undefined') return undefined;
return alasql._aggrOriginal[k].apply(null, arguments)
}
})

// String functions
stdfn.REPLACE = function (target,pattern,replacement) {
return (target||'').split(pattern).join(replacement);
Expand Down
15 changes: 12 additions & 3 deletions dist/alasql.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! AlaSQL v0.4.4 | © 2014-2016 Andrey Gershun & Mathias Rangel Wulff | License: MIT
//! AlaSQL v0.4.4-fix-aggregate-empty-sets-1566 | © 2014-2016 Andrey Gershun & Mathias Rangel Wulff | License: MIT
/*
@module alasql
@version 0.4.4
@version 0.4.4-fix-aggregate-empty-sets-1566

AlaSQL - JavaScript SQL database
© 2014-2016 Andrey Gershun & Mathias Rangel Wulff
Expand Down Expand Up @@ -137,7 +137,7 @@ var alasql = function(sql, params, cb, scope) {
Current version of alasql
@constant {string}
*/
alasql.version = '0.4.4';
alasql.version = '0.4.4-fix-aggregate-empty-sets-1566';

/**
Debug flag
Expand Down Expand Up @@ -10832,6 +10832,15 @@ alasql.aggr.STD = alasql.aggr.STDDEV = alasql.aggr.STDEVP = function(v,s,stage){
}
};

alasql._aggrOriginal = alasql.aggr
alasql.aggr = {}
Object.keys(alasql._aggrOriginal).forEach(function(k) {
alasql.aggr[k] = function(v, s, stage) {
if(stage === 3 && typeof(s) === 'undefined') return undefined;
return alasql._aggrOriginal[k].apply(null, arguments)
}
})

// String functions
stdfn.REPLACE = function (target,pattern,replacement) {
return (target||'').split(pattern).join(replacement);
Expand Down
12 changes: 6 additions & 6 deletions dist/alasql.min.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/55functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,15 @@ alasql.aggr.STD = alasql.aggr.STDDEV = alasql.aggr.STDEVP = function(v,s,stage){
}
};

alasql._aggrOriginal = alasql.aggr
alasql.aggr = {}
Object.keys(alasql._aggrOriginal).forEach(function(k) {
alasql.aggr[k] = function(v, s, stage) {
if(stage === 3 && typeof(s) === 'undefined') return undefined;
return alasql._aggrOriginal[k].apply(null, arguments)
}
})

// String functions
stdfn.REPLACE = function (target,pattern,replacement) {
return (target||'').split(pattern).join(replacement);
Expand Down
26 changes: 26 additions & 0 deletions test/test619.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
if(typeof exports === 'object') {
var assert = require("assert");
var alasql = require('..');
};

/*
Test for issue #917
*/

var test = 619;

describe('Test '+test+' calling aggregate functions on empty sets', function() {

before(function(){
alasql('CREATE DATABASE test'+test+';USE test'+test);
});

after(function(){
alasql('DROP DATABASE test'+test);
});

it('1. Should always return undefined', function(){
res = alasql('SELECT STDDEV(col) AS Result FROM ? WHERE 1=0', [[{col:1},{col:2}]]);
assert.equal(res[0]['Result'], undefined);
})
});

0 comments on commit dbcbedc

Please sign in to comment.