Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : #include "monetdb_config.h"
14 : #include "rel_physical.h"
15 : #include "rel_optimizer_private.h"
16 : #include "rel_rewriter.h"
17 : #include "rel_exp.h"
18 : #include "rel_rel.h"
19 :
20 : #define IS_ORDER_BASED_AGGR(fname, argc) (\
21 : (argc == 2 && (strcmp((fname), "quantile") == 0 || strcmp((fname), "quantile_avg") == 0)) || \
22 : (argc == 1 && (strcmp((fname), "median") == 0 || strcmp((fname), "median_avg") == 0)))
23 :
24 : static sql_rel *
25 1433591 : rel_add_orderby(visitor *v, sql_rel *rel)
26 : {
27 1433591 : if (is_groupby(rel->op)) {
28 17691 : if (rel->exps && !rel->r) { /* find quantiles */
29 4477 : sql_exp *obe = NULL, *oberef = NULL;
30 9418 : for(node *n = rel->exps->h; n; n = n->next) {
31 4941 : sql_exp *e = n->data;
32 :
33 4941 : if (is_aggr(e->type)) {
34 4928 : sql_subfunc *af = e->f;
35 4928 : list *aa = e->l;
36 :
37 : /* for now we only handle one sort order */
38 4928 : if (aa && IS_ORDER_BASED_AGGR(af->func->base.name, list_length(aa))) {
39 52 : sql_exp *nobe = aa->h->data;
40 52 : if (nobe && !obe) {
41 43 : sql_rel *l = rel->l = rel_project(v->sql->sa, rel->l, rel_projections(v->sql, rel->l, NULL, 1, 1));
42 43 : obe = nobe;
43 43 : oberef = nobe;
44 43 : if (l) {
45 43 : if (!is_alias(nobe->type)) {
46 3 : oberef = nobe = exp_label(v->sql->sa, exp_copy(v->sql, nobe), ++v->sql->label);
47 3 : append(l->exps, nobe);
48 : }
49 43 : set_nulls_first(nobe);
50 43 : set_ascending(nobe);
51 43 : aa->h->data = exp_ref(v->sql, nobe);
52 43 : list *o = l->r = sa_list(v->sql->sa);
53 43 : if (o)
54 43 : append(o, nobe);
55 : }
56 9 : } else if (exp_match_exp(nobe, obe)) {
57 7 : aa->h->data = exp_ref(v->sql, oberef);
58 : }
59 : }
60 : }
61 : }
62 : return rel;
63 : }
64 : }
65 : return rel;
66 : }
67 :
68 : static sql_exp *
69 6737017 : exp_timezone(visitor *v, sql_rel *rel, sql_exp *e, int depth)
70 : {
71 6737017 : (void)depth;
72 6737017 : (void)rel;
73 6737017 : if (e && e->type == e_func) {
74 164360 : list *l = e->l;
75 164360 : sql_subfunc *f = e->f;
76 164360 : const char *fname = f->func->base.name;
77 164360 : if (list_length(l) == 2) {
78 99243 : if (strcmp(fname, "timestamp_to_str") == 0 || strcmp(fname, "time_to_str") == 0) {
79 22 : sql_exp *e = l->h->data;
80 22 : sql_subtype *t = exp_subtype(e);
81 22 : if (t->type->eclass == EC_TIMESTAMP_TZ || t->type->eclass == EC_TIME_TZ) {
82 19 : sql_exp *offset = exp_atom_lng(v->sql->sa, v->sql->timezone);
83 19 : list_append(l, offset);
84 : }
85 99221 : } else if (strcmp(fname, "str_to_timestamp") == 0 || strcmp(fname, "str_to_time") == 0 || strcmp(fname, "str_to_date") == 0) {
86 70 : sql_exp *offset = exp_atom_lng(v->sql->sa, v->sql->timezone);
87 70 : list_append(l, offset);
88 : }
89 : }
90 : }
91 6737017 : return e;
92 : }
93 :
94 : sql_rel *
95 574771 : rel_physical(mvc *sql, sql_rel *rel)
96 : {
97 574771 : visitor v = { .sql = sql };
98 :
99 574771 : rel = rel_visitor_bottomup(&v, rel, &rel_add_orderby);
100 575801 : rel = rel_exp_visitor_topdown(&v, rel, &exp_timezone, true);
101 :
102 : #ifdef HAVE_HGE
103 575300 : if (rel && sql->no_int128) {
104 0 : sql_rel *r = rel;
105 0 : if (is_topn(r->op))
106 0 : r = r->l;
107 0 : if (r && is_project(r->op) && !list_empty(r->exps)) {
108 0 : for (node *n = r->exps->h; n; n = n->next) {
109 0 : sql_exp *e = n->data;
110 :
111 0 : if (exp_subtype(e)->type->localtype == TYPE_hge) /* down cast */
112 0 : e = n->data = exp_convert(sql, e, exp_subtype(e), sql_bind_localtype("lng"));
113 : }
114 : }
115 : }
116 : #endif
117 575300 : return rel;
118 : }
|