From 4acbea9c4eb7b7abfc6b2b9929f5ef9198e98b67 Mon Sep 17 00:00:00 2001 From: Georgy Moshkin <gmoshkin@picodata.io> Date: Fri, 21 Feb 2025 18:55:47 +0300 Subject: [PATCH] fix: fix a problem with using window functions in compound (UNION, INTERSECT etc.) queries. Cherry-picked from sqlite 0f5f54062cf943b0506018761b3ee5c6d0e842da NO_DOC=internal NO_CHANGELOG=internal --- src/box/sql/window.c | 2 +- test/sql-tap/window1.test.lua | 70 ++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/box/sql/window.c b/src/box/sql/window.c index 9e2bd5236c..629a5d2074 100644 --- a/src/box/sql/window.c +++ b/src/box/sql/window.c @@ -358,7 +358,7 @@ int sqlWindowRewrite(Parse *pParse, Select *p) { int rc = 0; - if (p->pWin) { + if (p->pWin && p->pPrior == 0) { Vdbe *v = sqlGetVdbe(pParse); /* The subquery */ Select *pSub = 0; diff --git a/test/sql-tap/window1.test.lua b/test/sql-tap/window1.test.lua index 2575f4cb5b..145c3a7487 100755 --- a/test/sql-tap/window1.test.lua +++ b/test/sql-tap/window1.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(59) +test:plan(66) test:execsql( [[ DROP TABLE IF EXISTS t1; @@ -750,4 +750,72 @@ test:do_execsql_test( '1', '1.5', '2.5', '3.5' }) +-- NOTE(gmoshkin): had to change query in test, because tarantool requires a +-- primary key to be specified for all tables +test:do_execsql_test( + "window1-13.1", + [[ + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a int primary key, b int); + INSERT INTO t1 VALUES(1,11); + INSERT INTO t1 VALUES(2,12); + ]] +) + +-- NOTE(gmoshkin): had to split this test into 2, because tarantool doesn't +-- support multiple queries in one call to box.execute +test:do_execsql_test( + "window1-13.2.1.1", + [[ + SELECT a, row_number() OVER(ORDER BY b) FROM t1; + ]], + { 1, 1, 2, 2, } +) +test:do_execsql_test( + "window1-13.2.1.2", + [[ + SELECT a, row_number() OVER(ORDER BY b DESC) FROM t1; + ]], + { 2, 1, 1, 2, } +) + +test:do_execsql_test( + "window1-13.2.2", + [[ + SELECT a, row_number() OVER(ORDER BY b) FROM t1 + UNION ALL + SELECT a, row_number() OVER(ORDER BY b DESC) FROM t1; + ]], + { 1, 1, 2, 2, 2, 1, 1, 2, } +) +test:do_execsql_test( + "window1-13.3", + [[ + SELECT a, row_number() OVER(ORDER BY b) FROM t1 + UNION + SELECT a, row_number() OVER(ORDER BY b DESC) FROM t1; + ]], + { 1, 1, 1, 2, 2, 1, 2, 2, } +) + +test:do_execsql_test( + "window1-13.4", + [[ + SELECT a, row_number() OVER(ORDER BY b) FROM t1 + EXCEPT + SELECT a, row_number() OVER(ORDER BY b DESC) FROM t1; + ]], + { 1, 1, 2, 2, } +) + +test:do_execsql_test( + "window1-13.5", + [[ + SELECT a, row_number() OVER(ORDER BY b) FROM t1 + INTERSECT + SELECT a, row_number() OVER(ORDER BY b DESC) FROM t1; + ]], + {} +) + test:finish_test() -- GitLab