avamingli commented on code in PR #685:
URL: https://github.com/apache/cloudberry/pull/685#discussion_r1867465748
##########
src/backend/optimizer/plan/transform.c:
##########
@@ -520,3 +522,87 @@ replace_sirvf_rte(Query *query, RangeTblEntry *rte)
return rte;
}
+
+/*
+ * Does target list have SRFs?
+ */
+static
+bool tlist_has_srf(Query *query)
+{
+ if (query->hasTargetSRFs)
+ {
+ return true;
+ }
+
+ if (expression_returns_set( (Node *) query->targetList))
+ {
+ return true;
+ }
+
+ 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) &&
+ !tlist_has_srf(parse))
+ {
+ List *useless_tlist = NIL;
+ List *tles;
+ List *sortops;
+ List *eqops;
+ ListCell *lc;
+
+ if (parse->distinctClause != NIL)
+ {
Review Comment:
It's intended, there may be either one or both, sometimes we could remove
clause but sometimes not, and process fields like hasDistinctOn and etc.
See codes for detail.
--
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]