If it includes {order by}, ROWNUM can not be rewrited to LIMIT

This commit is contained in:
zhouxiongjia
2020-10-13 14:36:05 +08:00
parent 399a957141
commit 916c0cfbe0
3 changed files with 18 additions and 1 deletions

View File

@ -2963,7 +2963,10 @@ void preprocess_rownum(PlannerInfo *root, Query *parse)
if (quals == NULL) {
return;
}
/* If it includes {order by}, can not be rewrited */
if (parse->sortClause != NULL) {
return;
}
if (parse->limitCount != NULL) {
parse->limitCount = eval_const_expressions(root, parse->limitCount);
if (!IsA(parse->limitCount, Const)) {

View File

@ -1632,7 +1632,18 @@ explain select * from student where not(rownum > 3 or id = 1);
-> Seq Scan on student (cost=0.00..24.18 rows=1123 width=42)
Filter: (id <> 1)
(3 rows)
-- ROWNUM with ORDER BY
explain select * from test where rownum < 5 order by 1;
QUERY PLAN
--------------------------------------------------------------
Sort (cost=40.36..41.30 rows=378 width=42)
Sort Key: id
-> Seq Scan on test (cost=0.00..24.18 rows=378 width=42)
Filter: (ROWNUM < 5)
(4 rows)
drop table student;
drop table test;

View File

@ -444,5 +444,8 @@ explain select * from student where not(rownum < 3 + 2);
explain select * from student where not(rownum < 3 and id = 1);
explain select * from student where not(rownum > 3 or id = 1);
-- ROWNUM with ORDER BY
explain select * from test where rownum < 5 order by 1;
drop table student;
drop table test;