Changeset: dfd6ee42a068 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/dfd6ee42a068
Modified Files:
        sql/server/rel_optimizer.c
        sql/test/SQLancer/Tests/sqlancer17.test
Branch: default
Log Message:

Merged with Jan2022


diffs (114 lines):

diff --git a/sql/server/rel_optimizer.c b/sql/server/rel_optimizer.c
--- a/sql/server/rel_optimizer.c
+++ b/sql/server/rel_optimizer.c
@@ -6524,7 +6524,8 @@ rel_push_project_up(visitor *v, sql_rel 
        return rel;
 }
 
-/* if local_proj is set: the current expression is from the same projection */
+/* if local_proj is >= -1, the current expression is from the same projection 
+   if local_proj is -1, then we don't care about self references (eg used to 
check for order by exps) */
 static int exp_mark_used(sql_rel *subrel, sql_exp *e, int local_proj);
 
 static int
@@ -6548,6 +6549,9 @@ exp_mark_used(sql_rel *subrel, sql_exp *
        switch(e->type) {
        case e_column:
                ne = rel_find_exp(subrel, e);
+               /* if looking in the same projection, make sure 'ne' is 
projected before the searched column */
+               if (ne && local_proj > -1 && list_position(subrel->exps, ne) > 
local_proj)
+                       ne = NULL;
                break;
        case e_convert:
                return exp_mark_used(subrel, e->l, local_proj);
@@ -6592,7 +6596,7 @@ exp_mark_used(sql_rel *subrel, sql_exp *
                break;
        }
        if (ne && e != ne) {
-               if (!local_proj || ne->type != e_column || (has_label(ne) || 
(ne->alias.rname && ne->alias.rname[0] == '%')) || (subrel->l && 
!rel_find_exp(subrel->l, e)))
+               if (local_proj == -2 || ne->type != e_column || (has_label(ne) 
|| (ne->alias.rname && ne->alias.rname[0] == '%')) || (subrel->l && 
!rel_find_exp(subrel->l, e)))
                        ne->used = 1;
                return ne->used;
        }
@@ -6630,7 +6634,7 @@ rel_exps_mark_used(sql_allocator *sa, sq
                        sql_exp *e = n->data;
 
                        e->used = 1;
-                       exp_mark_used(rel, e, 1);
+                       exp_mark_used(rel, e, -1);
                }
        }
 
@@ -6653,8 +6657,8 @@ rel_exps_mark_used(sql_allocator *sa, sq
 
                        if (!is_project(rel->op) || e->used) {
                                if (is_project(rel->op))
-                                       nr += exp_mark_used(rel, e, 1);
-                               nr += exp_mark_used(subrel, e, 0);
+                                       nr += exp_mark_used(rel, e, i);
+                               nr += exp_mark_used(subrel, e, -2);
                        }
                }
        }
@@ -6673,7 +6677,7 @@ rel_exps_mark_used(sql_allocator *sa, sq
 
                        e->used = 1;
                        /* possibly project/groupby uses columns from the inner 
*/
-                       exp_mark_used(subrel, e, 0);
+                       exp_mark_used(subrel, e, -2);
                }
        }
 }
@@ -6772,7 +6776,7 @@ rel_mark_used(mvc *sql, sql_rel *rel, in
                if (rel->l && rel->flag != TRIGGER_WRAPPER) {
                        rel_used(rel);
                        if (rel->r)
-                               exp_mark_used(rel->l, rel->r, 0);
+                               exp_mark_used(rel->l, rel->r, -2);
                        rel_mark_used(sql, rel->l, proj);
                }
                break;
diff --git a/sql/test/SQLancer/Tests/sqlancer17.test 
b/sql/test/SQLancer/Tests/sqlancer17.test
--- a/sql/test/SQLancer/Tests/sqlancer17.test
+++ b/sql/test/SQLancer/Tests/sqlancer17.test
@@ -628,3 +628,40 @@ SELECT 1 FROM tz, vx WHERE (vx.vc1 + tz.
 
 statement ok
 ROLLBACK
+
+statement ok
+START TRANSACTION
+
+statement ok
+CREATE TABLE rt0 (c0 INT)
+
+statement ok
+create or replace view vx(vc0) as (select 'a')
+
+statement ok
+create or replace view vy(vc0) as (select 2)
+
+# make sure self-references are eliminated by rel_dce
+query T nosort
+PLAN SELECT 1 FROM rt0 CROSS JOIN (SELECT CASE sub0.c2 WHEN 4 THEN 'c' END,
+least(3, sub0.c1) FROM vx JOIN (SELECT 4, vy.vc0 FROM vy) AS sub0(c1,c2) ON 
TRUE) AS sub0(c0,c2)
+----
+project (
+| crossproduct (
+| | table("sys"."rt0") [ "rt0"."%TID%" NOT NULL UNIQUE ],
+| | project (
+| | | crossproduct (
+| | | |  [ boolean(1) "true" as "%1"."%1" ],
+| | | |  [ boolean(1) "true" as "%2"."%2" ]
+| | | ) [  ]
+| | ) [ "sys"."casewhen"(tinyint(2) "2", tinyint(3) "4", char(1) "c", char(1) 
NULL) as "sub0"."c0" ]
+| ) [  ]
+) [ tinyint(1) "1" ]
+
+query I nosort
+SELECT 1 FROM rt0 CROSS JOIN (SELECT CASE sub0.c2 WHEN 4 THEN 'c' END,
+least(3, sub0.c1) FROM vx JOIN (SELECT 4, vy.vc0 FROM vy) AS sub0(c1,c2) ON 
TRUE) AS sub0(c0,c2)
+----
+
+statement ok
+ROLLBACK
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to