Skip to content

Commit

Permalink
fix insertion of empty objects with union all statements
Browse files Browse the repository at this point in the history
  • Loading branch information
kmr-rohit committed Jul 2, 2024
1 parent ca7ecb4 commit 1df1edc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/38query.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,16 @@ function queryfn3(query) {
ilen = nd.data.length;
for (var i = 0; i < ilen; i++) {
var r = {};
for (var j = Math.min(query.columns.length, nd.columns.length) - 1; 0 <= j; j--) {
r[query.columns[j].columnid] = nd.data[i][nd.columns[j].columnid];
if (query.columns.length) {
jlen = Math.min(query.columns.length, nd.columns.length);
for (var j = 0; j < jlen; j++) {
r[query.columns[j].columnid] = nd.data[i][nd.columns[j].columnid];
}
} else {
jlen = nd.columns.length;
for (var j = 0; j < jlen; j++) {
r[nd.columns[j].columnid] = nd.data[i][nd.columns[j].columnid];
}
}
ud.push(r);
}
Expand Down
31 changes: 31 additions & 0 deletions test/test1684.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
if (typeof exports === 'object') {
var assert = require('assert');
var alasql = require('..');
} else {
__dirname = '.';
}

describe('Test 1684 - UNION ALL still not returning correct results bug', function () {
it('1. should not insert empty objects in results when using UNION ALL Expression', function (done) {
var data = [
{ "city": "Madrid", "population": 3041579 },
{ "city": "Rome", "population": 2863223 },
{ "city": "Paris", "population": 2249975 }
]
var sql = "SELECT city FROM :data WHERE city = 'Madrid' \
UNION ALL SELECT city FROM :data WHERE city = 'Rome' \
UNION ALL SELECT city FROM :data WHERE city = 'Paris' \
"
var res = alasql(sql, { data });
assert.deepEqual(res, [{ city: 'Madrid' }, { city: 'Rome' }, { city: 'Paris' }]);

var sql = "SELECT * FROM :data WHERE city = 'Madrid' \
UNION ALL SELECT * FROM :data WHERE city = 'Rome' \
UNION ALL SELECT * FROM :data WHERE city = 'Paris' \
"
var res = alasql(sql, { data });
assert.deepEqual(res, [{ city: 'Madrid', population: 3041579 }, { city: 'Rome', population: 2863223 }, { city: 'Paris', population: 2249975 }]);

done();
});
});

0 comments on commit 1df1edc

Please sign in to comment.