Changeset: 3f170986ce2e for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/3f170986ce2e
Added Files:
        sql/test/BugTracker-2026/Tests/7363-double-alias-natural-join.test
        sql/test/BugTracker-2026/Tests/8066-semijoin-reducing-nested-select.test
Modified Files:
        sql/server/rel_optimize_sel.c
        sql/test/BugTracker-2026/Tests/All
Branch: default
Log Message:

fixes for regression of semijoin performance of tpch q21.
and cleanup of the same join expressions, as reported by issue # 7363.


diffs (288 lines):

diff --git a/sql/server/rel_optimize_sel.c b/sql/server/rel_optimize_sel.c
--- a/sql/server/rel_optimize_sel.c
+++ b/sql/server/rel_optimize_sel.c
@@ -1184,6 +1184,69 @@ merge_cmp_or_null(mvc *sql, sql_rel *rel
        return exps;
 }
 
+/* Resolve a join column reference to the first alias in its input's
+ * projection.  Two differently named aliases of the same base column have
+ * different labels, but their projection entries point at the same nid. */
+static sql_exp *
+canonical_join_ref(sql_rel *rel, sql_exp *e)
+{
+       sql_rel *side = NULL;
+       sql_exp *ce = NULL;
+
+       if (!rel || !e || e->type != e_column)
+               return NULL;
+       if (rel->l) {
+               ce = rel_find_exp(rel->l, e);
+               if (ce)
+                       side = rel->l;
+       }
+       if (!ce && rel->r) {
+               ce = rel_find_exp(rel->r, e);
+               if (ce)
+                       side = rel->r;
+       }
+       if (!ce || !side || !is_simple_project(side->op) || 
list_empty(side->exps))
+               return ce;
+       /* Follow alias chains, eg n1 -> n2 -> n3, to the first entry. */
+       int guard = list_length(side->exps);
+       while (ce && guard-- > 0) {
+               sql_exp *next = exps_bind_nid(side->exps, ce->nid);
+
+               if (!next || next == ce)
+                       break;
+               ce = next;
+       }
+       return ce;
+}
+
+static void
+canonicalize_join_equal_exps(sql_rel *rel, list *exps, int *changes)
+{
+       if (!is_join(rel->op) || !rel->l || !rel->r)
+               return;
+       for (node *n = exps->h; n; n = n->next) {
+               sql_exp *e = n->data;
+
+               if (e->type != e_cmp || e->flag != cmp_equal || e->f)
+                       continue;
+               void **operands[2] = {&e->l, &e->r};
+               for (int i = 0; i < 2; i++) {
+                       sql_exp *op = *operands[i], *ce;
+
+                       if (!op || op->type != e_column)
+                               continue;
+                       ce = canonical_join_ref(rel, op);
+                       if (!ce || !ce->alias.label ||
+                               (ce->alias.label == op->alias.label && 
ce->alias.label == op->nid))
+                               continue;
+                       exp_setalias(op, ce->alias.label, exp_relname(ce), 
exp_name(ce));
+                       op->nid = ce->alias.label;
+                       *operands[i] = op;
+                       (*changes)++;
+               }
+       }
+}
+
 static list *
 cleanup_equal_exps(mvc *sql, sql_rel *rel, list *exps, int *changes)
 {
@@ -1198,6 +1261,7 @@ cleanup_equal_exps(mvc *sql, sql_rel *re
                                exp_swap(e);
                        }
                }
+               canonicalize_join_equal_exps(rel, exps, changes);
        }
        bool needed = false;
        for(node *n = exps->h; !needed && n; n = n->next) {
@@ -2457,6 +2521,34 @@ rel_rewrite_semijoin(visitor *v, sql_rel
        return rel;
 }
 
+static bool
+rel_has_select_tree(sql_rel *rel)
+{
+       if (!rel || rel_is_ref(rel))
+               return false;
+       if (is_select(rel->op))
+               return true;
+       /* Only walk operators whose children are sql_rel pointers, not lists. 
*/
+       switch (rel->op) {
+       case op_join:
+       case op_left:
+       case op_right:
+       case op_full:
+       case op_semi:
+       case op_anti:
+       case op_inter:
+       case op_except:
+               return rel_has_select_tree(rel->l) || 
rel_has_select_tree(rel->r);
+       case op_project:
+       case op_groupby:
+       case op_topn:
+       case op_sample:
+               return rel_has_select_tree(rel->l);
+       default:
+               return false;
+       }
+}
+
 /*
  * Push semijoins down, pushes the semijoin through a join (if join doesn't 
have any selects).
  *
@@ -2540,10 +2632,6 @@ rel_push_semijoin_down_or_up(visitor *v,
                sql_rel *ll = l->l;
                sql_rel *lr = l->r;
 
-               /* only push down is join is not reducing */
-               if (is_select(ll->op) || is_select(lr->op))
-                       return rel;
-
                /* check which side is used and other exps are atoms or from 
right of semijoin */
                for(n = exps->h; n; n = n->next) {
                        sql_exp *sje = n->data;
@@ -2573,6 +2661,11 @@ rel_push_semijoin_down_or_up(visitor *v,
                        return rel;
                if (right && is_left(lop))
                        return rel;
+               /* only push down if the chosen side is not reducing */
+               if (left && rel_has_select_tree(ll))
+                       return rel;
+               if (right && rel_has_select_tree(lr))
+                       return rel;
                list *nsexps = exps_copy(v->sql, rel->exps);
                list *nsattr = exps_copy(v->sql, rel->attr);
                list *njexps = exps_copy(v->sql, l->exps);
diff --git a/sql/test/BugTracker-2026/Tests/7363-double-alias-natural-join.test 
b/sql/test/BugTracker-2026/Tests/7363-double-alias-natural-join.test
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2026/Tests/7363-double-alias-natural-join.test
@@ -0,0 +1,46 @@
+statement ok
+DROP TABLE IF EXISTS f7363
+
+statement ok
+DROP TABLE IF EXISTS b7363
+
+statement ok
+CREATE TABLE f7363(n INT, c INT)
+
+statement ok
+CREATE TABLE b7363(n INT, c INT)
+
+statement ok
+INSERT INTO f7363 VALUES (1, 10), (1, 10), (2, 20)
+
+statement ok
+INSERT INTO b7363 VALUES (1, 10), (2, 20), (2, 20)
+
+# The duplicate aliases must not produce a duplicate join equality.
+query T nosort
+EXPLAIN SELECT * FROM (SELECT n n1, n n2, c FROM f7363) AS fs NATURAL JOIN 
(SELECT n n1, n n2, c FROM b7363) AS bs
+----
+project (
+| join (
+| | project (
+| | | table("sys"."f7363") [ "f7363"."n", "f7363"."c" ]
+| | ) [ "f7363"."n" as "fs"."n1", "fs"."n1" as "fs"."n2", "f7363"."c" as 
"fs"."c" ],
+| | project (
+| | | table("sys"."b7363") [ "b7363"."n", "b7363"."c" ]
+| | ) [ "b7363"."n" as "bs"."n1", "b7363"."c" as "bs"."c" ]
+| ) [ ("fs"."n1") = ("bs"."n1"), ("fs"."c") = ("bs"."c") ]
+) [ "fs"."n1", "fs"."n2", "fs"."c" ]
+
+query III nosort
+SELECT * FROM (SELECT n n1, n n2, c FROM f7363) AS fs NATURAL JOIN (SELECT n 
n1, n n2, c FROM b7363) AS bs ORDER BY 1, 2, 3
+----
+1      1       10
+1      1       10
+2      2       20
+2      2       20
+
+statement ok
+DROP TABLE f7363
+
+statement ok
+DROP TABLE b7363
diff --git 
a/sql/test/BugTracker-2026/Tests/8066-semijoin-reducing-nested-select.test 
b/sql/test/BugTracker-2026/Tests/8066-semijoin-reducing-nested-select.test
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2026/Tests/8066-semijoin-reducing-nested-select.test
@@ -0,0 +1,74 @@
+statement ok
+CREATE TABLE a8066b (low_key INTEGER NOT NULL)
+
+statement ok
+CREATE TABLE b8066b (id INTEGER NOT NULL, low_key INTEGER NOT NULL, skey 
INTEGER NOT NULL)
+
+statement ok
+CREATE TABLE c8066b (id INTEGER NOT NULL)
+
+statement ok
+CREATE TABLE s8066b (id INTEGER NOT NULL)
+
+statement ok rowcount 20
+INSERT INTO a8066b SELECT CAST(value AS INTEGER) FROM sys.generate_series(0, 
20)
+
+statement ok rowcount 2000
+INSERT INTO b8066b SELECT CAST(value AS INTEGER), CAST(MOD(value, 100) AS 
INTEGER), CAST(MOD(value, 10) AS INTEGER) FROM sys.generate_series(0, 2000)
+
+statement ok rowcount 2000
+INSERT INTO c8066b SELECT CAST(value AS INTEGER) FROM sys.generate_series(0, 
2000)
+
+statement ok rowcount 10
+INSERT INTO s8066b SELECT CAST(value AS INTEGER) FROM sys.generate_series(0, 
10)
+
+statement ok
+ANALYZE sys.a8066b
+
+statement ok
+ANALYZE sys.b8066b
+
+statement ok
+ANALYZE sys.c8066b
+
+statement ok
+ANALYZE sys.s8066b
+
+# a semijoin must not be pushed through a reducing join whose child selects
+# are nested below another join (TPCH q21)
+query T nosort
+EXPLAIN SELECT COUNT(*) FROM s8066b s JOIN (SELECT * FROM b8066b WHERE low_key 
< 10) b ON s.id = b.skey JOIN a8066b a ON b.low_key = a.low_key JOIN c8066b c 
ON b.id = c.id
+----
+project (
+| group by (
+| | semijoin (
+| | | join (
+| | | | join (
+| | | | | table("sys"."s8066b") [ "s8066b"."id" as "s"."id" ],
+| | | | | select (
+| | | | | | table("sys"."b8066b") [ "b8066b"."id", "b8066b"."low_key", 
"b8066b"."skey" ]
+| | | | | ) [ ("b8066b"."low_key") < (int(7) "10") ]
+| | | | ) [ ("s"."id") = ("b8066b"."skey") ],
+| | | | table("sys"."a8066b") [ "a8066b"."low_key" as "a"."low_key" ]
+| | | ) [ ("b8066b"."low_key") = ("a"."low_key") ],
+| | | table("sys"."c8066b") [ "c8066b"."id" as "c"."id" ]
+| | ) [ ("b8066b"."id") = ("c"."id") ]
+| ) [  ] [ "sys"."count"() as "%1"."%1" ]
+) [ "%1"."%1" ]
+
+query I
+SELECT COUNT(*) FROM s8066b s JOIN (SELECT * FROM b8066b WHERE low_key < 10) b 
ON s.id = b.skey JOIN a8066b a ON b.low_key = a.low_key JOIN c8066b c ON b.id = 
c.id
+----
+200
+
+statement ok
+DROP TABLE c8066b
+
+statement ok
+DROP TABLE s8066b
+
+statement ok
+DROP TABLE b8066b
+
+statement ok
+DROP TABLE a8066b
diff --git a/sql/test/BugTracker-2026/Tests/All 
b/sql/test/BugTracker-2026/Tests/All
--- a/sql/test/BugTracker-2026/Tests/All
+++ b/sql/test/BugTracker-2026/Tests/All
@@ -161,6 +161,7 @@ PIPELINE?7966-optimize-select-distinct-f
 7973-groupby-list-null-row-bug
 PIPELINE?7974-multi-column-not-in-subquery-bug
 7975-cleanup-join-exps
+7363-double-alias-natural-join
 7976-dump_database-composite-key-order-bug
 7977-prepare-optimize
 7978-name-leak-prepare-error-recompile
@@ -226,6 +227,7 @@ KNOWNFAIL?8010-first_value-last_value-pa
 8065-unnamed-fkeys-same-column
 8061-session-authorization-current-user
 8066-semijoin-pushdown-after-join-order
+8066-semijoin-reducing-nested-select
 8067-duplicate-in-values
 8068-pipeline-limit-slice-join
 8070-sum-constant-drops-groupby-aggregates
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to