Skip to content

Commit

Permalink
fix #1415: UNION not returning correct result (#1629)
Browse files Browse the repository at this point in the history
  • Loading branch information
ambujsahu81 authored Feb 8, 2023
1 parent 09c53a4 commit 4287740
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/38query.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,16 @@ function queryfn3(query) {
ilen = nd.data.length;
for (var i = 0; i < ilen; i++) {
r = {};
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];
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
27 changes: 27 additions & 0 deletions test/test1415.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
if (typeof exports === 'object') {
var assert = require('assert');
var alasql = require('..');
} else {
__dirname = '.';
}

describe('Test 1415 - UNION Expression with empty query columns bug', function () {
it('1. should not insert empty objects in results when using UNION Expression', function (done) {
var data1 = [
{a: 'abc'},
{a: 'xyz'}
];
var data2 = [
{a: '123'},
{a: '987'}
];

var res = alasql("SELECT * FROM :a UNION SELECT * FROM :b", {a: data1, b: data2});
assert.deepEqual(res, [{a: '123'}, {a: '987'}, { a: 'abc'}, {a: 'xyz'}]);

var res = alasql("SELECT * FROM @[{x: true}, {x: 3}] UNION SELECT * FROM @[{x: false}, {x: 9}]");
assert.deepEqual(res, [{x: false}, {x: 9}, {x: true}, {x: 3}]);

done();
});
});

0 comments on commit 4287740

Please sign in to comment.