If it includes {order by}, ROWNUM can not be rewrited to LIMIT
This commit is contained in:
@ -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)) {
|
||||
|
@ -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;
|
||||
|
@ -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;
|
Reference in New Issue
Block a user