developers
Threads by month
- ----- 2025 -----
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- 4 participants
- 6856 discussions

Re: [Maria-developers] 4f070b59005: 29 Jan, MDEV-371 Unique indexes for blobs
by Sergei Golubchik 12 Feb '19
by Sergei Golubchik 12 Feb '19
12 Feb '19
Hi, Sachin!
That's generally pretty good.
But I still don't understand what you store in frm and how you
manipulate TABLE and TABLE_SHARE key lists.
Could you please explain that?
On Jan 29, Sachin Setiya wrote:
> diff --git a/mysql-test/main/long_unique.result b/mysql-test/main/long_unique.result
> new file mode 100644
> --- /dev/null
> +++ b/mysql-test/main/long_unique.result
> @@ -0,0 +1,1392 @@
> +#Structure of tests
> +#First we will check all option for
> +#table containing single unique column
> +#table containing keys like unique(a,b,c,d) etc
> +#then table containing 2 blob unique etc
> +set @allowed_packet= @@max_allowed_packet;
> +#table with single long blob column;
> +create table t1(a blob unique );
> +insert into t1 values(1),(2),(3),(56),('sachin'),('maria'),(123456789034567891),(null),(null),(123456789034567890);
> +#blob with primary key not allowed
> +create table t2(a blob,primary key(a(10000)));
> +ERROR 42000: Specified key was too long; max key length is 1000 bytes
> +create table t3(a varchar(10000) primary key);
> +ERROR 42000: Specified key was too long; max key length is 1000 bytes
> +insert into t1 values(2);
> +ERROR 23000: Duplicate entry '2' for key 'a'
> +#table structure;
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob YES UNI NULL
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + UNIQUE KEY `a` (`a`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table t1
> +Non_unique 0
> +Key_name a
> +Seq_in_index 1
> +Column_name a
> +Collation A
> +Cardinality NULL
> +Sub_part NULL
> +Packed NULL
> +Null YES
> +Index_type HASH
Good.
But this also mean that HASH is a valid index type, and that one
can write
CREATE TABLE t1 (a blob, unique (a) USING HASH)
generally, this should work. And one can write
CREATE TABLE t1 (a blob, unique (a) USING BTREE)
and this should fail, "key too long; max key length is 1000 bytes"
Note, the syntax is already possible. You just need to handle
these cases correctly in mysql_prepare_create_table().
And if neither HASH nor BTREE is specified, it's auto-detection,
basically. For short keys it's BTREE, for long keys it's HASH.
This is already implemented by this patch :)
> +Comment
> +Index_comment
> +
> +MyISAM file: DATADIR/test/t1
> +Record format: Packed
> +Character set: latin1_swedish_ci (8)
> +Data records: 10 Deleted blocks: 0
> +Recordlength: 20
> +
> +table description:
> +Key Start Len Index Type
> +1 12 8 multip. ulonglong NULL
> +select * from information_schema.columns where table_schema = 'test' and table_name = 't1';
> +TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION COLUMN_DEFAULT IS_NULLABLE DATA_TYPE CHARACTER_MAXIMUM_LENGTH CHARACTER_OCTET_LENGTH NUMERIC_PRECISION NUMERIC_SCALE DATETIME_PRECISION CHARACTER_SET_NAME COLLATION_NAME COLUMN_TYPE COLUMN_KEY EXTRA PRIVILEGES COLUMN_COMMENT IS_GENERATED GENERATION_EXPRESSION
> +def test t1 a 1 NULL YES blob 65535 65535 NULL NULL NULL NULL NULL blob UNI select,insert,update,references NEVER NULL
> +select * from information_schema.statistics where table_schema = 'test' and table_name = 't1';
> +TABLE_CATALOG TABLE_SCHEMA TABLE_NAME NON_UNIQUE INDEX_SCHEMA INDEX_NAME SEQ_IN_INDEX COLUMN_NAME COLLATION CARDINALITY SUB_PART PACKED NULLABLE INDEX_TYPE COMMENT INDEX_COMMENT
> +def test t1 0 test a 1 a A NULL NULL NULL YES HASH
> +select * from information_schema.key_column_usage where table_schema= 'test' and table_name= 't1';
> +CONSTRAINT_CATALOG def
> +CONSTRAINT_SCHEMA test
> +CONSTRAINT_NAME a
> +TABLE_CATALOG def
> +TABLE_SCHEMA test
> +TABLE_NAME t1
> +COLUMN_NAME a
> +ORDINAL_POSITION 1
> +POSITION_IN_UNIQUE_CONSTRAINT NULL
> +REFERENCED_TABLE_SCHEMA NULL
> +REFERENCED_TABLE_NAME NULL
> +REFERENCED_COLUMN_NAME NULL
> +# table select we should not be able to see db_row_hash_column;
> +select * from t1 order by a;
> +a
> +NULL
> +NULL
> +1
> +123456789034567890
> +123456789034567891
> +2
> +3
> +56
> +maria
> +sachin
> +select db_row_hash_1 from t1;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 'field list'
> +#duplicate entry test;
> +insert into t1 values(2);
> +ERROR 23000: Duplicate entry '2' for key 'a'
> +insert into t1 values('sachin');
> +ERROR 23000: Duplicate entry 'sachin' for key 'a'
> +insert into t1 values(123456789034567891);
> +ERROR 23000: Duplicate entry '123456789034567891' for key 'a'
> +select * from t1 order by a;
> +a
> +NULL
> +NULL
> +1
> +123456789034567890
> +123456789034567891
> +2
> +3
> +56
> +maria
> +sachin
> +insert into t1 values(11),(22),(33);
> +insert into t1 values(12),(22);
> +ERROR 23000: Duplicate entry '22' for key 'a'
> +select * from t1 order by a;
> +a
> +NULL
> +NULL
> +1
> +11
> +12
> +123456789034567890
> +123456789034567891
> +2
> +22
> +3
> +33
> +56
> +maria
> +sachin
> +insert into t1 values(repeat('s',4000*10)),(repeat('s',4001*10));
> +insert into t1 values(repeat('m',4000*10)),(repeat('m',4000*10));
> +ERROR 23000: Duplicate entry 'mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm' for key 'a'
> +insert into t1 values(repeat('m',4001)),(repeat('m',4002));
> +truncate table t1;
> +insert into t1 values(1),(2),(3),(4),(5),(8),(7);
> +
> +MyISAM file: DATADIR/test/t1
> +Record format: Packed
> +Character set: latin1_swedish_ci (8)
> +Data records: 7 Deleted blocks: 0
> +Recordlength: 20
> +
> +table description:
> +Key Start Len Index Type
> +1 12 8 multip. ulonglong NULL
> +#now some alter commands;
> +alter table t1 add column b int;
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob YES UNI NULL
> +b int(11) YES NULL
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` int(11) DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +insert into t1 values(1,2);
> +ERROR 23000: Duplicate entry '1' for key 'a'
> +insert into t1 values(2,2);
> +ERROR 23000: Duplicate entry '2' for key 'a'
> +select db_row_hash_1 from t1;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 'field list'
> +#now try to change db_row_hash_1 column;
> +alter table t1 drop column db_row_hash_1;
> +ERROR 42000: Can't DROP COLUMN `db_row_hash_1`; check that it exists
> +alter table t1 add column d int , add column e int , drop column db_row_hash_1;
> +ERROR 42000: Can't DROP COLUMN `db_row_hash_1`; check that it exists
> +alter table t1 modify column db_row_hash_1 int ;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 add column a int , add column b int, modify column db_row_hash_1 int ;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 change column db_row_hash_1 dsds int;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 add column asd int, change column db_row_hash_1 dsds int;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 drop column b , add column c int;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `c` int(11) DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +#now add some column with name db_row_hash;
> +alter table t1 add column db_row_hash_1 int unique;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `c` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535)),
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +insert into t1 values(45,1,55),(46,1,55);
> +ERROR 23000: Duplicate entry '55' for key 'db_row_hash_1'
> +insert into t1 values(45,1,55),(45,1,55);
> +ERROR 23000: Duplicate entry '45' for key 'a'
> +alter table t1 add column db_row_hash_2 int, add column db_row_hash_3 int;
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob YES UNI NULL
> +c int(11) YES NULL
> +db_row_hash_1 int(11) YES UNI NULL
> +db_row_hash_2 int(11) YES NULL
> +db_row_hash_3 int(11) YES NULL
> +#this should also drop the unique index ;
> +alter table t1 drop column a;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `c` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_3` int(11) DEFAULT NULL,
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 db_row_hash_1 1 db_row_hash_1 A NULL NULL NULL YES BTREE
> +#add column with unique index on blob ;
> +alter table t1 add column a blob unique;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `c` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_3` int(11) DEFAULT NULL,
> + `a` blob DEFAULT NULL,
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `a` (`a`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +# try to change the blob unique name;
> +#this will change index to b tree;
> +alter table t1 change column a aa blob ;
eh, the comment is wrong. Your old code
changed the column to int, that was indeed changing the
index to a btree. But now you simply rename the blob.
the index is not changed to btree anymore.
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `c` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_3` int(11) DEFAULT NULL,
> + `aa` blob DEFAULT NULL,
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `a` (`aa`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 db_row_hash_1 1 db_row_hash_1 A NULL NULL NULL YES BTREE
> +t1 0 a 1 aa A NULL NULL NULL YES HASH
> +# try to change the blob unique datatype;
> +#this will change index to b tree;
> +alter table t1 modify column aa int ;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `c` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_3` int(11) DEFAULT NULL,
> + `aa` int(11) DEFAULT NULL,
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `a` (`aa`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 db_row_hash_1 1 db_row_hash_1 A NULL NULL NULL YES BTREE
> +t1 0 a 1 aa A NULL NULL NULL YES BTREE
> +alter table t1 add column clm blob unique;
> +#try changing the name ;
> +alter table t1 change column clm clm_changed blob;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `c` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_3` int(11) DEFAULT NULL,
> + `aa` int(11) DEFAULT NULL,
> + `clm_changed` blob DEFAULT NULL,
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `a` (`aa`),
> + UNIQUE KEY `clm` (`clm_changed`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 db_row_hash_1 1 db_row_hash_1 A NULL NULL NULL YES BTREE
> +t1 0 a 1 aa A NULL NULL NULL YES BTREE
> +t1 0 clm 1 clm_changed A NULL NULL NULL YES HASH
> +#now drop the unique key;
> +alter table t1 drop key clm;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `c` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_3` int(11) DEFAULT NULL,
> + `aa` int(11) DEFAULT NULL,
> + `clm_changed` blob DEFAULT NULL,
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `a` (`aa`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 db_row_hash_1 1 db_row_hash_1 A NULL NULL NULL YES BTREE
> +t1 0 a 1 aa A NULL NULL NULL YES BTREE
> +drop table t1;
> +create table t1 (a TEXT CHARSET latin1 COLLATE latin1_german2_ci unique);
> +desc t1;
> +Field Type Null Key Default Extra
> +a text YES UNI NULL
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL NULL NULL YES HASH
> +insert into t1 values ('ae');
> +insert into t1 values ('AE');
> +ERROR 23000: Duplicate entry 'AE' for key 'a'
> +insert into t1 values ('Ä');
> +drop table t1;
> +create table t1 (a int primary key, b blob unique);
> +desc t1;
> +Field Type Null Key Default Extra
> +a int(11) NO PRI NULL
> +b blob YES UNI NULL
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 PRIMARY 1 a A 0 NULL NULL BTREE
> +t1 0 b 1 b A NULL NULL NULL YES HASH
> +insert into t1 values(1,1),(2,2),(3,3);
> +insert into t1 values(1,1);
> +ERROR 23000: Duplicate entry '1' for key 'b'
> +insert into t1 values(7,1);
> +ERROR 23000: Duplicate entry '1' for key 'b'
> +drop table t1;
> +#table with multiple long blob column and varchar text column ;
> +create table t1(a blob unique, b int , c blob unique , d text unique , e varchar(3000) unique);
> +insert into t1 values(1,2,3,4,5),(2,11,22,33,44),(3111,222,333,444,555),(5611,2222,3333,4444,5555),
> +('sachin',341,'fdf','gfgfgfg','hghgr'),('maria',345,'frter','dasd','utyuty'),
> +(123456789034567891,353534,53453453453456,64565464564564,45435345345345),
> +(123456789034567890,43545,657567567567,78967657567567,657567567567567676);
> +#table structure;
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob YES UNI NULL
> +b int(11) YES NULL
> +c blob YES UNI NULL
> +d text YES UNI NULL
> +e varchar(3000) YES UNI NULL
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` int(11) DEFAULT NULL,
> + `c` blob DEFAULT NULL,
> + `d` text DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + UNIQUE KEY `a` (`a`),
> + UNIQUE KEY `c` (`c`),
> + UNIQUE KEY `d` (`d`),
> + UNIQUE KEY `e` (`e`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL NULL NULL YES HASH
> +t1 0 c 1 c A NULL NULL NULL YES HASH
> +t1 0 d 1 d A NULL NULL NULL YES HASH
> +t1 0 e 1 e A NULL NULL NULL YES HASH
> +
> +MyISAM file: DATADIR/test/t1
> +Record format: Packed
> +Character set: latin1_swedish_ci (8)
> +Data records: 8 Deleted blocks: 0
> +Recordlength: 3072
> +
> +table description:
> +Key Start Len Index Type
> +1 3063 8 multip. ulonglong NULL
> +2 3055 8 multip. ulonglong NULL
> +3 3047 8 multip. ulonglong NULL
> +4 3039 8 multip. ulonglong NULL
> +select * from information_schema.columns where table_schema = 'test' and table_name = 't1';
> +TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION COLUMN_DEFAULT IS_NULLABLE DATA_TYPE CHARACTER_MAXIMUM_LENGTH CHARACTER_OCTET_LENGTH NUMERIC_PRECISION NUMERIC_SCALE DATETIME_PRECISION CHARACTER_SET_NAME COLLATION_NAME COLUMN_TYPE COLUMN_KEY EXTRA PRIVILEGES COLUMN_COMMENT IS_GENERATED GENERATION_EXPRESSION
> +def test t1 a 1 NULL YES blob 65535 65535 NULL NULL NULL NULL NULL blob UNI select,insert,update,references NEVER NULL
> +def test t1 b 2 NULL YES int NULL NULL 10 0 NULL NULL NULL int(11) select,insert,update,references NEVER NULL
> +def test t1 c 3 NULL YES blob 65535 65535 NULL NULL NULL NULL NULL blob UNI select,insert,update,references NEVER NULL
> +def test t1 d 4 NULL YES text 65535 65535 NULL NULL NULL latin1 latin1_swedish_ci text UNI select,insert,update,references NEVER NULL
> +def test t1 e 5 NULL YES varchar 3000 3000 NULL NULL NULL latin1 latin1_swedish_ci varchar(3000) UNI select,insert,update,references NEVER NULL
> +select * from information_schema.statistics where table_schema = 'test' and table_name = 't1';
> +TABLE_CATALOG TABLE_SCHEMA TABLE_NAME NON_UNIQUE INDEX_SCHEMA INDEX_NAME SEQ_IN_INDEX COLUMN_NAME COLLATION CARDINALITY SUB_PART PACKED NULLABLE INDEX_TYPE COMMENT INDEX_COMMENT
> +def test t1 0 test a 1 a A NULL NULL NULL YES HASH
> +def test t1 0 test c 1 c A NULL NULL NULL YES HASH
> +def test t1 0 test d 1 d A NULL NULL NULL YES HASH
> +def test t1 0 test e 1 e A NULL NULL NULL YES HASH
> +select * from information_schema.key_column_usage where table_schema= 'test' and table_name= 't1';
> +CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION POSITION_IN_UNIQUE_CONSTRAINT REFERENCED_TABLE_SCHEMA REFERENCED_TABLE_NAME REFERENCED_COLUMN_NAME
> +def test a def test t1 a 1 NULL NULL NULL NULL
> +def test c def test t1 c 1 NULL NULL NULL NULL
> +def test d def test t1 d 1 NULL NULL NULL NULL
> +def test e def test t1 e 1 NULL NULL NULL NULL
> +#table select we should not be able to see db_row_hash_1 column;
> +select * from t1 order by a;
> +a b c d e
> +1 2 3 4 5
> +123456789034567890 43545 657567567567 78967657567567 657567567567567676
> +123456789034567891 353534 53453453453456 64565464564564 45435345345345
> +2 11 22 33 44
> +3111 222 333 444 555
> +5611 2222 3333 4444 5555
> +maria 345 frter dasd utyuty
> +sachin 341 fdf gfgfgfg hghgr
> +select db_row_hash_1 from t1;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 'field list'
> +select db_row_hash_2 from t1;
> +ERROR 42S22: Unknown column 'db_row_hash_2' in 'field list'
> +select db_row_hash_3 from t1;
> +ERROR 42S22: Unknown column 'db_row_hash_3' in 'field list'
> +#duplicate entry test;
> +insert into t1 values(21,2,3,42,51);
> +ERROR 23000: Duplicate entry '3' for key 'c'
> +insert into t1 values('sachin',null,null,null,null);
> +ERROR 23000: Duplicate entry 'sachin' for key 'a'
> +insert into t1 values(1234567890345671890,4353451,6575675675617,789676575675617,657567567567567676);
> +ERROR 23000: Duplicate entry '657567567567567676' for key 'e'
> +select * from t1 order by a;
> +a b c d e
> +1 2 3 4 5
> +123456789034567890 43545 657567567567 78967657567567 657567567567567676
> +123456789034567891 353534 53453453453456 64565464564564 45435345345345
> +2 11 22 33 44
> +3111 222 333 444 555
> +5611 2222 3333 4444 5555
> +maria 345 frter dasd utyuty
> +sachin 341 fdf gfgfgfg hghgr
> +insert into t1 values(repeat('s',4000*10),100,repeat('s',4000*10),repeat('s',4000*10),
> +repeat('s',400)),(repeat('s',4001*10),1000,repeat('s',4001*10),repeat('s',4001*10),
> +repeat('s',2995));
> +insert into t1 values(repeat('m',4000*11),10,repeat('s',4000*11),repeat('s',4000*11),repeat('s',2995));
> +ERROR 23000: Duplicate entry 'ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss' for key 'e'
> +truncate table t1;
> +insert into t1 values(1,2,3,4,5),(2,11,22,33,44),(3111,222,333,444,555),(5611,2222,3333,4444,5555);
> +#now some alter commands;
> +alter table t1 add column f int;
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob YES UNI NULL
> +b int(11) YES NULL
> +c blob YES UNI NULL
> +d text YES UNI NULL
> +e varchar(3000) YES UNI NULL
> +f int(11) YES NULL
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` int(11) DEFAULT NULL,
> + `c` blob DEFAULT NULL,
> + `d` text DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` int(11) DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535)),
> + UNIQUE KEY `c` (`c`(65535)),
> + UNIQUE KEY `d` (`d`(65535)),
> + UNIQUE KEY `e` (`e`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +#unique key should not break;
> +insert into t1 values(1,2,3,4,5,6);
> +ERROR 23000: Duplicate entry '1' for key 'a'
> +select db_row_hash_1 , db_row_hash_2, db_row_hash_3 from t1;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 'field list'
> +#now try to change db_row_hash_1 column;
> +alter table t1 drop column db_row_hash_1, drop column db_row_hash_2, drop column db_row_hash_3;
> +ERROR 42000: Can't DROP COLUMN `db_row_hash_1`; check that it exists
> +alter table t1 add column dg int , add column ef int , drop column db_row_hash_1;
> +ERROR 42000: Can't DROP COLUMN `db_row_hash_1`; check that it exists
> +alter table t1 modify column db_row_hash_1 int , modify column db_row_hash_2 int, modify column db_row_hash_3 int;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 add column ar int , add column rb int, modify column db_row_hash_1 int , modify column db_row_hash_3 int;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 change column db_row_hash_1 dsds int , change column db_row_hash_2 dfdf int , change column db_row_hash_3 gdfg int ;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 add column asd int, drop column a, change column db_row_hash_1 dsds int, change db_row_hash_3 fdfdfd int;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 drop column b , add column g int;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `c` blob DEFAULT NULL,
> + `d` text DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` int(11) DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535)),
> + UNIQUE KEY `c` (`c`(65535)),
> + UNIQUE KEY `d` (`d`(65535)),
> + UNIQUE KEY `e` (`e`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +#now add some column with name db_row_hash;
> +alter table t1 add column db_row_hash_1 int unique;
> +alter table t1 add column db_row_hash_2 int unique;
> +alter table t1 add column db_row_hash_3 int unique;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `c` blob DEFAULT NULL,
> + `d` text DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` int(11) DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_3` int(11) DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535)),
> + UNIQUE KEY `c` (`c`(65535)),
> + UNIQUE KEY `d` (`d`(65535)),
> + UNIQUE KEY `e` (`e`),
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `db_row_hash_2` (`db_row_hash_2`),
> + UNIQUE KEY `db_row_hash_3` (`db_row_hash_3`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +alter table t1 add column db_row_hash_7 int, add column db_row_hash_5 int , add column db_row_hash_4 int ;
> +alter table t1 drop column db_row_hash_7,drop column db_row_hash_3, drop column db_row_hash_4;
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob YES UNI NULL
> +c blob YES UNI NULL
> +d text YES UNI NULL
> +e varchar(3000) YES UNI NULL
> +f int(11) YES NULL
> +g int(11) YES NULL
> +db_row_hash_1 int(11) YES UNI NULL
> +db_row_hash_2 int(11) YES UNI NULL
> +db_row_hash_5 int(11) YES NULL
> +#this should not break anything;
> +insert into t1 values(1,2,3,4,5,6,23,5,6);
> +ERROR 23000: Duplicate entry '1' for key 'a'
> +#this should also drop the unique index;
> +alter table t1 drop column a, drop column c;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `d` text DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` int(11) DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_5` int(11) DEFAULT NULL,
> + UNIQUE KEY `d` (`d`(65535)),
> + UNIQUE KEY `e` (`e`),
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `db_row_hash_2` (`db_row_hash_2`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 d 1 d A NULL 65535 NULL YES HASH
> +t1 0 e 1 e A NULL NULL NULL YES HASH
> +t1 0 db_row_hash_1 1 db_row_hash_1 A NULL NULL NULL YES BTREE
> +t1 0 db_row_hash_2 1 db_row_hash_2 A NULL NULL NULL YES BTREE
> +#add column with unique index on blob;
> +alter table t1 add column a blob unique;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `d` text DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` int(11) DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_5` int(11) DEFAULT NULL,
> + `a` blob DEFAULT NULL,
> + UNIQUE KEY `d` (`d`(65535)),
> + UNIQUE KEY `e` (`e`),
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `db_row_hash_2` (`db_row_hash_2`),
> + UNIQUE KEY `a` (`a`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 d 1 d A NULL 65535 NULL YES HASH
> +t1 0 e 1 e A NULL NULL NULL YES HASH
> +t1 0 db_row_hash_1 1 db_row_hash_1 A NULL NULL NULL YES BTREE
> +t1 0 db_row_hash_2 1 db_row_hash_2 A NULL NULL NULL YES BTREE
> +t1 0 a 1 a A NULL NULL NULL YES HASH
> +#try to change the blob unique column name;
> +#this will change index to b tree;
> +alter table t1 modify column a int , modify column e int;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `d` text DEFAULT NULL,
> + `e` int(11) DEFAULT NULL,
> + `f` int(11) DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_5` int(11) DEFAULT NULL,
> + `a` int(11) DEFAULT NULL,
> + UNIQUE KEY `d` (`d`(65535)),
> + UNIQUE KEY `e` (`e`),
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `db_row_hash_2` (`db_row_hash_2`),
> + UNIQUE KEY `a` (`a`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 d 1 d A NULL 65535 NULL YES HASH
> +t1 0 e 1 e A NULL NULL NULL YES BTREE
> +t1 0 db_row_hash_1 1 db_row_hash_1 A NULL NULL NULL YES BTREE
> +t1 0 db_row_hash_2 1 db_row_hash_2 A NULL NULL NULL YES BTREE
> +t1 0 a 1 a A NULL NULL NULL YES BTREE
> +alter table t1 add column clm1 blob unique,add column clm2 blob unique;
> +#try changing the name;
> +alter table t1 change column clm1 clm_changed1 blob, change column clm2 clm_changed2 blob;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `d` text DEFAULT NULL,
> + `e` int(11) DEFAULT NULL,
> + `f` int(11) DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_5` int(11) DEFAULT NULL,
> + `a` int(11) DEFAULT NULL,
> + `clm_changed1` blob DEFAULT NULL,
> + `clm_changed2` blob DEFAULT NULL,
> + UNIQUE KEY `d` (`d`(65535)),
> + UNIQUE KEY `e` (`e`),
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `db_row_hash_2` (`db_row_hash_2`),
> + UNIQUE KEY `a` (`a`),
> + UNIQUE KEY `clm1` (`clm_changed1`),
> + UNIQUE KEY `clm2` (`clm_changed2`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 d 1 d A NULL 65535 NULL YES HASH
> +t1 0 e 1 e A NULL NULL NULL YES BTREE
> +t1 0 db_row_hash_1 1 db_row_hash_1 A NULL NULL NULL YES BTREE
> +t1 0 db_row_hash_2 1 db_row_hash_2 A NULL NULL NULL YES BTREE
> +t1 0 a 1 a A NULL NULL NULL YES BTREE
> +t1 0 clm1 1 clm_changed1 A NULL NULL NULL YES HASH
> +t1 0 clm2 1 clm_changed2 A NULL NULL NULL YES HASH
> +#now drop the unique key;
> +alter table t1 drop key clm1, drop key clm2;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `d` text DEFAULT NULL,
> + `e` int(11) DEFAULT NULL,
> + `f` int(11) DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + `db_row_hash_5` int(11) DEFAULT NULL,
> + `a` int(11) DEFAULT NULL,
> + `clm_changed1` blob DEFAULT NULL,
> + `clm_changed2` blob DEFAULT NULL,
> + UNIQUE KEY `d` (`d`(65535)),
> + UNIQUE KEY `e` (`e`),
> + UNIQUE KEY `db_row_hash_1` (`db_row_hash_1`),
> + UNIQUE KEY `db_row_hash_2` (`db_row_hash_2`),
> + UNIQUE KEY `a` (`a`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 d 1 d A NULL 65535 NULL YES HASH
> +t1 0 e 1 e A NULL NULL NULL YES BTREE
> +t1 0 db_row_hash_1 1 db_row_hash_1 A NULL NULL NULL YES BTREE
> +t1 0 db_row_hash_2 1 db_row_hash_2 A NULL NULL NULL YES BTREE
> +t1 0 a 1 a A NULL NULL NULL YES BTREE
> +drop table t1;
> +#now the table with key on multiple columns; the ultimate test;
> +create table t1(a blob, b int , c varchar(2000) , d text , e varchar(3000) , f longblob , g int , h text ,
> +unique(a,b,c), unique(c,d,e),unique(e,f,g,h), unique(b,d,g,h));
> +insert into t1 values(1,1,1,1,1,1,1,1),(2,2,2,2,2,2,2,2),(3,3,3,3,3,3,3,3),(4,4,4,4,4,4,4,4),(5,5,5,5,5,5,5,5),
> +('maria',6,'maria','maria','maria','maria',6,'maria'),('mariadb',7,'mariadb','mariadb','mariadb','mariadb',8,'mariadb')
> +,(null,null,null,null,null,null,null,null),(null,null,null,null,null,null,null,null);
> +#table structure;
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob YES MUL NULL
> +b int(11) YES MUL NULL
> +c varchar(2000) YES MUL NULL
> +d text YES NULL
> +e varchar(3000) YES MUL NULL
> +f longblob YES NULL
> +g int(11) YES NULL
> +h text YES NULL
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` int(11) DEFAULT NULL,
> + `c` varchar(2000) DEFAULT NULL,
> + `d` text DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` longblob DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `h` text DEFAULT NULL,
> + UNIQUE KEY `a` (`a`,`b`,`c`),
> + UNIQUE KEY `c` (`c`,`d`,`e`),
> + UNIQUE KEY `e` (`e`,`f`,`g`,`h`),
> + UNIQUE KEY `b` (`b`,`d`,`g`,`h`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL NULL NULL YES HASH
> +t1 0 a 2 b A NULL NULL NULL YES HASH
> +t1 0 a 3 c A NULL NULL NULL YES HASH
> +t1 0 c 1 c A NULL NULL NULL YES HASH
> +t1 0 c 2 d A 0 NULL NULL YES HASH
> +t1 0 c 3 e A 0 NULL NULL YES HASH
> +t1 0 e 1 e A 0 NULL NULL YES HASH
> +t1 0 e 2 f A 0 NULL NULL YES HASH
> +t1 0 e 3 g A 0 NULL NULL YES HASH
> +t1 0 e 4 h A 0 NULL NULL YES HASH
> +t1 0 b 1 b A 0 NULL NULL YES HASH
> +t1 0 b 2 d A 0 NULL NULL YES HASH
> +t1 0 b 3 g A 0 NULL NULL YES HASH
> +t1 0 b 4 h A 0 NULL NULL YES HASH
> +
> +MyISAM file: DATADIR/test/t1
> +Record format: Packed
> +Character set: latin1_swedish_ci (8)
> +Data records: 9 Deleted blocks: 0
> +Recordlength: 5092
> +
> +table description:
> +Key Start Len Index Type
> +1 5081 8 multip. ulonglong NULL
> +2 5073 8 multip. ulonglong NULL
> +3 5065 8 multip. ulonglong NULL
> +4 5057 8 multip. ulonglong NULL
> +select * from information_schema.columns where table_schema = 'test' and table_name = 't1';
> +TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION COLUMN_DEFAULT IS_NULLABLE DATA_TYPE CHARACTER_MAXIMUM_LENGTH CHARACTER_OCTET_LENGTH NUMERIC_PRECISION NUMERIC_SCALE DATETIME_PRECISION CHARACTER_SET_NAME COLLATION_NAME COLUMN_TYPE COLUMN_KEY EXTRA PRIVILEGES COLUMN_COMMENT IS_GENERATED GENERATION_EXPRESSION
> +def test t1 a 1 NULL YES blob 65535 65535 NULL NULL NULL NULL NULL blob MUL select,insert,update,references NEVER NULL
> +def test t1 b 2 NULL YES int NULL NULL 10 0 NULL NULL NULL int(11) MUL select,insert,update,references NEVER NULL
> +def test t1 c 3 NULL YES varchar 2000 2000 NULL NULL NULL latin1 latin1_swedish_ci varchar(2000) MUL select,insert,update,references NEVER NULL
> +def test t1 d 4 NULL YES text 65535 65535 NULL NULL NULL latin1 latin1_swedish_ci text select,insert,update,references NEVER NULL
> +def test t1 e 5 NULL YES varchar 3000 3000 NULL NULL NULL latin1 latin1_swedish_ci varchar(3000) MUL select,insert,update,references NEVER NULL
> +def test t1 f 6 NULL YES longblob 4294967295 4294967295 NULL NULL NULL NULL NULL longblob select,insert,update,references NEVER NULL
> +def test t1 g 7 NULL YES int NULL NULL 10 0 NULL NULL NULL int(11) select,insert,update,references NEVER NULL
> +def test t1 h 8 NULL YES text 65535 65535 NULL NULL NULL latin1 latin1_swedish_ci text select,insert,update,references NEVER NULL
> +select * from information_schema.statistics where table_schema = 'test' and table_name = 't1';
> +TABLE_CATALOG TABLE_SCHEMA TABLE_NAME NON_UNIQUE INDEX_SCHEMA INDEX_NAME SEQ_IN_INDEX COLUMN_NAME COLLATION CARDINALITY SUB_PART PACKED NULLABLE INDEX_TYPE COMMENT INDEX_COMMENT
> +def test t1 0 test a 1 a A NULL NULL NULL YES HASH
> +def test t1 0 test a 2 b A NULL NULL NULL YES HASH
> +def test t1 0 test a 3 c A NULL NULL NULL YES HASH
> +def test t1 0 test c 1 c A NULL NULL NULL YES HASH
> +def test t1 0 test c 2 d A 0 NULL NULL YES HASH
> +def test t1 0 test c 3 e A 0 NULL NULL YES HASH
> +def test t1 0 test e 1 e A 0 NULL NULL YES HASH
> +def test t1 0 test e 2 f A 0 NULL NULL YES HASH
> +def test t1 0 test e 3 g A 0 NULL NULL YES HASH
> +def test t1 0 test e 4 h A 0 NULL NULL YES HASH
> +def test t1 0 test b 1 b A 0 NULL NULL YES HASH
> +def test t1 0 test b 2 d A 0 NULL NULL YES HASH
> +def test t1 0 test b 3 g A 0 NULL NULL YES HASH
> +def test t1 0 test b 4 h A 0 NULL NULL YES HASH
> +select * from information_schema.key_column_usage where table_schema= 'test' and table_name= 't1';
> +CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION POSITION_IN_UNIQUE_CONSTRAINT REFERENCED_TABLE_SCHEMA REFERENCED_TABLE_NAME REFERENCED_COLUMN_NAME
> +def test a def test t1 a 1 NULL NULL NULL NULL
> +def test a def test t1 b 2 NULL NULL NULL NULL
> +def test a def test t1 c 3 NULL NULL NULL NULL
> +def test c def test t1 c 1 NULL NULL NULL NULL
> +def test c def test t1 d 2 NULL NULL NULL NULL
> +def test c def test t1 e 3 NULL NULL NULL NULL
> +def test e def test t1 e 1 NULL NULL NULL NULL
> +def test e def test t1 f 2 NULL NULL NULL NULL
> +def test e def test t1 g 3 NULL NULL NULL NULL
> +def test e def test t1 h 4 NULL NULL NULL NULL
> +def test b def test t1 b 1 NULL NULL NULL NULL
> +def test b def test t1 d 2 NULL NULL NULL NULL
> +def test b def test t1 g 3 NULL NULL NULL NULL
> +def test b def test t1 h 4 NULL NULL NULL NULL
> +# table select we should not be able to see db_row_hash_1 column;
> +select * from t1 order by a;
> +a b c d e f g h
> +NULL NULL NULL NULL NULL NULL NULL NULL
> +NULL NULL NULL NULL NULL NULL NULL NULL
> +1 1 1 1 1 1 1 1
> +2 2 2 2 2 2 2 2
> +3 3 3 3 3 3 3 3
> +4 4 4 4 4 4 4 4
> +5 5 5 5 5 5 5 5
> +maria 6 maria maria maria maria 6 maria
> +mariadb 7 mariadb mariadb mariadb mariadb 8 mariadb
> +select db_row_hash_1 from t1;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 'field list'
> +select db_row_hash_2 from t1;
> +ERROR 42S22: Unknown column 'db_row_hash_2' in 'field list'
> +select db_row_hash_3 from t1;
> +ERROR 42S22: Unknown column 'db_row_hash_3' in 'field list'
> +#duplicate entry test;
> +#duplicate keys entry;
> +insert into t1 values(1,1,1,0,0,0,0,0);
> +ERROR 23000: Duplicate entry '1-1-1' for key 'a'
> +insert into t1 values(0,0,1,1,1,0,0,0);
> +ERROR 23000: Duplicate entry '1-1-1' for key 'c'
> +insert into t1 values(0,0,0,0,1,1,1,1);
> +ERROR 23000: Duplicate entry '1-1-1-1' for key 'e'
> +insert into t1 values(1,1,1,1,1,0,0,0);
> +ERROR 23000: Duplicate entry '1-1-1' for key 'a'
> +insert into t1 values(0,0,0,0,1,1,1,1);
> +ERROR 23000: Duplicate entry '1-1-1-1' for key 'e'
> +insert into t1 values(1,1,1,1,1,1,1,1);
> +ERROR 23000: Duplicate entry '1-1-1' for key 'a'
> +select db_row_hash_1,db_row_hash_2,db_row_hash_3,db_row_hash_4,db_row_hash_5 from t1;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 'field list'
> +alter table t1 drop column db_row_hash_1, drop column db_row_hash_2, drop column db_row_hash_3;
> +ERROR 42000: Can't DROP COLUMN `db_row_hash_1`; check that it exists
> +alter table t1 add column dg int , add column ef int , drop column db_row_hash_1;
> +ERROR 42000: Can't DROP COLUMN `db_row_hash_1`; check that it exists
> +alter table t1 modify column db_row_hash_1 int , modify column db_row_hash_2 int, modify column db_row_hash_3 int;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 add column ar int , add column rb int, modify column db_row_hash_1 int , modify column db_row_hash_3 int;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 change column db_row_hash_1 dsds int , change column db_row_hash_2 dfdf int , change column db_row_hash_3 gdfg int ;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +alter table t1 add column asd int, drop column a, change column db_row_hash_1 dsds int, change db_row_hash_3 fdfdfd int;
> +ERROR 42S22: Unknown column 'db_row_hash_1' in 't1'
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` int(11) DEFAULT NULL,
> + `c` varchar(2000) DEFAULT NULL,
> + `d` text DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` longblob DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `h` text DEFAULT NULL,
> + UNIQUE KEY `a` (`a`,`b`,`c`),
> + UNIQUE KEY `c` (`c`,`d`,`e`),
> + UNIQUE KEY `e` (`e`,`f`,`g`,`h`),
> + UNIQUE KEY `b` (`b`,`d`,`g`,`h`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +# add column named db_row_hash_*;
> +alter table t1 add column db_row_hash_7 int , add column db_row_hash_5 int,
> +add column db_row_hash_1 int, add column db_row_hash_2 int;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` int(11) DEFAULT NULL,
> + `c` varchar(2000) DEFAULT NULL,
> + `d` text DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` longblob DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `h` text DEFAULT NULL,
> + `db_row_hash_7` int(11) DEFAULT NULL,
> + `db_row_hash_5` int(11) DEFAULT NULL,
> + `db_row_hash_1` int(11) DEFAULT NULL,
> + `db_row_hash_2` int(11) DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535),`b`,`c`),
> + UNIQUE KEY `c` (`c`,`d`(65535),`e`),
> + UNIQUE KEY `e` (`e`,`f`(65535),`g`,`h`(65535)),
> + UNIQUE KEY `b` (`b`,`d`(65535),`g`,`h`(65535))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL 65535 NULL YES HASH
> +t1 0 a 2 b A NULL NULL NULL YES HASH
> +t1 0 a 3 c A NULL NULL NULL YES HASH
> +t1 0 c 1 c A NULL NULL NULL YES HASH
> +t1 0 c 2 d A 0 65535 NULL YES HASH
> +t1 0 c 3 e A 0 NULL NULL YES HASH
> +t1 0 e 1 e A 0 NULL NULL YES HASH
> +t1 0 e 2 f A 0 65535 NULL YES HASH
> +t1 0 e 3 g A 0 NULL NULL YES HASH
> +t1 0 e 4 h A 0 65535 NULL YES HASH
> +t1 0 b 1 b A 0 NULL NULL YES HASH
> +t1 0 b 2 d A 0 65535 NULL YES HASH
> +t1 0 b 3 g A 0 NULL NULL YES HASH
> +t1 0 b 4 h A 0 65535 NULL YES HASH
> +alter table t1 drop column db_row_hash_7 , drop column db_row_hash_5 ,
> +drop column db_row_hash_1, drop column db_row_hash_2 ;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` int(11) DEFAULT NULL,
> + `c` varchar(2000) DEFAULT NULL,
> + `d` text DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` longblob DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `h` text DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535),`b`,`c`),
> + UNIQUE KEY `c` (`c`,`d`(65535),`e`),
> + UNIQUE KEY `b` (`b`,`d`(65535),`g`,`h`(65535)),
> + UNIQUE KEY `e` (`e`,`f`(65535),`g`,`h`(65535))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL 65535 NULL YES HASH
> +t1 0 a 2 b A NULL NULL NULL YES HASH
> +t1 0 a 3 c A NULL NULL NULL YES HASH
> +t1 0 c 1 c A NULL NULL NULL YES HASH
> +t1 0 c 2 d A 0 65535 NULL YES HASH
> +t1 0 c 3 e A 0 NULL NULL YES HASH
> +t1 0 b 1 b A 0 NULL NULL YES HASH
> +t1 0 b 2 d A 0 65535 NULL YES HASH
> +t1 0 b 3 g A 0 NULL NULL YES HASH
> +t1 0 b 4 h A 0 65535 NULL YES HASH
> +t1 0 e 1 e A 0 NULL NULL YES HASH
> +t1 0 e 2 f A 0 65535 NULL YES HASH
> +t1 0 e 3 g A 0 NULL NULL YES HASH
> +t1 0 e 4 h A 0 65535 NULL YES HASH
> +#try to change column names;
> +alter table t1 change column a aa blob , change column b bb blob , change column d dd blob;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `aa` blob DEFAULT NULL,
> + `bb` blob DEFAULT NULL,
> + `c` varchar(2000) DEFAULT NULL,
> + `dd` blob DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` longblob DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `h` text DEFAULT NULL,
> + UNIQUE KEY `a` (`aa`(65535),`bb`,`c`),
> + UNIQUE KEY `c` (`c`,`dd`(65535),`e`),
> + UNIQUE KEY `b` (`bb`,`dd`(65535),`g`,`h`(65535)),
> + UNIQUE KEY `e` (`e`,`f`(65535),`g`,`h`(65535))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 aa A NULL 65535 NULL YES HASH
> +t1 0 a 2 bb A NULL NULL NULL YES HASH
> +t1 0 a 3 c A NULL NULL NULL YES HASH
> +t1 0 c 1 c A NULL NULL NULL YES HASH
> +t1 0 c 2 dd A 0 65535 NULL YES HASH
> +t1 0 c 3 e A 0 NULL NULL YES HASH
> +t1 0 b 1 bb A 0 NULL NULL YES HASH
> +t1 0 b 2 dd A 0 65535 NULL YES HASH
> +t1 0 b 3 g A 0 NULL NULL YES HASH
> +t1 0 b 4 h A 0 65535 NULL YES HASH
> +t1 0 e 1 e A 0 NULL NULL YES HASH
> +t1 0 e 2 f A 0 65535 NULL YES HASH
> +t1 0 e 3 g A 0 NULL NULL YES HASH
> +t1 0 e 4 h A 0 65535 NULL YES HASH
> +alter table t1 change column aa a blob , change column bb b blob , change column dd d blob;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` blob DEFAULT NULL,
> + `c` varchar(2000) DEFAULT NULL,
> + `d` blob DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` longblob DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `h` text DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535),`b`,`c`),
> + UNIQUE KEY `c` (`c`,`d`(65535),`e`),
> + UNIQUE KEY `b` (`b`,`d`(65535),`g`,`h`(65535)),
> + UNIQUE KEY `e` (`e`,`f`(65535),`g`,`h`(65535))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL 65535 NULL YES HASH
> +t1 0 a 2 b A NULL NULL NULL YES HASH
> +t1 0 a 3 c A NULL NULL NULL YES HASH
> +t1 0 c 1 c A NULL NULL NULL YES HASH
> +t1 0 c 2 d A 0 65535 NULL YES HASH
> +t1 0 c 3 e A 0 NULL NULL YES HASH
> +t1 0 b 1 b A 0 NULL NULL YES HASH
> +t1 0 b 2 d A 0 65535 NULL YES HASH
> +t1 0 b 3 g A 0 NULL NULL YES HASH
> +t1 0 b 4 h A 0 65535 NULL YES HASH
> +t1 0 e 1 e A 0 NULL NULL YES HASH
> +t1 0 e 2 f A 0 65535 NULL YES HASH
> +t1 0 e 3 g A 0 NULL NULL YES HASH
> +t1 0 e 4 h A 0 65535 NULL YES HASH
> +#now we will change the data type to int and varchar limit so that we no longer require hash_index;
> +#on key a_b_c;
> +alter table t1 modify column a varchar(20) , modify column b varchar(20) , modify column c varchar(20);
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` varchar(20) DEFAULT NULL,
> + `b` varchar(20) DEFAULT NULL,
> + `c` varchar(20) DEFAULT NULL,
> + `d` blob DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` longblob DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `h` text DEFAULT NULL,
> + UNIQUE KEY `a` (`a`,`b`,`c`),
> + UNIQUE KEY `c` (`c`,`d`(65535),`e`),
> + UNIQUE KEY `b` (`b`,`d`(65535),`g`,`h`(65535)),
> + UNIQUE KEY `e` (`e`,`f`(65535),`g`,`h`(65535))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL NULL NULL YES BTREE
> +t1 0 a 2 b A NULL NULL NULL YES BTREE
> +t1 0 a 3 c A NULL NULL NULL YES BTREE
> +t1 0 c 1 c A NULL NULL NULL YES HASH
> +t1 0 c 2 d A NULL 65535 NULL YES HASH
> +t1 0 c 3 e A NULL NULL NULL YES HASH
> +t1 0 b 1 b A 0 NULL NULL YES HASH
> +t1 0 b 2 d A 0 65535 NULL YES HASH
> +t1 0 b 3 g A 0 NULL NULL YES HASH
> +t1 0 b 4 h A 0 65535 NULL YES HASH
> +t1 0 e 1 e A 0 NULL NULL YES HASH
> +t1 0 e 2 f A 0 65535 NULL YES HASH
> +t1 0 e 3 g A 0 NULL NULL YES HASH
> +t1 0 e 4 h A 0 65535 NULL YES HASH
> +#change it back;
> +alter table t1 modify column a blob , modify column b blob , modify column c blob;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` blob DEFAULT NULL,
> + `c` blob DEFAULT NULL,
> + `d` blob DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` longblob DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `h` text DEFAULT NULL,
> + UNIQUE KEY `a` (`a`,`b`,`c`),
> + UNIQUE KEY `c` (`c`,`d`(65535),`e`),
> + UNIQUE KEY `b` (`b`,`d`(65535),`g`,`h`(65535)),
> + UNIQUE KEY `e` (`e`,`f`(65535),`g`,`h`(65535))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL NULL NULL YES HASH
> +t1 0 a 2 b A NULL NULL NULL YES HASH
> +t1 0 a 3 c A NULL NULL NULL YES HASH
> +t1 0 c 1 c A NULL NULL NULL YES HASH
> +t1 0 c 2 d A 0 65535 NULL YES HASH
> +t1 0 c 3 e A 0 NULL NULL YES HASH
> +t1 0 b 1 b A 0 NULL NULL YES HASH
> +t1 0 b 2 d A 0 65535 NULL YES HASH
> +t1 0 b 3 g A 0 NULL NULL YES HASH
> +t1 0 b 4 h A 0 65535 NULL YES HASH
> +t1 0 e 1 e A 0 NULL NULL YES HASH
> +t1 0 e 2 f A 0 65535 NULL YES HASH
> +t1 0 e 3 g A 0 NULL NULL YES HASH
> +t1 0 e 4 h A 0 65535 NULL YES HASH
> +#try to delete blob column in unique;
> +truncate table t1;
> +#now try to delete keys;
> +alter table t1 drop key c, drop key e;
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` blob DEFAULT NULL,
> + `c` blob DEFAULT NULL,
> + `d` blob DEFAULT NULL,
> + `e` varchar(3000) DEFAULT NULL,
> + `f` longblob DEFAULT NULL,
> + `g` int(11) DEFAULT NULL,
> + `h` text DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535),`b`(65535),`c`(65535)),
> + UNIQUE KEY `b` (`b`(65535),`d`(65535),`g`,`h`(65535))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL 65535 NULL YES HASH
> +t1 0 a 2 b A NULL 65535 NULL YES HASH
> +t1 0 a 3 c A 0 65535 NULL YES HASH
> +t1 0 b 1 b A 0 65535 NULL YES HASH
> +t1 0 b 2 d A 0 65535 NULL YES HASH
> +t1 0 b 3 g A 0 NULL NULL YES HASH
> +t1 0 b 4 h A 0 65535 NULL YES HASH
> +drop table t1;
> +#now alter table containing some data basically some tests with ignore;
> +create table t1 (a blob);
> +insert into t1 values(1),(2),(3);
> +#normal alter table;
> +alter table t1 add unique key(a);
> +alter table t1 drop key a;
> +truncate table t1;
> +insert into t1 values(1),(1),(2),(2),(3);
> +alter table t1 add unique key(a);
> +ERROR 23000: Duplicate entry '1' for key 'a'
> +alter ignore table t1 add unique key(a);
> +select * from t1 order by a;
> +a
> +1
> +2
> +3
> +insert into t1 values(1);
> +ERROR 23000: Duplicate entry '1' for key 'a'
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL 65535 NULL YES HASH
> +drop table t1;
> +#Now with multiple keys;
> +create table t1(a blob , b blob, c blob , d blob , e int);
> +insert into t1 values (1,1,1,1,1);
> +insert into t1 values (1,1,1,1,1);
> +insert into t1 values (2,1,1,1,1);
> +insert into t1 values (2,2,2,2,2);
> +insert into t1 values (3,3,4,4,4);
> +insert into t1 values (4,4,4,4,4);
> +alter table t1 add unique key(a,c), add unique key(b,d), add unique key(e);
> +ERROR 23000: Duplicate entry '1-1' for key 'a'
> +alter ignore table t1 add unique key(a,c), add unique key(b,d), add unique key(e);
> +select * from t1 order by a;
> +a b c d e
> +1 1 1 1 1
> +2 2 2 2 2
> +3 3 4 4 4
> +insert into t1 values (1,12,1,13,14);
> +ERROR 23000: Duplicate entry '1-1' for key 'a'
> +insert into t1 values (12,1,14,1,14);
> +ERROR 23000: Duplicate entry '1-1' for key 'b'
> +insert into t1 values (13,12,13,14,4);
> +ERROR 23000: Duplicate entry '4' for key 'e'
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + `b` blob DEFAULT NULL,
> + `c` blob DEFAULT NULL,
> + `d` blob DEFAULT NULL,
> + `e` int(11) DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(65535),`c`(65535)),
> + UNIQUE KEY `b` (`b`(65535),`d`(65535)),
> + UNIQUE KEY `e` (`e`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL 65535 NULL YES HASH
> +t1 0 a 2 c A NULL 65535 NULL YES HASH
> +t1 0 b 1 b A NULL 65535 NULL YES HASH
> +t1 0 b 2 d A 0 65535 NULL YES HASH
> +t1 0 e 1 e A 0 NULL NULL YES BTREE
> +drop table t1;
> +#visibility of db_row_hash
> +create table t1 (a blob unique , b blob unique);
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob YES UNI NULL
> +b blob YES UNI NULL
> +insert into t1 values(1,19);
> +insert into t1 values(2,29);
> +insert into t1 values(3,39);
> +insert into t1 values(4,49);
> +create table t2 (DB_ROW_HASH_1 int, DB_ROW_HASH_2 int);
> +insert into t2 values(11,1);
> +insert into t2 values(22,2);
> +insert into t2 values(33,3);
> +insert into t2 values(44,4);
> +select * from t1 order by a;
> +a b
> +1 19
> +2 29
> +3 39
> +4 49
> +select * from t2 order by DB_ROW_HASH_1;
> +DB_ROW_HASH_1 DB_ROW_HASH_2
> +11 1
> +22 2
> +33 3
> +44 4
> +select DB_ROW_HASH_1, DB_ROW_HASH_2 from t1;
> +ERROR 42S22: Unknown column 'DB_ROW_HASH_1' in 'field list'
> +#bug
> +select DB_ROW_HASH_1, DB_ROW_HASH_2 from t1,t2;
> +DB_ROW_HASH_1 DB_ROW_HASH_2
> +11 1
> +11 1
> +11 1
> +11 1
> +22 2
> +22 2
> +22 2
> +22 2
> +33 3
> +33 3
> +33 3
> +33 3
> +44 4
> +44 4
> +44 4
> +44 4
> +select * from t1 where DB_ROW_HASH_1 in (select DB_ROW_HASH_1 from t2);
> +ERROR 42S22: Unknown column 'DB_ROW_HASH_1' in 'IN/ALL/ANY subquery'
> +select DB_ROW_HASH_1, DB_ROW_HASH_2 from t1,t2 where DB_ROW_HASH_1 in (select DB_ROW_HASH_1 from t2);
> +DB_ROW_HASH_1 DB_ROW_HASH_2
> +11 1
> +22 2
> +33 3
> +44 4
> +11 1
> +22 2
> +33 3
> +44 4
> +11 1
> +22 2
> +33 3
> +44 4
> +11 1
> +22 2
> +33 3
> +44 4
> +select * from t2 where DB_ROW_HASH_1 in (select DB_ROW_HASH_1 from t1);
> +DB_ROW_HASH_1 DB_ROW_HASH_2
> +11 1
> +22 2
> +33 3
> +44 4
> +select DB_ROW_HASH_1 from t1,t2 where t1.DB_ROW_HASH_1 = t2.DB_ROW_HASH_2;
> +ERROR 42S22: Unknown column 't1.DB_ROW_HASH_1' in 'where clause'
> +select DB_ROW_HASH_1 from t1 inner join t2 on t1.a = t2.DB_ROW_HASH_2;
> +DB_ROW_HASH_1
> +11
> +22
> +33
> +44
> +drop table t1,t2;
> +#very long blob entry;
> +SET @@GLOBAL.max_allowed_packet=67108864;
> +connect 'newcon', localhost, root,,;
> +connection newcon;
> +show variables like 'max_allowed_packet';
> +Variable_name Value
> +max_allowed_packet 67108864
> +create table t1(a longblob unique, b longblob , c longblob , unique(b,c));
> +desc t1;
> +Field Type Null Key Default Extra
> +a longblob YES UNI NULL
> +b longblob YES MUL NULL
> +c longblob YES NULL
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` longblob DEFAULT NULL,
> + `b` longblob DEFAULT NULL,
> + `c` longblob DEFAULT NULL,
> + UNIQUE KEY `a` (`a`),
> + UNIQUE KEY `b` (`b`,`c`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL NULL NULL YES HASH
> +t1 0 b 1 b A NULL NULL NULL YES HASH
> +t1 0 b 2 c A 0 NULL NULL YES HASH
> +insert into t1 values(concat(repeat('sachin',10000000),'1'),concat(repeat('sachin',10000000),'1'),
> +concat(repeat('sachin',10000000),'1'));
> +insert into t1 values(concat(repeat('sachin',10000000),'2'),concat(repeat('sachin',10000000),'2'),
> +concat(repeat('sachin',10000000),'1'));
> +insert into t1 values(concat(repeat('sachin',10000000),'2'),concat(repeat('sachin',10000000),'2'),
> +concat(repeat('sachin',10000000),'4'));
> +ERROR 23000: Duplicate entry 'sachinsachinsachinsachinsachinsachinsachinsachinsachinsachinsach' for key 'a'
> +insert into t1 values(concat(repeat('sachin',10000000),'3'),concat(repeat('sachin',10000000),'1'),
> +concat(repeat('sachin',10000000),'1'));
> +ERROR 23000: Duplicate entry 'sachinsachinsachinsachinsachinsachinsachinsachinsachinsachinsach' for key 'b'
> +drop table t1;
> +#long key unique with different key length
> +create table t1(a blob, unique(a(3000)));
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob YES UNI NULL
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL 3000 NULL YES HASH
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(3000))
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +insert into t1 value(concat(repeat('s',3000),'1'));
> +insert into t1 value(concat(repeat('s',3000),'2'));
> +ERROR 23000: Duplicate entry 'ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss' for key 'a'
> +insert into t1 value(concat(repeat('a',3000),'2'));
> +drop table t1;
> +create table t1(a varchar(4000), b longblob , c varchar(5000), d longblob,
> +unique(a(3500), b), unique(c(4500), d));
> +desc t1;
> +Field Type Null Key Default Extra
> +a varchar(4000) YES MUL NULL
> +b longblob YES NULL
> +c varchar(5000) YES MUL NULL
> +d longblob YES NULL
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` varchar(4000) DEFAULT NULL,
> + `b` longblob DEFAULT NULL,
> + `c` varchar(5000) DEFAULT NULL,
> + `d` longblob DEFAULT NULL,
> + UNIQUE KEY `a` (`a`(3500),`b`),
> + UNIQUE KEY `c` (`c`(4500),`d`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +show keys from t1;
> +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
> +t1 0 a 1 a A NULL 3500 NULL YES HASH
> +t1 0 a 2 b A NULL NULL NULL YES HASH
> +t1 0 c 1 c A 0 4500 NULL YES HASH
> +t1 0 c 2 d A 0 NULL NULL YES HASH
> +drop table t1;
> +disconnect newcon;
> +connection default;
> +SET @@GLOBAL.max_allowed_packet=4194304;
> +#ext bug
> +create table t1(a int primary key, b blob unique, c int, d blob , index(c));
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` int(11) NOT NULL,
> + `b` blob DEFAULT NULL,
> + `c` int(11) DEFAULT NULL,
> + `d` blob DEFAULT NULL,
> + PRIMARY KEY (`a`),
> + UNIQUE KEY `b` (`b`),
> + KEY `c` (`c`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +insert into t1 values(1,23,1,33);
> +insert into t1 values(2,23,1,33);
> +ERROR 23000: Duplicate entry '23' for key 'b'
> +drop table t1;
> +create table t2 (a blob unique , c int , index(c));
> +show create table t2;
> +Table Create Table
> +t2 CREATE TABLE `t2` (
> + `a` blob DEFAULT NULL,
> + `c` int(11) DEFAULT NULL,
> + UNIQUE KEY `a` (`a`),
> + KEY `c` (`c`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +insert into t2 values(1,1);
> +insert into t2 values(2,1);
> +drop table t2;
> +#not null test //todo solve warnings
> +create table t1(a blob unique not null);
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob NO UNI NULL
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob NOT NULL,
> + UNIQUE KEY `a` (`a`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +insert into t1 values(1);
> +insert into t1 values(3);
> +insert into t1 values(1);
> +ERROR 23000: Duplicate entry '1' for key 'a'
> +drop table t1;
> +create table t1(a int primary key, b blob unique , c blob unique not null);
> +insert into t1 values(1,1,1);
> +insert into t1 values(2,1,2);
> +ERROR 23000: Duplicate entry '1' for key 'b'
> +insert into t1 values(3,3,1);
> +ERROR 23000: Duplicate entry '1' for key 'c'
> +drop table t1;
> +create table t1 (a blob unique not null, b blob not null, c blob not null, unique(b,c));
> +desc t1;
> +Field Type Null Key Default Extra
> +a blob NO UNI NULL
> +b blob NO MUL NULL
> +c blob NO NULL
> +show create table t1;
> +Table Create Table
> +t1 CREATE TABLE `t1` (
> + `a` blob NOT NULL,
> + `b` blob NOT NULL,
> + `c` blob NOT NULL,
> + UNIQUE KEY `a` (`a`),
> + UNIQUE KEY `b` (`b`,`c`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +insert into t1 values (1, 2, 3);
> +insert into t1 values (2, 1, 3);
> +insert into t1 values (2, 1, 3);
> +ERROR 23000: Duplicate entry '2' for key 'a'
> +drop table t1;
> +#partition
> +create table t1(a blob unique) partition by hash(a);
> +ERROR HY000: A BLOB field is not allowed in partition function
> +#key length > 2^15 -1
> +create table t1(a blob, unique(a(100001)));
> +ERROR 42000: Specified key was too long; max key length is 1000 bytes
> +set @@GLOBAL.max_allowed_packet= @allowed_packet;
> diff --git a/mysql-test/main/long_unique_debug.result b/mysql-test/main/long_unique_debug.result
> new file mode 100644
> --- /dev/null
> +++ b/mysql-test/main/long_unique_debug.result
> @@ -0,0 +1,32 @@
> +#In this test case we will check what will happen in the case of hash collusion
collision, not collusion :)
> +SET debug_dbug="d,same_long_unique_hash";
> +create table t1(a blob unique);
> +insert into t1 values('xyz');
> +insert into t1 values('abc');
> +insert into t1 values('sachin');
> +insert into t1 values('sachin');
> +ERROR 23000: Duplicate entry 'sachin' for key 'a'
> +insert into t1 values('maria');
> +insert into t1 values('maria');
> +ERROR 23000: Duplicate entry 'maria' for key 'a'
> +drop table t1;
> +create table t1(a blob unique, b blob unique);
> +insert into t1 values('xyz', 11);
> +insert into t1 values('abc', 22);
> +insert into t1 values('sachin', 1);
> +insert into t1 values('sachin', 4);
> +ERROR 23000: Duplicate entry 'sachin' for key 'a'
> +insert into t1 values('maria', 2);
> +insert into t1 values('maria', 3);
> +ERROR 23000: Duplicate entry 'maria' for key 'a'
> +drop table t1;
> +create table t1(a blob , b blob , unique(a,b));
> +insert into t1 values('xyz', 11);
> +insert into t1 values('abc', 22);
> +insert into t1 values('sachin', 1);
> +insert into t1 values('sachin', 1);
> +ERROR 23000: Duplicate entry 'sachin-1' for key 'a'
> +insert into t1 values('maria', 2);
> +insert into t1 values('maria', 2);
> +ERROR 23000: Duplicate entry 'maria-2' for key 'a'
> +drop table t1;
> diff --git a/mysql-test/main/mdev-504.test b/mysql-test/main/mdev-504.test
> --- a/mysql-test/main/mdev-504.test
> +++ b/mysql-test/main/mdev-504.test
> @@ -44,7 +43,7 @@ while ($trial)
> --connect (con2,localhost,root,,)
> --send CALL p_analyze()
>
> - --let $run = 100
> + --let $run = 1
Why?
>
> while ($run)
> {
> diff --git a/mysql-test/main/type_blob.result b/mysql-test/main/type_blob.result
> --- a/mysql-test/main/type_blob.result
> +++ b/mysql-test/main/type_blob.result
> @@ -370,7 +370,7 @@ a 1
> hello 1
> drop table t1;
> create table t1 (a text, unique (a(2100)));
> -ERROR 42000: Specified key was too long; max key length is 1000 bytes
> +drop table t1;
or remove the create table instead, it's not a useful test anymore
> create table t1 (a text, key (a(2100)));
> Warnings:
> Note 1071 Specified key was too long; max key length is 1000 bytes
> diff --git a/sql/field.h b/sql/field.h
> --- a/sql/field.h
> +++ b/sql/field.h
> @@ -687,7 +687,7 @@ class Field: public Value_source
> GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
> GEOM_GEOMETRYCOLLECTION = 7
> };
> - enum imagetype { itRAW, itMBR};
> + enum imagetype { itRAW, itMBR, itHASH};
unused
>
> utype unireg_check;
> uint32 field_length; // Length of field
> diff --git a/sql/handler.cc b/sql/handler.cc
> --- a/sql/handler.cc
> +++ b/sql/handler.cc
> @@ -3686,9 +3686,19 @@ void handler::print_error(int error, myf errflag)
> uint key_nr=get_dup_key(error);
> if ((int) key_nr >= 0 && key_nr < table->s->keys)
> {
> + KEY *long_key= NULL;
> + if (table->key_info[key_nr].algorithm
> + == HA_KEY_ALG_LONG_HASH)
> + {
> + long_key= table->key_info + key_nr;
> + setup_keyinfo_hash(long_key);
> + }
> print_keydup_error(table, &table->key_info[key_nr], errflag);
> + if (long_key)
> + re_setup_keyinfo_hash(long_key);
> DBUG_VOID_RETURN;
> }
> + table->re_setup_table();
> }
> textno=ER_DUP_KEY;
> break;
> @@ -6256,6 +6268,162 @@ int handler::ha_reset()
> DBUG_RETURN(reset());
> }
>
> +static int check_duplicate_long_entry_key(TABLE *table, handler *h, uchar *new_rec,
> + uint key_no)
> +{
> + Field *hash_field;
> + int result, error= 0;
> + KEY *key_info= table->key_info + key_no;
> + hash_field= key_info->key_part->field;
> + DBUG_ASSERT((table->key_info[key_no].flags & HA_NULL_PART_KEY &&
> + table->key_info[key_no].key_length == HA_HASH_KEY_LENGTH_WITH_NULL)
> + || table->key_info[key_no].key_length == HA_HASH_KEY_LENGTH_WITHOUT_NULL);
> + uchar ptr[HA_HASH_KEY_LENGTH_WITH_NULL];
> +
> + if (hash_field->is_real_null())
> + return 0;
> +
remember, you have `key_info= table->key_info + key_no` above :)
you don't need to write `table->key_info[key_no]` anymore here
> + key_copy(ptr, new_rec, &table->key_info[key_no],
> + table->key_info[key_no].key_length, false);
> +
> + if (!table->check_unique_buf)
> + table->check_unique_buf= (uchar *)alloc_root(&table->mem_root,
> + table->s->reclength*sizeof(uchar));
C99 standard says
6.5.3.4 The sizeof operator
When applied to an operand that has type char, unsigned
char, or signed char, (or a qualified version thereof) the
result is 1.
so don't bother with `*sizeof(uchar)`
> +
> + result= h->ha_index_init(key_no, 0);
> + if (result)
> + return result;
> + result= h->ha_index_read_map(table->check_unique_buf,
> + ptr, HA_WHOLE_KEY, HA_READ_KEY_EXACT);
> + if (!result)
> + {
> + bool is_same;
> + Field * t_field;
> + Item_func_hash * temp= (Item_func_hash *)hash_field->vcol_info->expr;
> + Item ** arguments= temp->arguments();
> + uint arg_count= temp->argument_count();
> + do
> + {
> + long diff= table->check_unique_buf - new_rec;
> + is_same= true;
> + for (uint j=0; j < arg_count; j++)
you can add `is_same && ` here to the for() condition too.
> + {
> + DBUG_ASSERT(arguments[j]->type() == Item::FIELD_ITEM ||
> + // this one for left(fld_name,length)
> + arguments[j]->type() == Item::FUNC_ITEM);
> + if (arguments[j]->type() == Item::FIELD_ITEM)
> + {
> + t_field= static_cast<Item_field *>(arguments[j])->field;
> + if (t_field->cmp_offset(diff))
> + is_same= false;
> + }
> + else
> + {
> + Item_func_left *fnc= static_cast<Item_func_left *>(arguments[j]);
> + DBUG_ASSERT(!my_strcasecmp(system_charset_info, "left", fnc->func_name()));
> + DBUG_ASSERT(fnc->arguments()[0]->type() == Item::FIELD_ITEM);
> + t_field= static_cast<Item_field *>(fnc->arguments()[0])->field;
> + uint length= fnc->arguments()[1]->val_int();
> + if (t_field->cmp_max(t_field->ptr, t_field->ptr + diff, length))
> + is_same= false;
> + }
> + }
> + }
> + while (!is_same && !(result= table->file->ha_index_next_same(table->check_unique_buf,
> + ptr, table->key_info[key_no].key_length)));
> + if (is_same)
> + {
> + table->dupp_hash_key= key_no;
> + error= HA_ERR_FOUND_DUPP_KEY;
> + goto exit;
> + }
> + else
> + goto exit;
> + }
> + if (result == HA_ERR_LOCK_WAIT_TIMEOUT)
> + {
> + table->dupp_hash_key= key_no;
> + //TODO check if this is the only case
> + error= HA_ERR_FOUND_DUPP_KEY;
why not to keep HA_ERR_LOCK_WAIT_TIMEOUT?
> + }
> + exit:
> + h->ha_index_end();
> + return error;
> +}
empty line between functions, please
> +/** @brief
> + check whether inserted records breaks the
> + unique constraint on long columns.
> + @returns 0 if no duplicate else returns error
> + */
> +static int check_duplicate_long_entries(TABLE *table, handler *h, uchar *new_rec)
> +{
> + table->dupp_hash_key= -1;
> + int result;
> + for (uint i= 0; i < table->s->keys; i++)
> + {
> + if (table->key_info[i].algorithm == HA_KEY_ALG_LONG_HASH &&
> + (result= check_duplicate_long_entry_key(table, h, new_rec, i)))
> + return result;
> + }
> + return 0;
> +}
> +
> +/** @brief
> + check whether updated records breaks the
> + unique constraint on long columns.
> + In the case of update we just need to check the specic key
> + reason for that is consider case
> + create table t1(a blob , b blob , x blob , y blob ,unique(a,b)
> + ,unique(x,y))
> + and update statement like this
> + update t1 set a=23+a; in this case if we try to scan for
> + whole keys in table then index scan on x_y will return 0
> + because data is same so in the case of update we take
> + key as a parameter in normal insert key should be -1
> + @returns 0 if no duplicate else returns error
> + */
> +static int check_duplicate_long_entries_update(TABLE *table, handler *h, uchar *new_rec)
> +{
> + Field *field;
> + uint key_parts;
> + int error= 0;
> + KEY *keyinfo;
> + KEY_PART_INFO *keypart;
> + /*
> + Here we are comparing whether new record and old record are same
> + with respect to fields in hash_str
> + */
> + long reclength= table->record[1]-table->record[0];
> + if (!table->update_handler)
> + table->clone_handler_for_update();
> + for (uint i= 0; i < table->s->keys; i++)
> + {
> + keyinfo= table->key_info + i;
> + if (keyinfo->algorithm == HA_KEY_ALG_LONG_HASH)
> + {
> + key_parts= fields_in_hash_keyinfo(keyinfo);
> + keypart= keyinfo->key_part - key_parts;
> + for (uint j= 0; j < key_parts; j++, keypart++)
> + {
> + field= keypart->field;
> + /* Compare fields if they are different then check for duplicates*/
> + if(field->cmp_binary_offset(reclength))
> + {
> + if((error= check_duplicate_long_entry_key(table, table->update_handler,
> + new_rec, i)))
> + goto exit;
> + /*
> + break beacuse check_duplicate_long_entrie_key will
> + take care of remaning fields
three typos in two lines :)
> + */
> + break;
> + }
> + }
> + }
> + }
> + exit:
> + return error;
> +}
>
> int handler::ha_write_row(uchar *buf)
> {
> diff --git a/sql/item.cc b/sql/item.cc
> --- a/sql/item.cc
> +++ b/sql/item.cc
> @@ -356,7 +356,7 @@ Item::Item(THD *thd):
> /* Initially this item is not attached to any JOIN_TAB. */
> join_tab_idx= MAX_TABLES;
>
> - /* Put item in free list so that we can free all items at end */
> + /* Put item in free list so that we can free all items at end only if*/
Huh?
> next= thd->free_list;
> thd->free_list= this;
> /*
> diff --git a/sql/item_func.cc b/sql/item_func.cc
> --- a/sql/item_func.cc
> +++ b/sql/item_func.cc
> @@ -1718,6 +1718,48 @@ bool Item_func_mod::fix_length_and_dec()
> DBUG_RETURN(FALSE);
> }
>
> +inline void calc_hash_for_unique(ulong &nr1, ulong &nr2, String *str)
static inline (or simply static, a compiler will inline it anyway)
> +{
> + CHARSET_INFO *cs;
> + uchar l[4];
> + int4store(l, str->length());
> + cs= str->charset();
> + cs->coll->hash_sort(cs, l, sizeof(l), &nr1, &nr2);
> + cs= str->charset();
> + cs->coll->hash_sort(cs, (uchar *)str->ptr(), str->length(), &nr1, &nr2);
> +}
> +
> +longlong Item_func_hash::val_int()
> +{
> + DBUG_EXECUTE_IF("same_long_unique_hash", return 9;);
good that you've added tests for collisions!
> + unsigned_flag= true;
> + ulong nr1= 1,nr2= 4;
> + CHARSET_INFO *cs;
> + String * str;
> + for(uint i= 0;i<arg_count;i++)
> + {
> + str = args[i]->val_str();
> + if(args[i]->null_value)
> + {
> + null_value= 1;
> + return 0;
> + }
> + calc_hash_for_unique(nr1, nr2, str);
> + }
> + null_value= 0;
> + return (longlong)nr1;
> +}
> +
> +
> +bool Item_func_hash::fix_length_and_dec()
> +{
> + maybe_null= 1;
Why are you forcing maybe_null=1?
Most functions are "normal" - they can return NULL if one of the
arguments is NULL, otherwise they don't return NULL.
And Item_func::fix_fields() sets maybe_null accordingly, it'll
be 1 if any of the arguments has maybe_null==1, otherwise
maybe_null will be 0.
You only need to set maybe_null explicitly if your function is
special, for ISNULL(x) # never returns NULL, and NULLIF(x,y)
can return NULL when both arguments are not NULL.
Looks like in your case the default maybe_null beahavior is
correct, though.
> + decimals= 0;
> + max_length= 8;
> + return false;
> +}
> +
> +
>
> double Item_func_neg::real_op()
> {
> diff --git a/sql/sql_base.cc b/sql/sql_base.cc
> --- a/sql/sql_base.cc
> +++ b/sql/sql_base.cc
> @@ -994,6 +994,8 @@ void close_thread_table(THD *thd, TABLE **table_ptr)
> table->file->update_global_table_stats();
> table->file->update_global_index_stats();
> }
> + if (table->update_handler)
> + table->delete_update_handler();
you're doing it in sql_update.cc, isn't it enough?
>
> /*
> This look is needed to allow THD::notify_shared_lock() to
> diff --git a/sql/sql_show.cc b/sql/sql_show.cc
> --- a/sql/sql_show.cc
> +++ b/sql/sql_show.cc
> @@ -2306,7 +2307,7 @@ int show_create_table(THD *thd, TABLE_LIST *table_list, String *packet,
> */
> packet->append(STRING_WITH_LEN("PRIMARY KEY"));
> }
> - else if (key_info->flags & HA_NOSAME)
> + else if (key_info->flags & HA_NOSAME || key_info->algorithm == HA_KEY_ALG_LONG_HASH)
I'd think that after setup_table_hash(), HA_NOSAME should be set
and you wouldn't need extra checks.
> packet->append(STRING_WITH_LEN("UNIQUE KEY "));
> else if (key_info->flags & HA_FULLTEXT)
> packet->append(STRING_WITH_LEN("FULLTEXT KEY "));
> @@ -6546,7 +6548,7 @@ static int get_schema_stat_record(THD *thd, TABLE_LIST *tables,
> else if (!tables->view)
> {
> TABLE *show_table= tables->table;
> - KEY *key_info=show_table->s->key_info;
> + KEY *key_info=show_table->key_info;
you mean, TABLE_SHARE::key_info shows your HASH definition?
and but you change (with setup_table_hash) TABLE::key_info
to store the original user's UNIQUE definition?
Why is TABLE_SHARE::key_info changed?
> if (show_table->file)
> {
> show_table->file->info(HA_STATUS_VARIABLE |
> diff --git a/sql/sql_table.cc b/sql/sql_table.cc
> --- a/sql/sql_table.cc
> +++ b/sql/sql_table.cc
> @@ -4106,12 +4177,28 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
> if (key->type == Key::UNIQUE && !(key_info->flags & HA_NULL_PART_KEY))
> unique_key=1;
> key_info->key_length=(uint16) key_length;
> - if (key_length > max_key_length && key->type != Key::FULLTEXT)
> + if (key_length > max_key_length && key->type != Key::FULLTEXT &&
> + !is_hash_field_needed)
> {
> my_error(ER_TOO_LONG_KEY,MYF(0),max_key_length);
> DBUG_RETURN(TRUE);
> }
>
> + if (is_hash_field_needed)
> + {
> + Create_field *hash_fld= add_hash_field(thd, &alter_info->create_list,
> + create_info->default_table_charset,
> + key_info);
> + hash_fld->offset= record_offset;
> + record_offset+= hash_fld->pack_length;
> + if (key_info->flags & HA_NULL_PART_KEY)
> + null_fields++;
> + else
> + {
> + hash_fld->flags|= NOT_NULL_FLAG;
> + hash_fld->pack_flag&= ~FIELDFLAG_MAYBE_NULL;
> + }
> + }
Hmm, haven't we agreed *not* to write the virtual hash field
into the frm, not show it to create/alter table and
add it automatically when the table is opened?
> if (validate_comment_length(thd, &key->key_create_info.comment,
> INDEX_COMMENT_MAXLEN,
> ER_TOO_LONG_INDEX_COMMENT,
> diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
> --- a/sql/sql_yacc.yy
> +++ b/sql/sql_yacc.yy
> @@ -10820,6 +10820,12 @@ function_call_conflict:
> if (unlikely($$ == NULL))
> MYSQL_YYABORT;
> }
> + | HASH_SYM '(' expr_list ')'
> + {
> + $$= new (thd->mem_root)Item_func_hash(thd,*$3);
> + if (unlikely($$ == NULL))
> + MYSQL_YYABORT;
> + }
Haven't we agreed to make this HASH() function fully internal,
not visible to the parser?
> /* LAST_VALUE here conflicts with the definition for window functions.
> We have these 2 separate rules to remove the shift/reduce conflict.
> */
> diff --git a/sql/table.cc b/sql/table.cc
> --- a/sql/table.cc
> +++ b/sql/table.cc
> @@ -747,7 +746,7 @@ static bool create_key_infos(const uchar *strpos, const uchar *frm_image_end,
> if (i == 0)
> {
> ext_key_parts+= (share->use_ext_keys ? first_keyinfo->user_defined_key_parts*(keys-1) : 0);
> - n_length=keys * sizeof(KEY) + ext_key_parts * sizeof(KEY_PART_INFO);
> + n_length=keys * sizeof(KEY) + (ext_key_parts) * sizeof(KEY_PART_INFO);
Another left-over from an intermediate implementation? :)
> if (!(keyinfo= (KEY*) alloc_root(&share->mem_root,
> n_length + len)))
> return 1;
> @@ -798,6 +797,12 @@ static bool create_key_infos(const uchar *strpos, const uchar *frm_image_end,
> }
> key_part->store_length=key_part->length;
> }
> + if (keyinfo->algorithm == HA_KEY_ALG_LONG_HASH)
> + {
> + keyinfo->key_length= HA_HASH_KEY_LENGTH_WITHOUT_NULL;
> + //Storing key hash
> + key_part++;
> + }
I don't understand what you do here
>
> /*
> Add primary key to end of extended keys for non unique keys for
> @@ -831,7 +836,9 @@ static bool create_key_infos(const uchar *strpos, const uchar *frm_image_end,
> if (j == first_key_parts)
> keyinfo->ext_key_flags= keyinfo->flags | HA_EXT_NOSAME;
> }
> - share->ext_key_parts+= keyinfo->ext_key_parts;
> + if (keyinfo->algorithm == HA_KEY_ALG_LONG_HASH)
> + share->ext_key_parts++;
> + share->ext_key_parts+= keyinfo->ext_key_parts;
I don't understand what you do here
> }
> keynames=(char*) key_part;
> strpos+= strnmov(keynames, (char *) strpos, frm_image_end - strpos) - keynames;
> @@ -3322,6 +3421,36 @@ enum open_frm_error open_table_from_share(THD *thd, TABLE_SHARE *share,
>
> key_part_end= key_part + (share->use_ext_keys ? key_info->ext_key_parts :
> key_info->user_defined_key_parts) ;
> + if (key_info->algorithm == HA_KEY_ALG_LONG_HASH)
> + {
> + /*
> + Either it can be first time opening the table share or it can be second time
> + of more. The difference is when it is first time key_part[0]->fieldnr points
> + to blob/long field, but when it is 2nd time there will bw always one key_part
> + and it will point to hash_field.
> + So in the case of 2nd time we will make key_info->key_part point to start of long
> + field.
you're overcomplicating here.
if you would *always* create your HASH key_def in
parse_vcol_defs(), you'll have TABLE_SHARE to be always "like
the first time", you simply never modify TABLE_SHARE and always
modify TABLEin parse_vcol_defs().
and you won't need setup_keyinfo_hash/setup_table_hash functions
either.
> + For example we have unique(a,b,c)
> + In first share opening key_part will point to a field
> + but in parse_vcol_defs it will be changed to point to db_row_hash field
> + in Second or later opening key_part will be pointing to db_row_hash
> + We will chnage it back to point to field a, because in this way we can create
> + vcol_info for hash field in parse_vcol_defs.
> + */
> + //Second or more time share opening
> + key_info->user_defined_key_parts= 0;
> + key_part_end= key_part;
> + while(!(share->field[key_part_end->fieldnr -1 ]->flags & LONG_UNIQUE_HASH_FIELD))
> + {
> + key_part_end++;
> + key_info->user_defined_key_parts++;
> + }
> + key_info->usable_key_parts= key_info->ext_key_parts= key_info->user_defined_key_parts;
> + key_part_end++;
> + share_keyinfo= share->key_info + key_no;
> + if (share_keyinfo->key_part->field->flags & LONG_UNIQUE_HASH_FIELD)
> + share_keyinfo->key_part-= key_info->user_defined_key_parts;
> + }
> for ( ; key_part < key_part_end; key_part++)
> {
> Field *field= key_part->field= outparam->field[key_part->fieldnr - 1];
> @@ -8498,6 +8630,120 @@ double KEY::actual_rec_per_key(uint i)
> }
>
>
> +/*
> + find out the field positoin in hash_str()
> + position starts from 0
> + else return -1;
> +*/
> +int find_field_pos_in_hash(Item *hash_item, const char * field_name)
> +{
> + Item_func_or_sum * temp= static_cast<Item_func_or_sum *>(hash_item);
> + Item_args * t_item= static_cast<Item_args *>(temp);
> + uint arg_count= t_item->argument_count();
> + Item ** arguments= t_item->arguments();
> + Field * t_field;
> +
> + for (uint j=0; j < arg_count; j++)
> + {
> + DBUG_ASSERT(arguments[j]->type() == Item::FIELD_ITEM ||
> + arguments[j]->type() == Item::FUNC_ITEM);
> + if (arguments[j]->type() == Item::FIELD_ITEM)
> + {
> + t_field= static_cast<Item_field *>(arguments[j])->field;
> + }
> + else
> + {
> + Item_func_left *fnc= static_cast<Item_func_left *>(arguments[j]);
> + t_field= static_cast<Item_field *>(fnc->arguments()[0])->field;
> + }
> + if (!my_strcasecmp(system_charset_info, t_field->field_name.str, field_name))
> + return j;
> + }
> + return -1;
> +}
> +
> +/*
> + find total number of field in hash expr
> +*/
> +int fields_in_hash_keyinfo(KEY *keyinfo)
> +{
> + Item_func_hash * temp= (Item_func_hash *)
> + keyinfo->key_part->field->vcol_info->expr;
> + return temp->argument_count();
> +}
> +
> +void setup_keyinfo_hash(KEY *key_info)
> +{
> + uint no_of_keyparts= fields_in_hash_keyinfo(key_info);
> + key_info->key_part-= no_of_keyparts;
> + key_info->user_defined_key_parts= key_info->usable_key_parts=
> + key_info->ext_key_parts= no_of_keyparts;
add DBUG_ASSERT to make sure it's your HA_KEY_ALG_LONG_HASH
> +}
> +
> +void re_setup_keyinfo_hash(KEY *key_info)
> +{
> + while(!(key_info->key_part->field->flags & LONG_UNIQUE_HASH_FIELD))
> + key_info->key_part++;
> + key_info->user_defined_key_parts= key_info->usable_key_parts=
> + key_info->ext_key_parts= 1;
> +}
> +/**
> + @brief clone of current handler.
> + Creates a clone of handler used in update for
> + unique hash key.
> +*/
> +void TABLE::clone_handler_for_update()
> +{
> + handler *update_handler= NULL;
> + if (!s->long_unique_table)
> + return;
> + update_handler= file->clone(s->normalized_path.str,
> + in_use->mem_root);
> + update_handler->ha_external_lock(in_use, F_RDLCK);
> + this->update_handler= update_handler;
> + return;
> +}
> +
> +/**
> + @brief Deletes update handler object
> +*/
> +void TABLE::delete_update_handler()
> +{
> + update_handler->ha_external_lock(in_use, F_UNLCK);
> + update_handler->ha_close();
> + delete update_handler;
> + this->update_handler= NULL;
> +}
> +/**
> + @brief This function makes table object with
> + long unique keys ready for optimizer and alter table
Looks like a bad function name and a bad explanation.
I think - but not sure! - that it restores the original
UNIQUE definition, with columns as specified by the user.
Is that so?
> + @param table Table object
> + */
> +void TABLE::setup_table_hash()
> +{
> +
> + if (!this->s->long_unique_table)
> + return;
> + KEY *keyinfo= key_info;
> + for (uint i= 0; i < this->s->keys; i++, keyinfo++)
> + if (keyinfo->algorithm == HA_KEY_ALG_LONG_HASH)
> + setup_keyinfo_hash(keyinfo);
> +}
> +
> +/**
> + @brief Revert the effect of setup_table_hash
> + @param table Table Object
> + */
> +void TABLE::re_setup_table()
> +{
> + if (!s->long_unique_table)
> + return;
> + KEY *keyinfo= key_info;
> + for (uint i= 0; i < s->keys; i++, keyinfo++)
> + if (keyinfo->algorithm == HA_KEY_ALG_LONG_HASH)
> + re_setup_keyinfo_hash(keyinfo);
> +}
> +
> LEX_CSTRING *fk_option_name(enum_fk_option opt)
> {
> static LEX_CSTRING names[]=
> diff --git a/sql/table.h b/sql/table.h
> --- a/sql/table.h
> +++ b/sql/table.h
> @@ -1105,6 +1120,17 @@ struct TABLE
> THD *in_use; /* Which thread uses this */
>
> uchar *record[3]; /* Pointer to records */
> + /* record buf to resolve hash collisions for long UNIQUE constraints */
> + uchar *check_unique_buf;
> + handler *update_handler; /* Handler used in case of update */
> + /*
> + In the case of write row for long unique we are unable to find
> + which key is violated. Because we in case of duplicate hash we never reach
> + handler write_row function. So print_error will always print that
> + key 0 is violated. We store which key is violated in this variable
> + by default this should be initialized to -1
> + */
> + int dupp_hash_key;
couldn't you simply store it in table->file->errkey ?
> uchar *write_row_record; /* Used as optimisation in
> THD::write_row */
> uchar *insert_values; /* used by INSERT ... UPDATE */
> diff --git a/sql/table_cache.cc b/sql/table_cache.cc
> --- a/sql/table_cache.cc
> +++ b/sql/table_cache.cc
> @@ -822,13 +822,10 @@ TABLE_SHARE *tdc_acquire_share(THD *thd, TABLE_LIST *tl, uint flags,
>
> if (res == -1)
> DBUG_RETURN(0);
> - else if (res == 1)
> - continue;
Why?
>
> element= (TDC_element*) lf_hash_search_using_hash_value(&tdc_hash,
> thd->tdc_hash_pins, hash_value, (uchar*) key, key_length);
> lf_hash_search_unpin(thd->tdc_hash_pins);
> - DBUG_ASSERT(element);
>
> if (!(share= alloc_table_share(tl->db.str, tl->table_name.str, key, key_length)))
> {
> diff --git a/storage/myisam/myisamchk.c b/storage/myisam/myisamchk.c
> --- a/storage/myisam/myisamchk.c
> +++ b/storage/myisam/myisamchk.c
> @@ -1333,7 +1333,8 @@ static void descript(HA_CHECK *param, register MI_INFO *info, char * name)
> key++,keyinfo++)
> {
> keyseg=keyinfo->seg;
> - if (keyinfo->flag & HA_NOSAME) text="unique ";
> + if (keyinfo->flag & HA_NOSAME)
> + text="unique ";
I presume you can revert this change, looks like some left-over
from when you tried to make myisamchk to recognize your uniques
and then later reverted it.
> else if (keyinfo->flag & HA_FULLTEXT) text="fulltext ";
> else text="multip.";
>
>
Regards,
Sergei
Chief Architect MariaDB
and security(a)mariadb.org
2
2

Re: [Maria-developers] 4b01d3aee60: MDEV-17082 Application-time periods: CREATE
by Sergei Golubchik 12 Feb '19
by Sergei Golubchik 12 Feb '19
12 Feb '19
Hi, Nikita!
On Jan 31, Nikita Malyavin wrote:
> revision-id: 4b01d3aee60 (versioning-1.0.7-2-g4b01d3aee60)
> parent(s): 44144533e50
> author: Nikita Malyavin <nikitamalyavin(a)gmail.com>
> committer: Nikita Malyavin <nikitamalyavin(a)gmail.com>
> timestamp: 2019-01-30 22:53:43 +1000
> message:
>
> MDEV-17082 Application-time periods: CREATE
>
> * add syntax `CREATE TABLE ... PERIOD FOR <apptime>`
> * add table period entity
>
> diff --git a/mysql-test/suite/period/r/create.result b/mysql-test/suite/period/r/create.result
> --- /dev/null
> +++ b/mysql-test/suite/period/r/create.result
> @@ -0,0 +1,82 @@
> +create or replace table t (id int primary key, s date, e date,
> +period for mytime(s,e));
> +show create table t;
> +Table Create Table
> +t CREATE TABLE `t` (
> + `id` int(11) NOT NULL,
> + `s` date NOT NULL,
> + `e` date NOT NULL,
> + PRIMARY KEY (`id`),
> + PERIOD FOR `mytime` (`s`, `e`),
> + CONSTRAINT `CONSTRAINT_1` CHECK (`s` < `e`)
as discussed, let's give the constraint the same name as the period.
but don't forget to add a test where a user explicitly creates a
constraint with this name. Like
create or replace table t (id int primary key, s date, e date,
period for mytime(s,e), constraint mytime (id < 100));
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +create or replace table t (id int primary key, s timestamp(6), e timestamp(6),
> +period for mytime(s,e));
> +show create table t;
> +Table Create Table
> +t CREATE TABLE `t` (
> + `id` int(11) NOT NULL,
> + `s` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000',
> + `e` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000',
> + PRIMARY KEY (`id`),
> + PERIOD FOR `mytime` (`s`, `e`),
> + CONSTRAINT `CONSTRAINT_1` CHECK (`s` < `e`)
> +) ENGINE=MyISAM DEFAULT CHARSET=latin1
> +# SQL16, Part 2, 11.3 <table definition>, Syntax Rules, 2)a)
> +# 2) If a <table period definition> TPD is specified, then:
> +# a) <table scope> shall not be specified.
> +create or replace temporary table t (s date, e date, period for mytime(s,e));
> +ERROR HY000: Temporary tables with application-time period not allowed
_are_ not allowed.
or, better,
Application-time period table cannot be temporary
> +# SQL16, Part 2, 11.3 <table definition>, Syntax Rules, 2)e)iii)
> +# The <data type or domain name> contained in CD1 is either DATE or a
> +# timestamp type and it is equivalent to the <data type or domain name>
> +# contained in CD2.
> +create or replace table t (id int primary key, s datetime, e date,
> +period for mytime(s,e));
> +ERROR HY000: PERIOD FOR `mytime` fields types mismatch
_field_ type mismatch
or
Fields `s` and `t` of PERIOD FOR `mytime` have different types.
or
Fields of PERIOD FOR `mytime` have different types.
> +create or replace table t (s timestamp(2), e timestamp(6),
> +period for mytime(s,e));
> +ERROR HY000: PERIOD FOR `mytime` fields types mismatch
> +create or replace table t (id int primary key, s int, e date,
> +period for mytime(s,e));
> +ERROR 42000: Incorrect column specifier for column 's'
incorrect column _type_?
> +create or replace table t (id int primary key, s date, e date,
> +period for mytime(s,x));
> +ERROR 42S22: Unknown column 'x' in 'mytime'
> +create or replace table t (id int primary key, s date, e date,
> +period for mytime(s,e),
> +period for mytime2(s,e));
> +ERROR HY000: Cannot specify more than one application-time period
> +# SQL16, Part 2, 11.3 <table definition>, Syntax Rules, 2)d)
> +# No <column name> in any <column definition> shall be equivalent to PN.
> +create or replace table t (mytime int, s date, e date,
> +period for mytime(s,e));
> +ERROR HY000: Could not specify name `mytime` for field. It is already used by period.
Better "Column and period `mytime` have the same name".
Because "already used" implies that period got the name first, while in
the table definition the field was specified first.
> +# SQL16, Part 2, 11.3 <table definition>, Syntax Rules, 2)e)v)2)A)
> +# Neither CD1 nor CD2 shall contain an <identity column specification>, a
> +# <generation clause>, a <system time period start column specification>,
> +# or a <system time period end column specification>.
> +create or replace table t (id int primary key,
> +s date,
> +e date generated always as (s+1),
> +period for mytime(s,e));
> +ERROR HY000: Period field `e` cannot be GENERATED ALWAYS AS
> +create or replace table t (id int primary key,
> +s date,
> +e date as (s+1) VIRTUAL,
> +period for mytime(s,e));
> +ERROR HY000: Period field `e` cannot be GENERATED ALWAYS AS
> +create or replace table t (id int primary key, s timestamp(6), e timestamp(6),
> +st timestamp(6) as row start,
> +en timestamp(6) as row end,
> +period for system_time (st, en),
> +period for mytime(st,e)) with system versioning;
> +ERROR HY000: Period field `st` cannot be GENERATED ALWAYS AS
> +# SQL16, Part 2, 11.3 <table definition>, Syntax Rules, 2)
> +# Let IDCN be an implementation-dependent <constraint name> that is not
> +# equivalent to the <constraint name> of any table constraint descriptor
> +# included in S.
> +create or replace table t (x int, s date, e date,
> +period for mytime(s, e),
> +constraint mytime check (x > 1));
please, add SHOW CREATE TABLE here (add it after every successful CREATE TABLE).
> +create or replace database test;
> diff --git a/sql/datadict.cc b/sql/datadict.cc
> --- a/sql/datadict.cc
> +++ b/sql/datadict.cc
> @@ -292,6 +292,12 @@ bool dd_read_extra2(const uchar *frm_image, size_t len, extra2_fields *fields)
We've discussed that TRUNCATE TABLE bug fix, it won't need dd_read_extra2().
You still want to have a separate function for reading extra2, fine, but
as dd_frm_type() won't needed it, I'd rather move it into table.cc
and declared it static.
In fact, if you'd like to keep extra2 code separate, you could move
everything extra2 related to a separate object (like your extra2_fields)
with methods for reading and writing it. But, please, don't do this
refactoring before March.
> fields->field_flags.str= extra2;
> fields->field_flags.length= length;
> break;
> + case EXTRA2_APPLICATION_TIME_PERIOD:
> + if (fields->application_period.str)
> + DBUG_RETURN(true);
> + fields->application_period.str= extra2;
> + fields->application_period.length= length;
> + break;
> default:
> /* abort frm parsing if it's an unknown but important extra2 value */
> if (type >= EXTRA2_ENGINE_IMPORTANT)
> diff --git a/sql/handler.cc b/sql/handler.cc
> --- a/sql/handler.cc
> +++ b/sql/handler.cc
> @@ -7352,3 +7352,99 @@ bool Vers_parse_info::check_sys_fields(const Lex_table_name &table_name,
>
> +bool Table_scope_and_contents_source_st::check_fields(
> + THD *thd, Alter_info *alter_info, TABLE_LIST &create_table)
> +{
> + bool res= vers_check_system_fields(thd, alter_info, create_table);
> + if (res)
> + return true;
> +
> + if (!period_info.name)
> + return false;
> +
> + if (tmp_table())
> + {
> + my_error(ER_PERIOD_TEMPORARY_NOT_ALLOWED, MYF(0));
> + return true;
> + }
> +
> + Table_period_info::start_end_t &period= period_info.period;
> + const Create_field *row_start= NULL;
> + const Create_field *row_end= NULL;
> + List_iterator<Create_field> it(alter_info->create_list);
> + while (const Create_field *f= it++)
> + {
> + if (period.start.streq(f->field_name)) row_start= f;
> + else if (period.end.streq(f->field_name)) row_end= f;
> +
> + if (period_info.name.streq(f->field_name))
> + {
> + // TODO this stub should be removed by MDEV-16976
why removed?
> + my_error(ER_PERIOD_NAME_IS_NOT_ALLOWED_FOR_FIELD, MYF(0), f->field_name.str);
> + return true;
> + }
> + }
>
> diff --git a/sql/table.h b/sql/table.h
> --- a/sql/table.h
> +++ b/sql/table.h
> @@ -1730,6 +1747,9 @@ class IS_table_read_plan;
> /** The threshold size a blob field buffer before it is freed */
> #define MAX_TDC_BLOB_SIZE 65536
>
> +/** number of bytes read by uint2korr and sint2korr */
> +#define korr2size 2
No, that's silly. It's like #define TWO 2
Of course korr2size is 2. And korr3size is 3, and korr4size is 4.
If you want to get rid of the magic number, don't put it into the name
of the macro :)
That's why I suggested something like
#define fieldno_size 2
#define fieldno_korr uint2korr
#define fieldno_store int2store
Regards,
Sergei
Chief Architect MariaDB
and security(a)mariadb.org
2
5
A final reminder about the MariaDB Developers Unconference taking place
23 - 24 February in New York, just before OpenWorks.
We're almost at capacity, so if you haven't signed up yet, please do so
soon. Details about where to sign up and how to suggest sessions are at
https://mariadb.org/2019-developers-unconference-new-york/
Hope to see you there!
1
0

10 Feb '19
Hi Sergei!
Can you review that you are happy with the storage engine API changes?
I'veustructured the commit to be as small as possible to achieve the
desired outcome. In my tests, we are now twice as fast as MySQL for a
10 mil row table with 13 columns.
Vicențiu
-------- Forwarded Message --------From: vicentiu(a)mariadb.orgTo:
commits(a)mariadb.orgSubject: 53730224efd: Improve histogram collection
performance by samplingDate: Sun, 10 Feb 2019 20:09:49 +0200 (EET)
revision-id: 53730224efd987f97a6cc968ff5214ee499d84e0 (mariadb-10.4.1-
163-g53730224efd)parent(s):
3c305d3f1951f1667f84e48ddd98674c6318c39dauthor: Vicențiu
Ciorbarucommitter: Vicențiu Ciorbarutimestamp: 2019-02-10 19:54:50
+0200message:
Improve histogram collection performance by sampling
Histogram collection is done by sampling a percentage of rows from the
table,not looking at all individual ones.
The default implementation, to keep the server's Engine
IndepenentStatistics component working uses Bernoulli sampling. It does
a simpletable scan, but only accepts rows that pass a dice roll.
Thisimplementation is done as a storage engine interface method, so as
toallow faster and/or more efficient implementations for storage
enginesinternally.
The number of rows collected is capped to a minimum of 50000
andincreases logarithmically with a coffecient of 4096. The coffecient
ischosen so that we expect an error of less than 3% in our
estimationsaccording to the paper:"Random Sampling for Histogram
Construction: How much is enough?”– Surajit Chaudhuri, Rajeev Motwani,
Vivek Narasayya, ACM SIGMOD, 1998.
This interface is also a precursor to allowing SELECT ... FROM
tablewith sampling to work.
Performance wise, for a table of 10 million rows and 13 columns, 6
int,6 doubles, one string, the previous analyze table statistics took1
minute 20 seconds to collect all data. Post implementation, thetime is
less than 6 seconds. This was run on an InnoDB table, NVME SSD
withapproximately 2GB/s read speed and 500MB/s write speed.
--- mysql-test/main/selectivity.result | 8 +++---- mysql-
test/main/selectivity_innodb.result | 8 +++--
-- sql/handler.cc | 14
+++++++++++ sql/handler.h | 40
++++++++++++++++++++++++++++++- sql/sql_statistics.cc
| 32 +++++++++++++++++++------ 5 files changed, 86 insertions(+), 16
deletions(-)
diff --git a/mysql-test/main/selectivity.result b/mysql-
test/main/selectivity.resultindex 00907235ecc..6d09c1c9b62 100644---
a/mysql-test/main/selectivity.result+++ b/mysql-
test/main/selectivity.result@@ -1290,9 +1290,9 @@ explain
extended select * from t1, t2, t1 as t3 where t1.b=t2.c and t2.d=t3.a
and t3.b<5 and t1.a < 2000; id select_type table type possibl
e_keys key key_len ref rows filtered Extra-1 SIMPLE
t1 ALL NULL NULL NULL NULL 262144 100.00 Using
where+1 SIMPLE t1 ALL NULL NULL NULL NULL 262117
100.00 Using where 1 SIMPLE t2 ref c,d c 5
test.t1.b 5 100.00 -1 SIMPLE t3 ALL NULL
NULL NULL NULL 262144 100.00 Using where; Using join buffer
(flat, BNL join)+1 SIMPLE t3 ALL NULL NULL NULL
NULL 262117 100.00 Using where; Using join buffer (flat, BNL
join) Warnings: Note 1003 select `test`.`t1`.`a` AS
`a`,`test`.`t1`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`test`.`t2`.`d` AS
`d`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS `b` from `test`.`t1` join
`test`.`t2` join `test`.`t1` `t3` where `test`.`t2`.`c` =
`test`.`t1`.`b` and `test`.`t3`.`a` = `test`.`t2`.`d` and
`test`.`t3`.`b` < 5 and `test`.`t1`.`a` < 2000 select * from t1, t2, t1
as t3@@ -1307,9 +1307,9 @@ explain extended select * from t1, t2, t1 as
t3 where t1.b=t2.c and t2.d=t3.a and t3.b<5 and t1.a < 2000; id select_
type table type possible_keys key key_len ref rows
filtered Extra-1 SIMPLE t3 ALL NULL NULL NULL
NULL 262144 0.00 Using where+1 SIMPLE t3 ALL NULL
NULL NULL NULL 262117 0.00 Using where 1 SIMPLE t2
ref c,d d 5 test.t3.a 7 100.00 -1
SIMPLE t1 ALL NULL NULL NULL NULL 262144 2.00
Using where; Using join buffer (flat, BNL join)+1 SIMPLE t1
ALL NULL NULL NULL NULL 262117 2.00 Using where;
Using join buffer (flat, BNL join) Warnings: Note 1003 select
`test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t2`.`c` AS
`c`,`test`.`t2`.`d` AS `d`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS
`b` from `test`.`t1` join `test`.`t2` join `test`.`t1` `t3` where
`test`.`t1`.`b` = `test`.`t2`.`c` and `test`.`t2`.`d` = `test`.`t3`.`a`
and `test`.`t3`.`b` < 5 and `test`.`t1`.`a` < 2000 select * from t1,
t2, t1 as t3diff --git a/mysql-test/main/selectivity_innodb.result
b/mysql-test/main/selectivity_innodb.resultindex
93917065722..0b20a40f69f 100644--- a/mysql-
test/main/selectivity_innodb.result+++ b/mysql-
test/main/selectivity_innodb.result@@ -1300,9 +1300,9 @@ explain
extended select * from t1, t2, t1 as t3 where t1.b=t2.c and t2.d=t3.a
and t3.b<5 and t1.a < 2000; id select_type table type possibl
e_keys key key_len ref rows filtered Extra-1 SIMPLE
t1 ALL NULL NULL NULL NULL 262144 100.00 Using
where+1 SIMPLE t1 ALL NULL NULL NULL NULL 262117
100.00 Using where 1 SIMPLE t2 ref c,d c 5
test.t1.b 5 100.00 -1 SIMPLE t3 ALL NULL
NULL NULL NULL 262144 100.00 Using where; Using join buffer
(flat, BNL join)+1 SIMPLE t3 ALL NULL NULL NULL
NULL 262117 100.00 Using where; Using join buffer (flat, BNL
join) Warnings: Note 1003 select `test`.`t1`.`a` AS
`a`,`test`.`t1`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`test`.`t2`.`d` AS
`d`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS `b` from `test`.`t1` join
`test`.`t2` join `test`.`t1` `t3` where `test`.`t2`.`c` =
`test`.`t1`.`b` and `test`.`t3`.`a` = `test`.`t2`.`d` and
`test`.`t3`.`b` < 5 and `test`.`t1`.`a` < 2000 select * from t1, t2, t1
as t3@@ -1317,9 +1317,9 @@ explain extended select * from t1, t2, t1 as
t3 where t1.b=t2.c and t2.d=t3.a and t3.b<5 and t1.a < 2000; id select_
type table type possible_keys key key_len ref rows
filtered Extra-1 SIMPLE t3 ALL NULL NULL NULL
NULL 262144 0.00 Using where+1 SIMPLE t3 ALL NULL
NULL NULL NULL 262117 0.00 Using where 1 SIMPLE t2
ref c,d d 5 test.t3.a 7 100.00 -1
SIMPLE t1 ALL NULL NULL NULL NULL 262144 2.00
Using where; Using join buffer (flat, BNL join)+1 SIMPLE t1
ALL NULL NULL NULL NULL 262117 2.00 Using where;
Using join buffer (flat, BNL join) Warnings: Note 1003 select
`test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t2`.`c` AS
`c`,`test`.`t2`.`d` AS `d`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS
`b` from `test`.`t1` join `test`.`t2` join `test`.`t1` `t3` where
`test`.`t1`.`b` = `test`.`t2`.`c` and `test`.`t2`.`d` = `test`.`t3`.`a`
and `test`.`t3`.`b` < 5 and `test`.`t1`.`a` < 2000 select * from t1,
t2, t1 as t3diff --git a/sql/handler.cc b/sql/handler.ccindex
2e45b5883ea..16fb126918e 100644--- a/sql/handler.cc+++
b/sql/handler.cc@@ -7547,3 +7547,17 @@ bool
Vers_parse_info::check_sys_fields(const Lex_table_name
&table_name, "ROW END" : found_flag ? "ROW START" : "ROW
START/END"); return true; }++int handler::random_sample(uchar
*buf)+{+ int rc;+ THD *thd= ha_thd();+ do+ {+ if (table->in_use-
>check_killed(1))+ return HA_ERR_ABORTED_BY_USER;+ rc=
rnd_next(buf);+ } while (rc == HA_ERR_RECORD_DELETED || thd_rnd(thd) >
sample_fraction);++ return rc;+}diff --git a/sql/handler.h
b/sql/handler.hindex dfb2333b24e..fcdadbeb42b 100644---
a/sql/handler.h+++ b/sql/handler.h@@ -1913,6 +1913,11 @@ enum
enum_stats_auto_recalc { HA_STATS_AUTO_RECALC_DEFAULT=
0, HA_STATS_AUTO_RECALC_ON,
HA_STATS_AUTO_RECALC_OFF }; +enum sample_mode
{+ HA_SAMPLE_BERNOULLI= 0,+ HA_SAMPLE_SYSTEM+};+ /** A helper
struct for schema DDL statements: CREATE SCHEMA [IF NOT EXISTS]
name [ schema_specification... ]@@ -2947,9 +2952,11 @@ class handler
:public Sql_alloc /** Length of ref (1-8 or the clustered key length)
*/ uint ref_length; FT_INFO *ft_handler;- enum init_stat { NONE=0,
INDEX, RND };+ enum init_stat { NONE=0, INDEX, RND, SAMPLE
}; init_stat inited, pre_inited; + double sample_fraction= 0;+ enum
sample_mode sample_mode; const COND
*pushed_cond; /** next_insert_id is the next value which should
be inserted into the@@ -3112,6 +3119,31 @@ class handler :public
Sql_alloc virtual int prepare_range_scan(const key_range *start_key,
const key_range *end_key) { return 0; } + int
ha_random_sample_init(THD *thd, enum sample_mode mode, double
fraction)+ __attribute__((warn_unused_result))+ {+ DBUG_ENTER("h
a_random_sample_init");+ DBUG_ASSERT(inited==NONE);+ int
result;+ sample_mode= mode;+ sample_fraction=
fraction;+ inited= (result= random_sample_init(mode, fraction)) ?
NONE : SAMPLE;+ DBUG_RETURN(result);+ }+ int
ha_random_sample(uchar
*buf)+ __attribute__((warn_unused_result))+ {+ DBUG_ENTER("ha_ra
ndom_sample");+ DBUG_ASSERT(inited ==
SAMPLE);+ DBUG_RETURN(random_sample(buf));+ }+ int
ha_random_sample_end()+ {+ DBUG_ENTER("ha_random_sample_end");+
inited= NONE;+ DBUG_RETURN(random_sample_end());+ }+ int
ha_rnd_init(bool scan) __attribute__
((warn_unused_result)) { DBUG_EXECUTE_IF("ha_rnd_init_fail",
return HA_ERR_TABLE_DEF_CHANGED;);@@ -4425,6 +4457,12 @@ class handler
:public Sql_alloc /* Note: ha_index_read_idx_map() may bypass
index_init() */ virtual int index_init(uint idx, bool sorted) {
return 0; } virtual int index_end() { return 0; }+ virtual int
random_sample_init(enum sample_mode mode, double
fraction)+ {+ return rnd_init(TRUE);+ }+ virtual int
random_sample(uchar *buf);+ virtual int random_sample_end() { return
rnd_end(); } /** rnd_init() can be called two times without
rnd_end() in between (it only makes sense if scan=1).diff --git
a/sql/sql_statistics.cc b/sql/sql_statistics.ccindex
db214a1fe28..22a015821de 100644--- a/sql/sql_statistics.cc+++
b/sql/sql_statistics.cc@@ -2727,12 +2727,16 @@ int
collect_statistics_for_table(THD *thd, TABLE *table) Field
*table_field; ha_rows rows= 0; handler *file=table->file;+ double
sample_fraction;+ const ha_rows MIN_THRESHOLD_FOR_SAMPLING=
50000; DBUG_ENTER("collect_statistics_for_table"); table-
>collected_stats->cardinality_is_null= TRUE; table->collected_stats-
>cardinality= 0; + table->file->info(HA_STATUS_VARIABLE);+ for
(field_ptr= table->field; *field_ptr; field_ptr++) { table_field=
*field_ptr; @@ -2743,12 +2747,27 @@ int
collect_statistics_for_table(THD *thd, TABLE
*table) restore_record(table, s->default_values); - /* Perform a
full table scan to collect statistics on 'table's columns */- if
(!(rc= file->ha_rnd_init(TRUE)))- { ++ if (file->records() <
MIN_THRESHOLD_FOR_SAMPLING)+ {+ sample_fraction=
1;+ }+ else+ {+ sample_fraction=
std::fmin(+ (MIN_THRESHOLD_FOR_SAMPLING + 4096
*+ log(200 * file->records())) / file->records(),
1);+ }+++ /* Fetch samples from the table to collect statistics on
table's columns */++ if (!(rc= file->ha_random_sample_init(thd,
HA_SAMPLE_BERNOULLI,+ sample_fra
ction)))+ { DEBUG_SYNC(table->in_use,
"statistics_collection_start"); - while ((rc= file-
>ha_rnd_next(table->record[0])) != HA_ERR_END_OF_FILE)+ while ((rc=
file->ha_random_sample(table->record[0])) !=
HA_ERR_END_OF_FILE) { if (thd->killed) break;@@
-2768,10 +2787,9 @@ int collect_statistics_for_table(THD *thd, TABLE
*table) break; rows++; }- file-
>ha_rnd_end();+ file->ha_random_sample_end(); } rc= (rc ==
HA_ERR_END_OF_FILE && !thd->killed) ? 0 : 1;- /* Calculate
values for all statistical characteristics on columns and and for
each field f of 'table' save them in the write_stat structure@@ -2780,7
+2798,7 @@ int collect_statistics_for_table(THD *thd, TABLE
*table) if (!rc) { table->collected_stats->cardinality_is_null=
FALSE;- table->collected_stats->cardinality= rows;+ table-
>collected_stats->cardinality= rows /
sample_fraction; } bitmap_clear_all(table->write_set);
1
0
Hi Varun,
I've did some adjustments MDEV-18489 and pushed the patch into
10.4-optimizer-trace, please check it out.
Also I have filed MDEV-18527 and MDEV-18528.
Some input on the code:
> --- 10.4-optimizer-trace-orig/sql/sql_select.cc
> +++ 10.4-optimizer-trace-cl/sql/sql_select.cc
> @@ -15983,12 +16250,26 @@ optimize_cond(JOIN *join, COND *conds,
> that occurs in a function set a pointer to the multiple equality
> predicate. Substitute a constant instead of this field if the
> multiple equality contains a constant.
> - */
> - DBUG_EXECUTE("where", print_where(conds, "original", QT_ORDINARY););
> + */
> +
> + Opt_trace_context *const trace = &thd->opt_trace;
> + Json_writer *writer= trace->get_current_json();
> + Json_writer_object trace_wrapper(writer);
> + Json_writer_object trace_cond(writer, "condition_processing");
> + trace_cond.add("condition", join->conds == conds ? "WHERE" : "HAVING")
> + .add("original_condition", conds);
> +
> + Json_writer_array trace_steps(writer, "steps");
> + DBUG_EXECUTE("where", print_where(conds, "original", QT_ORDINARY););
Small question: Why was DBUG_EXECUTE shifted right?
A bigger question: the code seems unnecesarily verbose:
1. > + Opt_trace_context *const trace = &thd->opt_trace;
2. > + Json_writer *writer= trace->get_current_json();
3. > + Json_writer_object trace_wrapper(writer);
4. > + Json_writer_object trace_cond(writer, "condition_processing");
Can we save the space by just calling the constructors:
Json_writer_object trace_wrapper(thd);
Json_writer_object trace_cond(thd, "condition_processing");
?
This applies here and in many other places.
Alternative, we could use:
Json_writer_object trace_wrapper(thd);
Json_writer_object trace_cond(trace_wrapper, "condition_processing");
.. which makes the nesting clearer (and we could also add debug safety check: it
is invalid to operate on trace_wrapper until trace_cond hasn't been end()'ed)
> --- 10.4-optimizer-trace-orig/sql/opt_range.cc
> +++ 10.4-optimizer-trace-cl/sql/opt_range.cc
>
> +void TRP_ROR_UNION::trace_basic_info(const PARAM *param,
> + Json_writer_object *trace_object) const
> +{
> + Opt_trace_context *const trace = ¶m->thd->opt_trace;
> + Json_writer* writer= trace->get_current_json();
> + trace_object->add("type", "index_roworder_union");
> + Json_writer_array ota(writer, "union_of");
The name 'ota' makes sense in MySQL codebase (where it is a contraction of
Optimizer_trace_array), but is really confusing in MariaDB codebase. Please
change everywhere to "smth_trace" or something like that (jwa in the worst
case).
> @@ -2654,12 +2833,18 @@ int SQL_SELECT::test_quick_select(THD *t
>
> if (cond)
> {
> - if ((tree= cond->get_mm_tree(¶m, &cond)))
> + {
> + Json_writer_array trace_range_summary(writer,
> + "setup_range_conditions");
> + tree= cond->get_mm_tree(¶m, &cond);
> + }
Does this ever produce anything meaningful, other than empty:
"setup_range_conditions": [],
In MySQL, the possible contents of this array are:
"impossible_condition": {
"cause": "comparison_with_null_always_false"
} /* impossible_condition */
"impossible_condition": {
"cause": "null_field_in_non_null_column"
} /* impossible_condition */
But in MariaDB we don't have this (should we?)
Also, why_use_underscores_in_value_of_cause? It is a quoted string where
spaces are allowed. MySQL seems to have figured this out at some point and have
a few cause strings using spaces.
> --- 10.4-optimizer-trace-orig/sql/opt_table_elimination.cc
> +++ 10.4-optimizer-trace-cl/sql/opt_table_elimination.cc
> @@ -522,7 +524,8 @@ eliminate_tables_for_list(JOIN *join,
> List<TABLE_LIST> *join_list,
> table_map tables_in_list,
> Item *on_expr,
> - table_map tables_used_elsewhere);
> + table_map tables_used_elsewhere,
> + Json_writer_array* eliminate_tables);
Please change the name to something indicating it's just a trace.
BR
Sergei
--
Sergei Petrunia, Software Developer
MariaDB Corporation | Skype: sergefp | Blog: http://s.petrunia.net/blog
1
1

Re: [Maria-developers] b1a4d1e4937: MDEV-16973 Application-time periods: DELETE
by Sergei Golubchik 08 Feb '19
by Sergei Golubchik 08 Feb '19
08 Feb '19
Hi, Nikita!
On Jan 31, Nikita Malyavin wrote:
> revision-id: b1a4d1e4937 (versioning-1.0.7-3-gb1a4d1e4937)
> parent(s): 4b01d3aee60
> author: Nikita Malyavin <nikitamalyavin(a)gmail.com>
> committer: Nikita Malyavin <nikitamalyavin(a)gmail.com>
> timestamp: 2019-01-30 22:54:00 +1000
> message:
>
> MDEV-16973 Application-time periods: DELETE
>
> * inject portion of time updates into mysql_delete main loop
> * triggered case emits delete+insert, no updates
> * PORTION OF `SYSTEM_TIME` is forbidden
> * `DELETE HISTORY .. FOR PORTION OF ...` is forbidden as well
> diff --git a/mysql-test/suite/period/r/delete.result b/mysql-test/suite/period/r/delete.result
> --- /dev/null
> +++ b/mysql-test/suite/period/r/delete.result
> @@ -0,0 +1,244 @@
> +create or replace table t (id int, s date, e date, period for apptime(s,e));
> +insert into t values(1, '1999-01-01', '2018-12-12');
> +insert into t values(1, '1999-01-01', '2017-01-01');
> +insert into t values(1, '2017-01-01', '2019-01-01');
> +insert into t values(2, '1998-01-01', '2018-12-12');
> +insert into t values(3, '1997-01-01', '2015-01-01');
> +insert into t values(4, '2016-01-01', '2020-01-01');
> +insert into t values(5, '2010-01-01', '2015-01-01');
> +create or replace table t1 (id int, s date, e date, period for apptime(s,e));
> +insert t1 select * from t;
> +create or replace table t2 (id int, s date, e date, period for apptime(s,e));
> +insert t2 select * from t;
> +delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
> +delete from t1 for portion of APPTIME from '2000-01-01' to '2018-01-01';
> +select * from t;
> +id s e
> +1 1999-01-01 2000-01-01
> +1 1999-01-01 2000-01-01
> +1 2018-01-01 2018-12-12
> +1 2018-01-01 2019-01-01
> +2 1998-01-01 2000-01-01
> +2 2018-01-01 2018-12-12
> +3 1997-01-01 2000-01-01
> +4 2018-01-01 2020-01-01
> +select * from t1;
> +id s e
> +1 1999-01-01 2000-01-01
> +1 1999-01-01 2000-01-01
> +1 2018-01-01 2018-12-12
> +1 2018-01-01 2019-01-01
> +2 1998-01-01 2000-01-01
> +2 2018-01-01 2018-12-12
> +3 1997-01-01 2000-01-01
> +4 2018-01-01 2020-01-01
> +select * from log_tbl;
> +id log
here (and everywhere selecting from log_tbl), better do ORDER BY id
it's quite difficult to follow the sequence of events otherwise
> +1 >DEL: 1, 1999-01-01, 2018-12-12
> +10 <INS: 1, 1999-01-01, 2000-01-01
> +11 >DEL: 1, 2017-01-01, 2019-01-01
> +12 <DEL: 1, 2017-01-01, 2019-01-01
> +13 >INS: 1, 2018-01-01, 2019-01-01
> +14 <INS: 1, 2018-01-01, 2019-01-01
> +15 >DEL: 2, 1998-01-01, 2018-12-12
> +16 <DEL: 2, 1998-01-01, 2018-12-12
> +17 >INS: 2, 1998-01-01, 2000-01-01
> +18 <INS: 2, 1998-01-01, 2000-01-01
> +19 >INS: 2, 2018-01-01, 2018-12-12
> +2 <DEL: 1, 1999-01-01, 2018-12-12
> +20 <INS: 2, 2018-01-01, 2018-12-12
> +21 >DEL: 3, 1997-01-01, 2015-01-01
> +22 <DEL: 3, 1997-01-01, 2015-01-01
> +23 >INS: 3, 1997-01-01, 2000-01-01
> +24 <INS: 3, 1997-01-01, 2000-01-01
> +25 >DEL: 4, 2016-01-01, 2020-01-01
> +26 <DEL: 4, 2016-01-01, 2020-01-01
> +27 >INS: 4, 2018-01-01, 2020-01-01
> +28 <INS: 4, 2018-01-01, 2020-01-01
> +29 >DEL: 5, 2010-01-01, 2015-01-01
> +3 >INS: 1, 1999-01-01, 2000-01-01
> +30 <DEL: 5, 2010-01-01, 2015-01-01
> +4 <INS: 1, 1999-01-01, 2000-01-01
> +5 >INS: 1, 2018-01-01, 2018-12-12
> +6 <INS: 1, 2018-01-01, 2018-12-12
> +7 >DEL: 1, 1999-01-01, 2017-01-01
> +8 <DEL: 1, 1999-01-01, 2017-01-01
> +9 >INS: 1, 1999-01-01, 2000-01-01
> +# INSERT trigger only also works
> +drop trigger tr1del_t2;
> +drop trigger tr2del_t2;
> +delete from t2 for portion of APPTIME from '2000-01-01' to '2018-01-01';
> +select * from log_tbl;
> +id log
> +1 >INS: 1, 1999-01-01, 2000-01-01
> +10 <INS: 2, 1998-01-01, 2000-01-01
> +11 >INS: 2, 2018-01-01, 2018-12-12
> +12 <INS: 2, 2018-01-01, 2018-12-12
> +13 >INS: 3, 1997-01-01, 2000-01-01
> +14 <INS: 3, 1997-01-01, 2000-01-01
> +15 >INS: 4, 2018-01-01, 2020-01-01
> +16 <INS: 4, 2018-01-01, 2020-01-01
> +2 <INS: 1, 1999-01-01, 2000-01-01
> +3 >INS: 1, 2018-01-01, 2018-12-12
> +4 <INS: 1, 2018-01-01, 2018-12-12
> +5 >INS: 1, 1999-01-01, 2000-01-01
> +6 <INS: 1, 1999-01-01, 2000-01-01
> +7 >INS: 1, 2018-01-01, 2019-01-01
> +8 <INS: 1, 2018-01-01, 2019-01-01
> +9 >INS: 2, 1998-01-01, 2000-01-01
> +# multi-table DELETE is not possible
> +delete t, t1 from t1, t for portion of apptime from '2000-01-01' to '2018-01-01';
> +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'for portion of apptime from '2000-01-01' to '2018-01-01'' at line 1
> +delete t for portion of apptime from '2000-01-01' to '2018-01-01', t1 from t, t1;
> +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'for portion of apptime from '2000-01-01' to '2018-01-01', t1 from t, t1' at line 1
> +# Here another check fails before parsing ends
> +delete t, t1 from t for portion of apptime from '2000-01-01' to '2018-01-01', t1;
> +ERROR 42S02: Unknown table 't1' in MULTI DELETE
> +delete history from t2 for portion of apptime from '2000-01-01' to '2018-01-01';
> +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'for portion of apptime from '2000-01-01' to '2018-01-01'' at line 1
> +delete from t for portion of othertime from '2000-01-01' to '2018-01-01';
> +ERROR HY000: Period `othertime` is not found in table
> +delete from t for portion of system_time from '2000-01-01' to '2018-01-01';
> +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'system_time' at line 1
> +create or replace table t (id int, str text, s date, e date,
> +period for apptime(s,e));
> +insert into t values(1, 'data', '1999-01-01', '2018-12-12');
> +insert into t values(1, 'other data', '1999-01-01', '2018-12-12');
> +insert into t values(1, 'deleted', '2000-01-01', '2018-01-01');
> +delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
> +show warnings;
> +Level Code Message
> +select * from t;
> +id str s e
> +1 data 1999-01-01 2000-01-01
> +1 data 2018-01-01 2018-12-12
> +1 other data 1999-01-01 2000-01-01
> +1 other data 2018-01-01 2018-12-12
> +drop table t1;
> +# SQL16, Part 2, 15.7 <Effect of deleting rows from base tables>,
> +# General rules, 8)b)i)
> +# If the column descriptor that corresponds to the i-th field of BR
> +# describes an identity column, a generated column, a system-time period
> +# start column, or a system-time period end column, then let V i be
> +# DEFAULT.
> +# auto_increment field is updated
> +create or replace table t (id int primary key auto_increment, s date, e date,
> +period for apptime(s, e));
> +insert into t values (default, '1999-01-01', '2018-12-12');
> +select * from t;
> +id s e
> +1 1999-01-01 2018-12-12
> +delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
> +select * from t;
> +id s e
> +2 1999-01-01 2000-01-01
> +3 2018-01-01 2018-12-12
> +truncate t;
> +# same for trigger case
> +insert into t values (default, '1999-01-01', '2018-12-12');
> +delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
> +select * from t;
> +id s e
> +2 1999-01-01 2000-01-01
> +3 2018-01-01 2018-12-12
> +select * from log_tbl;
> +id log
> +1 >DEL: 1999-01-01, 2018-12-12
> +2 <DEL: 1999-01-01, 2018-12-12
> +3 >INS: 1999-01-01, 2000-01-01
> +4 <INS: 1999-01-01, 2000-01-01
> +5 >INS: 2018-01-01, 2018-12-12
> +6 <INS: 2018-01-01, 2018-12-12
> +# auto_increment field overflow
> +create or replace table t (id tinyint auto_increment primary key,
> +s date, e date, period for apptime(s,e));
> +insert into t values(127, '1999-01-01', '2018-12-12');
> +delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
> +ERROR 22003: Out of range value for column 'id' at row 1
add select * from t here, please
> +# same for trigger case
> +delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
> +ERROR 22003: Out of range value for column 'id' at row 1
and here
and select from the log_tbl too
> +# generated columns are updated
> +create or replace table t (s date, e date,
> +xs date as (s) stored, xe date as (e) stored,
> +period for apptime(s, e));
> +insert into t values('1999-01-01', '2018-12-12', default, default);
> +select * from t;
> +s e xs xe
> +1999-01-01 2018-12-12 1999-01-01 2018-12-12
> +delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
> +select * from t;
> +s e xs xe
> +1999-01-01 2000-01-01 1999-01-01 2000-01-01
> +2018-01-01 2018-12-12 2018-01-01 2018-12-12
> +truncate t;
> +# same for trigger case
> +insert into t values('1999-01-01', '2018-12-12', default, default);
> +delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
> +select * from t;
> +s e xs xe
> +1999-01-01 2000-01-01 1999-01-01 2000-01-01
> +2018-01-01 2018-12-12 2018-01-01 2018-12-12
> +select * from log_tbl;
> +id log
> +1 >DEL: 1999-01-01, 2018-12-12
> +2 <DEL: 1999-01-01, 2018-12-12
> +3 >INS: 1999-01-01, 2000-01-01
> +4 <INS: 1999-01-01, 2000-01-01
> +5 >INS: 2018-01-01, 2018-12-12
> +6 <INS: 2018-01-01, 2018-12-12
> +# View can't be used
> +create or replace view v as select * from t;
> +delete from v for portion of p from '2000-01-01' to '2018-01-01';
> +ERROR 42S02: 'v' is a view
> +# system_time columns are updated
> +create or replace table t (
> +s date, e date,
> +row_start SYS_TYPE as row start invisible,
> +row_end SYS_TYPE as row end invisible,
> +period for apptime(s, e),
> +period for system_time (row_start, row_end)) with system versioning;
> +insert into t values('1999-01-01', '2018-12-12'),
> +('1999-01-01', '1999-12-12');
> +select row_start into @ins_time from t limit 1;
> +select * from t order by s, e;
> +s e
> +1999-01-01 1999-12-12
> +1999-01-01 2018-12-12
> +delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
> +select *, if(row_start = @ins_time, "OLD", "NEW"), check_row(row_start, row_end)
> +from t for system_time all
> +order by s, e, row_start;
> +s e if(row_start = @ins_time, "OLD", "NEW") check_row(row_start, row_end)
> +1999-01-01 1999-12-12 OLD CURRENT ROW
> +1999-01-01 2000-01-01 NEW CURRENT ROW
> +1999-01-01 2018-12-12 OLD HISTORICAL ROW
> +2018-01-01 2018-12-12 NEW CURRENT ROW
> +# same for trigger case
> +delete from t;
> +delete history from t;
> +insert into t values('1999-01-01', '2018-12-12'),
> +('1999-01-01', '1999-12-12');
> +select row_start into @ins_time from t limit 1;
> +select * from t order by s, e;
> +s e
> +1999-01-01 1999-12-12
> +1999-01-01 2018-12-12
> +delete from t for portion of apptime from '2000-01-01' to '2018-01-01';
> +select *, if(row_start = @ins_time, "OLD", "NEW"), check_row(row_start, row_end)
> +from t for system_time all
> +order by s, e, row_start;
> +s e if(row_start = @ins_time, "OLD", "NEW") check_row(row_start, row_end)
> +1999-01-01 1999-12-12 OLD CURRENT ROW
> +1999-01-01 2000-01-01 NEW CURRENT ROW
> +1999-01-01 2018-12-12 OLD HISTORICAL ROW
> +2018-01-01 2018-12-12 NEW CURRENT ROW
> +select * from log_tbl;
> +id log
> +1 >DEL: 1999-01-01, 2018-12-12
> +2 <DEL: 1999-01-01, 2018-12-12
> +3 >INS: 1999-01-01, 2000-01-01
> +4 <INS: 1999-01-01, 2000-01-01
> +5 >INS: 2018-01-01, 2018-12-12
> +6 <INS: 2018-01-01, 2018-12-12
> +create or replace database test;
> diff --git a/mysql-test/suite/versioning/t/select.test b/mysql-test/suite/versioning/t/select.test
> --- a/mysql-test/suite/versioning/t/select.test
> +++ b/mysql-test/suite/versioning/t/select.test
> @@ -107,6 +107,32 @@ for system_time as of timestamp @t0 as t;
> drop table t1;
> drop table t2;
>
> +# Query conditions check
> +
> +create or replace table t1(x int) with system versioning;
> +insert into t1 values (1);
> +delete from t1;
> +insert into t1 values (2);
> +delete from t1;
> +insert into t1 values (3);
> +delete from t1;
> +
> +select row_start into @start1 from t1 for system_time all where x = 1;
> +select row_end into @end1 from t1 for system_time all where x = 1;
> +select row_start into @start2 from t1 for system_time all where x = 2;
> +select row_end into @end2 from t1 for system_time all where x = 2;
> +select row_start into @start3 from t1 for system_time all where x = 3;
> +select row_end into @end3 from t1 for system_time all where x = 3;
> +
> +select x as ASOF_x from t1 for system_time as of @start2;
> +select x as ASOF_x from t1 for system_time as of @end2;
> +select x as FROMTO_x from t1 for system_time from @start1 to @end3;
> +select x as FROMTO_x from t1 for system_time from @end1 to @start2;
> +select x as BETWAND_x from t1 for system_time between @start1 and @end3;
> +select x as BETWAND_x from t1 for system_time between @end1 and @start2;
> +
> +drop table t1;
what does that have to do with MDEV-16973?
> +
> # Wildcard expansion on hidden fields
>
> create table t1(
> @@ -233,9 +259,9 @@ select x from t1 for system_time as of @trx_start;
> --echo ### Issue #365, bug 4 (related to #226, optimized fields)
> create or replace table t1 (i int, b int) with system versioning;
> insert into t1 values (0, 0), (0, 0);
> -select min(i) over (partition by b) as f
> -from (select i + 0 as i, b from t1) as tt
> -order by i;
> +#select min(i) over (partition by b) as f
> +#from (select i + 0 as i, b from t1) as tt
> +#order by i;
why is this?
>
> --echo ### Issue #365, bug 5 (dangling AND)
> create or replace table t1 (a int);
> diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
> --- a/sql/sql_delete.cc
> +++ b/sql/sql_delete.cc
> @@ -245,6 +245,48 @@ static bool record_should_be_deleted(THD *thd, TABLE *table, SQL_SELECT *sel,
> return false;
> }
>
> +inline
> +int TABLE::update_portion_of_time(THD *thd,
> + const vers_select_conds_t &period_conds,
> + bool *inside_period)
I don't understand why you want to keep this very much DELETE-only
functionality in the TABLE class which is used everywhere.
And what's the point of pretending it's in a common TABLE class,
if it can only be used in sql_delete.cc? I find it quite confusing :(
> +{
> + bool lcond= period_conds.field_start->val_datetime_packed(thd)
> + < period_conds.start.item->val_datetime_packed(thd);
> + bool rcond= period_conds.field_end->val_datetime_packed(thd)
> + > period_conds.end.item->val_datetime_packed(thd);
> +
> + *inside_period= !lcond && !rcond;
> + if (*inside_period)
> + return 0;
> +
> + DBUG_ASSERT(!triggers || (!triggers->has_triggers(TRG_EVENT_INSERT,
> + TRG_ACTION_BEFORE)
> + && !triggers->has_triggers(TRG_EVENT_INSERT,
> + TRG_ACTION_AFTER)
> + && !triggers->has_delete_triggers()));
> +
> + int res= 0;
> + Item *src= lcond ? period_conds.start.item : period_conds.end.item;
> + uint dst_fieldno= lcond ? s->period.end_fieldno : s->period.start_fieldno;
> +
> + store_record(this, record[1]);
> + if (likely(!res))
> + res= src->save_in_field(field[dst_fieldno], true);
> +
> + if (likely(!res))
> + res= update_generated_fields();
> +
> + if(likely(!res))
> + res= file->ha_update_row(record[1], record[0]);
> +
> + restore_record(this, record[1]);
> +
> + if (likely(!res) && lcond && rcond)
> + res= period_make_insert(period_conds.end.item,
> + field[s->period.start_fieldno]);
> +
> + return res;
> +}
>
> inline
> int TABLE::delete_row()
> @@ -672,6 +736,9 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
>
> table->mark_columns_needed_for_delete();
>
> + if (table_list->has_period())
> + table->use_all_columns();
may be even
if (table_list->has_period())
table->use_all_columns()
else
table->mark_columns_needed_for_delete();
> +
> if ((table->file->ha_table_flags() & HA_CAN_FORCE_BULK_DELETE) &&
> !table->prepare_triggers_for_delete_stmt_or_event())
> will_batch= !table->file->start_bulk_delete();
> @@ -727,6 +794,16 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
> delete_record= true;
> }
>
> + /*
> + From SQL2016, Part 2, 15.7 <Effect of deleting rows from base table>,
> + General Rules, 8), we can conclude that DELETE FOR PORTTION OF time performs
> + 0-2 INSERTS + DELETE. We can substitute INSERT+DELETE with one UPDATE, but
> + only if there are no triggers set.
> + It is also meaningless for system-versioned table
> + */
> + portion_of_time_through_update= !has_triggers
> + && !table->versioned(VERS_TIMESTAMP);
I still don't understand why you disable portion_of_time_through_update
for VERS_TIMESTAMP, but not for VERS_TRX_ID.
> +
> THD_STAGE_INFO(thd, stage_updating);
> while (likely(!(error=info.read_record())) && likely(!thd->killed) &&
> likely(!thd->is_error()))
> diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
> --- a/sql/sql_lex.cc
> +++ b/sql/sql_lex.cc
> @@ -3576,6 +3577,20 @@ void LEX::set_trg_event_type_for_tables()
> break;
> }
>
> + if (period_conditions.is_set())
> + {
> + switch (sql_command)
> + {
> + case SQLCOM_DELETE:
> + case SQLCOM_UPDATE:
> + case SQLCOM_REPLACE:
> + new_trg_event_map |= static_cast<uint8>
> + (1 << static_cast<int>(TRG_EVENT_INSERT));
I've added a helper for this recently, use
new_trg_event_map |= trg2bit(TRG_EVENT_INSERT);
> + default:
> + break;
> + }
> + }
> +
>
> /*
> Do not iterate over sub-selects, only the tables in the outermost
> diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
> --- a/sql/sql_yacc.yy
> +++ b/sql/sql_yacc.yy
> @@ -9295,6 +9296,22 @@ history_point:
> $$= Vers_history_point($1, $2);
> }
> ;
> +opt_for_portion_of_time_clause:
> + /* empty */
> + {
> + $$= false;
> + }
> + | FOR_SYM PORTION_SYM OF_SYM ident FROM history_point TO_SYM history_point
history_point allows TIMESTAMP '2010-10-10 10:10:10' and
TRANSACTION 1234.
You don't need any of that, just use a corresponding expression
rule. E.g. bit_expr, like history_point does.
> + {
> + if (unlikely(0 == strcasecmp($4.str, "SYSTEM_TIME")))
> + {
> + thd->parse_error(ER_SYNTAX_ERROR, $4.str);
no, for the error message to look correct you need to pass the pointer
into the query text. It's usually done like this:
FOR_SYM PORTION_SYM OF_SYM remember_tok_start ident FROM ...
{
...
thd->parse_error(ER_SYNTAX_ERROR, $4);
> + MYSQL_YYABORT;
> + }
> + $$= true;
> + Lex->period_conditions.init(SYSTEM_TIME_FROM_TO, $6, $8, $4);
> + }
> + ;
>
> opt_for_system_time_clause:
> /* empty */
Regards,
Sergei
Chief Architect MariaDB
and security(a)mariadb.org
2
3
Hello,
So, MDEV-16934 introduced eq_range_index_dives_limit into 10.2.8 and 10.3.0.
The default was set to 0 (which means no limit) in order to not introduce
optimizer behavior change into stable versions.
The question is: should 10.4 also have 0 by default or we can set it to some
finite limit? MySQL's default value is 10.
BR
Sergei
--
Sergei Petrunia, Software Developer
MariaDB Corporation | Skype: sergefp | Blog: http://s.petrunia.net/blog
3
2

Re: [Maria-developers] d1ccc60: MDEV-14005 Remove need for Partition Key to be part of Primary Key.
by Sergei Golubchik 05 Feb '19
by Sergei Golubchik 05 Feb '19
05 Feb '19
Hi, Alexey!
On Feb 04, Alexey Botchkov wrote:
> Hi, Sergei.
>
> Below are my reply on your comments.
> I think it's better get these agreed before I produce the new patch.
>
> > > + if ((sav_inited= f->inited) == RND)
> > > + f->ha_rnd_end();
> >
> > When can this happen?
> UPDATE statements do the rnd_init() in order to access through rnd_pos,
> and when they need to do the table scan.
So, UPDATE does rnd_init(), then rnd_pos(), then update_row(), and
at that point you need to look up the key in all partitions, so you need
index_read()?
Could you, please, add a comment here to explain this sequence of
events?
> > > +int ha_partition::check_uniques_insert(uchar *buf,
> > > ...
> > > + {
> >
> > can you use key_copy() here?
> Difficult to use here key_copy() as it is.
> Here i need to get the record from the arbitrary buffer,
> while key_copy() gets it from the table->record[0].
> It seems weird that the key_copy() actually has the 'from_record
> argument, but only uses it to check the null bit.
May be, fix it?
Meaning, adding 'offset' argument to Field::get_key_image(), I suppose.
> > > @@ -283,6 +283,14 @@ class partition_info : public Sql_alloc
> ..
> > > + uint n_uniques_to_check;
> > > + uchar *unique_key_buf[2];
> >
> > Is it safe? Isn't partition_info shared between many handlers
> > so that every handler can only use it read-only?
> > it's not quite clear to me. it seems that every TABLE has its own
> > partition_info. But cloned handlers all share the same partition_info.
>
> You're right, cloned handlers all share the same part_info. I didn't notice
> this.
> So i guess i should move the uniue_key_buf[2] to the class ha_partition.
> Do you know BTW if we ever use cloned handlers to write/update?
I don't know, sorry.
Regards,
Sergei
Chief Architect MariaDB
and security(a)mariadb.org
1
0

Re: [Maria-developers] f93af1f93e2: MDEV-12836 Avoid table rebuild when adding or removing of auto_increment settings
by Sergei Golubchik 05 Feb '19
by Sergei Golubchik 05 Feb '19
05 Feb '19
Hi, Eugene!
On Feb 04, Eugene Kosov wrote:
> revision-id: f93af1f93e2 (mariadb-10.3.12-19-gf93af1f93e2)
> parent(s): d4144c8e010
> author: Eugene Kosov <claprix(a)yandex.ru>
> committer: Eugene Kosov <claprix(a)yandex.ru>
> timestamp: 2019-01-23 19:32:02 +0300
> message:
>
> MDEV-12836 Avoid table rebuild when adding or removing of auto_increment settings
>
> ALTER_AUTO_INC: this flag indicates that ALTER operation
> adds or removes AUTO_INCREMENT from some field
>
> Field::is_equal(): now sometime returns IS_EQUAL_BUT_AUTO_INC
> which means that only AUTO_INCREMENT was changed for that field
You enable instant alter for adding auto_inc. This is not what
Valerii asked for in MDEV-12836, and for a reason. Consider this test
case:
create table t1 (a int unique);
insert t1 values (NULL),(NULL),(NULL);
alter table t1 modify a int auto_increment;
here `alter table` modifies the data, actually generating auto-inc
values for all NULL columns. I don't think you can do that instantly.
You can only remove auto-inc instantly. And for that you don't need any
big changes, just modify Field::is_equal() to allow it:
diff --git a/sql/field.cc b/sql/field.cc
--- a/sql/field.cc
+++ b/sql/field.cc
@@ -3507,7 +3507,7 @@ uint Field_new_decimal::is_equal(Create_field *new_field)
return ((new_field->type_handler() == type_handler()) &&
((new_field->flags & UNSIGNED_FLAG) ==
(uint) (flags & UNSIGNED_FLAG)) &&
- ((new_field->flags & AUTO_INCREMENT_FLAG) ==
+ ((new_field->flags & AUTO_INCREMENT_FLAG) <=
(uint) (flags & AUTO_INCREMENT_FLAG)) &&
(new_field->length == max_display_length()) &&
(new_field->decimals == dec));
@@ -9508,7 +9508,7 @@ uint Field_num::is_equal(Create_field *new_field)
return ((new_field->type_handler() == type_handler()) &&
((new_field->flags & UNSIGNED_FLAG) ==
(uint) (flags & UNSIGNED_FLAG)) &&
- ((new_field->flags & AUTO_INCREMENT_FLAG) ==
+ ((new_field->flags & AUTO_INCREMENT_FLAG) <=
(uint) (flags & AUTO_INCREMENT_FLAG)) &&
(new_field->pack_length == pack_length()));
}
And this should be enough.
Regards,
Sergei
Chief Architect MariaDB
and security(a)mariadb.org
2
3

Re: [Maria-developers] c39f74ce0d9: MDEV-16974 Application period tables: UPDATE
by Sergei Golubchik 04 Feb '19
by Sergei Golubchik 04 Feb '19
04 Feb '19
Hi, Nikita!
See comments below
On Jan 06, Nikita Malyavin wrote:
> revision-id: c39f74ce0d9 (versioning-1.0.7-4-gc39f74ce0d9)
> parent(s): fd18ed07d65
> author: Nikita Malyavin <nikitamalyavin(a)gmail.com>
> committer: Nikita Malyavin <nikitamalyavin(a)gmail.com>
> timestamp: 2018-12-03 13:19:18 +1000
> message:
>
> MDEV-16974 Application period tables: UPDATE
>
> diff --git a/mysql-test/suite/period/r/delete.result b/mysql-test/suite/period/r/delete.result
> index 30c9220d6f9..ccb8663bf72 100644
> --- a/mysql-test/suite/period/r/delete.result
> +++ b/mysql-test/suite/period/r/delete.result
> @@ -83,6 +83,9 @@ log
> <INS: 3, 1997-01-01, 2000-01-01
> >INS: 4, 2018-01-01, 2020-01-01
> <INS: 4, 2018-01-01, 2020-01-01
> +# multi-table DELETE is prohibited
> +delete t, t1 from t for portion of apptime from '2000-01-01' to '2018-01-01', t1;
> +ERROR HY000: PORTION OF time is not allowed here
why not ER_PARSE_ERROR?
> delete history from t2 for portion of apptime from '2000-01-01' to '2018-01-01';
> ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
> delete from t for portion of othertime from '2000-01-01' to '2018-01-01';
> diff --git a/mysql-test/suite/period/r/update.result b/mysql-test/suite/period/r/update.result
> new file mode 100644
> index 00000000000..1f59102c131
> --- /dev/null
> +++ b/mysql-test/suite/period/r/update.result
> @@ -0,0 +1,244 @@
> +create or replace table t (id int, s date, e date, period for apptime(s,e));
> +insert into t values(1, '1999-01-01', '2018-12-12');
> +insert into t values(1, '1999-01-01', '2017-01-01');
> +insert into t values(1, '2017-01-01', '2019-01-01');
> +insert into t values(2, '1998-01-01', '2018-12-12');
> +insert into t values(3, '1997-01-01', '2015-01-01');
> +insert into t values(4, '2016-01-01', '2020-01-01');
I'd insert also, say, (5, '2010-01-01', '2015-01-01').
> +create or replace table t1 (id int, s date, e date, period for apptime(s,e));
> +insert t1 select * from t;
> +create or replace table t2 (id int, s date, e date, period for apptime(s,e));
> +insert t2 select * from t;
> +update t for portion of apptime from '2000-01-01' to '2018-01-01'
> + set id=id + 5;
> +select * from t;
same comment about unsorted selects
> +id s e
> +6 2000-01-01 2018-01-01
> +6 2000-01-01 2017-01-01
> +6 2017-01-01 2018-01-01
> +7 2000-01-01 2018-01-01
> +8 2000-01-01 2015-01-01
> +9 2016-01-01 2018-01-01
> +1 1999-01-01 2000-01-01
> +1 2018-01-01 2018-12-12
> +1 1999-01-01 2000-01-01
> +1 2018-01-01 2019-01-01
> +2 1998-01-01 2000-01-01
> +2 2018-01-01 2018-12-12
> +3 1997-01-01 2000-01-01
> +4 2018-01-01 2020-01-01
> +# Check triggers
> +update t1 for portion of apptime from '2000-01-01' to '2018-01-01'
> + set id=id + 5;
> +select * from t1 order by id, s, e;
> +id s e
> +1 1999-01-01 2000-01-01
> +1 1999-01-01 2000-01-01
> +1 2018-01-01 2018-12-12
> +1 2018-01-01 2019-01-01
> +2 1998-01-01 2000-01-01
> +2 2018-01-01 2018-12-12
> +3 1997-01-01 2000-01-01
> +4 2018-01-01 2020-01-01
> +6 2000-01-01 2017-01-01
> +6 2000-01-01 2018-01-01
> +6 2017-01-01 2018-01-01
> +7 2000-01-01 2018-01-01
> +8 2000-01-01 2015-01-01
> +9 2016-01-01 2018-01-01
> +select * from log_tbl;
> +log
> +>UPD: 1, 1999-01-01, 2018-12-12 -> 6, 2000-01-01, 2018-01-01
> +<UPD: 1, 1999-01-01, 2018-12-12 -> 6, 2000-01-01, 2018-01-01
> +>INS: 1, 1999-01-01, 2000-01-01
> +<INS: 1, 1999-01-01, 2000-01-01
> +>INS: 1, 2018-01-01, 2018-12-12
> +<INS: 1, 2018-01-01, 2018-12-12
> +>UPD: 1, 1999-01-01, 2017-01-01 -> 6, 2000-01-01, 2017-01-01
> +<UPD: 1, 1999-01-01, 2017-01-01 -> 6, 2000-01-01, 2017-01-01
> +>INS: 1, 1999-01-01, 2000-01-01
> +<INS: 1, 1999-01-01, 2000-01-01
> +>UPD: 1, 2017-01-01, 2019-01-01 -> 6, 2017-01-01, 2018-01-01
> +<UPD: 1, 2017-01-01, 2019-01-01 -> 6, 2017-01-01, 2018-01-01
> +>INS: 1, 2018-01-01, 2019-01-01
> +<INS: 1, 2018-01-01, 2019-01-01
> +>UPD: 2, 1998-01-01, 2018-12-12 -> 7, 2000-01-01, 2018-01-01
> +<UPD: 2, 1998-01-01, 2018-12-12 -> 7, 2000-01-01, 2018-01-01
> +>INS: 2, 1998-01-01, 2000-01-01
> +<INS: 2, 1998-01-01, 2000-01-01
> +>INS: 2, 2018-01-01, 2018-12-12
> +<INS: 2, 2018-01-01, 2018-12-12
> +>UPD: 3, 1997-01-01, 2015-01-01 -> 8, 2000-01-01, 2015-01-01
> +<UPD: 3, 1997-01-01, 2015-01-01 -> 8, 2000-01-01, 2015-01-01
> +>INS: 3, 1997-01-01, 2000-01-01
> +<INS: 3, 1997-01-01, 2000-01-01
> +>UPD: 4, 2016-01-01, 2020-01-01 -> 9, 2016-01-01, 2018-01-01
> +<UPD: 4, 2016-01-01, 2020-01-01 -> 9, 2016-01-01, 2018-01-01
> +>INS: 4, 2018-01-01, 2020-01-01
> +<INS: 4, 2018-01-01, 2020-01-01
> +# INSERT trigger only also works
> +drop trigger tr1upd_t2;
> +drop trigger tr2upd_t2;
> +update t2 for portion of apptime from '2000-01-01' to '2018-01-01'
> + set id=id + 5;
> +select * from t2 order by id, s, e;
> +id s e
> +1 1999-01-01 2000-01-01
> +1 1999-01-01 2000-01-01
> +1 2018-01-01 2018-12-12
> +1 2018-01-01 2019-01-01
> +2 1998-01-01 2000-01-01
> +2 2018-01-01 2018-12-12
> +3 1997-01-01 2000-01-01
> +4 2018-01-01 2020-01-01
> +6 2000-01-01 2017-01-01
> +6 2000-01-01 2018-01-01
> +6 2017-01-01 2018-01-01
> +7 2000-01-01 2018-01-01
> +8 2000-01-01 2015-01-01
> +9 2016-01-01 2018-01-01
> +select * from log_tbl;
> +log
> +>INS: 1, 1999-01-01, 2000-01-01
> +<INS: 1, 1999-01-01, 2000-01-01
> +>INS: 1, 2018-01-01, 2018-12-12
> +<INS: 1, 2018-01-01, 2018-12-12
> +>INS: 1, 1999-01-01, 2000-01-01
> +<INS: 1, 1999-01-01, 2000-01-01
> +>INS: 1, 2018-01-01, 2019-01-01
> +<INS: 1, 2018-01-01, 2019-01-01
> +>INS: 2, 1998-01-01, 2000-01-01
> +<INS: 2, 1998-01-01, 2000-01-01
> +>INS: 2, 2018-01-01, 2018-12-12
> +<INS: 2, 2018-01-01, 2018-12-12
> +>INS: 3, 1997-01-01, 2000-01-01
> +<INS: 3, 1997-01-01, 2000-01-01
> +>INS: 4, 2018-01-01, 2020-01-01
> +<INS: 4, 2018-01-01, 2020-01-01
> +select * from t for portion of apptime from 0 to 1 for system_time all;
> +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
should be "right syntax to use near 'for portion of apptime from ...'"
> +update t for portion of apptime from 0 to 1 for system_time all set id=1;
> +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'set id=1' at line 1
why not "right syntax to use near 'for system_time all ...'" ?
> +# Modifying period start/end fields is forbidden.
> +# SQL17:
fix the reference, please
(SQL:2016, Part 2, section ... paragraph ...)
> +# Neither BSTARTCOL nor BENDCOL shall be an explicit <object column>
> +# contained in the <set clause list>.
> +update t for portion of apptime from '2000-01-01' to '2018-01-01'
> + set id= id + 5, s=subdate(s, 5), e=adddate(e, 5);
> +ERROR HY000: Column `s` used in period `apptime` specified in update SET list
> +# Precision timestamps
> +create or replace table t (id int, s timestamp(5), e timestamp(5),
> +period for apptime(s,e));
> +insert into t values(1, '1999-01-01', '2018-12-12');
> +insert into t values(1, '1999-01-01', '2017-01-01');
> +update t for portion of apptime from '2000-01-01 00:00:00.00015'
> + to '2018-01-01 12:34:56.31415'
> + set id= id + 5;
> +select * from t;
> +id s e
> +6 2000-01-01 00:00:00.00015 2018-01-01 12:34:56.31415
> +6 2000-01-01 00:00:00.00015 2017-01-01 00:00:00.00000
> +1 1999-01-01 00:00:00.00000 2000-01-01 00:00:00.00015
> +1 2018-01-01 12:34:56.31415 2018-12-12 00:00:00.00000
> +1 1999-01-01 00:00:00.00000 2000-01-01 00:00:00.00015
> +# Strings
> +create or replace table t (id int, str text, s date, e date,
> +period for apptime(s,e));
> +insert into t values(1, 'data', '1999-01-01', '2018-12-12');
> +insert into t values(1, 'other data', '1999-01-01', '2018-12-12');
> +update t for portion of apptime from '2000-01-01' to '2018-01-01'
> + set id= id + 5;
> +select * from t;
> +id str s e
> +6 data 2000-01-01 2018-01-01
> +6 other data 2000-01-01 2018-01-01
> +1 data 1999-01-01 2000-01-01
> +1 data 2018-01-01 2018-12-12
> +1 other data 1999-01-01 2000-01-01
> +1 other data 2018-01-01 2018-12-12
> +# multi-table UPDATE is prohibited
> +create or replace table t1(x int);
> +update t for portion of apptime from '2000-01-01' to '2018-01-01', t1
> +set t.id= t.id + 5;
> +ERROR HY000: PORTION OF time is not allowed here
why not ER_PARSE_ERROR?
> +update t1 set x= (select id from t for portion of apptime from '2000-01-01' to '2018-01-01');
> +ERROR HY000: PORTION OF time is not allowed here
why not ER_PARSE_ERROR?
> +# SQL17:
fix the reference, please
> +# Let FROMVAL be <point in time 1>. FROMVAL shall not generally contain a
> +# reference to a column of T or a <routine invocation>
> +# whose subject routine is an SQL-invoked routine that
> +# is possibly non-deterministic or that possibly modifies SQL-data.
> +# ...Same for <point in time 2> (TOVAL)
> +update t for portion of apptime from 5*(5+s) to 1 set t.id= t.id + 5;
> +ERROR HY000: Values in range FOR PORTION OF `apptime` should be constant expressions
> +update t for portion of apptime from 1 to e set t.id= t.id + 5;
> +ERROR HY000: Values in range FOR PORTION OF `apptime` should be constant expressions
> +set @s= '2000-01-01';
> +set @e= '2018-01-01';
> +create or replace function f() returns date return @e;
> +create or replace function g() returns date not deterministic return @e;
> +create or replace function h() returns date deterministic return @e;
> +update t for portion of apptime from @s to f() set t.id= t.id + 5;
> +ERROR HY000: Values in range FOR PORTION OF `apptime` should be constant expressions
> +update t for portion of apptime from @s to g() set t.id= t.id + 5;
> +ERROR HY000: Values in range FOR PORTION OF `apptime` should be constant expressions
> +# success
> +update t for portion of apptime from @s to h() set t.id= t.id + 5;
> +# select value is cached
> +update t for portion of apptime from (select s from t2 limit 1) to h() set t.id= t.id + 5;
> +# auto_inrement field is updated
> +create or replace table t (id int primary key auto_increment, x int,
> +s date, e date, period for apptime(s, e));
> +insert into t values (default, 1, '1999-01-01', '2018-12-12');
> +update t for portion of apptime from '2000-01-01' to '2018-01-01' set x= x + 5;
> +select * from t;
> +id x s e
> +1 6 2000-01-01 2018-01-01
> +2 1 1999-01-01 2000-01-01
> +3 1 2018-01-01 2018-12-12
> +truncate t;
> +insert into t values (default, 1, '1999-01-01', '2018-12-12');
> +update t for portion of apptime from '2000-01-01' to '2018-01-01' set x= 1;
> +select * from t;
> +id x s e
> +1 1 2000-01-01 2018-01-01
> +2 1 1999-01-01 2000-01-01
> +3 1 2018-01-01 2018-12-12
> +# generated columns are updated
> +create or replace table t (x int, s date, e date,
> +xs date as (s) stored, xe date as (e) stored,
> +period for apptime(s, e));
> +insert into t values(1, '1999-01-01', '2018-12-12', default, default);
> +select * from t;
> +x s e xs xe
> +1 1999-01-01 2018-12-12 1999-01-01 2018-12-12
> +update t for portion of apptime from '2000-01-01' to '2018-01-01' set x= x + 5;
> +select *, xs=s and xe=e from t;
> +x s e xs xe xs=s and xe=e
> +6 2000-01-01 2018-01-01 2000-01-01 2018-01-01 1
> +1 1999-01-01 2000-01-01 1999-01-01 2000-01-01 1
> +1 2018-01-01 2018-12-12 2018-01-01 2018-12-12 1
> +# system_time columns are updated
> +create or replace table t (x int, s date, e date,
> +row_start SYS_TYPE as row start invisible,
> +row_end SYS_TYPE as row end invisible,
a bit strange to use invisible in tests for strict standard compliance :)
but ok, whatever
> +period for apptime(s, e),
> +period for system_time(row_start, row_end)) with system versioning;
> +insert into t values(1, '1999-01-01', '2018-12-12'),
> +(2, '1999-01-01', '1999-12-12');
> +select row_start into @ins_time from t limit 1;
> +select * from t order by x, s, e;
> +x s e
> +1 1999-01-01 2018-12-12
> +2 1999-01-01 1999-12-12
> +update t for portion of apptime from '2000-01-01' to '2018-01-01' set x= x + 5;
> +select *, if(row_start = @ins_time, "OLD", "NEW"), check_row(row_start, row_end)
> +from t for system_time all
> +order by x, s, e, row_start;
> +x s e if(row_start = @ins_time, "OLD", "NEW") check_row(row_start, row_end)
> +1 1999-01-01 2000-01-01 NEW CURRENT ROW
> +1 1999-01-01 2018-12-12 OLD HISTORICAL ROW
> +1 2018-01-01 2018-12-12 NEW CURRENT ROW
> +2 1999-01-01 1999-12-12 OLD CURRENT ROW
> +6 2000-01-01 2018-01-01 NEW CURRENT ROW
> +create or replace database test;
> diff --git a/mysql-test/suite/period/t/delete.test b/mysql-test/suite/period/t/delete.test
> index b34de6c37e7..39a68959523 100644
> --- a/mysql-test/suite/period/t/delete.test
> +++ b/mysql-test/suite/period/t/delete.test
> @@ -34,6 +34,10 @@ drop trigger tr2del_t2;
> delete from t2 for portion of APPTIME from '2000-01-01' to '2018-01-01';
> select * from log_tbl;
>
> +--echo # multi-table DELETE is prohibited
> +--error ER_PORTION_OF_TIME_NOT_ALLOWED
> +delete t, t1 from t for portion of apptime from '2000-01-01' to '2018-01-01', t1;
move this into your MDEV-16973 commit is possible, please,
> +
> --error ER_PARSE_ERROR
> delete history from t2 for portion of apptime from '2000-01-01' to '2018-01-01';
>
> diff --git a/mysql-test/suite/period/t/update.opt b/mysql-test/suite/period/t/update.opt
> new file mode 100644
> index 00000000000..92a881bef67
> --- /dev/null
> +++ b/mysql-test/suite/period/t/update.opt
> @@ -0,0 +1 @@
> +--explicit_defaults_for_timestamp
Why?
it's best to avoid non-default command line options, unless you actually
test this particular option. If you just need it for convenience - don't.
every non-default command line options means a server restart, and they're slow.
write DEFAULT NULL explicitly when needed.
> diff --git a/sql/share/errmsg-utf8.txt b/sql/share/errmsg-utf8.txt
> index 7ceff1a215e..c5a359b09e8 100644
> --- a/sql/share/errmsg-utf8.txt
> +++ b/sql/share/errmsg-utf8.txt
> @@ -7951,3 +7951,12 @@ ER_PERIOD_NOT_FOUND
>
> ER_PERIOD_SYSTEM_TIME_NOT_ALLOWED
> eng "period SYSTEM_TIME is not allowed here"
> +
> +ER_PORTION_OF_TIME_NOT_ALLOWED
> + eng "PORTION OF time is not allowed here"
> +
> +ER_PERIOD_PORTION_OF_TIME_CONSTANT
> + eng "Values in range FOR PORTION OF %`s should be constant expressions"
Reuse ER_NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR please. Like
ER_NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR -> ER_NOT_CONSTANT_EXPRESSION
eng "Expression in RANGE/LIST VALUES must be constant" ->
eng "Expression in %s must be constant"
and in mysql.h
#define ER_NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR ER_NOT_CONSTANT_EXPRESSION
> +
> +ER_PERIOD_COLUMNS_UPDATED
> + eng "Column %`s used in period %`s specified in update SET list"
> diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
> index 7a18ac82236..93d2f417a50 100644
> --- a/sql/sql_lex.cc
> +++ b/sql/sql_lex.cc
> @@ -1343,13 +1343,16 @@ int Lex_input_stream::lex_token(YYSTYPE *yylval, THD *thd)
> /*
> * Additional look-ahead to resolve doubtful cases like:
> * SELECT ... FOR UPDATE
> - * SELECT ... FOR SYSTEM_TIME ... .
> + * SELECT ... FOR SYSTEM_TIME ...
> + * SELECT ... FOR PORTION OF ... .
Can you show an example of the statement where you need a second look-ahead
to distinguish between FOR PORTION OF and FOR SYSTEM_TIME or FOR UPDATE ?
> */
> token= lex_one_token(yylval, thd);
> add_digest_token(token, yylval);
> switch(token) {
> case SYSTEM_TIME_SYM:
> return FOR_SYSTEM_TIME_SYM;
> + case PORTION:
> + return FOR_PORTION_SYM;
> default:
> /*
> Save the token following 'FOR_SYM'
> diff --git a/sql/sql_base.cc b/sql/sql_base.cc
> index 220fcf6cc34..44989b20fae 100644
> --- a/sql/sql_base.cc
> +++ b/sql/sql_base.cc
> @@ -8121,6 +8121,12 @@ int setup_conds(THD *thd, TABLE_LIST *tables, List<TABLE_LIST> &leaves,
>
> for (table= tables; table; table= table->next_local)
> {
> + if (table->has_period() && !thd->lex->portion_of_time_applicable())
> + {
> + my_error(ER_PORTION_OF_TIME_NOT_ALLOWED, MYF(0));
> + goto err_no_arena;
> + }
I don't understand that either, why do you need to check the correct syntax
in setup_conds() ?
> +
> if (select_lex == thd->lex->first_select_lex() &&
> select_lex->first_cond_optimization &&
> table->merged_for_insert &&
> diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
> index e76eb729536..4726557d762 100644
> --- a/sql/sql_delete.cc
> +++ b/sql/sql_delete.cc
> @@ -752,6 +752,10 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
> delete_record= true;
> }
>
> + /* SQL standard defines DELETE FOR PORTTION OF time as 0-2 INSERTS + DELETE
> + * We can substitute INSERT+DELETE with one UPDATE, but only if there are
> + * no triggers set. It is also meaningless for system-versioned table
> + */
1. format multi-line comments like everywhere in the code, please
2. every time you refer to the standard, say where the standard says that
3. move this comment in your MDEV-16973 commit, if possible
> portion_of_time_through_update= !has_period_triggers
> && !table->versioned(VERS_TIMESTAMP);
>
> diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
> index 0c368b40836..10535326ce5 100644
> --- a/sql/sql_yacc.yy
> +++ b/sql/sql_yacc.yy
> @@ -11910,12 +11911,13 @@ join_table_parens:
>
>
> table_primary_ident:
> - table_ident opt_use_partition opt_for_system_time_clause
> + table_ident opt_use_partition
> + opt_for_portion_of_time_clause opt_for_system_time_clause
Oh, no. FOR PORTION OF has very few well defined places where the standard
allows it. Don't put it into the generic used-everywhere table_primary_ident.
That's why you needed FOR_PORTION_SYM and a second look-ahead.
> opt_table_alias_clause opt_key_definition
> {
> SELECT_LEX *sel= Select;
> sel->table_join_options= 0;
> - if (!($$= Select->add_table_to_list(thd, $1, $4,
> + if (!($$= Select->add_table_to_list(thd, $1, $5,
> Select->get_table_join_options(),
> YYPS->m_lock_type,
> YYPS->m_mdl_type,
> diff --git a/sql/table.h b/sql/table.h
> index 1b897c1b4c6..f104f4fc99c 100644
> --- a/sql/table.h
> +++ b/sql/table.h
> @@ -2420,7 +2423,7 @@ struct TABLE_LIST
>
> bool has_period() const
> {
> - return period_conditions.name;
> + return period_conditions.is_set();
why?
> }
>
> /**
>
> diff --git a/sql/table.cc b/sql/table.cc
> index 8f1c94dd633..93908a9e2e3 100644
> --- a/sql/table.cc
> +++ b/sql/table.cc
> @@ -6456,7 +6456,7 @@ void TABLE::mark_columns_needed_for_delete(bool with_period)
> retrieve the row again.
> */
>
> -void TABLE::mark_columns_needed_for_update()
> +void TABLE::mark_columns_needed_for_update(bool with_period)
just like in the delete case, I think this is the caller's problem
> {
> DBUG_ENTER("TABLE::mark_columns_needed_for_update");
> bool need_signal= false;
> @@ -7870,19 +7871,41 @@ static int period_make_insert(TABLE *table, Item *src, Field *dst)
> }
>
> +int TABLE::cut_fields_for_portion_of_time(THD *thd, const vers_select_conds_t &period_conds)
looks like something that belongs to sql_update.cc, not TABLE
> +{
> + bool lcond= period_conds.field_start->val_datetime_packed(thd)
> + < period_conds.start.item->val_datetime_packed(thd);
> + bool rcond= period_conds.field_end->val_datetime_packed(thd)
> + > period_conds.end.item->val_datetime_packed(thd);
> +
> + Field *start_field= field[s->period.start_fieldno];
> + Field *end_field= field[s->period.end_fieldno];
> +
> + int res= 0;
> + if (lcond && !start_field->has_explicit_value())
> + res= period_conds.start.item->save_in_field(start_field, true);
> +
> + if (likely(!res) && rcond && !end_field->has_explicit_value())
> + res= period_conds.end.item->save_in_field(end_field, true);
> +
> + return res;
> +}
> +
> -int TABLE::update_portion_of_time(THD *thd, vers_select_conds_t &period_conds,
> +int TABLE::update_portion_of_time(THD *thd, const vers_select_conds_t &period_conds,
> bool *inside_period)
looks like something that belongs to sql_delete.cc, not TABLE
> {
> bool lcond= period_conds.field_start->val_datetime_packed(thd)
> @@ -7917,7 +7940,8 @@ int TABLE::update_portion_of_time(THD *thd, vers_select_conds_t &period_conds,
> return res;
> }
>
> -int TABLE::insert_portion_of_time(THD *thd, vers_select_conds_t &period_conds)
> +int TABLE::insert_portion_of_time(THD *thd, const vers_select_conds_t &period_conds,
> + ha_rows &rows_inserted)
pointers or _constant_ references. Here, rows_inserted must be a pointer.
> {
> bool lcond= period_conds.field_start->val_datetime_packed(thd)
> < period_conds.start.item->val_datetime_packed(thd);
> diff --git a/sql/sql_update.cc b/sql/sql_update.cc
> index 27bd6f04670..d5240f5524e 100644
> --- a/sql/sql_update.cc
> +++ b/sql/sql_update.cc
> @@ -177,6 +178,21 @@ static bool check_fields(THD *thd, List<Item> &items, bool update_view)
> f->set_has_explicit_value();
> }
> }
> +
> + if (thd->lex->sql_command == SQLCOM_UPDATE && table->has_period())
what else can sql_command be here? SQLCOM_UPDATE_MULTI?
SQLCOM_UPDATE_MULTI cannot have table->has_period().
> + {
> + for (List_iterator_fast<Item> it(items); (item=it++);)
> + {
> + Lex_ident name(item->name);
> + vers_select_conds_t &period= table->period_conditions;
> + if (name.streq(period.field_start->name)
> + || name.streq(period.field_end->name))
Don't compare names, compare fields.
The field to be updated is, I guess, item->field_for_view_update()->field
> + {
> + my_error(ER_PERIOD_COLUMNS_UPDATED, MYF(0), name.str, period.name.str);
> + return true;
> + }
> + }
> + }
> return FALSE;
> }
>
> @@ -323,6 +339,7 @@ int mysql_update(THD *thd,
> List<Item> all_fields;
> killed_state killed_status= NOT_KILLED;
> bool has_triggers, binlog_is_row, do_direct_update= FALSE;
> + bool has_period_triggers= false;
same as for sql_delete.cc, one has_triggers is enough, second has_period_triggers
is quite confusing.
> Update_plan query_plan(thd->mem_root);
> Explain_update *explain;
> TABLE_LIST *update_source_table;
> @@ -880,14 +905,25 @@ int mysql_update(THD *thd,
> explain->tracker.on_record_after_where();
> store_record(table,record[1]);
>
> + if (table_list->has_period())
> + table->cut_fields_for_portion_of_time(thd, table_list->period_conditions);
1. this should be done after BEFORE triggers
(see SQL:2016, part 2, 15.13 "Effect of replacing rows in base tables")
2. why in cut_fields_for_portion_of_time() you check for has_explicit_value()?
fields cannot have explicit values there, you've checked that in check_fields().
> +
> if (fill_record_n_invoke_before_triggers(thd, table, fields, values, 0,
> TRG_EVENT_UPDATE))
> break; /* purecov: inspected */
>
> found++;
>
> - if (!can_compare_record || compare_record(table))
> + bool record_was_same= false;
> + bool need_update= !can_compare_record || compare_record(table) ||
> + thd->lex->sql_command == SQLCOM_DELETE;
why sql_command would be SQLCOM_DELETE here?
> +
> + if (need_update)
> {
> + if (table->versioned(VERS_TIMESTAMP) &&
> + thd->lex->sql_command == SQLCOM_DELETE)
> + table->vers_update_end();
> +
> if (table->default_field && table->update_default_fields(1, ignore))
> {
> error= 1;
> @@ -994,6 +1032,13 @@ int mysql_update(THD *thd,
> break;
> }
>
> + if (need_update && !record_was_same && table_list->has_period())
I suspect this should happen even if record_was_same. The standard never says
"if new values are the same as old values, don't update anything"
> + {
> + restore_record(table, record[1]);
> + table->insert_portion_of_time(thd, table_list->period_conditions,
> + rows_inserted);
> + }
> +
> if (!--limit && using_limit)
> {
> /*
Regards,
Sergei
Chief Architect MariaDB
and security(a)mariadb.org
2
4