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 63068 : statistics_atom_max(mvc *sql, atom *v1, atom *v2)
34 : {
35 63068 : sql_subtype super;
36 63068 : atom *cast1 = v1, *cast2 = v2;
37 :
38 63068 : supertype(&super, &v1->tpe, &v2->tpe);
39 63068 : 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 63067 : if (subtype_cmp(&v2->tpe, &super) != 0) {
45 7 : cast2 = atom_copy(sql->sa, v2);
46 7 : if (!(cast2 = atom_cast(sql->sa, cast2, &super)))
47 : return NULL;
48 : }
49 63067 : return atom_cmp(cast1, cast2) > 0 ? cast1 : cast2;
50 : }
51 :
52 : static inline atom *
53 61683 : statistics_atom_min(mvc *sql, atom *v1, atom *v2)
54 : {
55 61683 : sql_subtype super;
56 61683 : atom *cast1 = v1, *cast2 = v2;
57 :
58 61683 : supertype(&super, &v1->tpe, &v2->tpe);
59 61683 : 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 61682 : if (subtype_cmp(&v2->tpe, &super) != 0) {
65 201 : cast2 = atom_copy(sql->sa, v2);
66 201 : if (!(cast2 = atom_cast(sql->sa, cast2, &super)))
67 : return NULL;
68 : }
69 61682 : return atom_cmp(cast1, cast2) > 0 ? cast2 : cast1;
70 : }
71 :
72 : static inline void
73 3249227 : set_minmax_property(mvc *sql, sql_exp *e, rel_prop kind, atom *val)
74 : {
75 3249227 : if (val == NULL)
76 : return;
77 :
78 3249225 : sql_subtype *tpe = exp_subtype(e);
79 3249219 : prop *found = find_prop(e->p, kind);
80 :
81 3249226 : if (subtype_cmp(&val->tpe, tpe) != 0) { /* make sure it's the same type */
82 5032 : val = atom_copy(sql->sa, val);
83 5032 : if (!(val = atom_cast(sql->sa, val, tpe)))
84 : return;
85 : }
86 3249140 : if (found) {
87 1916098 : found->value.pval = val;
88 : } else {
89 1333042 : prop *p = e->p = prop_create(sql->sa, kind, e->p);
90 1333044 : 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_*/
|