diff --git a/src/select/columns.js b/src/select/columns.js index c2284c8baa..420c0a843e 100755 --- a/src/select/columns.js +++ b/src/select/columns.js @@ -917,27 +917,36 @@ export default function install(alasql, internal) { // SELECY * REMOVE [COLUMNS] col-list, LIKE '' yy.Select.prototype.compileRemoveColumns = function (query) { - var self = this; if (typeof this.removecolumns !== 'undefined') { - query.removeKeys = query.removeKeys.concat( - this.removecolumns - .filter(function (column) { - return typeof column.like === 'undefined'; - }) - .map(function (column) { - return column.columnid; - }) - ); + var removeKeys = this.removecolumns + .filter(function (column) { + return typeof column.like === 'undefined'; + }) + .map(function (column) { + return column.columnid; + }); + query.removeKeys = query.removeKeys.concat(removeKeys); - //console.log(query.removeKeys,this.removecolumns); query.removeLikeKeys = this.removecolumns .filter(function (column) { return typeof column.like !== 'undefined'; }) .map(function (column) { - // return new RegExp((column.like.value||'').replace(/\%/g,'.*').replace(/\?|_/g,'.'),'g'); return column.like.value; }); + + // The columns of the result no longer include the removed ones, so a query + // around this one expands * from the columns that are left. + query.columns = query.columns.filter(function (column) { + return ( + !removeKeys.some(function (key) { + return column.columnid == key; + }) && + !query.removeLikeKeys.some(function (key) { + return alasql.utils.like(key, column.columnid); + }) + ); + }); } }; } diff --git a/src/select/join.js b/src/select/join.js index fb746257e6..cbdb96c5ab 100755 --- a/src/select/join.js +++ b/src/select/join.js @@ -41,7 +41,9 @@ export default function install(alasql, internal) { columns: [], // TODO check this }; source.applyselect = jn.select.compile(query.database.databaseid); - source.columns = source.applyselect.query.columns; + source.columns = source.applyselect.query.dirtyColumns + ? [] + : source.applyselect.query.columns; source.datafn = function (query, params, cb, idx, alasql) { let res; @@ -114,7 +116,7 @@ export default function install(alasql, internal) { if (typeof source.subquery.query.modifier === 'undefined') { source.subquery.query.modifier = 'RECORDSET'; // Subqueries always return recordsets } - source.columns = source.subquery.query.columns; + source.columns = source.subquery.query.dirtyColumns ? [] : source.subquery.query.columns; source.datafn = subqueryDatafn(source); query.aliases[source.alias] = {type: 'subquery'}; diff --git a/src/select/pivot.js b/src/select/pivot.js index 8154e58b12..8532d19588 100755 --- a/src/select/pivot.js +++ b/src/select/pivot.js @@ -10,6 +10,9 @@ export default function install(alasql) { */ yy.Select.prototype.compilePivot = function (query) { var self = this; + // The pivoted columns depend on the rows, so a query around this one + // expands * from the rows instead of the columns of the select list. + query.dirtyColumns = true; /** @type {string} Main pivoting column */ var columnid = self.pivot.columnid; @@ -337,6 +340,18 @@ export default function install(alasql) { return l.columnid; }); + // UNPIVOT replaces the columns of the select list with columns that are + // known when the query is compiled. + query.columns = query.columns + .filter(function (col) { + return ( + inlist.indexOf(col.columnid) == -1 && + col.columnid != forcolumnid && + col.columnid != tocolumnid + ); + }) + .concat([{columnid: forcolumnid}, {columnid: tocolumnid}]); + // console.log(inlist, tocolumnid, forcolumnid); return function () { diff --git a/test/test3194.js b/test/test3194.js new file mode 100644 index 0000000000..c868bff269 --- /dev/null +++ b/test/test3194.js @@ -0,0 +1,57 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} + +let testId = '3194'; + +describe(`Test ${testId} - REMOVE and PIVOT in a FROM subquery`, function () { + before(function () { + alasql('create database test' + testId); + alasql('use test' + testId); + alasql('CREATE TABLE A (a INT, b STRING)'); + alasql('INSERT INTO A VALUES (3,"x"),(1,"y"),(2,"z")'); + alasql('CREATE TABLE U (id INT, x INT, y INT)'); + alasql('INSERT INTO U VALUES (1,10,20)'); + }); + + after(function () { + alasql('drop database test' + testId); + }); + + it('A) REMOVE COLUMN in a FROM subquery', function () { + var res = alasql('SELECT * FROM (SELECT * REMOVE COLUMN b FROM A LIMIT 1)'); + assert.deepStrictEqual(res, [{a: 3}]); + }); + + it('B) Plain REMOVE COLUMN still removes the column', function () { + var res = alasql('SELECT * REMOVE COLUMN b FROM A LIMIT 1'); + assert.deepStrictEqual(res, [{a: 3}]); + }); + + it('C) REMOVE COLUMNS LIKE in a FROM subquery', function () { + var res = alasql('SELECT * FROM (SELECT * REMOVE COLUMNS LIKE "b%" FROM A LIMIT 1)'); + assert.deepStrictEqual(res, [{a: 3}]); + }); + + it('D) PIVOT in a FROM subquery', function () { + var res = alasql('SELECT * FROM (SELECT * FROM A PIVOT (SUM(a) FOR b)) q'); + assert.deepStrictEqual(res, [{x: 3, y: 1, z: 2}]); + }); + + it('E) UNPIVOT in a FROM subquery', function () { + var res = alasql('SELECT * FROM (SELECT * FROM U UNPIVOT (amount FOR label IN (x,y))) q'); + assert.deepStrictEqual(res, [ + {id: 1, label: 'x', amount: 10}, + {id: 1, label: 'y', amount: 20}, + ]); + }); + + it('F) PIVOT in a joined subquery', function () { + var res = alasql( + 'SELECT * FROM (SELECT 1 AS id) k JOIN ' + + '(SELECT * FROM A PIVOT (SUM(a) FOR b)) q ON k.id = 1' + ); + assert.deepStrictEqual(res, [{id: 1, x: 3, y: 1, z: 2}]); + }); +});