Skip to content

Commit

Permalink
refact: refact sql parser and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkinben committed Jan 29, 2022
1 parent 3983d2f commit 81a3b21
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 60 deletions.
File renamed without changes.
1 change: 0 additions & 1 deletion sqlparser/calcdemo/calc.y → sql-parser/calcdemo/calc.y
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ term: NUMBER { $$ = $1; }
;

%%

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ run:
flex calc.l
bison -d calc.y
# use 'gcc -fl' if you build on Linux
gcc -ll calc.tab.c lex.yy.c
gcc -ll calc.tab.c lex.yy.c
./a.out
clean:
rm *.tab.c *.tab.h lex.yy.c a.out
11 changes: 9 additions & 2 deletions sqlparser/condition.h → sql-parser/condition.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "sql.h"
#include <assert.h>
#include "schema.h"
#ifndef __CONDITION_H__
#define __CONDITION_H__
typedef enum
Expand All @@ -14,7 +15,7 @@ typedef enum
} operator_t;

/**
* A condition_t denote: `lvalue op rvalue`,
* A condition_t struct denotes: `lvalue op rvalue`,
* 1) `lvalue` is always the column name.
* 2) `rvalue` could be a integer or a string.
* 3) When is_leaf = 1, then we should use lvalue/rvalue.
Expand Down Expand Up @@ -69,5 +70,11 @@ static void print_tree(condition_t *node, int depth)
}
}

static void destroy_tree(condition_t *node)
{
printf("TODO: PLEASE implement me. \n");
assert(0);
}


#endif
File renamed without changes.
46 changes: 46 additions & 0 deletions sql-parser/schema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "../list.h"
#include <stdint.h>
#include <stdlib.h>
#ifndef __SCHEMA_H__
#define __SCHEMA_H__

const uint32_t TBALE_COLUMN_NAME_MAX_LENGTH = 32;
typedef enum
{
DUMMY = -1,
COLUMN_INT, // -> uint_t in tinydb kernel
COLUMN_VARCHAR // -> varchar(n) in tinydb kernel
} column_type_t;

/* Table Schema
* This struct has two usages:
* 1) denote a column when executing create-sql
* 2) denote a table name temporarily when `column_type` = -1
*/
typedef struct
{
column_type_t column_type;
uint32_t width;
char fieldname[TBALE_COLUMN_NAME_MAX_LENGTH];
} schema_t;

struct schema_node_t
{
struct list_head entry;
schema_t schema;
};
typedef struct schema_node_t schema_node_t;


schema_node_t *alloc_schema_node(const char *filedname, uint32_t width, column_type_t column_type)
{
schema_node_t *node = (schema_node_t *)malloc(sizeof(schema_node_t));
strcpy(node->schema.fieldname, filedname);
node->schema.width = width;
node->schema.column_type = column_type;
init_list_head(&(node->entry));
return node;
}


#endif
File renamed without changes.
48 changes: 22 additions & 26 deletions sqlparser/sqlparser.y → sql-parser/sqlparser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "sql.h"
#include "schema.h"
#include "condition.h"
int yyparse();
int yylex();
Expand All @@ -18,7 +18,7 @@ condition_t *condition_tree; // select `select_list` from tbl where `condition_
{
int32_t intval;
uint32_t uintval;
char *strval;
const char *strval;
struct schema_node_t *schema_node;
struct schema_node_t *select_node;
struct condition_t *condition_tree;
Expand Down Expand Up @@ -53,13 +53,14 @@ statements: statements statement {}
| statement {}
;

statement: selectsql | createsql
statement: selectsql | createsql | '\n'
;

selectsql:
SELECT '*' FROM IDNAME ';' '\n'
{
printf("TODO: please select * from %s \n", $4);
printf("TODO: please select * from [%s] \n", $4);
printf("IDNAME = %p\n", $4);
}
| SELECT selectitemlist FROM IDNAME ';' '\n'
{
Expand All @@ -71,11 +72,14 @@ selectsql:
printf("%s ", fieldname);
}
printf("from %s \n", $4);
init_list_head(&(select_list->entry));
}
| SELECT '*' FROM IDNAME WHERE conditions ';' '\n'
{
printf("[SELECT] cond ptr = %p, table name = %s \n", $6, $4);
print_tree($6, 0);
init_list_head(&(select_list->entry));
condition_tree = NULL;
}
| SELECT selectitemlist FROM IDNAME WHERE conditions ';' '\n'
{
Expand All @@ -89,14 +93,15 @@ selectsql:
printf("from %s \n", $4);
printf("---> where condition is \n");
print_tree($6, 0);
init_list_head(&(select_list->entry));
condition_tree = NULL;
}
;

columnitem:
IDNAME
{
$$ = (schema_node_t *)malloc(sizeof(schema_node_t));
strcpy($$->schema.fieldname, $1);
$$ = alloc_schema_node($1, 0, -1);
}
;

Expand Down Expand Up @@ -148,7 +153,6 @@ conditions:
| '(' conditions ')'
{
$$ = $2;
printf("[conditions]");
}
| conditions logic_op conditions
{
Expand All @@ -164,7 +168,7 @@ conditions:
createsql:
CREATE TABLE IDNAME '(' createitemlist ')' ';' '\n'
{
// create table mytable (id int(16), msg1 varchar(32), msg2 varchar(64))
/* TODO: When after creating table, we should free the `createitemlist` */
printf("table name = %s\n", $3);
list_node_t *pos;
list_for_each(pos, &(schema_list->entry))
Expand All @@ -182,19 +186,11 @@ createsql:
createitem:
IDNAME INT '(' NUMBER ')'
{
$$ = (schema_node_t *)malloc(sizeof(schema_node_t));
schema_t *schema = &($$->schema);
schema->width = $4;
schema->column_type = COLUMN_INT;
strcpy(schema->fieldname, $1);
$$ = alloc_schema_node($1, $4, COLUMN_INT);
}
| IDNAME CHAR '(' NUMBER ')'
{
$$ = (schema_node_t *)malloc(sizeof(schema_node_t));
schema_t *schema = &($$->schema);
schema->width = $4;
schema->column_type = COLUMN_VARCHAR;
strcpy(schema->fieldname, $1);
$$ = alloc_schema_node($1, $4, COLUMN_VARCHAR);
}

;
Expand All @@ -206,8 +202,6 @@ createitemlist:
}
| createitemlist ',' createitem
{
// createitem($3) is the new item that lexer scanned
// printf("%s %s\n", $1->schema.fieldname, $3->schema.fieldname);
list_add_tail(&($3->entry), &(schema_list->entry));
}
;
Expand All @@ -222,13 +216,15 @@ void yyerror(const char *msg)

void __attribute__((constructor)) init()
{
/* used by create sql, e.g. create table tbl (`schema_list`) */
schema_list = (schema_node_t *)malloc(sizeof(schema_node_t));
init_list_head(&(schema_list->entry));
/* used by create sql, e.g. create table tbl (`schema_list`),
* schema_list is a dummy list head node.
*/
schema_list = alloc_schema_node("", 0, DUMMY);

/* used by select sql, e.g. select `select_list` from tbl */
select_list = (schema_node_t *)malloc(sizeof(schema_node_t));
init_list_head(&(select_list->entry));
/* used by select sql, e.g. select `select_list` from tbl,
* select_list is a dummy list head node.
*/
select_list = alloc_schema_node("", 0, DUMMY);

/* used by where condition, e.g.
* select `select_list` from tbl where `condition_tree`,
Expand Down
30 changes: 0 additions & 30 deletions sqlparser/sql.h

This file was deleted.

0 comments on commit 81a3b21

Please sign in to comment.