MDEV-36662 CHECK constraint does not repeat in case of error

CHECK constraint uses caching items and the value is cached even in
case of error. At the second execution the cached value is taken and
the error is not thrown. The fix does not cache value in case error
was thrown during value retrieval.
This commit is contained in:
Aleksey Midenkov 2025-06-12 18:22:29 +03:00
parent f1f9284181
commit 9eb3edb1dc
3 changed files with 154 additions and 3 deletions

View File

@ -320,3 +320,61 @@ drop table t1;
#
# End of 10.4 tests
#
#
# MDEV-36662 CHECK constraint does not repeat in case of error
#
create table t1 (d date check (d > 2020-01-01));
insert into t1 values ('2023-12-05');
ERROR 22007: Truncated incorrect datetime value: '2018'
INSERT into t1 values ('2024-12-05');
ERROR 22007: Truncated incorrect datetime value: '2018'
create or replace table t1 (d time check (d > "a"));
insert into t1 values ('22:30');
ERROR 22007: Truncated incorrect time value: 'a'
insert into t1 values ('23:30');
ERROR 22007: Truncated incorrect time value: 'a'
create or replace table t1 (d datetime check (d > "a"));
insert into t1 values ('2023-12-05');
ERROR 22007: Truncated incorrect datetime value: 'a'
insert into t1 values ('2024-12-05');
ERROR 22007: Truncated incorrect datetime value: 'a'
create or replace table t1 (d timestamp check (d > "a"));
insert into t1 values ('2023-12-05');
ERROR 22007: Truncated incorrect datetime value: 'a'
insert into t1 values ('2024-12-05');
ERROR 22007: Truncated incorrect datetime value: 'a'
create or replace table t1 (d year check (d > "a"));
insert into t1 values ('2023');
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
insert into t1 values ('2024');
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
create or replace table t1 (d int check (d > "a"));
insert into t1 values (0);
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
insert into t1 values (1);
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
create or replace table t1 (d real check (d > "a"));
insert into t1 values (0.1);
ERROR 22007: Truncated incorrect DOUBLE value: 'a'
insert into t1 values (1.1);
ERROR 22007: Truncated incorrect DOUBLE value: 'a'
create or replace table t1 (d decimal check (d > "a"));
insert into t1 values (0);
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
insert into t1 values (1);
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
create or replace table t1 (d bool check (d != "a"));
insert into t1 values (0);
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
insert into t1 values (1);
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
drop table t1;
create or replace table t1 (d varchar(30) check (d != 1));
insert into t1 values ("a");
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
insert into t1 values ("b");
ERROR 22007: Truncated incorrect DECIMAL value: 'b'
drop table t1;
#
# End of 10.11 tests
#

View File

@ -247,3 +247,64 @@ drop table t1;
--echo #
--echo # End of 10.4 tests
--echo #
--echo #
--echo # MDEV-36662 CHECK constraint does not repeat in case of error
--echo #
create table t1 (d date check (d > 2020-01-01));
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ('2023-12-05');
--error ER_TRUNCATED_WRONG_VALUE
INSERT into t1 values ('2024-12-05');
create or replace table t1 (d time check (d > "a"));
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ('22:30');
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ('23:30');
create or replace table t1 (d datetime check (d > "a"));
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ('2023-12-05');
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ('2024-12-05');
create or replace table t1 (d timestamp check (d > "a"));
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ('2023-12-05');
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ('2024-12-05');
create or replace table t1 (d year check (d > "a"));
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ('2023');
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ('2024');
create or replace table t1 (d int check (d > "a"));
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values (0);
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values (1);
create or replace table t1 (d real check (d > "a"));
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values (0.1);
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values (1.1);
create or replace table t1 (d decimal check (d > "a"));
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values (0);
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values (1);
create or replace table t1 (d bool check (d != "a"));
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values (0);
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values (1);
drop table t1;
create or replace table t1 (d varchar(30) check (d != 1));
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ("a");
--error ER_TRUNCATED_WRONG_VALUE
insert into t1 values ("b");
drop table t1;
--echo #
--echo # End of 10.11 tests
--echo #

View File

@ -10446,7 +10446,11 @@ bool Item_cache_bool::cache_value()
if (!example)
return false;
value_cached= true;
THD *thd= current_thd;
const bool err= thd->is_error();
value= example->val_bool_result();
if (!err && thd->is_error())
value_cached= false;
null_value_inside= null_value= example->null_value;
unsigned_flag= false;
return true;
@ -10458,7 +10462,11 @@ bool Item_cache_int::cache_value()
if (!example)
return FALSE;
value_cached= TRUE;
THD *thd= current_thd;
const bool err= thd->is_error();
value= example->val_int_result();
if (!err && thd->is_error())
value_cached= false;
null_value_inside= null_value= example->null_value;
unsigned_flag= example->unsigned_flag;
return TRUE;
@ -10535,7 +10543,11 @@ bool Item_cache_temporal::cache_value()
if (!example)
return false;
value_cached= true;
value= example->val_datetime_packed_result(current_thd);
THD *thd= current_thd;
const bool err= thd->is_error();
value= example->val_datetime_packed_result(thd);
if (!err && thd->is_error())
value_cached= false;
null_value_inside= null_value= example->null_value;
return true;
}
@ -10546,7 +10558,11 @@ bool Item_cache_time::cache_value()
if (!example)
return false;
value_cached= true;
value= example->val_time_packed_result(current_thd);
THD *thd= current_thd;
const bool err= thd->is_error();
value= example->val_time_packed_result(thd);
if (!err && thd->is_error())
value_cached= false;
null_value_inside= null_value= example->null_value;
return true;
}
@ -10674,8 +10690,12 @@ bool Item_cache_timestamp::cache_value()
if (!example)
return false;
value_cached= true;
THD *thd= current_thd;
const bool err= thd->is_error();
null_value_inside= null_value=
example->val_native_with_conversion_result(current_thd, &m_native, type_handler());
example->val_native_with_conversion_result(thd, &m_native, type_handler());
if (!err && thd->is_error())
value_cached= false;
return true;
}
@ -10685,7 +10705,11 @@ bool Item_cache_real::cache_value()
if (!example)
return FALSE;
value_cached= TRUE;
THD *thd= current_thd;
const bool err= thd->is_error();
value= example->val_result();
if (!err && thd->is_error())
value_cached= false;
null_value_inside= null_value= example->null_value;
return TRUE;
}
@ -10752,7 +10776,11 @@ bool Item_cache_decimal::cache_value()
if (!example)
return FALSE;
value_cached= TRUE;
THD *thd= current_thd;
const bool err= thd->is_error();
my_decimal *val= example->val_decimal_result(&decimal_value);
if (!err && thd->is_error())
value_cached= false;
if (!(null_value_inside= null_value= example->null_value) &&
val != &decimal_value)
my_decimal2decimal(val, &decimal_value);
@ -10808,8 +10836,12 @@ bool Item_cache_str::cache_value()
return FALSE;
}
value_cached= TRUE;
THD *thd= current_thd;
const bool err= thd->is_error();
value_buff.set(buffer, sizeof(buffer), example->collation.collation);
value= example->str_result(&value_buff);
if (!err && thd->is_error())
value_cached= false;
if ((null_value= null_value_inside= example->null_value))
value= 0;
else if (value != &value_buff)