patch 4.0
This commit is contained in:
@ -19,24 +19,18 @@
|
||||
#include "sql/parser/parse_node_hash.h"
|
||||
#include "sql/parser/parse_define.h"
|
||||
#include "sql/parser/sql_parser_base.h"
|
||||
extern const char* get_type_name(int type);
|
||||
extern const char *get_type_name(int type);
|
||||
|
||||
#ifdef SQL_PARSER_COMPILATION
|
||||
#include "sql/parser/parser_proxy_func.h"
|
||||
#endif // SQL_PARSER_COMPILATION
|
||||
#endif // SQL_PARSER_COMPILATION
|
||||
|
||||
#define WINDOW_FUNCTION_NUM 41
|
||||
struct FuncPair {
|
||||
char* func_name_;
|
||||
int yytokentype_;
|
||||
};
|
||||
int count_child(ParseNode *root, void *malloc_pool, int *count);
|
||||
|
||||
int count_child(ParseNode* root, void* malloc_pool, int* count);
|
||||
//merge_child:if succ ,return 0, else return 1
|
||||
int merge_child(ParseNode *node, void *malloc_pool, ParseNode *source_tree, int *index);
|
||||
|
||||
// merge_child:if succ ,return 0, else return 1
|
||||
int merge_child(ParseNode* node, void* malloc_pool, ParseNode* source_tree, int* index);
|
||||
|
||||
void destroy_tree(ParseNode* root)
|
||||
void destroy_tree(ParseNode *root)
|
||||
{
|
||||
(void)root;
|
||||
/*
|
||||
@ -64,41 +58,139 @@ void destroy_tree(ParseNode* root)
|
||||
}*/
|
||||
}
|
||||
|
||||
ParseNode* new_node(void* malloc_pool, ObItemType type, int num)
|
||||
int get_deep_copy_size(const ParseNode *node, int64_t *size)
|
||||
{
|
||||
int ret = OB_PARSER_SUCCESS;
|
||||
if (OB_UNLIKELY(NULL == node)) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR node is null\n");
|
||||
} else if (OB_UNLIKELY(NULL == size)) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR size is null\n");
|
||||
} else {
|
||||
*size += sizeof(ParseNode);
|
||||
if (node->str_len_ > 0 && node->str_value_ != NULL) {
|
||||
*size += node->str_len_ + 1;
|
||||
}
|
||||
if (node->text_len_ > 0 && node->raw_text_ != NULL) {
|
||||
*size += node->text_len_ + 1;
|
||||
}
|
||||
if (node->num_child_ > 0) {
|
||||
if (OB_UNLIKELY(NULL == node->children_)) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR children is null\n");
|
||||
} else {
|
||||
*size += (sizeof(ParseNode*) * node->num_child_);
|
||||
}
|
||||
for (int64_t i = 0; OB_PARSER_SUCCESS == ret && i < node->num_child_; ++i) {
|
||||
ParseNode *child_node = node->children_[i];
|
||||
if (OB_UNLIKELY(NULL == child_node)) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR child node is null\n");
|
||||
} else if (OB_PARSER_SUCCESS != (ret = get_deep_copy_size(child_node, size))) {
|
||||
(void)fprintf(stderr, "ERROR failed to get deep copy size\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int deep_copy_parse_node(void *malloc_pool, const ParseNode *src_node, ParseNode *dst_node)
|
||||
{
|
||||
int ret = OB_PARSER_SUCCESS;
|
||||
if (OB_UNLIKELY(NULL == src_node) || OB_UNLIKELY(NULL == dst_node)) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR node is null\n");
|
||||
} else if (OB_UNLIKELY(NULL == malloc_pool)) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR malloc pool is null\n");
|
||||
} else {
|
||||
dst_node->type_ = src_node->type_;
|
||||
dst_node->num_child_ = src_node->num_child_;
|
||||
dst_node->param_num_ = src_node->param_num_;
|
||||
dst_node->flag_ = src_node->flag_;
|
||||
dst_node->value_ = src_node->value_;
|
||||
dst_node->pos_ = src_node->pos_;
|
||||
dst_node->stmt_loc_ = src_node->stmt_loc_;
|
||||
dst_node->raw_param_idx_ = src_node->raw_param_idx_;
|
||||
#ifdef SQL_PARSER_COMPILATION
|
||||
dst_node->token_off_= src_node->token_off_;
|
||||
dst_node->token_len_ = src_node->token_len_;
|
||||
#endif
|
||||
if (src_node->str_len_ > 0 && src_node->str_value_ != NULL) {
|
||||
char *buf = NULL;
|
||||
if (OB_UNLIKELY(NULL == (buf = (char *)parser_alloc(malloc_pool, src_node->str_len_ + 1)))) {
|
||||
ret = OB_PARSER_ERR_NO_MEMORY;
|
||||
(void)fprintf(stderr, "ERROR failed to allocate memory\n");
|
||||
} else {
|
||||
dst_node->str_len_ = src_node->str_len_;
|
||||
MEMCPY(buf, src_node->str_value_, dst_node->str_len_);
|
||||
buf[dst_node->str_len_] = '\0';
|
||||
dst_node->str_value_ = buf;
|
||||
}
|
||||
}
|
||||
if (OB_PARSER_SUCCESS == ret && src_node->text_len_ > 0 && src_node->raw_text_ != NULL) {
|
||||
char *buf = NULL;
|
||||
if (OB_UNLIKELY(NULL == (buf = (char *)parser_alloc(malloc_pool, src_node->text_len_ + 1)))) {
|
||||
ret = OB_PARSER_ERR_NO_MEMORY;
|
||||
(void)fprintf(stderr, "ERROR failed to allocate memory\n");
|
||||
} else {
|
||||
dst_node->text_len_ = src_node->text_len_;
|
||||
dst_node->pos_ = src_node->pos_;
|
||||
MEMCPY(buf, src_node->raw_text_, dst_node->text_len_);
|
||||
buf[dst_node->text_len_] = '\0';
|
||||
dst_node->raw_text_ = buf;
|
||||
}
|
||||
}
|
||||
if (OB_PARSER_SUCCESS == ret && src_node->num_child_ > 0) {
|
||||
if (OB_UNLIKELY(NULL == src_node->children_)) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR children is null\n");
|
||||
} else if (OB_UNLIKELY(NULL == (dst_node->children_ = (ParseNode **)parser_alloc(
|
||||
malloc_pool, sizeof(ParseNode*) * src_node->num_child_)))) {
|
||||
ret = OB_PARSER_ERR_NO_MEMORY;
|
||||
(void)fprintf(stderr, "ERROR failed to allocate memory\n");
|
||||
}
|
||||
for (int64_t i = 0; OB_PARSER_SUCCESS == ret && i < src_node->num_child_; ++i) {
|
||||
ParseNode *dst_node = NULL;
|
||||
ParseNode *child_node = src_node->children_[i];
|
||||
if (OB_UNLIKELY(NULL == child_node)) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR child node is null\n");
|
||||
} else if (OB_UNLIKELY(NULL == (dst_node =
|
||||
(ParseNode *)parser_alloc(malloc_pool, sizeof(ParseNode))))) {
|
||||
ret = OB_PARSER_ERR_NO_MEMORY;
|
||||
(void)fprintf(stderr, "ERROR failed to allocate memory\n");
|
||||
} else if (OB_PARSER_SUCCESS != (ret = deep_copy_parse_node(malloc_pool, child_node, dst_node))) {
|
||||
(void)fprintf(stderr, "ERROR failed to deep copy parse node\n");
|
||||
} else {
|
||||
dst_node->children_[i] = dst_node;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ParseNode *new_node(void *malloc_pool, ObItemType type, int num)
|
||||
{
|
||||
// the mem alloced by parse_malloc has been memset;
|
||||
ParseNode* node = (ParseNode*)parse_malloc(sizeof(ParseNode), malloc_pool);
|
||||
ParseNode *node = (ParseNode *)parse_malloc(sizeof(ParseNode), malloc_pool);
|
||||
if (OB_UNLIKELY(NULL == node)) {
|
||||
(void)printf("malloc memory failed\n");
|
||||
} else {
|
||||
node->type_ = type;
|
||||
node->num_child_ = num;
|
||||
node->param_num_ = 0;
|
||||
node->is_neg_ = 0;
|
||||
node->is_hidden_const_ = 0;
|
||||
node->is_date_unit_ = 0;
|
||||
node->is_tree_not_param_ = 0;
|
||||
node->length_semantics_ = 0;
|
||||
node->is_change_to_char_ = 0;
|
||||
node->is_val_paramed_item_idx_ = 0;
|
||||
node->is_copy_raw_text_ = 0;
|
||||
node->is_column_varchar_ = 0;
|
||||
node->is_trans_from_minus_ = 0;
|
||||
node->is_assigned_from_child_ = 0;
|
||||
node->is_num_must_be_pos_ = 0;
|
||||
node->value_ = INT64_MAX;
|
||||
node->str_len_ = 0;
|
||||
node->str_value_ = NULL;
|
||||
node->text_len_ = 0;
|
||||
node->raw_text_ = NULL;
|
||||
node->pos_ = 0;
|
||||
node->str_off_ = -1;
|
||||
#ifdef SQL_PARSER_COMPILATION
|
||||
node->token_off_ = -1;
|
||||
node->token_len_ = -1;
|
||||
#endif
|
||||
if (num > 0) {
|
||||
int64_t alloc_size = sizeof(ParseNode*) * num;
|
||||
node->children_ = (ParseNode**)parse_malloc(alloc_size, malloc_pool);
|
||||
int64_t alloc_size = sizeof(ParseNode *) * num ;
|
||||
node->children_ = (ParseNode **)parse_malloc(alloc_size, malloc_pool);
|
||||
if (OB_UNLIKELY(NULL == node->children_)) {
|
||||
parse_free(node);
|
||||
node = NULL;
|
||||
@ -110,10 +202,10 @@ ParseNode* new_node(void* malloc_pool, ObItemType type, int num)
|
||||
return node;
|
||||
}
|
||||
|
||||
int count_child(ParseNode* root, void* malloc_pool, int* count)
|
||||
int count_child(ParseNode *root, void *malloc_pool, int *count)
|
||||
{
|
||||
int ret = OB_PARSER_SUCCESS;
|
||||
ParserLinkNode* stack_top = NULL;
|
||||
ParserLinkNode *stack_top = NULL;
|
||||
if (NULL == count) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR invalid parameter ret=%d\n", ret);
|
||||
@ -145,9 +237,9 @@ int count_child(ParseNode* root, void* malloc_pool, int* count)
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR invalid null children\n");
|
||||
}
|
||||
ParserLinkNode* tmp_node = NULL;
|
||||
ParserLinkNode *tmp_node = NULL;
|
||||
for (int64_t i = tree->num_child_ - 1; OB_PARSER_SUCCESS == ret && i >= 0; i--) {
|
||||
ParseNode* child = tree->children_[i];
|
||||
ParseNode *child = tree->children_[i];
|
||||
if (NULL == child) {
|
||||
// do nothing
|
||||
} else if (NULL == (tmp_node = new_link_node(malloc_pool))) {
|
||||
@ -158,23 +250,23 @@ int count_child(ParseNode* root, void* malloc_pool, int* count)
|
||||
tmp_node->next_ = stack_top;
|
||||
stack_top = tmp_node;
|
||||
}
|
||||
} // for end
|
||||
} // for end
|
||||
}
|
||||
} while (OB_PARSER_SUCCESS == ret && NULL != stack_top);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// merge_child:if succ ,return 0, else return 1
|
||||
int merge_child(ParseNode* node, void* malloc_pool, ParseNode* source_tree, int* index)
|
||||
//merge_child:if succ ,return 0, else return 1
|
||||
int merge_child(ParseNode *node, void *malloc_pool, ParseNode *source_tree, int *index)
|
||||
{
|
||||
int ret = 0;
|
||||
ParserLinkNode* stack_top = NULL;
|
||||
ParserLinkNode *stack_top = NULL;
|
||||
if (OB_UNLIKELY(NULL == node || NULL == index)) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR node%p or index:%p is NULL\n", node, index);
|
||||
} else if (NULL == source_tree) {
|
||||
// do nothing
|
||||
//do nothing
|
||||
} else if (NULL == (stack_top = new_link_node(malloc_pool))) {
|
||||
ret = OB_PARSER_ERR_NO_MEMORY;
|
||||
(void)fprintf(stderr, "ERROR failed to malloc memory\n");
|
||||
@ -195,8 +287,8 @@ int merge_child(ParseNode* node, void* malloc_pool, ParseNode* source_tree, int*
|
||||
} else if (T_LINK_NODE != tree->type_) {
|
||||
if (OB_UNLIKELY(*index < 0 || *index >= node->num_child_)) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(
|
||||
stderr, "ERROR invalid index: %d, num_child:%d\n tree: %d", *index, node->num_child_, tree->type_);
|
||||
(void)fprintf(stderr, "ERROR invalid index: %d, num_child:%d\n tree: %d",
|
||||
*index, node->num_child_, tree->type_);
|
||||
} else if (NULL == node->children_) {
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR invalid null children pointer\n");
|
||||
@ -210,7 +302,7 @@ int merge_child(ParseNode* node, void* malloc_pool, ParseNode* source_tree, int*
|
||||
ret = OB_PARSER_ERR_UNEXPECTED;
|
||||
(void)fprintf(stderr, "ERROR invalid children pointer\n");
|
||||
} else {
|
||||
ParserLinkNode* tmp_node = NULL;
|
||||
ParserLinkNode *tmp_node = NULL;
|
||||
for (int64_t i = tree->num_child_ - 1; OB_PARSER_SUCCESS == ret && i >= 0; i--) {
|
||||
if (NULL == tree->children_[i]) {
|
||||
// do nothing
|
||||
@ -223,30 +315,35 @@ int merge_child(ParseNode* node, void* malloc_pool, ParseNode* source_tree, int*
|
||||
tmp_node->next_ = stack_top;
|
||||
stack_top = tmp_node;
|
||||
}
|
||||
} // for end
|
||||
} // for end
|
||||
}
|
||||
} while (OB_PARSER_SUCCESS == ret && (stack_top != NULL));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ParseNode* merge_tree(void* malloc_pool, int* fatal_error, ObItemType node_tag, ParseNode* source_tree)
|
||||
ParseNode *merge_tree(void *malloc_pool, int *fatal_error,
|
||||
ObItemType node_tag, ParseNode *source_tree)
|
||||
{
|
||||
ParseNode* node = NULL;
|
||||
ParseNode* ret_node = NULL;
|
||||
ParseNode *node = NULL;
|
||||
ParseNode *ret_node = NULL;
|
||||
if (OB_UNLIKELY(NULL == malloc_pool) || OB_UNLIKELY(NULL == fatal_error)) {
|
||||
(void)fprintf(stderr, "ERROR parser result is NULL\n");
|
||||
} else if (NULL == source_tree) {
|
||||
// source_tree may be NULL, do nothing
|
||||
//source_tree may be NULL, do nothing
|
||||
} else {
|
||||
int index = 0;
|
||||
int num = 0;
|
||||
int tmp_ret = 0;
|
||||
if (OB_UNLIKELY(OB_PARSER_SUCCESS != (tmp_ret = count_child(source_tree, malloc_pool, &num)))) {
|
||||
if (OB_UNLIKELY(OB_PARSER_SUCCESS != (tmp_ret = count_child(source_tree,
|
||||
malloc_pool, &num)))) {
|
||||
(void)fprintf(stderr, "ERROR fail to , count child num code : %d\n", tmp_ret);
|
||||
*fatal_error = tmp_ret;
|
||||
} else if (OB_LIKELY(NULL != (node = new_node(malloc_pool, node_tag, num)))) {
|
||||
if (OB_UNLIKELY(OB_PARSER_SUCCESS != (tmp_ret = merge_child(node, malloc_pool, source_tree, &index)))) {
|
||||
if (OB_UNLIKELY(OB_PARSER_SUCCESS != (tmp_ret = merge_child(node,
|
||||
malloc_pool,
|
||||
source_tree,
|
||||
&index)))) {
|
||||
(void)fprintf(stderr, "ERROR fail to merge_child, error code : %d\n", tmp_ret);
|
||||
*fatal_error = tmp_ret;
|
||||
} else if (index != num) {
|
||||
@ -261,15 +358,15 @@ ParseNode* merge_tree(void* malloc_pool, int* fatal_error, ObItemType node_tag,
|
||||
return ret_node;
|
||||
}
|
||||
|
||||
ParseNode* new_terminal_node(void* malloc_pool, ObItemType type)
|
||||
ParseNode *new_terminal_node(void *malloc_pool, ObItemType type)
|
||||
{
|
||||
int children_num = 0;
|
||||
return new_node(malloc_pool, type, children_num);
|
||||
}
|
||||
|
||||
ParseNode* new_non_terminal_node(void* malloc_pool, ObItemType node_tag, int num, ...)
|
||||
ParseNode *new_non_terminal_node(void *malloc_pool, ObItemType node_tag, int num, ...)
|
||||
{
|
||||
ParseNode* ret_node = NULL;
|
||||
ParseNode *ret_node = NULL;
|
||||
if (OB_UNLIKELY(num <= 0)) {
|
||||
(void)fprintf(stderr, "ERROR invalid num:%d\n", num);
|
||||
} else {
|
||||
@ -279,7 +376,7 @@ ParseNode* new_non_terminal_node(void* malloc_pool, ObItemType node_tag, int num
|
||||
if (OB_LIKELY(NULL != ret_node)) {
|
||||
va_start(va, num);
|
||||
for (; i < num; ++i) {
|
||||
ret_node->children_[i] = va_arg(va, ParseNode*);
|
||||
ret_node->children_[i] = va_arg(va, ParseNode *);
|
||||
}
|
||||
va_end(va);
|
||||
}
|
||||
@ -287,9 +384,9 @@ ParseNode* new_non_terminal_node(void* malloc_pool, ObItemType node_tag, int num
|
||||
return ret_node;
|
||||
}
|
||||
|
||||
char* copy_expr_string(ParseResult* p, int expr_start, int expr_end)
|
||||
char *copy_expr_string(ParseResult *p, int expr_start, int expr_end)
|
||||
{
|
||||
char* expr_string = NULL;
|
||||
char *expr_string = NULL;
|
||||
if (OB_UNLIKELY(NULL == p)) {
|
||||
(void)fprintf(stderr, "ERROR parser result is NULL\n");
|
||||
} else if (OB_UNLIKELY(NULL == p->input_sql_)) {
|
||||
@ -298,9 +395,9 @@ char* copy_expr_string(ParseResult* p, int expr_start, int expr_end)
|
||||
(void)fprintf(stderr, "ERROR invalid argument, expr_start:%d, expr_end:%d\n", expr_start, expr_end);
|
||||
} else {
|
||||
int len = expr_end - expr_start + 1;
|
||||
expr_string = (char*)parse_malloc(len + 1, p->malloc_pool_);
|
||||
expr_string = (char *)parse_malloc(len + 1, p->malloc_pool_);
|
||||
if (OB_UNLIKELY(NULL == expr_string)) {
|
||||
(void)printf("malloc memory failed\n");
|
||||
(void)printf("malloc memory failed\n");
|
||||
} else {
|
||||
memmove(expr_string, p->input_sql_ + expr_start - 1, len);
|
||||
expr_string[len] = '\0';
|
||||
@ -308,34 +405,62 @@ char* copy_expr_string(ParseResult* p, int expr_start, int expr_end)
|
||||
}
|
||||
return expr_string;
|
||||
}
|
||||
int store_prentthese_info(int left, int right, ParseResult *result)
|
||||
{
|
||||
int ret = 0;
|
||||
if (OB_UNLIKELY(NULL == result)) {
|
||||
ret = -1;
|
||||
(void)fprintf(stderr, "ERROR invalid parentheses pointer\n");
|
||||
} else {
|
||||
ParenthesesOffset *object =
|
||||
(ParenthesesOffset *)malloc_parentheses_info(sizeof(ParenthesesOffset), result->malloc_pool_);
|
||||
if (OB_UNLIKELY(NULL == object)) {
|
||||
ret = OB_PARSER_ERR_NO_MEMORY;
|
||||
(void)fprintf(stderr, "ERROR invalid parentheses pointer\n");
|
||||
} else {
|
||||
object->left_parentheses_ = left;
|
||||
object->right_parentheses_ = right;
|
||||
object->next_ = NULL;
|
||||
if (NULL == result->ins_multi_value_res_->ref_parentheses_) {
|
||||
result->ins_multi_value_res_->ref_parentheses_ = object;
|
||||
result->ins_multi_value_res_->values_count_ = 1;
|
||||
} else {
|
||||
result->ins_multi_value_res_->tail_parentheses_->next_ = object;
|
||||
result->ins_multi_value_res_->values_count_++;
|
||||
}
|
||||
result->ins_multi_value_res_->tail_parentheses_ = object;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
unsigned char escaped_char(unsigned char c, int* with_back_slash)
|
||||
unsigned char escaped_char(unsigned char c, int *with_back_slash)
|
||||
{
|
||||
*with_back_slash = 0;
|
||||
switch (c) {
|
||||
case 'n':
|
||||
return '\n';
|
||||
case 't':
|
||||
return '\t';
|
||||
case 'r':
|
||||
return '\r';
|
||||
case 'b':
|
||||
return '\b';
|
||||
case '0':
|
||||
return '\0';
|
||||
case 'Z':
|
||||
return '\032';
|
||||
case '_':
|
||||
case '%':
|
||||
*with_back_slash = 1;
|
||||
return c;
|
||||
default:
|
||||
return c;
|
||||
case 'n':
|
||||
return '\n';
|
||||
case 't':
|
||||
return '\t';
|
||||
case 'r':
|
||||
return '\r';
|
||||
case 'b':
|
||||
return '\b';
|
||||
case '0':
|
||||
return '\0';
|
||||
case 'Z':
|
||||
return '\032';
|
||||
case '_':
|
||||
case '%':
|
||||
*with_back_slash = 1;
|
||||
return c;
|
||||
default:
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
///* quote_type: 0 - single quotes; 1 - double quotation marks */
|
||||
// int64_t ob_parse_string(const char *src, char *dest, int64_t len, int quote_type)
|
||||
//int64_t ob_parse_string(const char *src, char *dest, int64_t len, int quote_type)
|
||||
//{
|
||||
// int64_t i;
|
||||
// int64_t index = 0;
|
||||
@ -367,7 +492,7 @@ unsigned char escaped_char(unsigned char c, int* with_back_slash)
|
||||
|
||||
static char char_int(char c)
|
||||
{
|
||||
return (c >= '0' && c <= '9' ? c - '0' : (c >= 'A' && c <= 'Z' ? c - 'A' + 10 : c - 'a' + 10));
|
||||
return (c >= '0' && c <= '9' ? c-'0': (c >= 'A' && c <= 'Z' ? c - 'A' + 10 : c - 'a' + 10));
|
||||
}
|
||||
|
||||
int64_t ob_parse_binary_len(int64_t len)
|
||||
@ -375,19 +500,21 @@ int64_t ob_parse_binary_len(int64_t len)
|
||||
return (len + 1) / 2;
|
||||
}
|
||||
|
||||
void ob_parse_binary(const char* src, int64_t len, char* dest)
|
||||
void ob_parse_binary(const char *src, int64_t len, char *dest)
|
||||
{
|
||||
if (OB_UNLIKELY(NULL == src || len <= 0 || NULL == dest)) {
|
||||
// do nothing
|
||||
//do nothing
|
||||
} else {
|
||||
if (len > 0 && len % 2 != 0) {
|
||||
if (len > 0 && len % 2 != 0)
|
||||
{
|
||||
*dest = char_int(src[0]);
|
||||
++src;
|
||||
++dest;
|
||||
}
|
||||
const char* end = src + len - 1;
|
||||
for (; src <= end; src += 2) {
|
||||
*dest = (char)(16 * char_int(src[0]) + char_int(src[1]));
|
||||
const char *end = src + len -1;
|
||||
for (; src <= end; src += 2)
|
||||
{
|
||||
*dest = (char)(16*char_int(src[0]) + char_int(src[1]));
|
||||
++dest;
|
||||
}
|
||||
}
|
||||
@ -401,36 +528,39 @@ int64_t ob_parse_bit_string_len(int64_t len)
|
||||
void ob_parse_bit_string(const char* src, int64_t len, char* dest)
|
||||
{
|
||||
if (OB_UNLIKELY(NULL == src || len <= 0 || NULL == dest)) {
|
||||
// do nothing
|
||||
//do nothing
|
||||
} else {
|
||||
const char* end = src + len - 1;
|
||||
const char* end = src + len -1;
|
||||
char* dest_end = dest + ob_parse_bit_string_len(len) - 1;
|
||||
if (len > 0) {
|
||||
if (len > 0)
|
||||
{
|
||||
unsigned char c = 0;
|
||||
unsigned int one_bit = 1;
|
||||
for (; end >= src; --end) {
|
||||
if (256 == one_bit) /* one byte ready */
|
||||
for ( ; end >= src; --end)
|
||||
{
|
||||
if (256 == one_bit) /* one byte ready */
|
||||
{
|
||||
*dest_end = c;
|
||||
--dest_end;
|
||||
c = 0;
|
||||
one_bit = 1;
|
||||
}
|
||||
if ('1' == *end) {
|
||||
if ('1' == *end)
|
||||
{
|
||||
c |= one_bit;
|
||||
}
|
||||
one_bit <<= 1;
|
||||
} /* end for */
|
||||
*dest_end = c; /* the first byte */
|
||||
} /* end for */
|
||||
*dest_end = c; /* the first byte */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char* str_tolower(char* buff, int64_t len)
|
||||
char *str_tolower(char *buff, int64_t len)
|
||||
{
|
||||
if (OB_LIKELY(NULL != buff)) {
|
||||
char* ptr = buff;
|
||||
char* end = buff + len;
|
||||
char *ptr = buff;
|
||||
char *end = buff + len;
|
||||
unsigned char ch = *ptr;
|
||||
while (ptr != end) {
|
||||
ch = *ptr;
|
||||
@ -446,11 +576,11 @@ char* str_tolower(char* buff, int64_t len)
|
||||
return buff;
|
||||
}
|
||||
|
||||
char* str_toupper(char* buff, int64_t len)
|
||||
char *str_toupper(char *buff, int64_t len)
|
||||
{
|
||||
if (OB_LIKELY(NULL != buff)) {
|
||||
char* ptr = buff;
|
||||
char* end = buff + len;
|
||||
char *ptr = buff;
|
||||
char *end = buff + len;
|
||||
unsigned char ch = *ptr;
|
||||
while (ptr != end) {
|
||||
ch = *ptr;
|
||||
@ -466,7 +596,7 @@ char* str_toupper(char* buff, int64_t len)
|
||||
return buff;
|
||||
}
|
||||
|
||||
int64_t str_remove_space(char* buff, int64_t len)
|
||||
int64_t str_remove_space(char *buff, int64_t len)
|
||||
{
|
||||
int64_t length = 0;
|
||||
if (OB_LIKELY(NULL != buff)) {
|
||||
@ -481,17 +611,20 @@ int64_t str_remove_space(char* buff, int64_t len)
|
||||
|
||||
// calculate hash value of syntax tree recursively
|
||||
// every member of ParseNode is calculated using murmurhash
|
||||
uint64_t parsenode_hash(const ParseNode* node)
|
||||
uint64_t parsenode_hash(const ParseNode *node, int *ret)
|
||||
{
|
||||
uint64_t hash_val = 0;
|
||||
if (check_stack_overflow_c()) {
|
||||
(void)fprintf(stderr, "ERROR stack overflow in recursive function\n");
|
||||
if (ret) {
|
||||
*ret = OB_PARSER_ERR_SIZE_OVERFLOW;
|
||||
}
|
||||
} else if (OB_LIKELY(NULL != node)) {
|
||||
hash_val = murmurhash(&node->type_, sizeof(node->type_), hash_val);
|
||||
hash_val = murmurhash(&node->value_, sizeof(node->value_), hash_val);
|
||||
hash_val = murmurhash(&node->str_len_, sizeof(node->str_len_), hash_val);
|
||||
if (NULL != node->str_value_) {
|
||||
hash_val = murmurhash(node->str_value_, node->str_len_, hash_val);
|
||||
hash_val = murmurhash(node->str_value_, (int32_t)node->str_len_, hash_val);
|
||||
}
|
||||
|
||||
uint64_t child_hash_val = 0;
|
||||
@ -502,7 +635,10 @@ uint64_t parsenode_hash(const ParseNode* node)
|
||||
} else {
|
||||
for (; i < node->num_child_; ++i) {
|
||||
if (NULL != node->children_[i]) {
|
||||
child_hash_val = parsenode_hash(node->children_[i]);
|
||||
child_hash_val = parsenode_hash(node->children_[i], ret);
|
||||
if (ret && OB_PARSER_ERR_SIZE_OVERFLOW == *ret) {
|
||||
break;
|
||||
}
|
||||
hash_val = murmurhash(&child_hash_val, sizeof(child_hash_val), hash_val);
|
||||
}
|
||||
}
|
||||
@ -514,31 +650,37 @@ uint64_t parsenode_hash(const ParseNode* node)
|
||||
|
||||
// compare syntax tree recursively
|
||||
// every member of ParseNode is compared
|
||||
bool parsenode_equal(const ParseNode* lnode, const ParseNode* rnode)
|
||||
bool parsenode_equal(const ParseNode *lnode, const ParseNode *rnode, int *ret)
|
||||
{
|
||||
bool result = true;
|
||||
if (check_stack_overflow_c()) {
|
||||
(void)fprintf(stderr, "ERROR stack overflow in recursive function\n");
|
||||
if (ret) {
|
||||
*ret = OB_PARSER_ERR_SIZE_OVERFLOW;
|
||||
}
|
||||
} else if (NULL == lnode && NULL == rnode) {
|
||||
result = true;
|
||||
} else if ((NULL == lnode && NULL != rnode) || (NULL != lnode && NULL == rnode)) {
|
||||
} else if ((NULL == lnode && NULL != rnode)
|
||||
|| (NULL != lnode && NULL == rnode)) {
|
||||
result = false;
|
||||
} else {
|
||||
if (lnode->type_ != rnode->type_ || lnode->value_ != rnode->value_ || lnode->str_len_ != rnode->str_len_ ||
|
||||
lnode->num_child_ != rnode->num_child_) {
|
||||
if (lnode->type_ != rnode->type_
|
||||
|| lnode->value_ != rnode->value_
|
||||
|| lnode->str_len_ != rnode->str_len_
|
||||
|| lnode->num_child_ != rnode->num_child_) {
|
||||
result = false;
|
||||
} else {
|
||||
if (NULL == lnode->str_value_ && NULL == rnode->str_value_) {
|
||||
result = true;
|
||||
} else if ((NULL == lnode->str_value_ && NULL != rnode->str_value_) ||
|
||||
(NULL != lnode->str_value_ && NULL == rnode->str_value_)) {
|
||||
} else if ((NULL == lnode->str_value_ && NULL != rnode->str_value_)
|
||||
|| (NULL != lnode->str_value_ && NULL == rnode->str_value_ )) {
|
||||
result = false;
|
||||
} else if (lnode->str_len_ != rnode->str_len_) {
|
||||
result = false;
|
||||
} else {
|
||||
// T_VARCHAR type: value_ is length, str_value_ is ptr
|
||||
// @ref ob_raw_expr.cpp
|
||||
if (0 != strncmp(lnode->str_value_, rnode->str_value_, lnode->str_len_)) {
|
||||
if (0 != strncmp(lnode->str_value_, rnode->str_value_, lnode->str_len_)){
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
@ -549,7 +691,10 @@ bool parsenode_equal(const ParseNode* lnode, const ParseNode* rnode)
|
||||
} else {
|
||||
int32_t i = 0;
|
||||
for (; result && i < lnode->num_child_; ++i) {
|
||||
result = parsenode_equal(lnode->children_[i], rnode->children_[i]);
|
||||
result = parsenode_equal(lnode->children_[i], rnode->children_[i], ret);
|
||||
if (ret && OB_PARSER_ERR_SIZE_OVERFLOW == *ret) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -559,9 +704,8 @@ bool parsenode_equal(const ParseNode* lnode, const ParseNode* rnode)
|
||||
return result;
|
||||
}
|
||||
|
||||
// Search according to the name, return the subscript of the name when found, add the name and return the subscript when
|
||||
// found
|
||||
int64_t get_question_mark(ObQuestionMarkCtx* ctx, void* malloc_pool, const char* name)
|
||||
//根据名字寻找,找到后返回该名字的下标,找不到把名字加入并返回下标
|
||||
int64_t get_question_mark(ObQuestionMarkCtx *ctx, void *malloc_pool, const char *name)
|
||||
{
|
||||
int64_t idx = -1;
|
||||
if (OB_UNLIKELY(NULL == ctx || NULL == name)) {
|
||||
@ -607,9 +751,9 @@ int64_t get_question_mark(ObQuestionMarkCtx* ctx, void* malloc_pool, const char*
|
||||
return idx;
|
||||
}
|
||||
|
||||
ParserLinkNode* new_link_node(void* malloc)
|
||||
ParserLinkNode *new_link_node(void *malloc)
|
||||
{
|
||||
ParserLinkNode* new_node = (ParserLinkNode*)parse_malloc(sizeof(ParserLinkNode), malloc);
|
||||
ParserLinkNode *new_node = (ParserLinkNode *)parse_malloc(sizeof(ParserLinkNode), malloc);
|
||||
if (NULL == new_node) {
|
||||
(void)printf("ERROR malloc memory failed\n");
|
||||
} else {
|
||||
@ -620,7 +764,7 @@ ParserLinkNode* new_link_node(void* malloc)
|
||||
return new_node;
|
||||
}
|
||||
|
||||
bool nodename_equal(const ParseNode* node, const char* pattern, int64_t pat_len)
|
||||
bool nodename_equal(const ParseNode *node, const char *pattern, int64_t pat_len)
|
||||
{
|
||||
bool result = true;
|
||||
if (NULL == node || NULL == node->str_value_ || NULL == pattern || node->str_len_ != pat_len) {
|
||||
@ -635,22 +779,3 @@ bool nodename_equal(const ParseNode* node, const char* pattern, int64_t pat_len)
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int binary_search(const struct FuncPair* window_func, int64_t begin, int64_t end, const char* value)
|
||||
{
|
||||
int result = -1;
|
||||
bool need_break = false;
|
||||
while (!need_break && begin <= end) {
|
||||
int mid = begin + (end - begin) / 2;
|
||||
int cmp = strcmp(window_func[mid].func_name_, value);
|
||||
if (cmp > 0) {
|
||||
end = mid - 1;
|
||||
} else if (cmp < 0) {
|
||||
begin = mid + 1;
|
||||
} else {
|
||||
result = window_func[mid].yytokentype_;
|
||||
need_break = true;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user