Commit 75cf8c63 authored by Yuchen Pei's avatar Yuchen Pei Committed by Yuchen Pei

MDEV-29447 MDEV-26285 MDEV-31338 Refactor spider_db_mbase_util::open_item_func

spider_db_mbase_util::open_item_func() is a monster function.
It is difficult to maintain while it is expected that we need to
modify it when a new SQL function or a new func_type is added.

We split the function into two distinct functions: one handles the
case of str != NULL and the other handles the case of str == NULL.

This refactoring was done in a conservative way because we do not
have comprehensive tests on the function.

It also fixes MDEV-29447 and MDEV-31338 where field items that are
arguments of a func item may be used before created / initialised.

Note this commit is adapted from a patch by Nayuta for MDEV-26285.
parent 58f820be
#
# MDEV-29447 SIGSEGV in spider_db_open_item_field and SIGSEGV spider_db_print_item_type, on SELECT
#
for master_1
for child2
child2_1
child2_2
child2_3
for child3
connection child2_1;
CREATE DATABASE auto_test_remote;
USE auto_test_remote;
CREATE TABLE tbl_a (
a INT
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
connection master_1;
CREATE DATABASE auto_test_local;
USE auto_test_local;
CREATE TABLE tbl_a (
a INT
) ENGINE=Spider DEFAULT CHARSET=utf8 COMMENT='table "tbl_a", srv "s_2_1"';
SELECT TRIM(LEADING 'c' FROM a) FROM tbl_a;
TRIM(LEADING 'c' FROM a)
connection child2_1;
DROP DATABASE auto_test_remote;
connection master_1;
DROP DATABASE auto_test_local;
for master_1
for child2
child2_1
child2_2
child2_3
for child3
#
# MDEV-31338 UBSAN: runtime error: member access within null pointer of type 'struct SPIDER_FIELD_CHAIN' and SIGSEGV in spider_db_open_item_ident on SELECT
#
for master_1
for child2
for child3
CREATE SERVER srv FOREIGN DATA WRAPPER MYSQL OPTIONS (SOCKET "$MASTER_1_MYSOCK", DATABASE 'test',user 'root');
CREATE TABLE t (c BLOB) ENGINE=InnoDB;
CREATE TABLE ts (c BLOB) ENGINE=Spider COMMENT='WRAPPER "mysql",srv "srv",TABLE "t"';
SELECT TRIM(BOTH ' ' FROM c) FROM ts ORDER BY c;
TRIM(BOTH ' ' FROM c)
drop table t, ts;
for master_1
for child2
for child3
!include include/default_mysqld.cnf
!include ../my_1_1.cnf
!include ../my_2_1.cnf
--echo #
--echo # MDEV-29447 SIGSEGV in spider_db_open_item_field and SIGSEGV spider_db_print_item_type, on SELECT
--echo #
--disable_query_log
--disable_result_log
--source ../../t/test_init.inc
--enable_result_log
--enable_query_log
--connection child2_1
CREATE DATABASE auto_test_remote;
USE auto_test_remote;
eval CREATE TABLE tbl_a (
a INT
) $CHILD2_1_ENGINE $CHILD2_1_CHARSET;
--connection master_1
CREATE DATABASE auto_test_local;
USE auto_test_local;
eval CREATE TABLE tbl_a (
a INT
) $MASTER_1_ENGINE $MASTER_1_CHARSET COMMENT='table "tbl_a", srv "s_2_1"';
SELECT TRIM(LEADING 'c' FROM a) FROM tbl_a;
--connection child2_1
DROP DATABASE auto_test_remote;
--connection master_1
DROP DATABASE auto_test_local;
--disable_query_log
--disable_result_log
--source ../t/test_deinit.inc
--enable_query_log
--enable_result_log
--echo #
--echo # MDEV-31338 UBSAN: runtime error: member access within null pointer of type 'struct SPIDER_FIELD_CHAIN' and SIGSEGV in spider_db_open_item_ident on SELECT
--echo #
--source include/have_innodb.inc
--disable_query_log
--disable_result_log
--source ../../t/test_init.inc
--enable_result_log
--enable_query_log
evalp CREATE SERVER srv FOREIGN DATA WRAPPER MYSQL OPTIONS (SOCKET "$MASTER_1_MYSOCK", DATABASE 'test',user 'root');
CREATE TABLE t (c BLOB) ENGINE=InnoDB;
CREATE TABLE ts (c BLOB) ENGINE=Spider COMMENT='WRAPPER "mysql",srv "srv",TABLE "t"';
SELECT TRIM(BOTH ' ' FROM c) FROM ts ORDER BY c;
drop table t, ts;
--disable_query_log
--disable_result_log
--source ../../t/test_deinit.inc
--enable_result_log
--enable_query_log
...@@ -5468,6 +5468,13 @@ int spider_db_mbase_util::append_unlock_table( ...@@ -5468,6 +5468,13 @@ int spider_db_mbase_util::append_unlock_table(
DBUG_RETURN(0); DBUG_RETURN(0);
} }
/**
The function check if the given item_func and its arguments can be pushed
down to a data node. If so and str != NULL, the function also print, to str,
the string corresponding to the item_func.
@return 0: success, otherwise: error
*/
int spider_db_mbase_util::open_item_func( int spider_db_mbase_util::open_item_func(
Item_func *item_func, Item_func *item_func,
ha_spider *spider, ha_spider *spider,
...@@ -5476,6 +5483,166 @@ int spider_db_mbase_util::open_item_func( ...@@ -5476,6 +5483,166 @@ int spider_db_mbase_util::open_item_func(
uint alias_length, uint alias_length,
bool use_fields, bool use_fields,
spider_fields *fields spider_fields *fields
) {
DBUG_ENTER("spider_db_mbase_util::open_item_func");
int error = check_item_func(item_func, spider, alias,
alias_length, use_fields, fields);
if (error)
DBUG_RETURN(error);
if (!str)
DBUG_RETURN(0);
DBUG_RETURN(print_item_func(item_func, spider, str, alias,
alias_length, use_fields, fields));
}
static bool item_func_is_timestampdiff(
const char *func_name,
int func_name_length
) {
return func_name_length == 13 &&
!strncasecmp("timestampdiff", func_name, func_name_length);
}
static bool not_func_should_be_skipped(
Item_func *item_func
){
DBUG_ENTER("not_func_should_be_skipped");
DBUG_ASSERT(item_func->functype() == Item_func::NOT_FUNC);
Item **item_list = item_func->arguments();
if (item_list[0]->type() == Item::COND_ITEM)
{
DBUG_PRINT("info",("spider item_list[0] is COND_ITEM"));
Item_cond *item_cond = (Item_cond *) item_list[0];
if (item_cond->functype() == Item_func::COND_AND_FUNC)
{
DBUG_PRINT("info",("spider item_cond is COND_AND_FUNC"));
List_iterator_fast<Item> lif(*(item_cond->argument_list()));
bool has_expr_cache_item = FALSE;
bool has_isnotnull_func = FALSE;
bool has_other_item = FALSE;
while(Item *item = lif++)
{
if (
item->type() == Item::EXPR_CACHE_ITEM
) {
DBUG_PRINT("info",("spider EXPR_CACHE_ITEM"));
has_expr_cache_item = TRUE;
} else
if (
item->type() == Item::FUNC_ITEM &&
((Item_func *) item)->functype() == Item_func::ISNOTNULL_FUNC
) {
DBUG_PRINT("info",("spider ISNOTNULL_FUNC"));
has_isnotnull_func = TRUE;
} else {
DBUG_PRINT("info",("spider has other item"));
DBUG_PRINT("info",("spider COND type=%d", item->type()));
has_other_item = TRUE;
}
}
if (has_expr_cache_item && has_isnotnull_func && !has_other_item)
{
DBUG_PRINT("info",("spider NOT EXISTS skip"));
DBUG_RETURN(TRUE);
}
}
}
DBUG_RETURN(FALSE);
}
/**
Check if the given item_func and its arguments can be pushed down to
a data node. This function is recursive because we need to also check
the arguments of the item_func.
@return 0: success, otherwise: error
*/
int spider_db_mbase_util::check_item_func(
Item_func *item_func,
ha_spider *spider,
const char *alias,
uint alias_length,
bool use_fields,
spider_fields *fields
) {
int use_pushdown_udf;
DBUG_ENTER("spider_db_mbase_util::check_item_func");
Item_func::Functype func_type = item_func->functype();
DBUG_PRINT("info",("spider functype = %d", func_type));
const char *func_name = (char*) item_func->func_name();
int func_name_length = strlen(func_name);
DBUG_PRINT("info",("spider func_name = %s", func_name));
/* The blacklist of the functions that cannot be pushed down */
switch (func_type)
{
case Item_func::TRIG_COND_FUNC:
case Item_func::CASE_SEARCHED_FUNC:
case Item_func::CASE_SIMPLE_FUNC:
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
case Item_func::NOT_FUNC:
/* Why the following check is necessary? */
if (not_func_should_be_skipped(item_func))
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
break;
case Item_func::FUNC_SP:
case Item_func::UDF_FUNC:
use_pushdown_udf= spider_param_use_pushdown_udf(
spider->wide_handler->trx->thd, spider->share->use_pushdown_udf);
if (!use_pushdown_udf)
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
break;
case Item_func::FT_FUNC:
if (spider_db_check_ft_idx(item_func, spider) == MAX_KEY)
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
break;
#ifndef ITEM_FUNC_TIMESTAMPDIFF_ARE_PUBLIC
case Item_func::UNKNOWN_FUNC:
if (item_func_is_timestampdiff(func_name, func_name_length))
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
break;
#endif
default:
break;
}
/* End of the blacklist */
/* Check the arguments recursively */
if (uint item_count = item_func->argument_count())
{
Item **item_list = item_func->arguments();
for (uint roop_count = 0; roop_count < item_count; roop_count++)
{
Item *item = item_list[roop_count];
if (int error_num = spider_db_print_item_type(item, NULL, spider, NULL,
alias, alias_length, dbton_id, use_fields, fields))
DBUG_RETURN(error_num);
}
}
DBUG_RETURN(0);
}
/**
The function print the string corresponding to the given item_func to str.
The function is assumed to be called only when the check by the function
check_item_func() has passed.
@return 0: success, otherwise: error
*/
int spider_db_mbase_util::print_item_func(
Item_func *item_func,
ha_spider *spider,
spider_string *str,
const char *alias,
uint alias_length,
bool use_fields,
spider_fields *fields
) { ) {
int error_num; int error_num;
Item *item, **item_list = item_func->arguments(); Item *item, **item_list = item_func->arguments();
...@@ -5492,13 +5659,15 @@ int spider_db_mbase_util::open_item_func( ...@@ -5492,13 +5659,15 @@ int spider_db_mbase_util::open_item_func(
last_str_length = SPIDER_SQL_NULL_CHAR_LEN; last_str_length = SPIDER_SQL_NULL_CHAR_LEN;
int use_pushdown_udf; int use_pushdown_udf;
bool merge_func = FALSE; bool merge_func = FALSE;
DBUG_ENTER("spider_db_mbase_util::open_item_func"); DBUG_ENTER("spider_db_mbase_util::print_item_func");
if (str) DBUG_ASSERT(!check_item_func(item_func, spider, alias, alias_length,
{ use_fields, fields));
DBUG_ASSERT(str);
if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN)) if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN); str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
}
DBUG_PRINT("info",("spider functype = %d", item_func->functype())); DBUG_PRINT("info",("spider functype = %d", item_func->functype()));
switch (item_func->functype()) switch (item_func->functype())
{ {
...@@ -5547,13 +5716,8 @@ int spider_db_mbase_util::open_item_func( ...@@ -5547,13 +5716,8 @@ int spider_db_mbase_util::open_item_func(
{ {
if ( if (
!strncasecmp("rand", func_name, func_name_length) && !strncasecmp("rand", func_name, func_name_length) &&
#ifdef SPIDER_Item_args_arg_count_IS_PROTECTED
!item_func->argument_count() !item_func->argument_count()
#else
!item_func->arg_count
#endif
) { ) {
if (str)
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
DBUG_RETURN(spider_db_open_item_int(item_func, NULL, spider, str, DBUG_RETURN(spider_db_open_item_int(item_func, NULL, spider, str,
alias, alias_length, dbton_id, use_fields, fields)); alias, alias_length, dbton_id, use_fields, fields));
...@@ -5564,8 +5728,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -5564,8 +5728,7 @@ int spider_db_mbase_util::open_item_func(
/* item_count == 1 means this TRIM() is without a remove_str */ /* item_count == 1 means this TRIM() is without a remove_str */
item = item_list[0]; item = item_list[0];
Item *item_tmp = item_list[1]; Item *item_tmp = item_list[1];
if (str)
{
if (item_tmp->is_of_type(Item::CONST_ITEM, STRING_RESULT)) if (item_tmp->is_of_type(Item::CONST_ITEM, STRING_RESULT))
{ {
/* 1. append 'TRIM(BOTH ' */ /* 1. append 'TRIM(BOTH ' */
...@@ -5596,7 +5759,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5596,7 +5759,6 @@ int spider_db_mbase_util::open_item_func(
str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
SPIDER_SQL_CLOSE_PAREN_LEN); SPIDER_SQL_CLOSE_PAREN_LEN);
} }
}
item_count -= 2; item_count -= 2;
break; break;
} }
...@@ -5611,8 +5773,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5611,8 +5773,6 @@ int spider_db_mbase_util::open_item_func(
/* item_count == 2 means this TRIM(LEADING/TRAILING ...) is with a remove_str */ /* item_count == 2 means this TRIM(LEADING/TRAILING ...) is with a remove_str */
item = item_list[0]; item = item_list[0];
Item *item_tmp = item_list[1]; Item *item_tmp = item_list[1];
if (str)
{
if (item_tmp->is_of_type(Item::CONST_ITEM, STRING_RESULT)) if (item_tmp->is_of_type(Item::CONST_ITEM, STRING_RESULT))
{ {
/* 1. append 'TRIM(LEADING ' or 'TRIM(TRAILING ' */ /* 1. append 'TRIM(LEADING ' or 'TRIM(TRAILING ' */
...@@ -5655,7 +5815,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5655,7 +5815,6 @@ int spider_db_mbase_util::open_item_func(
str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
SPIDER_SQL_CLOSE_PAREN_LEN); SPIDER_SQL_CLOSE_PAREN_LEN);
} }
}
item_count -= 2; item_count -= 2;
break; break;
} }
...@@ -5677,15 +5836,12 @@ int spider_db_mbase_util::open_item_func( ...@@ -5677,15 +5836,12 @@ int spider_db_mbase_util::open_item_func(
!strncasecmp("curdate", func_name, func_name_length) || !strncasecmp("curdate", func_name, func_name_length) ||
!strncasecmp("curtime", func_name, func_name_length) !strncasecmp("curtime", func_name, func_name_length)
) { ) {
if (str)
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
DBUG_RETURN(spider_db_open_item_string(item_func, NULL, spider, str, DBUG_RETURN(spider_db_open_item_string(item_func, NULL, spider, str,
alias, alias_length, dbton_id, use_fields, fields)); alias, alias_length, dbton_id, use_fields, fields));
} else if ( } else if (
!strncasecmp("convert", func_name, func_name_length) !strncasecmp("convert", func_name, func_name_length)
) { ) {
if (str)
{
if (str->reserve(func_name_length * 2 + SPIDER_SQL_OPEN_PAREN_LEN)) if (str->reserve(func_name_length * 2 + SPIDER_SQL_OPEN_PAREN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
...@@ -5693,7 +5849,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5693,7 +5849,6 @@ int spider_db_mbase_util::open_item_func(
SPIDER_SQL_OPEN_PAREN_LEN); SPIDER_SQL_OPEN_PAREN_LEN);
last_str = SPIDER_SQL_CLOSE_PAREN_STR; last_str = SPIDER_SQL_CLOSE_PAREN_STR;
last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN; last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
}
break; break;
} }
} else if (func_name_length == 8 && } else if (func_name_length == 8 &&
...@@ -5702,7 +5857,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5702,7 +5857,6 @@ int spider_db_mbase_util::open_item_func(
!strncasecmp("utc_time", func_name, func_name_length) !strncasecmp("utc_time", func_name, func_name_length)
) )
) { ) {
if (str)
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
DBUG_RETURN(spider_db_open_item_string(item_func, NULL, spider, str, DBUG_RETURN(spider_db_open_item_string(item_func, NULL, spider, str,
alias, alias_length, dbton_id, use_fields, fields)); alias, alias_length, dbton_id, use_fields, fields));
...@@ -5720,15 +5874,13 @@ int spider_db_mbase_util::open_item_func( ...@@ -5720,15 +5874,13 @@ int spider_db_mbase_util::open_item_func(
last_str_length = SPIDER_SQL_IS_NOT_FALSE_LEN; last_str_length = SPIDER_SQL_IS_NOT_FALSE_LEN;
break; break;
} else if (!strncasecmp("column_get", func_name, func_name_length)) } else if (!strncasecmp("column_get", func_name, func_name_length))
{
if (str)
{ {
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN)) if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN); str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
}
func_name = SPIDER_SQL_COMMA_STR; func_name = SPIDER_SQL_COMMA_STR;
func_name_length = SPIDER_SQL_COMMA_LEN; func_name_length = SPIDER_SQL_COMMA_LEN;
separator_str = SPIDER_SQL_COMMA_STR; separator_str = SPIDER_SQL_COMMA_STR;
...@@ -5748,7 +5900,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -5748,7 +5900,7 @@ int spider_db_mbase_util::open_item_func(
{ {
const char *child_func_name; const char *child_func_name;
int child_func_name_length; int child_func_name_length;
LEX_CSTRING org_func_name= ifunc->func_name_cstring(); org_func_name= ifunc->func_name_cstring();
DBUG_PRINT("info",("spider child is UNKNOWN_FUNC")); DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
child_func_name = org_func_name.str; child_func_name = org_func_name.str;
child_func_name_length = org_func_name.length; child_func_name_length = org_func_name.length;
...@@ -5763,8 +5915,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5763,8 +5915,6 @@ int spider_db_mbase_util::open_item_func(
} }
} }
if (str)
{
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
if (!merge_func) if (!merge_func)
{ {
...@@ -5772,7 +5922,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5772,7 +5922,6 @@ int spider_db_mbase_util::open_item_func(
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN); str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
} }
}
last_str = SPIDER_SQL_AS_DATE_STR; last_str = SPIDER_SQL_AS_DATE_STR;
last_str_length = SPIDER_SQL_AS_DATE_LEN; last_str_length = SPIDER_SQL_AS_DATE_LEN;
break; break;
...@@ -5787,7 +5936,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -5787,7 +5936,7 @@ int spider_db_mbase_util::open_item_func(
{ {
const char *child_func_name; const char *child_func_name;
int child_func_name_length; int child_func_name_length;
LEX_CSTRING org_func_name= ifunc->func_name_cstring(); org_func_name= ifunc->func_name_cstring();
DBUG_PRINT("info",("spider child is UNKNOWN_FUNC")); DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
child_func_name = org_func_name.str; child_func_name = org_func_name.str;
child_func_name_length = org_func_name.length; child_func_name_length = org_func_name.length;
...@@ -5802,8 +5951,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5802,8 +5951,6 @@ int spider_db_mbase_util::open_item_func(
} }
} }
if (str)
{
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
if (!merge_func) if (!merge_func)
{ {
...@@ -5811,7 +5958,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5811,7 +5958,6 @@ int spider_db_mbase_util::open_item_func(
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN); str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
} }
}
last_str = SPIDER_SQL_AS_TIME_STR; last_str = SPIDER_SQL_AS_TIME_STR;
last_str_length = SPIDER_SQL_AS_TIME_LEN; last_str_length = SPIDER_SQL_AS_TIME_LEN;
break; break;
...@@ -5820,7 +5966,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5820,7 +5966,6 @@ int spider_db_mbase_util::open_item_func(
{ {
if (!strncasecmp("utc_timestamp", func_name, func_name_length)) if (!strncasecmp("utc_timestamp", func_name, func_name_length))
{ {
if (str)
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
DBUG_RETURN(spider_db_open_item_string(item_func, NULL, spider, str, DBUG_RETURN(spider_db_open_item_string(item_func, NULL, spider, str,
alias, alias_length, dbton_id, use_fields, fields)); alias, alias_length, dbton_id, use_fields, fields));
...@@ -5829,8 +5974,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5829,8 +5974,6 @@ int spider_db_mbase_util::open_item_func(
#ifdef ITEM_FUNC_TIMESTAMPDIFF_ARE_PUBLIC #ifdef ITEM_FUNC_TIMESTAMPDIFF_ARE_PUBLIC
Item_func_timestamp_diff *item_func_timestamp_diff = Item_func_timestamp_diff *item_func_timestamp_diff =
(Item_func_timestamp_diff *) item_func; (Item_func_timestamp_diff *) item_func;
if (str)
{
const char *interval_str; const char *interval_str;
uint interval_len; uint interval_len;
switch (item_func_timestamp_diff->int_type) switch (item_func_timestamp_diff->int_type)
...@@ -5884,7 +6027,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -5884,7 +6027,7 @@ int spider_db_mbase_util::open_item_func(
str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN); str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
str->q_append(interval_str, interval_len); str->q_append(interval_str, interval_len);
str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN); str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
}
if ((error_num = spider_db_print_item_type(item_list[0], NULL, spider, if ((error_num = spider_db_print_item_type(item_list[0], NULL, spider,
str, alias, alias_length, dbton_id, use_fields, fields))) str, alias, alias_length, dbton_id, use_fields, fields)))
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
...@@ -5922,7 +6065,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -5922,7 +6065,7 @@ int spider_db_mbase_util::open_item_func(
{ {
const char *child_func_name; const char *child_func_name;
int child_func_name_length; int child_func_name_length;
LEX_CSTRING org_func_name= ifunc->func_name_cstring(); org_func_name= ifunc->func_name_cstring();
DBUG_PRINT("info",("spider child is UNKNOWN_FUNC")); DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
child_func_name = org_func_name.str; child_func_name = org_func_name.str;
child_func_name_length = org_func_name.length; child_func_name_length = org_func_name.length;
...@@ -5937,8 +6080,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5937,8 +6080,6 @@ int spider_db_mbase_util::open_item_func(
} }
} }
if (str)
{
char *tmp_ptr, *tmp_ptr2; char *tmp_ptr, *tmp_ptr2;
DBUG_ASSERT(tmp_str.length() == 0); DBUG_ASSERT(tmp_str.length() == 0);
tmp_str.set_charset(str->charset()); tmp_str.set_charset(str->charset());
...@@ -5961,7 +6102,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5961,7 +6102,6 @@ int spider_db_mbase_util::open_item_func(
tmp_ptr = tmp_ptr2 + 1; tmp_ptr = tmp_ptr2 + 1;
last_str = tmp_ptr - 1; last_str = tmp_ptr - 1;
last_str_length = strlen(last_str) - SPIDER_SQL_CLOSE_PAREN_LEN; last_str_length = strlen(last_str) - SPIDER_SQL_CLOSE_PAREN_LEN;
}
break; break;
} else if (!strncasecmp("cast_as_signed", func_name, func_name_length)) } else if (!strncasecmp("cast_as_signed", func_name, func_name_length))
{ {
...@@ -5974,7 +6114,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -5974,7 +6114,7 @@ int spider_db_mbase_util::open_item_func(
{ {
const char *child_func_name; const char *child_func_name;
int child_func_name_length; int child_func_name_length;
LEX_CSTRING org_func_name= ifunc->func_name_cstring(); org_func_name= ifunc->func_name_cstring();
DBUG_PRINT("info",("spider child is UNKNOWN_FUNC")); DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
child_func_name = org_func_name.str; child_func_name = org_func_name.str;
child_func_name_length = org_func_name.length; child_func_name_length = org_func_name.length;
...@@ -5989,8 +6129,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5989,8 +6129,6 @@ int spider_db_mbase_util::open_item_func(
} }
} }
if (str)
{
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
if (!merge_func) if (!merge_func)
{ {
...@@ -5998,7 +6136,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -5998,7 +6136,6 @@ int spider_db_mbase_util::open_item_func(
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN); str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
} }
}
last_str = SPIDER_SQL_AS_SIGNED_STR; last_str = SPIDER_SQL_AS_SIGNED_STR;
last_str_length = SPIDER_SQL_AS_SIGNED_LEN; last_str_length = SPIDER_SQL_AS_SIGNED_LEN;
break; break;
...@@ -6016,7 +6153,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -6016,7 +6153,7 @@ int spider_db_mbase_util::open_item_func(
{ {
const char *child_func_name; const char *child_func_name;
int child_func_name_length; int child_func_name_length;
LEX_CSTRING org_func_name= ifunc->func_name_cstring(); org_func_name= ifunc->func_name_cstring();
DBUG_PRINT("info",("spider child is UNKNOWN_FUNC")); DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
child_func_name = org_func_name.str; child_func_name = org_func_name.str;
child_func_name_length = org_func_name.length; child_func_name_length = org_func_name.length;
...@@ -6031,8 +6168,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6031,8 +6168,6 @@ int spider_db_mbase_util::open_item_func(
} }
} }
if (str)
{
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
if (!merge_func) if (!merge_func)
{ {
...@@ -6040,7 +6175,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6040,7 +6175,6 @@ int spider_db_mbase_util::open_item_func(
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN); str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
} }
}
last_str = SPIDER_SQL_AS_UNSIGNED_STR; last_str = SPIDER_SQL_AS_UNSIGNED_STR;
last_str_length = SPIDER_SQL_AS_UNSIGNED_LEN; last_str_length = SPIDER_SQL_AS_UNSIGNED_LEN;
break; break;
...@@ -6056,7 +6190,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -6056,7 +6190,7 @@ int spider_db_mbase_util::open_item_func(
{ {
const char *child_func_name; const char *child_func_name;
int child_func_name_length; int child_func_name_length;
LEX_CSTRING org_func_name= ifunc->func_name_cstring(); org_func_name= ifunc->func_name_cstring();
DBUG_PRINT("info",("spider child is UNKNOWN_FUNC")); DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
child_func_name = org_func_name.str; child_func_name = org_func_name.str;
child_func_name_length = org_func_name.length; child_func_name_length = org_func_name.length;
...@@ -6071,8 +6205,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6071,8 +6205,6 @@ int spider_db_mbase_util::open_item_func(
} }
} }
if (str)
{
char *tmp_ptr, *tmp_ptr2; char *tmp_ptr, *tmp_ptr2;
DBUG_ASSERT(tmp_str.length() == 0); DBUG_ASSERT(tmp_str.length() == 0);
tmp_str.set_charset(str->charset()); tmp_str.set_charset(str->charset());
...@@ -6095,7 +6227,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6095,7 +6227,6 @@ int spider_db_mbase_util::open_item_func(
tmp_ptr = tmp_ptr2 + 1; tmp_ptr = tmp_ptr2 + 1;
last_str = tmp_ptr - 1; last_str = tmp_ptr - 1;
last_str_length = strlen(last_str) - SPIDER_SQL_CLOSE_PAREN_LEN; last_str_length = strlen(last_str) - SPIDER_SQL_CLOSE_PAREN_LEN;
}
break; break;
} else if (!strncasecmp("cast_as_datetime", func_name, } else if (!strncasecmp("cast_as_datetime", func_name,
func_name_length)) func_name_length))
...@@ -6109,7 +6240,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -6109,7 +6240,7 @@ int spider_db_mbase_util::open_item_func(
{ {
const char *child_func_name; const char *child_func_name;
int child_func_name_length; int child_func_name_length;
LEX_CSTRING org_func_name= ifunc->func_name_cstring(); org_func_name= ifunc->func_name_cstring();
DBUG_PRINT("info",("spider child is UNKNOWN_FUNC")); DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
child_func_name = org_func_name.str; child_func_name = org_func_name.str;
child_func_name_length = org_func_name.length; child_func_name_length = org_func_name.length;
...@@ -6124,8 +6255,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6124,8 +6255,6 @@ int spider_db_mbase_util::open_item_func(
} }
} }
if (str)
{
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
if (!merge_func) if (!merge_func)
{ {
...@@ -6133,7 +6262,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6133,7 +6262,6 @@ int spider_db_mbase_util::open_item_func(
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN); str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
} }
}
last_str = SPIDER_SQL_AS_DATETIME_STR; last_str = SPIDER_SQL_AS_DATETIME_STR;
last_str_length = SPIDER_SQL_AS_DATETIME_LEN; last_str_length = SPIDER_SQL_AS_DATETIME_LEN;
break; break;
...@@ -6150,8 +6278,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6150,8 +6278,6 @@ int spider_db_mbase_util::open_item_func(
if ((error_num = spider_db_print_item_type(item_list[0], NULL, spider, if ((error_num = spider_db_print_item_type(item_list[0], NULL, spider,
str, alias, alias_length, dbton_id, use_fields, fields))) str, alias, alias_length, dbton_id, use_fields, fields)))
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
if (str)
{
if (item_date_add_interval->date_sub_interval) if (item_date_add_interval->date_sub_interval)
{ {
if (str->reserve(SPIDER_SQL_NEGINTERVAL_LEN)) if (str->reserve(SPIDER_SQL_NEGINTERVAL_LEN))
...@@ -6163,28 +6289,21 @@ int spider_db_mbase_util::open_item_func( ...@@ -6163,28 +6289,21 @@ int spider_db_mbase_util::open_item_func(
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_INTERVAL_STR, SPIDER_SQL_INTERVAL_LEN); str->q_append(SPIDER_SQL_INTERVAL_STR, SPIDER_SQL_INTERVAL_LEN);
} }
}
if ((error_num = spider_db_print_item_type(item_list[1], NULL, spider, if ((error_num = spider_db_print_item_type(item_list[1], NULL, spider,
str, alias, alias_length, dbton_id, use_fields, fields))) str, alias, alias_length, dbton_id, use_fields, fields)))
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
if (str)
{
if (str->reserve(func_name_length + SPIDER_SQL_CLOSE_PAREN_LEN)) if (str->reserve(func_name_length + SPIDER_SQL_CLOSE_PAREN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
SPIDER_SQL_CLOSE_PAREN_LEN); SPIDER_SQL_CLOSE_PAREN_LEN);
}
DBUG_RETURN(0); DBUG_RETURN(0);
} }
} }
if (str)
{
if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN)) if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN); str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
}
func_name = SPIDER_SQL_COMMA_STR; func_name = SPIDER_SQL_COMMA_STR;
func_name_length = SPIDER_SQL_COMMA_LEN; func_name_length = SPIDER_SQL_COMMA_LEN;
separator_str = SPIDER_SQL_COMMA_STR; separator_str = SPIDER_SQL_COMMA_STR;
...@@ -6193,7 +6312,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6193,7 +6312,6 @@ int spider_db_mbase_util::open_item_func(
last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN; last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
break; break;
case Item_func::NOW_FUNC: case Item_func::NOW_FUNC:
if (str)
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
DBUG_RETURN(spider_db_open_item_string(item_func, NULL, spider, DBUG_RETURN(spider_db_open_item_string(item_func, NULL, spider,
str, alias, alias_length, dbton_id, use_fields, fields)); str, alias, alias_length, dbton_id, use_fields, fields));
...@@ -6209,7 +6327,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -6209,7 +6327,7 @@ int spider_db_mbase_util::open_item_func(
{ {
const char *child_func_name; const char *child_func_name;
int child_func_name_length; int child_func_name_length;
LEX_CSTRING org_func_name= ifunc->func_name_cstring(); org_func_name= ifunc->func_name_cstring();
DBUG_PRINT("info",("spider child is UNKNOWN_FUNC")); DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
child_func_name = org_func_name.str; child_func_name = org_func_name.str;
child_func_name_length = org_func_name.length; child_func_name_length = org_func_name.length;
...@@ -6224,8 +6342,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6224,8 +6342,6 @@ int spider_db_mbase_util::open_item_func(
} }
} }
if (str)
{
char *tmp_ptr, *tmp_ptr2; char *tmp_ptr, *tmp_ptr2;
DBUG_ASSERT(tmp_str.length() == 0); DBUG_ASSERT(tmp_str.length() == 0);
tmp_str.set_charset(str->charset()); tmp_str.set_charset(str->charset());
...@@ -6249,69 +6365,27 @@ int spider_db_mbase_util::open_item_func( ...@@ -6249,69 +6365,27 @@ int spider_db_mbase_util::open_item_func(
last_str = tmp_ptr - 1; last_str = tmp_ptr - 1;
last_str_length = strlen(last_str) - SPIDER_SQL_CLOSE_PAREN_LEN; last_str_length = strlen(last_str) - SPIDER_SQL_CLOSE_PAREN_LEN;
} }
}
break; break;
case Item_func::NOT_FUNC: case Item_func::NOT_FUNC:
DBUG_PRINT("info",("spider NOT_FUNC")); DBUG_PRINT("info",("spider NOT_FUNC"));
if (item_list[0]->type() == Item::COND_ITEM) if (not_func_should_be_skipped(item_func))
{
DBUG_PRINT("info",("spider item_list[0] is COND_ITEM"));
Item_cond *item_cond = (Item_cond *) item_list[0];
if (item_cond->functype() == Item_func::COND_AND_FUNC)
{
DBUG_PRINT("info",("spider item_cond is COND_AND_FUNC"));
List_iterator_fast<Item> lif(*(item_cond->argument_list()));
bool has_expr_cache_item = FALSE;
bool has_isnotnull_func = FALSE;
bool has_other_item = FALSE;
while((item = lif++))
{
if (
item->type() == Item::EXPR_CACHE_ITEM
) {
DBUG_PRINT("info",("spider EXPR_CACHE_ITEM"));
has_expr_cache_item = TRUE;
} else if (
item->type() == Item::FUNC_ITEM &&
((Item_func *) item)->functype() == Item_func::ISNOTNULL_FUNC
) {
DBUG_PRINT("info",("spider ISNOTNULL_FUNC"));
has_isnotnull_func = TRUE;
} else {
DBUG_PRINT("info",("spider has other item"));
DBUG_PRINT("info",("spider COND type=%d", item->type()));
has_other_item = TRUE;
}
}
if (has_expr_cache_item && has_isnotnull_func && !has_other_item)
{
DBUG_PRINT("info",("spider NOT EXISTS skip"));
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM); DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
} org_func_name= item_func->func_name_cstring();
}
}
if (str)
{
LEX_CSTRING org_func_name= item_func->func_name_cstring();
func_name = org_func_name.str; func_name = org_func_name.str;
func_name_length = org_func_name.length; func_name_length = org_func_name.length;
if (str->reserve(func_name_length + SPIDER_SQL_SPACE_LEN)) if (str->reserve(func_name_length + SPIDER_SQL_SPACE_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN); str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
}
break; break;
case Item_func::NEG_FUNC: case Item_func::NEG_FUNC:
if (str) org_func_name= item_func->func_name_cstring();
{
LEX_CSTRING org_func_name= item_func->func_name_cstring();
func_name = org_func_name.str; func_name = org_func_name.str;
func_name_length = org_func_name.length; func_name_length = org_func_name.length;
if (str->reserve(func_name_length + SPIDER_SQL_SPACE_LEN)) if (str->reserve(func_name_length + SPIDER_SQL_SPACE_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN); str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
}
break; break;
case Item_func::IN_FUNC: case Item_func::IN_FUNC:
if (((Item_func_opt_neg *) item_func)->negated) if (((Item_func_opt_neg *) item_func)->negated)
...@@ -6339,7 +6413,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -6339,7 +6413,7 @@ int spider_db_mbase_util::open_item_func(
separator_str = SPIDER_SQL_AND_STR; separator_str = SPIDER_SQL_AND_STR;
separator_str_length = SPIDER_SQL_AND_LEN; separator_str_length = SPIDER_SQL_AND_LEN;
} else { } else {
LEX_CSTRING org_func_name= item_func->func_name_cstring(); org_func_name= item_func->func_name_cstring();
func_name = org_func_name.str; func_name = org_func_name.str;
func_name_length = org_func_name.length; func_name_length = org_func_name.length;
separator_str = SPIDER_SQL_AND_STR; separator_str = SPIDER_SQL_AND_STR;
...@@ -6358,9 +6432,7 @@ int spider_db_mbase_util::open_item_func( ...@@ -6358,9 +6432,7 @@ int spider_db_mbase_util::open_item_func(
definition). Users can turn it on if they know what they are doing. definition). Users can turn it on if they know what they are doing.
*/ */
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM); DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
if (str) org_func_name= item_func->func_name_cstring();
{
LEX_CSTRING org_func_name= item_func->func_name_cstring();
func_name = org_func_name.str; func_name = org_func_name.str;
func_name_length = org_func_name.length; func_name_length = org_func_name.length;
DBUG_PRINT("info",("spider func_name = %s", func_name)); DBUG_PRINT("info",("spider func_name = %s", func_name));
...@@ -6369,7 +6441,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6369,7 +6441,6 @@ int spider_db_mbase_util::open_item_func(
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN); str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
}
func_name = SPIDER_SQL_COMMA_STR; func_name = SPIDER_SQL_COMMA_STR;
func_name_length = SPIDER_SQL_COMMA_LEN; func_name_length = SPIDER_SQL_COMMA_LEN;
separator_str = SPIDER_SQL_COMMA_STR; separator_str = SPIDER_SQL_COMMA_STR;
...@@ -6380,7 +6451,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6380,7 +6451,6 @@ int spider_db_mbase_util::open_item_func(
case Item_func::TRIG_COND_FUNC: case Item_func::TRIG_COND_FUNC:
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM); DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
case Item_func::GUSERVAR_FUNC: case Item_func::GUSERVAR_FUNC:
if (str)
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN); str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
if (item_func->result_type() == STRING_RESULT) if (item_func->result_type() == STRING_RESULT)
DBUG_RETURN(spider_db_open_item_string(item_func, NULL, spider, str, DBUG_RETURN(spider_db_open_item_string(item_func, NULL, spider, str,
...@@ -6392,20 +6462,15 @@ int spider_db_mbase_util::open_item_func( ...@@ -6392,20 +6462,15 @@ int spider_db_mbase_util::open_item_func(
if (spider_db_check_ft_idx(item_func, spider) == MAX_KEY) if (spider_db_check_ft_idx(item_func, spider) == MAX_KEY)
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM); DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
start_item = 1; start_item = 1;
if (str)
{
if (str->reserve(SPIDER_SQL_MATCH_LEN)) if (str->reserve(SPIDER_SQL_MATCH_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_MATCH_STR, SPIDER_SQL_MATCH_LEN); str->q_append(SPIDER_SQL_MATCH_STR, SPIDER_SQL_MATCH_LEN);
}
separator_str = SPIDER_SQL_COMMA_STR; separator_str = SPIDER_SQL_COMMA_STR;
separator_str_length = SPIDER_SQL_COMMA_LEN; separator_str_length = SPIDER_SQL_COMMA_LEN;
last_str = SPIDER_SQL_CLOSE_PAREN_STR; last_str = SPIDER_SQL_CLOSE_PAREN_STR;
last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN; last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
break; break;
case Item_func::SP_EQUALS_FUNC: case Item_func::SP_EQUALS_FUNC:
if (str)
{
func_name = SPIDER_SQL_MBR_EQUAL_STR; func_name = SPIDER_SQL_MBR_EQUAL_STR;
func_name_length = SPIDER_SQL_MBR_EQUAL_LEN; func_name_length = SPIDER_SQL_MBR_EQUAL_LEN;
DBUG_PRINT("info",("spider func_name = %s", func_name)); DBUG_PRINT("info",("spider func_name = %s", func_name));
...@@ -6413,7 +6478,6 @@ int spider_db_mbase_util::open_item_func( ...@@ -6413,7 +6478,6 @@ int spider_db_mbase_util::open_item_func(
if (str->reserve(func_name_length)) if (str->reserve(func_name_length))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
}
func_name = SPIDER_SQL_COMMA_STR; func_name = SPIDER_SQL_COMMA_STR;
func_name_length = SPIDER_SQL_COMMA_LEN; func_name_length = SPIDER_SQL_COMMA_LEN;
separator_str = SPIDER_SQL_COMMA_STR; separator_str = SPIDER_SQL_COMMA_STR;
...@@ -6428,25 +6492,15 @@ int spider_db_mbase_util::open_item_func( ...@@ -6428,25 +6492,15 @@ int spider_db_mbase_util::open_item_func(
case Item_func::SP_WITHIN_FUNC: case Item_func::SP_WITHIN_FUNC:
case Item_func::SP_CONTAINS_FUNC: case Item_func::SP_CONTAINS_FUNC:
case Item_func::SP_OVERLAPS_FUNC: case Item_func::SP_OVERLAPS_FUNC:
if (str) org_func_name= item_func->func_name_cstring();
{
LEX_CSTRING org_func_name= item_func->func_name_cstring();
func_name = org_func_name.str; func_name = org_func_name.str;
func_name_length = org_func_name.length; func_name_length = org_func_name.length;
DBUG_PRINT("info",("spider func_name = %s", func_name)); DBUG_PRINT("info",("spider func_name = %s", func_name));
DBUG_PRINT("info",("spider func_name_length = %d", func_name_length)); DBUG_PRINT("info",("spider func_name_length = %d", func_name_length));
if (str->reserve( if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN))
#ifndef SPIDER_ITEM_GEOFUNC_NAME_HAS_MBR
SPIDER_SQL_MBR_LEN +
#endif
func_name_length + SPIDER_SQL_OPEN_PAREN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
#ifndef SPIDER_ITEM_GEOFUNC_NAME_HAS_MBR
str->q_append(SPIDER_SQL_MBR_STR, SPIDER_SQL_MBR_LEN);
#endif
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN); str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
}
func_name = SPIDER_SQL_COMMA_STR; func_name = SPIDER_SQL_COMMA_STR;
func_name_length = SPIDER_SQL_COMMA_LEN; func_name_length = SPIDER_SQL_COMMA_LEN;
separator_str = SPIDER_SQL_COMMA_STR; separator_str = SPIDER_SQL_COMMA_STR;
...@@ -6462,16 +6516,11 @@ int spider_db_mbase_util::open_item_func( ...@@ -6462,16 +6516,11 @@ int spider_db_mbase_util::open_item_func(
case Item_func::GE_FUNC: case Item_func::GE_FUNC:
case Item_func::GT_FUNC: case Item_func::GT_FUNC:
case Item_func::XOR_FUNC: case Item_func::XOR_FUNC:
if (str) org_func_name= item_func->func_name_cstring();
{
LEX_CSTRING org_func_name= item_func->func_name_cstring();
func_name = org_func_name.str; func_name = org_func_name.str;
func_name_length = org_func_name.length; func_name_length = org_func_name.length;
}
break; break;
case Item_func::LIKE_FUNC: case Item_func::LIKE_FUNC:
if (str)
{
if (((Item_func_like *)item_func)->get_negated()) if (((Item_func_like *)item_func)->get_negated())
{ {
func_name = SPIDER_SQL_NOT_LIKE_STR; func_name = SPIDER_SQL_NOT_LIKE_STR;
...@@ -6479,88 +6528,21 @@ int spider_db_mbase_util::open_item_func( ...@@ -6479,88 +6528,21 @@ int spider_db_mbase_util::open_item_func(
} }
else else
{ {
LEX_CSTRING org_func_name= item_func->func_name_cstring(); org_func_name= item_func->func_name_cstring();
func_name = org_func_name.str; func_name = org_func_name.str;
func_name_length = org_func_name.length; func_name_length = org_func_name.length;
} }
}
break; break;
case Item_func::CASE_SEARCHED_FUNC: case Item_func::CASE_SEARCHED_FUNC:
case Item_func::CASE_SIMPLE_FUNC: case Item_func::CASE_SIMPLE_FUNC:
#ifdef ITEM_FUNC_CASE_PARAMS_ARE_PUBLIC
Item_func_case *item_func_case = (Item_func_case *) item_func;
if (str)
{
if (str->reserve(SPIDER_SQL_CASE_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_CASE_STR, SPIDER_SQL_CASE_LEN);
}
if (item_func_case->first_expr_num != -1)
{
if ((error_num = spider_db_print_item_type(
item_list[item_func_case->first_expr_num], NULL, spider, str,
alias, alias_length, dbton_id, use_fields, fields)))
DBUG_RETURN(error_num);
}
for (roop_count = 0; roop_count < item_func_case->ncases;
roop_count += 2)
{
if (str)
{
if (str->reserve(SPIDER_SQL_WHEN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_WHEN_STR, SPIDER_SQL_WHEN_LEN);
}
if ((error_num = spider_db_print_item_type(
item_list[roop_count], NULL, spider, str,
alias, alias_length, dbton_id, use_fields, fields)))
DBUG_RETURN(error_num);
if (str)
{
if (str->reserve(SPIDER_SQL_THEN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_THEN_STR, SPIDER_SQL_THEN_LEN);
}
if ((error_num = spider_db_print_item_type(
item_list[roop_count + 1], NULL, spider, str,
alias, alias_length, dbton_id, use_fields, fields)))
DBUG_RETURN(error_num);
}
if (item_func_case->else_expr_num != -1)
{
if (str)
{
if (str->reserve(SPIDER_SQL_ELSE_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_ELSE_STR, SPIDER_SQL_ELSE_LEN);
}
if ((error_num = spider_db_print_item_type(
item_list[item_func_case->else_expr_num], NULL, spider, str,
alias, alias_length, dbton_id, use_fields, fields)))
DBUG_RETURN(error_num);
}
if (str)
{
if (str->reserve(SPIDER_SQL_END_LEN + SPIDER_SQL_CLOSE_PAREN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_END_STR, SPIDER_SQL_END_LEN);
str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
SPIDER_SQL_CLOSE_PAREN_LEN);
}
DBUG_RETURN(0);
#else
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM); DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
#endif
case Item_func::JSON_EXTRACT_FUNC: case Item_func::JSON_EXTRACT_FUNC:
func_name = (char*) item_func->func_name(); func_name = (char*) item_func->func_name();
func_name_length = strlen(func_name); func_name_length = strlen(func_name);
if (str)
{
if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN)) if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN); str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
}
func_name = SPIDER_SQL_COMMA_STR; func_name = SPIDER_SQL_COMMA_STR;
func_name_length = SPIDER_SQL_COMMA_LEN; func_name_length = SPIDER_SQL_COMMA_LEN;
separator_str = SPIDER_SQL_COMMA_STR; separator_str = SPIDER_SQL_COMMA_STR;
...@@ -6574,12 +6556,9 @@ int spider_db_mbase_util::open_item_func( ...@@ -6574,12 +6556,9 @@ int spider_db_mbase_util::open_item_func(
if (spider_param_skip_default_condition(thd, if (spider_param_skip_default_condition(thd,
share->skip_default_condition)) share->skip_default_condition))
DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM); DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
if (str) org_func_name= item_func->func_name_cstring();
{
LEX_CSTRING org_func_name= item_func->func_name_cstring();
func_name = org_func_name.str; func_name = org_func_name.str;
func_name_length = org_func_name.length; func_name_length = org_func_name.length;
}
break; break;
} }
DBUG_PRINT("info",("spider func_name = %s", func_name)); DBUG_PRINT("info",("spider func_name = %s", func_name));
...@@ -6612,15 +6591,12 @@ int spider_db_mbase_util::open_item_func( ...@@ -6612,15 +6591,12 @@ int spider_db_mbase_util::open_item_func(
func_name = separator_str; func_name = separator_str;
func_name_length = separator_str_length; func_name_length = separator_str_length;
} }
if (str)
{
if (str->reserve(func_name_length + SPIDER_SQL_SPACE_LEN * 2)) if (str->reserve(func_name_length + SPIDER_SQL_SPACE_LEN * 2))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN); str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
str->q_append(func_name, func_name_length); str->q_append(func_name, func_name_length);
str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN); str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
} }
}
/* Print the last operand value */ /* Print the last operand value */
item = item_list[roop_count]; item = item_list[roop_count];
...@@ -6632,18 +6608,13 @@ int spider_db_mbase_util::open_item_func( ...@@ -6632,18 +6608,13 @@ int spider_db_mbase_util::open_item_func(
if (item_func->functype() == Item_func::FT_FUNC) if (item_func->functype() == Item_func::FT_FUNC)
{ {
Item_func_match *item_func_match = (Item_func_match *)item_func; Item_func_match *item_func_match = (Item_func_match *)item_func;
if (str)
{
if (str->reserve(SPIDER_SQL_AGAINST_LEN)) if (str->reserve(SPIDER_SQL_AGAINST_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(SPIDER_SQL_AGAINST_STR, SPIDER_SQL_AGAINST_LEN); str->q_append(SPIDER_SQL_AGAINST_STR, SPIDER_SQL_AGAINST_LEN);
}
item = item_list[0]; item = item_list[0];
if ((error_num = spider_db_print_item_type(item, NULL, spider, str, if ((error_num = spider_db_print_item_type(item, NULL, spider, str,
alias, alias_length, dbton_id, use_fields, fields))) alias, alias_length, dbton_id, use_fields, fields)))
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
if (str)
{
if (str->reserve( if (str->reserve(
((item_func_match->match_flags & FT_BOOL) ? ((item_func_match->match_flags & FT_BOOL) ?
SPIDER_SQL_IN_BOOLEAN_MODE_LEN : 0) + SPIDER_SQL_IN_BOOLEAN_MODE_LEN : 0) +
...@@ -6657,15 +6628,12 @@ int spider_db_mbase_util::open_item_func( ...@@ -6657,15 +6628,12 @@ int spider_db_mbase_util::open_item_func(
if (item_func_match->match_flags & FT_EXPAND) if (item_func_match->match_flags & FT_EXPAND)
str->q_append(SPIDER_SQL_WITH_QUERY_EXPANSION_STR, str->q_append(SPIDER_SQL_WITH_QUERY_EXPANSION_STR,
SPIDER_SQL_WITH_QUERY_EXPANSION_LEN); SPIDER_SQL_WITH_QUERY_EXPANSION_LEN);
}
} else if (item_func->functype() == Item_func::UNKNOWN_FUNC) } else if (item_func->functype() == Item_func::UNKNOWN_FUNC)
{ {
if ( if (
func_name_length == 7 && func_name_length == 7 &&
!strncasecmp("convert", func_name, func_name_length) !strncasecmp("convert", func_name, func_name_length)
) { ) {
if (str)
{
Item_func_conv_charset *item_func_conv_charset = Item_func_conv_charset *item_func_conv_charset =
(Item_func_conv_charset *)item_func; (Item_func_conv_charset *)item_func;
CHARSET_INFO *conv_charset = CHARSET_INFO *conv_charset =
...@@ -6677,16 +6645,12 @@ int spider_db_mbase_util::open_item_func( ...@@ -6677,16 +6645,12 @@ int spider_db_mbase_util::open_item_func(
str->q_append(conv_charset->cs_name.str, cset_length); str->q_append(conv_charset->cs_name.str, cset_length);
} }
} }
}
if (str)
{
if (merge_func) if (merge_func)
str->length(str->length() - SPIDER_SQL_CLOSE_PAREN_LEN); str->length(str->length() - SPIDER_SQL_CLOSE_PAREN_LEN);
if (str->reserve(last_str_length + SPIDER_SQL_CLOSE_PAREN_LEN)) if (str->reserve(last_str_length + SPIDER_SQL_CLOSE_PAREN_LEN))
DBUG_RETURN(HA_ERR_OUT_OF_MEM); DBUG_RETURN(HA_ERR_OUT_OF_MEM);
str->q_append(last_str, last_str_length); str->q_append(last_str, last_str_length);
str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN); str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
}
DBUG_RETURN(0); DBUG_RETURN(0);
} }
......
...@@ -126,6 +126,25 @@ class spider_db_mbase_util: public spider_db_util ...@@ -126,6 +126,25 @@ class spider_db_mbase_util: public spider_db_util
bool use_fields, bool use_fields,
spider_fields *fields spider_fields *fields
) override; ) override;
protected:
int check_item_func(
Item_func *item_func,
ha_spider *spider,
const char *alias,
uint alias_length,
bool use_fields,
spider_fields *fields
);
int print_item_func(
Item_func *item_func,
ha_spider *spider,
spider_string *str,
const char *alias,
uint alias_length,
bool use_fields,
spider_fields *fields
);
public:
int open_item_sum_func( int open_item_sum_func(
Item_sum *item_sum, Item_sum *item_sum,
ha_spider *spider, ha_spider *spider,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment