Fixed.
Yes, kind of weird. Why not though?
Fixed, anyways.
Diff:
diff --git a/src/box/sql/select.c b/src/box/sql/select.c
index 54f78a9..1a6b034 100644
--- a/src/box/sql/select.c
+++ b/src/box/sql/select.c
@@ -3893,14 +3893,14 @@ flattenSubquery(Parse * pParse, /* Parsing context */
* queries.
*/
if (pSub->pPrior) {
- if (pSub->pOrderBy) {
- return 0; /* Restriction 20 */
- }
if (isAgg || (p->selFlags & SF_Distinct) != 0
|| pSrc->nSrc != 1) {
return 0;
}
for (pSub1 = pSub; pSub1; pSub1 = pSub1->pPrior) {
+ /* Restriction 20 */
+ if (pSub1->pOrderBy != NULL)
+ return 0;
testcase((pSub1->
selFlags & (SF_Distinct | SF_Aggregate)) ==
SF_Distinct);
diff --git a/test/sql-tap/subquery.test.lua b/test/sql-tap/subquery.test.lua
index 06631c1..8fde62d 100755
--- a/test/sql-tap/subquery.test.lua
+++ b/test/sql-tap/subquery.test.lua
@@ -1,6 +1,6 @@
#!/usr/bin/env tarantool
test = require("sqltester")
-test:plan(69)
+test:plan(73)
--!./tcltestrunner.lua
-- 2005 January 19
@@ -926,6 +926,52 @@ test:do_execsql_test(
-- </subquery-8.1>
})
+test:do_catchsql_test(
+ "subquery-9.0",
+ [[
+ DROP TABLE IF EXISTS table1;
+ CREATE TABLE table1 (id VARCHAR(100) PRIMARY KEY);
+ INSERT INTO table1 VALUES ('abc'), ('abd');
+ ]], {
+ -- <subquery-9.0>
+ 0
+ -- <subquery-9.0>
+ })
+
+test:do_catchsql_test(
+ "subquery-9.1",
+ [[
+ SELECT * FROM (SELECT * FROM table1 UNION ALL
+ SELECT * FROM table1 ORDER BY 1 UNION ALL
+ SELECT * FROM table1);
+ ]], {
+ -- <subquery-9.1>
+ 1, 'ORDER BY clause should come after UNION ALL not before'
+ -- <subquery-9.1>
+ })
+
+test:do_execsql_test(
+ "subquery-9.2",
+ [[
+ SELECT * FROM (SELECT * FROM table1 UNION ALL
+ SELECT * FROM table1 UNION ALL
+ SELECT * FROM table1 ORDER BY 1);
+ ]], {
+ -- <subquery-9.2>
+ 'abc', 'abc', 'abc', 'abd', 'abd', 'abd'
+ -- <subquery-9.2>
+ })
+test:do_catchsql_test(
+ "subquery-9.3",
+ [[
+ SELECT * FROM (SELECT * FROM table1 ORDER BY 1 UNION ALL
+ SELECT * FROM table1 UNION ALL
+ SELECT * FROM table1);
+ ]], {
+ -- <subquery-9.3>
+ 1, 'ORDER BY clause should come after UNION ALL not before'
+ -- <subquery-9.3>
+ })
test:finish_test()