Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commit-message: 释放未解析成语法树的id #479 #485

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/observer/sql/parser/lex_sql.l
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ while (0);
%{
#include<string.h>
#include<stdio.h>
#include <vector>

/**
* flex 代码包含三个部分,使用 %% 分隔
Expand Down Expand Up @@ -119,7 +120,7 @@ GROUP RETURN_TOKEN(GROUP);
BY RETURN_TOKEN(BY);
STORAGE RETURN_TOKEN(STORAGE);
FORMAT RETURN_TOKEN(FORMAT);
{ID} yylval->cstring=strdup(yytext); RETURN_TOKEN(ID);
{ID} yylval->cstring=strdup(yytext); static_cast<std::vector<char*>*>(yyextra)->push_back(yylval->cstring); RETURN_TOKEN(ID);
"(" RETURN_TOKEN(LBRACE);
")" RETURN_TOKEN(RBRACE);

Expand All @@ -136,8 +137,8 @@ FORMAT RETURN_TOKEN(FORMAT);
"-" |
"*" |
"/" { return yytext[0]; }
\"[^"]*\" yylval->cstring = strdup(yytext); RETURN_TOKEN(SSS);
'[^']*\' yylval->cstring = strdup(yytext); RETURN_TOKEN(SSS);
\"[^"]*\" yylval->cstring = strdup(yytext); static_cast<std::vector<char*>*>(yyextra)->push_back(yylval->cstring); RETURN_TOKEN(SSS);
'[^']*\' yylval->cstring = strdup(yytext); static_cast<std::vector<char*>*>(yyextra)->push_back(yylval->cstring); RETURN_TOKEN(SSS);

. LOG_DEBUG("Unknown character [%c]",yytext[0]); return yytext[0];
%%
Expand Down
35 changes: 10 additions & 25 deletions src/observer/sql/parser/yacc_sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ drop_table_stmt: /*drop table 语句的语法解析树*/
DROP TABLE ID {
$$ = new ParsedSqlNode(SCF_DROP_TABLE);
$$->drop_table.relation_name = $3;
free($3);
};

show_tables_stmt:
Expand All @@ -267,7 +266,6 @@ desc_table_stmt:
DESC ID {
$$ = new ParsedSqlNode(SCF_DESC_TABLE);
$$->desc_table.relation_name = $2;
free($2);
}
;

Expand All @@ -279,9 +277,6 @@ create_index_stmt: /*create index 语句的语法解析树*/
create_index.index_name = $3;
create_index.relation_name = $5;
create_index.attribute_name = $7;
free($3);
free($5);
free($7);
}
;

Expand All @@ -291,8 +286,6 @@ drop_index_stmt: /*drop index 语句的语法解析树*/
$$ = new ParsedSqlNode(SCF_DROP_INDEX);
$$->drop_index.index_name = $3;
$$->drop_index.relation_name = $5;
free($3);
free($5);
}
;
create_table_stmt: /*create table 语句的语法解析树*/
Expand All @@ -301,7 +294,7 @@ create_table_stmt: /*create table 语句的语法解析树*/
$$ = new ParsedSqlNode(SCF_CREATE_TABLE);
CreateTableSqlNode &create_table = $$->create_table;
create_table.relation_name = $3;
free($3);
//free($3);

vector<AttrInfoSqlNode> *src_attrs = $6;

Expand All @@ -314,7 +307,6 @@ create_table_stmt: /*create table 语句的语法解析树*/
delete $5;
if ($8 != nullptr) {
create_table.storage_format = $8;
free($8);
}
}
;
Expand Down Expand Up @@ -342,15 +334,13 @@ attr_def:
$$->type = (AttrType)$2;
$$->name = $1;
$$->length = $4;
free($1);
}
| ID type
{
$$ = new AttrInfoSqlNode;
$$->type = (AttrType)$2;
$$->name = $1;
$$->length = 4;
free($1);
}
;
number:
Expand All @@ -374,7 +364,6 @@ insert_stmt: /*insert 语句的语法解析树*/
$$->insertion.values.emplace_back(*$6);
reverse($$->insertion.values.begin(), $$->insertion.values.end());
delete $6;
free($3);
}
;

Expand Down Expand Up @@ -406,7 +395,6 @@ value:
char *tmp = common::substr($1,1,strlen($1)-2);
$$ = new Value(tmp);
free(tmp);
free($1);
}
;
storage_format:
Expand All @@ -429,7 +417,6 @@ delete_stmt: /* delete 语句的语法解析树*/
$$->deletion.conditions.swap(*$4);
delete $4;
}
free($3);
}
;
update_stmt: /* update 语句的语法解析树*/
Expand All @@ -443,8 +430,6 @@ update_stmt: /* update 语句的语法解析树*/
$$->update.conditions.swap(*$7);
delete $7;
}
free($2);
free($4);
}
;
select_stmt: /* select 语句的语法解析树*/
Expand Down Expand Up @@ -538,14 +523,11 @@ rel_attr:
ID {
$$ = new RelAttrSqlNode;
$$->attribute_name = $1;
free($1);
}
| ID DOT ID {
$$ = new RelAttrSqlNode;
$$->relation_name = $1;
$$->attribute_name = $3;
free($1);
free($3);
}
;

Expand All @@ -558,7 +540,6 @@ rel_list:
relation {
$$ = new vector<string>();
$$->push_back($1);
free($1);
}
| relation COMMA rel_list {
if ($3 != nullptr) {
Expand All @@ -568,7 +549,6 @@ rel_list:
}

$$->insert($$->begin(), $1);
free($1);
}
;

Expand Down Expand Up @@ -672,7 +652,6 @@ load_data_stmt:
$$ = new ParsedSqlNode(SCF_LOAD_DATA);
$$->load_data.relation_name = $7;
$$->load_data.file_name = tmp_file_name;
free($7);
free(tmp_file_name);
}
;
Expand All @@ -691,7 +670,6 @@ set_variable_stmt:
$$ = new ParsedSqlNode(SCF_SET_VARIABLE);
$$->set_variable.name = $2;
$$->set_variable.value = *$4;
free($2);
delete $4;
}
;
Expand All @@ -705,9 +683,16 @@ extern void scan_string(const char *str, yyscan_t scanner);

int sql_parse(const char *s, ParsedSqlResult *sql_result) {
yyscan_t scanner;
yylex_init(&scanner);
std::vector<char *> allocated_strings;
yylex_init_extra(static_cast<void*>(&allocated_strings),&scanner);
scan_string(s, scanner);
int result = yyparse(s, sql_result, scanner);

for (char *ptr : allocated_strings) {
free(ptr);
}
allocated_strings.clear();

yylex_destroy(scanner);
return result;
}
}
Loading