You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudberry.apache.org by "avamingli (via GitHub)" <gi...@apache.org> on 2024/12/03 09:53:21 UTC

Re: [PR] Optimize DISTINCT, ORDER BY and DISTINCT ON when Aggregation without Group By. [cloudberry]

avamingli commented on code in PR #685:
URL: https://github.com/apache/cloudberry/pull/685#discussion_r1867398373


##########
src/backend/optimizer/plan/transform.c:
##########
@@ -520,3 +521,124 @@ 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)

Review Comment:
   Fixed, as well as JOIN of SRFs:
   
   ```sql
   explain(verbose, costs off)
   select distinct(count(a)) from generate_series(0, 1) as a;
                                                           QUERY PLAN                                                         
   ---------------------------------------------------------------------------------------------------------------------------
    Aggregate
      Output: count(a)
      ->  Function Scan on pg_catalog.generate_series a
            Output: a
            Function Call: generate_series(0, 1)
    Settings: enable_hashagg = 'on', enable_sort = 'on', gp_statistics_pullup_from_child_partition = 'off', optimizer = 'off'
    Optimizer: Postgres query optimizer
   (7 rows)
   
   explain(verbose, costs off)
   select distinct(count(*)) from generate_series(0, 1) a join generate_series(0, 2) b on true;
                                                           QUERY PLAN                                                         
   ---------------------------------------------------------------------------------------------------------------------------
    Aggregate
      Output: count(*)
      ->  Nested Loop
            ->  Function Scan on pg_catalog.generate_series a
                  Output: a.a
                  Function Call: generate_series(0, 1)
            ->  Function Scan on pg_catalog.generate_series b
                  Output: b.b
                  Function Call: generate_series(0, 2)
    Settings: enable_hashagg = 'on', enable_sort = 'on', gp_statistics_pullup_from_child_partition = 'off', optimizer = 'off'
    Optimizer: Postgres query optimizer
   (11 rows)
   
   ```



-- 
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: commits-unsubscribe@cloudberry.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org