Update prepnonjointree.cpp

This commit is contained in:
XL_up 2023-09-19 21:55:00 +08:00
parent 304749b6ef
commit 363bbba83a
1 changed files with 19 additions and 24 deletions

View File

@ -2124,24 +2124,24 @@ static void reduce_orderby_recurse(Query* query, Node* jtnode, bool reduce);
/*
* reduce_orderby_recurse() ---
* Recurse in a jointree or a setop tree and try to reduce NULL orderby-clause.
* Recurse in a jointree or a setop tree and try to reduce redundant orderby clauses.
*
* @ Caller: reduce_orderby()
*
* @ Param [IN] query: current node of query tree.
* @ Param [IN] jtnode: jointree node for RangeTblRef/FromExpr/JoinExpr or setop tree
* node for SetOperationStmt.
* @ Param [IN] reduce: the flag identify that runs through the recursing
* @ Param [IN] reduce: the flag identifier that runs through the recursing
* process that decide whether to reduce the orderby clause for subquery.
* @ Returns: void
*
* Note: this routine is the main recursive procedure through the orderby reducing
* process. It will recursively traverses a jointree or a setop tree in an attempt
* to drop NULL-returns orderby for it.
* to drop redundant orderby clauses for it.
*/
static void reduce_orderby_recurse(Query* query, Node* jtnode, bool reduce)
{
/* query isn't in a subquery, JOIN-structure or an aggregation. */
/* query isn't in a from-where clause, join-structure or an aggregation. */
if (jtnode == NULL)
return;
@ -2150,7 +2150,7 @@ static void reduce_orderby_recurse(Query* query, Node* jtnode, bool reduce)
int varno = ((RangeTblRef*)jtnode)->rtindex;
RangeTblEntry* rte = rt_fetch(varno, query->rtable);
/* Reduce sort procedure for this RTE. */
/* Try to drop orderby-clause on this RTE. */
reduce_orderby_final(rte, reduce);
}else if (IsA(jtnode, FromExpr)) {
@ -2171,18 +2171,17 @@ static void reduce_orderby_recurse(Query* query, Node* jtnode, bool reduce)
bool flag = false;
/*
* If the number of tables referenced by the from statement is 1, then there are two
* situations: If the from-clause is at the top of the query, we do not need to drop
* its orderby, even if the orderby-clause returns NULL; Otherwise, it indicates that
* the query refers to more than one table, at which point we can try to drop the
* orderby-clause of the only table referenced by the current from-clause.
* If the number of tables referenced by the from-where clause is 1, then there are two
* situations: If the from-clause is at the top of the query, then we don't need to drop
* this orderby; Otherwise the query refers to more than one table, at which point we
* can try to drop the orderby-clause of the tables referenced by current from-clause.
*/
if (1 == list_length(f->fromlist))
flag = reduce;
else
flag = true;
/* Recurse into each reference of query to reduce its NULL orderby. */
/* Recurse into each reference of from-clause to reduce its orderby. */
foreach (l, f->fromlist)
reduce_orderby_recurse(query, (Node*)lfirst(l), flag);
} else if (IsA(jtnode, JoinExpr)) {
@ -2216,7 +2215,7 @@ static void reduce_orderby_recurse(Query* query, Node* jtnode, bool reduce)
/*
* reduce_orderby_final() ---
* Reduce orderby clause for subquery if it has a NULL-returns orderby.
* Reduce orderby clause for subquery if it has a removable sortClause.
*
* @ Caller: reduce_orderby_recurse()
*
@ -2224,17 +2223,13 @@ static void reduce_orderby_recurse(Query* query, Node* jtnode, bool reduce)
* @ Param [IN] reduce: the flag identify that runs through the recursing
* process that decide whether to reduce the orderby clause for subquery.
* @ Returns: void
*
* Note: if the subquery has NULL-returns orderby and the passed-in parameter
* reduce is true, drop this orderby clause for the subquery.
*/
static void reduce_orderby_final(RangeTblEntry* rte, bool reduce)
{
/*
* Both subquery's limitOffset and limitCount are zero, which shows
* that this orderby actually doesn't return any item as the result
* of a query or used in join operation.
* Both subquery's limitOffset and limitCount are NULL, which shows that
* the orderby-clause on this RTE does not use LIMIT-clause to specify
* a subset of the query results.
*/
if (rte->rtekind == RTE_SUBQUERY) {
if (reduce && rte->subquery->sortClause && !rte->subquery->limitOffset && !rte->subquery->limitCount) {
@ -2257,7 +2252,7 @@ static void reduce_orderby_final(RangeTblEntry* rte, bool reduce)
* @ Caller: subquery_planner()
*
* @ Param [IN] query: current node of query tree.
* @ Param [IN] reduce: the flag identify that runs through the recursing
* @ Param [IN] reduce: the flag identifier that runs through the recursing
* process that decide whether to reduce the orderby clause for subquery.
* @ Returns: void
*
@ -2280,14 +2275,14 @@ void reduce_orderby(Query* query, bool reduce)
/*
* If subquery is not a SELECT, we should find SELECT query and deside
* whether to reduce order by in subquery or not.
* whether to reduce orderby in subquery or not.
*/
if (query->commandType != CMD_SELECT) {
foreach (l, query->rtable) {
rte = (RangeTblEntry*)lfirst(l);
/*
* Recurse into subqueries to locate orderby and decide
* whether to reduce it or not.
* whether to reduce it.
*/
if (rte->rtekind == RTE_SUBQUERY)
reduce_orderby(rte->subquery, reduce);
@ -2296,8 +2291,8 @@ void reduce_orderby(Query* query, bool reduce)
}
/*
* Recurse to find subquerys in FROM, JOIN or subqueries, and to decide
* whether to drop its orderby or not.
* Recurse to find subquerys in from-where clause, join clause or aggregation
* structure, and to decide whether to drop its orderby.
*/
reduce_orderby_recurse(query, (Node*)query->jointree, reduce);