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
26extern comp_type compare_str2type(char *compare_op);
27extern comp_type swap_compare( comp_type t );
28extern comp_type range2lcompare( int r );
29extern comp_type range2rcompare( int r );
30extern int compare2range( int l, int r );
31
32extern sql_exp *exp_compare(sql_allocator *sa, sql_exp *l, sql_exp *r, int cmptype);
33extern sql_exp *exp_compare2(sql_allocator *sa, sql_exp *l, sql_exp *r, sql_exp *h, int cmptype);
34extern sql_exp *exp_filter(sql_allocator *sa, list *l, list *r, sql_subfunc *f, int anti);
35extern sql_exp *exp_or(sql_allocator *sa, list *l, list *r, int anti);
36extern sql_exp *exp_in(sql_allocator *sa, sql_exp *l, list *r, int cmptype);
37extern sql_exp *exp_in_func(mvc *sql, sql_exp *le, sql_exp *vals, int anyequal, int is_tuple);
38extern 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
42extern sql_exp *exp_convert(sql_allocator *sa, sql_exp *exp, sql_subtype *fromtype, sql_subtype *totype );
43extern str number2name(str s, int len, int i);
44extern sql_exp *exp_op(sql_allocator *sa, list *l, sql_subfunc *f );
45extern 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)
56extern 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)
59extern sql_exp * exp_atom(sql_allocator *sa, atom *a);
60extern sql_exp * exp_atom_max(sql_allocator *sa, sql_subtype *tpe);
61extern sql_exp * exp_atom_bool(sql_allocator *sa, int b);
62extern sql_exp * exp_atom_bte(sql_allocator *sa, bte i);
63extern sql_exp * exp_atom_sht(sql_allocator *sa, sht i);
64extern sql_exp * exp_atom_int(sql_allocator *sa, int i);
65extern sql_exp * exp_atom_lng(sql_allocator *sa, lng l);
66#ifdef HAVE_HGE
67extern sql_exp * exp_atom_hge(sql_allocator *sa, hge l);
68#endif
69extern sql_exp * exp_atom_flt(sql_allocator *sa, flt f);
70extern sql_exp * exp_atom_dbl(sql_allocator *sa, dbl d);
71extern sql_exp * exp_atom_str(sql_allocator *sa, const char *s, sql_subtype *st);
72extern sql_exp * exp_atom_clob(sql_allocator *sa, const char *s);
73extern sql_exp * exp_atom_ptr(sql_allocator *sa, void *s);
74extern sql_exp * exp_atom_ref(sql_allocator *sa, int i, sql_subtype *tpe);
75extern sql_exp * exp_null(sql_allocator *sa, sql_subtype *tpe);
76extern sql_exp * exp_param(sql_allocator *sa, const char *name, sql_subtype *tpe, int frame);
77extern atom * exp_value(mvc *sql, sql_exp *e, atom **args, int maxarg);
78extern sql_exp * exp_values(sql_allocator *sa, list *exps);
79extern list * exp_get_values(sql_exp *e); /* get expression list from the values expression */
80extern list * exp_types(sql_allocator *sa, list *exps);
81extern int have_nil(list *exps);
82
83extern sql_exp * exp_column(sql_allocator *sa, const char *rname, const char *name, sql_subtype *t, unsigned int card, int has_nils, int intern);
84extern 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)
86extern 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);
87extern sql_exp * exp_alias_or_copy( mvc *sql, const char *tname, const char *cname, sql_rel *orel, sql_exp *old);
88extern sql_exp * exp_alias_ref(mvc *sql, sql_exp *e);
89extern sql_exp * exp_set(sql_allocator *sa, const char *name, sql_exp *val, int level);
90extern sql_exp * exp_var(sql_allocator *sa, const char *name, sql_subtype *type, int level);
91extern sql_exp * exp_table(sql_allocator *sa, const char *name, sql_table *t, int level);
92extern sql_exp * exp_return(sql_allocator *sa, sql_exp *val, int level);
93extern sql_exp * exp_while(sql_allocator *sa, sql_exp *cond, list *stmts);
94extern sql_exp * exp_exception(sql_allocator *sa, sql_exp *cond, char* error_message);
95extern sql_exp * exp_if(sql_allocator *sa, sql_exp *cond, list *if_stmts, list *else_stmts);
96extern sql_exp * exp_rel(mvc *sql, sql_rel * r);
97
98extern void exp_setname(sql_allocator *sa, sql_exp *e, const char *rname, const char *name );
99extern void exp_setrelname(sql_allocator *sa, sql_exp *e, int nr );
100extern void exp_setalias(sql_exp *e, const char *rname, const char *name);
101extern void exp_prop_alias(sql_allocator *sa, sql_exp *e, sql_exp *oe);
102
103extern void noninternexp_setname(sql_allocator *sa, sql_exp *e, const char *rname, const char *name );
104extern char* make_label(sql_allocator *sa, int nr);
105extern sql_exp* exp_label(sql_allocator *sa, sql_exp *e, int nr);
106extern sql_exp* exp_label_table(sql_allocator *sa, sql_exp *e, int nr);
107extern list* exps_label(sql_allocator *sa, list *exps, int nr);
108
109extern sql_exp * exp_copy( mvc *sql, sql_exp *e);
110extern list * exps_copy( mvc *sql, list *exps);
111extern list * exps_alias( sql_allocator *sa, list *exps);
112
113extern void exp_swap( sql_exp *e );
114
115extern sql_subtype * exp_subtype( sql_exp *e );
116extern const char * exp_name( sql_exp *e );
117extern const char * exp_relname( sql_exp *e );
118extern const char * exp_func_name( sql_exp *e );
119extern unsigned int exp_card(sql_exp *e);
120
121extern const char *exp_find_rel_name(sql_exp *e);
122
123extern sql_exp *rel_find_exp( sql_rel *rel, sql_exp *e);
124
125extern int exp_cmp( sql_exp *e1, sql_exp *e2);
126extern int exp_equal( sql_exp *e1, sql_exp *e2);
127extern int exp_refers( sql_exp *p, sql_exp *c);
128extern int exp_match( sql_exp *e1, sql_exp *e2);
129extern sql_exp* exps_find_exp( list *l, sql_exp *e);
130extern int exp_match_exp( sql_exp *e1, sql_exp *e2);
131/* match just the column (cmp equality) expressions */
132extern int exp_match_col_exps( sql_exp *e, list *l);
133extern int exps_match_col_exps( sql_exp *e1, sql_exp *e2);
134/* todo rename */
135extern int exp_match_list( list *l, list *r);
136extern int exp_is_join(sql_exp *e, list *rels);
137extern int exp_is_eqjoin(sql_exp *e);
138extern int exp_is_join_exp(sql_exp *e);
139extern int exp_is_atom(sql_exp *e);
140extern int exp_is_true(mvc *sql, sql_exp *e);
141extern int exp_is_zero(mvc *sql, sql_exp *e);
142extern int exp_is_not_null(mvc *sql, sql_exp *e);
143extern int exp_is_null(mvc *sql, sql_exp *e);
144extern int exp_is_rel(sql_exp *e);
145extern int exp_has_rel(sql_exp *e);
146extern int exps_have_rel_exp(list *exps);
147extern sql_rel *exp_rel_get_rel(sql_allocator *sa, sql_exp *e);
148extern sql_exp *exp_rel_update_exp(sql_allocator *sa, sql_exp *e);
149extern sql_exp *exp_rel_label(mvc *sql, sql_exp *e);
150extern int exps_are_atoms(list *exps);
151extern int exp_has_func(sql_exp *e);
152extern int exp_unsafe(sql_exp *e, int allow_identity);
153extern int exp_has_sideeffect(sql_exp *e);
154
155/* returns 0 when the relation contain the passed expression else < 0 */
156extern 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 */
158extern int rel_has_exps(sql_rel *rel, list *e);
159/* return 1 when the relation contains all of the passed expressions else 0 */
160extern int rel_has_all_exps(sql_rel *rel, list *e);
161
162extern sql_rel *find_rel(list *rels, sql_exp *e);
163extern sql_rel *find_one_rel(list *rels, sql_exp *e);
164
165extern sql_exp *exps_bind_column( list *exps, const char *cname, int *ambiguous);
166extern sql_exp *exps_bind_column2( list *exps, const char *rname, const char *cname);
167extern sql_exp *exps_bind_alias( list *exps, const char *rname, const char *cname);
168
169extern unsigned int exps_card( list *l );
170extern void exps_fix_card( list *exps, unsigned int card);
171extern void exps_setcard( list *exps, unsigned int card);
172extern int exps_intern(list *exps);
173
174extern char *compare_func( comp_type t, int anti );
175extern int is_identity( sql_exp *e, sql_rel *r);
176
177extern atom *exp_flatten(mvc *sql, sql_exp *e);
178
179extern void exp_sum_scales(sql_subfunc *f, sql_exp *l, sql_exp *r);
180
181extern sql_exp *create_table_part_atom_exp(mvc *sql, sql_subtype tpe, ptr value);
182
183extern int exp_aggr_is_count(sql_exp *e);
184
185extern void exps_reset_freevar(list *exps);
186
187extern 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