Changeset: 816a431b7ef2 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/816a431b7ef2
Added Files:
sql/backends/monet5/rel_physical.c
sql/backends/monet5/rel_physical.h
Modified Files:
sql/backends/monet5/CMakeLists.txt
sql/backends/monet5/rel_bin.c
sql/backends/monet5/sql.c
sql/backends/monet5/sql_statement.c
sql/test/BugTracker-2010/Tests/LIMIT_OFFSET_big-endian.Bug-2622.test
sql/test/BugTracker-2015/Tests/quantile_function_resolution.Bug-3773.test
sql/test/FeatureRequests/Tests/foreign_key_outer_join_dead_code_elimination-explain-0join-query.test
sql/test/FeatureRequests/Tests/foreign_key_outer_join_dead_code_elimination-explain-0join-view.test
sql/test/FeatureRequests/Tests/foreign_key_outer_join_dead_code_elimination-explain-1join-query.test
sql/test/FeatureRequests/Tests/foreign_key_outer_join_dead_code_elimination-explain-1join-view.test
sql/test/FeatureRequests/Tests/foreign_key_outer_join_dead_code_elimination-explain-2join-query.test
sql/test/FeatureRequests/Tests/foreign_key_outer_join_dead_code_elimination-explain-2join-view.test
Branch: default
Log Message:
add physical optimization step, when we have quantiles (over the full column)
add order by
for relational project with order by, we now keep the re-order-ed value columns
when the are projected
diffs (truncated from 415 to 300 lines):
diff --git a/sql/backends/monet5/CMakeLists.txt
b/sql/backends/monet5/CMakeLists.txt
--- a/sql/backends/monet5/CMakeLists.txt
+++ b/sql/backends/monet5/CMakeLists.txt
@@ -130,6 +130,7 @@ target_sources(sql
sql_assert.c sql_assert.h
sql_upgrades.c sql_upgrades.h
rel_bin.c rel_bin.h
+ rel_physical.c rel_physical.h
rel_predicates.c rel_predicates.h
sql_cat.c sql_cat.h
sql_transaction.c sql_transaction.h
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
@@ -3544,18 +3544,34 @@ rel2bin_inter(backend *be, sql_rel *rel,
return rel_rename(be, rel, sub);
}
+static int
+find_matching_exp(list *exps, sql_exp *e)
+{
+ int i = 0;
+ for (node *n = exps->h; n; n = n->next, i++) {
+ if (exp_match(n->data, e))
+ return i;
+ }
+ return -1;
+}
+
static stmt *
-sql_reorder(backend *be, stmt *order, stmt *s)
+sql_reorder(backend *be, stmt *order, list *exps, stmt *s, list *oexps, list
*ostmts)
{
list *l = sa_list(be->mvc->sa);
- node *n;
-
- for (n = s->op4.lval->h; n; n = n->next) {
+
+ for (node *n = s->op4.lval->h, *m = exps->h; n && m; n = n->next, m =
m->next) {
+ int pos = 0;
stmt *sc = n->data;
+ sql_exp *pe = m->data;
const char *cname = column_name(be->mvc->sa, sc);
const char *tname = table_name(be->mvc->sa, sc);
- sc = stmt_project(be, order, sc);
+ if (oexps && (pos = find_matching_exp(oexps, pe)) >= 0 &&
list_fetch(ostmts, pos)) {
+ sc = list_fetch(ostmts, pos);
+ } else {
+ sc = stmt_project(be, order, sc);
+ }
sc = stmt_alias(be, sc, tname, cname );
list_append(l, sc);
}
@@ -3729,6 +3745,7 @@ rel2bin_project(backend *be, sql_rel *re
list *oexps = rel->r;
stmt *orderby_ids = NULL, *orderby_grp = NULL;
+ list *ostmts = sa_list(be->mvc->sa);
for (en = oexps->h; en; en = en->next) {
stmt *orderby = NULL;
sql_exp *orderbycole = en->data;
@@ -3739,17 +3756,21 @@ rel2bin_project(backend *be, sql_rel *re
return NULL;
}
/* single values don't need sorting */
- if (orderbycolstmt->nrcols == 0)
+ if (orderbycolstmt->nrcols == 0) {
+ append(ostmts, NULL);
continue;
+ }
if (orderby_ids)
orderby = stmt_reorder(be, orderbycolstmt,
is_ascending(orderbycole), nulls_last(orderbycole), orderby_ids, orderby_grp);
else
orderby = stmt_order(be, orderbycolstmt,
is_ascending(orderbycole), nulls_last(orderbycole));
+ stmt *orderby_vals = stmt_result(be, orderby, 0);
+ append(ostmts, orderby_vals);
orderby_ids = stmt_result(be, orderby, 1);
orderby_grp = stmt_result(be, orderby, 2);
}
if (orderby_ids)
- psub = sql_reorder(be, orderby_ids, psub);
+ psub = sql_reorder(be, orderby_ids, rel->exps, psub,
oexps, ostmts);
}
return psub;
}
diff --git a/sql/backends/monet5/rel_physical.c
b/sql/backends/monet5/rel_physical.c
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/rel_physical.c
@@ -0,0 +1,60 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2022 MonetDB B.V.
+ */
+
+#include "monetdb_config.h"
+#include "rel_physical.h"
+#include "rel_optimizer_private.h"
+#include "rel_rewriter.h"
+#include "rel_exp.h"
+#include "rel_rel.h"
+
+static sql_rel *
+rel_add_orderby(visitor *v, sql_rel *rel)
+{
+ if (is_groupby(rel->op)) {
+ if (rel->exps && !rel->r) { /* find quantiles */
+ for(node *n = rel->exps->h; n; n = n->next) {
+ sql_exp *e = n->data;
+
+ if (is_aggr(e->type)) {
+ sql_subfunc *af = e->f;
+ list *aa = e->l;
+
+ /* for now we only handle one sort
order */
+ if (strcmp(af->func->base.name,
"quantile") == 0 && aa && list_length(aa) == 2) {
+ sql_exp *obe = aa->h->data;
+ if (obe) {
+ sql_rel *l = rel->l =
rel_project(v->sql->sa, rel->l, rel_projections(v->sql, rel->l, NULL, 1, 1));
+ if (l) {
+ if
(!is_alias(obe->type)) {
+
append(l->exps, obe);
+ obe =
exp_label(v->sql->sa, obe, ++v->sql->label);
+
aa->h->data = exp_ref(v->sql, obe);
+ }
+ list *o = l->r
= sa_list(v->sql->sa);
+ if (o)
+
append(o, obe);
+ }
+ }
+ return rel;
+ }
+ }
+ }
+ }
+ }
+ return rel;
+}
+
+sql_rel *
+rel_physical(mvc *sql, sql_rel *rel)
+{
+ visitor v = { .sql = sql };
+
+ rel = rel_visitor_bottomup(&v, rel, &rel_add_orderby);
+ return rel;
+}
diff --git a/sql/backends/monet5/rel_physical.h
b/sql/backends/monet5/rel_physical.h
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/rel_physical.h
@@ -0,0 +1,17 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2022 MonetDB B.V.
+ */
+
+#ifndef _REL_PHYSICAL_H_
+#define _REL_PHYSICAL_H_
+
+#include "sql_relation.h"
+#include "sql_mvc.h"
+
+extern sql_rel *rel_physical(mvc *sql, sql_rel *rel);
+
+#endif /*_REL_PHYSICAL_H_*/
diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -35,6 +35,7 @@
#include "rel_exp.h"
#include "rel_dump.h"
#include "rel_bin.h"
+#include "rel_physical.h"
#include "mal.h"
#include "mal_client.h"
#include "mal_interpreter.h"
@@ -151,6 +152,8 @@ sql_symbol2relation(backend *be, symbol
rel = rel_partition(be->mvc, rel);
if (rel && (rel_no_mitosis(be->mvc, rel) ||
rel_need_distinct_query(rel)))
be->no_mitosis = 1;
+ if (rel /*&& (be->mvc->emode != m_plan || (GDKdebug & FORCEMITOMASK) ==
0)*/)
+ rel = rel_physical(be->mvc, rel);
Tend = GDKusec();
be->reloptimizer = Tend - Tbegin;
diff --git a/sql/backends/monet5/sql_statement.c
b/sql/backends/monet5/sql_statement.c
--- a/sql/backends/monet5/sql_statement.c
+++ b/sql/backends/monet5/sql_statement.c
@@ -1079,7 +1079,10 @@ stmt_result(backend *be, stmt *s, int nr
ns->nr = s->nr;
}
ns->op1 = s;
- ns->op4.typeval = *sql_bind_localtype("oid");
+ if (!nr && (s->type == st_order || s->type == st_reorder))
+ ns->op4.typeval = *tail_type(s->op1);
+ else
+ ns->op4.typeval = *sql_bind_localtype("oid");
ns->flag = nr;
ns->nrcols = s->nrcols;
ns->key = s->key;
diff --git
a/sql/test/BugTracker-2010/Tests/LIMIT_OFFSET_big-endian.Bug-2622.test
b/sql/test/BugTracker-2010/Tests/LIMIT_OFFSET_big-endian.Bug-2622.test
--- a/sql/test/BugTracker-2010/Tests/LIMIT_OFFSET_big-endian.Bug-2622.test
+++ b/sql/test/BugTracker-2010/Tests/LIMIT_OFFSET_big-endian.Bug-2622.test
@@ -214,7 +214,7 @@ query T python .explain.function_histogr
EXPLAIN select * from oblo ORDER BY a
----
algebra.projection
-2
+1
algebra.sort
1
bat.pack
@@ -236,7 +236,7 @@ query T python .explain.function_histogr
EXPLAIN select * from oblo ORDER BY a OFFSET 2
----
algebra.projection
-3
+2
algebra.sort
1
algebra.subslice
@@ -262,7 +262,7 @@ EXPLAIN select * from oblo ORDER BY a LI
algebra.firstn
1
algebra.projection
-4
+3
algebra.sort
1
algebra.subslice
@@ -288,7 +288,7 @@ EXPLAIN select * from oblo ORDER BY a LI
algebra.firstn
1
algebra.projection
-4
+3
algebra.sort
1
algebra.subslice
@@ -314,7 +314,7 @@ EXPLAIN select * from oblo ORDER BY a LI
algebra.firstn
1
algebra.projection
-4
+3
algebra.sort
1
algebra.subslice
diff --git
a/sql/test/BugTracker-2015/Tests/quantile_function_resolution.Bug-3773.test
b/sql/test/BugTracker-2015/Tests/quantile_function_resolution.Bug-3773.test
--- a/sql/test/BugTracker-2015/Tests/quantile_function_resolution.Bug-3773.test
+++ b/sql/test/BugTracker-2015/Tests/quantile_function_resolution.Bug-3773.test
@@ -10,9 +10,11 @@ PLAN select quantile(y, 0.0) from x
project (
| group by (
| | project (
-| | | table("sys"."x") [ "x"."y" NOT NULL UNIQUE ]
-| | ) [ "x"."y" NOT NULL UNIQUE, double(53) "0" as "%2"."%2" ]
-| ) [ ] [ "sys"."quantile" no nil (decimal(18,3)["x"."y" NOT NULL UNIQUE] NOT
NULL, "%2"."%2" NOT NULL) UNIQUE as "%1"."%1" ]
+| | | project (
+| | | | table("sys"."x") [ "x"."y" NOT NULL UNIQUE ]
+| | | ) [ "x"."y" NOT NULL UNIQUE, double(53) "0" as "%2"."%2" ]
+| | ) [ "x"."y" NOT NULL UNIQUE, "%2"."%2" NOT NULL, decimal(18,3)["x"."y" NOT
NULL UNIQUE] NOT NULL as "%3"."%3" ] [ decimal(18,3)["x"."y" NOT NULL UNIQUE]
NOT NULL as "%3"."%3" ]
+| ) [ ] [ "sys"."quantile" no nil ("%3"."%3" NOT NULL, "%2"."%2" NOT NULL)
UNIQUE as "%1"."%1" ]
) [ "%1"."%1" UNIQUE ]
query T nosort
@@ -21,9 +23,11 @@ PLAN select quantile(y, 0) from x
project (
| group by (
| | project (
-| | | table("sys"."x") [ "x"."y" NOT NULL UNIQUE ]
-| | ) [ "x"."y" NOT NULL UNIQUE, double(53) "0" as "%2"."%2" ]
-| ) [ ] [ "sys"."quantile" no nil (decimal(18,3)["x"."y" NOT NULL UNIQUE] NOT
NULL, "%2"."%2" NOT NULL) UNIQUE as "%1"."%1" ]
+| | | project (
+| | | | table("sys"."x") [ "x"."y" NOT NULL UNIQUE ]
+| | | ) [ "x"."y" NOT NULL UNIQUE, double(53) "0" as "%2"."%2" ]
+| | ) [ "x"."y" NOT NULL UNIQUE, "%2"."%2" NOT NULL, decimal(18,3)["x"."y" NOT
NULL UNIQUE] NOT NULL as "%3"."%3" ] [ decimal(18,3)["x"."y" NOT NULL UNIQUE]
NOT NULL as "%3"."%3" ]
+| ) [ ] [ "sys"."quantile" no nil ("%3"."%3" NOT NULL, "%2"."%2" NOT NULL)
UNIQUE as "%1"."%1" ]
) [ "%1"."%1" UNIQUE ]
statement ok
diff --git
a/sql/test/FeatureRequests/Tests/foreign_key_outer_join_dead_code_elimination-explain-0join-query.test
b/sql/test/FeatureRequests/Tests/foreign_key_outer_join_dead_code_elimination-explain-0join-query.test
---
a/sql/test/FeatureRequests/Tests/foreign_key_outer_join_dead_code_elimination-explain-0join-query.test
+++
b/sql/test/FeatureRequests/Tests/foreign_key_outer_join_dead_code_elimination-explain-0join-query.test
@@ -17,7 +17,7 @@ query T python .explain.function_histogr
explain select id from fk order by id
----
algebra.projection
-2
+1
algebra.sort
1
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]