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_REL_H_
10#define _REL_REL_H_
11
12#include "sql_mvc.h"
13#include "sql_relation.h"
14#include "sql_semantic.h"
15
16#define sql_from 1
17#define sql_where 2
18#define sql_sel 4
19#define sql_having 8
20#define sql_orderby 16
21#define sql_groupby 32 //ORed
22#define sql_aggr 64 //ORed
23#define sql_farg 128 //ORed
24#define sql_window 256 //ORed
25#define sql_join 512 //ORed
26#define sql_outer 1024 //ORed
27
28#define is_sql_from(X) ((X & sql_from) == sql_from)
29#define is_sql_where(X) ((X & sql_where) == sql_where)
30#define is_sql_sel(X) ((X & sql_sel) == sql_sel)
31#define is_sql_having(X) ((X & sql_having) == sql_having)
32#define is_sql_orderby(X) ((X & sql_orderby) == sql_orderby)
33#define is_sql_groupby(X) ((X & sql_groupby) == sql_groupby)
34#define is_sql_aggr(X) ((X & sql_aggr) == sql_aggr)
35#define is_sql_farg(X) ((X & sql_farg) == sql_farg)
36#define is_sql_window(X) ((X & sql_window) == sql_window)
37#define is_sql_join(X) ((X & sql_join) == sql_join)
38#define is_sql_outer(X) ((X & sql_outer) == sql_outer)
39
40#define rel_groupby_gbe(m,r,e) rel_groupby(m, r, append(new_exp_list(m->sa), e))
41#define new_rel_list(sa) sa_list(sa)
42
43#define is_updateble(rel) \
44 (rel->op == op_basetable || \
45 (rel->op == op_ddl && (rel->flag == ddl_create_table || rel->flag == ddl_alter_table)))
46
47extern const char *rel_name( sql_rel *r );
48extern sql_rel *rel_distinct(sql_rel *l);
49
50extern sql_rel *rel_dup(sql_rel *r);
51extern void rel_destroy(sql_rel *rel);
52extern sql_rel *rel_create(sql_allocator *sa);
53extern sql_rel *rel_copy(mvc *sql, sql_rel *r, int deep);
54extern sql_rel *rel_select_copy(sql_allocator *sa, sql_rel *l, list *exps);
55
56extern sql_exp *rel_bind_column( mvc *sql, sql_rel *rel, const char *cname, int f );
57extern sql_exp *rel_bind_column2( mvc *sql, sql_rel *rel, const char *tname, const char *cname, int f );
58extern sql_exp *rel_first_column(mvc *sql, sql_rel *rel);
59
60extern sql_rel *rel_inplace_setop(sql_rel *rel, sql_rel *l, sql_rel *r, operator_type setop, list *exps);
61extern sql_rel *rel_inplace_project(sql_allocator *sa, sql_rel *rel, sql_rel *l, list *e);
62extern sql_rel *rel_inplace_groupby(sql_rel *rel, sql_rel *l, list *groupbyexps, list *exps );
63
64extern int rel_convert_types(mvc *sql, sql_rel *ll, sql_rel *rr, sql_exp **L, sql_exp **R, int scale_fixing, check_type tpe);
65extern sql_rel *rel_setop(sql_allocator *sa, sql_rel *l, sql_rel *r, operator_type setop);
66extern sql_rel *rel_setop_check_types(mvc *sql, sql_rel *l, sql_rel *r, list *ls, list *rs, operator_type op);
67extern sql_rel *rel_crossproduct(sql_allocator *sa, sql_rel *l, sql_rel *r, operator_type join);
68
69/* in case e is an constant and rel is a simple project of only e, free rel */
70extern sql_exp *rel_is_constant(sql_rel **rel, sql_exp *e);
71
72extern sql_rel *rel_topn(sql_allocator *sa, sql_rel *l, list *exps );
73extern sql_rel *rel_sample(sql_allocator *sa, sql_rel *l, list *exps );
74
75extern sql_rel *rel_label( mvc *sql, sql_rel *r, int all);
76extern sql_exp *rel_project_add_exp( mvc *sql, sql_rel *rel, sql_exp *e);
77extern sql_rel *rel_select_add_exp(sql_allocator *sa, sql_rel *l, sql_exp *e);
78extern void rel_join_add_exp(sql_allocator *sa, sql_rel *rel, sql_exp *e);
79extern sql_exp *rel_groupby_add_aggr(mvc *sql, sql_rel *rel, sql_exp *e);
80
81extern sql_rel *rel_select(sql_allocator *sa, sql_rel *l, sql_exp *e);
82extern sql_rel *rel_basetable(mvc *sql, sql_table *t, const char *tname);
83extern sql_rel *rel_groupby(mvc *sql, sql_rel *l, list *groupbyexps );
84extern sql_rel *rel_project(sql_allocator *sa, sql_rel *l, list *e);
85extern sql_rel *rel_project_exp(sql_allocator *sa, sql_exp *e);
86extern sql_rel *rel_exception(sql_allocator *sa, sql_rel *l, sql_rel *r, list *exps);
87
88extern sql_rel *rel_relational_func(sql_allocator *sa, sql_rel *l, list *exps);
89extern sql_rel *rel_table_func(sql_allocator *sa, sql_rel *l, sql_exp *f, list *exps, int kind);
90
91extern list *_rel_projections(mvc *sql, sql_rel *rel, const char *tname, int settname , int intern, int basecol);
92extern list *rel_projections(mvc *sql, sql_rel *rel, const char *tname, int settname , int intern);
93extern sql_rel *rel_safe_project(mvc *sql, sql_rel *rel);
94
95extern sql_rel *rel_push_select(mvc *sql, sql_rel *rel, sql_exp *ls, sql_exp *e);
96extern sql_rel *rel_push_join(mvc *sql, sql_rel *rel, sql_exp *ls, sql_exp *rs, sql_exp *rs2, sql_exp *e);
97extern sql_rel *rel_or(mvc *sql, sql_rel *rel, sql_rel *l, sql_rel *r, list *oexps, list *lexps, list *rexps);
98
99extern sql_table *rel_ddl_table_get(sql_rel *r);
100
101extern sql_rel *rel_add_identity(mvc *sql, sql_rel *rel, sql_exp **exp);
102extern sql_rel *rel_add_identity2(mvc *sql, sql_rel *rel, sql_exp **exp);
103extern sql_exp * rel_find_column( sql_allocator *sa, sql_rel *rel, const char *tname, const char *cname );
104
105extern int rel_in_rel(sql_rel *super, sql_rel *sub);
106
107extern list *rel_dependencies(mvc *sql, sql_rel *r);
108extern sql_exp * exps_find_match_exp(list *l, sql_exp *e);
109
110typedef sql_exp *(*exp_rewrite_fptr)(mvc *sql, sql_rel *rel, sql_exp *e, int depth /* depth of the nested expression */ );
111extern sql_rel *rel_exp_visitor(mvc *sql, sql_rel *rel, exp_rewrite_fptr exp_rewriter);
112
113typedef sql_rel *(*rel_rewrite_fptr)(mvc *sql, sql_rel *rel);
114extern sql_rel *rel_visitor(mvc *sql, sql_rel *rel, rel_rewrite_fptr rel_rewriter);
115
116#endif /* _REL_REL_H_ */
117