Changeset: aae3f55c78e2 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/aae3f55c78e2
Modified Files:
        sql/backends/monet5/rel_bin.c
        sql/server/rel_optimize_proj.c
        sql/server/sql_parser.y
Branch: Mar2025
Log Message:

handle ALL/DISTINCT with order by / with in group, ordered aggregation


diffs (99 lines):

diff --git a/sql/backends/monet5/rel_bin.c b/sql/backends/monet5/rel_bin.c
--- a/sql/backends/monet5/rel_bin.c
+++ b/sql/backends/monet5/rel_bin.c
@@ -1735,6 +1735,7 @@ exp_bin(backend *be, sql_exp *e, stmt *l
                if (attr && attr->h) {
                        node *en;
                        list *l = sa_list(sql->sa);
+                       stmt *next = NULL;
 
                        for (en = attr->h; en; en = en->next) {
                                sql_exp *at = en->data;
@@ -1756,7 +1757,7 @@ exp_bin(backend *be, sql_exp *e, stmt *l
                        if (need_distinct(e) && (grp || list_length(l) > 1)){
                                list *nl = sa_list(sql->sa);
                                stmt *ngrp = grp;
-                               stmt *next = ext;
+                               next = ext;
                                stmt *ncnt = cnt;
                                if (nl == NULL)
                                        return NULL;
@@ -1799,6 +1800,10 @@ exp_bin(backend *be, sql_exp *e, stmt *l
                                        for (node *n = obe->h; n; n = n->next) {
                                                sql_exp *oe = n->data;
                                                stmt *os = exp_bin(be, oe, 
left, right, NULL, NULL, NULL, sel, depth+1, 0, push);
+                                               if (!os)
+                                                       return NULL;
+                                               if (next)
+                                                       os = stmt_project(be, 
next, os);
                                                if (orderby)
                                                        orderby = 
stmt_reorder(be, os, is_ascending(oe), nulls_last(oe), orderby_ids, 
orderby_grp);
                                                else
diff --git a/sql/server/rel_optimize_proj.c b/sql/server/rel_optimize_proj.c
--- a/sql/server/rel_optimize_proj.c
+++ b/sql/server/rel_optimize_proj.c
@@ -2807,6 +2807,8 @@ rel_groupby_distinct(visitor *v, sql_rel
                        if (need_distinct(e)) {
                                distinct = n->data;
                                nr++;
+                               if (e->r) /* distinct and order by */
+                                       nr++;
                        }
                        anr += is_aggr(e->type);
                }
diff --git a/sql/server/sql_parser.y b/sql/server/sql_parser.y
--- a/sql/server/sql_parser.y
+++ b/sql/server/sql_parser.y
@@ -594,6 +594,7 @@ int yydebug=1;
        opt_best_effort
        opt_brackets
        opt_chain
+       all_distinct
        opt_distinct
        opt_escape
        opt_grant_for
@@ -3424,6 +3425,12 @@ opt_distinct:
  |  DISTINCT           { $$ = TRUE; }
  ;
 
+all_distinct:
+    ALL                        { $$ = FALSE; }
+ |  DISTINCT           { $$ = TRUE; }
+ ;
+
+
 assignment_commalist:
     assignment         { $$ = append_symbol(L(), $1 ); }
  |  assignment_commalist ',' assignment
@@ -5106,20 +5113,20 @@ aggr_or_window_ref:
                  else
                        append_symbol(l, $6);
                  $$ = _symbol_create_list( SQL_NOP, l ); }
- |  qfunc '(' DISTINCT expr_list ')'
+ |  qfunc '(' all_distinct expr_list opt_order_by_clause ')' opt_within_group
                { dlist *l = L();
                  append_list(l, $1);
-                 append_int(l, TRUE);
+                 append_int(l, $3);
+                 if ($5 && $7) {
+                       yyerror(m, "Cannot have both order by clause and within 
group clause");
+                       YYABORT;
+                 }
                  append_list(l, $4);
-                 $$ = _symbol_create_list( SQL_NOP, l );
-               }
- |  qfunc '(' ALL expr_list ')'
-               { dlist *l = L();
-                 append_list(l, $1);
-                 append_int(l, FALSE);
-                 append_list(l, $4);
-                 $$ = _symbol_create_list( SQL_NOP, l );
-               }
+                 if ($5)
+                       append_symbol(l, $5);
+                 else
+                       append_symbol(l, $7);
+                 $$ = _symbol_create_list( SQL_NOP, l ); }
  |  XML_aggregate
  ;
 
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to