From ad332429c580f1c0975f87f13f9bebe7332d6a60 Mon Sep 17 00:00:00 2001 From: Sanket Patel Date: Wed, 2 Dec 2026 15:04:21 +0000 Subject: [PATCH 1/3] test: cover TOP scoping in set operations --- test/test3196.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 test/test3196.js diff --git a/test/test3196.js b/test/test3196.js new file mode 100644 index 0000000000..255826f4da --- /dev/null +++ b/test/test3196.js @@ -0,0 +1,94 @@ +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)'); + }); + + 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 1 a FROM A EXCEPT SELECT a FROM D'), + column('SELECT TOP 1 a FROM A INTERSECT SELECT a FROM E'), + ], + [ + [3, 1, 9, 8, 2], + [], + [], + ] + ); + }); + + 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 TOP 1 a FROM A UNION ALL SELECT a FROM B LIMIT 1'), + column( + 'SELECT TOP 1 a FROM A UNION ALL SELECT a FROM B OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY' + ), + ], + [[3], [9, 8]] + ); + }); + + it('E) A final ORDER BY keeps the existing whole-result TOP behaviour', function () { + assert.deepStrictEqual(column('SELECT TOP 1 a FROM A UNION ALL SELECT a FROM B ORDER BY a'), [1]); + }); +}); From a4423dca44fa125eeb7f73471bf67d140386f258 Mon Sep 17 00:00:00 2001 From: Sanket Patel Date: Wed, 2 Dec 2026 15:18:34 +0000 Subject: [PATCH 2/3] fix: scope TOP to its SELECT in set operations --- src/engine/runcontext.js | 1 + src/select/queryfn.js | 14 ++++++++++++++ src/select/select.js | 16 +++++++++++++--- test/test2712.js | 3 +-- test/test3196.js | 23 +++++++++++++---------- 5 files changed, 42 insertions(+), 15 deletions(-) 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/select/queryfn.js b/src/select/queryfn.js index ba82d59abc..dfe9ebd26e 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,15 @@ 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); + } + } + // 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 index 255826f4da..722e856666 100644 --- a/test/test3196.js +++ b/test/test3196.js @@ -13,7 +13,7 @@ describe(`Test ${testId} - TOP of a SELECT in a set operation`, function () { 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)'); + alasql('CREATE TABLE E (a INT); INSERT INTO E VALUES (1),(2)'); }); after(function () { @@ -50,14 +50,10 @@ describe(`Test ${testId} - TOP of a SELECT in a set operation`, function () { assert.deepStrictEqual( [ column('SELECT TOP 2 a FROM A UNION SELECT a FROM B'), - column('SELECT TOP 1 a FROM A EXCEPT SELECT a FROM D'), - column('SELECT TOP 1 a FROM A INTERSECT SELECT a FROM E'), + column('SELECT TOP 2 a FROM A EXCEPT SELECT a FROM D'), + column('SELECT TOP 2 a FROM A INTERSECT SELECT a FROM E'), ], - [ - [3, 1, 9, 8, 2], - [], - [], - ] + [[9, 8, 2, 3, 1], [1], [1]] ); }); @@ -79,16 +75,23 @@ describe(`Test ${testId} - TOP of a SELECT in a set operation`, function () { 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 1 a FROM A UNION ALL SELECT a FROM B OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY' ), ], - [[3], [9, 8]] + [[3], [3], [9, 8]] ); }); it('E) A final ORDER BY keeps the existing whole-result TOP behaviour', function () { - assert.deepStrictEqual(column('SELECT TOP 1 a FROM A UNION ALL SELECT a FROM B ORDER BY a'), [1]); + 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]] + ); }); }); From 2cdb0208c36caf566af25e1d936478ccf8a5efa2 Mon Sep 17 00:00:00 2001 From: Sanket Patel Date: Wed, 2 Dec 2026 15:20:46 +0000 Subject: [PATCH 3/3] fix: keep TOP PERCENT scoped with its SELECT --- src/engine/yy.js | 7 ++++--- src/select/queryfn.js | 3 +++ test/test3196.js | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) 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 dfe9ebd26e..6c7da3753f 100755 --- a/src/select/queryfn.js +++ b/src/select/queryfn.js @@ -488,6 +488,9 @@ export default function install(alasql, internal) { 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 diff --git a/test/test3196.js b/test/test3196.js index 722e856666..7ac89ef771 100644 --- a/test/test3196.js +++ b/test/test3196.js @@ -77,11 +77,12 @@ describe(`Test ${testId} - TOP of a SELECT in a set operation`, function () { [ 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], [9, 8]] + [[3], [3], [3, 9], [9, 8]] ); });