diff --git a/src/engine/runcontext.js b/src/engine/runcontext.js index 57a7ec277d..f799b64ba0 100644 --- a/src/engine/runcontext.js +++ b/src/engine/runcontext.js @@ -9,5 +9,6 @@ export class RunContext { this.cb = cb; this.limit = undefined; this.offset = undefined; + this.top = undefined; } } diff --git a/src/engine/yy.js b/src/engine/yy.js index b5dd8ad27c..cb2008058c 100755 --- a/src/engine/yy.js +++ b/src/engine/yy.js @@ -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({ diff --git a/src/select/queryfn.js b/src/select/queryfn.js index ba82d59abc..6c7da3753f 100755 --- a/src/select/queryfn.js +++ b/src/select/queryfn.js @@ -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 @@ -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; diff --git a/src/select/select.js b/src/select/select.js index 0d2a86a2dd..89dfd440e8 100755 --- a/src/select/select.js +++ b/src/select/select.js @@ -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 { @@ -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; } @@ -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, @@ -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, diff --git a/test/test2712.js b/test/test2712.js index bcb473f12b..aef26385ec 100644 --- a/test/test2712.js +++ b/test/test2712.js @@ -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]] ); }); diff --git a/test/test3196.js b/test/test3196.js new file mode 100644 index 0000000000..7ac89ef771 --- /dev/null +++ b/test/test3196.js @@ -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]] + ); + }); +});