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 : extern void sql_column_get_statistics(mvc *sql, sql_column *c, sql_exp *e);
31 :
32 : static inline atom *
33 82163 : statistics_atom_max(mvc *sql, atom *v1, atom *v2)
34 : {
35 82163 : sql_subtype super;
36 82163 : atom *cast1 = v1, *cast2 = v2;
37 :
38 82163 : supertype(&super, &v1->tpe, &v2->tpe);
39 82163 : if (subtype_cmp(&v1->tpe, &super) != 0) {
40 2 : cast1 = atom_copy(sql->sa, v1);
41 2 : if (!(cast1 = atom_cast(sql->sa, cast1, &super)))
42 : return NULL;
43 : }
44 82162 : if (subtype_cmp(&v2->tpe, &super) != 0) {
45 4 : cast2 = atom_copy(sql->sa, v2);
46 4 : if (!(cast2 = atom_cast(sql->sa, cast2, &super)))
47 : return NULL;
48 : }
49 82162 : return atom_cmp(cast1, cast2) > 0 ? cast1 : cast2;
50 : }
51 :
52 : static inline atom *
53 80421 : statistics_atom_min(mvc *sql, atom *v1, atom *v2)
54 : {
55 80421 : sql_subtype super;
56 80421 : atom *cast1 = v1, *cast2 = v2;
57 :
58 80421 : supertype(&super, &v1->tpe, &v2->tpe);
59 80421 : if (subtype_cmp(&v1->tpe, &super) != 0) {
60 2 : cast1 = atom_copy(sql->sa, v1);
61 2 : if (!(cast1 = atom_cast(sql->sa, cast1, &super)))
62 : return NULL;
63 : }
64 80420 : if (subtype_cmp(&v2->tpe, &super) != 0) {
65 200 : cast2 = atom_copy(sql->sa, v2);
66 200 : if (!(cast2 = atom_cast(sql->sa, cast2, &super)))
67 : return NULL;
68 : }
69 80420 : return atom_cmp(cast1, cast2) > 0 ? cast2 : cast1;
70 : }
71 :
72 : static inline void
73 3635584 : set_minmax_property(mvc *sql, sql_exp *e, rel_prop kind, atom *val)
74 : {
75 3635584 : if (val == NULL)
76 : return;
77 :
78 3635582 : sql_subtype *tpe = exp_subtype(e);
79 3635581 : prop *found = find_prop(e->p, kind);
80 :
81 3635578 : if (subtype_cmp(&val->tpe, tpe) != 0) { /* make sure it's the same type */
82 4734 : val = atom_copy(sql->sa, val);
83 4734 : if (!(val = atom_cast(sql->sa, val, tpe)))
84 : return;
85 : }
86 3635512 : if (found) {
87 2070737 : found->value.pval = val;
88 : } else {
89 1564775 : prop *p = e->p = prop_create(sql->sa, kind, e->p);
90 1564774 : p->value.pval = val;
91 : }
92 : }
93 :
94 : extern sql_hash *sql_functions_lookup __attribute__((__visibility__("hidden")));
95 : extern void initialize_sql_functions_lookup(allocator *sa) __attribute__((__visibility__("hidden")));
96 :
97 : #endif /*_REL_STATISTICS_H_*/
|