diff --git a/src/40select.js b/src/40select.js index 44273bc256..7a97613fe0 100755 --- a/src/40select.js +++ b/src/40select.js @@ -468,6 +468,12 @@ function modify(query, res) { } else { // Cannot recognize columns columns = []; + if (query && query.sources) { + query.sources.forEach((source) => { + if(source?.columns?.columnid != null) + columns = columns.concat({columnid:source?.columns?.columnid}); + }) + } } } diff --git a/test/test1547.js b/test/test1547.js new file mode 100644 index 0000000000..63abb4f163 --- /dev/null +++ b/test/test1547.js @@ -0,0 +1,64 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} + +describe('Test 1547 - Empty recordset', function () { + const test = '1547'; + + before(function () { + alasql('create database test' + test); + alasql('use test' + test); + }); + + after(function () { + alasql('drop database test' + test); + }); + + it('Returns columns for empty recordset', function () { + alasql('create table one (a int)'); + alasql('insert into one values (1),(2),(3),(4),(5)'); + let res = alasql('recordset of select * from one where a = 999'); + assert.deepEqual(res, { + columns: [ + { + columnid: 'a', + }, + ], + data: [], + }); + + /* assert( + (() => { + const testdata = { + columns: [ + { + columnid: 'a', + }, + ], + data: [], + }; + + if ( + res.columns.length !== testdata.columns.length || + res.data.length !== testdata.data.length + ) { + return false; + } else { + let cols = res.columns; + + for (let col of cols) { + let index = testdata.columns.findIndex((c) => c.columnid === col.columnid); + + if (index === -1) { + return false; + } + } + + return true; + } + })(), + 'Response should not have missing coloumns' + );*/ + }); +});