-
Notifications
You must be signed in to change notification settings - Fork 0
/
relation.h
118 lines (95 loc) · 2.5 KB
/
relation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef RELATION_H_
#define RELATION_H_
#include "table.h"
typedef enum { TABLE, JOIN, QUERY } relation_type;
typedef enum join_type { INNER, LEFT, RIGHT, FULL } join_type;
typedef enum expression_type { ADD, SUBTRACT } expression_type;
typedef enum aggregration_type { COUNT, SUM, MIN, MAX, AVG, STDDEV } aggregation_type;
/* must be able to convert this to awk;
column names must be replaced by column numbers
*/
typedef struct expression {
enum expression_type head;
struct expression *arguments;
int is_aggregate;
} expression;
typedef struct target {
int id;
char *alias;
struct expression expr;
struct target *next;
} target;
/* A relation is the base object for
* tables, joins, and queries.
*
* From the perspective of the execution engine,
* joins are a tree.
*
* However, the parser always created joins which are
* a leftward expanding tree with the from table
* at the far left.
*/
typedef struct relation {
relation_type type;
struct table *table;
/* For a table, the columns are the same as table.columns.
* If there is an alias (in the FROM claus) it is appended.
*
* For a join, the alias (if any) is appended to the right
* relation.
*
* Aliases are required for subqueries. The top level query
* has a NULL alias.
*/
char *alias;
struct column *columns;
/* type: JOIN */
char *left_relation;
char *left_column_name;
char *right_relation;
char *right_column_name;
enum join_type join_type;
/* type: QUERY */
table *tables; /* how do we handle subqueries? */
struct relation *joins; /* NULL if no FROMclause; table relation if FROM but no JOIN */
struct target *targets;
struct expression where;
char **where_table_names; /* so that we know how soon we can apply where filter */
int *order_by_target_ids; /* store array size? */
int *group_by_targets_ids; /* store array size? */
expression *having;
int limit; /* actually an expr... */
int offset; /* actually an expr... */
} relation;
/* TODO: do we need these? */
typedef struct relation query;
typedef struct relation join;
void
relation_print(relation *rel);
query *
new_query();
/* returns SUCCESS
*/
int
query_add_table(table *tbl);
/* returns SUCCESS
*/
int
query_set_from_relation(table *tbl);
/* returns FAILURE if no FROM relation.
*/
int
query_add_join_relation();
int
query_set_where();
int
query_set_order_by();
int
query_set_group_by();
int
query_set_having();
int
query_set_limit();
int
query_set_offset();
#endif