Skip to content

Commit

Permalink
feat: add delete-where in sql parser
Browse files Browse the repository at this point in the history
`execute_delete_where()` in vm.h is not implemented yet.
  • Loading branch information
sinkinben committed Feb 2, 2022
1 parent 419875f commit ef0d99b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
12 changes: 7 additions & 5 deletions btree.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,16 @@ cursor_t *leaf_node_find(table_t *table, uint32_t page_num, uint32_t key)

/**
* Fake deletion of B+Tree
* Suppose there are `num_cell = 4` cells in `node` (which are [2,5,6,7]), and `key = 2`.
* 1) Suppose there are `num_cell = 4` cells in `node` (which are [2,5,6,7]), and `key = 2`.
* We simply delete the `key` like deletion in a sorted array (move data forward).
*
* After deleting `key = 2`, num_cells = 3 (which are [5,6,7]), and
*
* 2) After deleting `key = 2`, num_cells = 3 (which are [5,6,7]), and
* we DO NOT adjust the parent of `node`, and the structure of our B+Tree.
*
* Thus, we call it "fake_delete", since even if the row(cell) is deleted,
*
* 3) Thus, we call it "fake_delete", since even if the row(cell) is deleted,
* it still occupy disk space.
*
* 4) If the `num_cells` of leaf node is ZERO, then just return and delete nothing.
**/
void leaf_node_fake_delete(cursor_t *cursor, uint32_t key_to_delete)
{
Expand Down
13 changes: 6 additions & 7 deletions global.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#ifndef GLOBAL_H
#define GLOBAL_H
/**
* 1. 某些头文件的循环引用, 因此引入这一文件作为函数的全局声明, 破除循环 include
* 2. 工程上并不是一个好的设计, tinydb 为了方便, 把所有的函数实现都写在了 header file
* 1. 某些头文件存在循环引用, 因此引入这一文件作为函数的全局声明, 破除循环 include
* 2. 工程上并不是一个好的设计, tinydb 为了方便, 把所有的函数实现都写在了头文件
**/
void serialize_row(row_t *source, void *dest);
void *cursor_value(cursor_t *cursor);
void print_btree(pager_t *pager, uint32_t page_num, uint32_t indent_level);

#define TODO(filename, line_num) \
do \
{ \
printf("TODO at %s: %d\n", filename, line_num); \
assert(0); \
#define TODO(filename, line_num) \
do { \
printf("\033[31m TODO at %s: %d \033[0m \n", filename, line_num); \
assert(0); \
} while (0)

// #define CUSTOMED_LEAF_MAX_CELLS ((uint32_t)3)
Expand Down
3 changes: 3 additions & 0 deletions sql-parser/sqlex.l
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"rollback" |
"ROLLBACK" { return ROLLBACK; }

"delete" |
"DELETE" { return DELETE; }

"create" |
"CREATE" { return CREATE; }

Expand Down
17 changes: 13 additions & 4 deletions sql-parser/sqlparser.y
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void yyerror(const char *msg) { fprintf(stderr, "[tinydb] SQL Parser: %s\n", msg
}

// sql keywords
%token SELECT INSERT FROM TABLE CREATE WHERE VALUES INTO
%token SELECT INSERT CREATE DELETE
%token FROM TABLE WHERE VALUES INTO
%token COMMIT ROLLBACK

// sql operator
Expand Down Expand Up @@ -74,10 +75,18 @@ statement:
| selectsql {}
| createsql {}
| insertsql {}
| deletesql {}
| commitsql {}
| rollbacksql {}
;

deletesql:
DELETE FROM IDNAME WHERE conditions ';'
{
statement_set(stm_ptr, STATEMENT_DELETE_WHERE, $3, select_list, $5);
}
;

commitsql:
COMMIT ';'
{
Expand Down Expand Up @@ -112,15 +121,15 @@ selectsql:
}
| SELECT selectitemlist FROM IDNAME ';'
{
statement_set(stm_ptr, STATEMENT_SELECT_FIELDS, $4, select_list, NULL);
statement_set(stm_ptr, STATEMENT_SELECT_WHERE, $4, select_list, NULL);
}
| SELECT '*' FROM IDNAME WHERE conditions ';'
{
statement_set(stm_ptr, STATEMENT_SELECT_FIELDS, $4, select_list, $6);
statement_set(stm_ptr, STATEMENT_SELECT_WHERE, $4, select_list, $6);
}
| SELECT selectitemlist FROM IDNAME WHERE conditions ';'
{
statement_set(stm_ptr, STATEMENT_SELECT_FIELDS, $4, select_list, $6);
statement_set(stm_ptr, STATEMENT_SELECT_WHERE, $4, select_list, $6);
}
;

Expand Down
3 changes: 2 additions & 1 deletion sql-statement/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ typedef enum
{
STATEMENT_INSERT,
STATEMENT_SELECT,
STATEMENT_SELECT_FIELDS,
STATEMENT_SELECT_WHERE,
STATEMENT_UPDATE,
STATEMENT_DELETE,
STATEMENT_DELETE_WHERE,
STATEMENT_COMMIT,
STATEMENT_ROLLBACK
} statement_type_t;
Expand Down
29 changes: 20 additions & 9 deletions sql-vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
#define TINYDB_VM_H

/**
* The parsing result of a SQL statement sentence is stored in statement_t.
* The parsing result of a SQL statement is stored in statement_t.
* These `execute_xxx` functions are equal to the "Vitrual Machine" in sqlite.
* SQL Statement:
* - insert id username email
* - select
* - update id username email
* 1) execute_select, execute_delete are used by the dummy sql parser.
* > select (this will print all the rows)
* > delete [id] (this will delete the row whose id is `[id]` from B+Tree)
*
* 2) select_where, delete_where are used by the sql parser based on flex-bison.
* > select * from table where id > 10 and id < 30;
* > delete * from table where id > 10 and id < 30;
**/

execute_result_t execute_insert(statement_t *statement, table_t *table)
Expand Down Expand Up @@ -59,7 +63,7 @@ execute_result_t execute_select(statement_t *statement, table_t *table)
return EXECUTE_SUCCESS;
}

execute_result_t execute_select_fields(statement_t *statement, table_t *table)
execute_result_t execute_select_where(statement_t *statement, table_t *table)
{
cursor_t *cursor = table_start(table);
row_t row;
Expand Down Expand Up @@ -106,8 +110,6 @@ execute_result_t execute_delete(statement_t *statement, table_t *table)
cursor_t *cursor = table_exists(table, key_to_delete);
if (cursor != NULL)
{
// printf("[execute_delete] page num: %u\n", cursor->page_num);
// printf("[execute_delete] cell num: %u\n", cursor->cell_num);
leaf_node_fake_delete(cursor, key_to_delete);
#ifdef OPEN_TRANSACTION
row_t temp;
Expand All @@ -120,6 +122,13 @@ execute_result_t execute_delete(statement_t *statement, table_t *table)
return EXECUTE_NO_SUCH_KEY;
}

execute_result_t execute_delete_where(statement_t *statement, table_t *table)
{
/* There is bugs while scanning B+Tree and deleting at the same time. */
TODO(__FILE__, __LINE__);
return EXECUTE_UNKNOWN_STATEMENT;
}

execute_result_t execute_commit(statement_t *statement, table_t *table)
{
assert(statement->type == STATEMENT_COMMIT);
Expand Down Expand Up @@ -150,12 +159,14 @@ execute_result_t vm_executor(statement_t *statement, table_t *table)
return execute_insert(statement, table);
case STATEMENT_SELECT:
return execute_select(statement, table);
case STATEMENT_SELECT_FIELDS:
return execute_select_fields(statement, table);
case STATEMENT_SELECT_WHERE:
return execute_select_where(statement, table);
case STATEMENT_UPDATE:
return execute_update(statement, table);
case STATEMENT_DELETE:
return execute_delete(statement, table);
case STATEMENT_DELETE_WHERE:
return execute_delete_where(statement, table);
case STATEMENT_COMMIT:
return execute_commit(statement, table);
case STATEMENT_ROLLBACK:
Expand Down

0 comments on commit ef0d99b

Please sign in to comment.