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
1 change: 1 addition & 0 deletions src/engine/runcontext.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export class RunContext {
this.cb = cb;
this.limit = undefined;
this.offset = undefined;
this.top = undefined;
}
}
7 changes: 4 additions & 3 deletions src/engine/yy.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ export default function install(alasql) {
// SELECT * FROM (part), and takes over what belongs to the statement.
headOfPart(part, rest, parenthesised) {
var keepsRows = part.limit || setOperationOf(part) || (parenthesised && part.top);
// 4.x rule, removed in a later breaking step: an ORDER BY alone in the first part, and
// a TOP in a first part without parentheses, are for the whole result when no other
// part has an ORDER BY, LIMIT or TOP
// 4.x rule, removed in a later breaking step: an ORDER BY alone in the first part is
// for the whole result when no other part has an ORDER BY, LIMIT or TOP. A TOP in a
// first part without parentheses follows the same shape; Select.compile decides if it
// limits the part or the whole result (#3196)
if (!keepsRows && !((part.order || part.top) && ordersOrLimits(rest))) return part;

var head = new this.Select({
Expand Down
17 changes: 17 additions & 0 deletions src/select/queryfn.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ export default function install(alasql, internal) {
// Remove distinct values
doDistinct(query);

// The TOP of the first SELECT of a set operation limits that SELECT before its rows
// are combined. A TOP with the ORDER BY of the statement stays in query.limit and is
// applied after the set operation, as in 4.x.
doTop(query);

// UNION / UNION ALL
if (query.unionallfn) {
// TODO Simplify this part of program
Expand Down Expand Up @@ -476,6 +481,18 @@ export default function install(alasql, internal) {
return res;
}

// The TOP of one part of a set operation
function doTop(query) {
var top = typeof query.topParam === 'undefined' ? query.top : query.ctx.top;
if (top) {
var end = query.percent ? ((query.data.length * top) / 100) | 0 : top | 0;
query.data = query.data.slice(0, end);
}
if (typeof query.topParam !== 'undefined' || typeof query.top !== 'undefined') {
query.percent = undefined;
}
}

// Limiting
function doLimit(query) {
var limit = typeof query.limitParam === 'undefined' ? query.limit : query.ctx.limit;
Expand Down
16 changes: 13 additions & 3 deletions src/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,18 @@ export default function install(alasql, internal) {
if (this.unpivot) query.pivotfn = this.compileUnpivot(query);

// 10. Compile TOP/LIMIT/OFFSET/FETCH clause
var hasSetOperation = this.union || this.unionall || this.except || this.intersect;
var topOfPart = hasSetOperation && !this.order;
if (this.top) {
if (this.top instanceof yy.ParamValue) {
query.limitParam = this.top.param;
if (topOfPart) query.topParam = this.top.param;
else query.limitParam = this.top.param;
} else {
query.limit = this.top.value;
if (topOfPart) query.top = this.top.value;
else query.limit = this.top.value;
}
} else if (this.limit) {
}
if (this.limit && (!this.top || topOfPart)) {
if (this.limit instanceof yy.ParamValue) {
query.limitParam = this.limit.param;
} else {
Expand Down Expand Up @@ -501,6 +506,9 @@ export default function install(alasql, internal) {
if (typeof run.limitParam !== 'undefined') {
run.ctx.limit = params ? params[run.limitParam] : undefined;
}
if (typeof run.topParam !== 'undefined') {
run.ctx.top = params ? params[run.topParam] : undefined;
}
if (typeof run.offsetParam !== 'undefined') {
run.ctx.offset = params ? params[run.offsetParam] : undefined;
}
Expand Down Expand Up @@ -832,6 +840,7 @@ export default function install(alasql, internal) {
selectgfn: q.selectgfn,
corresponding: q.corresponding,
limit: q.limit,
top: q.top,
havingfns: q.havingfns,
pivotfn: q.pivotfn,
unionallfn: q.unionallfn,
Expand All @@ -846,6 +855,7 @@ export default function install(alasql, internal) {
intofn: q.intofn,
intoallfn: q.intoallfn,
limitParam: q.limitParam,
topParam: q.topParam,
preserveArrayResult: q.preserveArrayResult,
offsetParam: q.offsetParam,
ctx: q.ctx,
Expand Down
3 changes: 1 addition & 2 deletions test/test2712.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,10 @@ describe(`Test ${testId} - ORDER BY and LIMIT of each part of a set operation`,
column('SELECT 1 AS a INTERSECT SELECT 1 UNION ALL SELECT 2'),
column('SELECT a FROM A UNION ALL (SELECT a FROM B ORDER BY a LIMIT 1)'),
column('(SELECT a FROM A ORDER BY a) ORDER BY a DESC'),
column('SELECT TOP 2 a FROM A UNION ALL SELECT a FROM C'),
// An ORDER BY alone in the first part sorts the whole result. SQLite gives 1, 2, 3, 5, 4
column('(SELECT a FROM A ORDER BY a) UNION ALL SELECT a FROM C'),
],
[[1], [3, 1, 2, 2], [3, 2, 1], [3, 1], [1, 2, 3, 4, 5]]
[[1], [3, 1, 2, 2], [3, 2, 1], [1, 2, 3, 4, 5]]
);
});

Expand Down
98 changes: 98 additions & 0 deletions test/test3196.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
if (typeof exports === 'object') {
var assert = require('assert');
var alasql = require('..');
}

let testId = '3196';

describe(`Test ${testId} - TOP of a SELECT in a set operation`, function () {
before(function () {
alasql('create database test' + testId);
alasql('use test' + testId);
alasql('CREATE TABLE A (a INT); INSERT INTO A VALUES (3),(1),(2)');
alasql('CREATE TABLE B (a INT); INSERT INTO B VALUES (9),(8),(2)');
alasql('CREATE TABLE C (a INT); INSERT INTO C VALUES (5),(4)');
alasql('CREATE TABLE D (a INT); INSERT INTO D VALUES (3)');
alasql('CREATE TABLE E (a INT); INSERT INTO E VALUES (1),(2)');
});

after(function () {
alasql('drop database test' + testId);
});

function rows(sql, params) {
const statement = alasql.compile(sql);
const first = statement(params);
assert.deepStrictEqual(statement(params), first, 'second run of ' + sql);
return first;
}

function column(sql, params) {
return rows(sql, params).map(row => row.a);
}

it('A) The rows of the issue', function () {
assert.deepStrictEqual(
[
column('SELECT TOP 1 a FROM A UNION ALL SELECT a FROM B'),
column('SELECT a FROM A UNION ALL SELECT TOP 1 a FROM C UNION ALL SELECT a FROM B'),
column('SELECT TOP 1 a FROM A UNION ALL SELECT TOP 1 a FROM B'),
],
[
[3, 9, 8, 2],
[3, 1, 2, 5, 9, 8, 2],
[3, 9],
]
);
});

it('B) TOP of the first part in UNION, EXCEPT and INTERSECT', function () {
assert.deepStrictEqual(
[
column('SELECT TOP 2 a FROM A UNION SELECT a FROM B'),
column('SELECT TOP 2 a FROM A EXCEPT SELECT a FROM D'),
column('SELECT TOP 2 a FROM A INTERSECT SELECT a FROM E'),
],
[[9, 8, 2, 3, 1], [1], [1]]
);
});

it('C) TOP PERCENT and parameterized TOP belong to their own SELECT', function () {
assert.deepStrictEqual(
[
column('SELECT TOP 50 PERCENT a FROM A UNION ALL SELECT a FROM B'),
column('SELECT TOP ? a FROM A UNION ALL SELECT a FROM B', [1]),
column('SELECT TOP ($lim) a FROM A UNION ALL SELECT a FROM B', {lim: 1}),
],
[
[3, 9, 8, 2],
[3, 9, 8, 2],
[3, 9, 8, 2],
]
);
});

it('D) A trailing LIMIT or FETCH still applies to the whole set operation', function () {
assert.deepStrictEqual(
[
column('SELECT a FROM A UNION ALL SELECT a FROM B LIMIT 1'),
column('SELECT TOP 1 a FROM A UNION ALL SELECT a FROM B LIMIT 1'),
column('SELECT TOP 50 PERCENT a FROM A UNION ALL SELECT a FROM B LIMIT 2'),
column(
'SELECT TOP 1 a FROM A UNION ALL SELECT a FROM B OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY'
),
],
[[3], [3], [3, 9], [9, 8]]
);
});

it('E) A final ORDER BY keeps the existing whole-result TOP behaviour', function () {
assert.deepStrictEqual(
[
column('SELECT a FROM A UNION ALL SELECT a FROM B ORDER BY a'),
column('SELECT TOP 1 a FROM A UNION ALL SELECT a FROM B ORDER BY a'),
],
[[1, 2, 2, 3, 8, 9], [1]]
);
});
});