#
# Check of check constraints

set @save_check_constraint=@@check_constraint_checks;

create table t1 (a int check(a>10), b int check (b > 20), constraint `min` check (a+b > 100), constraint `max` check (a+b <500)) engine=myisam;
show create table t1;
insert into t1 values (100,100);
--error ER_CONSTRAINT_FAILED
insert into t1 values (1,1);
--error ER_CONSTRAINT_FAILED
insert into t1 values (20,1);
--error ER_CONSTRAINT_FAILED
insert into t1 values (20,30);
--error ER_CONSTRAINT_FAILED
insert into t1 values (500,500);

--error ER_CONSTRAINT_FAILED
insert into t1 values (101,101),(102,102),(600,600),(103,103);
select * from t1;
truncate table t1;
insert ignore into t1 values (101,101),(102,102),(600,600),(103,103);
select * from t1;
set check_constraint_checks=0;
truncate table t1;
insert into t1 values (101,101),(102,102),(600,600),(103,103);
select * from t1;
set check_constraint_checks=@save_check_constraint;

--error ER_CONSTRAINT_FAILED
alter table t1 add c int default 0 check (c < 10);

set check_constraint_checks=0;
alter table t1 add c int default 0 check (c < 10);
alter table t1 add check (a+b+c < 500);
set check_constraint_checks=@save_check_constraint;

show create table t1;
--error ER_CONSTRAINT_FAILED
insert into t1 values(105,105,105);
--error ER_CONSTRAINT_FAILED
insert into t1 values(249,249,9);
insert into t1 values(105,105,9);
select * from t1;

create table t2 like t1;
show create table t2;
--error ER_CANT_DROP_FIELD_OR_KEY
alter table t2 drop constraint c;
alter table t2 drop constraint if exists c;
alter table t2 drop constraint min;
show create table t2;

drop table t1,t2;

#
# check constraint name auto-generation:
#
create or replace table t1 (a int, b int, constraint check (a>b));
show create table t1;
create or replace table t1 (a int, b int,
  constraint CONSTRAINT_1 check (a>1),
  constraint check (b>1));
show create table t1;
create or replace table t1 (a int, b int,
  constraint CONSTRAINT_1 check (a>1),
  constraint check (b>1),
  constraint CONSTRAINT_2 check (a>b));
show create table t1;
drop table t1;

--echo #
--echo # MDEV-10370 Check constraints on virtual columns fails on INSERT when column not specified
--echo #
create table t1(c1 int, c2 int as (c1 + 1), check (c2 > 2));
--error ER_CONSTRAINT_FAILED
insert into t1(c1) values(1);
insert into t1(c1) values(2);
drop table t1;

--echo #
--echo # MDEV-11117 CHECK constraint fails on intermediate step of ALTER
--echo #
-- error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
create or replace table t1( c1 int auto_increment primary key, check( c1 > 0 or c1 is null ) );

--echo #
--echo # MDEV-12421 Check constraint with query crashes server and renders DB unusable
--echo #
--error ER_SUBQUERIES_NOT_SUPPORTED
create table t1 (a int check (@b in (select user from mysql.user)));
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
create table t1 (a int check (a > @b));

--echo #
--echo # MDEV-13596 CHECK constraints disallow NULL to pass through, violating SQL
--echo #
create table t1 (a int check (a = 1));
insert t1 values (1);
--error ER_CONSTRAINT_FAILED
insert t1 values (2);
insert t1 values (NULL);
select * from t1;
drop table t1;

--echo #
--echo # MDEV-15141 Check constraint validation on a datetime field crashes the process
--echo #
create table t1 (id int auto_increment primary key, datecol datetime, check (datecol>'0001-01-01 00:00:00'));
insert into t1 (datecol) values (now());
insert into t1 (datecol) values (now());
drop table t1;

--echo #
--echo # MDEV-15461 Check Constraints with binary logging makes insert inconsistent
--echo #

CREATE TABLE t1 (
  EmployeeID SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  FirstName VARCHAR(30) NOT NULL CHECK (CHAR_LENGTH(FirstName > 2))
);

--error ER_TRUNCATED_WRONG_VALUE
INSERT INTO t1 VALUES (NULL, 'Ken');
SHOW WARNINGS;
--error ER_TRUNCATED_WRONG_VALUE
INSERT INTO t1 VALUES (NULL, 'Ken'),(NULL, 'Brian');
SHOW WARNINGS;
INSERT IGNORE INTO t1 VALUES (NULL, 'Ken');
INSERT IGNORE INTO t1 VALUES (NULL, 'Ken'),(NULL, 'Brian');
set sql_mode="";
INSERT INTO t1 VALUES (NULL, 'Ken');
INSERT INTO t1 VALUES (NULL, 'Ken'),(NULL, 'Brian');
set sql_mode=default;
select * from t1;
drop table t1;

--echo #
--echo # MDEV-16630: Ambiguous error message when check constraint matches table name
--echo #

use test;
--disable_warnings
drop table if exists t;
--enable_warnings

create table t(a int, b int check(b>0),
               constraint b check(a<b), constraint a check(a>0),
               constraint x check (a>10));

show create table t;

# Generate error when field constraint 'b' is violated
--echo # Field constraint 'b' will fail
--error ER_CONSTRAINT_FAILED
insert into t values (-1, 0);

# Generate error when table constraint 'b' is violated.
--echo # Table constraint 'b' will fail
--error ER_CONSTRAINT_FAILED
insert into t values (1,1);

drop table t;

#
# check constraints and auto_is_null typo
#
create table t1 (a int auto_increment primary key, b int, check (b > 5));
--error ER_CONSTRAINT_FAILED
insert t1 (b) values (1);
insert t1 (b) values (10);
select * from t1 where a is null;
set sql_auto_is_null=1;
#Enable after fix MDEV-31307
--disable_ps2_protocol
select * from t1 where a is null;
--enable_ps2_protocol
--error ER_CONSTRAINT_FAILED
insert t1 (b) values (1);
drop table t1;

--echo #
--echo # MDEV-25638 Assertion `!result' failed in convert_const_to_int
--echo #

--enable_prepare_warnings
create table t1 (v1 bigint check (v1 not in ('x' , 'x111'))) ;
select * from t1;
select v1 from t1;
select * from t1;
prepare stmt from "select * from t1";
execute stmt;
execute stmt;
flush tables;
select * from t1;
select * from t1;
deallocate prepare stmt;
drop table t1;
--disable_prepare_warnings

--echo #
--echo # MDEV-26061 MariaDB server crash at Field::set_default
--echo #

create table t1 (v2 date check (v1 like default (v1)), v1 date default (from_days ('x')));
insert ignore into t1 values ( 'x' , 'x' ) ;
drop table t1;

--echo #
--echo # MDEV-15338 Assertion `!table || (!table->read_set || bitmap_is_set(table->read_set, field_index))' failed on dropping column with CHECK
--echo #

CREATE TABLE t1 (a INT, b INT, CHECK (a>0)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,2),(3,4);
ALTER TABLE t1 DROP COLUMN a;
CREATE OR REPLACE TABLE t1 (a INT, b INT, CHECK (a>0)) ENGINE=MyISAM;
ALTER TABLE t1 DROP COLUMN b;
CREATE OR REPLACE TABLE t1 (a INT, b INT, c INT, CHECK (a+b>0)) ENGINE=MyISAM;
--error ER_BAD_FIELD_ERROR
ALTER TABLE t1 DROP COLUMN b;
ALTER TABLE t1 DROP COLUMN a, DROP COLUMN b;
CREATE OR REPLACE TABLE t1 (a INT, b INT, c INT, CHECK (a+b>0)) ENGINE=MyISAM;
ALTER TABLE t1 DROP CONSTRAINT `CONSTRAINT_1`;
SHOW CREATE TABLE t1;
CREATE OR REPLACE TABLE t1 (a INT, b INT, c INT, CHECK (a+b>0)) ENGINE=MyISAM;
ALTER TABLE t1 DROP COLUMN b, DROP CONSTRAINT `CONSTRAINT_1`;
SHOW CREATE TABLE t1;
DROP TABLE t1;

--echo #
--echo # MDEV-16903 Assertion `!auto_increment_field_not_null' failed in TABLE::init after unsuccessful attempt to add CHECK constraint on temporary table
--echo #
create temporary table t1 (
  id int not null auto_increment primary key,
  f int not null default 0
);
insert into t1 () values ();
alter ignore table t1 add constraint check (f > 0);
alter table t1;
drop table t1;

--echo #
--echo # MDEV-16905 ASAN heap-use-after-free in __interceptor_strnlen / ... / TABLE::verify_constraints upon INSERT into temporary table with CHECK constraint
--echo #
create temporary table t1 (a int default 0, check (a > 0));
alter table t1 drop constraint if exists non_existing_constraint;
--error ER_CONSTRAINT_FAILED
insert into t1 () values ();
drop table t1;

--echo # End of 10.2 tests

--echo #
--echo # MDEV-26061 MariaDB server crash at Field::set_default
--echo #

create table t1 (d timestamp not null default now() check (default (d) is true)) as select 1;
show create table t1;
drop table t1;

--echo # End of 10.3 tests

--echo #
--echo # MDEV-24274 ALTER TABLE with CHECK CONSTRAINTS gives "Out of Memory" error
--echo #
create table t1 (id varchar(2), constraint id check (id regexp '[a-z]'));
alter table t1 force;
show create table t1;
drop table t1;

--echo #
--echo # MDEV-32586 incorrect error about cyclic reference about JSON type virtual column
--echo #

create table t1 (a int, b json as (a));
drop table t1;

create table t1 (a int, b int as (a) check (b > 0));
insert t1 (a) values (1);
--error ER_CONSTRAINT_FAILED
insert t1 (a) values (-1);
drop table t1;

--echo # End of 10.4 tests

--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 # End of 10.11 tests

--echo #
--echo # MDEV-32439 INSERT IGNORE VALUES (one row) errors on constraint
--echo #

CREATE TABLE t1 (v1 varchar(10), v2 varchar(10), constraint unequal check (v1 != v2));
INSERT IGNORE INTO t1 VALUES (1,1);
SHOW WARNINGS;
INSERT IGNORE INTO t1 VALUES (1,2),(2,2);
SHOW WARNINGS;
INSERT IGNORE INTO t1 VALUES (3,3),(4,4);
SHOW WARNINGS;
SELECT * FROM t1;
DROP TABLE t1;

--echo #
--echo # MDEV-32439 INSERT IGNORE VALUES (one row) errors on constraint
--echo #

CREATE TABLE t1 (v1 varchar(10), v2 varchar(10), constraint unequal check (v1 != v2));
INSERT IGNORE INTO t1 VALUES (1,1);
SHOW WARNINGS;
INSERT IGNORE INTO t1 VALUES (1,2),(2,2);
SHOW WARNINGS;
INSERT IGNORE INTO t1 VALUES (3,3),(4,4);
SHOW WARNINGS;
SELECT * FROM t1;
DROP TABLE t1;

--echo # End of 11.4 tests
