Skip to content

Commit 08bc062

Browse files
montywivuvova
authored andcommitted
Remove some usage of Check_level_instant_set and Sql_mode_save
The reason for the removal are: - Generates more code - Storing and retreving THD - Causes extra code and daata to be generated to handle possible throw exceptions (which never happens in MariaDB code) - Uses more stack space Other things: - Changed convert_const_to_int() to use item->save_in_field_no_warnings(), which made the code shorter and simpler. - Removed not needed code in Sp_handler::sp_create_routine() - Added thd as argument to store_key.copy() to make function simpler - Added thd as argument to some subselect* constructor that inherites from Item_subselect.
1 parent d754d3d commit 08bc062

File tree

9 files changed

+71
-47
lines changed

9 files changed

+71
-47
lines changed

sql/ha_partition.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4419,7 +4419,7 @@ int ha_partition::write_row(const uchar * buf)
44194419
bool have_auto_increment= table->next_number_field && buf == table->record[0];
44204420
MY_BITMAP *old_map;
44214421
THD *thd= ha_thd();
4422-
Sql_mode_save sms(thd);
4422+
sql_mode_t org_sql_mode= thd->variables.sql_mode;
44234423
bool saved_auto_inc_field_not_null= table->auto_increment_field_not_null;
44244424
DBUG_ENTER("ha_partition::write_row");
44254425
DBUG_PRINT("enter", ("partition this: %p", this));
@@ -4485,6 +4485,7 @@ int ha_partition::write_row(const uchar * buf)
44854485

44864486
exit:
44874487
table->auto_increment_field_not_null= saved_auto_inc_field_not_null;
4488+
thd->variables.sql_mode= org_sql_mode;
44884489
DBUG_RETURN(error);
44894490
}
44904491

sql/item.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,16 +1454,23 @@ int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
14541454
int res;
14551455
TABLE *table= field->table;
14561456
THD *thd= table->in_use;
1457-
Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
1458-
Sql_mode_save sms(thd);
1457+
enum_check_fields org_count_cuted_fields= thd->count_cuted_fields;
1458+
sql_mode_t org_sql_mode= thd->variables.sql_mode;
1459+
MY_BITMAP *old_map= dbug_tmp_use_all_columns(table, &table->write_set);
1460+
14591461
thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE);
14601462
thd->variables.sql_mode|= MODE_INVALID_DATES;
1461-
MY_BITMAP *old_map= dbug_tmp_use_all_columns(table, &table->write_set);
1463+
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1464+
14621465
res= save_in_field(field, no_conversions);
1466+
1467+
thd->count_cuted_fields= org_count_cuted_fields;
1468+
thd->variables.sql_mode= org_sql_mode;
14631469
dbug_tmp_restore_column_map(&table->write_set, old_map);
14641470
return res;
14651471
}
14661472

1473+
14671474
#ifndef DBUG_OFF
14681475
static inline
14691476
void mark_unsupported_func(const char *where, const char *processor_name)

sql/item_cmpfunc.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,29 +321,25 @@ static bool convert_const_to_int(THD *thd, Item_field *field_item,
321321
if ((*item)->can_eval_in_optimize())
322322
{
323323
TABLE *table= field->table;
324-
Sql_mode_save sql_mode(thd);
325-
Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
326324
MY_BITMAP *old_maps[2] = { NULL, NULL };
327325
ulonglong UNINIT_VAR(orig_field_val); /* original field value if valid */
326+
bool save_field_value;
328327

329328
/* table->read_set may not be set if we come here from a CREATE TABLE */
330329
if (table && table->read_set)
331330
dbug_tmp_use_all_columns(table, old_maps,
332331
&table->read_set, &table->write_set);
333-
/* For comparison purposes allow invalid dates like 2000-01-32 */
334-
thd->variables.sql_mode= (thd->variables.sql_mode & ~MODE_NO_ZERO_DATE) |
335-
MODE_INVALID_DATES;
336332

337333
/*
338334
Store the value of the field/constant because the call to save_in_field
339335
below overrides that value. Don't save field value if no data has been
340336
read yet.
341337
*/
342-
bool save_field_value= (field_item->const_item() ||
343-
!(field->table->status & STATUS_NO_RECORD));
338+
save_field_value= (field_item->const_item() ||
339+
!(field->table->status & STATUS_NO_RECORD));
344340
if (save_field_value)
345341
orig_field_val= field->val_int();
346-
if (!(*item)->save_in_field(field, 1) && !field->is_null())
342+
if (!(*item)->save_in_field_no_warnings(field, 1) && !field->is_null())
347343
{
348344
int field_cmp= 0;
349345
// If item is a decimal value, we must reject it if it was truncated.

sql/item_subselect.cc

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,9 @@ bool Item_subselect::walk(Item_processor processor, bool walk_subquery,
821821
bool Item_subselect::exec()
822822
{
823823
subselect_engine *org_engine= engine;
824-
825824
DBUG_ENTER("Item_subselect::exec");
826825
DBUG_ASSERT(fixed());
826+
DBUG_ASSERT(thd);
827827

828828
DBUG_EXECUTE_IF("Item_subselect",
829829
Item::Print print(this,
@@ -999,6 +999,8 @@ bool Item_in_subselect::exec()
999999
{
10001000
DBUG_ENTER("Item_in_subselect::exec");
10011001
DBUG_ASSERT(fixed());
1002+
DBUG_ASSERT(thd);
1003+
10021004
/*
10031005
Initialize the cache of the left predicate operand. This has to be done as
10041006
late as now, because Cached_item directly contains a resolved field (not
@@ -4005,11 +4007,10 @@ int join_read_next_same_or_null(READ_RECORD *info);
40054007

40064008
int subselect_single_select_engine::exec()
40074009
{
4008-
DBUG_ENTER("subselect_single_select_engine::exec");
4009-
40104010
char const *save_where= thd->where;
40114011
SELECT_LEX *save_select= thd->lex->current_select;
40124012
thd->lex->current_select= select_lex;
4013+
DBUG_ENTER("subselect_single_select_engine::exec");
40134014

40144015
if (join->optimization_state == JOIN::NOT_OPTIMIZED)
40154016
{
@@ -4219,7 +4220,7 @@ bool subselect_uniquesubquery_engine::copy_ref_key(bool skip_constants)
42194220
enum store_key::store_key_result store_res;
42204221
if (skip_constants && (*copy)->store_key_is_const())
42214222
continue;
4222-
store_res= (*copy)->copy();
4223+
store_res= (*copy)->copy(thd);
42234224
tab->ref.key_err= store_res;
42244225

42254226
if (store_res == store_key::STORE_KEY_FATAL)
@@ -4271,6 +4272,7 @@ int subselect_uniquesubquery_engine::exec()
42714272
table->status= 0;
42724273
Item_in_subselect *in_subs= item->get_IN_subquery();
42734274
DBUG_ASSERT(in_subs);
4275+
DBUG_ASSERT(thd);
42744276

42754277
if (!tab->preread_init_done && tab->preread_init())
42764278
DBUG_RETURN(1);
@@ -4431,6 +4433,7 @@ int subselect_indexsubquery_engine::exec()
44314433
bool null_finding= 0;
44324434
TABLE *table= tab->table;
44334435
Item_in_subselect *in_subs= item->get_IN_subquery();
4436+
DBUG_ASSERT(thd);
44344437

44354438
in_subs->value= 0;
44364439
empty_result_set= TRUE;
@@ -5650,7 +5653,6 @@ int subselect_hash_sj_engine::exec()
56505653
SELECT_LEX *save_select= thd->lex->current_select;
56515654
subselect_partial_match_engine *pm_engine= NULL;
56525655
int res= 0;
5653-
56545656
DBUG_ENTER("subselect_hash_sj_engine::exec");
56555657

56565658
/*
@@ -5757,7 +5759,8 @@ int subselect_hash_sj_engine::exec()
57575759
{
57585760
pm_engine=
57595761
(new (thd->mem_root)
5760-
subselect_rowid_merge_engine((subselect_uniquesubquery_engine*)
5762+
subselect_rowid_merge_engine(thd,
5763+
(subselect_uniquesubquery_engine*)
57615764
lookup_engine, tmp_table,
57625765
count_pm_keys,
57635766
has_covering_null_row,
@@ -5771,9 +5774,10 @@ int subselect_hash_sj_engine::exec()
57715774
init(nn_key_parts, &partial_match_key_parts))
57725775
{
57735776
/*
5774-
The call to init() would fail if there was not enough memory to allocate
5775-
all buffers for the rowid merge strategy. In this case revert to table
5776-
scanning which doesn't need any big buffers.
5777+
The call to init() would fail if there was not enough memory
5778+
to allocate all buffers for the rowid merge strategy. In
5779+
this case revert to table scanning which doesn't need any
5780+
big buffers.
57775781
*/
57785782
delete pm_engine;
57795783
pm_engine= NULL;
@@ -5785,7 +5789,8 @@ int subselect_hash_sj_engine::exec()
57855789
{
57865790
if (!(pm_engine=
57875791
(new (thd->mem_root)
5788-
subselect_table_scan_engine((subselect_uniquesubquery_engine*)
5792+
subselect_table_scan_engine(thd,
5793+
(subselect_uniquesubquery_engine*)
57895794
lookup_engine, tmp_table,
57905795
item, result,
57915796
semi_join_conds->argument_list(),
@@ -6255,6 +6260,7 @@ void Ordered_key::print(String *str)
62556260

62566261

62576262
subselect_partial_match_engine::subselect_partial_match_engine(
6263+
THD *thd_arg,
62586264
subselect_uniquesubquery_engine *engine_arg,
62596265
TABLE *tmp_table_arg, Item_subselect *item_arg,
62606266
select_result_interceptor *result_arg,
@@ -6268,13 +6274,16 @@ subselect_partial_match_engine::subselect_partial_match_engine(
62686274
has_covering_null_row(has_covering_null_row_arg),
62696275
has_covering_null_columns(has_covering_null_columns_arg),
62706276
count_columns_with_nulls(count_columns_with_nulls_arg)
6271-
{}
6277+
{
6278+
thd= thd_arg;
6279+
}
62726280

62736281

62746282
int subselect_partial_match_engine::exec()
62756283
{
62766284
Item_in_subselect *item_in= item->get_IN_subquery();
62776285
int lookup_res;
6286+
DBUG_ASSERT(thd);
62786287

62796288
DBUG_ASSERT(!(item_in->left_expr_has_null() &&
62806289
item_in->is_top_level_item()));
@@ -6877,6 +6886,7 @@ bool subselect_rowid_merge_engine::partial_match()
68776886

68786887

68796888
subselect_table_scan_engine::subselect_table_scan_engine(
6889+
THD *thd,
68806890
subselect_uniquesubquery_engine *engine_arg,
68816891
TABLE *tmp_table_arg,
68826892
Item_subselect *item_arg,
@@ -6885,7 +6895,7 @@ subselect_table_scan_engine::subselect_table_scan_engine(
68856895
bool has_covering_null_row_arg,
68866896
bool has_covering_null_columns_arg,
68876897
uint count_columns_with_nulls_arg)
6888-
:subselect_partial_match_engine(engine_arg, tmp_table_arg, item_arg,
6898+
:subselect_partial_match_engine(thd, engine_arg, tmp_table_arg, item_arg,
68896899
result_arg, equi_join_conds_arg,
68906900
has_covering_null_row_arg,
68916901
has_covering_null_columns_arg,

sql/item_subselect.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,10 @@ class subselect_uniquesubquery_engine: public subselect_engine
991991
subselect_uniquesubquery_engine(THD *thd_arg, st_join_table *tab_arg,
992992
Item_in_subselect *subs, Item *where)
993993
:subselect_engine(subs, 0), tab(tab_arg), cond(where)
994-
{ DBUG_ASSERT(subs); }
994+
{
995+
thd= thd_arg;
996+
DBUG_ASSERT(subs);
997+
}
995998
~subselect_uniquesubquery_engine();
996999
void cleanup();
9971000
int prepare(THD *);
@@ -1408,7 +1411,8 @@ class subselect_partial_match_engine : public subselect_engine
14081411
protected:
14091412
virtual bool partial_match()= 0;
14101413
public:
1411-
subselect_partial_match_engine(subselect_uniquesubquery_engine *engine_arg,
1414+
subselect_partial_match_engine(THD *thd,
1415+
subselect_uniquesubquery_engine *engine_arg,
14121416
TABLE *tmp_table_arg, Item_subselect *item_arg,
14131417
select_result_interceptor *result_arg,
14141418
List<Item> *equi_join_conds_arg,
@@ -1502,15 +1506,16 @@ class subselect_rowid_merge_engine: public subselect_partial_match_engine
15021506
bool exists_complementing_null_row(MY_BITMAP *keys_to_complement);
15031507
bool partial_match();
15041508
public:
1505-
subselect_rowid_merge_engine(subselect_uniquesubquery_engine *engine_arg,
1509+
subselect_rowid_merge_engine(THD *thd,
1510+
subselect_uniquesubquery_engine *engine_arg,
15061511
TABLE *tmp_table_arg, uint merge_keys_count_arg,
15071512
bool has_covering_null_row_arg,
15081513
bool has_covering_null_columns_arg,
15091514
uint count_columns_with_nulls_arg,
15101515
Item_subselect *item_arg,
15111516
select_result_interceptor *result_arg,
15121517
List<Item> *equi_join_conds_arg)
1513-
:subselect_partial_match_engine(engine_arg, tmp_table_arg,
1518+
:subselect_partial_match_engine(thd, engine_arg, tmp_table_arg,
15141519
item_arg, result_arg, equi_join_conds_arg,
15151520
has_covering_null_row_arg,
15161521
has_covering_null_columns_arg,
@@ -1529,7 +1534,8 @@ class subselect_table_scan_engine: public subselect_partial_match_engine
15291534
protected:
15301535
bool partial_match();
15311536
public:
1532-
subselect_table_scan_engine(subselect_uniquesubquery_engine *engine_arg,
1537+
subselect_table_scan_engine(THD *thd,
1538+
subselect_uniquesubquery_engine *engine_arg,
15331539
TABLE *tmp_table_arg, Item_subselect *item_arg,
15341540
select_result_interceptor *result_arg,
15351541
List<Item> *equi_join_conds_arg,

sql/sp.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,10 +1205,9 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
12051205
TABLE *table;
12061206
char definer_buf[USER_HOST_BUFF_SIZE];
12071207
LEX_CSTRING definer;
1208-
sql_mode_t saved_mode= thd->variables.sql_mode;
1209-
1208+
sql_mode_t org_sql_mode= thd->variables.sql_mode;
1209+
enum_check_fields org_count_cuted_fields= thd->count_cuted_fields;
12101210
CHARSET_INFO *db_cs= get_default_db_collation(thd, sp->m_db.str);
1211-
12121211
bool store_failed= FALSE;
12131212
DBUG_ENTER("sp_create_routine");
12141213
DBUG_PRINT("enter", ("type: %s name: %.*s",
@@ -1241,8 +1240,7 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
12411240

12421241
/* Reset sql_mode during data dictionary operations. */
12431242
thd->variables.sql_mode= 0;
1244-
1245-
Check_level_instant_set check_level_save(thd, CHECK_FIELD_WARN);
1243+
thd->count_cuted_fields= CHECK_FIELD_WARN;
12461244

12471245
if (!(table= open_proc_table_for_update(thd)))
12481246
{
@@ -1390,7 +1388,7 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
13901388

13911389
store_failed= store_failed ||
13921390
table->field[MYSQL_PROC_FIELD_SQL_MODE]->
1393-
store((longlong)saved_mode, TRUE);
1391+
store((longlong) org_sql_mode, TRUE);
13941392

13951393
if (sp->comment().str)
13961394
{
@@ -1478,13 +1476,13 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
14781476
sp->chistics(),
14791477
thd->lex->definer[0],
14801478
thd->lex->create_info,
1481-
saved_mode))
1479+
org_sql_mode))
14821480
{
14831481
my_error(ER_OUT_OF_RESOURCES, MYF(0));
14841482
goto done;
14851483
}
14861484
/* restore sql_mode when binloging */
1487-
thd->variables.sql_mode= saved_mode;
1485+
thd->variables.sql_mode= org_sql_mode;
14881486
/* Such a statement can always go directly to binlog, no trans cache */
14891487
if (thd->binlog_query(THD::STMT_QUERY_TYPE,
14901488
log_query.ptr(), log_query.length(),
@@ -1493,12 +1491,12 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
14931491
my_error(ER_ERROR_ON_WRITE, MYF(0), "binary log", -1);
14941492
goto done;
14951493
}
1496-
thd->variables.sql_mode= 0;
14971494
}
14981495
ret= FALSE;
14991496

15001497
done:
1501-
thd->variables.sql_mode= saved_mode;
1498+
thd->variables.sql_mode= org_sql_mode;
1499+
thd->count_cuted_fields= org_count_cuted_fields;
15021500
DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row());
15031501
DBUG_RETURN(ret);
15041502
}

sql/sql_select.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11039,7 +11039,7 @@ static bool create_ref_for_key(JOIN *join, JOIN_TAB *j,
1103911039
FALSE);
1104011040
if (unlikely(thd->is_fatal_error))
1104111041
DBUG_RETURN(TRUE);
11042-
tmp.copy();
11042+
tmp.copy(thd);
1104311043
j->ref.const_ref_part_map |= key_part_map(1) << i ;
1104411044
}
1104511045
else
@@ -24592,18 +24592,20 @@ cmp_buffer_with_ref(THD *thd, TABLE *table, TABLE_REF *tab_ref)
2459224592
bool
2459324593
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
2459424594
{
24595-
Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
24595+
enum_check_fields org_count_cuted_fields= thd->count_cuted_fields;
2459624596
MY_BITMAP *old_map= dbug_tmp_use_all_columns(table, &table->write_set);
2459724597
bool result= 0;
2459824598

24599+
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
2459924600
for (store_key **copy=ref->key_copy ; *copy ; copy++)
2460024601
{
24601-
if ((*copy)->copy() & 1)
24602+
if ((*copy)->copy(thd) & 1)
2460224603
{
2460324604
result= 1;
2460424605
break;
2460524606
}
2460624607
}
24608+
thd->count_cuted_fields= org_count_cuted_fields;
2460724609
dbug_tmp_restore_column_map(&table->write_set, old_map);
2460824610
return result;
2460924611
}

sql/sql_select.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,15 +1930,17 @@ class store_key :public Sql_alloc
19301930
@details this function makes sure truncation warnings when preparing the
19311931
key buffers don't end up as errors (because of an enclosing INSERT/UPDATE).
19321932
*/
1933-
enum store_key_result copy()
1933+
enum store_key_result copy(THD *thd)
19341934
{
19351935
enum store_key_result result;
1936-
THD *thd= to_field->table->in_use;
1937-
Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
1938-
Sql_mode_save sql_mode(thd);
1936+
enum_check_fields org_count_cuted_fields= thd->count_cuted_fields;
1937+
sql_mode_t org_sql_mode= thd->variables.sql_mode;
19391938
thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE);
19401939
thd->variables.sql_mode|= MODE_INVALID_DATES;
1940+
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
19411941
result= copy_inner();
1942+
thd->count_cuted_fields= org_count_cuted_fields;
1943+
thd->variables.sql_mode= org_sql_mode;
19421944
return result;
19431945
}
19441946

0 commit comments

Comments
 (0)