diff --git a/src/select/select.js b/src/select/select.js index 9302d59d79..0d2a86a2dd 100755 --- a/src/select/select.js +++ b/src/select/select.js @@ -919,67 +919,7 @@ export default function install(alasql, internal) { } var modifier = query.modifier || alasql.options.modifier; - var columns = query.columns; - - // If dirtyColumns is true, we need to merge columns from data with existing columns - // This happens when SELECT * is used with dynamic data sources (like parameters) - if (query.dirtyColumns && res.length > 0) { - var allcol = {}; - // First, scan the data to find all column names - for (var i = Math.min(res.length, alasql.options.columnlookup || 10) - 1; 0 <= i; i--) { - for (var key in res[i]) { - allcol[key] = true; - } - } - - // Create columns from data - var dataColumns = Object.keys(allcol).map(function (columnid) { - return {columnid: columnid}; - }); - - // If we don't have any columns yet, just use the data columns - if (!columns || columns.length === 0) { - columns = dataColumns; - } else { - // We have some columns (e.g., from explicit column expressions), - // merge them with data columns, avoiding duplicates - var existingColumnIds = {}; - columns.forEach(function (col) { - existingColumnIds[col.columnid] = true; - }); - - // Add data columns that aren't already in the list - dataColumns.forEach(function (col) { - if (!existingColumnIds[col.columnid]) { - columns.push(col); - } - }); - } - } else if (typeof columns === 'undefined' || columns.length === 0) { - // Try to create columns - if (res.length > 0) { - var allcol = {}; - for (var i = Math.min(res.length, alasql.options.columnlookup || 10) - 1; 0 <= i; i--) { - for (var key in res[i]) { - allcol[key] = true; - } - } - - columns = Object.keys(allcol).map(function (columnid) { - return {columnid: columnid}; - }); - } else { - // Cannot recognize columns - columns = []; - if (query && query.sources) { - query.sources.forEach(source => { - if (source && source.columns && Array.isArray(source.columns)) { - columns = columns.concat(source.columns); - } - }); - } - } - } + var columns = resultColumns(query, res); switch (modifier) { case 'VALUE': @@ -1048,5 +988,37 @@ export default function install(alasql, internal) { return res; } + // The columns of the result of one run: the compiled columns when the compiler knows them, + // otherwise read from the first rows, otherwise the columns of the sources. + function resultColumns(run, rows) { + const compiled = run.columns; + if (run.dirtyColumns && rows.length > 0) return mergeColumns(compiled, namesInRows(rows)); + if (compiled && compiled.length > 0) return compiled; + if (rows.length > 0) return namesInRows(rows); + return columnsOfSources(run.sources); + } + + // The keys of the first columnlookup rows. The rows are read from the last to the first, + // and that decides the order of the names. + function namesInRows(rows) { + const names = {}; + for (let i = Math.min(rows.length, alasql.options.columnlookup || 10) - 1; i >= 0; i--) { + for (const key in rows[i]) names[key] = true; + } + return Object.keys(names).map(columnid => ({columnid})); + } + + function mergeColumns(compiled, found) { + const known = new Set(compiled.map(column => String(column.columnid))); + return compiled.concat(found.filter(column => !known.has(column.columnid))); + } + + function columnsOfSources(sources) { + return (sources || []).flatMap(source => + source && Array.isArray(source.columns) ? source.columns : [] + ); + } + internal.modify = modify; + internal.resultColumns = resultColumns; } diff --git a/test/test2651.js b/test/test2651.js new file mode 100644 index 0000000000..c1b8a40ffb --- /dev/null +++ b/test/test2651.js @@ -0,0 +1,163 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} + +let testId = '2651'; + +describe(`Test ${testId} - columns of a SELECT result for each modifier`, function () { + before(function () { + alasql('create database test' + testId); + alasql('use test' + testId); + alasql('CREATE TABLE empty (a INT, b INT)'); + alasql('CREATE TABLE two (a INT, b INT); INSERT INTO two VALUES (1, 2), (3, 4)'); + }); + + after(function () { + alasql('drop database test' + testId); + }); + + function names(columns) { + return columns.map(column => column.columnid); + } + + // ALASQL_DETAILS has no SQL syntax; WITH sets it on the parsed statement + function details(sql, params) { + const select = alasql.parse('SELECT ' + sql).statements[0]; + select.modifier = 'ALASQL_DETAILS'; + return select.compile('test' + testId)(params); + } + + function result(sql, params) { + return { + recordset: names(alasql('SELECT RECORDSET ' + sql, params).columns), + details: names(details(sql, params).columns), + matrix: alasql('SELECT MATRIX ' + sql, params), + column: alasql('SELECT COLUMN ' + sql, params), + }; + } + + it('A) * over ? with rows that have different keys', function () { + assert.deepStrictEqual(result('* FROM ?', [[{a: 1}, {a: 2, b: 3}]]), { + recordset: ['a', 'b'], + details: ['a', 'b'], + matrix: [ + [1, undefined], + [2, 3], + ], + column: [1, 2], + }); + assert.deepStrictEqual(result('* FROM ?', [[{b: 1}, {a: 2}]]), { + recordset: ['a', 'b'], + details: ['a', 'b'], + matrix: [ + [undefined, 1], + [2, undefined], + ], + column: [undefined, 2], + }); + }); + + it('B) * and a named column over ?', function () { + assert.deepStrictEqual(result('*, a+1 AS c FROM ?', [[{a: 1}, {a: 2, b: 3}]]), { + recordset: ['c', 'a', 'b'], + details: ['c', 'a', 'b'], + matrix: [ + [2, 1, undefined], + [3, 2, 3], + ], + column: [2, 3], + }); + }); + + it('C) * over an empty ?', function () { + assert.deepStrictEqual(result('* FROM ?', [[]]), { + recordset: [], + details: [], + matrix: undefined, + column: [], + }); + }); + + it('D) * over an empty table', function () { + assert.deepStrictEqual(result('* FROM empty'), { + recordset: ['a', 'b'], + details: ['a', 'b'], + matrix: undefined, + column: [], + }); + }); + + it('E) * over a table with rows', function () { + assert.deepStrictEqual(result('* FROM two'), { + recordset: ['a', 'b'], + details: ['a', 'b'], + matrix: [ + [1, 2], + [3, 4], + ], + column: [1, 3], + }); + }); + + it('F) A key that first shows up after columnlookup rows is not a column', function () { + const rows = []; + for (let i = 0; i < 12; i++) rows.push({a: i}); + rows[11].z = 9; + const res = result('* FROM ?', [rows]); + assert.deepStrictEqual( + { + recordset: res.recordset, + details: res.details, + matrix: res.matrix[11], + column: res.column[11], + }, + {recordset: ['a'], details: ['a'], matrix: [11], column: 11} + ); + }); + + it('G) A compiled statement reads the columns of each run from its own rows', function () { + const star = alasql.compile('SELECT RECORDSET * FROM ?'); + const named = alasql.compile('SELECT RECORDSET *, 1 AS c FROM ?'); + assert.deepStrictEqual( + [ + names(star([[{a: 1}]]).columns), + names(star([[{b: 2}]]).columns), + names(star([[{a: 1}]]).columns), + names(named([[{a: 1}]]).columns), + names(named([[{b: 2}]]).columns), + ], + [['a'], ['b'], ['a'], ['c', 'a'], ['c', 'b']] + ); + }); + + it('H) A key named like a property of an object is a column next to a named column', function () { + assert.deepStrictEqual(result('*, 1 AS c FROM ?', [[{constructor: 5, valueOf: 6, a: 1}]]), { + recordset: ['c', 'constructor', 'valueOf', 'a'], + details: ['c', 'constructor', 'valueOf', 'a'], + matrix: [[1, 5, 6, 1]], + column: [1], + }); + }); + + it('I) PIVOT over ? names each column once', function () { + const pivot = 'SELECT RECORDSET * FROM ? PIVOT (SUM(a) FOR b)'; + assert.deepStrictEqual( + [ + names( + alasql(pivot, [ + [ + {a: 1, b: 2, g: 'x'}, + {a: 5, b: 3, g: 'y'}, + ], + ]).columns + ), + names(alasql(pivot, [[{a: 1, b: 2}, {a: 5}]]).columns), + ], + [ + [2, 3, 'g'], + [2, undefined], + ] + ); + }); +});