Commit 0bacef76 authored by Yuchen Pei's avatar Yuchen Pei

MDEV-26247 clean up spider_group_by_handler::init_scan()

parent 2d1e09a7
...@@ -1001,38 +1001,16 @@ spider_group_by_handler::~spider_group_by_handler() ...@@ -1001,38 +1001,16 @@ spider_group_by_handler::~spider_group_by_handler()
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
} }
int spider_group_by_handler::init_scan() static int spider_prepare_init_scan(
const Query& query, spider_fields *fields, ha_spider *spider,
SPIDER_TRX *trx, longlong& offset_limit, THD *thd)
{ {
int error_num, link_idx; int error_num, link_idx;
uint dbton_id; SPIDER_RESULT_LIST *result_list = &spider->result_list;
spider_db_handler *dbton_hdl;
st_select_lex *select_lex; st_select_lex *select_lex;
longlong select_limit; longlong select_limit, direct_order_limit;
longlong direct_order_limit;
SPIDER_SHARE *share = spider->share; SPIDER_SHARE *share = spider->share;
SPIDER_CONN *conn; DBUG_ENTER("spider_prepare_init_scan");
SPIDER_RESULT_LIST *result_list = &spider->result_list;
SPIDER_LINK_IDX_CHAIN *link_idx_chain;
SPIDER_LINK_IDX_HOLDER *link_idx_holder;
DBUG_ENTER("spider_group_by_handler::init_scan");
store_error = 0;
#ifndef DBUG_OFF
Field **field;
for (
field = table->field;
*field;
field++
) {
DBUG_PRINT("info",("spider field_name=%s",
SPIDER_field_name_str(*field)));
}
#endif
if (trx->thd->killed)
{
my_error(ER_QUERY_INTERRUPTED, MYF(0));
DBUG_RETURN(ER_QUERY_INTERRUPTED);
}
spider->use_fields = TRUE; spider->use_fields = TRUE;
spider->fields = fields; spider->fields = fields;
...@@ -1106,85 +1084,87 @@ int spider_group_by_handler::init_scan() ...@@ -1106,85 +1084,87 @@ int spider_group_by_handler::init_scan()
} else { } else {
offset_limit = 0; offset_limit = 0;
} }
DBUG_RETURN(0);
}
static int spider_make_query(const Query& query, spider_fields* fields, ha_spider *spider, TABLE *table)
{
uint dbton_id;
spider_db_handler* dbton_hdl;
SPIDER_RESULT_LIST *result_list = &spider->result_list;
int error_num;
DBUG_ENTER("spider_make_query");
/* making a query */
fields->set_pos_to_first_dbton_id(); fields->set_pos_to_first_dbton_id();
while ((dbton_id = fields->get_next_dbton_id()) < SPIDER_DBTON_SIZE) while ((dbton_id = fields->get_next_dbton_id()) < SPIDER_DBTON_SIZE)
{ {
dbton_hdl = spider->dbton_handler[dbton_id]; dbton_hdl = spider->dbton_handler[dbton_id];
result_list->direct_distinct = query.distinct; result_list->direct_distinct = query.distinct;
if ((error_num = dbton_hdl->reset_sql(SPIDER_SQL_TYPE_SELECT_SQL))) if ((error_num = dbton_hdl->reset_sql(SPIDER_SQL_TYPE_SELECT_SQL)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
if ((error_num = dbton_hdl->append_select_part(SPIDER_SQL_TYPE_SELECT_SQL))) if ((error_num = dbton_hdl->append_select_part(SPIDER_SQL_TYPE_SELECT_SQL)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
fields->set_field_ptr(table->field); fields->set_field_ptr(table->field);
if ((error_num = dbton_hdl->append_list_item_select_part( if ((error_num = dbton_hdl->append_list_item_select_part(
query.select, NULL, 0, TRUE, fields, SPIDER_SQL_TYPE_SELECT_SQL))) query.select, NULL, 0, TRUE, fields, SPIDER_SQL_TYPE_SELECT_SQL)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
if ((error_num = dbton_hdl->append_from_and_tables_part( if ((error_num = dbton_hdl->append_from_and_tables_part(
fields, SPIDER_SQL_TYPE_SELECT_SQL))) fields, SPIDER_SQL_TYPE_SELECT_SQL)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
if (query.where) if (query.where)
{ {
if ((error_num = if ((error_num =
dbton_hdl->append_where_part(SPIDER_SQL_TYPE_SELECT_SQL))) dbton_hdl->append_where_part(SPIDER_SQL_TYPE_SELECT_SQL)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
if ((error_num = dbton_hdl->append_item_type_part( if ((error_num = dbton_hdl->append_item_type_part(
query.where, NULL, 0, TRUE, fields, SPIDER_SQL_TYPE_SELECT_SQL))) query.where, NULL, 0, TRUE, fields, SPIDER_SQL_TYPE_SELECT_SQL)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
} }
if (query.group_by) if (query.group_by)
{ {
if ((error_num = dbton_hdl->append_group_by_part( if ((error_num = dbton_hdl->append_group_by_part(
query.group_by, NULL, 0, TRUE, fields, SPIDER_SQL_TYPE_SELECT_SQL))) query.group_by, NULL, 0, TRUE, fields, SPIDER_SQL_TYPE_SELECT_SQL)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
} }
if (query.having) if (query.having)
{ {
if ((error_num = if ((error_num =
dbton_hdl->append_having_part(SPIDER_SQL_TYPE_SELECT_SQL))) dbton_hdl->append_having_part(SPIDER_SQL_TYPE_SELECT_SQL)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
if ((error_num = dbton_hdl->append_item_type_part( if ((error_num = dbton_hdl->append_item_type_part(
query.having, NULL, 0, TRUE, fields, SPIDER_SQL_TYPE_SELECT_SQL))) query.having, NULL, 0, TRUE, fields, SPIDER_SQL_TYPE_SELECT_SQL)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
} }
if (query.order_by) if (query.order_by)
{ {
if ((error_num = dbton_hdl->append_order_by_part( if ((error_num = dbton_hdl->append_order_by_part(
query.order_by, NULL, 0, TRUE, fields, SPIDER_SQL_TYPE_SELECT_SQL))) query.order_by, NULL, 0, TRUE, fields,
{ SPIDER_SQL_TYPE_SELECT_SQL)))
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
} }
if ((error_num = dbton_hdl->append_limit_part(result_list->internal_offset, if ((error_num = dbton_hdl->append_limit_part(
result_list->limit_num, SPIDER_SQL_TYPE_SELECT_SQL))) result_list->internal_offset, result_list->limit_num,
{ SPIDER_SQL_TYPE_SELECT_SQL)))
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
if ((error_num = dbton_hdl->append_select_lock_part( if ((error_num = dbton_hdl->append_select_lock_part(
SPIDER_SQL_TYPE_SELECT_SQL))) SPIDER_SQL_TYPE_SELECT_SQL)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
} }
DBUG_RETURN(0);
}
static int spider_send_query(
spider_fields *fields, ha_spider *spider, SPIDER_TRX *trx, TABLE *table,
int& store_error)
{
int error_num, link_idx;
spider_db_handler *dbton_hdl;
SPIDER_RESULT_LIST *result_list = &spider->result_list;
SPIDER_SHARE *share = spider->share;
SPIDER_CONN *conn;
SPIDER_LINK_IDX_CHAIN *link_idx_chain;
SPIDER_LINK_IDX_HOLDER *link_idx_holder;
DBUG_ENTER("spider_send_query");
fields->set_pos_to_first_link_idx_chain(); fields->set_pos_to_first_link_idx_chain();
while ((link_idx_chain = fields->get_next_link_idx_chain())) while ((link_idx_chain = fields->get_next_link_idx_chain()))
...@@ -1204,12 +1184,8 @@ int spider_group_by_handler::init_scan() ...@@ -1204,12 +1184,8 @@ int spider_group_by_handler::init_scan()
dbton_hdl->first_link_idx, TRUE, FALSE, dbton_hdl->first_link_idx, TRUE, FALSE,
!fields->is_first_link_ok_chain(link_idx_chain)))) !fields->is_first_link_ok_chain(link_idx_chain))))
{ {
if ( if (error_num != HA_ERR_END_OF_FILE && spider->need_mons[link_idx])
error_num != HA_ERR_END_OF_FILE &&
spider->need_mons[link_idx]
) {
error_num = fields->ping_table_mon_from_table(link_idx_chain); error_num = fields->ping_table_mon_from_table(link_idx_chain);
}
if ((error_num = spider->check_error_mode_eof(error_num)) == HA_ERR_END_OF_FILE) if ((error_num = spider->check_error_mode_eof(error_num)) == HA_ERR_END_OF_FILE)
{ {
store_error = HA_ERR_END_OF_FILE; store_error = HA_ERR_END_OF_FILE;
...@@ -1220,12 +1196,9 @@ int spider_group_by_handler::init_scan() ...@@ -1220,12 +1196,9 @@ int spider_group_by_handler::init_scan()
} else { } else {
#endif #endif
pthread_mutex_assert_not_owner(&conn->mta_conn_mutex); pthread_mutex_assert_not_owner(&conn->mta_conn_mutex);
if ((error_num = if ((error_num = dbton_hdl->set_sql_for_exec(
dbton_hdl->set_sql_for_exec(SPIDER_SQL_TYPE_SELECT_SQL, link_idx, SPIDER_SQL_TYPE_SELECT_SQL, link_idx, link_idx_chain)))
link_idx_chain)))
{
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
}
pthread_mutex_lock(&conn->mta_conn_mutex); pthread_mutex_lock(&conn->mta_conn_mutex);
SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos); SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
conn->need_mon = &spider->need_mons[link_idx]; conn->need_mon = &spider->need_mons[link_idx];
...@@ -1235,6 +1208,7 @@ int spider_group_by_handler::init_scan() ...@@ -1235,6 +1208,7 @@ int spider_group_by_handler::init_scan()
conn->mta_conn_mutex_unlock_later = TRUE; conn->mta_conn_mutex_unlock_later = TRUE;
if ((error_num = spider_db_set_names(spider, conn, if ((error_num = spider_db_set_names(spider, conn,
link_idx))) link_idx)))
if ((error_num = spider_db_set_names(spider, conn, link_idx)))
{ {
DBUG_ASSERT(conn->mta_conn_mutex_lock_already); DBUG_ASSERT(conn->mta_conn_mutex_lock_already);
DBUG_ASSERT(conn->mta_conn_mutex_unlock_later); DBUG_ASSERT(conn->mta_conn_mutex_unlock_later);
...@@ -1242,37 +1216,32 @@ int spider_group_by_handler::init_scan() ...@@ -1242,37 +1216,32 @@ int spider_group_by_handler::init_scan()
conn->mta_conn_mutex_unlock_later = FALSE; conn->mta_conn_mutex_unlock_later = FALSE;
SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos); SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
pthread_mutex_unlock(&conn->mta_conn_mutex); pthread_mutex_unlock(&conn->mta_conn_mutex);
if ( if (spider->need_mons[link_idx])
spider->need_mons[link_idx]
) {
error_num = fields->ping_table_mon_from_table(link_idx_chain); error_num = fields->ping_table_mon_from_table(link_idx_chain);
} if ((error_num = spider->check_error_mode_eof(error_num)) ==
if ((error_num = spider->check_error_mode_eof(error_num)) == HA_ERR_END_OF_FILE) HA_ERR_END_OF_FILE)
{ {
store_error = HA_ERR_END_OF_FILE; store_error = HA_ERR_END_OF_FILE;
error_num = 0; error_num = 0;
} }
DBUG_RETURN(error_num); DBUG_RETURN(error_num);
} }
spider_conn_set_timeout_from_share(conn, link_idx, spider_conn_set_timeout_from_share(conn, link_idx, trx->thd, share);
trx->thd, share);
if (dbton_hdl->execute_sql( if (dbton_hdl->execute_sql(
SPIDER_SQL_TYPE_SELECT_SQL, SPIDER_SQL_TYPE_SELECT_SQL,
conn, conn,
spider->result_list.quick_mode, spider->result_list.quick_mode,
&spider->need_mons[link_idx]) &spider->need_mons[link_idx]))
) { {
DBUG_ASSERT(conn->mta_conn_mutex_lock_already); DBUG_ASSERT(conn->mta_conn_mutex_lock_already);
DBUG_ASSERT(conn->mta_conn_mutex_unlock_later); DBUG_ASSERT(conn->mta_conn_mutex_unlock_later);
conn->mta_conn_mutex_lock_already = FALSE; conn->mta_conn_mutex_lock_already = FALSE;
conn->mta_conn_mutex_unlock_later = FALSE; conn->mta_conn_mutex_unlock_later = FALSE;
error_num = spider_db_errorno(conn); error_num = spider_db_errorno(conn);
if ( if (spider->need_mons[link_idx])
spider->need_mons[link_idx]
) {
error_num = fields->ping_table_mon_from_table(link_idx_chain); error_num = fields->ping_table_mon_from_table(link_idx_chain);
} if ((error_num = spider->check_error_mode_eof(error_num)) ==
if ((error_num = spider->check_error_mode_eof(error_num)) == HA_ERR_END_OF_FILE) HA_ERR_END_OF_FILE)
{ {
store_error = HA_ERR_END_OF_FILE; store_error = HA_ERR_END_OF_FILE;
error_num = 0; error_num = 0;
...@@ -1288,13 +1257,10 @@ int spider_group_by_handler::init_scan() ...@@ -1288,13 +1257,10 @@ int spider_group_by_handler::init_scan()
{ {
if ((error_num = spider_db_store_result(spider, link_idx, table))) if ((error_num = spider_db_store_result(spider, link_idx, table)))
{ {
if ( if (error_num != HA_ERR_END_OF_FILE && spider->need_mons[link_idx])
error_num != HA_ERR_END_OF_FILE &&
spider->need_mons[link_idx]
) {
error_num = fields->ping_table_mon_from_table(link_idx_chain); error_num = fields->ping_table_mon_from_table(link_idx_chain);
} if ((error_num = spider->check_error_mode_eof(error_num)) ==
if ((error_num = spider->check_error_mode_eof(error_num)) == HA_ERR_END_OF_FILE) HA_ERR_END_OF_FILE)
{ {
store_error = HA_ERR_END_OF_FILE; store_error = HA_ERR_END_OF_FILE;
error_num = 0; error_num = 0;
...@@ -1303,7 +1269,8 @@ int spider_group_by_handler::init_scan() ...@@ -1303,7 +1269,8 @@ int spider_group_by_handler::init_scan()
} }
spider->result_link_idx = link_idx; spider->result_link_idx = link_idx;
spider->result_link_idx_chain = link_idx_chain; spider->result_link_idx_chain = link_idx_chain;
} else { } else
{
spider_db_discard_result(spider, link_idx, conn); spider_db_discard_result(spider, link_idx, conn);
SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos); SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
pthread_mutex_unlock(&conn->mta_conn_mutex); pthread_mutex_unlock(&conn->mta_conn_mutex);
...@@ -1312,6 +1279,35 @@ int spider_group_by_handler::init_scan() ...@@ -1312,6 +1279,35 @@ int spider_group_by_handler::init_scan()
} }
#endif #endif
} }
DBUG_RETURN(0);
}
int spider_group_by_handler::init_scan()
{
int error_num;
DBUG_ENTER("spider_group_by_handler::init_scan");
store_error = 0;
#ifndef DBUG_OFF
Field **field;
for (field = table->field; *field; field++)
DBUG_PRINT("info",("spider field_name=%s", SPIDER_field_name_str(*field)));
#endif
if (trx->thd->killed)
{
my_error(ER_QUERY_INTERRUPTED, MYF(0));
DBUG_RETURN(ER_QUERY_INTERRUPTED);
}
if ((error_num = spider_prepare_init_scan(
query, fields, spider, trx, offset_limit, thd)))
DBUG_RETURN(error_num);
if ((error_num = spider_make_query(query, fields, spider, table)))
DBUG_RETURN(error_num);
if ((error_num = spider_send_query(fields, spider, trx, table, store_error)))
DBUG_RETURN(error_num);
first = TRUE; first = TRUE;
DBUG_RETURN(0); DBUG_RETURN(0);
......
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