diff --git a/src/38query.js b/src/38query.js index 7ba5752103..6dffc5e2b5 100755 --- a/src/38query.js +++ b/src/38query.js @@ -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); } diff --git a/test/test1415.js b/test/test1415.js new file mode 100644 index 0000000000..f1a9926d8e --- /dev/null +++ b/test/test1415.js @@ -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(); + }); +});