Skip to content

Commit

Permalink
feat: 补齐字符串类函数
Browse files Browse the repository at this point in the history
  • Loading branch information
cyz-2023 authored and wy1433 committed Apr 19, 2024
1 parent cd28db2 commit 0874a84
Show file tree
Hide file tree
Showing 7 changed files with 555 additions and 16 deletions.
1 change: 1 addition & 0 deletions include/common/datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern int32_t datetime_to_time(uint64_t datetime);
extern uint64_t time_to_datetime(int32_t time);
extern std::string time_to_str(int32_t time);
extern int32_t str_to_time(const char* str_time);
extern int32_t time_to_seconds(int32_t time);
extern int32_t seconds_to_time(int32_t seconds);
struct DateTime;
extern uint64_t bin_date_to_datetime(DateTime time_struct);
Expand Down
20 changes: 20 additions & 0 deletions include/expr/internal_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ ExprValue lpad(const std::vector<ExprValue>& input);
ExprValue rpad(const std::vector<ExprValue>& input);
ExprValue instr(const std::vector<ExprValue>& input);
ExprValue json_extract(const std::vector<ExprValue>& input);
ExprValue export_set(const std::vector<ExprValue>& input);
ExprValue to_base64(const std::vector<ExprValue>& input);
ExprValue from_base64(const std::vector<ExprValue>& input);
ExprValue make_set(const std::vector<ExprValue>& input);
ExprValue oct(const std::vector<ExprValue>& input);
ExprValue hex(const std::vector<ExprValue>& input);
ExprValue unhex(const std::vector<ExprValue>& input);
ExprValue bin(const std::vector<ExprValue>& input);
ExprValue space(const std::vector<ExprValue>& input);
ExprValue elt(const std::vector<ExprValue>& input);
ExprValue char_length(const std::vector<ExprValue>& input);
ExprValue format(const std::vector<ExprValue>& input);
ExprValue field(const std::vector<ExprValue>& input);
ExprValue quote(const std::vector<ExprValue>& input);
ExprValue func_char(const std::vector<ExprValue>& input);
ExprValue soundex(const std::vector<ExprValue>& input);


// datetime functions
ExprValue unix_timestamp(const std::vector<ExprValue>& input);
Expand Down Expand Up @@ -118,6 +135,9 @@ ExprValue tso_to_timestamp(const std::vector<ExprValue>& input);
ExprValue timestamp_to_tso(const std::vector<ExprValue>& input);
ExprValue to_days(const std::vector<ExprValue>& input);
ExprValue to_seconds(const std::vector<ExprValue>& input);
ExprValue addtime(const std::vector<ExprValue>& input);
ExprValue subtime(const std::vector<ExprValue>& input);

// hll functions
ExprValue hll_add(const std::vector<ExprValue>& input);
ExprValue hll_merge(const std::vector<ExprValue>& input);
Expand Down
2 changes: 0 additions & 2 deletions include/sqlparser/sql_lex.l
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ REAL { return REAL; }
REFERENCES { return REFERENCES; }
REGEXP { return REGEXP; }
RENAME { return RENAME; }
REPEAT { return REPEAT; }
REPLACE { return REPLACE; }
MERGE { return MERGE; }
RESTRICT { return RESTRICT; }
Expand Down Expand Up @@ -337,7 +336,6 @@ REDUNDANT { un_reserved_keyword(yylval, yyscanner, parser); return REDUNDANT; }
RELOAD { un_reserved_keyword(yylval, yyscanner, parser); return RELOAD; }
REPEATABLE { un_reserved_keyword(yylval, yyscanner, parser); return REPEATABLE; }
REPLICATION { un_reserved_keyword(yylval, yyscanner, parser); return REPLICATION; }
REVERSE { un_reserved_keyword(yylval, yyscanner, parser); return REVERSE; }
ROLLBACK { un_reserved_keyword(yylval, yyscanner, parser); return ROLLBACK; }
ROUTINE { un_reserved_keyword(yylval, yyscanner, parser); return ROUTINE; }
ROW { un_reserved_keyword(yylval, yyscanner, parser); return ROW; }
Expand Down
33 changes: 29 additions & 4 deletions include/sqlparser/sql_parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ extern int sql_error(YYLTYPE* yylloc, yyscan_t yyscanner, SqlParser* parser, con
REFERENCES
REGEXP
RENAME
REPEAT
REPLACE
MERGE
RESTRICT
Expand Down Expand Up @@ -385,7 +384,6 @@ extern int sql_error(YYLTYPE* yylloc, yyscan_t yyscanner, SqlParser* parser, con
RELOAD
REPEATABLE
REPLICATION
REVERSE
ROLLBACK
ROUTINE
ROW
Expand Down Expand Up @@ -2023,7 +2021,7 @@ FunctionNameDateArith:
DATE_ADD | DATE_SUB
;
FunctionNameSubstring:
SUBSTR | SUBSTRING
SUBSTR | SUBSTRING | MID
;

TimestampUnit:
Expand Down Expand Up @@ -2502,6 +2500,34 @@ FunctionCallNonKeyword:
fun->children.push_back($5, parser->arena);
$$ = fun;
}
| FORMAT '(' Expr ',' Expr ')' {
FuncExpr* fun = new_node(FuncExpr);
fun->fn_name = "format";
fun->children.push_back($3, parser->arena);
fun->children.push_back($5, parser->arena);
$$ = fun;
}
| INSERT '(' Expr ',' Expr ',' Expr ',' Expr ')' {
FuncExpr* fun = new_node(FuncExpr);
fun->fn_name = "insert";
fun->children.push_back($3, parser->arena);
fun->children.push_back($5, parser->arena);
fun->children.push_back($7, parser->arena);
fun->children.push_back($9, parser->arena);
$$ = fun;
}
| ASCII '(' Expr ')' {
FuncExpr* fun = new_node(FuncExpr);
fun->fn_name = "ascii";
fun->children.push_back($3, parser->arena);
$$ = fun;
}
| CHAR '(' ExprList ')' {
FuncExpr* fun = new_node(FuncExpr);
fun->fn_name = "char";
fun->children = $3->children;
$$ = fun;
}
;
FunctionCallKeyword:
VALUES '(' ColumnName ')' {
Expand Down Expand Up @@ -2889,7 +2915,6 @@ AllIdent:
| RELOAD
| REPEATABLE
| REPLICATION
| REVERSE
| ROLLBACK
| ROUTINE
| ROW
Expand Down
17 changes: 17 additions & 0 deletions src/common/datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,23 @@ std::string time_to_str(int32_t time) {
snprintf(buf, sizeof(buf), "%s%02d:%02d:%02d", OP_STR[minus], hour, min, sec);
return std::string(buf);
}

int32_t time_to_seconds(int32_t time) {
bool minus = false;
if (time < 0) {
minus = true;
time = -time;
}
int hour = (time >> 12) & 0x3FF;
int min = (time >> 6) & 0x3F;
int sec = time & 0x3F;
int32_t res = sec + min * 60 + hour * 3600;
if (minus) {
res = res * -1;
}
return res;
}

int32_t str_to_time(const char* str_time) {
while (*str_time == ' ') {
str_time++;
Expand Down
21 changes: 21 additions & 0 deletions src/expr/fn_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ void FunctionManager::register_operators() {
register_object_ret("lcase", lower, pb::STRING);
register_object_ret("concat", concat, pb::STRING);
register_object_ret("substr", substr, pb::STRING);
register_object_ret("mid", substr, pb::STRING);
register_object_ret("left", left, pb::STRING);
register_object_ret("right", right, pb::STRING);
register_object_ret("trim", trim, pb::STRING);
Expand All @@ -153,6 +154,23 @@ void FunctionManager::register_operators() {
register_object_ret("rpad", rpad, pb::STRING);
register_object_ret("instr", instr, pb::INT32);
register_object_ret("json_extract", json_extract, pb::STRING);
register_object_ret("export_set", export_set, pb::STRING);
register_object_ret("to_base64", to_base64, pb::STRING);
register_object_ret("from_base64", from_base64, pb::STRING);
register_object_ret("make_set", make_set, pb::STRING);
register_object_ret("oct", oct, pb::STRING);
register_object_ret("hex", hex, pb::STRING);
register_object_ret("unhex", unhex, pb::STRING);
register_object_ret("bin", bin, pb::STRING);
register_object_ret("space", space, pb::STRING);
register_object_ret("elt", elt, pb::STRING);
register_object_ret("char_length", char_length, pb::INT32);
register_object_ret("format", format, pb::STRING);
register_object_ret("field", field, pb::INT32);
register_object_ret("quote", quote, pb::STRING);
register_object_ret("char", func_char, pb::STRING);
register_object_ret("soundex", soundex, pb::STRING);


// date funcs
register_object_ret("unix_timestamp", unix_timestamp, pb::INT64);
Expand All @@ -171,6 +189,9 @@ void FunctionManager::register_operators() {
register_object_ret("quarter", func_quarter, pb::INT32);
register_object_ret("microsecond", microsecond, pb::INT32);
register_object_ret("timestampadd", timestampadd, pb::TIMESTAMP);
register_object_ret("adddate", date_add, pb::DATETIME);
register_object_ret("addtime", addtime, pb::STRING);
register_object_ret("subtime", subtime, pb::STRING);
/*
str_to_date实现较为复杂,需要满足任意格式的string转换为标准形式的DATETIME,现在为了方便确保str_to_date可以使用,
默认string是标准形式的date,故其实现内容和date_format函数一致
Expand Down
Loading

0 comments on commit 0874a84

Please sign in to comment.