1 | /* |
2 | * This Source Code Form is subject to the terms of the Mozilla Public |
3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
5 | * |
6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
7 | */ |
8 | |
9 | #ifndef _REL_EXP_H_ |
10 | #define _REL_EXP_H_ |
11 | |
12 | #include "sql_relation.h" |
13 | #include "sql_mvc.h" |
14 | #include "sql_atom.h" |
15 | |
16 | #define new_exp_list(sa) sa_list(sa) |
17 | #define exp2list(sa,e) append(sa_list(sa),e) |
18 | |
19 | #define is_compare_func(sf) (!sf->func->s && \ |
20 | (strcmp(sf->func->base.name, "<") == 0 || strcmp(sf->func->base.name, "<=") == 0 || \ |
21 | strcmp(sf->func->base.name, "=") == 0 || strcmp(sf->func->base.name, "<>") == 0 || \ |
22 | strcmp(sf->func->base.name, ">") == 0 || strcmp(sf->func->base.name, ">=") == 0) ) |
23 | |
24 | #define is_notequal_func(sf) (!sf->func->s && strcmp(sf->func->base.name, "<>") == 0) |
25 | |
26 | extern comp_type compare_str2type(char *compare_op); |
27 | extern comp_type swap_compare( comp_type t ); |
28 | extern comp_type range2lcompare( int r ); |
29 | extern comp_type range2rcompare( int r ); |
30 | extern int compare2range( int l, int r ); |
31 | |
32 | extern sql_exp *exp_compare(sql_allocator *sa, sql_exp *l, sql_exp *r, int cmptype); |
33 | extern sql_exp *exp_compare2(sql_allocator *sa, sql_exp *l, sql_exp *r, sql_exp *h, int cmptype); |
34 | extern sql_exp *exp_filter(sql_allocator *sa, list *l, list *r, sql_subfunc *f, int anti); |
35 | extern sql_exp *exp_or(sql_allocator *sa, list *l, list *r, int anti); |
36 | extern sql_exp *exp_in(sql_allocator *sa, sql_exp *l, list *r, int cmptype); |
37 | extern sql_exp *exp_in_func(mvc *sql, sql_exp *le, sql_exp *vals, int anyequal, int is_tuple); |
38 | extern sql_exp *exp_compare_func(mvc *sql, sql_exp *le, sql_exp *re, sql_exp *oe, char *compareop, int quantifier); |
39 | |
40 | #define exp_fromtype(e) ((list*)e->r)->h->data |
41 | #define exp_totype(e) ((list*)e->r)->h->next->data |
42 | extern sql_exp *exp_convert(sql_allocator *sa, sql_exp *exp, sql_subtype *fromtype, sql_subtype *totype ); |
43 | extern str number2name(str s, int len, int i); |
44 | extern sql_exp *exp_op(sql_allocator *sa, list *l, sql_subfunc *f ); |
45 | extern sql_exp *exp_rank_op(sql_allocator *sa, list *largs, list *gbe, list *obe, sql_subfunc *f ); |
46 | |
47 | #define append(l,v) list_append(l,v) |
48 | #define exp_unop(sa,l,f) \ |
49 | exp_op(sa, append(new_exp_list(sa),l), f) |
50 | #define exp_binop(sa,l,r,f) \ |
51 | exp_op(sa, append(append(new_exp_list(sa),l),r), f) |
52 | #define exp_op3(sa,l,r,r2,f) \ |
53 | exp_op(sa, append(append(append(new_exp_list(sa),l),r),r2), f) |
54 | #define exp_op4(sa,l,r,r2,r3,f) \ |
55 | exp_op(sa, append(append(append(append(new_exp_list(sa),l),r),r2),r3), f) |
56 | extern sql_exp *exp_aggr(sql_allocator *sa, list *l, sql_subaggr *a, int distinct, int no_nils, unsigned int card, int has_nil ); |
57 | #define exp_aggr1(sa, e, a, d, n, c, hn) \ |
58 | exp_aggr(sa, append(new_exp_list(sa), e), a, d, n, c, hn) |
59 | extern sql_exp * exp_atom(sql_allocator *sa, atom *a); |
60 | extern sql_exp * exp_atom_max(sql_allocator *sa, sql_subtype *tpe); |
61 | extern sql_exp * exp_atom_bool(sql_allocator *sa, int b); |
62 | extern sql_exp * exp_atom_bte(sql_allocator *sa, bte i); |
63 | extern sql_exp * exp_atom_sht(sql_allocator *sa, sht i); |
64 | extern sql_exp * exp_atom_int(sql_allocator *sa, int i); |
65 | extern sql_exp * exp_atom_lng(sql_allocator *sa, lng l); |
66 | #ifdef HAVE_HGE |
67 | extern sql_exp * exp_atom_hge(sql_allocator *sa, hge l); |
68 | #endif |
69 | extern sql_exp * exp_atom_flt(sql_allocator *sa, flt f); |
70 | extern sql_exp * exp_atom_dbl(sql_allocator *sa, dbl d); |
71 | extern sql_exp * exp_atom_str(sql_allocator *sa, const char *s, sql_subtype *st); |
72 | extern sql_exp * exp_atom_clob(sql_allocator *sa, const char *s); |
73 | extern sql_exp * exp_atom_ptr(sql_allocator *sa, void *s); |
74 | extern sql_exp * exp_atom_ref(sql_allocator *sa, int i, sql_subtype *tpe); |
75 | extern sql_exp * exp_null(sql_allocator *sa, sql_subtype *tpe); |
76 | extern sql_exp * exp_param(sql_allocator *sa, const char *name, sql_subtype *tpe, int frame); |
77 | extern atom * exp_value(mvc *sql, sql_exp *e, atom **args, int maxarg); |
78 | extern sql_exp * exp_values(sql_allocator *sa, list *exps); |
79 | extern list * exp_get_values(sql_exp *e); /* get expression list from the values expression */ |
80 | extern list * exp_types(sql_allocator *sa, list *exps); |
81 | extern int have_nil(list *exps); |
82 | |
83 | extern sql_exp * exp_column(sql_allocator *sa, const char *rname, const char *name, sql_subtype *t, unsigned int card, int has_nils, int intern); |
84 | extern sql_exp * exp_propagate(sql_allocator *sa, sql_exp *ne, sql_exp *oe); |
85 | #define exp_ref(sa, e) exp_propagate(sa, exp_column(sa, exp_relname(e), exp_name(e), exp_subtype(e), exp_card(e), has_nil(e), is_intern(e)), e) |
86 | extern sql_exp * exp_alias(sql_allocator *sa, const char *arname, const char *acname, const char *org_rname, const char *org_cname, sql_subtype *t, unsigned int card, int has_nils, int intern); |
87 | extern sql_exp * exp_alias_or_copy( mvc *sql, const char *tname, const char *cname, sql_rel *orel, sql_exp *old); |
88 | extern sql_exp * exp_alias_ref(mvc *sql, sql_exp *e); |
89 | extern sql_exp * exp_set(sql_allocator *sa, const char *name, sql_exp *val, int level); |
90 | extern sql_exp * exp_var(sql_allocator *sa, const char *name, sql_subtype *type, int level); |
91 | extern sql_exp * exp_table(sql_allocator *sa, const char *name, sql_table *t, int level); |
92 | extern sql_exp * exp_return(sql_allocator *sa, sql_exp *val, int level); |
93 | extern sql_exp * exp_while(sql_allocator *sa, sql_exp *cond, list *stmts); |
94 | extern sql_exp * exp_exception(sql_allocator *sa, sql_exp *cond, char* error_message); |
95 | extern sql_exp * exp_if(sql_allocator *sa, sql_exp *cond, list *if_stmts, list *else_stmts); |
96 | extern sql_exp * exp_rel(mvc *sql, sql_rel * r); |
97 | |
98 | extern void exp_setname(sql_allocator *sa, sql_exp *e, const char *rname, const char *name ); |
99 | extern void exp_setrelname(sql_allocator *sa, sql_exp *e, int nr ); |
100 | extern void exp_setalias(sql_exp *e, const char *rname, const char *name); |
101 | extern void exp_prop_alias(sql_allocator *sa, sql_exp *e, sql_exp *oe); |
102 | |
103 | extern void noninternexp_setname(sql_allocator *sa, sql_exp *e, const char *rname, const char *name ); |
104 | extern char* make_label(sql_allocator *sa, int nr); |
105 | extern sql_exp* exp_label(sql_allocator *sa, sql_exp *e, int nr); |
106 | extern sql_exp* exp_label_table(sql_allocator *sa, sql_exp *e, int nr); |
107 | extern list* exps_label(sql_allocator *sa, list *exps, int nr); |
108 | |
109 | extern sql_exp * exp_copy( mvc *sql, sql_exp *e); |
110 | extern list * exps_copy( mvc *sql, list *exps); |
111 | extern list * exps_alias( sql_allocator *sa, list *exps); |
112 | |
113 | extern void exp_swap( sql_exp *e ); |
114 | |
115 | extern sql_subtype * exp_subtype( sql_exp *e ); |
116 | extern const char * exp_name( sql_exp *e ); |
117 | extern const char * exp_relname( sql_exp *e ); |
118 | extern const char * exp_func_name( sql_exp *e ); |
119 | extern unsigned int exp_card(sql_exp *e); |
120 | |
121 | extern const char *exp_find_rel_name(sql_exp *e); |
122 | |
123 | extern sql_exp *rel_find_exp( sql_rel *rel, sql_exp *e); |
124 | |
125 | extern int exp_cmp( sql_exp *e1, sql_exp *e2); |
126 | extern int exp_equal( sql_exp *e1, sql_exp *e2); |
127 | extern int exp_refers( sql_exp *p, sql_exp *c); |
128 | extern int exp_match( sql_exp *e1, sql_exp *e2); |
129 | extern sql_exp* exps_find_exp( list *l, sql_exp *e); |
130 | extern int exp_match_exp( sql_exp *e1, sql_exp *e2); |
131 | /* match just the column (cmp equality) expressions */ |
132 | extern int exp_match_col_exps( sql_exp *e, list *l); |
133 | extern int exps_match_col_exps( sql_exp *e1, sql_exp *e2); |
134 | /* todo rename */ |
135 | extern int exp_match_list( list *l, list *r); |
136 | extern int exp_is_join(sql_exp *e, list *rels); |
137 | extern int exp_is_eqjoin(sql_exp *e); |
138 | extern int exp_is_join_exp(sql_exp *e); |
139 | extern int exp_is_atom(sql_exp *e); |
140 | extern int exp_is_true(mvc *sql, sql_exp *e); |
141 | extern int exp_is_zero(mvc *sql, sql_exp *e); |
142 | extern int exp_is_not_null(mvc *sql, sql_exp *e); |
143 | extern int exp_is_null(mvc *sql, sql_exp *e); |
144 | extern int exp_is_rel(sql_exp *e); |
145 | extern int exp_has_rel(sql_exp *e); |
146 | extern int exps_have_rel_exp(list *exps); |
147 | extern sql_rel *exp_rel_get_rel(sql_allocator *sa, sql_exp *e); |
148 | extern sql_exp *exp_rel_update_exp(sql_allocator *sa, sql_exp *e); |
149 | extern sql_exp *exp_rel_label(mvc *sql, sql_exp *e); |
150 | extern int exps_are_atoms(list *exps); |
151 | extern int exp_has_func(sql_exp *e); |
152 | extern int exp_unsafe(sql_exp *e, int allow_identity); |
153 | extern int exp_has_sideeffect(sql_exp *e); |
154 | |
155 | /* returns 0 when the relation contain the passed expression else < 0 */ |
156 | extern int rel_has_exp(sql_rel *rel, sql_exp *e); |
157 | /* return 0 when the relation contain atleast one of the passed expressions else < 0 */ |
158 | extern int rel_has_exps(sql_rel *rel, list *e); |
159 | /* return 1 when the relation contains all of the passed expressions else 0 */ |
160 | extern int rel_has_all_exps(sql_rel *rel, list *e); |
161 | |
162 | extern sql_rel *find_rel(list *rels, sql_exp *e); |
163 | extern sql_rel *find_one_rel(list *rels, sql_exp *e); |
164 | |
165 | extern sql_exp *exps_bind_column( list *exps, const char *cname, int *ambiguous); |
166 | extern sql_exp *exps_bind_column2( list *exps, const char *rname, const char *cname); |
167 | extern sql_exp *exps_bind_alias( list *exps, const char *rname, const char *cname); |
168 | |
169 | extern unsigned int exps_card( list *l ); |
170 | extern void exps_fix_card( list *exps, unsigned int card); |
171 | extern void exps_setcard( list *exps, unsigned int card); |
172 | extern int exps_intern(list *exps); |
173 | |
174 | extern char *compare_func( comp_type t, int anti ); |
175 | extern int is_identity( sql_exp *e, sql_rel *r); |
176 | |
177 | extern atom *exp_flatten(mvc *sql, sql_exp *e); |
178 | |
179 | extern void exp_sum_scales(sql_subfunc *f, sql_exp *l, sql_exp *r); |
180 | |
181 | extern sql_exp *create_table_part_atom_exp(mvc *sql, sql_subtype tpe, ptr value); |
182 | |
183 | extern int exp_aggr_is_count(sql_exp *e); |
184 | |
185 | extern void exps_reset_freevar(list *exps); |
186 | |
187 | extern int rel_set_type_recurse(mvc *sql, sql_subtype *type, sql_rel *rel, const char **relname, const char **expname); |
188 | #endif /* _REL_EXP_H_ */ |
189 | |