Changeset: d1ce4c97c16f for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/d1ce4c97c16f
Modified Files:
        sql/server/rel_optimize_proj.c
        sql/test/rel-optimizers/Tests/const-aggr-elim.test
Branch: default
Log Message:

merged with Dec2025


diffs (177 lines):

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
@@ -2525,20 +2525,18 @@ rel_distinct_aggregate_on_unique_values(
 static inline sql_rel *
 rel_remove_const_aggr(visitor *v, sql_rel *rel)
 {
-       if(!rel) {
+       if (!rel)
                return rel;
-       }
 
        list *exps = rel->exps;
 
-       if(rel->op != op_groupby || list_empty(exps)) {
+       if (rel->op != op_groupby || list_empty(exps))
                return rel;
-       }
-
-       if(!list_empty(rel->r)) {
+
+       if (!list_empty(rel->r)) {
                /* in the general case in an expression of an aggregate over
                 * a constant can be rewritten as just the const e.g.
-                *   aggr(const) -> const
+                *   aggr(const) -> cast(const as restype)
                 */
 
                for(node *n = exps->h; n; n = n->next) {
@@ -2560,40 +2558,38 @@ rel_remove_const_aggr(visitor *v, sql_re
                                prd = strcmp(j->base.name, "prod") == 0,
                                cnt = strcmp(j->base.name, "count") == 0;
 
-                       if(!j->s && j->system == 1) {
+                       if (!j->s && j->system == 1) {
                                list *se = e->l;
 
                                if(se == NULL) {
                                        continue;
                                }
 
-                               for(node *m = se->h; m; m = m->next) {
+                               for (node *m = se->h; m; m = m->next) {
                                        sql_exp *w = m->data;
 
-                                       if(w->type == e_atom && w->card == 
CARD_ATOM) {
+                                       if (w->type == e_atom && w->card == 
CARD_ATOM) {
                                                atom *wa = w->l;
 
-                                               if(sum && !(wa->isnull || 
atom_is_zero(wa))) {
+                                               if (sum && !(wa->isnull || 
atom_is_zero(wa)))
                                                        continue;
-                                               }
-
-                                               if(prd && !(wa->isnull || 
atom_is_one(wa))) {
+
+                                               if (prd && !(wa->isnull || 
atom_is_one(wa)))
                                                        continue;
-                                               }
-
-                                               if(cnt) {
-                                                       if(wa->isnull) {
+
+                                               if (cnt) {
+                                                       if (wa->isnull) {
                                                                
list_remove_node(se, NULL, m);
 
                                                                
w=exp_atom_lng(v->sql->sa, 0);
                                                                list_append(se, 
w);
-                                                       }
-                                                       else {
+                                                       } else {
                                                                continue;
                                                        }
                                                }
 
                                                
exp_setalias(w,e->alias.label,e->alias.rname,e->alias.name);
+                                               w = exp_check_type(v->sql, 
exp_subtype(e), NULL, w, type_equal);
 
                                                n->data = w;
                                                v->changes++;
diff --git a/sql/test/rel-optimizers/Tests/const-aggr-elim.test 
b/sql/test/rel-optimizers/Tests/const-aggr-elim.test
--- a/sql/test/rel-optimizers/Tests/const-aggr-elim.test
+++ b/sql/test/rel-optimizers/Tests/const-aggr-elim.test
@@ -27,13 +27,13 @@ project (
 | ) [  ] [ "sys"."avg" no nil (tinyint(2) "3") NOT NULL UNIQUE as "%2"."%2" ]
 ) [ "%2"."%2" NOT NULL UNIQUE ]
 
-query I rowsort
+query R rowsort
 SELECT avg(3) FROM baz GROUP BY b
 ----
-3
-3
-3
-3
+3.000
+3.000
+3.000
+3.000
 
 query T nosort
 EXPLAIN SHOW DETAILS SELECT avg(3) FROM baz GROUP BY b
@@ -42,7 +42,7 @@ project (
 | group by (
 | | table("sys"."baz") [ "baz"."b" NOT NULL ]
 | ) [ "baz"."b" NOT NULL ] [ "baz"."b" NOT NULL ]
-) [ tinyint(2) "3" as "%1"."%1" ]
+) [ double(53) "3" as "%1"."%1" ]
 
 query R rowsort
 SELECT 1 + avg(3) FROM baz GROUP BY b
@@ -59,9 +59,9 @@ project (
 | group by (
 | | table("sys"."baz") [ "baz"."b" NOT NULL ]
 | ) [ "baz"."b" NOT NULL ] [ "baz"."b" NOT NULL ]
-) [ "sys"."sql_add"(double(53) "1", tinyint(2) "3") NOT NULL ]
+) [ "sys"."sql_add"(double(53) "1", double(53) "3") NOT NULL ]
 
-query I rowsort
+query D rowsort
 SELECT avg(NULL) FROM baz GROUP BY b
 ----
 NULL
@@ -89,19 +89,19 @@ project (
 | | group by (
 | | | table("sys"."baz") [ "baz"."b" NOT NULL ]
 | | ) [ "baz"."b" NOT NULL ] [ "baz"."b" NOT NULL ]
-| ) [ tinyint(2) "3" as "%1"."%1" ]
+| ) [ double(53) "3" as "%1"."%1" ]
 ) [ "sys"."sql_add"(double(53) "1", "%1"."%1" NOT NULL) NOT NULL, 
"sys"."sql_mul"("%1"."%1" NOT NULL, double(53) "10") NOT NULL ]
 
-query IR rowsort
+query RR rowsort
 SELECT avg(3), avg(b) FROM baz GROUP BY b
 ----
-3
+3.000
 1.000
-3
+3.000
 3.000
-3
+3.000
 6.000
-3
+3.000
 7.000
 
 query I rowsort
@@ -137,7 +137,7 @@ project (
 | group by (
 | | table("sys"."baz") [ "baz"."a" NOT NULL ]
 | ) [ "baz"."a" NOT NULL ] [ "baz"."a" NOT NULL ]
-) [ tinyint(1) "1" as "%1"."%1", tinyint(1) "0" as "%2"."%2" ]
+) [ hugeint(127) "1" as "%1"."%1", tinyint(4) "0" as "%2"."%2" ]
 
 query II rowsort
 select prod(1), sum(0) from baz group by a
@@ -153,7 +153,7 @@ 0
 1
 0
 
-query II rowsort
+query RR rowsort
 select prod(null) + 1, sum(null) + 1 from baz group by a
 ----
 NULL
@@ -174,7 +174,7 @@ project (
 | group by (
 | | table("sys"."baz") [ "baz"."a" NOT NULL ]
 | ) [ "baz"."a" NOT NULL ] [ "baz"."a" NOT NULL ]
-) [ "sys"."sql_add"(bigint(1) "0", bigint(2) "3") NOT NULL ]
+) [ "sys"."sql_add"(bigint(63) "0", bigint(2) "3") NOT NULL ]
 
 query I rowsort
 select count(null) + 3 from baz group by a
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to