Changeset: e53d63168645 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/e53d63168645
Added Files:
sql/test/BugTracker-2026/Tests/8117-duplicate-cte.test
Modified Files:
sql/backends/monet5/rel_bin.c
sql/backends/monet5/rel_physical.c
sql/include/sql_hash.h
sql/server/rel_optimize_others.c
sql/server/rel_optimizer.c
sql/server/rel_optimizer.h
sql/server/rel_optimizer_private.h
sql/server/rel_rel.c
sql/server/rel_rel.h
sql/test/BugTracker-2026/Tests/All
Branch: default
Log Message:
detect dupicate relations, use only one of those.
Solves issue #8117
diffs (truncated from 1028 to 300 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
@@ -8296,146 +8296,23 @@ rel_find_refs(mvc *sql, list *refs, sql_
return refs;
}
-/* todo change into simple nid1 -> nid2 */
-static bool
-exp_pair_match(list *exps, int nid1, int nid2)
-{
- if (list_empty(exps))
- return false;
- for(node *n = exps->h; n; n = n->next->next) {
- sql_exp *e = n->data;
- if (e->alias.label == nid1) {
- sql_exp *e2 = n->next->data;
- if (e2->alias.label == nid2)
- return true;
- }
- }
- return false;
-}
-
-static int exps_remap(list *lexps, list *rexps, list *remapped);
-static int
-exp_remap(sql_exp *le, sql_exp *re, list *remapped)
-{
- list *ll, *rl;
- if (!le || !re || le->type != re->type)
- return -1;
- switch(le->type) {
- case e_column:
- if (exp_pair_match(remapped, le->nid, re->nid))
- return 0;
- return -1;
- case e_convert:
- ll = le->r;
- rl = re->r;
- return (subtype_cmp(ll->h->data, rl->h->data) == 0 &&
- subtype_cmp(ll->h->next->data, rl->h->next->data)
== 0 &&
- exp_remap(le->l , re->l, remapped) == 0)?0:-1;
- case e_func: {
- sql_subfunc *lf = le->f, *rf = re->f;
- list *lexps = le->l, *rexps = re->l;
- if (subfunc_cmp(lf, rf) == 0 && list_length(lexps) ==
list_length(rexps))
- return exps_remap(lexps, rexps, remapped);
- } break;
- case e_cmp:
- if (le->flag != re->flag)
- return -1;
- if (le->flag <= cmp_equal) {
- return (exp_remap(le->l, re->l, remapped) == 0 &&
- exp_remap(le->r, re->r, remapped) == 0 &&
- ((!le->f && !re->f) || exp_remap(le->f, re->f,
remapped) == 0))?0:-1;
- } else if (le->flag == cmp_in) {
- return (exp_remap(le->l, re->l, remapped) == 0 &&
- exps_remap(le->r, re->r, remapped) ==
0)?0:-1;
- }
- return -1;
- case e_atom:
- if (le->l && re->l)
- return atom_cmp(le->l, re->l);
- return -1;
- default:
- return -1;
- }
- (void)remapped;
- return 0;
-}
-
-static int
-exps_remap(list *lexps, list *rexps, list *remapped)
-{
- if (list_length(lexps) != list_length(rexps))
- return -1;
- if (list_empty(lexps))
- return 0;
- for(node *n = lexps->h, *m = rexps->h; n && m; n = n->next, m =
m->next) {
- sql_exp *le = n->data, *re = m->data;
- if (exp_remap(le, re, remapped) != 0)
- return -1;
- }
- return 0;
-}
-
-static int
-rel_remap(mvc *sql, allocator *ta, sql_rel *l, sql_rel *r, list *remapped)
-{
- if (!l || !r || l->op != r->op)
- return -1;
- switch(l->op) {
- case op_project:
- case op_select:
- case op_buildhash:
- if (l->op == op_project && (l->r || r->r)) /* don't match
orderings */
- return -1;
- if (rel_remap(sql, ta, l->l, r->l, remapped) == 0)
- return (exps_remap(l->attr, r->attr, remapped) == 0 &&
- exps_remap(l->exps, r->exps, remapped)
== 0)?0:-1;
- break;
- case op_basetable:
- if (l->l == r->l) {
- if (list_length(l->exps) == list_length(r->exps)) {
- list *lexps = l->exps, *rexps = r->exps;
- for(node *n = lexps->h, *m = rexps->h; n && m;
n = n->next, m = m->next) {
- sql_exp *le = n->data, *re = m->data;
- assert(le->type == e_column && re->type
== e_column);
- if (strcmp(le->r, re->r) == 0) {
- append(remapped, le);
- append(remapped, re);
- } else {
- return -1;
- }
- }
- return 0;
- }
- }
- break;
- case op_groupby:
- if (rel_remap(sql, ta, l->l, r->l, remapped) == 0)
- return (exps_remap(l->r, r->r, remapped) == 0 &&
- exps_remap(l->exps, r->exps, remapped)
== 0)?0:-1;
- break;
- default:
- return 1;
- }
- return -1;
-}
+/* rel_remap moved to sql/server/rel_rel.c */
static list *
deduplicate_refs(mvc *sql, allocator *ta, list *refs, sql_rel *rel)
{
- (void)sql;
(void)rel; /* for now just deduplicate refs, later find duplicates with
relational graph */
list *prefs = sa_list(ta);
for(node *n = refs->h; n; n = n->next) {
sql_rel *or = n->data, *nr = or;
- prop *op = find_prop(or->p, PROP_HASH);
- if (or->op == op_buildhash && op && op->value.lval) {
- BUN h = op->value.lval;
+ BUN h = rel_hash(sql, or);
+ if (h) {
for(node *m = prefs->h; m; m = m->next->next) {
sql_rel *ir = m->data;
- prop *ip = find_prop(ir->p, PROP_HASH);
- if (ip && h == ip->value.lval && rel_remap(sql,
ta, or, ir, sa_list(ta)) == 0) {
+ BUN ih = rel_hash(sql, ir);
+ if (ih == h && rel_remap(sql, ta, or, ir,
sa_list(ta)) == 0) {
nr = ir;
break;
}
@@ -8477,7 +8354,9 @@ output_rel_bin(backend *be, sql_rel *rel
else {
s = refs_find_rel(nrefs, drel);
/* re-map */
- if (drel->op == op_buildhash) { /*
later more */
+ if (drel->op != op_buildhash &&
!is_project(drel->op) && !be->client->qryctx.pipeline_mode) {
+ s = rel2bin_materialize(be,
rel, nrefs, false);
+ } else if (drel->op == op_buildhash) {
/* later more */
/* s is stmt_list of payload
exps,
* op1 == stmt_list of hash
stmts (attrs of buildhash),
* op2 == hp_gid,
@@ -8510,6 +8389,24 @@ output_rel_bin(backend *be, sql_rel *rel
}
append(nrefs, rel);
append(nrefs, s);
+ } else if (s && s->type == st_list &&
rel->exps && list_length(s->op4.lval) == list_length(rel->exps)) {
+ list *nl = sa_list(be->mvc->sa);
+ for (node *sn = s->op4.lval->h,
*en = rel->exps->h; sn && en; sn = sn->next, en = en->next) {
+ stmt *ps = sn->data;
+ sql_exp *pe = en->data;
+ stmt *nps =
stmt_alias(be, ps, pe->alias.label, exp_relname(pe), exp_name(pe));
+ append(nl, nps);
+ }
+ stmt *ns = stmt_list(be, nl);
+ ns->cand = s->cand;
+ ns->flag = s->flag;
+ s = ns;
+ append(nrefs, rel);
+ append(nrefs, s);
+ } else if (s) {
+ s = subrel_project(be, s,
nrefs, rel);
+ append(nrefs, rel);
+ append(nrefs, s);
}
}
assert(s);
diff --git a/sql/backends/monet5/rel_physical.c
b/sql/backends/monet5/rel_physical.c
--- a/sql/backends/monet5/rel_physical.c
+++ b/sql/backends/monet5/rel_physical.c
@@ -649,120 +649,7 @@ clean_exp_list(list *exps, list *nl, sql
}
-static BUN
-subfunc_hash(sql_subfunc *sf)
-{
- return (BUN)hash_key(sf->func->base.name);
-}
-
-static BUN exps_hash(list *exps);
-
-static BUN
-exp_hash(sql_exp *e)
-{
- BUN h = e->type + 1;
- switch(e->type) {
- case e_convert:
- /* hash type changes */
- return h * exp_hash(e->l);
- case e_column:
- return h * hash_key(e->alias.name);
- case e_cmp:
- h *= (e->flag+1);
- if (e->flag == cmp_con || e->flag == cmp_dis)
- return h * exps_hash(e->l);
- else if (e->flag == cmp_in)
- return h * exp_hash(e->l) * exps_hash(e->r);
- else if (e->flag == cmp_filter)
- return h * exps_hash(e->l) * exps_hash(e->r);
- else
- return h * exp_hash(e->l) * exp_hash(e->r) *
(e->f? exp_hash(e->f) : 1);
- break;
- case e_atom:
- return 1;
- case e_aggr:
- case e_func:
- return h * subfunc_hash(e->f) * exps_hash(e->l);
- case e_psm:
- return 0;
- }
- return 1;
-}
-
-static BUN
-exps_hash(list *exps)
-{
- if (!exps)
- return 1;
- BUN h = 1;
- for(node *n = exps->h; n; n = n->next)
- h *= exp_hash(n->data);
- return h;
-}
-
-static BUN
-rel_hash(sql_rel *r)
-{
- sql_table *t = NULL;
- list *rels = NULL;
- if (!r)
- return 0;
- prop *hashp = find_prop(r->p, PROP_HASH);
- if (hashp)
- return hashp->value.lval;
- BUN h = (r->op + 1);
- switch(r->op) {
- case op_basetable:
- /* hash table */
- t = r->l;
- return h * hash_key(t->base.name);
- case op_select:
- /* hash select exps */
- return h * rel_hash(r->l) * exps_hash(r->exps);
- case op_sample:
- case op_topn:
- case op_project:
- /* hash projection list exps */
- if (r->l)
- h *= rel_hash(r->l);
- return h * exps_hash(r->exps) * (r->r ? exps_hash(r->r)
: 1);
- case op_groupby:
- /* hash gbe, aggr exps */
- h *= rel_hash(r->l);
- return h * exps_hash(r->exps) * (r->r ? exps_hash(r->r)
: 1);
- case op_probehash:
- case op_buildhash:
- case op_partition:
- h *= rel_hash(r->l);
- return h * exps_hash(r->exps) * (r->attr ?
exps_hash(r->attr) : 1);
- case op_anti:
- case op_semi:
- case op_join:
- case op_left:
- case op_right:
- case op_full:
- case op_inter:
- case op_except:
- h *= rel_hash(r->l);
- h *= rel_hash(r->r);
- return h * exps_hash(r->exps) * (r->attr ?
exps_hash(r->attr) : 1);
- case op_munion:
- rels = r->l;
- for (node *n = rels->h; n; n = n->next)
- h *= rel_hash(n->data);
- return h * exps_hash(r->exps) * (r->attr ?
exps_hash(r->attr) : 1);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]