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 : #ifndef _REL_STATISTICS_H_
14 : #define _REL_STATISTICS_H_
15 :
16 : #include "sql_relation.h"
17 : #include "sql_mvc.h"
18 : #include "rel_exp.h"
19 :
20 : typedef void (*lookup_function) (mvc*, sql_exp*);
21 :
22 : struct function_properties {
23 : const char *name;
24 : lookup_function func;
25 : };
26 :
27 : #define atom_max(X,Y) atom_cmp(X, Y) > 0 ? X : Y
28 : #define atom_min(X,Y) atom_cmp(X, Y) > 0 ? Y : X
29 :
30 : static inline atom *
31 66988 : statistics_atom_max(mvc *sql, atom *v1, atom *v2)
32 : {
33 66988 : sql_subtype super;
34 66988 : atom *cast1 = v1, *cast2 = v2;
35 :
36 66988 : supertype(&super, &v1->tpe, &v2->tpe);
37 66988 : if (subtype_cmp(&v1->tpe, &super) != 0) {
38 1 : cast1 = atom_copy(sql->sa, v1);
39 1 : if (!(cast1 = atom_cast(sql->sa, cast1, &super)))
40 : return NULL;
41 : }
42 66987 : if (subtype_cmp(&v2->tpe, &super) != 0) {
43 12 : cast2 = atom_copy(sql->sa, v2);
44 12 : if (!(cast2 = atom_cast(sql->sa, cast2, &super)))
45 : return NULL;
46 : }
47 66987 : return atom_cmp(cast1, cast2) > 0 ? cast1 : cast2;
48 : }
49 :
50 : static inline atom *
51 65365 : statistics_atom_min(mvc *sql, atom *v1, atom *v2)
52 : {
53 65365 : sql_subtype super;
54 65365 : atom *cast1 = v1, *cast2 = v2;
55 :
56 65365 : supertype(&super, &v1->tpe, &v2->tpe);
57 65365 : if (subtype_cmp(&v1->tpe, &super) != 0) {
58 1 : cast1 = atom_copy(sql->sa, v1);
59 1 : if (!(cast1 = atom_cast(sql->sa, cast1, &super)))
60 : return NULL;
61 : }
62 65364 : if (subtype_cmp(&v2->tpe, &super) != 0) {
63 2 : cast2 = atom_copy(sql->sa, v2);
64 2 : if (!(cast2 = atom_cast(sql->sa, cast2, &super)))
65 : return NULL;
66 : }
67 65364 : return atom_cmp(cast1, cast2) > 0 ? cast2 : cast1;
68 : }
69 :
70 : static inline void
71 3529240 : set_minmax_property(mvc *sql, sql_exp *e, rel_prop kind, atom *val)
72 : {
73 3529240 : if (val == NULL)
74 : return;
75 :
76 3529238 : sql_subtype *tpe = exp_subtype(e);
77 3529239 : prop *found = find_prop(e->p, kind);
78 :
79 3529239 : if (subtype_cmp(&val->tpe, tpe) != 0) { /* make sure it's the same type */
80 18844 : val = atom_copy(sql->sa, val);
81 18844 : if (!(val = atom_cast(sql->sa, val, tpe)))
82 : return;
83 : }
84 3529134 : if (found) {
85 123762 : found->value.pval = val;
86 : } else {
87 3405372 : prop *p = e->p = prop_create(sql->sa, kind, e->p);
88 3405365 : p->value.pval = val;
89 : }
90 : }
91 :
92 : extern sql_hash *sql_functions_lookup __attribute__((__visibility__("hidden")));
93 : extern void initialize_sql_functions_lookup(sql_allocator *sa) __attribute__((__visibility__("hidden")));
94 :
95 : #endif /*_REL_STATISTICS_H_*/
|