-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_parser.h
More file actions
139 lines (123 loc) · 3.38 KB
/
sql_parser.h
File metadata and controls
139 lines (123 loc) · 3.38 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef SQL_PARSER_H
#define SQL_PARSER_H
#define MAX_STRING_LENGTH 256
#define MAX_RES_TREE 25
#define BLOCK_SIZE 4096 // bytes
#define TUPLE_SIZE 100 // bytes (assumed average tuple size)
#define TUPLES_PER_BLOCK (BLOCK_SIZE / TUPLE_SIZE) // 40 tuples per block
#define INDEX_HEIGHT 1 // Assumed B+-tree height
#define BUFFER_BLOCKS 100 // Number of buffer blocks (M)
#define TS 40 // Seek time: 4ms = 40 * 0.1ms
#define TB 1 // Block transfer time: 0.1ms = 1 * 0.1ms
typedef enum
{
RA_EMPTY,
RA_PROJECTION,
RA_SELECTION,
RA_GROUPBY,
RA_ORDERBY,
RA_JOIN,
RA_SET_OP,
RA_WITH,
RA_WITH_LIST,
RA_WITH_QUERY,
RA_RELATION,
RA_ATTRIBUTE,
RA_ALIAS,
RA_DISTINCT,
RA_PROJECTION_LIST,
RA_GROUP_LIST,
RA_ORDER_LIST,
RA_ORDER_ITEM,
RA_INSERT,
RA_UPDATE,
RA_DELETE,
RA_VALUES,
RA_VALUE_LIST,
RA_VALUE_ITEMS,
RA_COLUMN_LIST,
RA_SET,
RA_SET_LIST,
RA_ASSIGNMENT,
RA_USING,
RA_FROM,
RA_JOIN_TYPE,
RA_OR,
RA_AND,
RA_NOT,
RA_COMPARISON,
RA_NULL_CHECK,
RA_IN,
RA_RANGE,
RA_CONCAT,
RA_ARITHMETIC,
RA_LITERAL,
RA_OPERATOR,
RA_EXISTS,
RA_BETWEEN,
RA_TRANSFORMED
} NodeType;
typedef struct
{
char *name;
int flag;
int num_distinct;
int max_len;
} attr;
typedef struct
{
char *TABLE_NAME;
attr *attr_list;
int num_tuples;
int num_attributes;
} TABLE;
struct scope_attr
{
char *name;
char *alias;
TABLE *tablename;
struct scope_attr *next;
};
typedef struct scope_attr scope_attr;
typedef struct ASTNode
{
NodeType type;
char value[MAX_STRING_LENGTH];
struct ASTNode *left;
struct ASTNode *right;
struct ASTNode *args;
struct ASTNode *condition; // For join conditions, WHERE, etc.
struct ASTNode *from; // For UPDATE's FROM clause
// scope_attr *scope; // For scoping attributes
int assoc_left_done;
int assoc_right_done;
} ASTNode;
extern ASTNode *parse_tree[10];
// Function prototypes
void yyerror(const char *s);
ASTNode *create_node(NodeType type, const char *value);
void print_tree(ASTNode *node, int depth);
void free_tree(ASTNode *node);
void print_subtree(ASTNode *node, int depth);
// Transformation functions
ASTNode *pre_transform_tree(ASTNode *node, scope_attr *req);
ASTNode *selection_pushdown(ASTNode *node);
ASTNode *projection_pushdown(ASTNode *node);
void join_associativity(ASTNode *node, ASTNode *node2, ASTNode *org_node);
void apply_join_transf(ASTNode *node);
ASTNode *apply_transformations(ASTNode *node);
// Helper functions
int condition_involves_only(ASTNode *cond, scope_attr *scope);
ASTNode *deep_copy_tree(ASTNode *original);
ASTNode *deep_copy_shallow(ASTNode *node);
ASTNode *extract_attributes_from_conditions(ASTNode *node);
// scope attribute functions
scope_attr *merge_scopes(scope_attr *left, scope_attr *right);
scope_attr *build_scope(ASTNode *node);
void free_scope(scope_attr *scope);
long long cost_calculation(ASTNode *node);
attr *get_attr_from_table(TABLE *table, const char *attr_name);
void print_tree_scope(ASTNode *node, int level);
long long estimate_size(ASTNode *node);
long long estimate_distinct_values(ASTNode *node, const char *attr_name);
#endif