From b62cc145a0562ea1016ef26d346920ccf01d7ff0 Mon Sep 17 00:00:00 2001 From: zhouxiongjia <719216473@qq.com> Date: Tue, 15 Jun 2021 20:52:44 +0800 Subject: [PATCH] fix two bugs about ROWNUM: 1. ROWNUM behaves incorrect in the partition table 2. if there is aggregate function, ROWNUM can not be rewrite to LIMIT --- src/gausskernel/optimizer/prep/preprownum.cpp | 4 ++-- src/gausskernel/runtime/executor/execAmi.cpp | 4 +++- src/gausskernel/runtime/executor/nodePartIterator.cpp | 1 + src/include/nodes/execnodes.h | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/gausskernel/optimizer/prep/preprownum.cpp b/src/gausskernel/optimizer/prep/preprownum.cpp index 431f7332b..cb3b82c59 100644 --- a/src/gausskernel/optimizer/prep/preprownum.cpp +++ b/src/gausskernel/optimizer/prep/preprownum.cpp @@ -51,8 +51,8 @@ void preprocess_rownum(PlannerInfo *root, Query *parse) if (quals == NULL) { return; } - /* If it includes {order by} or {group by}, can not be rewrited */ - if ((parse->sortClause != NULL) || (parse->groupClause != NULL)) { + /* If it includes aggregation function or {order by} or {group by}, can not be rewrited */ + if (parse->hasAggs || (parse->sortClause != NULL) || (parse->groupClause != NULL)) { return; } if (parse->limitCount != NULL) { diff --git a/src/gausskernel/runtime/executor/execAmi.cpp b/src/gausskernel/runtime/executor/execAmi.cpp index 5d0e4aaa8..12e7d0257 100755 --- a/src/gausskernel/runtime/executor/execAmi.cpp +++ b/src/gausskernel/runtime/executor/execAmi.cpp @@ -270,7 +270,9 @@ void ExecReScan(PlanState* node) } /* reset the rownum */ - node->ps_rownum = 0; + if (!node->do_not_reset_rownum) { + node->ps_rownum = 0; + } /* * If we have changed parameters, propagate that info. diff --git a/src/gausskernel/runtime/executor/nodePartIterator.cpp b/src/gausskernel/runtime/executor/nodePartIterator.cpp index 85a1260c8..158bfc77e 100755 --- a/src/gausskernel/runtime/executor/nodePartIterator.cpp +++ b/src/gausskernel/runtime/executor/nodePartIterator.cpp @@ -98,6 +98,7 @@ TupleTableSlot* ExecPartIterator(PartIteratorState* node) TupleTableSlot* slot = NULL; PartIterator* pi_node = (PartIterator*)node->ps.plan; EState* state = node->ps.lefttree->state; + node->ps.lefttree->do_not_reset_rownum = true; bool orig_early_free = state->es_skip_early_free; PlanState* noden = (PlanState*)node->ps.lefttree; diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index c8602d3ef..fbdef251c 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -1234,7 +1234,7 @@ typedef struct PlanState { List* plan_issues; bool recursive_reset; /* node already reset? */ bool qual_is_inited; - + bool do_not_reset_rownum; int64 ps_rownum; /* store current rownum */ } PlanState;