Skip to content

Commit a7126aa

Browse files
committed
ast can be freed after queryplanning; parse and use CHAR(x)
1 parent 94278c2 commit a7126aa

File tree

4 files changed

+71
-34
lines changed

4 files changed

+71
-34
lines changed

src/executor/statements/createTable.c

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,25 @@
11
#include "../../include/executor/executor.h"
22

3-
Datatype mapParsedDatatypeToEnumDatatype(char* parsed) {
43

54

6-
if (strcmp(parsed, "CHAR") == 0) return DTYPE_STR;
7-
if (strcmp(parsed, "INT") == 0) return DTYPE_LONG;
8-
if (strcmp(parsed, "LONG") == 0) return DTYPE_LONG;
5+
TDB constructTDB(CreateInfo info) {
96

107

11-
printf("Don't know how map nodetype %s to datatype\n", parsed);
12-
exit(1);
13-
}
14-
15-
16-
TDB constructTDB(Node* node) {
17-
checkPtrNotNull(node, "NULL-ptr to constructTDB\n");
18-
if (node->type != COLUMNLIST) {
19-
printf("Cannot create a TDB without a COLUMNLIST\n");
20-
exit(1);
21-
}
22-
238
struct TDB tbl;
24-
Node* ptr_col = node->child;
259

26-
size_t colIdx = 0;
27-
while (ptr_col != NULL) {
28-
checkPtrNotNull(ptr_col->child, "TDB needs datatype of column (NULL-ptr)\n");
29-
Datatype dtype = mapParsedDatatypeToEnumDatatype(ptr_col->child->content);
10+
for (size_t colIdx = 0; colIdx < info.colCount; colIdx++) {
3011

31-
strcpy(tbl.colNames[colIdx], ptr_col->content);
32-
tbl.datatypes[colIdx] = dtype;
12+
strcpy(tbl.colNames[colIdx], info.columns[colIdx].name);
13+
tbl.datatypes[colIdx] = info.columns[colIdx].type;
3314
tbl.lengths[colIdx] = sizeof(long);
34-
if (dtype == DTYPE_STR) {
35-
checkPtrNotNull(ptr_col->child->child, "STRING needs length\n");
36-
tbl.lengths[colIdx] = atoi(ptr_col->child->child->content);
15+
16+
if (tbl.datatypes[colIdx] == DTYPE_STR) {
17+
tbl.lengths[colIdx] = info.columns[colIdx].size;
3718
}
3819

39-
ptr_col = ptr_col->next;
40-
colIdx++;
4120
}
4221

43-
44-
tbl.colCount = colIdx;
22+
tbl.colCount = info.colCount;
4523
tbl.records = 0;
4624

4725
return tbl;
@@ -53,7 +31,7 @@ void executeCreateTable(Operator* op) {
5331
checkPtrNotNull(op, "Execute-error: nulltpr passed to executeCreateTable\n");
5432

5533

56-
struct TDB tbl = constructTDB(op->info.create.columnList);
34+
struct TDB tbl = constructTDB(op->info.create);
5735
strcpy(tbl.alias, op->info.create.objectName);
5836

5937
char filepath[CHARMAXSIZE];

src/include/planner/planner.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef struct {
4848
char resultSetAlias[CHARMAXSIZE];
4949
Datatype type;
5050
size_t identifier;
51+
size_t size;
5152
} ColumnMetadata;
5253

5354

@@ -123,7 +124,8 @@ typedef struct {
123124

124125

125126
typedef struct {
126-
Node* columnList;
127+
ColumnMetadata columns[ARRAYMAXSIZE];
128+
size_t colCount;
127129
char objectName[CHARMAXSIZE];
128130
} CreateInfo;
129131

@@ -152,6 +154,7 @@ typedef struct Operator {
152154

153155
void freeQueryplan(Operator *node);
154156
void copyResultDescription(Operator* opFrom, Operator* opTo, size_t offset);
157+
Datatype mapParsedDatatypeToEnumDatatype(char* parsed);
155158
void checkPtrNotNull(void* node, char* msg);
156159
void catalogFile(const char* path, TableMetadata* p_tablemetadata, char delimiter);
157160
int findColIdxInResDesc(ResultSet* resultDesc, char* name, char* tblref);

src/parser/parser.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,10 @@ void getDatatype() {
390390
ident(DATATYPE);
391391
skipWhite();
392392
if (isChar) {
393+
expectChar('(');
393394
nextToChild = true;
394395
number();
396+
expectChar(')');
395397
}
396398
}
397399

src/planner/planner.c

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ void freeQueryplan(Operator *node) {
2424
free(node);
2525
}
2626

27+
28+
Datatype mapParsedDatatypeToEnumDatatype(char* parsed) {
29+
30+
if (strcmp(parsed, "CHAR") == 0) return DTYPE_STR;
31+
if (strcmp(parsed, "INT") == 0) return DTYPE_LONG;
32+
if (strcmp(parsed, "LONG") == 0) return DTYPE_LONG;
33+
34+
printf("Don't know how map nodetype %s to datatype\n", parsed);
35+
exit(1);
36+
}
37+
38+
2739
void checkPtrNotNull(void* node, char* msg) {
2840
if (node == NULL) {
2941
printf("%s\n", msg);
@@ -129,7 +141,45 @@ Operator* buildCreateTable(Node* node) {
129141
*/
130142

131143
strcpy(op->info.create.objectName, node->next->next->content);
132-
op->info.create.columnList = node->next->next->next;
144+
145+
146+
147+
Node* ptr = node->next->next->next;
148+
149+
if (ptr->type != COLUMNLIST) {
150+
printf("Expected a COLUMNLIST planning CREATE TABLE\n");
151+
}
152+
153+
// Move to first column
154+
ptr = ptr->child;
155+
156+
size_t colIndex = 0;
157+
158+
while (ptr != NULL) {
159+
160+
if (ptr->type != IDENT_COL) {
161+
printf("Expected a IDENT_COL when planning CREATE TABLE\n");
162+
}
163+
164+
strcpy(op->info.create.columns[colIndex].name, ptr->content);
165+
166+
Datatype type = mapParsedDatatypeToEnumDatatype(ptr->child->content);
167+
op->info.create.columns[colIndex].type = type;
168+
169+
// For CHAR(), get the length
170+
if (type == DTYPE_STR) {
171+
if (ptr->child->child->type != NUMBER) {
172+
printf("Expected a NUMBER when parsing CHAR-definition in CREATE TABLE\n");
173+
}
174+
op->info.create.columns[colIndex].size = atol(ptr->child->child->content);
175+
}
176+
177+
colIndex++;
178+
179+
ptr = ptr->next;
180+
}
181+
182+
op->info.create.colCount = colIndex;
133183

134184
return op;
135185
}
@@ -225,5 +275,9 @@ Operator* planQuery(char* sqlStmt) {
225275
Node* ast = createParsetree();
226276
parse(sqlStmt, ast);
227277

228-
return planQueryAst((ast)->next);
278+
Operator* queryplan = planQueryAst((ast)->next);
279+
280+
freeTree(ast);
281+
282+
return queryplan;
229283
}

0 commit comments

Comments
 (0)