Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix insertion of empty objects with union all statements #1940

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
Loading