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(name) (strcmp((name), "quantile") == 0 || strcmp((name), "quantile_avg") == 0 || \
21 : strcmp((name), "median") == 0 || strcmp((name), "median_avg") == 0)
22 :
23 : static sql_rel *
24 1392266 : rel_add_orderby(visitor *v, sql_rel *rel)
25 : {
26 1392266 : if (is_groupby(rel->op)) {
27 17541 : if (rel->exps && !rel->r) { /* find quantiles */
28 4445 : sql_exp *obe = NULL, *oberef = NULL;
29 9349 : for(node *n = rel->exps->h; n; n = n->next) {
30 4904 : sql_exp *e = n->data;
31 :
32 4904 : if (is_aggr(e->type)) {
33 4893 : sql_subfunc *af = e->f;
34 4893 : list *aa = e->l;
35 :
36 : /* for now we only handle one sort order */
37 4893 : if (IS_ORDER_BASED_AGGR(af->func->base.name) && aa && list_length(aa) == 2) {
38 28 : sql_exp *nobe = aa->h->data;
39 28 : if (nobe && !obe) {
40 21 : sql_rel *l = rel->l = rel_project(v->sql->sa, rel->l, rel_projections(v->sql, rel->l, NULL, 1, 1));
41 21 : obe = nobe;
42 21 : oberef = nobe;
43 21 : if (l) {
44 21 : if (!is_alias(nobe->type)) {
45 0 : oberef = nobe = exp_label(v->sql->sa, exp_copy(v->sql, nobe), ++v->sql->label);
46 0 : append(l->exps, nobe);
47 : }
48 21 : set_nulls_first(nobe);
49 21 : set_ascending(nobe);
50 21 : aa->h->data = exp_ref(v->sql, nobe);
51 21 : list *o = l->r = sa_list(v->sql->sa);
52 21 : if (o)
53 21 : append(o, nobe);
54 : }
55 7 : } else if (exp_match_exp(nobe, obe)) {
56 3 : aa->h->data = exp_ref(v->sql, oberef);
57 : }
58 : }
59 : }
60 : }
61 : return rel;
62 : }
63 : }
64 : return rel;
65 : }
66 :
67 : static sql_exp *
68 6475096 : exp_timezone(visitor *v, sql_rel *rel, sql_exp *e, int depth)
69 : {
70 6475096 : (void)depth;
71 6475096 : (void)rel;
72 6475096 : if (e && e->type == e_func) {
73 163929 : list *l = e->l;
74 163929 : sql_subfunc *f = e->f;
75 163929 : const char *fname = f->func->base.name;
76 163929 : if (list_length(l) == 2) {
77 98917 : if (strcmp(fname, "timestamp_to_str") == 0 || strcmp(fname, "time_to_str") == 0) {
78 22 : sql_exp *e = l->h->data;
79 22 : sql_subtype *t = exp_subtype(e);
80 22 : if (t->type->eclass == EC_TIMESTAMP_TZ || t->type->eclass == EC_TIME_TZ) {
81 19 : sql_exp *offset = exp_atom_lng(v->sql->sa, v->sql->timezone);
82 19 : list_append(l, offset);
83 : }
84 98895 : } else if (strcmp(fname, "str_to_timestamp") == 0 || strcmp(fname, "str_to_time") == 0 || strcmp(fname, "str_to_date") == 0) {
85 70 : sql_exp *offset = exp_atom_lng(v->sql->sa, v->sql->timezone);
86 70 : list_append(l, offset);
87 : }
88 : }
89 : }
90 6475096 : return e;
91 : }
92 :
93 : sql_rel *
94 560615 : rel_physical(mvc *sql, sql_rel *rel)
95 : {
96 560615 : visitor v = { .sql = sql };
97 :
98 560615 : rel = rel_visitor_bottomup(&v, rel, &rel_add_orderby);
99 560603 : rel = rel_exp_visitor_topdown(&v, rel, &exp_timezone, true);
100 :
101 : #ifdef HAVE_HGE
102 560602 : if (rel && sql->no_int128) {
103 0 : sql_rel *r = rel;
104 0 : if (is_topn(r->op))
105 0 : r = r->l;
106 0 : if (r && is_project(r->op) && !list_empty(r->exps)) {
107 0 : for (node *n = r->exps->h; n; n = n->next) {
108 0 : sql_exp *e = n->data;
109 :
110 0 : if (exp_subtype(e)->type->localtype == TYPE_hge) /* down cast */
111 0 : e = n->data = exp_convert(sql, e, exp_subtype(e), sql_bind_localtype("lng"));
112 : }
113 : }
114 : }
115 : #endif
116 560602 : return rel;
117 : }
|