fanfuxiaoran commented on code in PR #685:
URL: https://github.com/apache/cloudberry/pull/685#discussion_r1858194701
##########
src/backend/optimizer/plan/transform.c:
##########
@@ -520,3 +521,134 @@ replace_sirvf_rte(Query *query, RangeTblEntry *rte)
return rte;
}
+
+/*
+ * Does query has SRFs, or WITH ORDINALITY?
+ */
+bool query_has_srf(Query *query)
+{
+ if (query->hasTargetSRFs)
+ {
+ return true;
+ }
+
+ /* Double check for subquery. */
+ if (expression_returns_set( (Node *) query->targetList))
+ {
+ return true;
+ }
+
+ ListCell *lcrte = NULL;
+ foreach (lcrte, query->rtable)
+ {
+ RangeTblEntry *rte = (RangeTblEntry *) lfirst(lcrte);
+
+ switch(rte->rtekind)
+ {
+ case RTE_FUNCTION:
+ {
+ ListCell *lcrtfunc;
+
+ /* WITH ORDINALITY */
+ if (rte->funcordinality)
+ return true;
+
+ foreach(lcrtfunc, rte->functions)
+ {
+ RangeTblFunction *rtfunc =
(RangeTblFunction *) lfirst(lcrtfunc);
+
+ if (!IsA(rtfunc->funcexpr, FuncExpr))
+ return true;
+
+ if (((FuncExpr *)
rtfunc->funcexpr)->funcretset)
+ {
+ /* SRF in FROM clause */
+ return true;
+ }
+ }
+ break;
+ }
+ case RTE_SUBQUERY:
+ {
+ Query *sq = (Query *) rte->subquery;
+
+ if (query_has_srf(sq))
+ {
+ return true;
+ }
+ break;
+ }
+ default:
+ {
+ break;
+ }
+ }
+ }
+
+ return false;
+}
+
+/*
+ * DISTINCT/DISTINCT ON/ORDER BY optimization.
+ * Remove DISTINCT clause if possibile, ex:
+ * select DISTINCT count(a) from t; to
+ * select count(a) from t;
+ * There is one row returned at most, DISTINCT and/or ON is pointless then.
+ * The same with ORDER BY clause;
+ */
+Query *remove_distinct_sort_clause(Query *parse)
+{
+ if (parse->hasAggs &&
+ parse->groupClause == NIL &&
+ !contain_mutable_functions((Node *) parse) &&
+ !query_has_srf(parse))
+ {
+ List *useless_tlist = NIL;
+ List *tles;
+ List *sortops;
+ List *eqops;
+ ListCell *lc;
+
+ if (parse->distinctClause != NIL)
Review Comment:
Hmm... If I understand correctly, `query_has_srf` doesn't need to care about
subquery or child nodes, it only need to check if the targetlist contains any
srf. (the targetlis which contains distinct expression).
But for `remove_distinct_sort_clause`, seems it only process the topmost
targetlist, subqueries are not processed
such as :
```
select * from t, (select distinct(count(*) from t);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]