Commit 825c0e2a authored by Anel Husakovic's avatar Anel Husakovic

MDEV-24601: INFORMATION_SCHEMA doesn't differentiate between column and...

MDEV-24601: INFORMATION_SCHEMA doesn't differentiate between column and table-level CHECK constraints

- Reviewed by: wlad@mariadb.com
parent 8cbada87
......@@ -16,25 +16,25 @@ CREATE TABLE t0
t int, check (t>32) # table constraint
) ENGINE=myisam;
SELECT * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def foo t0 CONSTRAINT_1 `t` > 32
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def foo t0 CONSTRAINT_1 Table `t` > 32
ALTER TABLE t0
ADD CONSTRAINT CHK_t0_t CHECK(t<100);
SELECT * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def foo t0 CHK_t0_t `t` < 100
def foo t0 CONSTRAINT_1 `t` > 32
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def foo t0 CHK_t0_t Table `t` < 100
def foo t0 CONSTRAINT_1 Table `t` > 32
ALTER TABLE t0
DROP CONSTRAINT CHK_t0_t;
SELECT * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def foo t0 CONSTRAINT_1 `t` > 32
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def foo t0 CONSTRAINT_1 Table `t` > 32
ALTER TABLE t0
ADD CONSTRAINT CHECK(t<50);
SELECT * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def foo t0 CONSTRAINT_1 `t` > 32
def foo t0 CONSTRAINT_2 `t` < 50
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def foo t0 CONSTRAINT_1 Table `t` > 32
def foo t0 CONSTRAINT_2 Table `t` < 50
CREATE TABLE t1
( t int CHECK(t>2), # field constraint
tt int,
......@@ -42,22 +42,22 @@ CONSTRAINT CHECK (tt > 32), CONSTRAINT CHECK (tt <50),# autogenerated names tabl
CONSTRAINT CHK_tt CHECK(tt<100) # named table constraint
) ENGINE=InnoDB;
SELECT * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def foo t0 CONSTRAINT_1 `t` > 32
def foo t0 CONSTRAINT_2 `t` < 50
def foo t1 CHK_tt `tt` < 100
def foo t1 CONSTRAINT_1 `tt` > 32
def foo t1 CONSTRAINT_2 `tt` < 50
def foo t1 t `t` > 2
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def foo t0 CONSTRAINT_1 Table `t` > 32
def foo t0 CONSTRAINT_2 Table `t` < 50
def foo t1 CHK_tt Table `tt` < 100
def foo t1 CONSTRAINT_1 Table `tt` > 32
def foo t1 CONSTRAINT_2 Table `tt` < 50
def foo t1 t Column `t` > 2
ALTER TABLE t1
DROP CONSTRAINT CHK_tt;
SELECT * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def foo t0 CONSTRAINT_1 `t` > 32
def foo t0 CONSTRAINT_2 `t` < 50
def foo t1 CONSTRAINT_1 `tt` > 32
def foo t1 CONSTRAINT_2 `tt` < 50
def foo t1 t `t` > 2
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def foo t0 CONSTRAINT_1 Table `t` > 32
def foo t0 CONSTRAINT_2 Table `t` < 50
def foo t1 CONSTRAINT_1 Table `tt` > 32
def foo t1 CONSTRAINT_2 Table `tt` < 50
def foo t1 t Column `t` > 2
CREATE TABLE t2
(
name VARCHAR(30) CHECK(CHAR_LENGTH(name)>2), #field constraint
......@@ -66,26 +66,26 @@ end_date DATE,
CONSTRAINT CHK_dates CHECK(start_date IS NULL) #table constraint
)ENGINE=Innodb;
SELECT * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def foo t0 CONSTRAINT_1 `t` > 32
def foo t0 CONSTRAINT_2 `t` < 50
def foo t1 CONSTRAINT_1 `tt` > 32
def foo t1 CONSTRAINT_2 `tt` < 50
def foo t1 t `t` > 2
def foo t2 CHK_dates `start_date` is null
def foo t2 name char_length(`name`) > 2
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def foo t0 CONSTRAINT_1 Table `t` > 32
def foo t0 CONSTRAINT_2 Table `t` < 50
def foo t1 CONSTRAINT_1 Table `tt` > 32
def foo t1 CONSTRAINT_2 Table `tt` < 50
def foo t1 t Column `t` > 2
def foo t2 CHK_dates Table `start_date` is null
def foo t2 name Column char_length(`name`) > 2
ALTER TABLE t1
ADD CONSTRAINT CHK_new_ CHECK(t>tt);
SELECT * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def foo t0 CONSTRAINT_1 `t` > 32
def foo t0 CONSTRAINT_2 `t` < 50
def foo t1 CHK_new_ `t` > `tt`
def foo t1 CONSTRAINT_1 `tt` > 32
def foo t1 CONSTRAINT_2 `tt` < 50
def foo t1 t `t` > 2
def foo t2 CHK_dates `start_date` is null
def foo t2 name char_length(`name`) > 2
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def foo t0 CONSTRAINT_1 Table `t` > 32
def foo t0 CONSTRAINT_2 Table `t` < 50
def foo t1 CHK_new_ Table `t` > `tt`
def foo t1 CONSTRAINT_1 Table `tt` > 32
def foo t1 CONSTRAINT_2 Table `tt` < 50
def foo t1 t Column `t` > 2
def foo t2 CHK_dates Table `start_date` is null
def foo t2 name Column char_length(`name`) > 2
CREATE TABLE t3
(
a int,
......@@ -95,22 +95,22 @@ CONSTRAINT b check (b>10), # table constraint
CONSTRAINT b1 check (b<123456789012345678901234567890123456789012345678901234567890123456789)
) ENGINE=InnoDB;
SELECT * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def foo t0 CONSTRAINT_1 `t` > 32
def foo t0 CONSTRAINT_2 `t` < 50
def foo t1 CHK_new_ `t` > `tt`
def foo t1 CONSTRAINT_1 `tt` > 32
def foo t1 CONSTRAINT_2 `tt` < 50
def foo t1 t `t` > 2
def foo t2 CHK_dates `start_date` is null
def foo t2 name char_length(`name`) > 2
def foo t3 b `b` > 0
def foo t3 b `b` > 10
def foo t3 b1 `b` < 123456789012345678901234567890123456789012345678901234567890123456789
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def foo t0 CONSTRAINT_1 Table `t` > 32
def foo t0 CONSTRAINT_2 Table `t` < 50
def foo t1 CHK_new_ Table `t` > `tt`
def foo t1 CONSTRAINT_1 Table `tt` > 32
def foo t1 CONSTRAINT_2 Table `tt` < 50
def foo t1 t Column `t` > 2
def foo t2 CHK_dates Table `start_date` is null
def foo t2 name Column char_length(`name`) > 2
def foo t3 b Column `b` > 0
def foo t3 b Table `b` > 10
def foo t3 b1 Table `b` < 123456789012345678901234567890123456789012345678901234567890123456789
disconnect con1;
CONNECT con2, localhost, boo2,, test;
SELECT * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
disconnect con2;
CONNECT con1, localhost, boo1,,foo;
DROP TABLE t0;
......@@ -136,16 +136,54 @@ Grants for foo@%
GRANT USAGE ON *.* TO `foo`@`%`
GRANT SELECT (a) ON `db`.`t1` TO `foo`@`%`
SELECT * FROM information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def db t1 CONSTRAINT_1 `b` > 0
def mysql global_priv Priv json_valid(`Priv`)
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def db t1 CONSTRAINT_1 Table `b` > 0
def mysql global_priv Priv Column json_valid(`Priv`)
CONNECT con1,localhost, foo,, db;
SELECT a FROM t1;
a
1
2
SELECT * FROM information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
connection default;
DROP USER foo;
DROP DATABASE db;
#
# MDEV-24601: INFORMATION_SCHEMA doesn't differentiate between
# column and table-level CHECK constraints
#
use test;
create table t(check (t0>0),
t0 int,
t1 int check (t1<0),
t2 int check (t2<-1),
CONSTRAINT tc_1 check(t1 > 1),
CONSTRAINT t2 check(t2 > 1));
show create table t;
Table Create Table
t CREATE TABLE `t` (
`t0` int(11) DEFAULT NULL,
`t1` int(11) DEFAULT NULL CHECK (`t1` < 0),
`t2` int(11) DEFAULT NULL CHECK (`t2` < -1),
CONSTRAINT `CONSTRAINT_1` CHECK (`t0` > 0),
CONSTRAINT `tc_1` CHECK (`t1` > 1),
CONSTRAINT `t2` CHECK (`t2` > 1)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
select * from information_schema.table_constraints where CONSTRAINT_TYPE='CHECK';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE
def mysql Priv mysql global_priv CHECK
def test CONSTRAINT_1 test t CHECK
def test t1 test t CHECK
def test t2 test t CHECK
def test t2 test t CHECK
def test tc_1 test t CHECK
select * from information_schema.check_constraints;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME LEVEL CHECK_CLAUSE
def mysql global_priv Priv Column json_valid(`Priv`)
def test t CONSTRAINT_1 Table `t0` > 0
def test t t1 Column `t1` < 0
def test t t2 Column `t2` < -1
def test t t2 Table `t2` > 1
def test t tc_1 Table `t1` > 1
drop table t;
......@@ -24,10 +24,11 @@ def information_schema CHARACTER_SETS CHARACTER_SET_NAME 1 '' NO varchar 32 96 N
def information_schema CHARACTER_SETS DEFAULT_COLLATE_NAME 2 '' NO varchar 32 96 NULL NULL NULL utf8 utf8_general_ci varchar(32) select NEVER NULL
def information_schema CHARACTER_SETS DESCRIPTION 3 '' NO varchar 60 180 NULL NULL NULL utf8 utf8_general_ci varchar(60) select NEVER NULL
def information_schema CHARACTER_SETS MAXLEN 4 0 NO bigint NULL NULL 19 0 NULL NULL NULL bigint(3) select NEVER NULL
def information_schema CHECK_CONSTRAINTS CHECK_CLAUSE 5 '' NO longtext 4294967295 4294967295 NULL NULL NULL utf8 utf8_general_ci longtext select NEVER NULL
def information_schema CHECK_CONSTRAINTS CHECK_CLAUSE 6 '' NO longtext 4294967295 4294967295 NULL NULL NULL utf8 utf8_general_ci longtext select NEVER NULL
def information_schema CHECK_CONSTRAINTS CONSTRAINT_CATALOG 1 '' NO varchar 512 1536 NULL NULL NULL utf8 utf8_general_ci varchar(512) select NEVER NULL
def information_schema CHECK_CONSTRAINTS CONSTRAINT_NAME 4 '' NO varchar 64 192 NULL NULL NULL utf8 utf8_general_ci varchar(64) select NEVER NULL
def information_schema CHECK_CONSTRAINTS CONSTRAINT_SCHEMA 2 '' NO varchar 64 192 NULL NULL NULL utf8 utf8_general_ci varchar(64) select NEVER NULL
def information_schema CHECK_CONSTRAINTS LEVEL 5 '' NO varchar 6 18 NULL NULL NULL utf8 utf8_general_ci varchar(6) select NEVER NULL
def information_schema CHECK_CONSTRAINTS TABLE_NAME 3 '' NO varchar 64 192 NULL NULL NULL utf8 utf8_general_ci varchar(64) select NEVER NULL
def information_schema CLIENT_STATISTICS ACCESS_DENIED 22 0 NO bigint NULL NULL 19 0 NULL NULL NULL bigint(21) select NEVER NULL
def information_schema CLIENT_STATISTICS BINLOG_BYTES_WRITTEN 9 0 NO bigint NULL NULL 19 0 NULL NULL NULL bigint(21) select NEVER NULL
......@@ -572,6 +573,7 @@ NULL information_schema CHARACTER_SETS MAXLEN bigint NULL NULL NULL NULL bigint(
3.0000 information_schema CHECK_CONSTRAINTS CONSTRAINT_SCHEMA varchar 64 192 utf8 utf8_general_ci varchar(64)
3.0000 information_schema CHECK_CONSTRAINTS TABLE_NAME varchar 64 192 utf8 utf8_general_ci varchar(64)
3.0000 information_schema CHECK_CONSTRAINTS CONSTRAINT_NAME varchar 64 192 utf8 utf8_general_ci varchar(64)
3.0000 information_schema CHECK_CONSTRAINTS LEVEL varchar 6 18 utf8 utf8_general_ci varchar(6)
1.0000 information_schema CHECK_CONSTRAINTS CHECK_CLAUSE longtext 4294967295 4294967295 utf8 utf8_general_ci longtext
3.0000 information_schema CLIENT_STATISTICS CLIENT varchar 64 192 utf8 utf8_general_ci varchar(64)
NULL information_schema CLIENT_STATISTICS TOTAL_CONNECTIONS bigint NULL NULL NULL NULL bigint(21)
......
......@@ -24,10 +24,11 @@ def information_schema CHARACTER_SETS CHARACTER_SET_NAME 1 '' NO varchar 32 96 N
def information_schema CHARACTER_SETS DEFAULT_COLLATE_NAME 2 '' NO varchar 32 96 NULL NULL NULL utf8 utf8_general_ci varchar(32) NEVER NULL
def information_schema CHARACTER_SETS DESCRIPTION 3 '' NO varchar 60 180 NULL NULL NULL utf8 utf8_general_ci varchar(60) NEVER NULL
def information_schema CHARACTER_SETS MAXLEN 4 0 NO bigint NULL NULL 19 0 NULL NULL NULL bigint(3) NEVER NULL
def information_schema CHECK_CONSTRAINTS CHECK_CLAUSE 5 '' NO longtext 4294967295 4294967295 NULL NULL NULL utf8 utf8_general_ci longtext NEVER NULL
def information_schema CHECK_CONSTRAINTS CHECK_CLAUSE 6 '' NO longtext 4294967295 4294967295 NULL NULL NULL utf8 utf8_general_ci longtext NEVER NULL
def information_schema CHECK_CONSTRAINTS CONSTRAINT_CATALOG 1 '' NO varchar 512 1536 NULL NULL NULL utf8 utf8_general_ci varchar(512) NEVER NULL
def information_schema CHECK_CONSTRAINTS CONSTRAINT_NAME 4 '' NO varchar 64 192 NULL NULL NULL utf8 utf8_general_ci varchar(64) NEVER NULL
def information_schema CHECK_CONSTRAINTS CONSTRAINT_SCHEMA 2 '' NO varchar 64 192 NULL NULL NULL utf8 utf8_general_ci varchar(64) NEVER NULL
def information_schema CHECK_CONSTRAINTS LEVEL 5 '' NO varchar 6 18 NULL NULL NULL utf8 utf8_general_ci varchar(6) NEVER NULL
def information_schema CHECK_CONSTRAINTS TABLE_NAME 3 '' NO varchar 64 192 NULL NULL NULL utf8 utf8_general_ci varchar(64) NEVER NULL
def information_schema CLIENT_STATISTICS ACCESS_DENIED 22 0 NO bigint NULL NULL 19 0 NULL NULL NULL bigint(21) NEVER NULL
def information_schema CLIENT_STATISTICS BINLOG_BYTES_WRITTEN 9 0 NO bigint NULL NULL 19 0 NULL NULL NULL bigint(21) NEVER NULL
......@@ -572,6 +573,7 @@ NULL information_schema CHARACTER_SETS MAXLEN bigint NULL NULL NULL NULL bigint(
3.0000 information_schema CHECK_CONSTRAINTS CONSTRAINT_SCHEMA varchar 64 192 utf8 utf8_general_ci varchar(64)
3.0000 information_schema CHECK_CONSTRAINTS TABLE_NAME varchar 64 192 utf8 utf8_general_ci varchar(64)
3.0000 information_schema CHECK_CONSTRAINTS CONSTRAINT_NAME varchar 64 192 utf8 utf8_general_ci varchar(64)
3.0000 information_schema CHECK_CONSTRAINTS LEVEL varchar 6 18 utf8 utf8_general_ci varchar(6)
1.0000 information_schema CHECK_CONSTRAINTS CHECK_CLAUSE longtext 4294967295 4294967295 utf8 utf8_general_ci longtext
3.0000 information_schema CLIENT_STATISTICS CLIENT varchar 64 192 utf8 utf8_general_ci varchar(64)
NULL information_schema CLIENT_STATISTICS TOTAL_CONNECTIONS bigint NULL NULL NULL NULL bigint(21)
......
......@@ -118,3 +118,24 @@ SELECT * FROM information_schema.check_constraints;
DROP USER foo;
DROP DATABASE db;
--echo #
--echo # MDEV-24601: INFORMATION_SCHEMA doesn't differentiate between
--echo # column and table-level CHECK constraints
--echo #
# Mix of table (>0) and field (<0) constraints
# Note that there are 2 constraints `t2` - this is not allowed MDEV-24601
use test;
create table t(check (t0>0),
t0 int,
t1 int check (t1<0),
t2 int check (t2<-1),
CONSTRAINT tc_1 check(t1 > 1),
CONSTRAINT t2 check(t2 > 1));
show create table t;
--sorted_result
select * from information_schema.table_constraints where CONSTRAINT_TYPE='CHECK';
--sorted_result
select * from information_schema.check_constraints;
drop table t;
......@@ -6932,10 +6932,14 @@ static int get_check_constraints_record(THD *thd, TABLE_LIST *tables,
table->field[0]->store(STRING_WITH_LEN("def"), system_charset_info);
table->field[3]->store(check->name.str, check->name.length,
system_charset_info);
const char *tmp_buff;
tmp_buff= (check->get_vcol_type() == VCOL_CHECK_FIELD ?
"Column" : "Table");
table->field[4]->store(tmp_buff, strlen(tmp_buff), system_charset_info);
/* Make sure the string is empty between each print. */
str.length(0);
check->print(&str);
table->field[4]->store(str.ptr(), str.length(), system_charset_info);
table->field[5]->store(str.ptr(), str.length(), system_charset_info);
if (schema_table_store_record(thd, table))
DBUG_RETURN(1);
}
......@@ -9508,6 +9512,7 @@ ST_FIELD_INFO check_constraints_fields_info[]=
Column("CONSTRAINT_SCHEMA", Name(), NOT_NULL, OPEN_FULL_TABLE),
Column("TABLE_NAME", Name(), NOT_NULL, OPEN_FULL_TABLE),
Column("CONSTRAINT_NAME", Name(), NOT_NULL, OPEN_FULL_TABLE),
Column("LEVEL", Varchar(6),NOT_NULL, OPEN_FULL_TABLE),
Column("CHECK_CLAUSE", Longtext(MAX_FIELD_VARCHARLENGTH),
NOT_NULL, OPEN_FULL_TABLE),
CEnd()
......@@ -9524,7 +9529,7 @@ extern ST_FIELD_INFO optimizer_trace_info[];
} //namespace Show
/*
Description of ST_FIELD_INFO in table.h
Description of ST_FIELD_INFO in sql_i_s.h
Make sure that the order of schema_tables and enum_schema_tables are the same.
......
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