Skip to content
Open
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
33 changes: 21 additions & 12 deletions src/select/columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
);
});
}
};
}
6 changes: 4 additions & 2 deletions src/select/join.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'};
Expand Down
15 changes: 15 additions & 0 deletions src/select/pivot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand Down
57 changes: 57 additions & 0 deletions test/test3194.js
Original file line number Diff line number Diff line change
@@ -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}]);
});
});