Skip to content

Commit 629b8d7

Browse files
committed
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.
1 parent f66cd04 commit 629b8d7

File tree

3 files changed

+154
-3
lines changed

3 files changed

+154
-3
lines changed

mysql-test/main/check_constraint.result

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,61 @@ drop table t1;
320320
#
321321
# End of 10.4 tests
322322
#
323+
#
324+
# MDEV-36662 CHECK constraint does not repeat in case of error
325+
#
326+
create table t1 (d date check (d > 2020-01-01));
327+
insert into t1 values ('2023-12-05');
328+
ERROR 22007: Truncated incorrect datetime value: '2018'
329+
INSERT into t1 values ('2024-12-05');
330+
ERROR 22007: Truncated incorrect datetime value: '2018'
331+
create or replace table t1 (d time check (d > "a"));
332+
insert into t1 values ('22:30');
333+
ERROR 22007: Truncated incorrect time value: 'a'
334+
insert into t1 values ('23:30');
335+
ERROR 22007: Truncated incorrect time value: 'a'
336+
create or replace table t1 (d datetime check (d > "a"));
337+
insert into t1 values ('2023-12-05');
338+
ERROR 22007: Truncated incorrect datetime value: 'a'
339+
insert into t1 values ('2024-12-05');
340+
ERROR 22007: Truncated incorrect datetime value: 'a'
341+
create or replace table t1 (d timestamp check (d > "a"));
342+
insert into t1 values ('2023-12-05');
343+
ERROR 22007: Truncated incorrect datetime value: 'a'
344+
insert into t1 values ('2024-12-05');
345+
ERROR 22007: Truncated incorrect datetime value: 'a'
346+
create or replace table t1 (d year check (d > "a"));
347+
insert into t1 values ('2023');
348+
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
349+
insert into t1 values ('2024');
350+
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
351+
create or replace table t1 (d int check (d > "a"));
352+
insert into t1 values (0);
353+
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
354+
insert into t1 values (1);
355+
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
356+
create or replace table t1 (d real check (d > "a"));
357+
insert into t1 values (0.1);
358+
ERROR 22007: Truncated incorrect DOUBLE value: 'a'
359+
insert into t1 values (1.1);
360+
ERROR 22007: Truncated incorrect DOUBLE value: 'a'
361+
create or replace table t1 (d decimal check (d > "a"));
362+
insert into t1 values (0);
363+
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
364+
insert into t1 values (1);
365+
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
366+
create or replace table t1 (d bool check (d != "a"));
367+
insert into t1 values (0);
368+
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
369+
insert into t1 values (1);
370+
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
371+
drop table t1;
372+
create or replace table t1 (d varchar(30) check (d != 1));
373+
insert into t1 values ("a");
374+
ERROR 22007: Truncated incorrect DECIMAL value: 'a'
375+
insert into t1 values ("b");
376+
ERROR 22007: Truncated incorrect DECIMAL value: 'b'
377+
drop table t1;
378+
#
379+
# End of 10.11 tests
380+
#

mysql-test/main/check_constraint.test

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,64 @@ drop table t1;
247247
--echo #
248248
--echo # End of 10.4 tests
249249
--echo #
250+
251+
--echo #
252+
--echo # MDEV-36662 CHECK constraint does not repeat in case of error
253+
--echo #
254+
create table t1 (d date check (d > 2020-01-01));
255+
--error ER_TRUNCATED_WRONG_VALUE
256+
insert into t1 values ('2023-12-05');
257+
--error ER_TRUNCATED_WRONG_VALUE
258+
INSERT into t1 values ('2024-12-05');
259+
create or replace table t1 (d time check (d > "a"));
260+
--error ER_TRUNCATED_WRONG_VALUE
261+
insert into t1 values ('22:30');
262+
--error ER_TRUNCATED_WRONG_VALUE
263+
insert into t1 values ('23:30');
264+
create or replace table t1 (d datetime check (d > "a"));
265+
--error ER_TRUNCATED_WRONG_VALUE
266+
insert into t1 values ('2023-12-05');
267+
--error ER_TRUNCATED_WRONG_VALUE
268+
insert into t1 values ('2024-12-05');
269+
create or replace table t1 (d timestamp check (d > "a"));
270+
--error ER_TRUNCATED_WRONG_VALUE
271+
insert into t1 values ('2023-12-05');
272+
--error ER_TRUNCATED_WRONG_VALUE
273+
insert into t1 values ('2024-12-05');
274+
create or replace table t1 (d year check (d > "a"));
275+
--error ER_TRUNCATED_WRONG_VALUE
276+
insert into t1 values ('2023');
277+
--error ER_TRUNCATED_WRONG_VALUE
278+
insert into t1 values ('2024');
279+
create or replace table t1 (d int check (d > "a"));
280+
--error ER_TRUNCATED_WRONG_VALUE
281+
insert into t1 values (0);
282+
--error ER_TRUNCATED_WRONG_VALUE
283+
insert into t1 values (1);
284+
create or replace table t1 (d real check (d > "a"));
285+
--error ER_TRUNCATED_WRONG_VALUE
286+
insert into t1 values (0.1);
287+
--error ER_TRUNCATED_WRONG_VALUE
288+
insert into t1 values (1.1);
289+
create or replace table t1 (d decimal check (d > "a"));
290+
--error ER_TRUNCATED_WRONG_VALUE
291+
insert into t1 values (0);
292+
--error ER_TRUNCATED_WRONG_VALUE
293+
insert into t1 values (1);
294+
create or replace table t1 (d bool check (d != "a"));
295+
--error ER_TRUNCATED_WRONG_VALUE
296+
insert into t1 values (0);
297+
--error ER_TRUNCATED_WRONG_VALUE
298+
insert into t1 values (1);
299+
drop table t1;
300+
create or replace table t1 (d varchar(30) check (d != 1));
301+
--error ER_TRUNCATED_WRONG_VALUE
302+
insert into t1 values ("a");
303+
--error ER_TRUNCATED_WRONG_VALUE
304+
insert into t1 values ("b");
305+
drop table t1;
306+
307+
--echo #
308+
--echo # End of 10.11 tests
309+
--echo #
310+

sql/item.cc

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10448,7 +10448,11 @@ bool Item_cache_bool::cache_value()
1044810448
if (!example)
1044910449
return false;
1045010450
value_cached= true;
10451+
THD *thd= current_thd;
10452+
const bool err= thd->is_error();
1045110453
value= example->val_bool_result();
10454+
if (!err && thd->is_error())
10455+
value_cached= false;
1045210456
null_value_inside= null_value= example->null_value;
1045310457
unsigned_flag= false;
1045410458
return true;
@@ -10460,7 +10464,11 @@ bool Item_cache_int::cache_value()
1046010464
if (!example)
1046110465
return FALSE;
1046210466
value_cached= TRUE;
10467+
THD *thd= current_thd;
10468+
const bool err= thd->is_error();
1046310469
value= example->val_int_result();
10470+
if (!err && thd->is_error())
10471+
value_cached= false;
1046410472
null_value_inside= null_value= example->null_value;
1046510473
unsigned_flag= example->unsigned_flag;
1046610474
return TRUE;
@@ -10537,7 +10545,11 @@ bool Item_cache_temporal::cache_value()
1053710545
if (!example)
1053810546
return false;
1053910547
value_cached= true;
10540-
value= example->val_datetime_packed_result(current_thd);
10548+
THD *thd= current_thd;
10549+
const bool err= thd->is_error();
10550+
value= example->val_datetime_packed_result(thd);
10551+
if (!err && thd->is_error())
10552+
value_cached= false;
1054110553
null_value_inside= null_value= example->null_value;
1054210554
return true;
1054310555
}
@@ -10548,7 +10560,11 @@ bool Item_cache_time::cache_value()
1054810560
if (!example)
1054910561
return false;
1055010562
value_cached= true;
10551-
value= example->val_time_packed_result(current_thd);
10563+
THD *thd= current_thd;
10564+
const bool err= thd->is_error();
10565+
value= example->val_time_packed_result(thd);
10566+
if (!err && thd->is_error())
10567+
value_cached= false;
1055210568
null_value_inside= null_value= example->null_value;
1055310569
return true;
1055410570
}
@@ -10676,8 +10692,12 @@ bool Item_cache_timestamp::cache_value()
1067610692
if (!example)
1067710693
return false;
1067810694
value_cached= true;
10695+
THD *thd= current_thd;
10696+
const bool err= thd->is_error();
1067910697
null_value_inside= null_value=
10680-
example->val_native_with_conversion_result(current_thd, &m_native, type_handler());
10698+
example->val_native_with_conversion_result(thd, &m_native, type_handler());
10699+
if (!err && thd->is_error())
10700+
value_cached= false;
1068110701
return true;
1068210702
}
1068310703

@@ -10687,7 +10707,11 @@ bool Item_cache_real::cache_value()
1068710707
if (!example)
1068810708
return FALSE;
1068910709
value_cached= TRUE;
10710+
THD *thd= current_thd;
10711+
const bool err= thd->is_error();
1069010712
value= example->val_result();
10713+
if (!err && thd->is_error())
10714+
value_cached= false;
1069110715
null_value_inside= null_value= example->null_value;
1069210716
return TRUE;
1069310717
}
@@ -10754,7 +10778,11 @@ bool Item_cache_decimal::cache_value()
1075410778
if (!example)
1075510779
return FALSE;
1075610780
value_cached= TRUE;
10781+
THD *thd= current_thd;
10782+
const bool err= thd->is_error();
1075710783
my_decimal *val= example->val_decimal_result(&decimal_value);
10784+
if (!err && thd->is_error())
10785+
value_cached= false;
1075810786
if (!(null_value_inside= null_value= example->null_value) &&
1075910787
val != &decimal_value)
1076010788
my_decimal2decimal(val, &decimal_value);
@@ -10810,8 +10838,12 @@ bool Item_cache_str::cache_value()
1081010838
return FALSE;
1081110839
}
1081210840
value_cached= TRUE;
10841+
THD *thd= current_thd;
10842+
const bool err= thd->is_error();
1081310843
value_buff.set(buffer, sizeof(buffer), example->collation.collation);
1081410844
value= example->str_result(&value_buff);
10845+
if (!err && thd->is_error())
10846+
value_cached= false;
1081510847
if ((null_value= null_value_inside= example->null_value))
1081610848
value= 0;
1081710849
else if (value != &value_buff)

0 commit comments

Comments
 (0)