diff options
Diffstat (limited to 'mysql-test/r')
94 files changed, 17714 insertions, 186 deletions
diff --git a/mysql-test/r/1st.result b/mysql-test/r/1st.result index 792d9eaf2f1..a948031ea2e 100644 --- a/mysql-test/r/1st.result +++ b/mysql-test/r/1st.result @@ -7,6 +7,7 @@ performance_schema test show tables in mysql; Tables_in_mysql +column_stats columns_priv db event @@ -17,6 +18,7 @@ help_keyword help_relation help_topic host +index_stats ndb_binlog_index plugin proc @@ -24,6 +26,7 @@ procs_priv proxies_priv servers slow_log +table_stats tables_priv time_zone time_zone_leap_second diff --git a/mysql-test/r/alter_table.result b/mysql-test/r/alter_table.result index 624cc7afa98..b2cfd079ff5 100644 --- a/mysql-test/r/alter_table.result +++ b/mysql-test/r/alter_table.result @@ -176,12 +176,12 @@ create table t1 (a int, b int); alter table t1 add unique (a,b), add key (b); 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 1 a A 3 NULL NULL YES BTREE +t1 0 a 2 b A 300 NULL NULL YES BTREE t1 1 b 1 b A 100 NULL NULL YES BTREE analyze table t1; Table Op Msg_type Msg_text -test.t1 analyze status OK +test.t1 analyze status Table is already up to date 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 3 NULL NULL YES BTREE diff --git a/mysql-test/r/alter_table_mdev539_maria.result b/mysql-test/r/alter_table_mdev539_maria.result new file mode 100644 index 00000000000..703908825d2 --- /dev/null +++ b/mysql-test/r/alter_table_mdev539_maria.result @@ -0,0 +1,252 @@ +# +set @@storage_engine= Aria; +# +# mdev-539: fast build of unique/primary indexes for MyISAM/Aria +# +DROP DATABASE IF EXISTS dbt3_s001; +CREATE DATABASE dbt3_s001; +use dbt3_s001; +drop index `primary` on lineitem; +show create table lineitem; +Table Create Table +lineitem CREATE TABLE `lineitem` ( + `l_orderkey` int(11) NOT NULL DEFAULT '0', + `l_partkey` int(11) DEFAULT NULL, + `l_suppkey` int(11) DEFAULT NULL, + `l_linenumber` int(11) NOT NULL DEFAULT '0', + `l_quantity` double DEFAULT NULL, + `l_extendedprice` double DEFAULT NULL, + `l_discount` double DEFAULT NULL, + `l_tax` double DEFAULT NULL, + `l_returnflag` char(1) DEFAULT NULL, + `l_linestatus` char(1) DEFAULT NULL, + `l_shipDATE` date DEFAULT NULL, + `l_commitDATE` date DEFAULT NULL, + `l_receiptDATE` date DEFAULT NULL, + `l_shipinstruct` char(25) DEFAULT NULL, + `l_shipmode` char(10) DEFAULT NULL, + `l_comment` varchar(44) DEFAULT NULL, + KEY `i_l_shipdate` (`l_shipDATE`), + KEY `i_l_suppkey_partkey` (`l_partkey`,`l_suppkey`), + KEY `i_l_partkey` (`l_partkey`), + KEY `i_l_suppkey` (`l_suppkey`), + KEY `i_l_receiptdate` (`l_receiptDATE`), + KEY `i_l_orderkey` (`l_orderkey`), + KEY `i_l_orderkey_quantity` (`l_orderkey`,`l_quantity`), + KEY `i_l_commitdate` (`l_commitDATE`) +) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1 +alter table lineitem add primary key (l_orderkey, l_linenumber); +show create table lineitem; +Table Create Table +lineitem CREATE TABLE `lineitem` ( + `l_orderkey` int(11) NOT NULL DEFAULT '0', + `l_partkey` int(11) DEFAULT NULL, + `l_suppkey` int(11) DEFAULT NULL, + `l_linenumber` int(11) NOT NULL DEFAULT '0', + `l_quantity` double DEFAULT NULL, + `l_extendedprice` double DEFAULT NULL, + `l_discount` double DEFAULT NULL, + `l_tax` double DEFAULT NULL, + `l_returnflag` char(1) DEFAULT NULL, + `l_linestatus` char(1) DEFAULT NULL, + `l_shipDATE` date DEFAULT NULL, + `l_commitDATE` date DEFAULT NULL, + `l_receiptDATE` date DEFAULT NULL, + `l_shipinstruct` char(25) DEFAULT NULL, + `l_shipmode` char(10) DEFAULT NULL, + `l_comment` varchar(44) DEFAULT NULL, + PRIMARY KEY (`l_orderkey`,`l_linenumber`), + KEY `i_l_shipdate` (`l_shipDATE`), + KEY `i_l_suppkey_partkey` (`l_partkey`,`l_suppkey`), + KEY `i_l_partkey` (`l_partkey`), + KEY `i_l_suppkey` (`l_suppkey`), + KEY `i_l_receiptdate` (`l_receiptDATE`), + KEY `i_l_orderkey` (`l_orderkey`), + KEY `i_l_orderkey_quantity` (`l_orderkey`,`l_quantity`), + KEY `i_l_commitdate` (`l_commitDATE`) +) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1 +drop index `primary` on lineitem; +select * from lineitem where l_orderkey=1 and l_linenumber=2; +l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +1 68 9 2 36 34850.16 0.09 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +insert into lineitem values +(1,68,9,2,36,34850.16,0.07,0.06,'N','O','1996-04-12','1996-02-28','1996-04-20','TAKE BACK RETURN','MAIL','slyly bold pinto beans detect s'); +select * from lineitem where l_orderkey=1 and l_linenumber=2; +l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +1 68 9 2 36 34850.16 0.09 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +1 68 9 2 36 34850.16 0.07 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +alter table lineitem add primary key (l_orderkey, l_linenumber); +ERROR 23000: Duplicate entry '1-2' for key 'PRIMARY' +show create table lineitem; +Table Create Table +lineitem CREATE TABLE `lineitem` ( + `l_orderkey` int(11) NOT NULL DEFAULT '0', + `l_partkey` int(11) DEFAULT NULL, + `l_suppkey` int(11) DEFAULT NULL, + `l_linenumber` int(11) NOT NULL DEFAULT '0', + `l_quantity` double DEFAULT NULL, + `l_extendedprice` double DEFAULT NULL, + `l_discount` double DEFAULT NULL, + `l_tax` double DEFAULT NULL, + `l_returnflag` char(1) DEFAULT NULL, + `l_linestatus` char(1) DEFAULT NULL, + `l_shipDATE` date DEFAULT NULL, + `l_commitDATE` date DEFAULT NULL, + `l_receiptDATE` date DEFAULT NULL, + `l_shipinstruct` char(25) DEFAULT NULL, + `l_shipmode` char(10) DEFAULT NULL, + `l_comment` varchar(44) DEFAULT NULL, + KEY `i_l_shipdate` (`l_shipDATE`), + KEY `i_l_suppkey_partkey` (`l_partkey`,`l_suppkey`), + KEY `i_l_partkey` (`l_partkey`), + KEY `i_l_suppkey` (`l_suppkey`), + KEY `i_l_receiptdate` (`l_receiptDATE`), + KEY `i_l_orderkey` (`l_orderkey`), + KEY `i_l_orderkey_quantity` (`l_orderkey`,`l_quantity`), + KEY `i_l_commitdate` (`l_commitDATE`) +) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1 +select * from lineitem where l_orderkey=1 and l_linenumber=2; +l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +1 68 9 2 36 34850.16 0.09 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +1 68 9 2 36 34850.16 0.07 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +delete from lineitem where l_orderkey=1 and l_linenumber=2 and l_discount=0.07; +alter table lineitem add primary key (l_orderkey, l_linenumber); +show create table lineitem; +Table Create Table +lineitem CREATE TABLE `lineitem` ( + `l_orderkey` int(11) NOT NULL DEFAULT '0', + `l_partkey` int(11) DEFAULT NULL, + `l_suppkey` int(11) DEFAULT NULL, + `l_linenumber` int(11) NOT NULL DEFAULT '0', + `l_quantity` double DEFAULT NULL, + `l_extendedprice` double DEFAULT NULL, + `l_discount` double DEFAULT NULL, + `l_tax` double DEFAULT NULL, + `l_returnflag` char(1) DEFAULT NULL, + `l_linestatus` char(1) DEFAULT NULL, + `l_shipDATE` date DEFAULT NULL, + `l_commitDATE` date DEFAULT NULL, + `l_receiptDATE` date DEFAULT NULL, + `l_shipinstruct` char(25) DEFAULT NULL, + `l_shipmode` char(10) DEFAULT NULL, + `l_comment` varchar(44) DEFAULT NULL, + PRIMARY KEY (`l_orderkey`,`l_linenumber`), + KEY `i_l_shipdate` (`l_shipDATE`), + KEY `i_l_suppkey_partkey` (`l_partkey`,`l_suppkey`), + KEY `i_l_partkey` (`l_partkey`), + KEY `i_l_suppkey` (`l_suppkey`), + KEY `i_l_receiptdate` (`l_receiptDATE`), + KEY `i_l_orderkey` (`l_orderkey`), + KEY `i_l_orderkey_quantity` (`l_orderkey`,`l_quantity`), + KEY `i_l_commitdate` (`l_commitDATE`) +) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1 +select * from lineitem where l_orderkey=1 and l_linenumber=2; +l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +1 68 9 2 36 34850.16 0.09 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +create unique index i_c_name on customer(c_name); +show create table customer; +Table Create Table +customer CREATE TABLE `customer` ( + `c_custkey` int(11) NOT NULL, + `c_name` varchar(25) DEFAULT NULL, + `c_address` varchar(40) DEFAULT NULL, + `c_nationkey` int(11) DEFAULT NULL, + `c_phone` char(15) DEFAULT NULL, + `c_acctbal` double DEFAULT NULL, + `c_mktsegment` char(10) DEFAULT NULL, + `c_comment` varchar(117) DEFAULT NULL, + PRIMARY KEY (`c_custkey`), + UNIQUE KEY `i_c_name` (`c_name`), + KEY `i_c_nationkey` (`c_nationkey`) +) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1 +select * from customer where c_name='Customer#000000003'; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +drop index i_c_name on customer; +insert into customer values +(303,'Customer#000000003','MG9kdTD2WBHm',1,'11-719-748-3364',7498.12,'AUTOMOBILE','special packages wake. slyly reg'); +select * from customer where c_name='Customer#000000003'; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +303 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +alter table customer add unique index i_c_name(c_name); +ERROR 23000: Duplicate entry 'Customer#000000003' for key 'i_c_name' +show create table customer; +Table Create Table +customer CREATE TABLE `customer` ( + `c_custkey` int(11) NOT NULL, + `c_name` varchar(25) DEFAULT NULL, + `c_address` varchar(40) DEFAULT NULL, + `c_nationkey` int(11) DEFAULT NULL, + `c_phone` char(15) DEFAULT NULL, + `c_acctbal` double DEFAULT NULL, + `c_mktsegment` char(10) DEFAULT NULL, + `c_comment` varchar(117) DEFAULT NULL, + PRIMARY KEY (`c_custkey`), + KEY `i_c_nationkey` (`c_nationkey`) +) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1 +select * from customer where c_name='Customer#000000003'; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +303 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +delete from customer where c_custkey=303; +select * from customer where c_name='Customer#000000003'; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +alter table customer add unique index i_c_name(c_name); +show create table customer; +Table Create Table +customer CREATE TABLE `customer` ( + `c_custkey` int(11) NOT NULL, + `c_name` varchar(25) DEFAULT NULL, + `c_address` varchar(40) DEFAULT NULL, + `c_nationkey` int(11) DEFAULT NULL, + `c_phone` char(15) DEFAULT NULL, + `c_acctbal` double DEFAULT NULL, + `c_mktsegment` char(10) DEFAULT NULL, + `c_comment` varchar(117) DEFAULT NULL, + PRIMARY KEY (`c_custkey`), + UNIQUE KEY `i_c_name` (`c_name`), + KEY `i_c_nationkey` (`c_nationkey`) +) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1 +select * from customer where c_name='Customer#000000003'; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +drop index `primary` on customer; +show create table customer; +Table Create Table +customer CREATE TABLE `customer` ( + `c_custkey` int(11) NOT NULL, + `c_name` varchar(25) DEFAULT NULL, + `c_address` varchar(40) DEFAULT NULL, + `c_nationkey` int(11) DEFAULT NULL, + `c_phone` char(15) DEFAULT NULL, + `c_acctbal` double DEFAULT NULL, + `c_mktsegment` char(10) DEFAULT NULL, + `c_comment` varchar(117) DEFAULT NULL, + UNIQUE KEY `i_c_name` (`c_name`), + KEY `i_c_nationkey` (`c_nationkey`) +) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1 +insert into customer values +(3,'Customer#000000303','MG9kdTD2WBHm',1,'11-719-748-3364',7498.12,'AUTOMOBILE','special packages wake. slyly reg'); +alter ignore table customer add primary key (c_custkey); +show create table customer; +Table Create Table +customer CREATE TABLE `customer` ( + `c_custkey` int(11) NOT NULL, + `c_name` varchar(25) DEFAULT NULL, + `c_address` varchar(40) DEFAULT NULL, + `c_nationkey` int(11) DEFAULT NULL, + `c_phone` char(15) DEFAULT NULL, + `c_acctbal` double DEFAULT NULL, + `c_mktsegment` char(10) DEFAULT NULL, + `c_comment` varchar(117) DEFAULT NULL, + PRIMARY KEY (`c_custkey`), + UNIQUE KEY `i_c_name` (`c_name`), + KEY `i_c_nationkey` (`c_nationkey`) +) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1 +select * from customer where c_custkey=3; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +DROP DATABASE dbt3_s001; +set @@storage_engine= default; diff --git a/mysql-test/r/alter_table_mdev539_myisam.result b/mysql-test/r/alter_table_mdev539_myisam.result new file mode 100644 index 00000000000..7140c544836 --- /dev/null +++ b/mysql-test/r/alter_table_mdev539_myisam.result @@ -0,0 +1,252 @@ +# +set @@storage_engine= MyISAM; +# +# mdev-539: fast build of unique/primary indexes for MyISAM/Aria +# +DROP DATABASE IF EXISTS dbt3_s001; +CREATE DATABASE dbt3_s001; +use dbt3_s001; +drop index `primary` on lineitem; +show create table lineitem; +Table Create Table +lineitem CREATE TABLE `lineitem` ( + `l_orderkey` int(11) NOT NULL DEFAULT '0', + `l_partkey` int(11) DEFAULT NULL, + `l_suppkey` int(11) DEFAULT NULL, + `l_linenumber` int(11) NOT NULL DEFAULT '0', + `l_quantity` double DEFAULT NULL, + `l_extendedprice` double DEFAULT NULL, + `l_discount` double DEFAULT NULL, + `l_tax` double DEFAULT NULL, + `l_returnflag` char(1) DEFAULT NULL, + `l_linestatus` char(1) DEFAULT NULL, + `l_shipDATE` date DEFAULT NULL, + `l_commitDATE` date DEFAULT NULL, + `l_receiptDATE` date DEFAULT NULL, + `l_shipinstruct` char(25) DEFAULT NULL, + `l_shipmode` char(10) DEFAULT NULL, + `l_comment` varchar(44) DEFAULT NULL, + KEY `i_l_shipdate` (`l_shipDATE`), + KEY `i_l_suppkey_partkey` (`l_partkey`,`l_suppkey`), + KEY `i_l_partkey` (`l_partkey`), + KEY `i_l_suppkey` (`l_suppkey`), + KEY `i_l_receiptdate` (`l_receiptDATE`), + KEY `i_l_orderkey` (`l_orderkey`), + KEY `i_l_orderkey_quantity` (`l_orderkey`,`l_quantity`), + KEY `i_l_commitdate` (`l_commitDATE`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +alter table lineitem add primary key (l_orderkey, l_linenumber); +show create table lineitem; +Table Create Table +lineitem CREATE TABLE `lineitem` ( + `l_orderkey` int(11) NOT NULL DEFAULT '0', + `l_partkey` int(11) DEFAULT NULL, + `l_suppkey` int(11) DEFAULT NULL, + `l_linenumber` int(11) NOT NULL DEFAULT '0', + `l_quantity` double DEFAULT NULL, + `l_extendedprice` double DEFAULT NULL, + `l_discount` double DEFAULT NULL, + `l_tax` double DEFAULT NULL, + `l_returnflag` char(1) DEFAULT NULL, + `l_linestatus` char(1) DEFAULT NULL, + `l_shipDATE` date DEFAULT NULL, + `l_commitDATE` date DEFAULT NULL, + `l_receiptDATE` date DEFAULT NULL, + `l_shipinstruct` char(25) DEFAULT NULL, + `l_shipmode` char(10) DEFAULT NULL, + `l_comment` varchar(44) DEFAULT NULL, + PRIMARY KEY (`l_orderkey`,`l_linenumber`), + KEY `i_l_shipdate` (`l_shipDATE`), + KEY `i_l_suppkey_partkey` (`l_partkey`,`l_suppkey`), + KEY `i_l_partkey` (`l_partkey`), + KEY `i_l_suppkey` (`l_suppkey`), + KEY `i_l_receiptdate` (`l_receiptDATE`), + KEY `i_l_orderkey` (`l_orderkey`), + KEY `i_l_orderkey_quantity` (`l_orderkey`,`l_quantity`), + KEY `i_l_commitdate` (`l_commitDATE`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop index `primary` on lineitem; +select * from lineitem where l_orderkey=1 and l_linenumber=2; +l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +1 68 9 2 36 34850.16 0.09 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +insert into lineitem values +(1,68,9,2,36,34850.16,0.07,0.06,'N','O','1996-04-12','1996-02-28','1996-04-20','TAKE BACK RETURN','MAIL','slyly bold pinto beans detect s'); +select * from lineitem where l_orderkey=1 and l_linenumber=2; +l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +1 68 9 2 36 34850.16 0.09 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +1 68 9 2 36 34850.16 0.07 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +alter table lineitem add primary key (l_orderkey, l_linenumber); +ERROR 23000: Duplicate entry '1-2' for key 'PRIMARY' +show create table lineitem; +Table Create Table +lineitem CREATE TABLE `lineitem` ( + `l_orderkey` int(11) NOT NULL DEFAULT '0', + `l_partkey` int(11) DEFAULT NULL, + `l_suppkey` int(11) DEFAULT NULL, + `l_linenumber` int(11) NOT NULL DEFAULT '0', + `l_quantity` double DEFAULT NULL, + `l_extendedprice` double DEFAULT NULL, + `l_discount` double DEFAULT NULL, + `l_tax` double DEFAULT NULL, + `l_returnflag` char(1) DEFAULT NULL, + `l_linestatus` char(1) DEFAULT NULL, + `l_shipDATE` date DEFAULT NULL, + `l_commitDATE` date DEFAULT NULL, + `l_receiptDATE` date DEFAULT NULL, + `l_shipinstruct` char(25) DEFAULT NULL, + `l_shipmode` char(10) DEFAULT NULL, + `l_comment` varchar(44) DEFAULT NULL, + KEY `i_l_shipdate` (`l_shipDATE`), + KEY `i_l_suppkey_partkey` (`l_partkey`,`l_suppkey`), + KEY `i_l_partkey` (`l_partkey`), + KEY `i_l_suppkey` (`l_suppkey`), + KEY `i_l_receiptdate` (`l_receiptDATE`), + KEY `i_l_orderkey` (`l_orderkey`), + KEY `i_l_orderkey_quantity` (`l_orderkey`,`l_quantity`), + KEY `i_l_commitdate` (`l_commitDATE`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +select * from lineitem where l_orderkey=1 and l_linenumber=2; +l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +1 68 9 2 36 34850.16 0.09 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +1 68 9 2 36 34850.16 0.07 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +delete from lineitem where l_orderkey=1 and l_linenumber=2 and l_discount=0.07; +alter table lineitem add primary key (l_orderkey, l_linenumber); +show create table lineitem; +Table Create Table +lineitem CREATE TABLE `lineitem` ( + `l_orderkey` int(11) NOT NULL DEFAULT '0', + `l_partkey` int(11) DEFAULT NULL, + `l_suppkey` int(11) DEFAULT NULL, + `l_linenumber` int(11) NOT NULL DEFAULT '0', + `l_quantity` double DEFAULT NULL, + `l_extendedprice` double DEFAULT NULL, + `l_discount` double DEFAULT NULL, + `l_tax` double DEFAULT NULL, + `l_returnflag` char(1) DEFAULT NULL, + `l_linestatus` char(1) DEFAULT NULL, + `l_shipDATE` date DEFAULT NULL, + `l_commitDATE` date DEFAULT NULL, + `l_receiptDATE` date DEFAULT NULL, + `l_shipinstruct` char(25) DEFAULT NULL, + `l_shipmode` char(10) DEFAULT NULL, + `l_comment` varchar(44) DEFAULT NULL, + PRIMARY KEY (`l_orderkey`,`l_linenumber`), + KEY `i_l_shipdate` (`l_shipDATE`), + KEY `i_l_suppkey_partkey` (`l_partkey`,`l_suppkey`), + KEY `i_l_partkey` (`l_partkey`), + KEY `i_l_suppkey` (`l_suppkey`), + KEY `i_l_receiptdate` (`l_receiptDATE`), + KEY `i_l_orderkey` (`l_orderkey`), + KEY `i_l_orderkey_quantity` (`l_orderkey`,`l_quantity`), + KEY `i_l_commitdate` (`l_commitDATE`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +select * from lineitem where l_orderkey=1 and l_linenumber=2; +l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +1 68 9 2 36 34850.16 0.09 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +create unique index i_c_name on customer(c_name); +show create table customer; +Table Create Table +customer CREATE TABLE `customer` ( + `c_custkey` int(11) NOT NULL, + `c_name` varchar(25) DEFAULT NULL, + `c_address` varchar(40) DEFAULT NULL, + `c_nationkey` int(11) DEFAULT NULL, + `c_phone` char(15) DEFAULT NULL, + `c_acctbal` double DEFAULT NULL, + `c_mktsegment` char(10) DEFAULT NULL, + `c_comment` varchar(117) DEFAULT NULL, + PRIMARY KEY (`c_custkey`), + UNIQUE KEY `i_c_name` (`c_name`), + KEY `i_c_nationkey` (`c_nationkey`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +select * from customer where c_name='Customer#000000003'; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +drop index i_c_name on customer; +insert into customer values +(303,'Customer#000000003','MG9kdTD2WBHm',1,'11-719-748-3364',7498.12,'AUTOMOBILE','special packages wake. slyly reg'); +select * from customer where c_name='Customer#000000003'; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +303 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +alter table customer add unique index i_c_name(c_name); +ERROR 23000: Duplicate entry 'Customer#000000003' for key 'i_c_name' +show create table customer; +Table Create Table +customer CREATE TABLE `customer` ( + `c_custkey` int(11) NOT NULL, + `c_name` varchar(25) DEFAULT NULL, + `c_address` varchar(40) DEFAULT NULL, + `c_nationkey` int(11) DEFAULT NULL, + `c_phone` char(15) DEFAULT NULL, + `c_acctbal` double DEFAULT NULL, + `c_mktsegment` char(10) DEFAULT NULL, + `c_comment` varchar(117) DEFAULT NULL, + PRIMARY KEY (`c_custkey`), + KEY `i_c_nationkey` (`c_nationkey`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +select * from customer where c_name='Customer#000000003'; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +303 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +delete from customer where c_custkey=303; +select * from customer where c_name='Customer#000000003'; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +alter table customer add unique index i_c_name(c_name); +show create table customer; +Table Create Table +customer CREATE TABLE `customer` ( + `c_custkey` int(11) NOT NULL, + `c_name` varchar(25) DEFAULT NULL, + `c_address` varchar(40) DEFAULT NULL, + `c_nationkey` int(11) DEFAULT NULL, + `c_phone` char(15) DEFAULT NULL, + `c_acctbal` double DEFAULT NULL, + `c_mktsegment` char(10) DEFAULT NULL, + `c_comment` varchar(117) DEFAULT NULL, + PRIMARY KEY (`c_custkey`), + UNIQUE KEY `i_c_name` (`c_name`), + KEY `i_c_nationkey` (`c_nationkey`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +select * from customer where c_name='Customer#000000003'; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +drop index `primary` on customer; +show create table customer; +Table Create Table +customer CREATE TABLE `customer` ( + `c_custkey` int(11) NOT NULL, + `c_name` varchar(25) DEFAULT NULL, + `c_address` varchar(40) DEFAULT NULL, + `c_nationkey` int(11) DEFAULT NULL, + `c_phone` char(15) DEFAULT NULL, + `c_acctbal` double DEFAULT NULL, + `c_mktsegment` char(10) DEFAULT NULL, + `c_comment` varchar(117) DEFAULT NULL, + UNIQUE KEY `i_c_name` (`c_name`), + KEY `i_c_nationkey` (`c_nationkey`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +insert into customer values +(3,'Customer#000000303','MG9kdTD2WBHm',1,'11-719-748-3364',7498.12,'AUTOMOBILE','special packages wake. slyly reg'); +alter ignore table customer add primary key (c_custkey); +show create table customer; +Table Create Table +customer CREATE TABLE `customer` ( + `c_custkey` int(11) NOT NULL, + `c_name` varchar(25) DEFAULT NULL, + `c_address` varchar(40) DEFAULT NULL, + `c_nationkey` int(11) DEFAULT NULL, + `c_phone` char(15) DEFAULT NULL, + `c_acctbal` double DEFAULT NULL, + `c_mktsegment` char(10) DEFAULT NULL, + `c_comment` varchar(117) DEFAULT NULL, + PRIMARY KEY (`c_custkey`), + UNIQUE KEY `i_c_name` (`c_name`), + KEY `i_c_nationkey` (`c_nationkey`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +select * from customer where c_custkey=3; +c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment +3 Customer#000000003 MG9kdTD2WBHm 1 11-719-748-3364 7498.12 AUTOMOBILE special packages wake. slyly reg +DROP DATABASE dbt3_s001; +set @@storage_engine= default; diff --git a/mysql-test/r/comments.result b/mysql-test/r/comments.result index 8aebe95c5ac..e65c886014d 100644 --- a/mysql-test/r/comments.result +++ b/mysql-test/r/comments.result @@ -10,7 +10,7 @@ ERROR 42000: Query was empty select 1 /*!32301 +1 */; 1 +1 2 -select 1 /*!52301 +1 */; +select 1 /*!952301 +1 */; 1 1 select 1--1; @@ -35,19 +35,19 @@ select 1 /*M!50000 +1 */; select 1 /*M!50300 +1 */; 1 +1 2 -select 2 /*M!99999 +1 */; +select 2 /*M!999999 +1 */; 2 2 select 2 /*M!0000 +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 '0000 +1 */' at line 1 select 1/*!2*/; 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 '2*/' at line 1 -select 1/*!000002*/; +select 1/*!0000002*/; 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 '2*/' at line 1 select 1/*!999992*/; 1 1 -select 1 + /*!00000 2 */ + 3 /*!99999 noise*/ + 4; +select 1 + /*!00000 2 */ + 3 /*!999999 noise*/ + 4; 1 + 2 + 3 + 4 10 drop table if exists table_28779; @@ -60,8 +60,8 @@ prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*! AND 2=2;"; 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 prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*! AND 2=2;*"; 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 -prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!98765' AND b = 'bar';"; -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 '/*!98765' AND b = 'bar'' at line 1 -prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!98765' AND b = 'bar';*"; -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 '/*!98765' AND b = 'bar';*' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!998765' AND b = 'bar';"; +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 '/*!998765' AND b = 'bar'' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!998765' AND b = 'bar';*"; +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 '/*!998765' AND b = 'bar';*' at line 1 drop table table_28779; diff --git a/mysql-test/r/connect.result b/mysql-test/r/connect.result index 09eaf9855b0..a4bd6a262a7 100644 --- a/mysql-test/r/connect.result +++ b/mysql-test/r/connect.result @@ -1,6 +1,7 @@ drop table if exists t1,t2; show tables; Tables_in_mysql +column_stats columns_priv db event @@ -11,6 +12,7 @@ help_keyword help_relation help_topic host +index_stats ndb_binlog_index plugin proc @@ -18,6 +20,7 @@ procs_priv proxies_priv servers slow_log +table_stats tables_priv time_zone time_zone_leap_second @@ -35,6 +38,7 @@ grant ALL on *.* to test@localhost identified by "gambling"; grant ALL on *.* to test@127.0.0.1 identified by "gambling"; show tables; Tables_in_mysql +column_stats columns_priv db event @@ -45,6 +49,7 @@ help_keyword help_relation help_topic host +index_stats ndb_binlog_index plugin proc @@ -52,6 +57,7 @@ procs_priv proxies_priv servers slow_log +table_stats tables_priv time_zone time_zone_leap_second @@ -77,6 +83,7 @@ ERROR HY000: Password hash should be a 41-digit hexadecimal number set password=old_password('gambling3'); show tables; Tables_in_mysql +column_stats columns_priv db event @@ -87,6 +94,7 @@ help_keyword help_relation help_topic host +index_stats ndb_binlog_index plugin proc @@ -94,6 +102,7 @@ procs_priv proxies_priv servers slow_log +table_stats tables_priv time_zone time_zone_leap_second diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index 4cb14aabb2c..7c556354d2e 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -55,9 +55,9 @@ ERROR 42000: Incorrect table name 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa create table a (`aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa` int); ERROR 42000: Identifier name 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' is too long create table t1 (a datetime default now()); -ERROR 42000: Invalid default value for 'a' +drop table t1; create table t1 (a datetime on update now()); -ERROR HY000: Invalid ON UPDATE clause for 'a' column +drop table t1; create table t1 (a int default 100 auto_increment); ERROR 42000: Invalid default value for 'a' create table t1 (a tinyint default 1000); @@ -1762,7 +1762,9 @@ t1 CREATE TABLE `t1` ( `TIME_MS` decimal(22,3) NOT NULL DEFAULT '0.000', `STAGE` tinyint(2) NOT NULL DEFAULT '0', `MAX_STAGE` tinyint(2) NOT NULL DEFAULT '0', - `PROGRESS` decimal(7,3) NOT NULL DEFAULT '0.000' + `PROGRESS` decimal(7,3) NOT NULL DEFAULT '0.000', + `MEMORY_USED` int(7) NOT NULL DEFAULT '0', + `EXAMINED_ROWS` int(7) NOT NULL DEFAULT '0' ) DEFAULT CHARSET=utf8 drop table t1; create temporary table t1 like information_schema.processlist; @@ -1780,7 +1782,9 @@ t1 CREATE TEMPORARY TABLE `t1` ( `TIME_MS` decimal(22,3) NOT NULL DEFAULT '0.000', `STAGE` tinyint(2) NOT NULL DEFAULT '0', `MAX_STAGE` tinyint(2) NOT NULL DEFAULT '0', - `PROGRESS` decimal(7,3) NOT NULL DEFAULT '0.000' + `PROGRESS` decimal(7,3) NOT NULL DEFAULT '0.000', + `MEMORY_USED` int(7) NOT NULL DEFAULT '0', + `EXAMINED_ROWS` int(7) NOT NULL DEFAULT '0' ) DEFAULT CHARSET=utf8 drop table t1; create table t1 like information_schema.character_sets; diff --git a/mysql-test/r/derived_view.result b/mysql-test/r/derived_view.result index 8630087ba72..40420a15c25 100644 --- a/mysql-test/r/derived_view.result +++ b/mysql-test/r/derived_view.result @@ -1468,14 +1468,14 @@ WHERE (t2.a ,t1.b) NOT IN (SELECT DISTINCT c,a FROM t3 t); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) -2 DEPENDENT SUBQUERY t eq_ref PRIMARY,c PRIMARY 4 func 1 Using where +2 DEPENDENT SUBQUERY t unique_subquery PRIMARY,c PRIMARY 4 func 1 Using where EXPLAIN SELECT * FROM t1 , t2 WHERE (t2.a ,t1.b) NOT IN (SELECT DISTINCT c,a FROM (SELECT * FROM t3) t); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) -2 DEPENDENT SUBQUERY t3 eq_ref PRIMARY,c PRIMARY 4 func 1 Using where +2 DEPENDENT SUBQUERY t3 unique_subquery PRIMARY,c PRIMARY 4 func 1 Using where SELECT * FROM t1 , t2 WHERE (t2.a ,t1.b) NOT IN (SELECT DISTINCT c,a FROM (SELECT * FROM t3) t); b a diff --git a/mysql-test/r/dyncol.result b/mysql-test/r/dyncol.result index 2411f1070d9..172179bd7f4 100644 --- a/mysql-test/r/dyncol.result +++ b/mysql-test/r/dyncol.result @@ -1088,7 +1088,7 @@ column_list(column_add(column_create(1, 1), 1, null)) select column_list(column_add(column_create(1, 1), 1, "")); column_list(column_add(column_create(1, 1), 1, "")) -1 +`1` select hex(column_add("", 1, 1)); hex(column_add("", 1, 1)) 00010001000002 @@ -1133,10 +1133,10 @@ column_exists(column_create(1, 1212 as integer, 2, 1212 as integer), 4) # column list select column_list(column_create(1, 1212 as integer, 2, 1212 as integer)); column_list(column_create(1, 1212 as integer, 2, 1212 as integer)) -1,2 +`1`,`2` select column_list(column_create(1, 1212 as integer)); column_list(column_create(1, 1212 as integer)) -1 +`1` select column_list(column_create(1, NULL as integer)); column_list(column_create(1, NULL as integer)) @@ -1218,35 +1218,35 @@ sum(column_get(str, 1 as int)) 11 select id, column_list(str) from t1 where id= 5; id column_list(str) -5 1,2,3,10 +5 `1`,`2`,`3`,`10` update t1 set str=column_delete(str, 3, 4, 2) where id= 5; select id, length(str), column_list(str), column_get(str, 1 as int), column_get(str, 2 as char), column_get(str, 3 as int) from t1; id length(str) column_list(str) column_get(str, 1 as int) column_get(str, 2 as char) column_get(str, 3 as int) -1 12 1,2 1 a NULL -2 12 1,2 2 a NULL -3 12 2,3 NULL c 100 -4 16 1,2,3 5 c 100 -5 15 1,10 6 NULL NULL -6 21 2,3,10 NULL c 100 +1 12 `1`,`2` 1 a NULL +2 12 `1`,`2` 2 a NULL +3 12 `2`,`3` NULL c 100 +4 16 `1`,`2`,`3` 5 c 100 +5 15 `1`,`10` 6 NULL NULL +6 21 `2`,`3`,`10` NULL c 100 update t1 set str=column_add(str, 4, 45 as char, 2, 'c') where id= 5; select id, length(str), column_list(str), column_get(str, 1 as int), column_get(str, 2 as char), column_get(str, 3 as int) from t1 where id = 5; id length(str) column_list(str) column_get(str, 1 as int) column_get(str, 2 as char) column_get(str, 3 as int) -5 26 1,2,4,10 6 c NULL +5 26 `1`,`2`,`4`,`10` 6 c NULL select id, length(str), column_list(str), column_exists(str, 4) from t1; id length(str) column_list(str) column_exists(str, 4) -1 12 1,2 0 -2 12 1,2 0 -3 12 2,3 0 -4 16 1,2,3 0 -5 26 1,2,4,10 1 -6 21 2,3,10 0 +1 12 `1`,`2` 0 +2 12 `1`,`2` 0 +3 12 `2`,`3` 0 +4 16 `1`,`2`,`3` 0 +5 26 `1`,`2`,`4`,`10` 1 +6 21 `2`,`3`,`10` 0 select sum(column_get(str, 1 as int)), column_list(str) from t1 group by 2; sum(column_get(str, 1 as int)) column_list(str) -3 1,2 -5 1,2,3 -6 1,2,4,10 -NULL 2,3 -NULL 2,3,10 +3 `1`,`2` +5 `1`,`2`,`3` +6 `1`,`2`,`4`,`10` +NULL `2`,`3` +NULL `2`,`3`,`10` select id, hex(str) from t1; id hex(str) 1 00020001000002000B020861 @@ -1282,11 +1282,11 @@ id 5 select id, column_list(str), length(str) from t1 where id=5; id column_list(str) length(str) -5 1,2,4,5,10 100048 +5 `1`,`2`,`4`,`5`,`10` 100048 update t1 set str=column_delete(str, 5) where id=5; select id, column_list(str), length(str) from t1 where id=5; id column_list(str) length(str) -5 1,2,4,10 34 +5 `1`,`2`,`4`,`10` 34 drop table t1; # # LP#778905: Assertion `value->year <= 9999' failed in @@ -1306,7 +1306,7 @@ INSERT INTO t1 SET f1 = COLUMN_CREATE( 2 , 'cde' ); SELECT HEX(COLUMN_ADD(f1, 1, 'abc')), COLUMN_LIST(f1) FROM t1; HEX(COLUMN_ADD(f1, 1, 'abc')) COLUMN_LIST(f1) NULL NULL -0002000100030200230861626308636465 2 +0002000100030200230861626308636465 `2` SELECT COLUMN_ADD(f1, 1, 'abc'), COLUMN_LIST(f1) FROM t1; DROP TABLE t1; # @@ -1406,3 +1406,305 @@ drop table t1; # # end of 5.3 tests # +# +# test of symbolic names +# +# creation test (names) +set names utf8; +select hex(column_create("адын", 1212)); +hex(column_create("адын", 1212)) +040100080000000000D0B0D0B4D18BD0BD7809 +select hex(column_create("1212", 1212)); +hex(column_create("1212", 1212)) +040100040000000000313231327809 +select hex(column_create(1212, 2, "www", 3)); +hex(column_create(1212, 2, "www", 3)) +04020007000000000003001000777777313231320604 +select hex(column_create("1212", 2, "www", 3)); +hex(column_create("1212", 2, "www", 3)) +04020007000000000003001000777777313231320604 +select hex(column_create("1212", 2, 3, 3)); +hex(column_create("1212", 2, 3, 3)) +0402000500000000000100100033313231320604 +select hex(column_create("1212", 2, "адын", 1, 3, 3)); +hex(column_create("1212", 2, "адын", 1, 3, 3)) +0403000D000000000001001000050020003331323132D0B0D0B4D18BD0BD060402 +set names default; +# fetching column test (names) +set names utf8; +select column_get(column_create("адын", 1212), "адын" as int); +column_get(column_create("адын", 1212), "адын" as int) +1212 +select column_get(column_create("1212", 2, "адын", 1, 3, 3), "адын" as int); +column_get(column_create("1212", 2, "адын", 1, 3, 3), "адын" as int) +1 +select column_get(column_create("1212", 2, "адын", 1, 3, 3), 1212 as int); +column_get(column_create("1212", 2, "адын", 1, 3, 3), 1212 as int) +2 +select column_get(column_create("1212", 2, "адын", 1, 3, 3), "3" as int); +column_get(column_create("1212", 2, "адын", 1, 3, 3), "3" as int) +3 +select column_get(column_create("1212", 2, "адын", 1, 3, 3), 3 as int); +column_get(column_create("1212", 2, "адын", 1, 3, 3), 3 as int) +3 +select column_get(column_create("1212", 2, "адын", 1, 3, 3), 4 as int); +column_get(column_create("1212", 2, "адын", 1, 3, 3), 4 as int) +NULL +select column_get(column_create("1212", 2, "адын", 1, 3, 3), "4" as int); +column_get(column_create("1212", 2, "адын", 1, 3, 3), "4" as int) +NULL +set names default; +# column existance test (names) +set names utf8; +select column_exists(column_create("адын", 1212), "адын"); +column_exists(column_create("адын", 1212), "адын") +1 +select column_exists(column_create("адын", 1212), "aады"); +column_exists(column_create("адын", 1212), "aады") +0 +select column_exists(column_create("1212", 2, "адын", 1, 3, 3), "адын"); +column_exists(column_create("1212", 2, "адын", 1, 3, 3), "адын") +1 +select column_exists(column_create("1212", 2, "адын", 1, 3, 3), 1212); +column_exists(column_create("1212", 2, "адын", 1, 3, 3), 1212) +1 +select column_exists(column_create("1212", 2, "адын", 1, 3, 3), "3"); +column_exists(column_create("1212", 2, "адын", 1, 3, 3), "3") +1 +select column_exists(column_create("1212", 2, "адын", 1, 3, 3), 3); +column_exists(column_create("1212", 2, "адын", 1, 3, 3), 3) +1 +select column_exists(column_create("1212", 2, "адын", 1, 3, 3), 4); +column_exists(column_create("1212", 2, "адын", 1, 3, 3), 4) +0 +select column_exists(column_create("1212", 2, "адын", 1, 3, 3), "4"); +column_exists(column_create("1212", 2, "адын", 1, 3, 3), "4") +0 +set names default; +# column changing test (names) +select hex(column_add(column_create(1, "AAA"), "b", "BBB")); +hex(column_add(column_create(1, "AAA"), "b", "BBB")) +0402000200000003000100430031620841414108424242 +select hex(column_add(column_create("1", "AAA"), "b", "BBB")); +hex(column_add(column_create("1", "AAA"), "b", "BBB")) +0402000200000003000100430031620841414108424242 +select column_get(column_add(column_create(1, "AAA"), "b", "BBB"), 1 as char); +column_get(column_add(column_create(1, "AAA"), "b", "BBB"), 1 as char) +AAA +select column_get(column_add(column_create(1, "AAA"), "b", "BBB"), "b" as char); +column_get(column_add(column_create(1, "AAA"), "b", "BBB"), "b" as char) +BBB +select hex(column_add(column_create("a", "AAA"), 1, "BBB")); +hex(column_add(column_create("a", "AAA"), 1, "BBB")) +0402000200000003000100430031610842424208414141 +select hex(column_add(column_create("a", "AAA"), "1", "BBB")); +hex(column_add(column_create("a", "AAA"), "1", "BBB")) +0402000200000003000100430031610842424208414141 +select hex(column_add(column_create("a", 1212 as integer), "b", "1212" as integer)); +hex(column_add(column_create("a", 1212 as integer), "b", "1212" as integer)) +04020002000000000001002000616278097809 +select hex(column_add(column_create("a", 1212 as integer), "a", "1212" as integer)); +hex(column_add(column_create("a", 1212 as integer), "a", "1212" as integer)) +040100010000000000617809 +select hex(column_add(column_create("a", 1212 as integer), "a", NULL as integer)); +hex(column_add(column_create("a", 1212 as integer), "a", NULL as integer)) +0400000000 +select hex(column_add(column_create("a", 1212 as integer), "b", NULL as integer)); +hex(column_add(column_create("a", 1212 as integer), "b", NULL as integer)) +040100010000000000617809 +select hex(column_add(column_create("a", 1212 as integer), "b", 1212 as integer, "a", 11 as integer)); +hex(column_add(column_create("a", 1212 as integer), "b", 1212 as integer, "a", 11 as integer)) +040200020000000000010010006162167809 +select column_get(column_add(column_create("a", 1212 as integer), "b", 1212 as integer, "a", 11 as integer), "a" as integer); +column_get(column_add(column_create("a", 1212 as integer), "b", 1212 as integer, "a", 11 as integer), "a" as integer) +11 +select column_get(column_add(column_create("a", 1212 as integer), "b", 1212 as integer, "a", 11 as integer), "b" as integer); +column_get(column_add(column_create("a", 1212 as integer), "b", 1212 as integer, "a", 11 as integer), "b" as integer) +1212 +select hex(column_add(column_create("a", 1212 as integer), "a", 1212 as integer, "b", 11 as integer)); +hex(column_add(column_create("a", 1212 as integer), "a", 1212 as integer, "b", 11 as integer)) +040200020000000000010020006162780916 +select hex(column_add(column_create("a", NULL as integer), "a", 1212 as integer, "b", 11 as integer)); +hex(column_add(column_create("a", NULL as integer), "a", 1212 as integer, "b", 11 as integer)) +040200020000000000010020006162780916 +select hex(column_add(column_create("a", 1212 as integer, "b", 1212 as integer), "a", 11 as integer)); +hex(column_add(column_create("a", 1212 as integer, "b", 1212 as integer), "a", 11 as integer)) +040200020000000000010010006162167809 +select hex(column_add(column_create("a", 1), "a", null)); +hex(column_add(column_create("a", 1), "a", null)) +0400000000 +select column_list(column_add(column_create("a", 1), "a", null)); +column_list(column_add(column_create("a", 1), "a", null)) + +select column_list(column_add(column_create("a", 1), "a", "")); +column_list(column_add(column_create("a", 1), "a", "")) +`a` +select hex(column_add("", "a", 1)); +hex(column_add("", "a", 1)) +0401000100000000006102 +# column delete (names) +select hex(column_delete(column_create("a", 1212 as integer, "b", 1212 as integer), "a")); +hex(column_delete(column_create("a", 1212 as integer, "b", 1212 as integer), "a")) +040100010000000000627809 +select hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "b")); +hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "b")) +0402000200000000000100100061630206 +select hex(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer)); +hex(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer)) +0403000300000000000100100002002000616263020406 +select hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "c")); +hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "c")) +0402000200000000000100100061620204 +select hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "d")); +hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "d")) +0403000300000000000100100002002000616263020406 +select hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "b", "a")); +hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "b", "a")) +0401000100000000006306 +select hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "b", "c")); +hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "b", "c")) +0401000100000000006102 +select hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "a", "b", "c")); +hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "a", "b", "c")) +0400000000 +select hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "a", "b", "c", "e")); +hex(column_delete(column_create("a", 1 as integer, "b", 2 as integer, "c", 3 as integer), "a", "b", "c", "e")) +0400000000 +select hex(column_delete(column_create("a", 1), "a")); +hex(column_delete(column_create("a", 1), "a")) +0400000000 +select hex(column_delete("", "a")); +hex(column_delete("", "a")) + +# +# MDEV-458 DNAMES: Server crashes on using an unquoted string +# as a dynamic column name +# +select COLUMN_CREATE(color, "black"); +ERROR 42S22: Unknown column 'color' in 'field list' +# +# MDEV-489 Assertion `offset < 0x1f' failed in +# type_and_offset_store on COLUMN_ADD +# +CREATE TABLE t1 (f1 tinyblob); +INSERT INTO t1 VALUES (COLUMN_CREATE('col1', REPEAT('a',30))); +select column_check(f1) from t1; +column_check(f1) +1 +UPDATE t1 SET f1 = COLUMN_ADD( f1, REPEAT('b',211), 'val2' ); +Warnings: +Warning 1265 Data truncated for column 'f1' at row 1 +select column_check(f1) from t1; +column_check(f1) +0 +UPDATE t1 SET f1 = COLUMN_ADD( f1, REPEAT('c',211), 'val3' ); +Warnings: +Warning 1265 Data truncated for column 'f1' at row 1 +select column_check(f1) from t1; +column_check(f1) +0 +drop table t1; +# +# MDEV-490/MDEV-491 null as arguments +# +SELECT COLUMN_GET( COLUMN_CREATE( 'col', 'val' ), NULL AS CHAR ); +COLUMN_GET( COLUMN_CREATE( 'col', 'val' ), NULL AS CHAR ) +NULL +SELECT COLUMN_GET( NULL, 'col' as char ); +COLUMN_GET( NULL, 'col' as char ) +NULL +SELECT COLUMN_EXISTS( COLUMN_CREATE( 'col', 'val' ), NULL); +COLUMN_EXISTS( COLUMN_CREATE( 'col', 'val' ), NULL) +NULL +SELECT COLUMN_EXISTS( NULL, 'col'); +COLUMN_EXISTS( NULL, 'col') +NULL +SELECT COLUMN_CREATE( NULL, 'val' ); +COLUMN_CREATE( NULL, 'val' ) +NULL +SELECT COLUMN_ADD( NULL, 'val', 'col'); +COLUMN_ADD( NULL, 'val', 'col') +NULL +# +# MDEV-488: Assertion `column_name->length < 255' failed on a +# column name with length 255 (precisely) +# +SELECT hex(COLUMN_CREATE(REPEAT('a',255),1)); +hex(COLUMN_CREATE(REPEAT('a',255),1)) +040100FF000000000061616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616102 +SELECT hex(COLUMN_CREATE(REPEAT('a',65536),1)); +ERROR 22007: Illegal value used as argument of dynamic column function +# +# JSON conversion +# +select column_json(column_create("int", -1212 as int, "uint", 12334 as unsigned int, "decimal", "23.344" as decimal, "double", 1.23444e50 as double, "string", 'gdgd\\dhdjh"dhdhd' as char, "time", "0:45:49.000001" AS time, "datetime", "2011-04-05 0:45:49.000001" AS datetime, "date", "2011-04-05" AS date)); +column_json(column_create("int", -1212 as int, "uint", 12334 as unsigned int, "decimal", "23.344" as decimal, "double", 1.23444e50 as double, "string", 'gdgd\\dhdjh"dhdhd' as char, "time", "0:45:49.000001" AS time, "datetime", "2011-04-05 0:45:49.000001" +{"int":-1212,"date":"2011-04-05","time":"00:45:49.000001","uint":12334,"double":"1.2e50","string":"gdgd\\dhdjh\"dhdhd","decimal":23.344,"datetime":"2011-04-05 00:45:49.000001"} +select column_json(column_create(1, -1212 as int, 2, 12334 as unsigned int, 3, "23.344" as decimal, 4, 1.23444e50 as double, 5, 'gdgd\\dhdjh"dhdhd' as char, 6, "0:45:49.000001" AS time, 7, "2011-04-05 0:45:49.000001" AS datetime, 8, "2011-04-05" AS date)); +column_json(column_create(1, -1212 as int, 2, 12334 as unsigned int, 3, "23.344" as decimal, 4, 1.23444e50 as double, 5, 'gdgd\\dhdjh"dhdhd' as char, 6, "0:45:49.000001" AS time, 7, "2011-04-05 0:45:49.000001" AS datetime, 8, "2011-04-05" AS date)) +{"1":-1212,"2":12334,"3":23.344,"4":"1.2e50","5":"gdgd\\dhdjh\"dhdhd","6":"00:45:49.000001","7":"2011-04-05 00:45:49.000001","8":"2011-04-05"} +# +# CHECK test +# +SELECT COLUMN_CHECK(COLUMN_CREATE(1,'a')); +COLUMN_CHECK(COLUMN_CREATE(1,'a')) +1 +SELECT COLUMN_CHECK('abracadabra'); +COLUMN_CHECK('abracadabra') +0 +SELECT COLUMN_CHECK(''); +COLUMN_CHECK('') +1 +SELECT COLUMN_CHECK(NULL); +COLUMN_CHECK(NULL) +NULL +# +# escaping check +# +select column_json(column_create("string", "'\"/\\`.,whatever")),hex(column_create("string", "'\"/\\`.,whatever")); +column_json(column_create("string", "'\"/\\`.,whatever")) hex(column_create("string", "'\"/\\`.,whatever")) +{"string":"'\"/\\`.,whatever"} 040100060000000300737472696E670827222F5C602E2C7768617465766572 +# +# embedding test +# +select column_json(column_create("val", "val", "emb", column_create("val2", "val2"))); +column_json(column_create("val", "val", "emb", column_create("val2", "val2"))) +{"emb":{"val2":"val2"},"val":"val"} +select column_json(column_create(1, "val", 2, column_create(3, "val2"))); +column_json(column_create(1, "val", 2, column_create(3, "val2"))) +{"1":"val","2":{"3":"val2"}} +# +# Time encoding +# +select hex(column_create("t", "800:46:06.23434" AS time)) as hex, +column_json(column_create("t", "800:46:06.23434" AS time)) as json; +hex json +04010001000000070074649363B82003 {"t":"800:46:06.234340"} +select hex(column_create(1, "800:46:06.23434" AS time)) as hex, +column_json(column_create(1, "800:46:06.23434" AS time)) as json; +hex json +000100010007649363B82003 {"1":"800:46:06.234340"} +select hex(column_create("t", "800:46:06" AS time)) as hex, +column_json(column_create("t", "800:46:06" AS time)) as json; +hex json +04010001000000070074860B32 {"t":"800:46:06"} +select hex(column_create(1, "800:46:06" AS time)) as hex, +column_json(column_create(1, "800:46:06" AS time)) as json; +hex json +000100010007000060B82003 {"1":"800:46:06"} +select hex(column_create("t", "2012-12-21 10:46:06.23434" AS datetime)) as hex, +column_json(column_create("t", "2012-12-21 10:46:06.23434" AS datetime)) as json; +hex json +0401000100000005007495B90F649363B80A00 {"t":"2012-12-21 10:46:06.234340"} +select hex(column_create(1, "2012-12-21 10:46:06.23434" AS datetime)) as hex, +column_json(column_create(1, "2012-12-21 10:46:06.23434" AS datetime)) as json; +hex json +00010001000595B90F649363B80A00 {"1":"2012-12-21 10:46:06.234340"} +select hex(column_create("t", "2012-12-21 10:46:06" AS datetime)) as hex, +column_json(column_create("t", "2012-12-21 10:46:06" AS datetime)) as json; +hex json +0401000100000005007495B90F86AB00 {"t":"2012-12-21 10:46:06"} +select hex(column_create(1, "2012-12-21 10:46:06" AS datetime)) as hex, +column_json(column_create(1, "2012-12-21 10:46:06" AS datetime)) as json; +hex json +00010001000595B90F000060B80A00 {"1":"2012-12-21 10:46:06"} diff --git a/mysql-test/r/filesort_debug.result b/mysql-test/r/filesort_debug.result index 6d6eb817259..13912ac8756 100644 --- a/mysql-test/r/filesort_debug.result +++ b/mysql-test/r/filesort_debug.result @@ -4,7 +4,7 @@ SET @old_debug= @@session.debug; # CREATE TABLE t1(f0 int auto_increment primary key, f1 int); INSERT INTO t1(f1) VALUES (0),(1),(2),(3),(4),(5); -SET session debug_dbug= '+d,make_sort_keys_alloc_fail'; +SET session debug_dbug= '+d,alloc_sort_buffer_fail'; CALL mtr.add_suppression("Out of sort memory"); SELECT * FROM t1 ORDER BY f1 ASC, f0; ERROR HY001: Out of sort memory, consider increasing server sort buffer size diff --git a/mysql-test/r/flush.result b/mysql-test/r/flush.result index d3b3cd16210..b64351045bf 100644 --- a/mysql-test/r/flush.result +++ b/mysql-test/r/flush.result @@ -489,3 +489,10 @@ UNLOCK TABLES; # Switching to connection 'default'. COMMIT; DROP TABLE t1; +# +# Test flushing slave or relay logs twice +# +flush relay logs,relay logs; +ERROR HY000: Incorrect usage of FLUSH and RELAY LOGS +flush slave,slave; +ERROR HY000: Incorrect usage of FLUSH and SLAVE diff --git a/mysql-test/r/func_system.result b/mysql-test/r/func_system.result index fa05021eb47..c0d63200f82 100644 --- a/mysql-test/r/func_system.result +++ b/mysql-test/r/func_system.result @@ -25,14 +25,14 @@ user() like _latin1"%@%" select charset(user()); charset(user()) utf8 -select version()>="3.23.29"; -version()>="3.23.29" +select version()>="03.23.29"; +version()>="03.23.29" 1 -select version()>=_utf8"3.23.29"; -version()>=_utf8"3.23.29" +select version()>=_utf8"03.23.29"; +version()>=_utf8"03.23.29" 1 -select version()>=_latin1"3.23.29"; -version()>=_latin1"3.23.29" +select version()>=_latin1"03.23.29"; +version()>=_latin1"03.23.29" 1 select charset(version()); charset(version()) diff --git a/mysql-test/r/func_time.result b/mysql-test/r/func_time.result index 38a547843ad..e8526ae8f1f 100644 --- a/mysql-test/r/func_time.result +++ b/mysql-test/r/func_time.result @@ -1023,11 +1023,11 @@ MAKETIME(CAST(-1 AS UNSIGNED), 0, 0) Warnings: Note 1105 Cast to unsigned converted negative integer to it's positive complement Warning 1292 Truncated incorrect time value: '18446744073709551615:00:00' -SELECT EXTRACT(HOUR FROM '100000:02:03'); -EXTRACT(HOUR FROM '100000:02:03') +SELECT EXTRACT(HOUR FROM '10000:02:03'); +EXTRACT(HOUR FROM '10000:02:03') 838 Warnings: -Warning 1292 Truncated incorrect time value: '100000:02:03' +Warning 1292 Truncated incorrect time value: '10000:02:03' CREATE TABLE t1(f1 TIME); INSERT INTO t1 VALUES('916:00:00 a'); Warnings: diff --git a/mysql-test/r/function_defaults.result b/mysql-test/r/function_defaults.result new file mode 100644 index 00000000000..27b9ee0a323 --- /dev/null +++ b/mysql-test/r/function_defaults.result @@ -0,0 +1,3067 @@ +# +# Test of function defaults for any server, including embedded. +# +# +# Function defaults run 1. No microsecond precision. +# +SET TIME_ZONE = "+00:00"; +# +# Test of errors for column data types that dont support function +# defaults. +# +CREATE TABLE t1( a BIT DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a TINYINT DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a SMALLINT DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a MEDIUMINT DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a INT DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a BIGINT DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a FLOAT DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a DECIMAL DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a DATE DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a TIME DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a YEAR DEFAULT CURRENT_TIMESTAMP ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a BIT ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a TINYINT ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a SMALLINT ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a MEDIUMINT ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a INT ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a BIGINT ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a FLOAT ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a DECIMAL ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a DATE ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a TIME ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a YEAR ON UPDATE CURRENT_TIMESTAMP ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +# +# Test that the default clause behaves like NOW() regarding time zones. +# +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +c TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +d TIMESTAMP NULL, +e DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +f DATETIME DEFAULT CURRENT_TIMESTAMP, +g DATETIME ON UPDATE CURRENT_TIMESTAMP, +h DATETIME +); +# 2011-09-27 14:11:08 UTC +SET TIMESTAMP = 1317132668.654321; +SET @old_time_zone = @@TIME_ZONE; +SET TIME_ZONE = "+05:00"; +INSERT INTO t1( d, h ) VALUES ( NOW(), NOW() ); +SELECT * FROM t1; +a b c d e f g h +2011-09-27 19:11:08 2011-09-27 19:11:08 0000-00-00 00:00:00 2011-09-27 19:11:08 2011-09-27 19:11:08 2011-09-27 19:11:08 NULL 2011-09-27 19:11:08 +# 1989-05-13 01:02:03 +SET TIMESTAMP = 611017323.543212; +UPDATE t1 SET d = NOW(), h = NOW(); +SELECT * FROM t1; +a b c d e f g h +1989-05-13 04:02:03 2011-09-27 19:11:08 1989-05-13 04:02:03 1989-05-13 04:02:03 1989-05-13 04:02:03 2011-09-27 19:11:08 1989-05-13 04:02:03 1989-05-13 04:02:03 +SET TIME_ZONE = @old_time_zone; +DROP TABLE t1; +# +# Test of several TIMESTAMP columns with different function defaults. +# +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +c TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +d TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +e TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +f INT +); +# 2011-04-19 07:22:02 UTC +SET TIMESTAMP = 1303197722.534231; +INSERT INTO t1 ( f ) VALUES (1); +SELECT * FROM t1; +a b c d e f +2011-04-19 07:22:02 2011-04-19 07:22:02 2011-04-19 07:22:02 0000-00-00 00:00:00 0000-00-00 00:00:00 1 +# 2011-04-19 07:23:18 UTC +SET TIMESTAMP = 1303197798.132435; +UPDATE t1 SET f = 2; +SELECT * FROM t1; +a b c d e f +2011-04-19 07:23:18 2011-04-19 07:23:18 2011-04-19 07:22:02 2011-04-19 07:23:18 2011-04-19 07:23:18 2 +DROP TABLE t1; +# +# Test of inserted values out of order. +# +CREATE TABLE t1 ( +a INT, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +c TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +d TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +e TIMESTAMP NULL, +f DATETIME, +g DATETIME DEFAULT CURRENT_TIMESTAMP, +h DATETIME ON UPDATE CURRENT_TIMESTAMP, +i DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +j INT +); +# 2011-04-19 07:22:02 UTC +SET TIMESTAMP = 1303197722.534231; +INSERT INTO t1 ( j, a ) VALUES ( 1, 1 ); +SELECT * FROM t1; +a b c d e f g h i j +1 2011-04-19 07:22:02 2011-04-19 07:22:02 0000-00-00 00:00:00 NULL NULL 2011-04-19 07:22:02 NULL 2011-04-19 07:22:02 1 +DROP TABLE t1; +# +# Test of ON DUPLICATE KEY UPDATE +# +CREATE TABLE t1 ( +a INT PRIMARY KEY, +b INT, +c TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +d TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +e TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +f TIMESTAMP NOT NULL DEFAULT '1986-09-27 03:00:00.098765', +g TIMESTAMP NULL, +h DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +i DATETIME DEFAULT CURRENT_TIMESTAMP, +j DATETIME ON UPDATE CURRENT_TIMESTAMP, +k DATETIME NULL, +l DATETIME DEFAULT '1986-09-27 03:00:00.098765' +); +# 1977-12-21 23:00:00 UTC +SET TIMESTAMP = 251593200.192837; +INSERT INTO t1(a) VALUES (1) ON DUPLICATE KEY UPDATE b = 2; +SELECT * FROM t1; +a b c d e f g h i j k l +1 NULL 1977-12-21 23:00:00 1977-12-21 23:00:00 0000-00-00 00:00:00 1986-09-27 03:00:00 NULL 1977-12-21 23:00:00 1977-12-21 23:00:00 NULL NULL 1986-09-27 03:00:00 +# 1975-05-21 23:00:00 UTC +SET TIMESTAMP = 169945200.918273; +INSERT INTO t1(a) VALUES (1) ON DUPLICATE KEY UPDATE b = 2; +SELECT * FROM t1; +a b c d e f g h i j k l +1 2 1975-05-21 23:00:00 1977-12-21 23:00:00 1975-05-21 23:00:00 1986-09-27 03:00:00 NULL 1975-05-21 23:00:00 1977-12-21 23:00:00 1975-05-21 23:00:00 NULL 1986-09-27 03:00:00 +# 1973-08-14 09:11:22 UTC +SET TIMESTAMP = 114167482.534231; +INSERT INTO t1(a) VALUES (2) ON DUPLICATE KEY UPDATE b = 2; +SELECT * FROM t1; +a b c d e f g h i j k l +1 2 1975-05-21 23:00:00 1977-12-21 23:00:00 1975-05-21 23:00:00 1986-09-27 03:00:00 NULL 1975-05-21 23:00:00 1977-12-21 23:00:00 1975-05-21 23:00:00 NULL 1986-09-27 03:00:00 +2 NULL 1973-08-14 09:11:22 1973-08-14 09:11:22 0000-00-00 00:00:00 1986-09-27 03:00:00 NULL 1973-08-14 09:11:22 1973-08-14 09:11:22 NULL NULL 1986-09-27 03:00:00 +DROP TABLE t1; +CREATE TABLE t1 ( a INT PRIMARY KEY, b INT, c TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); +# 2011-04-19 07:23:18 UTC +SET TIMESTAMP = 1303197798.945156; +INSERT INTO t1 VALUES +(1, 0, '2001-01-01 01:01:01.111111'), +(2, 0, '2002-02-02 02:02:02.222222'), +(3, 0, '2003-03-03 03:03:03.333333'); +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01 +2 0 2002-02-02 02:02:02 +3 0 2003-03-03 03:03:03 +UPDATE t1 SET b = 2, c = c WHERE a = 2; +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01 +2 2 2002-02-02 02:02:02 +3 0 2003-03-03 03:03:03 +INSERT INTO t1 (a) VALUES (4); +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01 +2 2 2002-02-02 02:02:02 +3 0 2003-03-03 03:03:03 +4 NULL 2011-04-19 07:23:18 +UPDATE t1 SET c = '2004-04-04 04:04:04.444444' WHERE a = 4; +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01 +2 2 2002-02-02 02:02:02 +3 0 2003-03-03 03:03:03 +4 NULL 2004-04-04 04:04:04 +INSERT INTO t1 ( a ) VALUES ( 3 ), ( 5 ) ON DUPLICATE KEY UPDATE b = 3, c = c; +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01 +2 2 2002-02-02 02:02:02 +3 3 2003-03-03 03:03:03 +4 NULL 2004-04-04 04:04:04 +5 NULL 2011-04-19 07:23:18 +INSERT INTO t1 (a, c) VALUES +(4, '2004-04-04 00:00:00.444444'), +(6, '2006-06-06 06:06:06.666666') +ON DUPLICATE KEY UPDATE b = 4; +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01 +2 2 2002-02-02 02:02:02 +3 3 2003-03-03 03:03:03 +4 4 2011-04-19 07:23:18 +5 NULL 2011-04-19 07:23:18 +6 NULL 2006-06-06 06:06:06 +DROP TABLE t1; +# +# Test of REPLACE INTO executed as UPDATE. +# +CREATE TABLE t1 ( +a INT PRIMARY KEY, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +c DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +d TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +e DATETIME DEFAULT CURRENT_TIMESTAMP, +f TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +g DATETIME ON UPDATE CURRENT_TIMESTAMP, +h TIMESTAMP NULL, +i DATETIME +); +# 1970-09-21 09:11:12 UTC +SET TIMESTAMP = 22756272.163584; +REPLACE INTO t1 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b c d e f g h i +1 1970-09-21 09:11:12 1970-09-21 09:11:12 1970-09-21 09:11:12 1970-09-21 09:11:12 0000-00-00 00:00:00 NULL NULL NULL +# 1970-11-10 14:16:17 UTC +SET TIMESTAMP = 27094577.852954; +REPLACE INTO t1 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b c d e f g h i +1 1970-11-10 14:16:17 1970-11-10 14:16:17 1970-11-10 14:16:17 1970-11-10 14:16:17 0000-00-00 00:00:00 NULL NULL NULL +DROP TABLE t1; +# +# Test of insertion of NULL, DEFAULT and an empty row for DEFAULT +# CURRENT_TIMESTAMP. +# +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +b DATETIME DEFAULT CURRENT_TIMESTAMP, +c INT +); +# 2011-04-20 09:53:41 UTC +SET TIMESTAMP = 1303293221.163578; +INSERT INTO t1 VALUES (NULL, NULL, 1), (DEFAULT, DEFAULT, 2); +INSERT INTO t1 ( a, b, c ) VALUES (NULL, NULL, 3), (DEFAULT, DEFAULT, 4); +SELECT * FROM t1; +a b c +2011-04-20 09:53:41 NULL 1 +2011-04-20 09:53:41 2011-04-20 09:53:41 2 +2011-04-20 09:53:41 NULL 3 +2011-04-20 09:53:41 2011-04-20 09:53:41 4 +SET TIME_ZONE = "+03:00"; +SELECT * FROM t1; +a b c +2011-04-20 12:53:41 NULL 1 +2011-04-20 12:53:41 2011-04-20 09:53:41 2 +2011-04-20 12:53:41 NULL 3 +2011-04-20 12:53:41 2011-04-20 09:53:41 4 +SET TIME_ZONE = "+00:00"; +DROP TABLE t1; +# 2011-04-20 07:05:39 UTC +SET TIMESTAMP = 1303283139.195624; +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT '2010-10-11 12:34:56' ON UPDATE CURRENT_TIMESTAMP, +b DATETIME DEFAULT '2010-10-11 12:34:56' +); +INSERT INTO t1 VALUES (NULL, NULL), (DEFAULT, DEFAULT); +INSERT INTO t1 ( a, b ) VALUES (NULL, NULL), (DEFAULT, DEFAULT); +SELECT * FROM t1; +a b +2011-04-20 07:05:39 NULL +2010-10-11 12:34:56 2010-10-11 12:34:56 +2011-04-20 07:05:39 NULL +2010-10-11 12:34:56 2010-10-11 12:34:56 +DROP TABLE t1; +# 2011-04-20 09:53:41 UTC +SET TIMESTAMP = 1303293221.136952; +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +c TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +d TIMESTAMP NOT NULL DEFAULT '1986-09-27 03:00:00.098765', +e TIMESTAMP NULL, +f DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +g DATETIME DEFAULT CURRENT_TIMESTAMP, +h DATETIME ON UPDATE CURRENT_TIMESTAMP, +i DATETIME NULL, +j DATETIME DEFAULT '1986-09-27 03:00:00.098765' +); +INSERT INTO t1 VALUES (); +INSERT INTO t1 SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL; +SELECT * FROM t1; +a b c d e f g h i j +2011-04-20 09:53:41 2011-04-20 09:53:41 0000-00-00 00:00:00 1986-09-27 03:00:00 NULL 2011-04-20 09:53:41 2011-04-20 09:53:41 NULL NULL 1986-09-27 03:00:00 +2011-04-20 09:53:41 2011-04-20 09:53:41 2011-04-20 09:53:41 2011-04-20 09:53:41 NULL NULL NULL NULL NULL NULL +DROP TABLE t1; +# +# Test of multiple-table UPDATE for DEFAULT CURRENT_TIMESTAMP +# +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +b DATETIME DEFAULT CURRENT_TIMESTAMP, +c INT +); +INSERT INTO t1 ( c ) VALUES (1); +SELECT * FROM t1; +a b c +2011-04-20 09:53:41 2011-04-20 09:53:41 1 +# 2011-04-20 17:06:13 UTC +SET TIMESTAMP = 1303311973.163587; +UPDATE t1 t11, t1 t12 SET t11.c = 1; +SELECT * FROM t1; +a b c +2011-04-20 09:53:41 2011-04-20 09:53:41 1 +UPDATE t1 t11, t1 t12 SET t11.c = 2; +SELECT * FROM t1; +a b c +2011-04-20 15:06:13 2011-04-20 09:53:41 2 +DROP TABLE t1; +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +b TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +c DATETIME DEFAULT CURRENT_TIMESTAMP, +d DATETIME ON UPDATE CURRENT_TIMESTAMP, +e INT +); +CREATE TABLE t2 ( +f INT, +g DATETIME ON UPDATE CURRENT_TIMESTAMP, +h DATETIME DEFAULT CURRENT_TIMESTAMP, +i TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +j TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); +# 1995-03-11 00:02:03 UTC +SET TIMESTAMP = 794880123.195676; +INSERT INTO t1 ( e ) VALUES ( 1 ), ( 2 ); +INSERT INTO t2 ( f ) VALUES ( 1 ), ( 2 ); +SELECT * FROM t1; +a b c d e +1995-03-11 00:02:03 0000-00-00 00:00:00 1995-03-11 00:02:03 NULL 1 +1995-03-11 00:02:03 0000-00-00 00:00:00 1995-03-11 00:02:03 NULL 2 +SELECT * FROM t2; +f g h i j +1 NULL 1995-03-11 00:02:03 0000-00-00 00:00:00 1995-03-11 00:02:03 +2 NULL 1995-03-11 00:02:03 0000-00-00 00:00:00 1995-03-11 00:02:03 +# 1980-12-13 02:02:01 UTC +SET TIMESTAMP = 345520921.196755; +UPDATE t1, t2 SET t1.e = 3, t2.f = 4; +SELECT * FROM t1; +a b c d e +1995-03-11 00:02:03 1980-12-13 02:02:01 1995-03-11 00:02:03 1980-12-13 02:02:01 3 +1995-03-11 00:02:03 1980-12-13 02:02:01 1995-03-11 00:02:03 1980-12-13 02:02:01 3 +SELECT * FROM t2; +f g h i j +4 1980-12-13 02:02:01 1995-03-11 00:02:03 1980-12-13 02:02:01 1995-03-11 00:02:03 +4 1980-12-13 02:02:01 1995-03-11 00:02:03 1980-12-13 02:02:01 1995-03-11 00:02:03 +DROP TABLE t1, t2; +# +# Test of multiple table update with temporary table and on the fly. +# +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +b DATETIME ON UPDATE CURRENT_TIMESTAMP, +c INT, +d INT +); +CREATE TABLE t2 ( +a TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +b DATETIME ON UPDATE CURRENT_TIMESTAMP, +c INT KEY, +d INT +); +INSERT INTO t1 ( c ) VALUES (1), (2); +INSERT INTO t2 ( c ) VALUES (1), (2); +# Test of multiple table update done on the fly +# 2011-04-20 15:06:13 UTC +SET TIMESTAMP = 1303311973.194685; +UPDATE t1 JOIN t2 USING ( c ) SET t2.d = 1; +SELECT * FROM t1; +a b c d +0000-00-00 00:00:00 NULL 1 NULL +0000-00-00 00:00:00 NULL 2 NULL +SELECT * FROM t2; +a b c d +2011-04-20 15:06:13 2011-04-20 15:06:13 1 1 +2011-04-20 15:06:13 2011-04-20 15:06:13 2 1 +# Test of multiple table update done with temporary table. +# 1979-01-15 03:02:01 +SET TIMESTAMP = 285213721.134679; +UPDATE t1 JOIN t2 USING ( c ) SET t1.d = 1; +SELECT * FROM t1; +a b c d +1979-01-15 02:02:01 1979-01-15 02:02:01 1 1 +1979-01-15 02:02:01 1979-01-15 02:02:01 2 1 +SELECT * FROM t2; +a b c d +2011-04-20 15:06:13 2011-04-20 15:06:13 1 1 +2011-04-20 15:06:13 2011-04-20 15:06:13 2 1 +DROP TABLE t1, t2; +# +# Test of ON UPDATE CURRENT_TIMESTAMP. +# +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +b DATETIME ON UPDATE CURRENT_TIMESTAMP, +c INT +); +# 2011-04-20 09:53:41 UTC +SET TIMESTAMP = 1303293221.794613; +INSERT INTO t1 ( c ) VALUES ( 1 ); +SELECT * FROM t1; +a b c +0000-00-00 00:00:00 NULL 1 +UPDATE t1 SET c = 1; +SELECT * FROM t1; +a b c +0000-00-00 00:00:00 NULL 1 +UPDATE t1 SET c = 2; +SELECT * FROM t1; +a b c +2011-04-20 09:53:41 2011-04-20 09:53:41 2 +# +# Test of multiple-table UPDATE for ON UPDATE CURRENT_TIMESTAMP +# +# 2011-04-20 15:06:13 UTC +SET TIMESTAMP = 1303311973.534231; +UPDATE t1 t11, t1 t12 SET t11.c = 2; +SELECT * FROM t1; +a b c +2011-04-20 09:53:41 2011-04-20 09:53:41 2 +UPDATE t1 t11, t1 t12 SET t11.c = 3; +SELECT * FROM t1; +a b c +2011-04-20 15:06:13 2011-04-20 15:06:13 3 +DROP TABLE t1; +# +# Test of a multiple-table update where only one table is updated and +# the updated table has a primary key. +# +CREATE TABLE t1 ( a INT, b INT, PRIMARY KEY (a) ); +INSERT INTO t1 VALUES (1, 1),(2, 2),(3, 3),(4, 4); +CREATE TABLE t2 ( a INT, b INT ); +INSERT INTO t2 VALUES (1, 1),(2, 2),(3, 3),(4, 4),(5, 5); +UPDATE t1, t2 SET t1.b = 100 WHERE t1.a = t2.a; +SELECT * FROM t1; +a b +1 100 +2 100 +3 100 +4 100 +SELECT * FROM t2; +a b +1 1 +2 2 +3 3 +4 4 +5 5 +DROP TABLE t1, t2; +# +# Test of ALTER TABLE, reordering columns. +# +CREATE TABLE t1 ( a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, b INT ); +ALTER TABLE t1 MODIFY a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` int(11) DEFAULT NULL, + `a` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a INT, b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, c TIMESTAMP NULL ); +ALTER TABLE t1 MODIFY b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP FIRST; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `a` int(11) DEFAULT NULL, + `c` timestamp NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a INT, b TIMESTAMP NULL ); +ALTER TABLE t1 MODIFY b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP FIRST; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `a` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, b TIMESTAMP NULL ); +ALTER TABLE t1 MODIFY a TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` timestamp NULL DEFAULT NULL, + `a` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, b TIMESTAMP NULL ); +ALTER TABLE t1 MODIFY a TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` timestamp NULL DEFAULT NULL, + `a` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE NOW(), b INT, c TIMESTAMP NULL ); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, + `b` int(11) DEFAULT NULL, + `c` timestamp NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +ALTER TABLE t1 MODIFY a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` int(11) DEFAULT NULL, + `a` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `c` timestamp NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE NOW(), b INT, c TIMESTAMP NULL ); +ALTER TABLE t1 MODIFY c TIMESTAMP NULL FIRST; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `c` timestamp NULL DEFAULT NULL, + `a` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, + `b` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE CURRENT_TIMESTAMP, b INT, c TIMESTAMP NULL ); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `b` int(11) DEFAULT NULL, + `c` timestamp NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +ALTER TABLE t1 MODIFY a TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE CURRENT_TIMESTAMP AFTER b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` int(11) DEFAULT NULL, + `a` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `c` timestamp NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE CURRENT_TIMESTAMP, b INT, c TIMESTAMP NULL ); +ALTER TABLE t1 MODIFY c TIMESTAMP NULL FIRST; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `c` timestamp NULL DEFAULT NULL, + `a` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `b` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +# +# Test of ALTER TABLE, adding columns. +# +CREATE TABLE t1 ( a INT ); +ALTER TABLE t1 ADD COLUMN b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) DEFAULT NULL, + `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +# +# Test of INSERT SELECT. +# +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +c DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +d DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP +); +CREATE TABLE t2 ( +placeholder1 INT, +placeholder2 INT, +placeholder3 INT, +placeholder4 INT, +a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +b TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00', +c DATETIME, +d DATETIME +); +# 1977-08-16 15:30:01 UTC +SET TIMESTAMP = 240589801.654312; +INSERT INTO t2 (a, b, c, d) VALUES ( +'1977-08-16 15:30:01.123456', +'1977-08-16 15:30:01.234567', +'1977-08-16 15:30:01.345678', +'1977-08-16 15:30:01.456789' +); +# 1986-09-27 01:00:00 UTC +SET TIMESTAMP = 528166800.132435; +INSERT INTO t1 ( a, c ) SELECT a, c FROM t2; +SELECT * FROM t1; +a b c d +1977-08-16 15:30:01 1986-09-27 01:00:00 1977-08-16 15:30:01 1986-09-27 01:00:00 +DROP TABLE t1, t2; +# +# Test of CREATE TABLE SELECT. +# +# We test that the columns of the source table are not used to determine +# function defaults for the receiving table. +# +# 1970-04-11 20:13:57 UTC +SET TIMESTAMP = 8712837.657898; +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +c TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +d TIMESTAMP NOT NULL DEFAULT '1986-09-27 03:00:00.098765', +e TIMESTAMP NULL, +f DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +g DATETIME DEFAULT CURRENT_TIMESTAMP, +h DATETIME ON UPDATE CURRENT_TIMESTAMP, +i DATETIME NULL, +j DATETIME DEFAULT '1986-09-27 03:00:00.098765' +); +INSERT INTO t1 VALUES (); +# 1971-01-31 21:13:57 UTC +SET TIMESTAMP = 34200837.164937; +CREATE TABLE t2 SELECT a FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `a` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t2; +a +1970-04-11 20:13:57 +CREATE TABLE t3 SELECT b FROM t1; +SHOW CREATE TABLE t3; +Table Create Table +t3 CREATE TABLE `t3` ( + `b` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t3; +b +1970-04-11 20:13:57 +CREATE TABLE t4 SELECT c FROM t1; +SHOW CREATE TABLE t4; +Table Create Table +t4 CREATE TABLE `t4` ( + `c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t4; +c +0000-00-00 00:00:00 +CREATE TABLE t5 SELECT d FROM t1; +SHOW CREATE TABLE t5; +Table Create Table +t5 CREATE TABLE `t5` ( + `d` timestamp NOT NULL DEFAULT '1986-09-27 03:00:00' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t5; +d +1986-09-27 03:00:00 +CREATE TABLE t6 SELECT e FROM t1; +SHOW CREATE TABLE t6; +Table Create Table +t6 CREATE TABLE `t6` ( + `e` timestamp NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t6; +e +NULL +CREATE TABLE t7 SELECT f FROM t1; +SHOW CREATE TABLE t7; +Table Create Table +t7 CREATE TABLE `t7` ( + `f` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t7; +f +1970-04-11 20:13:57 +CREATE TABLE t8 SELECT g FROM t1; +SHOW CREATE TABLE t8; +Table Create Table +t8 CREATE TABLE `t8` ( + `g` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t8; +g +1970-04-11 20:13:57 +CREATE TABLE t9 SELECT h FROM t1; +SHOW CREATE TABLE t9; +Table Create Table +t9 CREATE TABLE `t9` ( + `h` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t9; +h +NULL +CREATE TABLE t10 SELECT i FROM t1; +SHOW CREATE TABLE t10; +Table Create Table +t10 CREATE TABLE `t10` ( + `i` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t10; +i +NULL +CREATE TABLE t11 SELECT j FROM t1; +SHOW CREATE TABLE t11; +Table Create Table +t11 CREATE TABLE `t11` ( + `j` datetime DEFAULT '1986-09-27 03:00:00' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t11; +j +1986-09-27 03:00:00 +CREATE TABLE t12 ( +k TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +l TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +m TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +n TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +o TIMESTAMP NOT NULL DEFAULT '1986-09-27 03:00:00.098765', +p TIMESTAMP NULL, +q DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +r DATETIME DEFAULT CURRENT_TIMESTAMP, +s DATETIME ON UPDATE CURRENT_TIMESTAMP, +t DATETIME NULL, +u DATETIME DEFAULT '1986-09-27 03:00:00.098765' +) +SELECT * FROM t1; +SHOW CREATE TABLE t12; +Table Create Table +t12 CREATE TABLE `t12` ( + `k` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `l` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `m` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `n` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, + `o` timestamp NOT NULL DEFAULT '1986-09-27 03:00:00', + `p` timestamp NULL DEFAULT NULL, + `q` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `r` datetime DEFAULT CURRENT_TIMESTAMP, + `s` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, + `t` datetime DEFAULT NULL, + `u` datetime DEFAULT '1986-09-27 03:00:00', + `a` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', + `b` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', + `c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', + `d` timestamp NOT NULL DEFAULT '1986-09-27 03:00:00', + `e` timestamp NULL DEFAULT NULL, + `f` datetime DEFAULT NULL, + `g` datetime DEFAULT NULL, + `h` datetime DEFAULT NULL, + `i` datetime DEFAULT NULL, + `j` datetime DEFAULT '1986-09-27 03:00:00' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12; +# 1970-04-11 20:13:57 UTC +SET TIMESTAMP = 8712837.164953; +CREATE TABLE t1 ( +a DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +b DATETIME DEFAULT CURRENT_TIMESTAMP, +c DATETIME ON UPDATE CURRENT_TIMESTAMP, +d DATETIME NULL, +e DATETIME DEFAULT '1986-09-27 03:00:00.098765' +); +INSERT INTO t1 VALUES (); +# 1971-01-31 20:13:57 UTC +SET TIMESTAMP = 34200837.915736; +CREATE TABLE t2 SELECT a FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `a` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t2; +a +1970-04-11 20:13:57 +CREATE TABLE t3 SELECT b FROM t1; +SHOW CREATE TABLE t3; +Table Create Table +t3 CREATE TABLE `t3` ( + `b` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t3; +b +1970-04-11 20:13:57 +CREATE TABLE t4 SELECT c FROM t1; +SHOW CREATE TABLE t4; +Table Create Table +t4 CREATE TABLE `t4` ( + `c` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t4; +c +NULL +CREATE TABLE t5 SELECT d FROM t1; +SHOW CREATE TABLE t5; +Table Create Table +t5 CREATE TABLE `t5` ( + `d` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t5; +d +NULL +CREATE TABLE t6 SELECT e FROM t1; +SHOW CREATE TABLE t6; +Table Create Table +t6 CREATE TABLE `t6` ( + `e` datetime DEFAULT '1986-09-27 03:00:00' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t6; +e +1986-09-27 03:00:00 +DROP TABLE t1, t2, t3, t4, t5, t6; +# +# Test of a CREATE TABLE SELECT that also declared columns. In this case +# the function default should be de-activated during the execution of the +# CREATE TABLE statement. +# +# 1970-01-01 03:16:40 +SET TIMESTAMP = 1000.987654; +CREATE TABLE t1 ( a INT ); +INSERT INTO t1 VALUES ( 1 ), ( 2 ); +CREATE TABLE t2 ( b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) SELECT a FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `a` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SET TIMESTAMP = 2000.876543; +INSERT INTO t2( a ) VALUES ( 3 ); +SELECT * FROM t2; +b a +0000-00-00 00:00:00 1 +0000-00-00 00:00:00 2 +1970-01-01 00:33:20 3 +DROP TABLE t1, t2; +# +# Test of updating a view. +# +CREATE TABLE t1 ( a INT, b DATETIME DEFAULT CURRENT_TIMESTAMP ); +CREATE TABLE t2 ( a INT, b DATETIME ON UPDATE CURRENT_TIMESTAMP ); +CREATE VIEW v1 AS SELECT * FROM t1; +SHOW CREATE VIEW v1; +View Create View character_set_client collation_connection +v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a`,`t1`.`b` AS `b` from `t1` latin1 latin1_swedish_ci +CREATE VIEW v2 AS SELECT * FROM t2; +SHOW CREATE VIEW v2; +View Create View character_set_client collation_connection +v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS select `t2`.`a` AS `a`,`t2`.`b` AS `b` from `t2` latin1 latin1_swedish_ci +# 1971-01-31 21:13:57 UTC +SET TIMESTAMP = 34200837.348564; +INSERT INTO v1 ( a ) VALUES ( 1 ); +INSERT INTO v2 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b +1 1971-01-31 20:13:57 +SELECT * FROM v1; +a b +1 1971-01-31 20:13:57 +SELECT * FROM t2; +a b +1 NULL +SELECT * FROM v2; +a b +1 NULL +# 1970-04-11 20:13:57 UTC +SET TIMESTAMP = 8712837.567332; +UPDATE v1 SET a = 2; +UPDATE v2 SET a = 2; +SELECT * FROM t1; +a b +2 1971-01-31 20:13:57 +SELECT * FROM v1; +a b +2 1971-01-31 20:13:57 +SELECT * FROM t2; +a b +2 1970-04-11 20:13:57 +SELECT * FROM v2; +a b +2 1970-04-11 20:13:57 +DROP VIEW v1, v2; +DROP TABLE t1, t2; +# +# Test with stored procedures. +# +CREATE TABLE t1 ( +a INT, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +c TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +d TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +e TIMESTAMP NULL, +f DATETIME DEFAULT CURRENT_TIMESTAMP, +g DATETIME ON UPDATE CURRENT_TIMESTAMP +); +CREATE PROCEDURE p1() INSERT INTO test.t1( a ) VALUES ( 1 ); +CREATE PROCEDURE p2() UPDATE t1 SET a = 2 WHERE a = 1; +# 1971-01-31 20:13:57 UTC +SET TIMESTAMP = 34200837.876544; +CALL p1(); +SELECT * FROM t1; +a b c d e f g +1 1971-01-31 20:13:57 1971-01-31 20:13:57 0000-00-00 00:00:00 NULL 1971-01-31 20:13:57 NULL +# 1970-04-11 21:13:57 UTC +SET TIMESTAMP = 8712837.143546; +CALL p2(); +SELECT * FROM t1; +a b c d e f g +2 1970-04-11 20:13:57 1971-01-31 20:13:57 1970-04-11 20:13:57 NULL 1971-01-31 20:13:57 1970-04-11 20:13:57 +DROP PROCEDURE p1; +DROP PROCEDURE p2; +DROP TABLE t1; +# +# Test with triggers. +# +CREATE TABLE t1 ( +a INT, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +c TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +d TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +e TIMESTAMP NULL, +f DATETIME, +g DATETIME DEFAULT CURRENT_TIMESTAMP, +h DATETIME ON UPDATE CURRENT_TIMESTAMP, +i DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP +); +CREATE TABLE t2 ( a INT ); +CREATE TRIGGER t2_trg BEFORE INSERT ON t2 FOR EACH ROW +BEGIN +INSERT INTO t1 ( a ) VALUES ( 1 ); +END| +# 1971-01-31 21:13:57 UTC +SET TIMESTAMP = 34200837.978675; +INSERT INTO t2 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b c d e f g h i +1 1971-01-31 20:13:57 1971-01-31 20:13:57 0000-00-00 00:00:00 NULL NULL 1971-01-31 20:13:57 NULL 1971-01-31 20:13:57 +DROP TRIGGER t2_trg; +CREATE TRIGGER t2_trg BEFORE INSERT ON t2 FOR EACH ROW +BEGIN +UPDATE t1 SET a = 2; +END| +# 1970-04-11 21:13:57 UTC +SET TIMESTAMP = 8712837.456789; +INSERT INTO t2 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b c d e f g h i +2 1970-04-11 20:13:57 1971-01-31 20:13:57 1970-04-11 20:13:57 NULL NULL 1971-01-31 20:13:57 1970-04-11 20:13:57 1970-04-11 20:13:57 +DROP TABLE t1, t2; +# +# Test where the assignment target is not a column. +# +CREATE TABLE t1 ( a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); +CREATE TABLE t2 ( a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); +CREATE TABLE t3 ( a TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP ); +CREATE TABLE t4 ( a TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP ); +CREATE VIEW v1 AS SELECT a COLLATE latin1_german1_ci AS b FROM t1; +CREATE VIEW v2 ( b ) AS SELECT a COLLATE latin1_german1_ci FROM t2; +CREATE VIEW v3 AS SELECT a COLLATE latin1_german1_ci AS b FROM t3; +CREATE VIEW v4 ( b ) AS SELECT a COLLATE latin1_german1_ci FROM t4; +INSERT INTO v1 ( b ) VALUES ( '2007-10-24 00:03:34.010203' ); +SELECT a FROM t1; +a +2007-10-24 00:03:34 +INSERT INTO v2 ( b ) VALUES ( '2007-10-24 00:03:34.010203' ); +SELECT a FROM t2; +a +2007-10-24 00:03:34 +INSERT INTO t3 VALUES (); +UPDATE v3 SET b = '2007-10-24 00:03:34.010203'; +SELECT a FROM t3; +a +2007-10-24 00:03:34 +INSERT INTO t4 VALUES (); +UPDATE v4 SET b = '2007-10-24 00:03:34.010203'; +SELECT a FROM t4; +a +2007-10-24 00:03:34 +DROP VIEW v1, v2, v3, v4; +DROP TABLE t1, t2, t3, t4; +# +# Test of LOAD DATA/XML INFILE +# This tests behavior of function defaults for TIMESTAMP and DATETIME +# columns. during LOAD ... INFILE. +# As can be seen here, a TIMESTAMP column with only ON UPDATE +# CURRENT_TIMESTAMP will still have CURRENT_TIMESTAMP inserted on LOAD +# ... INFILE if the value is missing. For DATETIME columns a NULL value +# is inserted instead. +# +CREATE TABLE t1 ( +a INT, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +c TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +d TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +e TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +f DATETIME, +g DATETIME DEFAULT CURRENT_TIMESTAMP, +h DATETIME ON UPDATE CURRENT_TIMESTAMP, +i DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP +); +CREATE TABLE t2 ( +a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +c TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, +d TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +e DATETIME NOT NULL, +f DATETIME NOT NULL DEFAULT '1977-01-02 12:13:14', +g DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, +h DATETIME ON UPDATE CURRENT_TIMESTAMP NOT NULL, +i DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL +); +SELECT 1 INTO OUTFILE 't3.dat' FROM dual; +SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL +INTO OUTFILE 't4.dat' +FROM dual; +SELECT 1, 2 INTO OUTFILE 't5.dat' FROM dual; +# Mon Aug 1 15:11:19 2011 UTC +SET TIMESTAMP = 1312211479.918273; +LOAD DATA INFILE 't3.dat' INTO TABLE t1; +Warnings: +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +SELECT * FROM t1; +a 1 +b 2011-08-01 15:11:19 +c 2011-08-01 15:11:19 +d 2011-08-01 15:11:19 +e 2011-08-01 15:11:19 +f NULL +g NULL +h NULL +i NULL +LOAD DATA INFILE 't4.dat' INTO TABLE t2; +Warnings: +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'e' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'f' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'g' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'h' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'i' at row 1 +SELECT a FROM t2; +a +2011-08-01 15:11:19 +SELECT b FROM t2; +b +2011-08-01 15:11:19 +SELECT c FROM t2; +c +2011-08-01 15:11:19 +SELECT d FROM t2; +d +2011-08-01 15:11:19 +# As shown here, supplying a NULL value to a non-nullable +# column with no default value results in the zero date. +SELECT e FROM t2; +e +0000-00-00 00:00:00 +# As shown here, supplying a NULL value to a non-nullable column with a +# default value results in the zero date. +SELECT f FROM t2; +f +0000-00-00 00:00:00 +# As shown here, supplying a NULL value to a non-nullable column with a +# default function results in the zero date. +SELECT g FROM t2; +g +0000-00-00 00:00:00 +# As shown here, supplying a NULL value to a non-nullable DATETIME ON +# UPDATE CURRENT_TIMESTAMP column with no default value results in the +# zero date. +SELECT h FROM t2; +h +0000-00-00 00:00:00 +SELECT i FROM t2; +i +0000-00-00 00:00:00 +DELETE FROM t1; +DELETE FROM t2; +# Read t3 file into t1 +# The syntax will cause a different code path to be taken +# (read_fixed_length()) than under the LOAD ... INTO TABLE t1 command +# above. The code in this path is copy-pasted code from the path taken +# under the syntax used in the previous LOAD command. +LOAD DATA INFILE 't3.dat' INTO TABLE t1 +FIELDS TERMINATED BY '' ENCLOSED BY ''; +Warnings: +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +SELECT b FROM t1; +b +2011-08-01 15:11:19 +SELECT c FROM t1; +c +2011-08-01 15:11:19 +SELECT d FROM t1; +d +2011-08-01 15:11:19 +SELECT e FROM t1; +e +2011-08-01 15:11:19 +# Yes, a missing field cannot be NULL using this syntax, so it will +# zero date instead. Says a comment in read_fixed_length() : "No fields +# specified in fields_vars list can be NULL in this format." +# It appears to be by design. This is inconsistent with LOAD DATA INFILE +# syntax in previous test. +SELECT f FROM t1; +f +0000-00-00 00:00:00 +SELECT g FROM t1; +g +0000-00-00 00:00:00 +# See comment above "SELECT f FROM f1". +SELECT h FROM t1; +h +0000-00-00 00:00:00 +SELECT i FROM t1; +i +0000-00-00 00:00:00 +DELETE FROM t1; +LOAD DATA INFILE 't5.dat' INTO TABLE t1 ( a, @dummy ); +SELECT * FROM t1; +a b c d e f g h i +1 2011-08-01 15:11:19 2011-08-01 15:11:19 0000-00-00 00:00:00 2011-08-01 15:11:19 NULL 2011-08-01 15:11:19 NULL 2011-08-01 15:11:19 +SELECT @dummy; +@dummy +2 +DELETE FROM t1; +LOAD DATA INFILE 't3.dat' INTO TABLE t1 ( a ) SET c = '2005-06-06 08:09:10'; +SELECT * FROM t1; +a b c d e f g h i +1 2011-08-01 15:11:19 2005-06-06 08:09:10 0000-00-00 00:00:00 2011-08-01 15:11:19 NULL 2011-08-01 15:11:19 NULL 2011-08-01 15:11:19 +DELETE FROM t1; +LOAD DATA INFILE 't3.dat' INTO TABLE t1 ( a ) SET g = '2005-06-06 08:09:10'; +SELECT * FROM t1; +a b c d e f g h i +1 2011-08-01 15:11:19 2011-08-01 15:11:19 0000-00-00 00:00:00 2011-08-01 15:11:19 NULL 2005-06-06 08:09:10 NULL 2011-08-01 15:11:19 +DELETE FROM t1; +# Load a static XML file +LOAD XML INFILE '../../std_data/onerow.xml' INTO TABLE t1 +ROWS IDENTIFIED BY '<row>'; +Missing tags are treated as NULL +SELECT * FROM t1; +a 1 +b 2011-08-01 15:11:19 +c 2011-08-01 15:11:19 +d 2011-08-01 15:11:19 +e 2011-08-01 15:11:19 +f NULL +g NULL +h NULL +i NULL +DROP TABLE t1, t2; +# +# Similar LOAD DATA tests in another form +# +# All of this test portion has been run on a pre-WL5874 trunk +# (except that like_b and like_c didn't exist) and all result +# differences are a bug. +# Regarding like_b its definition is the same as b's except +# that the constant default is replaced with a function +# default. Our expectation is that like_b would behave +# like b: if b is set to NULL, or set to 0000-00-00, or set to +# its default, then the same should apply to like_b. Same for +# like_c vs c. +# Mon Aug 1 15:11:19 2011 UTC +SET TIMESTAMP = 1312211479.089786; +SELECT 1 INTO OUTFILE "file1.dat" FROM dual; +SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL +INTO OUTFILE "file2.dat" FROM dual; +# Too short row +CREATE TABLE t1 ( +dummy INT, +a DATETIME NULL DEFAULT NULL, +b DATETIME NULL DEFAULT "2011-11-18", +like_b DATETIME NULL DEFAULT CURRENT_TIMESTAMP, +c DATETIME NOT NULL DEFAULT "2011-11-18", +like_c DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, +d TIMESTAMP NULL DEFAULT "2011-05-03" ON UPDATE CURRENT_TIMESTAMP, +e TIMESTAMP NOT NULL DEFAULT "2011-05-03", +f TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +g TIMESTAMP NULL DEFAULT NULL, +h INT NULL, +i INT NOT NULL DEFAULT 42 +); +# There is no promotion +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `dummy` int(11) DEFAULT NULL, + `a` datetime DEFAULT NULL, + `b` datetime DEFAULT '2011-11-18 00:00:00', + `like_b` datetime DEFAULT CURRENT_TIMESTAMP, + `c` datetime NOT NULL DEFAULT '2011-11-18 00:00:00', + `like_c` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `d` timestamp NULL DEFAULT '2011-05-03 00:00:00' ON UPDATE CURRENT_TIMESTAMP, + `e` timestamp NOT NULL DEFAULT '2011-05-03 00:00:00', + `f` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `g` timestamp NULL DEFAULT NULL, + `h` int(11) DEFAULT NULL, + `i` int(11) NOT NULL DEFAULT '42' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +LOAD DATA INFILE "file1.dat" INTO table t1; +Warnings: +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +# It is strange that "like_b" gets NULL when "b" gets 0. But +# this is consistent with how "a" gets NULL when "b" gets 0, +# with how "g" gets NULL when "d" gets 0, and with how "h" gets +# NULL when "i" gets 0. Looks like "DEFAULT +# <non-NULL-constant>" is changed to 0, whereas DEFAULT NULL +# and DEFAULT NOW are changed to NULL. +SELECT * FROM t1; +dummy 1 +a NULL +b 0000-00-00 00:00:00 +like_b NULL +c 0000-00-00 00:00:00 +like_c 0000-00-00 00:00:00 +d 0000-00-00 00:00:00 +e 2011-08-01 15:11:19 +f 2011-08-01 15:11:19 +g NULL +h NULL +i 0 +delete from t1; +alter table t1 +modify f TIMESTAMP NULL default CURRENT_TIMESTAMP; +# There is no promotion +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `dummy` int(11) DEFAULT NULL, + `a` datetime DEFAULT NULL, + `b` datetime DEFAULT '2011-11-18 00:00:00', + `like_b` datetime DEFAULT CURRENT_TIMESTAMP, + `c` datetime NOT NULL DEFAULT '2011-11-18 00:00:00', + `like_c` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `d` timestamp NULL DEFAULT '2011-05-03 00:00:00' ON UPDATE CURRENT_TIMESTAMP, + `e` timestamp NOT NULL DEFAULT '2011-05-03 00:00:00', + `f` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + `g` timestamp NULL DEFAULT NULL, + `h` int(11) DEFAULT NULL, + `i` int(11) NOT NULL DEFAULT '42' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +LOAD DATA INFILE "file1.dat" INTO table t1; +Warnings: +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +SELECT * FROM t1; +dummy 1 +a NULL +b 0000-00-00 00:00:00 +like_b NULL +c 0000-00-00 00:00:00 +like_c 0000-00-00 00:00:00 +d 0000-00-00 00:00:00 +e 2011-08-01 15:11:19 +f NULL +g NULL +h NULL +i 0 +delete from t1; +drop table t1; +# Conclusion derived from trunk's results: +# DATETIME DEFAULT <non-NULL-constant> (b,c) gets 0000-00-00, +# DATETIME DEFAULT NULL (a) gets NULL, +# TIMESTAMP NULL DEFAULT <non-NULL-constant> (d) gets 0000-00-00, +# TIMESTAMP NULL DEFAULT NULL (g) gets NULL, +# TIMESTAMP NULL DEFAULT NOW (f after ALTER) gets NULL, +# TIMESTAMP NOT NULL (f before ALTER, e) gets NOW. +### Loading NULL ### +CREATE TABLE t1 ( +dummy INT, +a DATETIME NULL DEFAULT NULL, +b DATETIME NULL DEFAULT "2011-11-18", +like_b DATETIME NULL DEFAULT CURRENT_TIMESTAMP, +c DATETIME NOT NULL DEFAULT "2011-11-18", +like_c DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, +d TIMESTAMP NULL DEFAULT "2011-05-03" ON UPDATE CURRENT_TIMESTAMP, +e TIMESTAMP NOT NULL DEFAULT "2011-05-03", +f TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +g TIMESTAMP NULL DEFAULT NULL, +h INT NULL, +i INT NOT NULL DEFAULT 42 +); +# There is no promotion +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `dummy` int(11) DEFAULT NULL, + `a` datetime DEFAULT NULL, + `b` datetime DEFAULT '2011-11-18 00:00:00', + `like_b` datetime DEFAULT CURRENT_TIMESTAMP, + `c` datetime NOT NULL DEFAULT '2011-11-18 00:00:00', + `like_c` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `d` timestamp NULL DEFAULT '2011-05-03 00:00:00' ON UPDATE CURRENT_TIMESTAMP, + `e` timestamp NOT NULL DEFAULT '2011-05-03 00:00:00', + `f` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `g` timestamp NULL DEFAULT NULL, + `h` int(11) DEFAULT NULL, + `i` int(11) NOT NULL DEFAULT '42' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +LOAD DATA INFILE "file2.dat" INTO table t1; +Warnings: +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'c' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'like_c' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'i' at row 1 +SELECT * FROM t1; +dummy NULL +a NULL +b NULL +like_b NULL +c 0000-00-00 00:00:00 +like_c 0000-00-00 00:00:00 +d NULL +e 2011-08-01 15:11:19 +f 2011-08-01 15:11:19 +g NULL +h NULL +i 0 +delete from t1; +alter table t1 +modify f TIMESTAMP NULL default CURRENT_TIMESTAMP; +# There is no promotion +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `dummy` int(11) DEFAULT NULL, + `a` datetime DEFAULT NULL, + `b` datetime DEFAULT '2011-11-18 00:00:00', + `like_b` datetime DEFAULT CURRENT_TIMESTAMP, + `c` datetime NOT NULL DEFAULT '2011-11-18 00:00:00', + `like_c` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `d` timestamp NULL DEFAULT '2011-05-03 00:00:00' ON UPDATE CURRENT_TIMESTAMP, + `e` timestamp NOT NULL DEFAULT '2011-05-03 00:00:00', + `f` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + `g` timestamp NULL DEFAULT NULL, + `h` int(11) DEFAULT NULL, + `i` int(11) NOT NULL DEFAULT '42' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +LOAD DATA INFILE "file2.dat" INTO table t1; +Warnings: +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'c' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'like_c' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'i' at row 1 +SELECT * FROM t1; +dummy NULL +a NULL +b NULL +like_b NULL +c 0000-00-00 00:00:00 +like_c 0000-00-00 00:00:00 +d NULL +e 2011-08-01 15:11:19 +f NULL +g NULL +h NULL +i 0 +delete from t1; +# Conclusion derived from trunk's results: +# DATETIME NULL (a,b) gets NULL, +# DATETIME NOT NULL (c) gets 0000-00-00, +# TIMESTAMP NULL (d,f,g) gets NULL, +# TIMESTAMP NOT NULL (e) gets NOW. +drop table t1; +# +# Test of updatable views with check options. The option can be violated +# using ON UPDATE updates which is very strange as this offers a loophole +# in this integrity check. +# +SET TIME_ZONE = "+03:00"; +# 1970-01-01 03:16:40 +SET TIMESTAMP = 1000.123456; +CREATE TABLE t1 ( a INT, b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) ENGINE = INNODB; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) DEFAULT NULL, + `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +INSERT INTO t1 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b +1 1970-01-01 03:16:40 +CREATE VIEW v1 AS SELECT * FROM t1 WHERE b <= '1970-01-01 03:16:40.123456' +WITH CHECK OPTION; +SELECT * FROM v1; +a b +1 1970-01-01 03:16:40 +# 1970-01-01 03:33:20 +SET TIMESTAMP = 2000.000234; +UPDATE v1 SET a = 2; +ERROR HY000: CHECK OPTION failed 'test.v1' +SELECT * FROM t1; +a b +1 1970-01-01 03:16:40 +DROP VIEW v1; +DROP TABLE t1; +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT '1973-08-14 09:11:22.089786' ON UPDATE CURRENT_TIMESTAMP, +c INT KEY +); +# 1973-08-14 09:11:22 UTC +SET TIMESTAMP = 114167482.534231; +INSERT INTO t1 ( c ) VALUES ( 1 ); +CREATE VIEW v1 AS +SELECT * +FROM t1 +WHERE a >= '1973-08-14 09:11:22' +WITH LOCAL CHECK OPTION; +SELECT * FROM v1; +a c +1973-08-14 09:11:22 1 +SET TIMESTAMP = 1.126789; +INSERT INTO v1 ( c ) VALUES ( 1 ) ON DUPLICATE KEY UPDATE c = 2; +ERROR HY000: CHECK OPTION failed 'test.v1' +SELECT * FROM v1; +a c +1973-08-14 09:11:22 1 +DROP VIEW v1; +DROP TABLE t1; +# +# Bug 13095459 - MULTI-TABLE UPDATE MODIFIES A ROW TWICE +# +CREATE TABLE t1 ( +a INT, +b INT, +ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +PRIMARY KEY ( a, ts ) +) ENGINE = INNODB; +INSERT INTO t1( a, b, ts ) VALUES ( 1, 0, '2000-09-28 17:44:34' ); +CREATE TABLE t2 ( a INT ) ENGINE = INNODB; +INSERT INTO t2 VALUES ( 1 ); +UPDATE t1 STRAIGHT_JOIN t2 +SET t1.b = t1.b + 1 +WHERE t1.a = 1 AND t1.ts >= '2000-09-28 00:00:00'; +SELECT b FROM t1; +b +1 +DROP TABLE t1, t2; +# +# Bug#11745578: 17392: ALTER TABLE ADD COLUMN TIMESTAMP DEFAULT +# CURRENT_TIMESTAMP INSERTS ZERO +# +SET timestamp = 1000; +CREATE TABLE t1 ( b INT ); +INSERT INTO t1 VALUES (1); +ALTER TABLE t1 ADD COLUMN a6 DATETIME DEFAULT NOW() ON UPDATE NOW() FIRST; +ALTER TABLE t1 ADD COLUMN a5 DATETIME DEFAULT NOW() FIRST; +ALTER TABLE t1 ADD COLUMN a4 DATETIME ON UPDATE NOW() FIRST; +ALTER TABLE t1 ADD COLUMN a3 TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() FIRST; +ALTER TABLE t1 ADD COLUMN a2 TIMESTAMP NOT NULL DEFAULT NOW() FIRST; +ALTER TABLE t1 ADD COLUMN a1 TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE NOW() FIRST; +ALTER TABLE t1 ADD COLUMN c1 TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE NOW() AFTER b; +ALTER TABLE t1 ADD COLUMN c2 TIMESTAMP NOT NULL DEFAULT NOW() AFTER c1; +ALTER TABLE t1 ADD COLUMN c3 TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() AFTER c2; +ALTER TABLE t1 ADD COLUMN c4 DATETIME ON UPDATE NOW() AFTER c3; +ALTER TABLE t1 ADD COLUMN c5 DATETIME DEFAULT NOW() AFTER c4; +ALTER TABLE t1 ADD COLUMN c6 DATETIME DEFAULT NOW() ON UPDATE NOW() AFTER c5; +SELECT * FROM t1; +a1 a2 a3 a4 a5 a6 b c1 c2 c3 c4 c5 c6 +0000-00-00 00:00:00 1970-01-01 03:16:40 1970-01-01 03:16:40 NULL 1970-01-01 03:16:40 1970-01-01 03:16:40 1 0000-00-00 00:00:00 1970-01-01 03:16:40 1970-01-01 03:16:40 NULL 1970-01-01 03:16:40 1970-01-01 03:16:40 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE CURRENT_TIMESTAMP, b DATETIME DEFAULT NOW() ); +INSERT INTO t1 VALUES (); +SET timestamp = 1000000000; +ALTER TABLE t1 MODIFY COLUMN a TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3); +ALTER TABLE t1 MODIFY COLUMN b DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3); +SELECT * FROM t1; +a b +1970-01-01 03:16:40.000 1970-01-01 03:16:40.000 +DROP TABLE t1; +CREATE TABLE t1 ( +a TIMESTAMP NOT NULL DEFAULT '1999-12-01 11:22:33' ON UPDATE CURRENT_TIMESTAMP, +b DATETIME DEFAULT '1999-12-01 11:22:33' +); +INSERT INTO t1 VALUES (); +ALTER TABLE t1 MODIFY COLUMN a TIMESTAMP DEFAULT NOW(); +ALTER TABLE t1 MODIFY COLUMN b DATETIME DEFAULT NOW(); +INSERT INTO t1 VALUES (); +SELECT * FROM t1; +a b +1999-12-01 11:22:33 1999-12-01 11:22:33 +2001-09-09 04:46:40 2001-09-09 04:46:40 +DROP TABLE t1; +# +# Function defaults run 2. Six digits scale on seconds precision. +# +SET TIME_ZONE = "+00:00"; +# +# Test of errors for column data types that dont support function +# defaults. +# +CREATE TABLE t1( a BIT DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a TINYINT DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a SMALLINT DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a MEDIUMINT DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a INT DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a BIGINT DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a FLOAT DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a DECIMAL DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a DATE DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a TIME DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a YEAR DEFAULT CURRENT_TIMESTAMP(6) ); +ERROR 42000: Invalid default value for 'a' +CREATE TABLE t1( a BIT ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a TINYINT ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a SMALLINT ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a MEDIUMINT ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a INT ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a BIGINT ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a FLOAT ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a DECIMAL ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a DATE ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a TIME ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +CREATE TABLE t1( a YEAR ON UPDATE CURRENT_TIMESTAMP(6) ); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +# +# Test that the default clause behaves like NOW() regarding time zones. +# +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +c TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NULL, +e DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +f DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +g DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +h DATETIME(6) +); +# 2011-09-27 14:11:08 UTC +SET TIMESTAMP = 1317132668.654321; +SET @old_time_zone = @@TIME_ZONE; +SET TIME_ZONE = "+05:00"; +INSERT INTO t1( d, h ) VALUES ( NOW(6), NOW(6) ); +SELECT * FROM t1; +a b c d e f g h +2011-09-27 19:11:08.654321 2011-09-27 19:11:08.654321 0000-00-00 00:00:00.000000 2011-09-27 19:11:08.654321 2011-09-27 19:11:08.654321 2011-09-27 19:11:08.654321 NULL 2011-09-27 19:11:08.654321 +# 1989-05-13 01:02:03 +SET TIMESTAMP = 611017323.543212; +UPDATE t1 SET d = NOW(6), h = NOW(6); +SELECT * FROM t1; +a b c d e f g h +1989-05-13 04:02:03.543212 2011-09-27 19:11:08.654321 1989-05-13 04:02:03.543212 1989-05-13 04:02:03.543212 1989-05-13 04:02:03.543212 2011-09-27 19:11:08.654321 1989-05-13 04:02:03.543212 1989-05-13 04:02:03.543212 +SET TIME_ZONE = @old_time_zone; +DROP TABLE t1; +# +# Test of several TIMESTAMP columns with different function defaults. +# +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +c TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +e TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +f INT +); +# 2011-04-19 07:22:02 UTC +SET TIMESTAMP = 1303197722.534231; +INSERT INTO t1 ( f ) VALUES (1); +SELECT * FROM t1; +a b c d e f +2011-04-19 07:22:02.534231 2011-04-19 07:22:02.534231 2011-04-19 07:22:02.534231 0000-00-00 00:00:00.000000 0000-00-00 00:00:00.000000 1 +# 2011-04-19 07:23:18 UTC +SET TIMESTAMP = 1303197798.132435; +UPDATE t1 SET f = 2; +SELECT * FROM t1; +a b c d e f +2011-04-19 07:23:18.132435 2011-04-19 07:23:18.132435 2011-04-19 07:22:02.534231 2011-04-19 07:23:18.132435 2011-04-19 07:23:18.132435 2 +DROP TABLE t1; +# +# Test of inserted values out of order. +# +CREATE TABLE t1 ( +a INT, +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +c TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +e TIMESTAMP(6) NULL, +f DATETIME(6), +g DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +h DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +i DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +j INT +); +# 2011-04-19 07:22:02 UTC +SET TIMESTAMP = 1303197722.534231; +INSERT INTO t1 ( j, a ) VALUES ( 1, 1 ); +SELECT * FROM t1; +a b c d e f g h i j +1 2011-04-19 07:22:02.534231 2011-04-19 07:22:02.534231 0000-00-00 00:00:00.000000 NULL NULL 2011-04-19 07:22:02.534231 NULL 2011-04-19 07:22:02.534231 1 +DROP TABLE t1; +# +# Test of ON DUPLICATE KEY UPDATE +# +CREATE TABLE t1 ( +a INT PRIMARY KEY, +b INT, +c TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +e TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +f TIMESTAMP(6) NOT NULL DEFAULT '1986-09-27 03:00:00.098765', +g TIMESTAMP(6) NULL, +h DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +i DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +j DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +k DATETIME(6) NULL, +l DATETIME(6) DEFAULT '1986-09-27 03:00:00.098765' +); +# 1977-12-21 23:00:00 UTC +SET TIMESTAMP = 251593200.192837; +INSERT INTO t1(a) VALUES (1) ON DUPLICATE KEY UPDATE b = 2; +SELECT * FROM t1; +a b c d e f g h i j k l +1 NULL 1977-12-21 23:00:00.192837 1977-12-21 23:00:00.192837 0000-00-00 00:00:00.000000 1986-09-27 03:00:00.098765 NULL 1977-12-21 23:00:00.192837 1977-12-21 23:00:00.192837 NULL NULL 1986-09-27 03:00:00.098765 +# 1975-05-21 23:00:00 UTC +SET TIMESTAMP = 169945200.918273; +INSERT INTO t1(a) VALUES (1) ON DUPLICATE KEY UPDATE b = 2; +SELECT * FROM t1; +a b c d e f g h i j k l +1 2 1975-05-21 23:00:00.918273 1977-12-21 23:00:00.192837 1975-05-21 23:00:00.918273 1986-09-27 03:00:00.098765 NULL 1975-05-21 23:00:00.918273 1977-12-21 23:00:00.192837 1975-05-21 23:00:00.918273 NULL 1986-09-27 03:00:00.098765 +# 1973-08-14 09:11:22 UTC +SET TIMESTAMP = 114167482.534231; +INSERT INTO t1(a) VALUES (2) ON DUPLICATE KEY UPDATE b = 2; +SELECT * FROM t1; +a b c d e f g h i j k l +1 2 1975-05-21 23:00:00.918273 1977-12-21 23:00:00.192837 1975-05-21 23:00:00.918273 1986-09-27 03:00:00.098765 NULL 1975-05-21 23:00:00.918273 1977-12-21 23:00:00.192837 1975-05-21 23:00:00.918273 NULL 1986-09-27 03:00:00.098765 +2 NULL 1973-08-14 09:11:22.534231 1973-08-14 09:11:22.534231 0000-00-00 00:00:00.000000 1986-09-27 03:00:00.098765 NULL 1973-08-14 09:11:22.534231 1973-08-14 09:11:22.534231 NULL NULL 1986-09-27 03:00:00.098765 +DROP TABLE t1; +CREATE TABLE t1 ( a INT PRIMARY KEY, b INT, c TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) ); +# 2011-04-19 07:23:18 UTC +SET TIMESTAMP = 1303197798.945156; +INSERT INTO t1 VALUES +(1, 0, '2001-01-01 01:01:01.111111'), +(2, 0, '2002-02-02 02:02:02.222222'), +(3, 0, '2003-03-03 03:03:03.333333'); +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01.111111 +2 0 2002-02-02 02:02:02.222222 +3 0 2003-03-03 03:03:03.333333 +UPDATE t1 SET b = 2, c = c WHERE a = 2; +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01.111111 +2 2 2002-02-02 02:02:02.222222 +3 0 2003-03-03 03:03:03.333333 +INSERT INTO t1 (a) VALUES (4); +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01.111111 +2 2 2002-02-02 02:02:02.222222 +3 0 2003-03-03 03:03:03.333333 +4 NULL 2011-04-19 07:23:18.945156 +UPDATE t1 SET c = '2004-04-04 04:04:04.444444' WHERE a = 4; +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01.111111 +2 2 2002-02-02 02:02:02.222222 +3 0 2003-03-03 03:03:03.333333 +4 NULL 2004-04-04 04:04:04.444444 +INSERT INTO t1 ( a ) VALUES ( 3 ), ( 5 ) ON DUPLICATE KEY UPDATE b = 3, c = c; +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01.111111 +2 2 2002-02-02 02:02:02.222222 +3 3 2003-03-03 03:03:03.333333 +4 NULL 2004-04-04 04:04:04.444444 +5 NULL 2011-04-19 07:23:18.945156 +INSERT INTO t1 (a, c) VALUES +(4, '2004-04-04 00:00:00.444444'), +(6, '2006-06-06 06:06:06.666666') +ON DUPLICATE KEY UPDATE b = 4; +SELECT * FROM t1; +a b c +1 0 2001-01-01 01:01:01.111111 +2 2 2002-02-02 02:02:02.222222 +3 3 2003-03-03 03:03:03.333333 +4 4 2011-04-19 07:23:18.945156 +5 NULL 2011-04-19 07:23:18.945156 +6 NULL 2006-06-06 06:06:06.666666 +DROP TABLE t1; +# +# Test of REPLACE INTO executed as UPDATE. +# +CREATE TABLE t1 ( +a INT PRIMARY KEY, +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +c DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +e DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +f TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +g DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +h TIMESTAMP(6) NULL, +i DATETIME(6) +); +# 1970-09-21 09:11:12 UTC +SET TIMESTAMP = 22756272.163584; +REPLACE INTO t1 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b c d e f g h i +1 1970-09-21 09:11:12.163584 1970-09-21 09:11:12.163584 1970-09-21 09:11:12.163584 1970-09-21 09:11:12.163584 0000-00-00 00:00:00.000000 NULL NULL NULL +# 1970-11-10 14:16:17 UTC +SET TIMESTAMP = 27094577.852954; +REPLACE INTO t1 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b c d e f g h i +1 1970-11-10 14:16:17.852954 1970-11-10 14:16:17.852954 1970-11-10 14:16:17.852954 1970-11-10 14:16:17.852954 0000-00-00 00:00:00.000000 NULL NULL NULL +DROP TABLE t1; +# +# Test of insertion of NULL, DEFAULT and an empty row for DEFAULT +# CURRENT_TIMESTAMP. +# +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +b DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +c INT +); +# 2011-04-20 09:53:41 UTC +SET TIMESTAMP = 1303293221.163578; +INSERT INTO t1 VALUES (NULL, NULL, 1), (DEFAULT, DEFAULT, 2); +INSERT INTO t1 ( a, b, c ) VALUES (NULL, NULL, 3), (DEFAULT, DEFAULT, 4); +SELECT * FROM t1; +a b c +2011-04-20 09:53:41.163578 NULL 1 +2011-04-20 09:53:41.163578 2011-04-20 09:53:41.163578 2 +2011-04-20 09:53:41.163578 NULL 3 +2011-04-20 09:53:41.163578 2011-04-20 09:53:41.163578 4 +SET TIME_ZONE = "+03:00"; +SELECT * FROM t1; +a b c +2011-04-20 12:53:41.163578 NULL 1 +2011-04-20 12:53:41.163578 2011-04-20 09:53:41.163578 2 +2011-04-20 12:53:41.163578 NULL 3 +2011-04-20 12:53:41.163578 2011-04-20 09:53:41.163578 4 +SET TIME_ZONE = "+00:00"; +DROP TABLE t1; +# 2011-04-20 07:05:39 UTC +SET TIMESTAMP = 1303283139.195624; +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT '2010-10-11 12:34:56' ON UPDATE CURRENT_TIMESTAMP(6), +b DATETIME(6) DEFAULT '2010-10-11 12:34:56' +); +INSERT INTO t1 VALUES (NULL, NULL), (DEFAULT, DEFAULT); +INSERT INTO t1 ( a, b ) VALUES (NULL, NULL), (DEFAULT, DEFAULT); +SELECT * FROM t1; +a b +2011-04-20 07:05:39.195624 NULL +2010-10-11 12:34:56.000000 2010-10-11 12:34:56.000000 +2011-04-20 07:05:39.195624 NULL +2010-10-11 12:34:56.000000 2010-10-11 12:34:56.000000 +DROP TABLE t1; +# 2011-04-20 09:53:41 UTC +SET TIMESTAMP = 1303293221.136952; +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +c TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NOT NULL DEFAULT '1986-09-27 03:00:00.098765', +e TIMESTAMP(6) NULL, +f DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +g DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +h DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +i DATETIME(6) NULL, +j DATETIME(6) DEFAULT '1986-09-27 03:00:00.098765' +); +INSERT INTO t1 VALUES (); +INSERT INTO t1 SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL; +SELECT * FROM t1; +a b c d e f g h i j +2011-04-20 09:53:41.136952 2011-04-20 09:53:41.136952 0000-00-00 00:00:00.000000 1986-09-27 03:00:00.098765 NULL 2011-04-20 09:53:41.136952 2011-04-20 09:53:41.136952 NULL NULL 1986-09-27 03:00:00.098765 +2011-04-20 09:53:41.136952 2011-04-20 09:53:41.136952 2011-04-20 09:53:41.136952 2011-04-20 09:53:41.136952 NULL NULL NULL NULL NULL NULL +DROP TABLE t1; +# +# Test of multiple-table UPDATE for DEFAULT CURRENT_TIMESTAMP +# +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +b DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +c INT +); +INSERT INTO t1 ( c ) VALUES (1); +SELECT * FROM t1; +a b c +2011-04-20 09:53:41.136952 2011-04-20 09:53:41.136952 1 +# 2011-04-20 17:06:13 UTC +SET TIMESTAMP = 1303311973.163587; +UPDATE t1 t11, t1 t12 SET t11.c = 1; +SELECT * FROM t1; +a b c +2011-04-20 09:53:41.136952 2011-04-20 09:53:41.136952 1 +UPDATE t1 t11, t1 t12 SET t11.c = 2; +SELECT * FROM t1; +a b c +2011-04-20 15:06:13.163587 2011-04-20 09:53:41.136952 2 +DROP TABLE t1; +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +b TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +c DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +d DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +e INT +); +CREATE TABLE t2 ( +f INT, +g DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +h DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +i TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +j TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) +); +# 1995-03-11 00:02:03 UTC +SET TIMESTAMP = 794880123.195676; +INSERT INTO t1 ( e ) VALUES ( 1 ), ( 2 ); +INSERT INTO t2 ( f ) VALUES ( 1 ), ( 2 ); +SELECT * FROM t1; +a b c d e +1995-03-11 00:02:03.195676 0000-00-00 00:00:00.000000 1995-03-11 00:02:03.195676 NULL 1 +1995-03-11 00:02:03.195676 0000-00-00 00:00:00.000000 1995-03-11 00:02:03.195676 NULL 2 +SELECT * FROM t2; +f g h i j +1 NULL 1995-03-11 00:02:03.195676 0000-00-00 00:00:00.000000 1995-03-11 00:02:03.195676 +2 NULL 1995-03-11 00:02:03.195676 0000-00-00 00:00:00.000000 1995-03-11 00:02:03.195676 +# 1980-12-13 02:02:01 UTC +SET TIMESTAMP = 345520921.196755; +UPDATE t1, t2 SET t1.e = 3, t2.f = 4; +SELECT * FROM t1; +a b c d e +1995-03-11 00:02:03.195676 1980-12-13 02:02:01.196755 1995-03-11 00:02:03.195676 1980-12-13 02:02:01.196755 3 +1995-03-11 00:02:03.195676 1980-12-13 02:02:01.196755 1995-03-11 00:02:03.195676 1980-12-13 02:02:01.196755 3 +SELECT * FROM t2; +f g h i j +4 1980-12-13 02:02:01.196755 1995-03-11 00:02:03.195676 1980-12-13 02:02:01.196755 1995-03-11 00:02:03.195676 +4 1980-12-13 02:02:01.196755 1995-03-11 00:02:03.195676 1980-12-13 02:02:01.196755 1995-03-11 00:02:03.195676 +DROP TABLE t1, t2; +# +# Test of multiple table update with temporary table and on the fly. +# +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +b DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +c INT, +d INT +); +CREATE TABLE t2 ( +a TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +b DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +c INT KEY, +d INT +); +INSERT INTO t1 ( c ) VALUES (1), (2); +INSERT INTO t2 ( c ) VALUES (1), (2); +# Test of multiple table update done on the fly +# 2011-04-20 15:06:13 UTC +SET TIMESTAMP = 1303311973.194685; +UPDATE t1 JOIN t2 USING ( c ) SET t2.d = 1; +SELECT * FROM t1; +a b c d +0000-00-00 00:00:00.000000 NULL 1 NULL +0000-00-00 00:00:00.000000 NULL 2 NULL +SELECT * FROM t2; +a b c d +2011-04-20 15:06:13.194685 2011-04-20 15:06:13.194685 1 1 +2011-04-20 15:06:13.194685 2011-04-20 15:06:13.194685 2 1 +# Test of multiple table update done with temporary table. +# 1979-01-15 03:02:01 +SET TIMESTAMP = 285213721.134679; +UPDATE t1 JOIN t2 USING ( c ) SET t1.d = 1; +SELECT * FROM t1; +a b c d +1979-01-15 02:02:01.134679 1979-01-15 02:02:01.134679 1 1 +1979-01-15 02:02:01.134679 1979-01-15 02:02:01.134679 2 1 +SELECT * FROM t2; +a b c d +2011-04-20 15:06:13.194685 2011-04-20 15:06:13.194685 1 1 +2011-04-20 15:06:13.194685 2011-04-20 15:06:13.194685 2 1 +DROP TABLE t1, t2; +# +# Test of ON UPDATE CURRENT_TIMESTAMP. +# +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +b DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +c INT +); +# 2011-04-20 09:53:41 UTC +SET TIMESTAMP = 1303293221.794613; +INSERT INTO t1 ( c ) VALUES ( 1 ); +SELECT * FROM t1; +a b c +0000-00-00 00:00:00.000000 NULL 1 +UPDATE t1 SET c = 1; +SELECT * FROM t1; +a b c +0000-00-00 00:00:00.000000 NULL 1 +UPDATE t1 SET c = 2; +SELECT * FROM t1; +a b c +2011-04-20 09:53:41.794613 2011-04-20 09:53:41.794613 2 +# +# Test of multiple-table UPDATE for ON UPDATE CURRENT_TIMESTAMP +# +# 2011-04-20 15:06:13 UTC +SET TIMESTAMP = 1303311973.534231; +UPDATE t1 t11, t1 t12 SET t11.c = 2; +SELECT * FROM t1; +a b c +2011-04-20 09:53:41.794613 2011-04-20 09:53:41.794613 2 +UPDATE t1 t11, t1 t12 SET t11.c = 3; +SELECT * FROM t1; +a b c +2011-04-20 15:06:13.534231 2011-04-20 15:06:13.534231 3 +DROP TABLE t1; +# +# Test of a multiple-table update where only one table is updated and +# the updated table has a primary key. +# +CREATE TABLE t1 ( a INT, b INT, PRIMARY KEY (a) ); +INSERT INTO t1 VALUES (1, 1),(2, 2),(3, 3),(4, 4); +CREATE TABLE t2 ( a INT, b INT ); +INSERT INTO t2 VALUES (1, 1),(2, 2),(3, 3),(4, 4),(5, 5); +UPDATE t1, t2 SET t1.b = 100 WHERE t1.a = t2.a; +SELECT * FROM t1; +a b +1 100 +2 100 +3 100 +4 100 +SELECT * FROM t2; +a b +1 1 +2 2 +3 3 +4 4 +5 5 +DROP TABLE t1, t2; +# +# Test of ALTER TABLE, reordering columns. +# +CREATE TABLE t1 ( a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), b INT ); +ALTER TABLE t1 MODIFY a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) AFTER b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` int(11) DEFAULT NULL, + `a` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a INT, b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), c TIMESTAMP(6) NULL ); +ALTER TABLE t1 MODIFY b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) FIRST; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + `a` int(11) DEFAULT NULL, + `c` timestamp(6) NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a INT, b TIMESTAMP(6) NULL ); +ALTER TABLE t1 MODIFY b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) FIRST; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + `a` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), b TIMESTAMP(6) NULL ); +ALTER TABLE t1 MODIFY a TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` timestamp(6) NULL DEFAULT NULL, + `a` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), b TIMESTAMP(6) NULL ); +ALTER TABLE t1 MODIFY a TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` timestamp(6) NULL DEFAULT NULL, + `a` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE NOW(6), b INT, c TIMESTAMP(6) NULL ); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000' ON UPDATE CURRENT_TIMESTAMP(6), + `b` int(11) DEFAULT NULL, + `c` timestamp(6) NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +ALTER TABLE t1 MODIFY a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) AFTER b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` int(11) DEFAULT NULL, + `a` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + `c` timestamp(6) NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE NOW(6), b INT, c TIMESTAMP(6) NULL ); +ALTER TABLE t1 MODIFY c TIMESTAMP(6) NULL FIRST; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `c` timestamp(6) NULL DEFAULT NULL, + `a` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000' ON UPDATE CURRENT_TIMESTAMP(6), + `b` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP(6) NOT NULL DEFAULT NOW(6) ON UPDATE CURRENT_TIMESTAMP(6), b INT, c TIMESTAMP(6) NULL ); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + `b` int(11) DEFAULT NULL, + `c` timestamp(6) NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +ALTER TABLE t1 MODIFY a TIMESTAMP(6) NOT NULL DEFAULT NOW(6) ON UPDATE CURRENT_TIMESTAMP(6) AFTER b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `b` int(11) DEFAULT NULL, + `a` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + `c` timestamp(6) NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP(6) NOT NULL DEFAULT NOW(6) ON UPDATE CURRENT_TIMESTAMP(6), b INT, c TIMESTAMP(6) NULL ); +ALTER TABLE t1 MODIFY c TIMESTAMP(6) NULL FIRST; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `c` timestamp(6) NULL DEFAULT NULL, + `a` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + `b` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +# +# Test of ALTER TABLE, adding columns. +# +CREATE TABLE t1 ( a INT ); +ALTER TABLE t1 ADD COLUMN b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) DEFAULT NULL, + `b` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +# +# Test of INSERT SELECT. +# +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +c DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +d DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) +); +CREATE TABLE t2 ( +placeholder1 INT, +placeholder2 INT, +placeholder3 INT, +placeholder4 INT, +a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +b TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00', +c DATETIME(6), +d DATETIME(6) +); +# 1977-08-16 15:30:01 UTC +SET TIMESTAMP = 240589801.654312; +INSERT INTO t2 (a, b, c, d) VALUES ( +'1977-08-16 15:30:01.123456', +'1977-08-16 15:30:01.234567', +'1977-08-16 15:30:01.345678', +'1977-08-16 15:30:01.456789' +); +# 1986-09-27 01:00:00 UTC +SET TIMESTAMP = 528166800.132435; +INSERT INTO t1 ( a, c ) SELECT a, c FROM t2; +SELECT * FROM t1; +a b c d +1977-08-16 15:30:01.123456 1986-09-27 01:00:00.132435 1977-08-16 15:30:01.345678 1986-09-27 01:00:00.132435 +DROP TABLE t1, t2; +# +# Test of CREATE TABLE SELECT. +# +# We test that the columns of the source table are not used to determine +# function defaults for the receiving table. +# +# 1970-04-11 20:13:57 UTC +SET TIMESTAMP = 8712837.657898; +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +c TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NOT NULL DEFAULT '1986-09-27 03:00:00.098765', +e TIMESTAMP(6) NULL, +f DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +g DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +h DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +i DATETIME(6) NULL, +j DATETIME(6) DEFAULT '1986-09-27 03:00:00.098765' +); +INSERT INTO t1 VALUES (); +# 1971-01-31 21:13:57 UTC +SET TIMESTAMP = 34200837.164937; +CREATE TABLE t2 SELECT a FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `a` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t2; +a +1970-04-11 20:13:57.657897 +CREATE TABLE t3 SELECT b FROM t1; +SHOW CREATE TABLE t3; +Table Create Table +t3 CREATE TABLE `t3` ( + `b` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t3; +b +1970-04-11 20:13:57.657897 +CREATE TABLE t4 SELECT c FROM t1; +SHOW CREATE TABLE t4; +Table Create Table +t4 CREATE TABLE `t4` ( + `c` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t4; +c +0000-00-00 00:00:00.000000 +CREATE TABLE t5 SELECT d FROM t1; +SHOW CREATE TABLE t5; +Table Create Table +t5 CREATE TABLE `t5` ( + `d` timestamp(6) NOT NULL DEFAULT '1986-09-27 03:00:00.098765' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t5; +d +1986-09-27 03:00:00.098765 +CREATE TABLE t6 SELECT e FROM t1; +SHOW CREATE TABLE t6; +Table Create Table +t6 CREATE TABLE `t6` ( + `e` timestamp(6) NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t6; +e +NULL +CREATE TABLE t7 SELECT f FROM t1; +SHOW CREATE TABLE t7; +Table Create Table +t7 CREATE TABLE `t7` ( + `f` datetime(6) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t7; +f +1970-04-11 20:13:57.657897 +CREATE TABLE t8 SELECT g FROM t1; +SHOW CREATE TABLE t8; +Table Create Table +t8 CREATE TABLE `t8` ( + `g` datetime(6) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t8; +g +1970-04-11 20:13:57.657897 +CREATE TABLE t9 SELECT h FROM t1; +SHOW CREATE TABLE t9; +Table Create Table +t9 CREATE TABLE `t9` ( + `h` datetime(6) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t9; +h +NULL +CREATE TABLE t10 SELECT i FROM t1; +SHOW CREATE TABLE t10; +Table Create Table +t10 CREATE TABLE `t10` ( + `i` datetime(6) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t10; +i +NULL +CREATE TABLE t11 SELECT j FROM t1; +SHOW CREATE TABLE t11; +Table Create Table +t11 CREATE TABLE `t11` ( + `j` datetime(6) DEFAULT '1986-09-27 03:00:00.098765' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t11; +j +1986-09-27 03:00:00.098765 +CREATE TABLE t12 ( +k TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +l TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +m TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +n TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +o TIMESTAMP(6) NOT NULL DEFAULT '1986-09-27 03:00:00.098765', +p TIMESTAMP(6) NULL, +q DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +r DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +s DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +t DATETIME(6) NULL, +u DATETIME(6) DEFAULT '1986-09-27 03:00:00.098765' +) +SELECT * FROM t1; +SHOW CREATE TABLE t12; +Table Create Table +t12 CREATE TABLE `t12` ( + `k` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + `l` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + `m` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), + `n` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000' ON UPDATE CURRENT_TIMESTAMP(6), + `o` timestamp(6) NOT NULL DEFAULT '1986-09-27 03:00:00.098765', + `p` timestamp(6) NULL DEFAULT NULL, + `q` datetime(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + `r` datetime(6) DEFAULT CURRENT_TIMESTAMP(6), + `s` datetime(6) DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(6), + `t` datetime(6) DEFAULT NULL, + `u` datetime(6) DEFAULT '1986-09-27 03:00:00.098765', + `a` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000', + `b` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000', + `c` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000', + `d` timestamp(6) NOT NULL DEFAULT '1986-09-27 03:00:00.098765', + `e` timestamp(6) NULL DEFAULT NULL, + `f` datetime(6) DEFAULT NULL, + `g` datetime(6) DEFAULT NULL, + `h` datetime(6) DEFAULT NULL, + `i` datetime(6) DEFAULT NULL, + `j` datetime(6) DEFAULT '1986-09-27 03:00:00.098765' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12; +# 1970-04-11 20:13:57 UTC +SET TIMESTAMP = 8712837.164953; +CREATE TABLE t1 ( +a DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +b DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +c DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +d DATETIME(6) NULL, +e DATETIME(6) DEFAULT '1986-09-27 03:00:00.098765' +); +INSERT INTO t1 VALUES (); +# 1971-01-31 20:13:57 UTC +SET TIMESTAMP = 34200837.915736; +CREATE TABLE t2 SELECT a FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `a` datetime(6) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t2; +a +1970-04-11 20:13:57.164953 +CREATE TABLE t3 SELECT b FROM t1; +SHOW CREATE TABLE t3; +Table Create Table +t3 CREATE TABLE `t3` ( + `b` datetime(6) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t3; +b +1970-04-11 20:13:57.164953 +CREATE TABLE t4 SELECT c FROM t1; +SHOW CREATE TABLE t4; +Table Create Table +t4 CREATE TABLE `t4` ( + `c` datetime(6) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t4; +c +NULL +CREATE TABLE t5 SELECT d FROM t1; +SHOW CREATE TABLE t5; +Table Create Table +t5 CREATE TABLE `t5` ( + `d` datetime(6) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t5; +d +NULL +CREATE TABLE t6 SELECT e FROM t1; +SHOW CREATE TABLE t6; +Table Create Table +t6 CREATE TABLE `t6` ( + `e` datetime(6) DEFAULT '1986-09-27 03:00:00.098765' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM t6; +e +1986-09-27 03:00:00.098765 +DROP TABLE t1, t2, t3, t4, t5, t6; +# +# Test of a CREATE TABLE SELECT that also declared columns. In this case +# the function default should be de-activated during the execution of the +# CREATE TABLE statement. +# +# 1970-01-01 03:16:40 +SET TIMESTAMP = 1000.987654; +CREATE TABLE t1 ( a INT ); +INSERT INTO t1 VALUES ( 1 ), ( 2 ); +CREATE TABLE t2 ( b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)) SELECT a FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `b` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + `a` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SET TIMESTAMP = 2000.876543; +INSERT INTO t2( a ) VALUES ( 3 ); +SELECT * FROM t2; +b a +0000-00-00 00:00:00.000000 1 +0000-00-00 00:00:00.000000 2 +1970-01-01 00:33:20.876543 3 +DROP TABLE t1, t2; +# +# Test of updating a view. +# +CREATE TABLE t1 ( a INT, b DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ); +CREATE TABLE t2 ( a INT, b DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6) ); +CREATE VIEW v1 AS SELECT * FROM t1; +SHOW CREATE VIEW v1; +View Create View character_set_client collation_connection +v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a`,`t1`.`b` AS `b` from `t1` latin1 latin1_swedish_ci +CREATE VIEW v2 AS SELECT * FROM t2; +SHOW CREATE VIEW v2; +View Create View character_set_client collation_connection +v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS select `t2`.`a` AS `a`,`t2`.`b` AS `b` from `t2` latin1 latin1_swedish_ci +# 1971-01-31 21:13:57 UTC +SET TIMESTAMP = 34200837.348564; +INSERT INTO v1 ( a ) VALUES ( 1 ); +INSERT INTO v2 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b +1 1971-01-31 20:13:57.348564 +SELECT * FROM v1; +a b +1 1971-01-31 20:13:57.348564 +SELECT * FROM t2; +a b +1 NULL +SELECT * FROM v2; +a b +1 NULL +# 1970-04-11 20:13:57 UTC +SET TIMESTAMP = 8712837.567332; +UPDATE v1 SET a = 2; +UPDATE v2 SET a = 2; +SELECT * FROM t1; +a b +2 1971-01-31 20:13:57.348564 +SELECT * FROM v1; +a b +2 1971-01-31 20:13:57.348564 +SELECT * FROM t2; +a b +2 1970-04-11 20:13:57.567332 +SELECT * FROM v2; +a b +2 1970-04-11 20:13:57.567332 +DROP VIEW v1, v2; +DROP TABLE t1, t2; +# +# Test with stored procedures. +# +CREATE TABLE t1 ( +a INT, +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +c TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +e TIMESTAMP(6) NULL, +f DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +g DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6) +); +CREATE PROCEDURE p1() INSERT INTO test.t1( a ) VALUES ( 1 ); +CREATE PROCEDURE p2() UPDATE t1 SET a = 2 WHERE a = 1; +# 1971-01-31 20:13:57 UTC +SET TIMESTAMP = 34200837.876544; +CALL p1(); +SELECT * FROM t1; +a b c d e f g +1 1971-01-31 20:13:57.876544 1971-01-31 20:13:57.876544 0000-00-00 00:00:00.000000 NULL 1971-01-31 20:13:57.876544 NULL +# 1970-04-11 21:13:57 UTC +SET TIMESTAMP = 8712837.143546; +CALL p2(); +SELECT * FROM t1; +a b c d e f g +2 1970-04-11 20:13:57.143546 1971-01-31 20:13:57.876544 1970-04-11 20:13:57.143546 NULL 1971-01-31 20:13:57.876544 1970-04-11 20:13:57.143546 +DROP PROCEDURE p1; +DROP PROCEDURE p2; +DROP TABLE t1; +# +# Test with triggers. +# +CREATE TABLE t1 ( +a INT, +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +c TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +e TIMESTAMP(6) NULL, +f DATETIME(6), +g DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +h DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +i DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) +); +CREATE TABLE t2 ( a INT ); +CREATE TRIGGER t2_trg BEFORE INSERT ON t2 FOR EACH ROW +BEGIN +INSERT INTO t1 ( a ) VALUES ( 1 ); +END| +# 1971-01-31 21:13:57 UTC +SET TIMESTAMP = 34200837.978675; +INSERT INTO t2 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b c d e f g h i +1 1971-01-31 20:13:57.978675 1971-01-31 20:13:57.978675 0000-00-00 00:00:00.000000 NULL NULL 1971-01-31 20:13:57.978675 NULL 1971-01-31 20:13:57.978675 +DROP TRIGGER t2_trg; +CREATE TRIGGER t2_trg BEFORE INSERT ON t2 FOR EACH ROW +BEGIN +UPDATE t1 SET a = 2; +END| +# 1970-04-11 21:13:57 UTC +SET TIMESTAMP = 8712837.456789; +INSERT INTO t2 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b c d e f g h i +2 1970-04-11 20:13:57.456789 1971-01-31 20:13:57.978675 1970-04-11 20:13:57.456789 NULL NULL 1971-01-31 20:13:57.978675 1970-04-11 20:13:57.456789 1970-04-11 20:13:57.456789 +DROP TABLE t1, t2; +# +# Test where the assignment target is not a column. +# +CREATE TABLE t1 ( a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) ); +CREATE TABLE t2 ( a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) ); +CREATE TABLE t3 ( a TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6) ); +CREATE TABLE t4 ( a TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6) ); +CREATE VIEW v1 AS SELECT a COLLATE latin1_german1_ci AS b FROM t1; +CREATE VIEW v2 ( b ) AS SELECT a COLLATE latin1_german1_ci FROM t2; +CREATE VIEW v3 AS SELECT a COLLATE latin1_german1_ci AS b FROM t3; +CREATE VIEW v4 ( b ) AS SELECT a COLLATE latin1_german1_ci FROM t4; +INSERT INTO v1 ( b ) VALUES ( '2007-10-24 00:03:34.010203' ); +SELECT a FROM t1; +a +2007-10-24 00:03:34.010203 +INSERT INTO v2 ( b ) VALUES ( '2007-10-24 00:03:34.010203' ); +SELECT a FROM t2; +a +2007-10-24 00:03:34.010203 +INSERT INTO t3 VALUES (); +UPDATE v3 SET b = '2007-10-24 00:03:34.010203'; +SELECT a FROM t3; +a +2007-10-24 00:03:34.010203 +INSERT INTO t4 VALUES (); +UPDATE v4 SET b = '2007-10-24 00:03:34.010203'; +SELECT a FROM t4; +a +2007-10-24 00:03:34.010203 +DROP VIEW v1, v2, v3, v4; +DROP TABLE t1, t2, t3, t4; +# +# Test of LOAD DATA/XML INFILE +# This tests behavior of function defaults for TIMESTAMP and DATETIME +# columns. during LOAD ... INFILE. +# As can be seen here, a TIMESTAMP column with only ON UPDATE +# CURRENT_TIMESTAMP will still have CURRENT_TIMESTAMP inserted on LOAD +# ... INFILE if the value is missing. For DATETIME columns a NULL value +# is inserted instead. +# +CREATE TABLE t1 ( +a INT, +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +c TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +e TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +f DATETIME(6), +g DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), +h DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6), +i DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) +); +CREATE TABLE t2 ( +a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +c TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +e DATETIME(6) NOT NULL, +f DATETIME(6) NOT NULL DEFAULT '1977-01-02 12:13:14', +g DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) NOT NULL, +h DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6) NOT NULL, +i DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) NOT NULL +); +SELECT 1 INTO OUTFILE 't3.dat' FROM dual; +SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL +INTO OUTFILE 't4.dat' +FROM dual; +SELECT 1, 2 INTO OUTFILE 't5.dat' FROM dual; +# Mon Aug 1 15:11:19 2011 UTC +SET TIMESTAMP = 1312211479.918273; +LOAD DATA INFILE 't3.dat' INTO TABLE t1; +Warnings: +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +SELECT * FROM t1; +a 1 +b 2011-08-01 15:11:19.918273 +c 2011-08-01 15:11:19.918273 +d 2011-08-01 15:11:19.918273 +e 2011-08-01 15:11:19.918273 +f NULL +g NULL +h NULL +i NULL +LOAD DATA INFILE 't4.dat' INTO TABLE t2; +Warnings: +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'e' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'f' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'g' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'h' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'i' at row 1 +SELECT a FROM t2; +a +2011-08-01 15:11:19.918273 +SELECT b FROM t2; +b +2011-08-01 15:11:19.918273 +SELECT c FROM t2; +c +2011-08-01 15:11:19.918273 +SELECT d FROM t2; +d +2011-08-01 15:11:19.918273 +# As shown here, supplying a NULL value to a non-nullable +# column with no default value results in the zero date. +SELECT e FROM t2; +e +0000-00-00 00:00:00.000000 +# As shown here, supplying a NULL value to a non-nullable column with a +# default value results in the zero date. +SELECT f FROM t2; +f +0000-00-00 00:00:00.000000 +# As shown here, supplying a NULL value to a non-nullable column with a +# default function results in the zero date. +SELECT g FROM t2; +g +0000-00-00 00:00:00.000000 +# As shown here, supplying a NULL value to a non-nullable DATETIME ON +# UPDATE CURRENT_TIMESTAMP column with no default value results in the +# zero date. +SELECT h FROM t2; +h +0000-00-00 00:00:00.000000 +SELECT i FROM t2; +i +0000-00-00 00:00:00.000000 +DELETE FROM t1; +DELETE FROM t2; +# Read t3 file into t1 +# The syntax will cause a different code path to be taken +# (read_fixed_length()) than under the LOAD ... INTO TABLE t1 command +# above. The code in this path is copy-pasted code from the path taken +# under the syntax used in the previous LOAD command. +LOAD DATA INFILE 't3.dat' INTO TABLE t1 +FIELDS TERMINATED BY '' ENCLOSED BY ''; +Warnings: +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +SELECT b FROM t1; +b +2011-08-01 15:11:19.918273 +SELECT c FROM t1; +c +2011-08-01 15:11:19.918273 +SELECT d FROM t1; +d +2011-08-01 15:11:19.918273 +SELECT e FROM t1; +e +2011-08-01 15:11:19.918273 +# Yes, a missing field cannot be NULL using this syntax, so it will +# zero date instead. Says a comment in read_fixed_length() : "No fields +# specified in fields_vars list can be NULL in this format." +# It appears to be by design. This is inconsistent with LOAD DATA INFILE +# syntax in previous test. +SELECT f FROM t1; +f +0000-00-00 00:00:00.000000 +SELECT g FROM t1; +g +0000-00-00 00:00:00.000000 +# See comment above "SELECT f FROM f1". +SELECT h FROM t1; +h +0000-00-00 00:00:00.000000 +SELECT i FROM t1; +i +0000-00-00 00:00:00.000000 +DELETE FROM t1; +LOAD DATA INFILE 't5.dat' INTO TABLE t1 ( a, @dummy ); +SELECT * FROM t1; +a b c d e f g h i +1 2011-08-01 15:11:19.918273 2011-08-01 15:11:19.918273 0000-00-00 00:00:00.000000 2011-08-01 15:11:19.918273 NULL 2011-08-01 15:11:19.918273 NULL 2011-08-01 15:11:19.918273 +SELECT @dummy; +@dummy +2 +DELETE FROM t1; +LOAD DATA INFILE 't3.dat' INTO TABLE t1 ( a ) SET c = '2005-06-06 08:09:10'; +SELECT * FROM t1; +a b c d e f g h i +1 2011-08-01 15:11:19.918273 2005-06-06 08:09:10.000000 0000-00-00 00:00:00.000000 2011-08-01 15:11:19.918273 NULL 2011-08-01 15:11:19.918273 NULL 2011-08-01 15:11:19.918273 +DELETE FROM t1; +LOAD DATA INFILE 't3.dat' INTO TABLE t1 ( a ) SET g = '2005-06-06 08:09:10'; +SELECT * FROM t1; +a b c d e f g h i +1 2011-08-01 15:11:19.918273 2011-08-01 15:11:19.918273 0000-00-00 00:00:00.000000 2011-08-01 15:11:19.918273 NULL 2005-06-06 08:09:10.000000 NULL 2011-08-01 15:11:19.918273 +DELETE FROM t1; +# Load a static XML file +LOAD XML INFILE '../../std_data/onerow.xml' INTO TABLE t1 +ROWS IDENTIFIED BY '<row>'; +Missing tags are treated as NULL +SELECT * FROM t1; +a 1 +b 2011-08-01 15:11:19.918273 +c 2011-08-01 15:11:19.918273 +d 2011-08-01 15:11:19.918273 +e 2011-08-01 15:11:19.918273 +f NULL +g NULL +h NULL +i NULL +DROP TABLE t1, t2; +# +# Similar LOAD DATA tests in another form +# +# All of this test portion has been run on a pre-WL5874 trunk +# (except that like_b and like_c didn't exist) and all result +# differences are a bug. +# Regarding like_b its definition is the same as b's except +# that the constant default is replaced with a function +# default. Our expectation is that like_b would behave +# like b: if b is set to NULL, or set to 0000-00-00, or set to +# its default, then the same should apply to like_b. Same for +# like_c vs c. +# Mon Aug 1 15:11:19 2011 UTC +SET TIMESTAMP = 1312211479.089786; +SELECT 1 INTO OUTFILE "file1.dat" FROM dual; +SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL +INTO OUTFILE "file2.dat" FROM dual; +# Too short row +CREATE TABLE t1 ( +dummy INT, +a DATETIME(6) NULL DEFAULT NULL, +b DATETIME(6) NULL DEFAULT "2011-11-18", +like_b DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6), +c DATETIME(6) NOT NULL DEFAULT "2011-11-18", +like_c DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NULL DEFAULT "2011-05-03" ON UPDATE CURRENT_TIMESTAMP(6), +e TIMESTAMP(6) NOT NULL DEFAULT "2011-05-03", +f TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +g TIMESTAMP(6) NULL DEFAULT NULL, +h INT NULL, +i INT NOT NULL DEFAULT 42 +); +# There is no promotion +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `dummy` int(11) DEFAULT NULL, + `a` datetime(6) DEFAULT NULL, + `b` datetime(6) DEFAULT '2011-11-18 00:00:00.000000', + `like_b` datetime(6) DEFAULT CURRENT_TIMESTAMP(6), + `c` datetime(6) NOT NULL DEFAULT '2011-11-18 00:00:00.000000', + `like_c` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), + `d` timestamp(6) NULL DEFAULT '2011-05-03 00:00:00.000000' ON UPDATE CURRENT_TIMESTAMP(6), + `e` timestamp(6) NOT NULL DEFAULT '2011-05-03 00:00:00.000000', + `f` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), + `g` timestamp(6) NULL DEFAULT NULL, + `h` int(11) DEFAULT NULL, + `i` int(11) NOT NULL DEFAULT '42' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +LOAD DATA INFILE "file1.dat" INTO table t1; +Warnings: +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +# It is strange that "like_b" gets NULL when "b" gets 0. But +# this is consistent with how "a" gets NULL when "b" gets 0, +# with how "g" gets NULL when "d" gets 0, and with how "h" gets +# NULL when "i" gets 0. Looks like "DEFAULT +# <non-NULL-constant>" is changed to 0, whereas DEFAULT NULL +# and DEFAULT NOW are changed to NULL. +SELECT * FROM t1; +dummy 1 +a NULL +b 0000-00-00 00:00:00.000000 +like_b NULL +c 0000-00-00 00:00:00.000000 +like_c 0000-00-00 00:00:00.000000 +d 0000-00-00 00:00:00.000000 +e 2011-08-01 15:11:19.089786 +f 2011-08-01 15:11:19.089786 +g NULL +h NULL +i 0 +delete from t1; +alter table t1 +modify f TIMESTAMP NULL default CURRENT_TIMESTAMP; +# There is no promotion +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `dummy` int(11) DEFAULT NULL, + `a` datetime(6) DEFAULT NULL, + `b` datetime(6) DEFAULT '2011-11-18 00:00:00.000000', + `like_b` datetime(6) DEFAULT CURRENT_TIMESTAMP(6), + `c` datetime(6) NOT NULL DEFAULT '2011-11-18 00:00:00.000000', + `like_c` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), + `d` timestamp(6) NULL DEFAULT '2011-05-03 00:00:00.000000' ON UPDATE CURRENT_TIMESTAMP(6), + `e` timestamp(6) NOT NULL DEFAULT '2011-05-03 00:00:00.000000', + `f` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + `g` timestamp(6) NULL DEFAULT NULL, + `h` int(11) DEFAULT NULL, + `i` int(11) NOT NULL DEFAULT '42' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +LOAD DATA INFILE "file1.dat" INTO table t1; +Warnings: +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +Warning 1261 Row 1 doesn't contain data for all columns +SELECT * FROM t1; +dummy 1 +a NULL +b 0000-00-00 00:00:00.000000 +like_b NULL +c 0000-00-00 00:00:00.000000 +like_c 0000-00-00 00:00:00.000000 +d 0000-00-00 00:00:00.000000 +e 2011-08-01 15:11:19.089786 +f NULL +g NULL +h NULL +i 0 +delete from t1; +drop table t1; +# Conclusion derived from trunk's results: +# DATETIME DEFAULT <non-NULL-constant> (b,c) gets 0000-00-00, +# DATETIME DEFAULT NULL (a) gets NULL, +# TIMESTAMP NULL DEFAULT <non-NULL-constant> (d) gets 0000-00-00, +# TIMESTAMP NULL DEFAULT NULL (g) gets NULL, +# TIMESTAMP NULL DEFAULT NOW (f after ALTER) gets NULL, +# TIMESTAMP NOT NULL (f before ALTER, e) gets NOW. +### Loading NULL ### +CREATE TABLE t1 ( +dummy INT, +a DATETIME(6) NULL DEFAULT NULL, +b DATETIME(6) NULL DEFAULT "2011-11-18", +like_b DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6), +c DATETIME(6) NOT NULL DEFAULT "2011-11-18", +like_c DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +d TIMESTAMP(6) NULL DEFAULT "2011-05-03" ON UPDATE CURRENT_TIMESTAMP(6), +e TIMESTAMP(6) NOT NULL DEFAULT "2011-05-03", +f TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), +g TIMESTAMP(6) NULL DEFAULT NULL, +h INT NULL, +i INT NOT NULL DEFAULT 42 +); +# There is no promotion +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `dummy` int(11) DEFAULT NULL, + `a` datetime(6) DEFAULT NULL, + `b` datetime(6) DEFAULT '2011-11-18 00:00:00.000000', + `like_b` datetime(6) DEFAULT CURRENT_TIMESTAMP(6), + `c` datetime(6) NOT NULL DEFAULT '2011-11-18 00:00:00.000000', + `like_c` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), + `d` timestamp(6) NULL DEFAULT '2011-05-03 00:00:00.000000' ON UPDATE CURRENT_TIMESTAMP(6), + `e` timestamp(6) NOT NULL DEFAULT '2011-05-03 00:00:00.000000', + `f` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), + `g` timestamp(6) NULL DEFAULT NULL, + `h` int(11) DEFAULT NULL, + `i` int(11) NOT NULL DEFAULT '42' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +LOAD DATA INFILE "file2.dat" INTO table t1; +Warnings: +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'c' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'like_c' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'i' at row 1 +SELECT * FROM t1; +dummy NULL +a NULL +b NULL +like_b NULL +c 0000-00-00 00:00:00.000000 +like_c 0000-00-00 00:00:00.000000 +d NULL +e 2011-08-01 15:11:19.089786 +f 2011-08-01 15:11:19.089786 +g NULL +h NULL +i 0 +delete from t1; +alter table t1 +modify f TIMESTAMP NULL default CURRENT_TIMESTAMP; +# There is no promotion +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `dummy` int(11) DEFAULT NULL, + `a` datetime(6) DEFAULT NULL, + `b` datetime(6) DEFAULT '2011-11-18 00:00:00.000000', + `like_b` datetime(6) DEFAULT CURRENT_TIMESTAMP(6), + `c` datetime(6) NOT NULL DEFAULT '2011-11-18 00:00:00.000000', + `like_c` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), + `d` timestamp(6) NULL DEFAULT '2011-05-03 00:00:00.000000' ON UPDATE CURRENT_TIMESTAMP(6), + `e` timestamp(6) NOT NULL DEFAULT '2011-05-03 00:00:00.000000', + `f` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + `g` timestamp(6) NULL DEFAULT NULL, + `h` int(11) DEFAULT NULL, + `i` int(11) NOT NULL DEFAULT '42' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +LOAD DATA INFILE "file2.dat" INTO table t1; +Warnings: +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'c' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'like_c' at row 1 +Warning 1263 Column set to default value; NULL supplied to NOT NULL column 'i' at row 1 +SELECT * FROM t1; +dummy NULL +a NULL +b NULL +like_b NULL +c 0000-00-00 00:00:00.000000 +like_c 0000-00-00 00:00:00.000000 +d NULL +e 2011-08-01 15:11:19.089786 +f NULL +g NULL +h NULL +i 0 +delete from t1; +# Conclusion derived from trunk's results: +# DATETIME NULL (a,b) gets NULL, +# DATETIME NOT NULL (c) gets 0000-00-00, +# TIMESTAMP NULL (d,f,g) gets NULL, +# TIMESTAMP NOT NULL (e) gets NOW. +drop table t1; +# +# Test of updatable views with check options. The option can be violated +# using ON UPDATE updates which is very strange as this offers a loophole +# in this integrity check. +# +SET TIME_ZONE = "+03:00"; +# 1970-01-01 03:16:40 +SET TIMESTAMP = 1000.123456; +CREATE TABLE t1 ( a INT, b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)) ENGINE = INNODB; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) DEFAULT NULL, + `b` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +INSERT INTO t1 ( a ) VALUES ( 1 ); +SELECT * FROM t1; +a b +1 1970-01-01 03:16:40.123456 +CREATE VIEW v1 AS SELECT * FROM t1 WHERE b <= '1970-01-01 03:16:40.123456' +WITH CHECK OPTION; +SELECT * FROM v1; +a b +1 1970-01-01 03:16:40.123456 +# 1970-01-01 03:33:20 +SET TIMESTAMP = 2000.000234; +UPDATE v1 SET a = 2; +ERROR HY000: CHECK OPTION failed 'test.v1' +SELECT * FROM t1; +a b +1 1970-01-01 03:16:40.123456 +DROP VIEW v1; +DROP TABLE t1; +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT '1973-08-14 09:11:22.089786' ON UPDATE CURRENT_TIMESTAMP(6), +c INT KEY +); +# 1973-08-14 09:11:22 UTC +SET TIMESTAMP = 114167482.534231; +INSERT INTO t1 ( c ) VALUES ( 1 ); +CREATE VIEW v1 AS +SELECT * +FROM t1 +WHERE a >= '1973-08-14 09:11:22' +WITH LOCAL CHECK OPTION; +SELECT * FROM v1; +a c +1973-08-14 09:11:22.089786 1 +SET TIMESTAMP = 1.126789; +INSERT INTO v1 ( c ) VALUES ( 1 ) ON DUPLICATE KEY UPDATE c = 2; +ERROR HY000: CHECK OPTION failed 'test.v1' +SELECT * FROM v1; +a c +1973-08-14 09:11:22.089786 1 +DROP VIEW v1; +DROP TABLE t1; +# +# Bug 13095459 - MULTI-TABLE UPDATE MODIFIES A ROW TWICE +# +CREATE TABLE t1 ( +a INT, +b INT, +ts TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), +PRIMARY KEY ( a, ts ) +) ENGINE = INNODB; +INSERT INTO t1( a, b, ts ) VALUES ( 1, 0, '2000-09-28 17:44:34' ); +CREATE TABLE t2 ( a INT ) ENGINE = INNODB; +INSERT INTO t2 VALUES ( 1 ); +UPDATE t1 STRAIGHT_JOIN t2 +SET t1.b = t1.b + 1 +WHERE t1.a = 1 AND t1.ts >= '2000-09-28 00:00:00'; +SELECT b FROM t1; +b +1 +DROP TABLE t1, t2; +# +# Bug#11745578: 17392: ALTER TABLE ADD COLUMN TIMESTAMP DEFAULT +# CURRENT_TIMESTAMP INSERTS ZERO +# +SET timestamp = 1000; +CREATE TABLE t1 ( b INT ); +INSERT INTO t1 VALUES (1); +ALTER TABLE t1 ADD COLUMN a6 DATETIME(6) DEFAULT NOW(6) ON UPDATE NOW(6) FIRST; +ALTER TABLE t1 ADD COLUMN a5 DATETIME(6) DEFAULT NOW(6) FIRST; +ALTER TABLE t1 ADD COLUMN a4 DATETIME(6) ON UPDATE NOW(6) FIRST; +ALTER TABLE t1 ADD COLUMN a3 TIMESTAMP(6) NOT NULL DEFAULT NOW(6) ON UPDATE NOW(6) FIRST; +ALTER TABLE t1 ADD COLUMN a2 TIMESTAMP(6) NOT NULL DEFAULT NOW(6) FIRST; +ALTER TABLE t1 ADD COLUMN a1 TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE NOW(6) FIRST; +ALTER TABLE t1 ADD COLUMN c1 TIMESTAMP(6) NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE NOW(6) AFTER b; +ALTER TABLE t1 ADD COLUMN c2 TIMESTAMP(6) NOT NULL DEFAULT NOW(6) AFTER c1; +ALTER TABLE t1 ADD COLUMN c3 TIMESTAMP(6) NOT NULL DEFAULT NOW(6) ON UPDATE NOW(6) AFTER c2; +ALTER TABLE t1 ADD COLUMN c4 DATETIME(6) ON UPDATE NOW(6) AFTER c3; +ALTER TABLE t1 ADD COLUMN c5 DATETIME(6) DEFAULT NOW(6) AFTER c4; +ALTER TABLE t1 ADD COLUMN c6 DATETIME(6) DEFAULT NOW(6) ON UPDATE NOW(6) AFTER c5; +SELECT * FROM t1; +a1 a2 a3 a4 a5 a6 b c1 c2 c3 c4 c5 c6 +0000-00-00 00:00:00.000000 1970-01-01 03:16:40.000000 1970-01-01 03:16:40.000000 NULL 1970-01-01 03:16:40.000000 1970-01-01 03:16:40.000000 1 0000-00-00 00:00:00.000000 1970-01-01 03:16:40.000000 1970-01-01 03:16:40.000000 NULL 1970-01-01 03:16:40.000000 1970-01-01 03:16:40.000000 +DROP TABLE t1; +CREATE TABLE t1 ( a TIMESTAMP(6) NOT NULL DEFAULT NOW(6) ON UPDATE CURRENT_TIMESTAMP(6), b DATETIME(6) DEFAULT NOW(6) ); +INSERT INTO t1 VALUES (); +SET timestamp = 1000000000; +ALTER TABLE t1 MODIFY COLUMN a TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3); +ALTER TABLE t1 MODIFY COLUMN b DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3); +SELECT * FROM t1; +a b +1970-01-01 03:16:40.000 1970-01-01 03:16:40.000 +DROP TABLE t1; +CREATE TABLE t1 ( +a TIMESTAMP(6) NOT NULL DEFAULT '1999-12-01 11:22:33' ON UPDATE CURRENT_TIMESTAMP(6), +b DATETIME(6) DEFAULT '1999-12-01 11:22:33' +); +INSERT INTO t1 VALUES (); +ALTER TABLE t1 MODIFY COLUMN a TIMESTAMP(6) DEFAULT NOW(6); +ALTER TABLE t1 MODIFY COLUMN b DATETIME(6) DEFAULT NOW(6); +INSERT INTO t1 VALUES (); +SELECT * FROM t1; +a b +1999-12-01 11:22:33.000000 1999-12-01 11:22:33.000000 +2001-09-09 04:46:40.000000 2001-09-09 04:46:40.000000 +DROP TABLE t1; diff --git a/mysql-test/r/function_defaults_notembedded.result b/mysql-test/r/function_defaults_notembedded.result new file mode 100644 index 00000000000..c54ae14aef4 --- /dev/null +++ b/mysql-test/r/function_defaults_notembedded.result @@ -0,0 +1,171 @@ +# +# Test of function defaults for non-embedded server. +# +# +# Function defaults run 1. No microsecond precision. +# +SET TIME_ZONE = "+00:00"; +# +# Test of INSERT DELAYED ... SET ... +# +# 2011-04-19 08:02:40 UTC +SET TIMESTAMP = 1303200160.123456; +CREATE TABLE t1 ( a INT, b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); +INSERT DELAYED INTO t1 SET a = 1; +FLUSH TABLE t1; +SELECT * FROM t1; +a b +1 2011-04-19 08:02:40 +SELECT * FROM t1 WHERE b = 0; +a b +INSERT DELAYED INTO t1 SET a = 2, b = '1980-01-02 10:20:30.405060'; +FLUSH TABLE t1; +SELECT * FROM t1; +a b +1 2011-04-19 08:02:40 +2 1980-01-02 10:20:30 +DROP TABLE t1; +# +# Test of INSERT DELAYED ... VALUES ... +# +# 2011-04-19 08:04:01 UTC +SET TIMESTAMP = 1303200241.234567; +CREATE TABLE t1 ( a INT, b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); +INSERT DELAYED INTO t1 ( a ) VALUES (1); +FLUSH TABLE t1; +SELECT * FROM t1; +a b +1 2011-04-19 08:04:01 +INSERT DELAYED INTO t1 VALUES (2, '1977-12-19 12:34:56.789123'); +FLUSH TABLE t1; +SELECT * FROM t1; +a b +1 2011-04-19 08:04:01 +2 1977-12-19 12:34:56 +DROP TABLE t1; +# +# Test of a delayed insert handler servicing two insert operations +# with different sets of active defaults. +# +CREATE TABLE t1 ( a INT, b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); +# 2011-04-19 08:04:01 UTC +SET TIMESTAMP = 1303200241.345678; +SET debug_sync = 'before_write_delayed SIGNAL parked WAIT_FOR go'; +INSERT DELAYED INTO t1 ( a ) VALUES (1), (2), (3); +SET debug_sync = 'now WAIT_FOR parked'; +# 2011-04-19 08:04:01 UTC +SET TIME_ZONE="+03:00"; +SET TIMESTAMP = 1303200241.456789; +INSERT DELAYED INTO t1 ( a, b ) VALUES (4, '1977-12-19 12:34:56.789123'), (5, '1977-12-19 12:34:57.891234'), (6, '1977-12-19 12:34:58.912345'); +SET debug_sync = 'now SIGNAL go'; +SELECT * FROM t1; +a b +1 2011-04-19 08:04:01 +2 2011-04-19 08:04:01 +3 2011-04-19 08:04:01 +4 1977-12-19 09:34:56 +5 1977-12-19 09:34:57 +6 1977-12-19 09:34:58 +DROP TABLE t1; +# +# Test of early activation of function defaults. +# +CREATE TABLE t1 ( a INT, b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); +SET TIMESTAMP = 1317235172.987654; +INSERT DELAYED INTO t1 ( a ) VALUES (1), (2), (3); +SET TIMESTAMP = 385503754.876543; +INSERT DELAYED INTO t1 ( a ) VALUES (4), (5), (6); +FLUSH TABLE t1; +SELECT * FROM t1; +a b +1 2011-09-28 18:39:32 +2 2011-09-28 18:39:32 +3 2011-09-28 18:39:32 +4 1982-03-20 20:22:34 +5 1982-03-20 20:22:34 +6 1982-03-20 20:22:34 +DROP TABLE t1; +# +# Function defaults run 2. Six digits scale on seconds precision. +# +SET TIME_ZONE = "+00:00"; +# +# Test of INSERT DELAYED ... SET ... +# +# 2011-04-19 08:02:40 UTC +SET TIMESTAMP = 1303200160.123456; +CREATE TABLE t1 ( a INT, b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)); +INSERT DELAYED INTO t1 SET a = 1; +FLUSH TABLE t1; +SELECT * FROM t1; +a b +1 2011-04-19 08:02:40.123456 +SELECT * FROM t1 WHERE b = 0; +a b +INSERT DELAYED INTO t1 SET a = 2, b = '1980-01-02 10:20:30.405060'; +FLUSH TABLE t1; +SELECT * FROM t1; +a b +1 2011-04-19 08:02:40.123456 +2 1980-01-02 10:20:30.405060 +DROP TABLE t1; +# +# Test of INSERT DELAYED ... VALUES ... +# +# 2011-04-19 08:04:01 UTC +SET TIMESTAMP = 1303200241.234567; +CREATE TABLE t1 ( a INT, b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)); +INSERT DELAYED INTO t1 ( a ) VALUES (1); +FLUSH TABLE t1; +SELECT * FROM t1; +a b +1 2011-04-19 08:04:01.234567 +INSERT DELAYED INTO t1 VALUES (2, '1977-12-19 12:34:56.789123'); +FLUSH TABLE t1; +SELECT * FROM t1; +a b +1 2011-04-19 08:04:01.234567 +2 1977-12-19 12:34:56.789123 +DROP TABLE t1; +# +# Test of a delayed insert handler servicing two insert operations +# with different sets of active defaults. +# +CREATE TABLE t1 ( a INT, b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)); +# 2011-04-19 08:04:01 UTC +SET TIMESTAMP = 1303200241.345678; +SET debug_sync = 'before_write_delayed SIGNAL parked WAIT_FOR go'; +INSERT DELAYED INTO t1 ( a ) VALUES (1), (2), (3); +SET debug_sync = 'now WAIT_FOR parked'; +# 2011-04-19 08:04:01 UTC +SET TIME_ZONE="+03:00"; +SET TIMESTAMP = 1303200241.456789; +INSERT DELAYED INTO t1 ( a, b ) VALUES (4, '1977-12-19 12:34:56.789123'), (5, '1977-12-19 12:34:57.891234'), (6, '1977-12-19 12:34:58.912345'); +SET debug_sync = 'now SIGNAL go'; +SELECT * FROM t1; +a b +1 2011-04-19 08:04:01.345678 +2 2011-04-19 08:04:01.345678 +3 2011-04-19 08:04:01.345678 +4 1977-12-19 09:34:56.789123 +5 1977-12-19 09:34:57.891234 +6 1977-12-19 09:34:58.912345 +DROP TABLE t1; +# +# Test of early activation of function defaults. +# +CREATE TABLE t1 ( a INT, b TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)); +SET TIMESTAMP = 1317235172.987654; +INSERT DELAYED INTO t1 ( a ) VALUES (1), (2), (3); +SET TIMESTAMP = 385503754.876543; +INSERT DELAYED INTO t1 ( a ) VALUES (4), (5), (6); +FLUSH TABLE t1; +SELECT * FROM t1; +a b +1 2011-09-28 18:39:32.987654 +2 2011-09-28 18:39:32.987654 +3 2011-09-28 18:39:32.987654 +4 1982-03-20 20:22:34.876543 +5 1982-03-20 20:22:34.876543 +6 1982-03-20 20:22:34.876543 +DROP TABLE t1; diff --git a/mysql-test/r/group_by.result b/mysql-test/r/group_by.result index 9f9dcd59d56..8ca2762b190 100644 --- a/mysql-test/r/group_by.result +++ b/mysql-test/r/group_by.result @@ -2490,3 +2490,48 @@ i GROUP_CONCAT( d1, d2 ORDER BY d1, d2 ) 2 22.2 NULL 11.1,22.2 DROP TABLE t1; +# +# Bug #58782 +# Missing rows with SELECT .. WHERE .. IN subquery +# with full GROUP BY and no aggr +# +CREATE TABLE t1 ( +pk INT NOT NULL, +col_int_nokey INT, +PRIMARY KEY (pk) +); +INSERT INTO t1 VALUES (10,7); +INSERT INTO t1 VALUES (11,1); +INSERT INTO t1 VALUES (12,5); +INSERT INTO t1 VALUES (13,3); +SELECT pk AS field1, col_int_nokey AS field2 +FROM t1 +WHERE col_int_nokey > 0 +GROUP BY field1, field2; +field1 field2 +10 7 +11 1 +12 5 +13 3 +CREATE TABLE where_subselect +SELECT pk AS field1, col_int_nokey AS field2 +FROM t1 +WHERE col_int_nokey > 0 +GROUP BY field1, field2 +; +SELECT * +FROM where_subselect +WHERE (field1, field2) IN ( +SELECT pk AS field1, col_int_nokey AS field2 +FROM t1 +WHERE col_int_nokey > 0 +GROUP BY field1, field2 +); +field1 field2 +10 7 +11 1 +12 5 +13 3 +DROP TABLE t1; +DROP TABLE where_subselect; +# End of Bug #58782 diff --git a/mysql-test/r/have_debug_sync.require b/mysql-test/r/have_debug_sync.require deleted file mode 100644 index c2090bc5657..00000000000 --- a/mysql-test/r/have_debug_sync.require +++ /dev/null @@ -1,2 +0,0 @@ -debug_sync -1 diff --git a/mysql-test/r/information_schema.result b/mysql-test/r/information_schema.result index 98d16bf713c..a30fe668490 100644 --- a/mysql-test/r/information_schema.result +++ b/mysql-test/r/information_schema.result @@ -81,6 +81,7 @@ TRIGGERS USER_PRIVILEGES USER_STATISTICS VIEWS +column_stats columns_priv db event @@ -91,6 +92,7 @@ help_keyword help_relation help_topic host +index_stats plugin proc procs_priv @@ -102,6 +104,7 @@ t2 t3 t4 t5 +table_stats tables_priv time_zone time_zone_leap_second @@ -120,6 +123,7 @@ TABLE_CONSTRAINTS TABLE_CONSTRAINTS TABLE_PRIVILEGES TABLE_PRIVILEGES TABLE_STATISTICS TABLE_STATISTICS TRIGGERS TRIGGERS +table_stats table_stats tables_priv tables_priv time_zone time_zone time_zone_leap_second time_zone_leap_second @@ -141,6 +145,7 @@ TABLE_CONSTRAINTS TABLE_CONSTRAINTS TABLE_PRIVILEGES TABLE_PRIVILEGES TABLE_STATISTICS TABLE_STATISTICS TRIGGERS TRIGGERS +table_stats table_stats tables_priv tables_priv time_zone time_zone time_zone_leap_second time_zone_leap_second @@ -162,6 +167,7 @@ TABLE_CONSTRAINTS TABLE_CONSTRAINTS TABLE_PRIVILEGES TABLE_PRIVILEGES TABLE_STATISTICS TABLE_STATISTICS TRIGGERS TRIGGERS +table_stats table_stats tables_priv tables_priv time_zone time_zone time_zone_leap_second time_zone_leap_second diff --git a/mysql-test/r/information_schema_all_engines.result b/mysql-test/r/information_schema_all_engines.result index 4dac9a3e53c..455f2e36ffc 100644 --- a/mysql-test/r/information_schema_all_engines.result +++ b/mysql-test/r/information_schema_all_engines.result @@ -418,4 +418,4 @@ Wildcard: inf_rmation_schema SELECT table_schema, count(*) FROM information_schema.TABLES WHERE table_schema IN ('mysql', 'INFORMATION_SCHEMA', 'test', 'mysqltest') AND table_name<>'ndb_binlog_index' AND table_name<>'ndb_apply_status' GROUP BY TABLE_SCHEMA; table_schema count(*) information_schema 58 -mysql 23 +mysql 26 diff --git a/mysql-test/r/innodb_mysql_sync.result b/mysql-test/r/innodb_mysql_sync.result index 58254412c5b..044e582ceb6 100644 --- a/mysql-test/r/innodb_mysql_sync.result +++ b/mysql-test/r/innodb_mysql_sync.result @@ -18,10 +18,10 @@ SET DEBUG_SYNC='now SIGNAL table_altered'; # Complete optimization Table Op Msg_type Msg_text test.t1 optimize note Table does not support optimize, doing recreate + analyze instead -test.t1 optimize error Got error -1 from storage engine +test.t1 optimize error Got error -1 "Internal error < 0 (Not system error)" from storage engine test.t1 optimize status Operation failed Warnings: -Error 1030 Got error -1 from storage engine +Error 1030 Got error -1 "Internal error < 0 (Not system error)" from storage engine DROP TABLE t1; SET DEBUG_SYNC='RESET'; # diff --git a/mysql-test/r/is_debug_build.require b/mysql-test/r/is_debug_build.require deleted file mode 100644 index 4d77bcdc1ed..00000000000 --- a/mysql-test/r/is_debug_build.require +++ /dev/null @@ -1,2 +0,0 @@ -instr(version(), "debug") > 0 -1 diff --git a/mysql-test/r/join.result b/mysql-test/r/join.result index ba16d7dd9de..1d045d0a58d 100644 --- a/mysql-test/r/join.result +++ b/mysql-test/r/join.result @@ -1473,3 +1473,44 @@ dog_id dog_id birthday dog_id t_id birthday dog_id t_id birthday a_id dog_id SET optimizer_switch=@tmp_optimizer_switch; DROP TABLE t1,t2,t3,t4,t5; SET optimizer_switch=@save_optimizer_switch; +# +# Bug #35268: Parser can't handle STRAIGHT_JOIN with USING +# +CREATE TABLE t1 (a int); +INSERT INTO t1 (a) VALUES (1),(2),(3),(4),(5),(6),(7),(8); +CREATE TABLE t2 (a int); +INSERT INTO t2 (a) VALUES (1),(2),(3),(4); +EXPLAIN +SELECT t1.a FROM t1 NATURAL INNER JOIN t2 ORDER BY t1.a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort +1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join) +SELECT t1.a FROM t1 NATURAL INNER JOIN t2 ORDER BY t1.a; +a +1 +2 +3 +4 +EXPLAIN +SELECT t1.a FROM t1 STRAIGHT_JOIN t2 USING(a) ORDER BY t1.a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using temporary; Using filesort +1 SIMPLE t2 ALL NULL NULL NULL NULL 4 Using where; Using join buffer (flat, BNL join) +SELECT t1.a FROM t1 STRAIGHT_JOIN t2 USING(a) ORDER BY t1.a; +a +1 +2 +3 +4 +EXPLAIN +SELECT t1.a FROM t1 NATURAL STRAIGHT_JOIN t2 ORDER BY t1.a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using temporary; Using filesort +1 SIMPLE t2 ALL NULL NULL NULL NULL 4 Using where; Using join buffer (flat, BNL join) +SELECT t1.a FROM t1 NATURAL STRAIGHT_JOIN t2 ORDER BY t1.a; +a +1 +2 +3 +4 +DROP TABLE t1,t2; diff --git a/mysql-test/r/loaddata.result b/mysql-test/r/loaddata.result index 328d26eec72..932c1c76027 100644 --- a/mysql-test/r/loaddata.result +++ b/mysql-test/r/loaddata.result @@ -5,8 +5,8 @@ Warnings: Warning 1265 Data truncated for column 'a' at row 1 Warning 1265 Data truncated for column 'c' at row 1 Warning 1265 Data truncated for column 'd' at row 1 -Warning 1264 Out of range value for column 'a' at row 2 -Warning 1264 Out of range value for column 'b' at row 2 +Warning 1265 Data truncated for column 'a' at row 2 +Warning 1265 Data truncated for column 'b' at row 2 Warning 1265 Data truncated for column 'd' at row 2 load data infile '../../std_data/loaddata1.dat' into table t1 fields terminated by ',' IGNORE 2 LINES; SELECT * from t1; @@ -20,7 +20,7 @@ load data infile '../../std_data/loaddata1.dat' into table t1 fields terminated Warnings: Warning 1265 Data truncated for column 'c' at row 1 Warning 1265 Data truncated for column 'd' at row 1 -Warning 1264 Out of range value for column 'b' at row 2 +Warning 1265 Data truncated for column 'b' at row 2 Warning 1265 Data truncated for column 'd' at row 2 SELECT * from t1; a b c d diff --git a/mysql-test/r/log_slow.result b/mysql-test/r/log_slow.result index 75e92e7a0b5..76cf45631bd 100644 --- a/mysql-test/r/log_slow.result +++ b/mysql-test/r/log_slow.result @@ -44,7 +44,7 @@ select @@log_slow_verbosity; innodb show fields from mysql.slow_log; Field Type Null Key Default Extra -start_time timestamp(6) NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP +start_time timestamp(6) NO CURRENT_TIMESTAMP(6) on update CURRENT_TIMESTAMP user_host mediumtext NO NULL query_time time(6) NO NULL lock_time time(6) NO NULL diff --git a/mysql-test/r/log_tables.result b/mysql-test/r/log_tables.result index 62775f3db4d..dd3bee0ac88 100644 --- a/mysql-test/r/log_tables.result +++ b/mysql-test/r/log_tables.result @@ -53,7 +53,7 @@ ERROR HY000: You can't use locks with log tables. show create table mysql.general_log; Table Create Table general_log CREATE TABLE `general_log` ( - `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `thread_id` int(11) NOT NULL, `server_id` int(10) unsigned NOT NULL, @@ -62,7 +62,7 @@ general_log CREATE TABLE `general_log` ( ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log' show fields from mysql.general_log; Field Type Null Key Default Extra -event_time timestamp(6) NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP +event_time timestamp(6) NO CURRENT_TIMESTAMP(6) on update CURRENT_TIMESTAMP user_host mediumtext NO NULL thread_id int(11) NO NULL server_id int(10) unsigned NO NULL @@ -71,7 +71,7 @@ argument mediumtext NO NULL show create table mysql.slow_log; Table Create Table slow_log CREATE TABLE `slow_log` ( - `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `query_time` time(6) NOT NULL, `lock_time` time(6) NOT NULL, @@ -85,7 +85,7 @@ slow_log CREATE TABLE `slow_log` ( ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log' show fields from mysql.slow_log; Field Type Null Key Default Extra -start_time timestamp(6) NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP +start_time timestamp(6) NO CURRENT_TIMESTAMP(6) on update CURRENT_TIMESTAMP user_host mediumtext NO NULL query_time time(6) NO NULL lock_time time(6) NO NULL @@ -164,7 +164,7 @@ set global slow_query_log='OFF'; show create table mysql.general_log; Table Create Table general_log CREATE TABLE `general_log` ( - `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `thread_id` int(11) NOT NULL, `server_id` int(10) unsigned NOT NULL, @@ -174,7 +174,7 @@ general_log CREATE TABLE `general_log` ( show create table mysql.slow_log; Table Create Table slow_log CREATE TABLE `slow_log` ( - `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `query_time` time(6) NOT NULL, `lock_time` time(6) NOT NULL, @@ -191,7 +191,7 @@ alter table mysql.slow_log engine=myisam; show create table mysql.general_log; Table Create Table general_log CREATE TABLE `general_log` ( - `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `thread_id` int(11) NOT NULL, `server_id` int(10) unsigned NOT NULL, @@ -201,7 +201,7 @@ general_log CREATE TABLE `general_log` ( show create table mysql.slow_log; Table Create Table slow_log CREATE TABLE `slow_log` ( - `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `query_time` time(6) NOT NULL, `lock_time` time(6) NOT NULL, diff --git a/mysql-test/r/log_tables_upgrade.result b/mysql-test/r/log_tables_upgrade.result index 5ed59eecc31..66194267380 100644 --- a/mysql-test/r/log_tables_upgrade.result +++ b/mysql-test/r/log_tables_upgrade.result @@ -19,6 +19,7 @@ mtr mtr.global_suppressions OK mtr.test_suppressions OK mysql +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -28,6 +29,7 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK @@ -35,6 +37,7 @@ mysql.procs_priv OK mysql.proxies_priv OK mysql.renamed_general_log OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK diff --git a/mysql-test/r/lowercase_table2.result b/mysql-test/r/lowercase_table2.result index abd16641b56..c478d792e52 100644 --- a/mysql-test/r/lowercase_table2.result +++ b/mysql-test/r/lowercase_table2.result @@ -272,7 +272,7 @@ Database Table In_use Name_locked test t_bug44738_uppercase 0 0 # So attempt to create table with the same name should fail. create table t_bug44738_UPPERCASE (i int); -ERROR HY000: Can't find file: 't_bug44738_uppercase' (errno: 2) +ERROR HY000: Can't find file: 't_bug44738_uppercase' (errno: 2 "No such file or directory") # And should succeed after FLUSH TABLES. flush tables; create table t_bug44738_UPPERCASE (i int); diff --git a/mysql-test/r/mdev-504.result b/mysql-test/r/mdev-504.result new file mode 100644 index 00000000000..e178127cf2a --- /dev/null +++ b/mysql-test/r/mdev-504.result @@ -0,0 +1,21 @@ +CREATE TABLE A ( +pk INTEGER AUTO_INCREMENT PRIMARY KEY, +fdate DATE +) ENGINE=MyISAM; +CREATE PROCEDURE p_analyze() +BEGIN +DECLARE attempts INTEGER DEFAULT 100; +wl_loop: WHILE attempts > 0 DO +ANALYZE TABLE A; +SET attempts = attempts - 1; +END WHILE wl_loop; +END | +CREATE FUNCTION rnd3() RETURNS INT +BEGIN +RETURN ROUND(3 * RAND() + 0.5); +END | +SET GLOBAL use_stat_tables = PREFERABLY; +DROP TABLE A; +DROP PROCEDURE p_analyze; +DROP FUNCTION rnd3; +SET GLOBAL use_stat_tables = DEFAULT; diff --git a/mysql-test/r/myisam-system.result b/mysql-test/r/myisam-system.result index b3ba8066f5c..924e7885814 100644 --- a/mysql-test/r/myisam-system.result +++ b/mysql-test/r/myisam-system.result @@ -2,7 +2,7 @@ drop table if exists t1,t2; create table t1 (a int) engine=myisam; drop table if exists t1; Warnings: -Warning 2 Can't find file: 't1' (errno: 2) +Warning 2 Can't find file: 't1' (errno: 2 "No such file or directory") create table t1 (a int) engine=myisam; drop table t1; Got one of the listed errors diff --git a/mysql-test/r/myisam.result b/mysql-test/r/myisam.result index c969b237293..1460467a91a 100644 --- a/mysql-test/r/myisam.result +++ b/mysql-test/r/myisam.result @@ -1697,7 +1697,7 @@ t1 CREATE TABLE `t1` ( ) ENGINE=MyISAM DEFAULT CHARSET=latin1 drop table t1; create table t1 (v varchar(65535)); -ERROR 42000: Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs +ERROR 42000: Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs set storage_engine=MyISAM; set @save_concurrent_insert=@@concurrent_insert; set global concurrent_insert=1; diff --git a/mysql-test/r/myisampack.result b/mysql-test/r/myisampack.result index 56f61ccdf47..2cefa385048 100644 --- a/mysql-test/r/myisampack.result +++ b/mysql-test/r/myisampack.result @@ -113,7 +113,7 @@ ERROR 42S02: Table 'test.t3' doesn't exist # ===== myisampack.4 ===== #Tests the myisampack join operation with an existing destination .frm,.MYI,.MDI #the command should fail with exit status 2 -myisampack: Can't create/write to file (Errcode: 17) +myisampack: Can't create/write to file (Errcode: 17 "File exists") Aborted: file is not compressed DROP TABLE t1,t2,t3; DROP TABLE mysql_db1.t1; diff --git a/mysql-test/r/mysql_upgrade.result b/mysql-test/r/mysql_upgrade.result index 247c2b80d62..1bd2944f86e 100644 --- a/mysql-test/r/mysql_upgrade.result +++ b/mysql-test/r/mysql_upgrade.result @@ -7,6 +7,7 @@ mtr mtr.global_suppressions OK mtr.test_suppressions OK mysql +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -16,12 +17,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK @@ -44,6 +47,7 @@ mtr mtr.global_suppressions OK mtr.test_suppressions OK mysql +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -53,12 +57,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK @@ -81,6 +87,7 @@ mtr mtr.global_suppressions OK mtr.test_suppressions OK mysql +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -90,12 +97,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK @@ -121,6 +130,7 @@ mtr mtr.global_suppressions OK mtr.test_suppressions OK mysql +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -130,12 +140,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK @@ -164,6 +176,7 @@ mtr mtr.global_suppressions OK mtr.test_suppressions OK mysql +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -173,12 +186,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK @@ -210,6 +225,7 @@ mtr mtr.global_suppressions OK mtr.test_suppressions OK mysql +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -219,12 +235,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK @@ -259,6 +277,7 @@ mtr mtr.global_suppressions OK mtr.test_suppressions OK mysql +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -268,12 +287,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK diff --git a/mysql-test/r/mysql_upgrade_ssl.result b/mysql-test/r/mysql_upgrade_ssl.result index a08e7c115cc..29a33a7e8ac 100644 --- a/mysql-test/r/mysql_upgrade_ssl.result +++ b/mysql-test/r/mysql_upgrade_ssl.result @@ -9,6 +9,7 @@ mtr mtr.global_suppressions OK mtr.test_suppressions OK mysql +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -18,12 +19,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK diff --git a/mysql-test/r/mysqlbinlog.result b/mysql-test/r/mysqlbinlog.result index 823c7dc8629..5c297284e06 100644 --- a/mysql-test/r/mysqlbinlog.result +++ b/mysql-test/r/mysqlbinlog.result @@ -924,6 +924,7 @@ DROP DATABASE test1; FLUSH LOGS; show binlog events in 'master-bin.000002' from <binlog_start>; Log_name Pos Event_type Server_id End_log_pos Info +master-bin.000002 # Binlog_checkpoint # # master-bin.000002 master-bin.000002 # Query # # CREATE DATABASE test1 master-bin.000002 # Query # # use `test1`; CREATE TABLE t1(id int) master-bin.000002 # Query # # DROP DATABASE test1 diff --git a/mysql-test/r/mysqlcheck.result b/mysql-test/r/mysqlcheck.result index ab707ceef80..034913a72e6 100644 --- a/mysql-test/r/mysqlcheck.result +++ b/mysql-test/r/mysqlcheck.result @@ -3,6 +3,7 @@ drop view if exists v1; drop database if exists client_test_db; mtr.global_suppressions OK mtr.test_suppressions OK +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -12,12 +13,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK @@ -27,6 +30,7 @@ mysql.time_zone_transition_type OK mysql.user OK mtr.global_suppressions Table is already up to date mtr.test_suppressions Table is already up to date +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -36,12 +40,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK @@ -49,6 +55,7 @@ mysql.time_zone_name OK mysql.time_zone_transition OK mysql.time_zone_transition_type OK mysql.user OK +mysql.column_stats OK mysql.columns_priv OK mysql.db OK mysql.event OK @@ -58,12 +65,14 @@ mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK +mysql.index_stats OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.proxies_priv OK mysql.servers OK +mysql.table_stats OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK @@ -71,6 +80,7 @@ mysql.time_zone_name OK mysql.time_zone_transition OK mysql.time_zone_transition_type OK mysql.user OK +mysql.column_stats Table is already up to date mysql.columns_priv Table is already up to date mysql.db Table is already up to date mysql.event Table is already up to date @@ -80,12 +90,14 @@ mysql.help_keyword Table is already up to date mysql.help_relation Table is already up to date mysql.help_topic Table is already up to date mysql.host Table is already up to date +mysql.index_stats Table is already up to date mysql.ndb_binlog_index Table is already up to date mysql.plugin Table is already up to date mysql.proc Table is already up to date mysql.procs_priv Table is already up to date mysql.proxies_priv Table is already up to date mysql.servers Table is already up to date +mysql.table_stats Table is already up to date mysql.tables_priv Table is already up to date mysql.time_zone Table is already up to date mysql.time_zone_leap_second Table is already up to date diff --git a/mysql-test/r/mysqld--help.result b/mysql-test/r/mysqld--help.result index c36cc96bb1a..d29631eabc0 100644 --- a/mysql-test/r/mysqld--help.result +++ b/mysql-test/r/mysqld--help.result @@ -361,8 +361,7 @@ The following options may be given as the first argument: --max-binlog-cache-size=# Sets the total size of the transactional cache --max-binlog-size=# Binary log will be rotated automatically when the size - exceeds this value. Will also apply to relay logs if - max_relay_log_size is 0 + exceeds this value. --max-binlog-stmt-cache-size=# Sets the total size of the statement cache --max-connect-errors=# @@ -388,9 +387,9 @@ The following options may be given as the first argument: --max-prepared-stmt-count=# Maximum number of prepared statements in the server --max-relay-log-size=# - If non-zero: relay log will be rotated automatically when - the size exceeds this value; if zero: when the size - exceeds max_binlog_size + relay log will be rotated automatically when the size + exceeds this value. If 0 are startup, it's set to + max_binlog_size --max-seeks-for-key=# Limit assumed max number of seeks when looking up rows based on a key @@ -499,8 +498,8 @@ The following options may be given as the first argument: mrr_cost_based, mrr_sort_keys, optimize_join_buffer_size, outer_join_with_cache, partial_match_rowid_merge, partial_match_table_scan, semijoin, semijoin_with_cache, - subquery_cache, table_elimination, extended_keys } and - val is one of {on, off, default} + subquery_cache, table_elimination, extended_keys, + exists_to_in } and val is one of {on, off, default} --performance-schema Enable the performance schema. --performance-schema-events-waits-history-long-size=# @@ -745,7 +744,7 @@ The following options may be given as the first argument: The maximum packet length to sent successfully from the master to slave. --slave-net-timeout=# - Number of seconds to wait for more data from a + Number of seconds to wait for more data from any master/slave connection before aborting the read --slave-skip-errors=name Tells the slave thread to continue replication when a @@ -843,6 +842,9 @@ The following options may be given as the first argument: Prohibit update of a VIEW, which does not contain a key of the underlying table and the query uses a LIMIT clause (usually get from GUI tools) + --use-stat-tables=name + Specifies how to use system statistics tables. Possible + values are NEVER, COMPLEMENTARY, PREVERABLY -u, --user=name Run mysqld daemon as user. --userstat Enables statistics gathering for USER_STATISTICS, CLIENT_STATISTICS, INDEX_STATISTICS and TABLE_STATISTICS @@ -970,7 +972,7 @@ max-join-size 18446744073709551615 max-length-for-sort-data 1024 max-long-data-size 1048576 max-prepared-stmt-count 16382 -max-relay-log-size 0 +max-relay-log-size 1073741824 max-seeks-for-key 18446744073709551615 max-sort-length 1024 max-sp-recursion-depth 0 @@ -1096,6 +1098,7 @@ transaction-alloc-block-size 8192 transaction-isolation REPEATABLE-READ transaction-prealloc-size 4096 updatable-views-with-limit YES +use-stat-tables NEVER userstat FALSE verbose TRUE wait-timeout 28800 diff --git a/mysql-test/r/mysqldump-max.result b/mysql-test/r/mysqldump-max.result index 6722f308358..fb0be20c119 100644 --- a/mysql-test/r/mysqldump-max.result +++ b/mysql-test/r/mysqldump-max.result @@ -332,12 +332,12 @@ a b 2 1 DROP TABLE t1; DROP TABLE t2; -SHOW BINLOG EVENTS LIMIT 6,3; +SHOW BINLOG EVENTS LIMIT 7,3; Log_name Pos Event_type Server_id End_log_pos Info -master-bin.000001 663 Query 1 731 BEGIN -master-bin.000001 731 Query 1 828 use `test`; INSERT INTO t2 VALUES (1,0), (2,0) -master-bin.000001 828 Xid 1 855 COMMIT /* XID */ --- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=855; +master-bin.000001 704 Query 1 772 BEGIN +master-bin.000001 772 Query 1 869 use `test`; INSERT INTO t2 VALUES (1,0), (2,0) +master-bin.000001 869 Xid 1 896 COMMIT /* XID */ +-- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=896; SELECT * FROM t1 ORDER BY a; a 1 diff --git a/mysql-test/r/mysqldump.result b/mysql-test/r/mysqldump.result index e3d5df479b3..da96d843273 100644 --- a/mysql-test/r/mysqldump.result +++ b/mysql-test/r/mysqldump.result @@ -5239,7 +5239,7 @@ Error 1146 Table 'mysql.event' doesn't exist SHOW CREATE TABLE mysql.general_log; Table Create Table general_log CREATE TABLE `general_log` ( - `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `thread_id` int(11) NOT NULL, `server_id` int(10) unsigned NOT NULL, @@ -5249,7 +5249,7 @@ general_log CREATE TABLE `general_log` ( SHOW CREATE TABLE mysql.slow_log; Table Create Table slow_log CREATE TABLE `slow_log` ( - `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `query_time` time(6) NOT NULL, `lock_time` time(6) NOT NULL, diff --git a/mysql-test/r/mysqltest.result b/mysql-test/r/mysqltest.result index a3043aed711..811861a6d93 100644 --- a/mysql-test/r/mysqltest.result +++ b/mysql-test/r/mysqltest.result @@ -232,7 +232,7 @@ mysqltest: At line 2: Spurious text after `query` expression mysqltest: At line 1: Missing argument(s) to 'error' mysqltest: At line 1: Missing argument(s) to 'error' mysqltest: At line 1: The sqlstate definition must start with an uppercase S -mysqltest: At line 1: The error name definition must start with an uppercase E +mysqltest: At line 1: The error name definition must start with an uppercase E or W mysqltest: At line 1: Invalid argument to error: '9eeeee' - the errno may only consist of digits[0-9] mysqltest: At line 1: Invalid argument to error: '1sssss' - the errno may only consist of digits[0-9] mysqltest: At line 1: The sqlstate must be exactly 5 chars long diff --git a/mysql-test/r/not_openssl.require b/mysql-test/r/not_openssl.require deleted file mode 100644 index 2b5e423999c..00000000000 --- a/mysql-test/r/not_openssl.require +++ /dev/null @@ -1,2 +0,0 @@ -Variable_name Value -have_openssl NO diff --git a/mysql-test/r/order_by.result b/mysql-test/r/order_by.result index 94e7d5e757a..054dc9e4fc4 100644 --- a/mysql-test/r/order_by.result +++ b/mysql-test/r/order_by.result @@ -1439,6 +1439,7 @@ CALL mtr.add_suppression("Out of sort memory"); select * from t1 order by b; ERROR HY001: Out of sort memory, consider increasing server sort buffer size drop table t1; +set session sort_buffer_size= 30000; # # Bug #39844: Query Crash Mysql Server 5.0.67 # @@ -1533,6 +1534,890 @@ ppfcz1 DE ppfcz1 14 8 57.5 ppfcz1 DE ppfcz1 14 9 59.5 ppfcz1 DE ppfcz1 14 10 61.5 DROP TABLE t1,t2,t3; +# +# WL#1393 - Optimizing filesort with small limit +# +CREATE TABLE t1(f0 int auto_increment primary key, f1 int, f2 varchar(200)); +INSERT INTO t1(f1, f2) VALUES +(0,"0"),(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5"), +(6,"6"),(7,"7"),(8,"8"),(9,"9"),(10,"10"), +(11,"11"),(12,"12"),(13,"13"),(14,"14"),(15,"15"), +(16,"16"),(17,"17"),(18,"18"),(19,"19"),(20,"20"), +(21,"21"),(22,"22"),(23,"23"),(24,"24"),(25,"25"), +(26,"26"),(27,"27"),(28,"28"),(29,"29"),(30,"30"), +(31,"31"),(32,"32"),(33,"33"),(34,"34"),(35,"35"), +(36,"36"),(37,"37"),(38,"38"),(39,"39"),(40,"40"), +(41,"41"),(42,"42"),(43,"43"),(44,"44"),(45,"45"), +(46,"46"),(47,"47"),(48,"48"),(49,"49"),(50,"50"), +(51,"51"),(52,"52"),(53,"53"),(54,"54"),(55,"55"), +(56,"56"),(57,"57"),(58,"58"),(59,"59"),(60,"60"), +(61,"61"),(62,"62"),(63,"63"),(64,"64"),(65,"65"), +(66,"66"),(67,"67"),(68,"68"),(69,"69"),(70,"70"), +(71,"71"),(72,"72"),(73,"73"),(74,"74"),(75,"75"), +(76,"76"),(77,"77"),(78,"78"),(79,"79"),(80,"80"), +(81,"81"),(82,"82"),(83,"83"),(84,"84"),(85,"85"), +(86,"86"),(87,"87"),(88,"88"),(89,"89"),(90,"90"), +(91,"91"),(92,"92"),(93,"93"),(94,"94"),(95,"95"), +(96,"96"),(97,"97"),(98,"98"),(99,"99"); +SELECT * FROM t1 ORDER BY f1 ASC, f0 LIMIT 100; +f0 f1 f2 +1 0 0 +2 1 1 +3 2 2 +4 3 3 +5 4 4 +6 5 5 +7 6 6 +8 7 7 +9 8 8 +10 9 9 +11 10 10 +12 11 11 +13 12 12 +14 13 13 +15 14 14 +16 15 15 +17 16 16 +18 17 17 +19 18 18 +20 19 19 +21 20 20 +22 21 21 +23 22 22 +24 23 23 +25 24 24 +26 25 25 +27 26 26 +28 27 27 +29 28 28 +30 29 29 +31 30 30 +32 31 31 +33 32 32 +34 33 33 +35 34 34 +36 35 35 +37 36 36 +38 37 37 +39 38 38 +40 39 39 +41 40 40 +42 41 41 +43 42 42 +44 43 43 +45 44 44 +46 45 45 +47 46 46 +48 47 47 +49 48 48 +50 49 49 +51 50 50 +52 51 51 +53 52 52 +54 53 53 +55 54 54 +56 55 55 +57 56 56 +58 57 57 +59 58 58 +60 59 59 +61 60 60 +62 61 61 +63 62 62 +64 63 63 +65 64 64 +66 65 65 +67 66 66 +68 67 67 +69 68 68 +70 69 69 +71 70 70 +72 71 71 +73 72 72 +74 73 73 +75 74 74 +76 75 75 +77 76 76 +78 77 77 +79 78 78 +80 79 79 +81 80 80 +82 81 81 +83 82 82 +84 83 83 +85 84 84 +86 85 85 +87 86 86 +88 87 87 +89 88 88 +90 89 89 +91 90 90 +92 91 91 +93 92 92 +94 93 93 +95 94 94 +96 95 95 +97 96 96 +98 97 97 +99 98 98 +100 99 99 +SELECT * FROM t1 ORDER BY f1 ASC, f0 LIMIT 30; +f0 f1 f2 +1 0 0 +2 1 1 +3 2 2 +4 3 3 +5 4 4 +6 5 5 +7 6 6 +8 7 7 +9 8 8 +10 9 9 +11 10 10 +12 11 11 +13 12 12 +14 13 13 +15 14 14 +16 15 15 +17 16 16 +18 17 17 +19 18 18 +20 19 19 +21 20 20 +22 21 21 +23 22 22 +24 23 23 +25 24 24 +26 25 25 +27 26 26 +28 27 27 +29 28 28 +30 29 29 +SELECT * FROM t1 ORDER BY f1 ASC, f0 LIMIT 0; +f0 f1 f2 +SELECT * FROM t1 ORDER BY f2 DESC, f0 LIMIT 30; +f0 f1 f2 +100 99 99 +99 98 98 +98 97 97 +97 96 96 +96 95 95 +95 94 94 +94 93 93 +93 92 92 +92 91 91 +91 90 90 +10 9 9 +90 89 89 +89 88 88 +88 87 87 +87 86 86 +86 85 85 +85 84 84 +84 83 83 +83 82 82 +82 81 81 +81 80 80 +9 8 8 +80 79 79 +79 78 78 +78 77 77 +77 76 76 +76 75 75 +75 74 74 +74 73 73 +73 72 72 +SELECT * FROM t1 ORDER BY f2 DESC, f0 LIMIT 0; +f0 f1 f2 +SELECT * FROM t1 WHERE f1>10 ORDER BY f2, f0 LIMIT 20; +f0 f1 f2 +12 11 11 +13 12 12 +14 13 13 +15 14 14 +16 15 15 +17 16 16 +18 17 17 +19 18 18 +20 19 19 +21 20 20 +22 21 21 +23 22 22 +24 23 23 +25 24 24 +26 25 25 +27 26 26 +28 27 27 +29 28 28 +30 29 29 +31 30 30 +SELECT * FROM t1 WHERE f1>10 ORDER BY f2, f0 LIMIT 0; +f0 f1 f2 +SELECT * FROM t1 WHERE f1>10 ORDER BY f2, f0 LIMIT 10 OFFSET 10; +f0 f1 f2 +22 21 21 +23 22 22 +24 23 23 +25 24 24 +26 25 25 +27 26 26 +28 27 27 +29 28 28 +30 29 29 +31 30 30 +SELECT * FROM t1 WHERE f1>10 ORDER BY f2, f0 LIMIT 0 OFFSET 10; +f0 f1 f2 +set sort_buffer_size= 32768; +CREATE TEMPORARY TABLE tmp (f1 int, f2 varchar(20)); +INSERT INTO tmp SELECT f1, f2 FROM t1; +INSERT INTO t1(f1, f2) SELECT * FROM tmp; +INSERT INTO tmp SELECT f1, f2 FROM t1; +INSERT INTO t1(f1, f2) SELECT * FROM tmp; +SELECT * FROM t1 ORDER BY f1 ASC, f0 LIMIT 30; +f0 f1 f2 +1 0 0 +101 0 0 +201 0 0 +301 0 0 +401 0 0 +2 1 1 +102 1 1 +202 1 1 +302 1 1 +402 1 1 +3 2 2 +103 2 2 +203 2 2 +303 2 2 +403 2 2 +4 3 3 +104 3 3 +204 3 3 +304 3 3 +404 3 3 +5 4 4 +105 4 4 +205 4 4 +305 4 4 +405 4 4 +6 5 5 +106 5 5 +206 5 5 +306 5 5 +406 5 5 +SELECT * FROM t1 ORDER BY f1 ASC, f0 LIMIT 0; +f0 f1 f2 +SELECT * FROM t1 ORDER BY f2 DESC, f0 LIMIT 30; +f0 f1 f2 +100 99 99 +200 99 99 +300 99 99 +400 99 99 +500 99 99 +99 98 98 +199 98 98 +299 98 98 +399 98 98 +499 98 98 +98 97 97 +198 97 97 +298 97 97 +398 97 97 +498 97 97 +97 96 96 +197 96 96 +297 96 96 +397 96 96 +497 96 96 +96 95 95 +196 95 95 +296 95 95 +396 95 95 +496 95 95 +95 94 94 +195 94 94 +295 94 94 +395 94 94 +495 94 94 +SELECT * FROM t1 ORDER BY f2 DESC, f0 LIMIT 0; +f0 f1 f2 +SELECT * FROM t1 WHERE f1>10 ORDER BY f2, f0 LIMIT 20; +f0 f1 f2 +12 11 11 +112 11 11 +212 11 11 +312 11 11 +412 11 11 +13 12 12 +113 12 12 +213 12 12 +313 12 12 +413 12 12 +14 13 13 +114 13 13 +214 13 13 +314 13 13 +414 13 13 +15 14 14 +115 14 14 +215 14 14 +315 14 14 +415 14 14 +SELECT * FROM t1 WHERE f1>10 ORDER BY f2, f0 LIMIT 0; +f0 f1 f2 +SELECT * FROM t1 WHERE f1>10 ORDER BY f2, f0 LIMIT 10 OFFSET 10; +f0 f1 f2 +14 13 13 +114 13 13 +214 13 13 +314 13 13 +414 13 13 +15 14 14 +115 14 14 +215 14 14 +315 14 14 +415 14 14 +SELECT * FROM t1 WHERE f1>10 ORDER BY f2, f0 LIMIT 0 OFFSET 10; +f0 f1 f2 +set sort_buffer_size= 32768; +SELECT SQL_CALC_FOUND_ROWS * FROM t1 +ORDER BY f1, f0 LIMIT 30; +f0 f1 f2 +1 0 0 +101 0 0 +201 0 0 +301 0 0 +401 0 0 +2 1 1 +102 1 1 +202 1 1 +302 1 1 +402 1 1 +3 2 2 +103 2 2 +203 2 2 +303 2 2 +403 2 2 +4 3 3 +104 3 3 +204 3 3 +304 3 3 +404 3 3 +5 4 4 +105 4 4 +205 4 4 +305 4 4 +405 4 4 +6 5 5 +106 5 5 +206 5 5 +306 5 5 +406 5 5 +SELECT FOUND_ROWS(); +FOUND_ROWS() +500 +SELECT SQL_CALC_FOUND_ROWS * FROM t1 +ORDER BY f1, f0 LIMIT 0; +f0 f1 f2 +SELECT FOUND_ROWS(); +FOUND_ROWS() +500 +SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE f1>10 +ORDER BY f2, f0 LIMIT 20; +f0 f1 f2 +12 11 11 +112 11 11 +212 11 11 +312 11 11 +412 11 11 +13 12 12 +113 12 12 +213 12 12 +313 12 12 +413 12 12 +14 13 13 +114 13 13 +214 13 13 +314 13 13 +414 13 13 +15 14 14 +115 14 14 +215 14 14 +315 14 14 +415 14 14 +SELECT FOUND_ROWS(); +FOUND_ROWS() +445 +SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE f1>10 +ORDER BY f2, f0 LIMIT 0; +f0 f1 f2 +SELECT FOUND_ROWS(); +FOUND_ROWS() +445 +SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE f1>10 +ORDER BY f2, f0 LIMIT 10 OFFSET 10; +f0 f1 f2 +14 13 13 +114 13 13 +214 13 13 +314 13 13 +414 13 13 +15 14 14 +115 14 14 +215 14 14 +315 14 14 +415 14 14 +SELECT FOUND_ROWS(); +FOUND_ROWS() +445 +SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE f1>10 +ORDER BY f2, f0 LIMIT 0 OFFSET 10; +f0 f1 f2 +SELECT FOUND_ROWS(); +FOUND_ROWS() +445 +set sort_buffer_size= 327680; +SELECT * FROM t1 JOIN tmp on t1.f2=tmp.f2 +ORDER BY tmp.f1, f0 LIMIT 30; +f0 f1 f2 f1 f2 +1 0 0 0 0 +1 0 0 0 0 +1 0 0 0 0 +101 0 0 0 0 +101 0 0 0 0 +101 0 0 0 0 +201 0 0 0 0 +201 0 0 0 0 +201 0 0 0 0 +301 0 0 0 0 +301 0 0 0 0 +301 0 0 0 0 +401 0 0 0 0 +401 0 0 0 0 +401 0 0 0 0 +2 1 1 1 1 +2 1 1 1 1 +2 1 1 1 1 +102 1 1 1 1 +102 1 1 1 1 +102 1 1 1 1 +202 1 1 1 1 +202 1 1 1 1 +202 1 1 1 1 +302 1 1 1 1 +302 1 1 1 1 +302 1 1 1 1 +402 1 1 1 1 +402 1 1 1 1 +402 1 1 1 1 +SELECT * FROM t1 JOIN tmp on t1.f2=tmp.f2 +ORDER BY tmp.f1, f0 LIMIT 30 OFFSET 30; +f0 f1 f2 f1 f2 +3 2 2 2 2 +3 2 2 2 2 +3 2 2 2 2 +103 2 2 2 2 +103 2 2 2 2 +103 2 2 2 2 +203 2 2 2 2 +203 2 2 2 2 +203 2 2 2 2 +303 2 2 2 2 +303 2 2 2 2 +303 2 2 2 2 +403 2 2 2 2 +403 2 2 2 2 +403 2 2 2 2 +4 3 3 3 3 +4 3 3 3 3 +4 3 3 3 3 +104 3 3 3 3 +104 3 3 3 3 +104 3 3 3 3 +204 3 3 3 3 +204 3 3 3 3 +204 3 3 3 3 +304 3 3 3 3 +304 3 3 3 3 +304 3 3 3 3 +404 3 3 3 3 +404 3 3 3 3 +404 3 3 3 3 +SELECT SQL_CALC_FOUND_ROWS * FROM t1 JOIN tmp on t1.f2=tmp.f2 +ORDER BY tmp.f1, f0 LIMIT 30 OFFSET 30; +f0 f1 f2 f1 f2 +3 2 2 2 2 +3 2 2 2 2 +3 2 2 2 2 +103 2 2 2 2 +103 2 2 2 2 +103 2 2 2 2 +203 2 2 2 2 +203 2 2 2 2 +203 2 2 2 2 +303 2 2 2 2 +303 2 2 2 2 +303 2 2 2 2 +403 2 2 2 2 +403 2 2 2 2 +403 2 2 2 2 +4 3 3 3 3 +4 3 3 3 3 +4 3 3 3 3 +104 3 3 3 3 +104 3 3 3 3 +104 3 3 3 3 +204 3 3 3 3 +204 3 3 3 3 +204 3 3 3 3 +304 3 3 3 3 +304 3 3 3 3 +304 3 3 3 3 +404 3 3 3 3 +404 3 3 3 3 +404 3 3 3 3 +SELECT FOUND_ROWS(); +FOUND_ROWS() +1500 +SELECT SQL_CALC_FOUND_ROWS * FROM t1 JOIN tmp on t1.f2=tmp.f2 +WHERE t1.f2>20 +ORDER BY tmp.f1, f0 LIMIT 30 OFFSET 30; +f0 f1 f2 f1 f2 +24 23 23 23 23 +24 23 23 23 23 +24 23 23 23 23 +124 23 23 23 23 +124 23 23 23 23 +124 23 23 23 23 +224 23 23 23 23 +224 23 23 23 23 +224 23 23 23 23 +324 23 23 23 23 +324 23 23 23 23 +324 23 23 23 23 +424 23 23 23 23 +424 23 23 23 23 +424 23 23 23 23 +25 24 24 24 24 +25 24 24 24 24 +25 24 24 24 24 +125 24 24 24 24 +125 24 24 24 24 +125 24 24 24 24 +225 24 24 24 24 +225 24 24 24 24 +225 24 24 24 24 +325 24 24 24 24 +325 24 24 24 24 +325 24 24 24 24 +425 24 24 24 24 +425 24 24 24 24 +425 24 24 24 24 +SELECT FOUND_ROWS(); +FOUND_ROWS() +1185 +CREATE VIEW v1 as SELECT * FROM t1 ORDER BY f1, f0 LIMIT 30; +SELECT * FROM v1; +f0 f1 f2 +1 0 0 +101 0 0 +201 0 0 +301 0 0 +401 0 0 +2 1 1 +102 1 1 +202 1 1 +302 1 1 +402 1 1 +3 2 2 +103 2 2 +203 2 2 +303 2 2 +403 2 2 +4 3 3 +104 3 3 +204 3 3 +304 3 3 +404 3 3 +5 4 4 +105 4 4 +205 4 4 +305 4 4 +405 4 4 +6 5 5 +106 5 5 +206 5 5 +306 5 5 +406 5 5 +drop view v1; +CREATE VIEW v1 as SELECT * FROM t1 ORDER BY f1, f0 LIMIT 100; +SELECT * FROM v1 ORDER BY f2, f0 LIMIT 30; +f0 f1 f2 +1 0 0 +101 0 0 +201 0 0 +301 0 0 +401 0 0 +2 1 1 +102 1 1 +202 1 1 +302 1 1 +402 1 1 +11 10 10 +111 10 10 +211 10 10 +311 10 10 +411 10 10 +12 11 11 +112 11 11 +212 11 11 +312 11 11 +412 11 11 +13 12 12 +113 12 12 +213 12 12 +313 12 12 +413 12 12 +14 13 13 +114 13 13 +214 13 13 +314 13 13 +414 13 13 +CREATE VIEW v2 as SELECT * FROM t1 ORDER BY f2, f0 LIMIT 100; +SELECT * FROM v1 JOIN v2 on v1.f1=v2.f1 ORDER BY v1.f2,v1.f0,v2.f0 +LIMIT 30; +f0 f1 f2 f0 f1 f2 +1 0 0 1 0 0 +1 0 0 101 0 0 +1 0 0 201 0 0 +1 0 0 301 0 0 +1 0 0 401 0 0 +101 0 0 1 0 0 +101 0 0 101 0 0 +101 0 0 201 0 0 +101 0 0 301 0 0 +101 0 0 401 0 0 +201 0 0 1 0 0 +201 0 0 101 0 0 +201 0 0 201 0 0 +201 0 0 301 0 0 +201 0 0 401 0 0 +301 0 0 1 0 0 +301 0 0 101 0 0 +301 0 0 201 0 0 +301 0 0 301 0 0 +301 0 0 401 0 0 +401 0 0 1 0 0 +401 0 0 101 0 0 +401 0 0 201 0 0 +401 0 0 301 0 0 +401 0 0 401 0 0 +2 1 1 2 1 1 +2 1 1 102 1 1 +2 1 1 202 1 1 +2 1 1 302 1 1 +2 1 1 402 1 1 +SELECT floor(f1/10) f3, count(f2) FROM t1 +GROUP BY 1 ORDER BY 2,1 LIMIT 5; +f3 count(f2) +0 50 +1 50 +2 50 +3 50 +4 50 +SELECT floor(f1/10) f3, count(f2) FROM t1 +GROUP BY 1 ORDER BY 2,1 LIMIT 0; +f3 count(f2) +CREATE PROCEDURE wl1393_sp_test() +BEGIN +SELECT * FROM t1 WHERE f1>10 ORDER BY f2, f0 LIMIT 30; +SELECT * FROM t1 WHERE f1>10 ORDER BY f2, f0 LIMIT 15 OFFSET 15; +SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE f1>10 +ORDER BY f2, f0 LIMIT 15 OFFSET 15; +SELECT FOUND_ROWS(); +SELECT * FROM v1 ORDER BY f2, f0 LIMIT 30; +END| +CALL wl1393_sp_test()| +f0 f1 f2 +12 11 11 +112 11 11 +212 11 11 +312 11 11 +412 11 11 +13 12 12 +113 12 12 +213 12 12 +313 12 12 +413 12 12 +14 13 13 +114 13 13 +214 13 13 +314 13 13 +414 13 13 +15 14 14 +115 14 14 +215 14 14 +315 14 14 +415 14 14 +16 15 15 +116 15 15 +216 15 15 +316 15 15 +416 15 15 +17 16 16 +117 16 16 +217 16 16 +317 16 16 +417 16 16 +f0 f1 f2 +15 14 14 +115 14 14 +215 14 14 +315 14 14 +415 14 14 +16 15 15 +116 15 15 +216 15 15 +316 15 15 +416 15 15 +17 16 16 +117 16 16 +217 16 16 +317 16 16 +417 16 16 +f0 f1 f2 +15 14 14 +115 14 14 +215 14 14 +315 14 14 +415 14 14 +16 15 15 +116 15 15 +216 15 15 +316 15 15 +416 15 15 +17 16 16 +117 16 16 +217 16 16 +317 16 16 +417 16 16 +FOUND_ROWS() +445 +f0 f1 f2 +1 0 0 +101 0 0 +201 0 0 +301 0 0 +401 0 0 +2 1 1 +102 1 1 +202 1 1 +302 1 1 +402 1 1 +11 10 10 +111 10 10 +211 10 10 +311 10 10 +411 10 10 +12 11 11 +112 11 11 +212 11 11 +312 11 11 +412 11 11 +13 12 12 +113 12 12 +213 12 12 +313 12 12 +413 12 12 +14 13 13 +114 13 13 +214 13 13 +314 13 13 +414 13 13 +DROP PROCEDURE wl1393_sp_test| +SELECT d1.f1, d1.f2 FROM t1 +LEFT JOIN (SELECT * FROM t1 ORDER BY f1 LIMIT 30) d1 on t1.f1=d1.f1 +ORDER BY d1.f2 DESC LIMIT 30; +f1 f2 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +5 5 +4 4 +4 4 +4 4 +4 4 +4 4 +SELECT * FROM t1 WHERE f1 = (SELECT f1 FROM t1 ORDER BY 1 LIMIT 1); +f0 f1 f2 +1 0 0 +101 0 0 +201 0 0 +301 0 0 +401 0 0 +SELECT * FROM t1 WHERE f1 = (SELECT f1 FROM t1 ORDER BY 1 LIMIT 2); +ERROR 21000: Subquery returns more than 1 row +DROP TABLE t1, tmp; +DROP VIEW v1, v2; +# end of WL#1393 - Optimizing filesort with small limit +# +# Bug #58761 +# Crash in Field::is_null in field.h on subquery in WHERE clause +# +CREATE TABLE t1 ( +pk INT NOT NULL AUTO_INCREMENT, +col_int_key INT DEFAULT NULL, +col_varchar_key VARCHAR(1) DEFAULT NULL, +PRIMARY KEY (pk), +KEY col_varchar_key (col_varchar_key,col_int_key) +); +INSERT INTO t1 VALUES (27,7,'x'); +INSERT INTO t1 VALUES (28,6,'m'); +INSERT INTO t1 VALUES (29,4,'c'); +CREATE TABLE where_subselect +SELECT DISTINCT `pk` AS field1 , `pk` AS field2 +FROM t1 AS alias1 +WHERE alias1 . `col_int_key` > 229 +OR alias1 . `col_varchar_key` IS NOT NULL +GROUP BY field1, field2 +; +SELECT * +FROM where_subselect +WHERE (field1, field2) IN ( +SELECT DISTINCT `pk` AS field1 , `pk` AS field2 +FROM t1 AS alias1 +WHERE alias1 . `col_int_key` > 229 +OR alias1 . `col_varchar_key` IS NOT NULL +GROUP BY field1, field2 +); +field1 field2 +27 27 +28 28 +29 29 +DROP TABLE t1; +DROP TABLE where_subselect; +# End of Bug #58761 CREATE TABLE t1 ( id1 INT NULL, id2 INT NOT NULL, diff --git a/mysql-test/r/order_by_sortkey.result b/mysql-test/r/order_by_sortkey.result new file mode 100644 index 00000000000..717780f0af2 --- /dev/null +++ b/mysql-test/r/order_by_sortkey.result @@ -0,0 +1,159 @@ +CREATE TABLE t1( +f0 int auto_increment PRIMARY KEY, +f1 int, +f2 varchar(200) +); +INSERT INTO t1(f1, f2) VALUES +(0,"0"),(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5"), +(6,"6"),(7,"7"),(8,"8"),(9,"9"),(10,"10"), +(11,"11"),(12,"12"),(13,"13"),(14,"14"),(15,"15"), +(16,"16"),(17,"17"),(18,"18"),(19,"19"),(20,"20"), +(21,"21"),(22,"22"),(23,"23"),(24,"24"),(25,"25"), +(26,"26"),(27,"27"),(28,"28"),(29,"29"),(30,"30"), +(31,"31"),(32,"32"),(33,"33"),(34,"34"),(35,"35"), +(36,"36"),(37,"37"),(38,"38"),(39,"39"),(40,"40"), +(41,"41"),(42,"42"),(43,"43"),(44,"44"),(45,"45"), +(46,"46"),(47,"47"),(48,"48"),(49,"49"),(50,"50"), +(51,"51"),(52,"52"),(53,"53"),(54,"54"),(55,"55"), +(56,"56"),(57,"57"),(58,"58"),(59,"59"),(60,"60"), +(61,"61"),(62,"62"),(63,"63"),(64,"64"),(65,"65"), +(66,"66"),(67,"67"),(68,"68"),(69,"69"),(70,"70"), +(71,"71"),(72,"72"),(73,"73"),(74,"74"),(75,"75"), +(76,"76"),(77,"77"),(78,"78"),(79,"79"),(80,"80"), +(81,"81"),(82,"82"),(83,"83"),(84,"84"),(85,"85"), +(86,"86"),(87,"87"),(88,"88"),(89,"89"),(90,"90"), +(91,"91"),(92,"92"),(93,"93"),(94,"94"),(95,"95"), +(96,"96"),(97,"97"),(98,"98"),(99,"99"); +CREATE TEMPORARY TABLE tmp (f1 int, f2 varchar(20)); +INSERT INTO tmp SELECT f1,f2 FROM t1; +INSERT INTO t1(f1,f2) SELECT * FROM tmp; +INSERT INTO tmp SELECT f1,f2 FROM t1; +INSERT INTO t1(f1,f2) SELECT * FROM tmp; +INSERT INTO t1(f1,f2) SELECT * FROM tmp; +INSERT INTO tmp SELECT f1,f2 FROM t1; +INSERT INTO t1(f1,f2) SELECT * FROM tmp; +INSERT INTO tmp SELECT f1,f2 FROM t1; +INSERT INTO t1(f1,f2) SELECT * FROM tmp; +INSERT INTO tmp SELECT f1,f2 FROM t1; +INSERT INTO t1(f1,f2) SELECT * FROM tmp; +INSERT INTO tmp SELECT f1,f2 FROM t1; +INSERT INTO t1(f1,f2) SELECT * FROM tmp; +INSERT INTO tmp SELECT f1,f2 FROM t1; +INSERT INTO t1(f1,f2) SELECT * FROM tmp; +set sort_buffer_size= 32768; +FLUSH STATUS; +SHOW SESSION STATUS LIKE 'Sort%'; +Variable_name Value +Sort_merge_passes 0 +Sort_range 0 +Sort_rows 0 +Sort_scan 0 +SELECT * FROM t1 ORDER BY f2 LIMIT 100; +f0 f1 f2 +1 0 0 +101 0 0 +201 0 0 +301 0 0 +401 0 0 +501 0 0 +601 0 0 +701 0 0 +801 0 0 +901 0 0 +1001 0 0 +1101 0 0 +1201 0 0 +1301 0 0 +1401 0 0 +1501 0 0 +1601 0 0 +1701 0 0 +1801 0 0 +1901 0 0 +2001 0 0 +2101 0 0 +2201 0 0 +2301 0 0 +2401 0 0 +2501 0 0 +2601 0 0 +2701 0 0 +2801 0 0 +2901 0 0 +3001 0 0 +3101 0 0 +3201 0 0 +3301 0 0 +3401 0 0 +3501 0 0 +3601 0 0 +3701 0 0 +3801 0 0 +3901 0 0 +4001 0 0 +4101 0 0 +4201 0 0 +4301 0 0 +4401 0 0 +4501 0 0 +4601 0 0 +4701 0 0 +4801 0 0 +4901 0 0 +5001 0 0 +5101 0 0 +5201 0 0 +5301 0 0 +5401 0 0 +5501 0 0 +5601 0 0 +5701 0 0 +5801 0 0 +5901 0 0 +6001 0 0 +6101 0 0 +6201 0 0 +6301 0 0 +6401 0 0 +6501 0 0 +6601 0 0 +6701 0 0 +6801 0 0 +6901 0 0 +7001 0 0 +7101 0 0 +7201 0 0 +7301 0 0 +7401 0 0 +7501 0 0 +7601 0 0 +7701 0 0 +7801 0 0 +7901 0 0 +8001 0 0 +8101 0 0 +8201 0 0 +8301 0 0 +8401 0 0 +8501 0 0 +8601 0 0 +8701 0 0 +8801 0 0 +8901 0 0 +9001 0 0 +9101 0 0 +9201 0 0 +9301 0 0 +9401 0 0 +9501 0 0 +9601 0 0 +9701 0 0 +9801 0 0 +9901 0 0 +SHOW SESSION STATUS LIKE 'Sort%'; +Variable_name Value +Sort_merge_passes 0 +Sort_range 0 +Sort_rows 100 +Sort_scan 1 +DROP TABLE t1, tmp; diff --git a/mysql-test/r/parser.result b/mysql-test/r/parser.result index 10700d0ba73..54378f16d49 100644 --- a/mysql-test/r/parser.result +++ b/mysql-test/r/parser.result @@ -441,7 +441,7 @@ select master_pos_wait(); ERROR 42000: Incorrect parameter count in the call to native function 'master_pos_wait' select master_pos_wait(1); ERROR 42000: Incorrect parameter count in the call to native function 'master_pos_wait' -select master_pos_wait(1, 2, 3, 4); +select master_pos_wait(1, 2, 3, 4, 5); ERROR 42000: Incorrect parameter count in the call to native function 'master_pos_wait' select rand(1, 2, 3); ERROR 42000: Incorrect parameter count in the call to native function 'rand' diff --git a/mysql-test/r/partition_datatype.result b/mysql-test/r/partition_datatype.result index e0658f71612..8d72d8781ee 100644 --- a/mysql-test/r/partition_datatype.result +++ b/mysql-test/r/partition_datatype.result @@ -315,11 +315,11 @@ drop table t1; create table t1 (a varchar(3070)) partition by key (a); ERROR HY000: The total length of the partitioning fields is too large create table t1 (a varchar(65533)) partition by key (a); -ERROR 42000: Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs +ERROR 42000: Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs create table t1 (a varchar(65534) not null) partition by key (a); -ERROR 42000: Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs +ERROR 42000: Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs create table t1 (a varchar(65535)) partition by key (a); -ERROR 42000: Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs +ERROR 42000: Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs create table t1 (a bit(27), primary key (a)) engine=myisam partition by hash (a) (partition p0, partition p1, partition p2); diff --git a/mysql-test/r/partition_innodb_plugin.result b/mysql-test/r/partition_innodb_plugin.result index 21f18f715d0..fa2ef5c6c18 100644 --- a/mysql-test/r/partition_innodb_plugin.result +++ b/mysql-test/r/partition_innodb_plugin.result @@ -67,7 +67,7 @@ LOCK TABLE t1 WRITE; # ALTER fails because COMPRESSED/KEY_BLOCK_SIZE # are incompatible with innodb_file_per_table = OFF; ALTER TABLE t1 ADD PARTITION PARTITIONS 1; -ERROR HY000: Got error 1478 from storage engine +ERROR HY000: Got error 140 "Wrong create options" from storage engine t1#P#p0.ibd t1.frm t1.par @@ -76,18 +76,18 @@ t1.par SET innodb_strict_mode = OFF; ALTER TABLE t1 ADD PARTITION PARTITIONS 2; Warnings: -Warning 1478 InnoDB: KEY_BLOCK_SIZE requires innodb_file_per_table. -Warning 1478 InnoDB: ignoring KEY_BLOCK_SIZE=4. -Warning 1478 InnoDB: KEY_BLOCK_SIZE requires innodb_file_per_table. -Warning 1478 InnoDB: ignoring KEY_BLOCK_SIZE=4. -Warning 1478 InnoDB: KEY_BLOCK_SIZE requires innodb_file_per_table. -Warning 1478 InnoDB: ignoring KEY_BLOCK_SIZE=4. +Warning 140 InnoDB: KEY_BLOCK_SIZE requires innodb_file_per_table. +Warning 140 InnoDB: ignoring KEY_BLOCK_SIZE=4. +Warning 140 InnoDB: KEY_BLOCK_SIZE requires innodb_file_per_table. +Warning 140 InnoDB: ignoring KEY_BLOCK_SIZE=4. +Warning 140 InnoDB: KEY_BLOCK_SIZE requires innodb_file_per_table. +Warning 140 InnoDB: ignoring KEY_BLOCK_SIZE=4. t1.frm t1.par ALTER TABLE t1 REBUILD PARTITION p0; Warnings: -Warning 1478 InnoDB: KEY_BLOCK_SIZE requires innodb_file_per_table. -Warning 1478 InnoDB: ignoring KEY_BLOCK_SIZE=4. +Warning 140 InnoDB: KEY_BLOCK_SIZE requires innodb_file_per_table. +Warning 140 InnoDB: ignoring KEY_BLOCK_SIZE=4. UNLOCK TABLES; SHOW CREATE TABLE t1; Table Create Table diff --git a/mysql-test/r/partition_open_files_limit.result b/mysql-test/r/partition_open_files_limit.result index 1441ba4e78e..fed32a69c44 100644 --- a/mysql-test/r/partition_open_files_limit.result +++ b/mysql-test/r/partition_open_files_limit.result @@ -5,7 +5,7 @@ ENGINE=MyISAM PARTITION BY KEY () PARTITIONS 1; INSERT INTO t1 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11); # if the bug exists, then crash will happen here ALTER TABLE t1 ADD PARTITION PARTITIONS 511; -ERROR HY000: Out of resources when opening file '<partition file>' (Errcode: 24) +ERROR HY000: Out of resources when opening file '<partition file>' (Errcode: 24 "Too many open files") SELECT * FROM t1; a 1 diff --git a/mysql-test/r/ps_1general.result b/mysql-test/r/ps_1general.result index 0a3d16cf48e..3ca0cf8554f 100644 --- a/mysql-test/r/ps_1general.result +++ b/mysql-test/r/ps_1general.result @@ -533,7 +533,7 @@ drop table t2; prepare stmt1 from ' rename table t5 to t6, t7 to t8 ' ; create table t5 (a int) ; execute stmt1 ; -ERROR HY000: Can't find file: './test/t7' (errno: 2) +ERROR HY000: Can't find file: './test/t7' (errno: 2 "No such file or directory") create table t7 (a int) ; execute stmt1 ; execute stmt1 ; diff --git a/mysql-test/r/range_vs_index_merge.result b/mysql-test/r/range_vs_index_merge.result index cc8a345a2ff..424b1009a97 100644 --- a/mysql-test/r/range_vs_index_merge.result +++ b/mysql-test/r/range_vs_index_merge.result @@ -1576,7 +1576,7 @@ update t1 set c1=lpad(id+1000, 12, ' '), c2=lpad(id+10000, 15, ' '); alter table t1 add unique index (c1), add unique index (c2), add index (c3); analyze table t1; Table Op Msg_type Msg_text -test.t1 analyze status OK +test.t1 analyze status Table is already up to date explain select * from t1 where (c1=' 100000' or c2=' 2000000'); id select_type table type possible_keys key key_len ref rows Extra diff --git a/mysql-test/r/row-checksum-old.result b/mysql-test/r/row-checksum-old.result index ef523463860..87f0bb8af2d 100644 --- a/mysql-test/r/row-checksum-old.result +++ b/mysql-test/r/row-checksum-old.result @@ -73,7 +73,7 @@ test.t1 4108368782 drop table if exists t1; create table t1 (a int null, v varchar(100)) engine=innodb checksum=0 row_format=fixed; Warnings: -Warning 1478 InnoDB: assuming ROW_FORMAT=COMPACT. +Warning 140 InnoDB: assuming ROW_FORMAT=COMPACT. insert into t1 values(null, null), (1, "hello"); checksum table t1; Table Checksum diff --git a/mysql-test/r/row-checksum.result b/mysql-test/r/row-checksum.result index fb8a1260a1d..9e58d6fa96e 100644 --- a/mysql-test/r/row-checksum.result +++ b/mysql-test/r/row-checksum.result @@ -73,7 +73,7 @@ test.t1 3885665021 drop table if exists t1; create table t1 (a int null, v varchar(100)) engine=innodb checksum=0 row_format=fixed; Warnings: -Warning 1478 InnoDB: assuming ROW_FORMAT=COMPACT. +Warning 140 InnoDB: assuming ROW_FORMAT=COMPACT. insert into t1 values(null, null), (1, "hello"); checksum table t1; Table Checksum diff --git a/mysql-test/r/server_id.require b/mysql-test/r/server_id.require deleted file mode 100644 index adffcc483b1..00000000000 --- a/mysql-test/r/server_id.require +++ /dev/null @@ -1,2 +0,0 @@ -Variable_name Value -server_id 1 diff --git a/mysql-test/r/server_id1.require b/mysql-test/r/server_id1.require deleted file mode 100644 index 666c94ef633..00000000000 --- a/mysql-test/r/server_id1.require +++ /dev/null @@ -1,2 +0,0 @@ -Variable_name Value -server_id 102 diff --git a/mysql-test/r/show_explain.result b/mysql-test/r/show_explain.result new file mode 100644 index 00000000000..da132a102e2 --- /dev/null +++ b/mysql-test/r/show_explain.result @@ -0,0 +1,1100 @@ +drop table if exists t0, t1, t2, t3, t4; +drop view if exists v1; +SET @old_debug= @@session.debug; +set debug_sync='RESET'; +create table t0 (a int); +insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); +create table t1 (a int); +insert into t1 select A.a + 10*B.a + 100*C.a from t0 A, t0 B, t0 C; +alter table t1 add b int, add c int, add filler char(32); +update t1 set b=a, c=a, filler='fooo'; +alter table t1 add key(a), add key(b); +show explain for 2000000000; +ERROR HY000: Unknown thread id: 2000000000 +show explain for (select max(a) from t0); +ERROR HY000: You may only use constant expressions in this statement +SET @old_debug= @@session.debug; +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +show explain for $thr1; +ERROR HY000: Target is not running an EXPLAINable command +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select count(*) from t1 where a < 100000; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index a a 5 NULL 1000 Using where; Using index +Warnings: +Note 1003 select count(*) from t1 where a < 100000 +count(*) +1000 +select max(c) from t1 where a < 10; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 range a a 5 NULL 10 Using index condition +Warnings: +Note 1003 select max(c) from t1 where a < 10 +max(c) +9 +# We can catch EXPLAIN, too. +set @show_expl_tmp= @@optimizer_switch; +set optimizer_switch='index_condition_pushdown=on,mrr=on,mrr_sort_keys=on'; +explain select max(c) from t1 where a < 10; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 range a a 5 NULL 10 Using index condition; Rowid-ordered scan +Warnings: +Note 1003 explain select max(c) from t1 where a < 10 +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 range a a 5 NULL 10 Using index condition; Rowid-ordered scan +set optimizer_switch= @show_expl_tmp; +set debug_dbug=@old_debug; +# UNION, first branch +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +explain select a from t0 A union select a+1 from t0 B; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY A ALL NULL NULL NULL NULL 10 +2 UNION B ALL NULL NULL NULL NULL 10 +NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL +Warnings: +Note 1003 explain select a from t0 A union select a+1 from t0 B +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY A ALL NULL NULL NULL NULL 10 +2 UNION B ALL NULL NULL NULL NULL 10 +NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL +set debug_dbug=@old_debug; +# UNION, second branch +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +explain select a from t0 A union select a+1 from t0 B; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY A ALL NULL NULL NULL NULL 10 +2 UNION B ALL NULL NULL NULL NULL 10 +NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL +Warnings: +Note 1003 explain select a from t0 A union select a+1 from t0 B +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY A ALL NULL NULL NULL NULL 10 +2 UNION B ALL NULL NULL NULL NULL 10 +NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL +set debug_dbug=@old_debug; +# Uncorrelated subquery, select +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select a, (select max(a) from t0 B) from t0 A where a<1; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY A ALL NULL NULL NULL NULL 10 Using where +2 SUBQUERY B ALL NULL NULL NULL NULL 10 +Warnings: +Note 1003 select a, (select max(a) from t0 B) from t0 A where a<1 +a (select max(a) from t0 B) +0 9 +set debug_dbug=@old_debug; +# Uncorrelated subquery, explain +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +explain select a, (select max(a) from t0 B) from t0 A where a<1; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY A ALL NULL NULL NULL NULL 10 Using where +2 SUBQUERY B ALL NULL NULL NULL NULL 10 +Warnings: +Note 1003 explain select a, (select max(a) from t0 B) from t0 A where a<1 +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY A ALL NULL NULL NULL NULL 10 Using where +2 SUBQUERY B ALL NULL NULL NULL NULL 10 +set debug_dbug=@old_debug; +# correlated subquery, select +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where +2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where +Warnings: +Note 1003 select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1 +a (select max(a) from t0 b where b.a+a.a<10) +0 9 +set debug_dbug=@old_debug; +# correlated subquery, explain +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where +2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where +Warnings: +Note 1003 select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1 +a (select max(a) from t0 b where b.a+a.a<10) +0 9 +set debug_dbug=@old_debug; +# correlated subquery, select, while inside the subquery +set @show_explain_probe_select_id=2; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where +2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where +Warnings: +Note 1003 select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1 +a (select max(a) from t0 b where b.a+a.a<10) +0 9 +set debug_dbug=@old_debug; +# correlated subquery, explain, while inside the subquery +set @show_explain_probe_select_id=2; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where +2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where +Warnings: +Note 1003 select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1 +a (select max(a) from t0 b where b.a+a.a<10) +0 9 +set debug_dbug=@old_debug; +# correlated subquery, explain, while inside the subquery +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_end'; +select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1; +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +a (select max(a) from t0 b where b.a+a.a<10) +0 9 +set debug_dbug=@old_debug; +# Try to do SHOW EXPLAIN for a query that runs a SET command: +# I've found experimentally that select_id==2 here... +# +set @show_explain_probe_select_id=2; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +set @foo= (select max(a) from t0 where sin(a) >0); +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +set debug_dbug=@old_debug; +# +# Attempt SHOW EXPLAIN for an UPDATE +# +create table t2 as select a as a, a as dummy from t0 limit 2; +set @show_explain_probe_select_id=2; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +update t2 set dummy=0 where (select max(a) from t0 where t2.a + t0.a <3) >3 ; +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +drop table t2; +set debug_dbug=@old_debug; +# +# Attempt SHOW EXPLAIN for a DELETE +# +create table t2 as select a as a, a as dummy from t0 limit 2; +set @show_explain_probe_select_id=2; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +delete from t2 where (select max(a) from t0 where t2.a + t0.a <3) >3 ; +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +drop table t2; +set debug_dbug=@old_debug; +# +# Multiple SHOW EXPLAIN calls for one select +# +create table t2 as select a as a, a as dummy from t0 limit 3; +set @show_explain_probe_select_id=2; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select t2.a, ((select max(a) from t0 where t2.a + t0.a <3) >3) as SUBQ from t2; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 3 +2 DEPENDENT SUBQUERY t0 ALL NULL NULL NULL NULL 10 Using where +Warnings: +Note 1003 select t2.a, ((select max(a) from t0 where t2.a + t0.a <3) >3) as SUBQ from t2 +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 3 +2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Query plan already deleted +Warnings: +Note 1003 select t2.a, ((select max(a) from t0 where t2.a + t0.a <3) >3) as SUBQ from t2 +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 3 +2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Query plan already deleted +Warnings: +Note 1003 select t2.a, ((select max(a) from t0 where t2.a + t0.a <3) >3) as SUBQ from t2 +a SUBQ +0 0 +1 0 +2 0 +drop table t2; +set debug_dbug=@old_debug; +# +# SHOW EXPLAIN for SELECT ... ORDER BY with "Using filesort" +# +explain select * from t0 order by a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using filesort +set debug_dbug='+d,show_explain_probe_join_exec_start'; +set @show_explain_probe_select_id=1; +select * from t0 order by a; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using filesort +Warnings: +Note 1003 select * from t0 order by a +a +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +set debug_dbug=@old_debug; +# +# SHOW EXPLAIN for SELECT ... with "Using temporary" +# +explain select distinct a from t0; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using temporary +set debug_dbug='+d,show_explain_probe_join_exec_start'; +set @show_explain_probe_select_id=1; +select distinct a from t0; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using temporary +Warnings: +Note 1003 select distinct a from t0 +a +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +set debug_dbug=@old_debug; +# +# SHOW EXPLAIN for SELECT ... with "Using temporary; Using filesort" +# +explain select distinct a from t0; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using temporary +set debug_dbug='+d,show_explain_probe_join_exec_start'; +set @show_explain_probe_select_id=1; +select distinct a from t0; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using temporary +Warnings: +Note 1003 select distinct a from t0 +a +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +set debug_dbug=@old_debug; +# +# MDEV-238: SHOW EXPLAIN: Server crashes in JOIN::print_explain with FROM subquery and GROUP BY +# +CREATE TABLE t2 ( a INT ); +INSERT INTO t2 VALUES (1),(2),(1),(4),(2); +explain SELECT alias.a FROM t2, ( SELECT * FROM t2 ) AS alias GROUP BY alias.a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 5 Using temporary; Using filesort +1 SIMPLE t2 ALL NULL NULL NULL NULL 5 Using join buffer (flat, BNL join) +set debug_dbug='+d,show_explain_in_find_all_keys'; +SELECT alias.a FROM t2, ( SELECT * FROM t2 ) AS alias GROUP BY alias.a; +# FIXED by "conservative assumptions about when QEP is available" fix: +# NOTE: current code will not show "Using join buffer": +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +a +1 +2 +4 +set debug_dbug=@old_debug; +DROP TABLE t2; +# +# MDEV-239: Assertion `field_types == 0 ... ' failed in Protocol_text::store(double, uint32, String*) with +# SHOW EXPLAIN over EXPLAIN EXTENDED +# +CREATE TABLE t2 (a INT); +INSERT INTO t2 VALUES (1),(2),(1),(4),(2); +EXPLAIN EXTENDED SELECT alias.a FROM t2, ( SELECT * FROM t2 ) AS alias GROUP BY alias.a ; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 5 100.00 Using temporary; Using filesort +1 SIMPLE t2 ALL NULL NULL NULL NULL 5 100.00 Using join buffer (flat, BNL join) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` join `test`.`t2` group by `test`.`t2`.`a` +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_end'; +EXPLAIN EXTENDED SELECT alias.a FROM t2, ( SELECT * FROM t2 ) AS alias GROUP BY alias.a ; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 5 Using temporary; Using filesort +1 SIMPLE t2 ALL NULL NULL NULL NULL 5 Using join buffer (flat, BNL join) +Warnings: +Note 1003 EXPLAIN EXTENDED SELECT alias.a FROM t2, ( SELECT * FROM t2 ) AS alias GROUP BY alias.a +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 5 100.00 Using temporary; Using filesort +1 SIMPLE t2 ALL NULL NULL NULL NULL 5 100.00 Using join buffer (flat, BNL join) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` join `test`.`t2` group by `test`.`t2`.`a` +set debug_dbug=@old_debug; +DROP TABLE t2; +# +# MDEV-240: SHOW EXPLAIN: Assertion `this->optimized == 2' failed in +# JOIN::print_explain on query with a JOIN, TEMPTABLE view, +# +CREATE TABLE t3 (a INT); +CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t3; +INSERT INTO t3 VALUES (8); +CREATE TABLE t2 (b INT); +INSERT INTO t2 VALUES (4),(5),(6),(7),(8),(9); +explain SELECT * FROM v1, t2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY <derived2> system NULL NULL NULL NULL 1 +1 PRIMARY t2 ALL NULL NULL NULL NULL 6 +2 DERIVED t3 system NULL NULL NULL NULL 1 +set @show_explain_probe_select_id=2; +set debug_dbug='+d,show_explain_probe_join_exec_end'; +SELECT * FROM v1, t2; +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +a b +8 4 +8 5 +8 6 +8 7 +8 8 +8 9 +set debug_dbug=@old_debug; +DROP VIEW v1; +DROP TABLE t2, t3; +# +# MDEV-267: SHOW EXPLAIN: Server crashes in JOIN::print_explain on most of queries +# +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_end'; +select sleep(1); +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL No tables used +Warnings: +Note 1003 select sleep(1) +sleep(1) +0 +set debug_dbug=@old_debug; +# +# Same as above, but try another reason for JOIN to be degenerate +# +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_end'; +select * from t0 where 1>10; +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +a +set debug_dbug=@old_debug; +# +# Same as above, but try another reason for JOIN to be degenerate (2) +# +create table t3(a int primary key); +insert into t3 select a from t0; +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_end'; +select * from t0,t3 where t3.a=112233; +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +a a +set debug_dbug=@old_debug; +drop table t3; +# +# MDEV-270: SHOW EXPLAIN: server crashes in JOIN::print_explain on a query with +# select tables optimized away +# +CREATE TABLE t2 (pk INT PRIMARY KEY, a INT ) ENGINE=MyISAM; +INSERT INTO t2 VALUES +(1,4),(2,62),(3,7),(4,1),(5,0),(6,7),(7,7),(8,1),(9,7),(10,1), +(11,5),(12,2),(13,0),(14,1),(15,8),(16,1),(17,1),(18,9),(19,1),(20,5) ; +explain SELECT * FROM t2 WHERE a = +(SELECT MAX(a) FROM t2 +WHERE pk= (SELECT MAX(pk) FROM t2 WHERE pk = 3) +); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 20 Using where +2 SUBQUERY t2 const PRIMARY PRIMARY 4 const 1 Using where +3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +set @show_explain_probe_select_id=2; +set debug_dbug='+d,show_explain_probe_do_select'; +SELECT * FROM t2 WHERE a = +(SELECT MAX(a) FROM t2 +WHERE pk= (SELECT MAX(pk) FROM t2 WHERE pk = 3) +); +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 20 Using where +2 SUBQUERY t2 const PRIMARY PRIMARY 4 const 1 Using where +3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +Warnings: +Note 1003 SELECT * FROM t2 WHERE a = +(SELECT MAX(a) FROM t2 +WHERE pk= (SELECT MAX(pk) FROM t2 WHERE pk = 3) +) +pk a +3 7 +6 7 +7 7 +9 7 +set debug_dbug=@old_debug; +drop table t2; +# +# MDEV-273: SHOW EXPLAIN: server crashes in JOIN::print_explain on a query with impossible WHERE +# +CREATE TABLE t2 (a1 INT, KEY(a1)) ENGINE=MyISAM; +INSERT INTO t2 VALUES +(4),(6),(7),(1),(0),(7),(7),(1),(7),(1), +(5),(2),(0),(1),(8),(1),(1),(9),(1),(5); +CREATE TABLE t3 (b1 INT) ENGINE=MyISAM; +INSERT INTO t3 VALUES +(4),(5),(8),(4),(8),(2),(9),(6),(4),(8), +(3),(5),(9),(6),(8),(3),(2),(6),(3),(1), +(4),(3),(1),(7),(0),(0),(9),(5),(9),(0), +(2),(2),(5),(9),(1),(4),(8),(6),(5),(5), +(1),(7),(2),(8),(9),(3),(2),(6),(6),(5), +(4),(3),(2),(7),(4),(6),(0),(8),(5),(8), +(2),(9),(7),(5),(7),(0),(4),(3),(1),(0), +(6),(2),(8),(3),(7),(3),(5),(5),(1),(2), +(1),(7),(1),(9),(9),(8),(3); +CREATE TABLE t4 (c1 INT) ENGINE=MyISAM; +EXPLAIN +SELECT count(*) FROM t2, t3 +WHERE a1 < ALL ( +SELECT a1 FROM t2 +WHERE a1 IN ( SELECT a1 FROM t2, t4 ) +); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 index NULL a1 5 NULL 20 Using where; Using index +1 PRIMARY t3 ALL NULL NULL NULL NULL 87 Using join buffer (flat, BNL join) +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_do_select'; +SELECT count(*) FROM t2, t3 +WHERE a1 < ALL ( +SELECT a1 FROM t2 +WHERE a1 IN ( SELECT a1 FROM t2, t4 ) +); +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 index NULL a1 5 NULL 20 Using where; Using index +1 PRIMARY t3 ALL NULL NULL NULL NULL 87 Using join buffer (flat, BNL join) +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table +Warnings: +Note 1003 SELECT count(*) FROM t2, t3 +WHERE a1 < ALL ( +SELECT a1 FROM t2 +WHERE a1 IN ( SELECT a1 FROM t2, t4 ) +) +count(*) +1740 +set debug_dbug=@old_debug; +drop table t2, t3, t4; +# +# MDEV-275: SHOW EXPLAIN: server crashes in JOIN::print_explain with IN subquery and aggregate function +# +CREATE TABLE t2 ( `pk` INT NOT NULL PRIMARY KEY, `a1` INT NOT NULL, KEY(`a1`)) ENGINE=MyISAM; +INSERT INTO t2 VALUES +(1,5),(2,4),(3,6),(4,9),(5,2),(6,8),(7,4),(8,8),(9,0),(10,43), +(11,23),(12,3),(13,45),(14,16),(15,2),(16,33),(17,2),(18,5),(19,9),(20,2); +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_end'; +SELECT * FROM t2 WHERE (5, 78) IN (SELECT `a1`, MAX(`a1`) FROM t2 GROUP BY `a1`); +show explain for $thr2; +ERROR HY000: Target is not running an EXPLAINable command +pk a1 +set debug_dbug=@old_debug; +DROP TABLE t2; +DROP TABLE t1; +# +# MDEV-305: SHOW EXPLAIN: ref returned by SHOW EXPLAIN is different from the normal EXPLAIN ('const' vs empty string) +# +CREATE TABLE t1(a INT, KEY(a)); +INSERT INTO t1 VALUES (3),(1),(5),(1); +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +SELECT 'test' FROM t1 WHERE a=1; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref a a 5 const 1 Using index +Warnings: +Note 1003 SELECT 'test' FROM t1 WHERE a=1 +test +test +test +set debug_dbug=@old_debug; +DROP TABLE t1; +# +# MDEV-299: SHOW EXPLAIN: Plan produced by SHOW EXPLAIN changes back and forth during query execution +# +create table t1 (key1 int, col1 int, col2 int, filler char(100), key(key1)); +insert into t1 select A.a+ 10 * B.a, 10, 10, 'filler-data' from t0 A, t0 B; +update t1 set col1=3, col2=10 where key1=1; +update t1 set col1=3, col2=1000 where key1=2; +update t1 set col1=3, col2=10 where key1=3; +update t1 set col1=3, col2=1000 where key1=4; +set @tmp_mdev299_jcl= @@join_cache_level; +set join_cache_level=0; +explain select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE A ALL NULL NULL NULL NULL 100 Using where +1 SIMPLE B ALL key1 NULL NULL NULL 100 Range checked for each record (index map: 0x1) +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_test_if_quick_select'; +select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE A ALL NULL NULL NULL NULL 100 Using where +1 SIMPLE B ALL key1 NULL NULL NULL 100 Range checked for each record (index map: 0x1) +Warnings: +Note 1003 select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100 +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE A ALL NULL NULL NULL NULL 100 Using where +1 SIMPLE B ALL key1 NULL NULL NULL 100 Range checked for each record (index map: 0x1) +Warnings: +Note 1003 select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100 +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE A ALL NULL NULL NULL NULL 100 Using where +1 SIMPLE B ALL key1 NULL NULL NULL 100 Range checked for each record (index map: 0x1) +Warnings: +Note 1003 select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100 +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE A ALL NULL NULL NULL NULL 100 Using where +1 SIMPLE B ALL key1 NULL NULL NULL 100 Range checked for each record (index map: 0x1) +Warnings: +Note 1003 select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100 +count(*) +212 +set debug_dbug=@old_debug; +drop table t1; +# +# MDEV-297: SHOW EXPLAIN: Server gets stuck until timeout occurs while +# executing SHOW INDEX and SHOW EXPLAIN in parallel +# +CREATE TABLE t1(a INT, b INT, c INT, KEY(a), KEY(b), KEY(c)); +INSERT INTO t1 (a) VALUES (3),(1),(5),(1); +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +SHOW INDEX FROM t1; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE STATISTICS ALL NULL NULL NULL NULL NULL Skip_open_table; Scanned all databases +Warnings: +Note 1003 SHOW INDEX FROM t1 +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment +t1 1 a 1 a A NULL NULL NULL YES BTREE +t1 1 b 1 b A NULL NULL NULL YES BTREE +t1 1 c 1 c A NULL NULL NULL YES BTREE +set debug_dbug=@old_debug; +DROP TABLE t1; +# +# MDEV-324: SHOW EXPLAIN: Plan produced by SHOW EXPLAIN for a query with TEMPTABLE view +# loses 'DERIVED' line on the way without saying that the plan was already deleted +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2); +CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1; +EXPLAIN SELECT a + 1 FROM v1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 +2 DERIVED t1 ALL NULL NULL NULL NULL 2 +set debug_dbug='+d,show_explain_probe_join_tab_preread'; +set @show_explain_probe_select_id=1; +SELECT a + 1 FROM v1; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 +2 DERIVED NULL NULL NULL NULL NULL NULL NULL Query plan already deleted +Warnings: +Note 1003 SELECT a + 1 FROM v1 +a + 1 +2 +3 +set debug_dbug=@old_debug; +DROP VIEW v1; +DROP TABLE t1; +# +# MDEV-323: SHOW EXPLAIN: Plan produced by SHOW EXPLAIN loses +# 'UNION RESULT' line on the way without saying that the plan was already deleted +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (4),(6); +EXPLAIN +SELECT a FROM t1 WHERE a IN ( SELECT 1+SLEEP(0.01) UNION SELECT 2 ); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where +2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used +3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL +set debug_dbug='+d,show_explain_probe_union_read'; +SELECT a FROM t1 WHERE a IN ( SELECT 1+SLEEP(0.01) UNION SELECT 2 ); +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where +2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used +3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL +Warnings: +Note 1003 SELECT a FROM t1 WHERE a IN ( SELECT 1+SLEEP(0.01) UNION SELECT 2 ) +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where +2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used +3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL +Warnings: +Note 1003 SELECT a FROM t1 WHERE a IN ( SELECT 1+SLEEP(0.01) UNION SELECT 2 ) +a +set debug_dbug=@old_debug; +DROP TABLE t1; +# +# MDEV-327: SHOW EXPLAIN: Different select_type in plans produced by SHOW EXPLAIN +# and standard EXPLAIN: 'SUBQUERY' vs 'DEPENDENT SUBQUERY' +# +CREATE TABLE t1 (a INT) ENGINE=Aria; +INSERT INTO t1 VALUES +(4),(6),(3),(5),(3),(246),(2),(9),(3),(8), +(1),(8),(8),(5),(7),(5),(1),(6),(2),(9); +CREATE TABLE t2 (b INT) ENGINE=Aria; +INSERT INTO t2 VALUES +(1),(7),(4),(7),(0),(2),(9),(4),(0),(9), +(1),(3),(8),(8),(18),(84),(6),(3),(6),(6); +EXPLAIN +SELECT * FROM t1, ( SELECT * FROM t2 ) AS alias +WHERE a < ALL ( SELECT b FROM t1, t2 WHERE a = b ); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 20 Using where +1 PRIMARY t2 ALL NULL NULL NULL NULL 20 +3 SUBQUERY t1 ALL NULL NULL NULL NULL 20 +3 SUBQUERY t2 ALL NULL NULL NULL NULL 20 Using where +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +SELECT * FROM t1, ( SELECT * FROM t2 ) AS alias +WHERE a < ALL ( SELECT b FROM t1, t2 WHERE a = b ); +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 20 Using where +1 PRIMARY t2 ALL NULL NULL NULL NULL 20 +3 SUBQUERY t1 ALL NULL NULL NULL NULL 20 +3 SUBQUERY t2 ALL NULL NULL NULL NULL 20 Using where +Warnings: +Note 1003 SELECT * FROM t1, ( SELECT * FROM t2 ) AS alias +WHERE a < ALL ( SELECT b FROM t1, t2 WHERE a = b ) +a b +set debug_dbug=@old_debug; +DROP TABLE t1, t2; +# +# Test that SHOW EXPLAIN will print 'Distinct'. +# +CREATE TABLE t1 (a int(10) unsigned not null primary key,b int(10) unsigned); +INSERT INTO t1 VALUES (1,1),(2,1),(3,1),(4,1); +CREATE TABLE t3 (a int(10) unsigned, key(A), b text); +INSERT INTO t3 VALUES (1,'1'),(2,'2'); +create temporary table t4 select * from t3; +insert into t3 select * from t4; +insert into t4 select * from t3; +insert into t3 select * from t4; +insert into t4 select * from t3; +insert into t3 select * from t4; +insert into t4 select * from t3; +insert into t3 select * from t4; +explain select distinct t1.a from t1,t3 where t1.a=t3.a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index PRIMARY PRIMARY 4 NULL 4 Using index; Using temporary +1 SIMPLE t3 ref a a 5 test.t1.a 7 Using index; Distinct +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select distinct t1.a from t1,t3 where t1.a=t3.a; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index PRIMARY PRIMARY 4 NULL 4 Using index; Using temporary +1 SIMPLE t3 ref a a 5 test.t1.a 7 Using index; Distinct +Warnings: +Note 1003 select distinct t1.a from t1,t3 where t1.a=t3.a +a +1 +2 +set debug_dbug=@old_debug; +drop table t1,t3,t4; +# +# ---------- SHOW EXPLAIN and permissions ----------------- +# +grant ALL on test.* to test2@localhost; +grant super on *.* to test2@localhost; +# +# First, make sure that user 'test2' cannot do SHOW EXPLAIN on us +# +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select * from t0 where a < 3; +show explain for $thr2; +ERROR 42000: Access denied; you need (at least one of) the PROCESS privilege(s) for this operation +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using where +Warnings: +Note 1003 select * from t0 where a < 3 +a +0 +1 +2 +set debug_dbug=@old_debug; +# +# Check that user test2 can do SHOW EXPLAIN on its own queries +# +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select * from t0 where a < 3; +show explain for $thr_con2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using where +Warnings: +Note 1003 select * from t0 where a < 3 +a +0 +1 +2 +# +# Now, grant test2 a PROCESSLIST permission, and see that he's able to observe us +# +grant process on *.* to test2@localhost; +set debug_dbug=@old_debug; +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select * from t0 where a < 3; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using where +Warnings: +Note 1003 select * from t0 where a < 3 +a +0 +1 +2 +set debug_dbug=@old_debug; +revoke all privileges on test.* from test2@localhost; +drop user test2@localhost; +# +# Test that it is possible to KILL a SHOW EXPLAIN command that's waiting +# on its target thread +# +create table t1 (pk int primary key, data char(64)) engine=innodb; +insert into t1 select A.a + 10 * B.a + 100 * C.a, 'data1' from t0 A, t0 B, t0 C; +# Lock two threads +set autocommit=0; +select * from t1 where pk between 10 and 20 for update; +pk data +10 data1 +11 data1 +12 data1 +13 data1 +14 data1 +15 data1 +16 data1 +17 data1 +18 data1 +19 data1 +20 data1 +set autocommit=0; +select * from t1 where pk between 10 and 20 for update; +# do: send_eval show explain for thr2; +kill query $thr_default; +ERROR 70100: Query execution was interrupted +rollback; +pk data +10 data1 +11 data1 +12 data1 +13 data1 +14 data1 +15 data1 +16 data1 +17 data1 +18 data1 +19 data1 +20 data1 +drop table t1; +# +# Check that the I_S table is invisible +# +select table_name from information_schema.tables where table_schema='information_schema' and table_name like '%explain%'; +table_name +# +# MDEV-325: SHOW EXPLAIN: Plan produced by SHOW EXPLAIN is different from standard EXPLAIN: type ALL vs 'index_merge'.. +# +CREATE TABLE t1 (a INT, b INT, KEY(a), KEY(b)) ENGINE=MyISAM; +INSERT INTO t1 VALUES +(8,0),(128,5050),(5372,8),(234,7596),(2,0),(2907,8930),(1,0), +(0,5224),(8,7638),(960,5),(9872,1534),(0,2295),(3408,9809), +(7,0),(1168,0),(2089,5570),(0,205),(88,1018),(0,26528), +(0,0),(4,5567),(1444,145),(6,0),(1,7535),(7793,534),(70,9), +(178,1),(44,5),(189,0),(3,0); +EXPLAIN +SELECT a+SLEEP(0.01) FROM t1 +WHERE a IN ( 255, 0 ) OR b BETWEEN 6 AND 129 +ORDER BY b; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index_merge a,b a,b 5,5 NULL 8 Using sort_union(a,b); Using where; Using filesort +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +SELECT a+SLEEP(0.01) FROM t1 +WHERE a IN ( 255, 0 ) OR b BETWEEN 6 AND 129 +ORDER BY b; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index_merge a,b a,b 5,5 NULL 8 Using sort_union(a,b); Using where; Using filesort +Warnings: +Note 1003 SELECT a+SLEEP(0.01) FROM t1 +WHERE a IN ( 255, 0 ) OR b BETWEEN 6 AND 129 +ORDER BY b +a+SLEEP(0.01) +0 +5372 +70 +0 +0 +0 +0 +set debug_dbug=@old_debug; +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_do_select'; +SELECT a+SLEEP(0.01) FROM t1 +WHERE a IN ( 255, 0 ) OR b BETWEEN 6 AND 129 +ORDER BY b; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index_merge a,b a,b 5,5 NULL 8 Using sort_union(a,b); Using where; Using filesort +Warnings: +Note 1003 SELECT a+SLEEP(0.01) FROM t1 +WHERE a IN ( 255, 0 ) OR b BETWEEN 6 AND 129 +ORDER BY b +a+SLEEP(0.01) +0 +5372 +70 +0 +0 +0 +0 +set debug_dbug=@old_debug; +drop table t1; +# +# MDEV-298: SHOW EXPLAIN: Plan returned by SHOW EXPLAIN only contains +# 'Using temporary' while the standard EXPLAIN says 'Using temporary; Using filesort' +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9), +(10),(11),(12),(13),(14),(15),(16); +INSERT INTO t1 SELECT t11.a FROM t1 t11, t1 t12, t1 t13; +EXPLAIN SELECT a FROM t1 GROUP BY a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 4112 Using temporary; Using filesort +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +SELECT a FROM t1 GROUP BY a; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 4112 Using temporary; Using filesort +Warnings: +Note 1003 SELECT a FROM t1 GROUP BY a +a +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +set debug_dbug=@old_debug; +drop table t1; +# +# MDEV-408: SHOW EXPLAIN: Some values are chopped off in SHOW EXPLAIN output +# +CREATE TABLE t1 (a INT, b VARCHAR(35)) ENGINE=InnoDB; +INSERT INTO t1 VALUES (3989,'Abilene'),(3873,'Akron'); +CREATE TABLE t2 (c INT, d VARCHAR(52) PRIMARY KEY, KEY(c)) ENGINE=InnoDB; +INSERT INTO t2 VALUES (86,'English'),(87,'Russian'); +explain SELECT SUM(a + SLEEP(0.1)) FROM t1 WHERE a IN ( SELECT c FROM t2 WHERE d < b ) OR b < 's'; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where +2 DEPENDENT SUBQUERY t2 index_subquery PRIMARY,c c 5 func 1 Using index; Using where +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +SELECT SUM(a + SLEEP(0.1)) FROM t1 WHERE a IN ( SELECT c FROM t2 WHERE d < b ) OR b < 's'; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where +2 DEPENDENT SUBQUERY t2 index_subquery PRIMARY,c c 5 func 1 Using index; Using where +Warnings: +Note 1003 SELECT SUM(a + SLEEP(0.1)) FROM t1 WHERE a IN ( SELECT c FROM t2 WHERE d < b ) OR b < 's' +SUM(a + SLEEP(0.1)) +7862 +set debug_dbug=@old_debug; +drop table t1, t2; +# +# MDEV-412: SHOW EXPLAIN: Server crashes in JOIN::print_explain on a query with inner join and ORDER BY the same column twice +# +CREATE TABLE t1 (a INT PRIMARY KEY, b VARCHAR(3), KEY(b)) ENGINE=MyISAM; +INSERT INTO t1 VALUES +(3795,'USA'),(3913,'USA'),(3846,'ITA'),(4021,'USA'),(4005,'RUS'),(4038,'USA'), +(3825,'USA'),(3840,'USA'),(3987,'USA'),(3807,'USA'),(3896,'USA'),(4052,'USA'), +(3973,'USA'),(3982,'ITA'),(3965,'USA'),(3852,'RUS'),(4006,'USA'),(3800,'USA'), +(4020,'USA'),(4040,'USA'),(3916,'USA'),(3817,'USA'),(3885,'USA'),(3802,'USA'), +(4009,'ITA'),(3895,'USA'),(3963,'RUS'),(4045,'USA'),(3988,'USA'),(3815,'USA'), +(4063,'USA'),(3978,'USA'),(4019,'USA'),(3954,'USA'),(3950,'USA'),(3974,'ITA'), +(4054,'USA'),(4061,'RUS'),(3976,'USA'),(3966,'USA'),(3957,'USA'),(3981,'USA'), +(3923,'USA'),(3876,'USA'),(3819,'USA'),(3877,'USA'),(3829,'ITA'),(3964,'USA'), +(4053,'RUS'),(3917,'USA'),(3874,'USA'),(4023,'USA'),(4001,'USA'),(3872,'USA'), +(3890,'USA'),(3962,'USA'),(3886,'USA'),(4026,'ITA'),(3869,'USA'),(3937,'RUS'), +(3975,'USA'),(3944,'USA'),(3908,'USA'),(3867,'USA'),(3947,'USA'),(3838,'USA'), +(3796,'USA'),(3893,'USA'),(3920,'ITA'),(3994,'USA'),(3875,'RUS'),(4011,'USA'), +(4013,'USA'),(3810,'USA'),(3834,'USA'),(3968,'USA'),(3931,'USA'),(3839,'USA'), +(4042,'USA'),(4039,'ITA'),(3811,'USA'),(3837,'RUS'),(4041,'USA'),(3884,'USA'), +(3894,'USA'),(3879,'USA'),(3942,'USA'),(3959,'USA'),(3814,'USA'),(4044,'USA'), +(3971,'ITA'),(3823,'USA'),(3793,'RUS'),(3855,'USA'),(3905,'USA'),(3865,'USA'), +(4046,'USA'),(3990,'USA'),(4022,'USA'),(3833,'USA'),(3918,'USA'),(4064,'ITA'), +(3821,'USA'),(3836,'RUS'),(3921,'USA'),(3914,'USA'),(3888,'USA'); +CREATE TABLE t2 (c VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM; +INSERT INTO t2 VALUES ('USA'); +CREATE TABLE t3 (d VARCHAR(3), e VARCHAR(52), PRIMARY KEY (d,e)) ENGINE=MyISAM; +INSERT INTO t3 VALUES +('JPN','Japanese'),('KOR','Korean'),('POL','Polish'),('PRT','Portuguese'), +('ESP','Spanish'),('FRA','French'),('VNM','Vietnamese'); +explain +SELECT b AS field1, b AS field2 FROM t1, t2, t3 WHERE d = b ORDER BY field1, field2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 system NULL NULL NULL NULL 1 Using filesort +1 SIMPLE t1 index b b 6 NULL 107 Using where; Using index +1 SIMPLE t3 ref PRIMARY PRIMARY 5 test.t1.b 1 Using index +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_do_select'; +SELECT b AS field1, b AS field2 FROM t1, t2, t3 WHERE d = b ORDER BY field1, field2; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 system NULL NULL NULL NULL 1 Using filesort +1 SIMPLE t1 index b b 6 NULL 107 Using where; Using index +1 SIMPLE t3 ref PRIMARY PRIMARY 5 test.t1.b 1 Using index +Warnings: +Note 1003 SELECT b AS field1, b AS field2 FROM t1, t2, t3 WHERE d = b ORDER BY field1, field2 +field1 field2 +set debug_dbug=@old_debug; +DROP TABLE t1,t2,t3; +# +# MDEV-423: SHOW EXPLAIN: 'Using where' for a subquery is shown in EXPLAIN, but not in SHOW EXPLAIN output +# +CREATE TABLE t1 (a INT) ENGINE=MyISAM; +INSERT INTO t1 VALUES (7),(0),(9),(3),(4),(2),(5),(7),(0),(9),(3),(4),(2),(5); +CREATE TABLE t2 (b INT, c INT) ENGINE=MyISAM; +INSERT INTO t2 VALUES +(0,4),(8,6),(1,3),(8,5),(9,3),(24,246),(6,2),(1,9),(6,3),(2,8), +(4,1),(8,8),(4,8),(4,5),(7,7),(4,5),(1,1),(9,6),(4,2),(8,9); +create table t3 like t2; +insert into t3 select * from t2; +explain +SELECT max(a+b+c) FROM t1 AS alias1, ( SELECT * FROM t2 ) AS alias +WHERE EXISTS ( SELECT * FROM t3 WHERE b = c ) OR a <= 10; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY alias1 ALL NULL NULL NULL NULL 14 +1 PRIMARY t2 ALL NULL NULL NULL NULL 20 +3 SUBQUERY t3 ALL NULL NULL NULL NULL 20 Using where +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +SELECT max(a+b+c) FROM t1 AS alias1, ( SELECT * FROM t2 ) AS alias +WHERE EXISTS ( SELECT * FROM t3 WHERE b = c ) OR a <= 10; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY alias1 ALL NULL NULL NULL NULL 14 +1 PRIMARY t2 ALL NULL NULL NULL NULL 20 +3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Query plan already deleted +Warnings: +Note 1003 SELECT max(a+b+c) FROM t1 AS alias1, ( SELECT * FROM t2 ) AS alias +WHERE EXISTS ( SELECT * FROM t3 WHERE b = c ) OR a <= 10 +max(a+b+c) +279 +set debug_dbug=@old_debug; +DROP TABLE t1,t2,t3; +# +# MDEV-416: Server crashes in SQL_SELECT::cleanup on EXPLAIN with SUM ( DISTINCT ) in a non-correlated subquery (5.5-show-explain tree) +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2); +CREATE TABLE t2 (b INT); +INSERT INTO t2 VALUES (8),(9); +EXPLAIN SELECT * FROM t1 +WHERE ( 8, 89 ) IN ( SELECT b, SUM( DISTINCT b ) FROM t2 GROUP BY b ); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using filesort +DROP TABLE t1,t2; +# +# Check if queries in non-default charsets work. +# +set names cp1251; +select charset('ãû'); +charset('ãû') +cp1251 +select hex('ãû'); +hex('ãû') +E3FB +set @show_explain_probe_select_id=1; +set debug_dbug='+d,show_explain_probe_join_exec_start'; +select * from t0 where length('ãû') = a; +set names utf8; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using where +Warnings: +Note 1003 select * from t0 where length('гы') = a +set names default; +a +2 +set debug_dbug=@old_debug; +set names default; +# +# MDEV-462: SHOW EXPLAIN: Assertion `table_list->table' fails in find_field_in_table_ref if FOR contains a non-numeric value +# +show explain for foo; +ERROR HY000: You may only use constant expressions in this statement +# End +drop table t0; +set debug_sync='RESET'; diff --git a/mysql-test/r/show_explain_ps.result b/mysql-test/r/show_explain_ps.result new file mode 100644 index 00000000000..625b9cfddae --- /dev/null +++ b/mysql-test/r/show_explain_ps.result @@ -0,0 +1,27 @@ +drop table if exists t0, t1; +select * from performance_schema.setup_instruments where name like '%show_explain%'; +NAME ENABLED TIMED +wait/synch/cond/sql/show_explain YES YES +# We've got no instances +select * from performance_schema.cond_instances where name like '%show_explain%'; +NAME OBJECT_INSTANCE_BEGIN +# Check out if our cond is hit. +create table t0 (a int); +insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); +set @show_explain_probe_select_id=1; +set debug_dbug='d,show_explain_probe_join_exec_start'; +select count(*) from t0 where a < 100000; +show explain for $thr2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using where +Warnings: +Note 1003 select count(*) from t0 where a < 100000 +count(*) +10 +set debug_dbug=''; +select event_name +from performance_schema.events_waits_history_long +where event_name='wait/synch/cond/sql/show_explain'; +event_name +wait/synch/cond/sql/show_explain +drop table t0; diff --git a/mysql-test/r/signal_code.result b/mysql-test/r/signal_code.result index ca46f1d2079..4cd1de97729 100644 --- a/mysql-test/r/signal_code.result +++ b/mysql-test/r/signal_code.result @@ -20,16 +20,16 @@ return 0; end $$ show procedure code signal_proc; Pos Instruction -0 stmt 130 "SIGNAL foo" -1 stmt 130 "SIGNAL foo SET MESSAGE_TEXT = "This i..." -2 stmt 131 "RESIGNAL foo" -3 stmt 131 "RESIGNAL foo SET MESSAGE_TEXT = "This..." +0 stmt 132 "SIGNAL foo" +1 stmt 132 "SIGNAL foo SET MESSAGE_TEXT = "This i..." +2 stmt 133 "RESIGNAL foo" +3 stmt 133 "RESIGNAL foo SET MESSAGE_TEXT = "This..." drop procedure signal_proc; show function code signal_func; Pos Instruction -0 stmt 130 "SIGNAL foo" -1 stmt 130 "SIGNAL foo SET MESSAGE_TEXT = "This i..." -2 stmt 131 "RESIGNAL foo" -3 stmt 131 "RESIGNAL foo SET MESSAGE_TEXT = "This..." +0 stmt 132 "SIGNAL foo" +1 stmt 132 "SIGNAL foo SET MESSAGE_TEXT = "This i..." +2 stmt 133 "RESIGNAL foo" +3 stmt 133 "RESIGNAL foo SET MESSAGE_TEXT = "This..." 4 freturn 3 0 drop function signal_func; diff --git a/mysql-test/r/sp-code.result b/mysql-test/r/sp-code.result index 67651294498..3061c58747a 100644 --- a/mysql-test/r/sp-code.result +++ b/mysql-test/r/sp-code.result @@ -155,11 +155,11 @@ Pos Instruction 0 stmt 9 "drop temporary table if exists sudoku..." 1 stmt 1 "create temporary table sudoku_work ( ..." 2 stmt 1 "create temporary table sudoku_schedul..." -3 stmt 88 "call sudoku_init()" +3 stmt 90 "call sudoku_init()" 4 jump_if_not 7(8) p_naive@0 5 stmt 4 "update sudoku_work set cnt = 0 where ..." 6 jump 8 -7 stmt 88 "call sudoku_count()" +7 stmt 90 "call sudoku_count()" 8 stmt 6 "insert into sudoku_schedule (row,col)..." 9 set v_scounter@2 0 10 set v_i@3 1 diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result index defd3955f6c..c297ceda572 100644 --- a/mysql-test/r/sp.result +++ b/mysql-test/r/sp.result @@ -6403,14 +6403,14 @@ select 1; /*! select 2; */ select 3; /*!00000 select 4; */ -/*!99999 select 5; */ +/*!999999 select 5; */ end $$ create procedure proc_25411_b( /* real comment */ /*! p1 int, */ /*!00000 p2 int */ -/*!99999 ,p3 int */ +/*!999999 ,p3 int */ ) begin select p1, p2; @@ -6418,11 +6418,11 @@ end $$ create procedure proc_25411_c() begin -select 1/*!,2*//*!00000,3*//*!99999,4*/; -select 1/*! ,2*//*!00000 ,3*//*!99999 ,4*/; -select 1/*!,2 *//*!00000,3 *//*!99999,4 */; -select 1/*! ,2 *//*!00000 ,3 *//*!99999 ,4 */; -select 1 /*!,2*/ /*!00000,3*/ /*!99999,4*/ ; +select 1/*!,2*//*!00000,3*//*!999999,4*/; +select 1/*! ,2*//*!00000 ,3*//*!999999 ,4*/; +select 1/*!,2 *//*!00000,3 *//*!999999,4 */; +select 1/*! ,2 *//*!00000 ,3 *//*!999999 ,4 */; +select 1 /*!,2*/ /*!00000,3*/ /*!999999,4*/ ; end $$ show create procedure proc_25411_a; diff --git a/mysql-test/r/sql_mode.result b/mysql-test/r/sql_mode.result index 6736ca7f541..4fdac6b9cea 100644 --- a/mysql-test/r/sql_mode.result +++ b/mysql-test/r/sql_mode.result @@ -71,7 +71,7 @@ t1 CREATE TABLE `t1` ( `email` varchar(60) NOT NULL DEFAULT '', PRIMARY KEY (`a`), UNIQUE KEY `email` (`email`) -) TYPE=HEAP ROW_FORMAT=DYNAMIC +) TYPE=MEMORY ROW_FORMAT=DYNAMIC set sql_mode="postgresql,oracle,mssql,db2,maxdb"; select @@sql_mode; @@sql_mode diff --git a/mysql-test/r/stat_tables.result b/mysql-test/r/stat_tables.result new file mode 100644 index 00000000000..6905725a6a2 --- /dev/null +++ b/mysql-test/r/stat_tables.result @@ -0,0 +1,383 @@ +select @@global.use_stat_tables; +@@global.use_stat_tables +COMPLEMENTARY +select @@session.use_stat_tables; +@@session.use_stat_tables +COMPLEMENTARY +set @save_use_stat_tables=@@use_stat_tables; +set use_stat_tables='preferably'; +DROP DATABASE IF EXISTS dbt3_s001; +CREATE DATABASE dbt3_s001; +use dbt3_s001; +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='extended_keys=off'; +select * from mysql.table_stats; +db_name table_name cardinality +dbt3_s001 customer 150 +dbt3_s001 lineitem 6005 +dbt3_s001 nation 25 +dbt3_s001 orders 1500 +dbt3_s001 part 200 +dbt3_s001 partsupp 700 +dbt3_s001 region 5 +dbt3_s001 supplier 10 +select * from mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 customer PRIMARY 1 1.0000 +dbt3_s001 customer i_c_nationkey 1 6.0000 +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 nation PRIMARY 1 1.0000 +dbt3_s001 nation i_n_regionkey 1 5.0000 +dbt3_s001 orders PRIMARY 1 1.0000 +dbt3_s001 orders i_o_orderdate 1 1.3321 +dbt3_s001 orders i_o_custkey 1 15.0000 +dbt3_s001 part PRIMARY 1 1.0000 +dbt3_s001 part i_p_retailprice 1 1.0000 +dbt3_s001 partsupp PRIMARY 1 3.5000 +dbt3_s001 partsupp PRIMARY 2 1.0000 +dbt3_s001 partsupp i_ps_partkey 1 3.5000 +dbt3_s001 partsupp i_ps_suppkey 1 70.0000 +dbt3_s001 region PRIMARY 1 1.0000 +dbt3_s001 supplier PRIMARY 1 1.0000 +dbt3_s001 supplier i_s_nationkey 1 1.1111 +set optimizer_switch=@save_optimizer_switch; +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='index_condition_pushdown=off'; +EXPLAIN select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue +from customer, orders, lineitem, supplier, nation, region +where c_custkey = o_custkey and l_orderkey = o_orderkey +and l_suppkey = s_suppkey and c_nationkey = s_nationkey +and s_nationkey = n_nationkey and n_regionkey = r_regionkey +and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01' + and o_orderdate < date '1995-01-01' + interval '1' year +group by n_name +order by revenue desc; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE orders range PRIMARY,i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 179 Using where; Using temporary; Using filesort +1 SIMPLE customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where +1 SIMPLE nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 +1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join) +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where +1 SIMPLE supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where +select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue +from customer, orders, lineitem, supplier, nation, region +where c_custkey = o_custkey and l_orderkey = o_orderkey +and l_suppkey = s_suppkey and c_nationkey = s_nationkey +and s_nationkey = n_nationkey and n_regionkey = r_regionkey +and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01' + and o_orderdate < date '1995-01-01' + interval '1' year +group by n_name +order by revenue desc; +n_name revenue +PERU 321915.8715 +ARGENTINA 69817.1451 +set optimizer_switch=@save_optimizer_switch; +delete from mysql.index_stats; +select * from mysql.table_stats; +db_name table_name cardinality +dbt3_s001 customer 150 +dbt3_s001 lineitem 6005 +dbt3_s001 nation 25 +dbt3_s001 orders 1500 +dbt3_s001 part 200 +dbt3_s001 partsupp 700 +dbt3_s001 region 5 +dbt3_s001 supplier 10 +select * from mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 customer PRIMARY 1 1.0000 +dbt3_s001 customer i_c_nationkey 1 6.0000 +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 nation PRIMARY 1 1.0000 +dbt3_s001 nation i_n_regionkey 1 5.0000 +dbt3_s001 orders PRIMARY 1 1.0000 +dbt3_s001 orders i_o_orderdate 1 1.3321 +dbt3_s001 orders i_o_custkey 1 15.0000 +dbt3_s001 part PRIMARY 1 1.0000 +dbt3_s001 part i_p_retailprice 1 1.0000 +dbt3_s001 partsupp PRIMARY 1 3.5000 +dbt3_s001 partsupp PRIMARY 2 1.0000 +dbt3_s001 partsupp i_ps_partkey 1 3.5000 +dbt3_s001 partsupp i_ps_suppkey 1 70.0000 +dbt3_s001 region PRIMARY 1 1.0000 +dbt3_s001 supplier PRIMARY 1 1.0000 +dbt3_s001 supplier i_s_nationkey 1 1.1111 +select * from mysql.table_stats where table_name='orders'; +db_name table_name cardinality +dbt3_s001 orders 1500 +select * from mysql.index_stats where table_name='orders'; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 orders PRIMARY 1 1.0000 +dbt3_s001 orders i_o_orderdate 1 1.3321 +dbt3_s001 orders i_o_custkey 1 15.0000 +select (select cardinality from mysql.table_stats where table_name='orders') / +(select avg_frequency from mysql.index_stats +where index_name='i_o_orderdate' and prefix_arity=1) as n_distinct; +n_distinct +1126.0416 +select count(distinct o_orderdate) from orders; +count(distinct o_orderdate) +1126 +select (select cardinality from mysql.table_stats where table_name='orders') / +(select avg_frequency from mysql.index_stats +where index_name='i_o_custkey' and prefix_arity=1) as n_distinct; +n_distinct +100.0000 +select count(distinct o_custkey) from orders; +count(distinct o_custkey) +100 +show index from orders; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment +orders 0 PRIMARY 1 o_orderkey A 1500 NULL NULL BTREE +orders 1 i_o_orderdate 1 o_orderDATE A 1126 NULL NULL YES BTREE +orders 1 i_o_custkey 1 o_custkey A 100 NULL NULL YES BTREE +select index_name, column_name, cardinality from information_schema.statistics +where table_name='orders'; +index_name column_name cardinality +PRIMARY o_orderkey 1500 +i_o_orderdate o_orderDATE 1126 +i_o_custkey o_custkey 100 +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='index_condition_pushdown=off'; +EXPLAIN select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue +from customer, orders, lineitem, supplier, nation, region +where c_custkey = o_custkey and l_orderkey = o_orderkey +and l_suppkey = s_suppkey and c_nationkey = s_nationkey +and s_nationkey = n_nationkey and n_regionkey = r_regionkey +and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01' + and o_orderdate < date '1995-01-01' + interval '1' year +group by n_name +order by revenue desc; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE orders range PRIMARY,i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 179 Using where; Using temporary; Using filesort +1 SIMPLE customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where +1 SIMPLE nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 +1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join) +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where +1 SIMPLE supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where +select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue +from customer, orders, lineitem, supplier, nation, region +where c_custkey = o_custkey and l_orderkey = o_orderkey +and l_suppkey = s_suppkey and c_nationkey = s_nationkey +and s_nationkey = n_nationkey and n_regionkey = r_regionkey +and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01' + and o_orderdate < date '1995-01-01' + interval '1' year +group by n_name +order by revenue desc; +n_name revenue +PERU 321915.8715 +ARGENTINA 69817.1451 +set optimizer_switch=@save_optimizer_switch; +EXPLAIN select o_year, +sum(case when nation = 'UNITED STATES' then volume else 0 end) / +sum(volume) as mkt_share +from (select extract(year from o_orderdate) as o_year, +l_extendedprice * (1-l_discount) as volume, +n2.n_name as nation +from part, supplier, lineitem, orders, customer, +nation n1, nation n2, region +where p_partkey = l_partkey and s_suppkey = l_suppkey +and l_orderkey = o_orderkey and o_custkey = c_custkey +and c_nationkey = n1.n_nationkey and n1.n_regionkey = r_regionkey +and r_name = 'AMERICA' and s_nationkey = n2.n_nationkey +and o_orderdate between date '1995-01-01' and date '1996-12-31' + and p_type = 'STANDARD BRUSHED STEEL' ) as all_nations +group by o_year +order by o_year; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE orders ALL PRIMARY,i_o_orderdate,i_o_custkey NULL NULL NULL 1500 Using where; Using temporary; Using filesort +1 SIMPLE customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where +1 SIMPLE n1 eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 +1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join) +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where +1 SIMPLE part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where +1 SIMPLE supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where +1 SIMPLE n2 eq_ref PRIMARY PRIMARY 4 dbt3_s001.supplier.s_nationkey 1 +select o_year, +sum(case when nation = 'UNITED STATES' then volume else 0 end) / +sum(volume) as mkt_share +from (select extract(year from o_orderdate) as o_year, +l_extendedprice * (1-l_discount) as volume, +n2.n_name as nation +from part, supplier, lineitem, orders, customer, +nation n1, nation n2, region +where p_partkey = l_partkey and s_suppkey = l_suppkey +and l_orderkey = o_orderkey and o_custkey = c_custkey +and c_nationkey = n1.n_nationkey and n1.n_regionkey = r_regionkey +and r_name = 'AMERICA' and s_nationkey = n2.n_nationkey +and o_orderdate between date '1995-01-01' and date '1996-12-31' + and p_type = 'STANDARD BRUSHED STEEL' ) as all_nations +group by o_year +order by o_year; +o_year mkt_share +1995 0.4495521838895718 +1996 0.024585468215352495 +EXPLAIN select nation, o_year, sum(amount) as sum_profit +from (select n_name as nation, +extract(year from o_orderdate) as o_year, +l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount +from part, supplier, lineitem, partsupp, orders, nation +where s_suppkey = l_suppkey and ps_suppkey = l_suppkey +and ps_partkey = l_partkey and p_partkey = l_partkey +and o_orderkey = l_orderkey and s_nationkey = n_nationkey +and p_name like '%green%') as profit +group by nation, o_year +order by nation, o_year desc; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE supplier ALL PRIMARY,i_s_nationkey NULL NULL NULL 10 Using where; Using temporary; Using filesort +1 SIMPLE nation eq_ref PRIMARY PRIMARY 4 dbt3_s001.supplier.s_nationkey 1 +1 SIMPLE partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 70 +1 SIMPLE part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey_partkey 10 dbt3_s001.partsupp.ps_partkey,dbt3_s001.supplier.s_suppkey 8 +1 SIMPLE orders eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_orderkey 1 +select nation, o_year, sum(amount) as sum_profit +from (select n_name as nation, +extract(year from o_orderdate) as o_year, +l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount +from part, supplier, lineitem, partsupp, orders, nation +where s_suppkey = l_suppkey and ps_suppkey = l_suppkey +and ps_partkey = l_partkey and p_partkey = l_partkey +and o_orderkey = l_orderkey and s_nationkey = n_nationkey +and p_name like '%green%') as profit +group by nation, o_year +order by nation, o_year desc; +nation o_year sum_profit +ARGENTINA 1997 18247.873399999993 +ARGENTINA 1996 7731.089399999995 +ARGENTINA 1995 134490.5697 +ARGENTINA 1994 36767.101500000004 +ARGENTINA 1993 35857.08 +ARGENTINA 1992 35740 +ETHIOPIA 1998 2758.7801999999992 +ETHIOPIA 1997 19419.294599999997 +ETHIOPIA 1995 51231.87439999999 +ETHIOPIA 1994 3578.9478999999974 +ETHIOPIA 1992 1525.8234999999986 +IRAN 1998 37817.229600000006 +IRAN 1997 52643.77359999999 +IRAN 1996 70143.7761 +IRAN 1995 84094.58260000001 +IRAN 1994 18140.925599999995 +IRAN 1993 78655.1676 +IRAN 1992 87142.23960000002 +IRAQ 1998 22860.8082 +IRAQ 1997 93676.24359999999 +IRAQ 1996 45103.3242 +IRAQ 1994 36010.728599999995 +IRAQ 1993 33221.9399 +IRAQ 1992 47755.05900000001 +KENYA 1998 44194.831999999995 +KENYA 1997 57578.36259999999 +KENYA 1996 59195.90210000001 +KENYA 1995 79262.6278 +KENYA 1994 102360.66609999999 +KENYA 1993 128422.0196 +KENYA 1992 181517.2089 +MOROCCO 1998 41797.823199999984 +MOROCCO 1997 23685.801799999994 +MOROCCO 1996 62115.19579999998 +MOROCCO 1995 42442.64300000001 +MOROCCO 1994 48655.878000000004 +MOROCCO 1993 22926.744400000003 +MOROCCO 1992 32239.8088 +PERU 1998 86999.36459999997 +PERU 1997 121110.41070000001 +PERU 1996 177040.40759999995 +PERU 1995 122247.94520000002 +PERU 1994 88046.25329999998 +PERU 1993 49379.813799999996 +PERU 1992 80646.86050000001 +UNITED KINGDOM 1998 50577.25560000001 +UNITED KINGDOM 1997 114288.8605 +UNITED KINGDOM 1996 147684.46480000002 +UNITED KINGDOM 1995 225267.65759999998 +UNITED KINGDOM 1994 140595.5864 +UNITED KINGDOM 1993 322548.49210000003 +UNITED KINGDOM 1992 67747.88279999999 +UNITED STATES 1998 3957.0431999999996 +UNITED STATES 1997 94729.5704 +UNITED STATES 1996 79297.85670000002 +UNITED STATES 1995 62201.23360000001 +UNITED STATES 1994 43075.629899999985 +UNITED STATES 1993 27168.486199999996 +UNITED STATES 1992 34092.366 +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='extended_keys=on'; +EXPLAIN select o_orderkey, p_partkey +from part, lineitem, orders +where p_retailprice > 1100 and o_orderdate='1997-01-01' +and o_orderkey=l_orderkey and p_partkey=l_partkey; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE orders ref PRIMARY,i_o_orderdate i_o_orderdate 4 const 1 +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where +1 SIMPLE part eq_ref PRIMARY,i_p_retailprice PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where +select o_orderkey, p_partkey +from part, lineitem, orders +where p_retailprice > 1100 and o_orderdate='1997-01-01' +and o_orderkey=l_orderkey and p_partkey=l_partkey; +o_orderkey p_partkey +5895 200 +set optimizer_switch=@save_optimizer_switch; +DROP DATABASE dbt3_s001; +use test; +# +# Bug mdev-473: ANALYZE table locked for write +# +set use_stat_tables='complementary'; +create table t1 (i int); +lock table t1 write; +analyze table t1; +Table Op Msg_type Msg_text +test.t1 analyze status Table is already up to date +alter table t1 add column a varchar(8); +drop table t1; +# +# Bug mdev-487: memory leak in ANALYZE with stat tables +# +SET use_stat_tables = 'preferably'; +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2); +DELETE FROM t1 WHERE a=1; +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +DROP TABLE t1; +# +# Bug mdev-518: corrupted/missing statistical tables +# +CREATE TABLE t1 (i int) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(2); +FLUSH TABLE t1; +SET use_stat_tables='never'; +EXPLAIN SELECT * FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 2 +FLUSH TABLES; +SET use_stat_tables='preferably'; +EXPLAIN SELECT * FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 2 +DROP TABLE t1; +set use_stat_tables=@save_use_stat_tables; diff --git a/mysql-test/r/stat_tables_disabled.result b/mysql-test/r/stat_tables_disabled.result new file mode 100644 index 00000000000..f57abc34e0c --- /dev/null +++ b/mysql-test/r/stat_tables_disabled.result @@ -0,0 +1,70 @@ +SET SESSION STORAGE_ENGINE='InnoDB'; +select @@global.use_stat_tables; +@@global.use_stat_tables +NEVER +select @@session.use_stat_tables; +@@session.use_stat_tables +NEVER +set @save_use_stat_tables=@@use_stat_tables; +set use_stat_tables='preferably'; +DROP DATABASE IF EXISTS dbt3_s001; +CREATE DATABASE dbt3_s001; +use dbt3_s001; +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='extended_keys=off'; +# +# Bug mdev-503: optimizer ignores setting use_stat_tables='preferably' +# +flush tables +customer, lineitem, nation, orders, part, partsupp, region, supplier; +set use_stat_tables='never'; +EXPLAIN select sql_calc_found_rows straight_join +l_orderkey, sum(l_extendedprice*(1-l_discount)) as revenue, +o_orderdate, o_shippriority +from orders, customer, lineitem +where c_mktsegment = 'BUILDING' and c_custkey = o_custkey +and l_orderkey = o_orderkey and o_orderdate < date '1995-03-15' + and l_shipdate > date '1995-03-15' +group by l_orderkey, o_orderdate, o_shippriority +order by revenue desc, o_orderdate +limit 10; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE orders ALL PRIMARY,i_o_orderdate,i_o_custkey NULL NULL NULL # Using where; Using temporary; Using filesort +1 SIMPLE customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey # Using where +1 SIMPLE lineitem ref PRIMARY,i_l_shipdate,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey # Using where +set use_stat_tables='preferably'; +EXPLAIN select sql_calc_found_rows straight_join +l_orderkey, sum(l_extendedprice*(1-l_discount)) as revenue, +o_orderdate, o_shippriority +from orders, customer, lineitem +where c_mktsegment = 'BUILDING' and c_custkey = o_custkey +and l_orderkey = o_orderkey and o_orderdate < date '1995-03-15' + and l_shipdate > date '1995-03-15' +group by l_orderkey, o_orderdate, o_shippriority +order by revenue desc, o_orderdate +limit 10; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE orders ALL PRIMARY,i_o_orderdate,i_o_custkey NULL NULL NULL 1500 Using where; Using temporary; Using filesort +1 SIMPLE customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where +1 SIMPLE lineitem ref PRIMARY,i_l_shipdate,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where +flush tables customer, orders, lineitem; +EXPLAIN select sql_calc_found_rows straight_join +l_orderkey, sum(l_extendedprice*(1-l_discount)) as revenue, +o_orderdate, o_shippriority +from orders, customer, lineitem +where c_mktsegment = 'BUILDING' and c_custkey = o_custkey +and l_orderkey = o_orderkey and o_orderdate < date '1995-03-15' + and l_shipdate > date '1995-03-15' +group by l_orderkey, o_orderdate, o_shippriority +order by revenue desc, o_orderdate +limit 10; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE orders ALL PRIMARY,i_o_orderdate,i_o_custkey NULL NULL NULL 1500 Using where; Using temporary; Using filesort +1 SIMPLE customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where +1 SIMPLE lineitem ref PRIMARY,i_l_shipdate,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where +# End of the test case for mdev-503 +set optimizer_switch=@save_optimizer_switch; +DROP DATABASE dbt3_s001; +use test; +set use_stat_tables=@save_use_stat_tables; +SET SESSION STORAGE_ENGINE=DEFAULT; diff --git a/mysql-test/r/stat_tables_innodb.result b/mysql-test/r/stat_tables_innodb.result new file mode 100644 index 00000000000..51e1567309b --- /dev/null +++ b/mysql-test/r/stat_tables_innodb.result @@ -0,0 +1,412 @@ +SET SESSION STORAGE_ENGINE='InnoDB'; +set @save_optimizer_switch_for_stat_tables_test=@@optimizer_switch; +set optimizer_switch='extended_keys=on'; +select @@global.use_stat_tables; +@@global.use_stat_tables +COMPLEMENTARY +select @@session.use_stat_tables; +@@session.use_stat_tables +COMPLEMENTARY +set @save_use_stat_tables=@@use_stat_tables; +set use_stat_tables='preferably'; +DROP DATABASE IF EXISTS dbt3_s001; +CREATE DATABASE dbt3_s001; +use dbt3_s001; +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='extended_keys=off'; +select * from mysql.table_stats; +db_name table_name cardinality +dbt3_s001 customer 150 +dbt3_s001 lineitem 6005 +dbt3_s001 nation 25 +dbt3_s001 orders 1500 +dbt3_s001 part 200 +dbt3_s001 partsupp 700 +dbt3_s001 region 5 +dbt3_s001 supplier 10 +select * from mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 customer PRIMARY 1 1.0000 +dbt3_s001 customer i_c_nationkey 1 6.0000 +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 nation PRIMARY 1 1.0000 +dbt3_s001 nation i_n_regionkey 1 5.0000 +dbt3_s001 orders PRIMARY 1 1.0000 +dbt3_s001 orders i_o_orderdate 1 1.3321 +dbt3_s001 orders i_o_custkey 1 15.0000 +dbt3_s001 part PRIMARY 1 1.0000 +dbt3_s001 part i_p_retailprice 1 1.0000 +dbt3_s001 partsupp PRIMARY 1 3.5000 +dbt3_s001 partsupp PRIMARY 2 1.0000 +dbt3_s001 partsupp i_ps_partkey 1 3.5000 +dbt3_s001 partsupp i_ps_suppkey 1 70.0000 +dbt3_s001 region PRIMARY 1 1.0000 +dbt3_s001 supplier PRIMARY 1 1.0000 +dbt3_s001 supplier i_s_nationkey 1 1.1111 +set optimizer_switch=@save_optimizer_switch; +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='index_condition_pushdown=off'; +EXPLAIN select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue +from customer, orders, lineitem, supplier, nation, region +where c_custkey = o_custkey and l_orderkey = o_orderkey +and l_suppkey = s_suppkey and c_nationkey = s_nationkey +and s_nationkey = n_nationkey and n_regionkey = r_regionkey +and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01' + and o_orderdate < date '1995-01-01' + interval '1' year +group by n_name +order by revenue desc; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE orders range PRIMARY,i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 211 Using where; Using temporary; Using filesort +1 SIMPLE customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where +1 SIMPLE nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 +1 SIMPLE supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.customer.c_nationkey 1 Using index +1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join) +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where +select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue +from customer, orders, lineitem, supplier, nation, region +where c_custkey = o_custkey and l_orderkey = o_orderkey +and l_suppkey = s_suppkey and c_nationkey = s_nationkey +and s_nationkey = n_nationkey and n_regionkey = r_regionkey +and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01' + and o_orderdate < date '1995-01-01' + interval '1' year +group by n_name +order by revenue desc; +n_name revenue +PERU 321915.8715 +ARGENTINA 69817.1451 +set optimizer_switch=@save_optimizer_switch; +delete from mysql.index_stats; +select * from mysql.table_stats; +db_name table_name cardinality +dbt3_s001 customer 150 +dbt3_s001 lineitem 6005 +dbt3_s001 nation 25 +dbt3_s001 orders 1500 +dbt3_s001 part 200 +dbt3_s001 partsupp 700 +dbt3_s001 region 5 +dbt3_s001 supplier 10 +select * from mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 customer PRIMARY 1 1.0000 +dbt3_s001 customer i_c_nationkey 1 6.0000 +dbt3_s001 customer i_c_nationkey 2 1.0000 +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_shipdate 2 1.0149 +dbt3_s001 lineitem i_l_shipdate 3 1.0000 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_suppkey_partkey 3 1.0030 +dbt3_s001 lineitem i_l_suppkey_partkey 4 1.0000 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_partkey 2 1.0089 +dbt3_s001 lineitem i_l_partkey 3 1.0000 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey 2 1.2073 +dbt3_s001 lineitem i_l_suppkey 3 1.0000 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_receiptdate 2 1.0152 +dbt3_s001 lineitem i_l_receiptdate 3 1.0000 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey 2 1.0000 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_orderkey_quantity 3 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_commitdate 2 1.0364 +dbt3_s001 lineitem i_l_commitdate 3 1.0000 +dbt3_s001 nation PRIMARY 1 1.0000 +dbt3_s001 nation i_n_regionkey 1 5.0000 +dbt3_s001 nation i_n_regionkey 2 1.0000 +dbt3_s001 orders PRIMARY 1 1.0000 +dbt3_s001 orders i_o_orderdate 1 1.3321 +dbt3_s001 orders i_o_orderdate 2 1.0000 +dbt3_s001 orders i_o_custkey 1 15.0000 +dbt3_s001 orders i_o_custkey 2 1.0000 +dbt3_s001 part PRIMARY 1 1.0000 +dbt3_s001 part i_p_retailprice 1 1.0000 +dbt3_s001 part i_p_retailprice 2 1.0000 +dbt3_s001 partsupp PRIMARY 1 3.5000 +dbt3_s001 partsupp PRIMARY 2 1.0000 +dbt3_s001 partsupp i_ps_partkey 1 3.5000 +dbt3_s001 partsupp i_ps_partkey 2 1.0000 +dbt3_s001 partsupp i_ps_suppkey 1 70.0000 +dbt3_s001 partsupp i_ps_suppkey 2 1.0000 +dbt3_s001 region PRIMARY 1 1.0000 +dbt3_s001 supplier PRIMARY 1 1.0000 +dbt3_s001 supplier i_s_nationkey 1 1.1111 +dbt3_s001 supplier i_s_nationkey 2 1.0000 +select * from mysql.table_stats where table_name='orders'; +db_name table_name cardinality +dbt3_s001 orders 1500 +select * from mysql.index_stats where table_name='orders'; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 orders PRIMARY 1 1.0000 +dbt3_s001 orders i_o_orderdate 1 1.3321 +dbt3_s001 orders i_o_orderdate 2 1.0000 +dbt3_s001 orders i_o_custkey 1 15.0000 +dbt3_s001 orders i_o_custkey 2 1.0000 +select (select cardinality from mysql.table_stats where table_name='orders') / +(select avg_frequency from mysql.index_stats +where index_name='i_o_orderdate' and prefix_arity=1) as n_distinct; +n_distinct +1126.0416 +select count(distinct o_orderdate) from orders; +count(distinct o_orderdate) +1126 +select (select cardinality from mysql.table_stats where table_name='orders') / +(select avg_frequency from mysql.index_stats +where index_name='i_o_custkey' and prefix_arity=1) as n_distinct; +n_distinct +100.0000 +select count(distinct o_custkey) from orders; +count(distinct o_custkey) +100 +show index from orders; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment +orders 0 PRIMARY 1 o_orderkey A 1500 NULL NULL BTREE +orders 1 i_o_orderdate 1 o_orderDATE A 1126 NULL NULL YES BTREE +orders 1 i_o_custkey 1 o_custkey A 100 NULL NULL YES BTREE +select index_name, column_name, cardinality from information_schema.statistics +where table_name='orders'; +index_name column_name cardinality +PRIMARY o_orderkey 1500 +i_o_orderdate o_orderDATE 1126 +i_o_custkey o_custkey 100 +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='index_condition_pushdown=off'; +EXPLAIN select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue +from customer, orders, lineitem, supplier, nation, region +where c_custkey = o_custkey and l_orderkey = o_orderkey +and l_suppkey = s_suppkey and c_nationkey = s_nationkey +and s_nationkey = n_nationkey and n_regionkey = r_regionkey +and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01' + and o_orderdate < date '1995-01-01' + interval '1' year +group by n_name +order by revenue desc; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE orders range PRIMARY,i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 211 Using where; Using temporary; Using filesort +1 SIMPLE customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where +1 SIMPLE nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 +1 SIMPLE supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.customer.c_nationkey 1 Using index +1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join) +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where +select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue +from customer, orders, lineitem, supplier, nation, region +where c_custkey = o_custkey and l_orderkey = o_orderkey +and l_suppkey = s_suppkey and c_nationkey = s_nationkey +and s_nationkey = n_nationkey and n_regionkey = r_regionkey +and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01' + and o_orderdate < date '1995-01-01' + interval '1' year +group by n_name +order by revenue desc; +n_name revenue +PERU 321915.8715 +ARGENTINA 69817.1451 +set optimizer_switch=@save_optimizer_switch; +EXPLAIN select o_year, +sum(case when nation = 'UNITED STATES' then volume else 0 end) / +sum(volume) as mkt_share +from (select extract(year from o_orderdate) as o_year, +l_extendedprice * (1-l_discount) as volume, +n2.n_name as nation +from part, supplier, lineitem, orders, customer, +nation n1, nation n2, region +where p_partkey = l_partkey and s_suppkey = l_suppkey +and l_orderkey = o_orderkey and o_custkey = c_custkey +and c_nationkey = n1.n_nationkey and n1.n_regionkey = r_regionkey +and r_name = 'AMERICA' and s_nationkey = n2.n_nationkey +and o_orderdate between date '1995-01-01' and date '1996-12-31' + and p_type = 'STANDARD BRUSHED STEEL' ) as all_nations +group by o_year +order by o_year; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE orders ALL PRIMARY,i_o_orderdate,i_o_custkey NULL NULL NULL 1500 Using where; Using temporary; Using filesort +1 SIMPLE customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where +1 SIMPLE n1 eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 +1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join) +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where +1 SIMPLE part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where +1 SIMPLE supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where +1 SIMPLE n2 eq_ref PRIMARY PRIMARY 4 dbt3_s001.supplier.s_nationkey 1 +select o_year, +sum(case when nation = 'UNITED STATES' then volume else 0 end) / +sum(volume) as mkt_share +from (select extract(year from o_orderdate) as o_year, +l_extendedprice * (1-l_discount) as volume, +n2.n_name as nation +from part, supplier, lineitem, orders, customer, +nation n1, nation n2, region +where p_partkey = l_partkey and s_suppkey = l_suppkey +and l_orderkey = o_orderkey and o_custkey = c_custkey +and c_nationkey = n1.n_nationkey and n1.n_regionkey = r_regionkey +and r_name = 'AMERICA' and s_nationkey = n2.n_nationkey +and o_orderdate between date '1995-01-01' and date '1996-12-31' + and p_type = 'STANDARD BRUSHED STEEL' ) as all_nations +group by o_year +order by o_year; +o_year mkt_share +1995 0.4495521838895718 +1996 0.024585468215352495 +EXPLAIN select nation, o_year, sum(amount) as sum_profit +from (select n_name as nation, +extract(year from o_orderdate) as o_year, +l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount +from part, supplier, lineitem, partsupp, orders, nation +where s_suppkey = l_suppkey and ps_suppkey = l_suppkey +and ps_partkey = l_partkey and p_partkey = l_partkey +and o_orderkey = l_orderkey and s_nationkey = n_nationkey +and p_name like '%green%') as profit +group by nation, o_year +order by nation, o_year desc; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE supplier index PRIMARY,i_s_nationkey i_s_nationkey 5 NULL 10 Using where; Using index; Using temporary; Using filesort +1 SIMPLE nation eq_ref PRIMARY PRIMARY 4 dbt3_s001.supplier.s_nationkey 1 +1 SIMPLE partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 70 +1 SIMPLE part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey_partkey 10 dbt3_s001.partsupp.ps_partkey,dbt3_s001.supplier.s_suppkey 8 +1 SIMPLE orders eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_orderkey 1 +select nation, o_year, sum(amount) as sum_profit +from (select n_name as nation, +extract(year from o_orderdate) as o_year, +l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount +from part, supplier, lineitem, partsupp, orders, nation +where s_suppkey = l_suppkey and ps_suppkey = l_suppkey +and ps_partkey = l_partkey and p_partkey = l_partkey +and o_orderkey = l_orderkey and s_nationkey = n_nationkey +and p_name like '%green%') as profit +group by nation, o_year +order by nation, o_year desc; +nation o_year sum_profit +ARGENTINA 1997 18247.873399999993 +ARGENTINA 1996 7731.089399999995 +ARGENTINA 1995 134490.5697 +ARGENTINA 1994 36767.101500000004 +ARGENTINA 1993 35857.08 +ARGENTINA 1992 35740 +ETHIOPIA 1998 2758.7801999999992 +ETHIOPIA 1997 19419.294599999997 +ETHIOPIA 1995 51231.87439999999 +ETHIOPIA 1994 3578.9478999999974 +ETHIOPIA 1992 1525.8234999999986 +IRAN 1998 37817.229600000006 +IRAN 1997 52643.77359999999 +IRAN 1996 70143.7761 +IRAN 1995 84094.58260000001 +IRAN 1994 18140.925599999995 +IRAN 1993 78655.1676 +IRAN 1992 87142.23960000002 +IRAQ 1998 22860.8082 +IRAQ 1997 93676.24359999999 +IRAQ 1996 45103.3242 +IRAQ 1994 36010.728599999995 +IRAQ 1993 33221.9399 +IRAQ 1992 47755.05900000001 +KENYA 1998 44194.831999999995 +KENYA 1997 57578.36259999999 +KENYA 1996 59195.90210000001 +KENYA 1995 79262.6278 +KENYA 1994 102360.66609999999 +KENYA 1993 128422.0196 +KENYA 1992 181517.2089 +MOROCCO 1998 41797.823199999984 +MOROCCO 1997 23685.801799999994 +MOROCCO 1996 62115.19579999998 +MOROCCO 1995 42442.64300000001 +MOROCCO 1994 48655.878000000004 +MOROCCO 1993 22926.744400000003 +MOROCCO 1992 32239.8088 +PERU 1998 86999.36459999997 +PERU 1997 121110.41070000001 +PERU 1996 177040.40759999995 +PERU 1995 122247.94520000002 +PERU 1994 88046.25329999998 +PERU 1993 49379.813799999996 +PERU 1992 80646.86050000001 +UNITED KINGDOM 1998 50577.25560000001 +UNITED KINGDOM 1997 114288.8605 +UNITED KINGDOM 1996 147684.46480000002 +UNITED KINGDOM 1995 225267.65759999998 +UNITED KINGDOM 1994 140595.5864 +UNITED KINGDOM 1993 322548.49210000003 +UNITED KINGDOM 1992 67747.88279999999 +UNITED STATES 1998 3957.0431999999996 +UNITED STATES 1997 94729.5704 +UNITED STATES 1996 79297.85670000002 +UNITED STATES 1995 62201.23360000001 +UNITED STATES 1994 43075.629899999985 +UNITED STATES 1993 27168.486199999996 +UNITED STATES 1992 34092.366 +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='extended_keys=on'; +EXPLAIN select o_orderkey, p_partkey +from part, lineitem, orders +where p_retailprice > 1100 and o_orderdate='1997-01-01' +and o_orderkey=l_orderkey and p_partkey=l_partkey; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE part range PRIMARY,i_p_retailprice i_p_retailprice 9 NULL 1 Using where; Using index +1 SIMPLE orders ref PRIMARY,i_o_orderdate i_o_orderdate 4 const 1 Using index +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_orderkey,i_l_orderkey_quantity i_l_partkey 9 dbt3_s001.part.p_partkey,dbt3_s001.orders.o_orderkey 1 Using index +select o_orderkey, p_partkey +from part, lineitem, orders +where p_retailprice > 1100 and o_orderdate='1997-01-01' +and o_orderkey=l_orderkey and p_partkey=l_partkey; +o_orderkey p_partkey +5895 200 +set optimizer_switch=@save_optimizer_switch; +DROP DATABASE dbt3_s001; +use test; +# +# Bug mdev-473: ANALYZE table locked for write +# +set use_stat_tables='complementary'; +create table t1 (i int); +lock table t1 write; +analyze table t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +alter table t1 add column a varchar(8); +drop table t1; +# +# Bug mdev-487: memory leak in ANALYZE with stat tables +# +SET use_stat_tables = 'preferably'; +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2); +DELETE FROM t1 WHERE a=1; +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +DROP TABLE t1; +# +# Bug mdev-518: corrupted/missing statistical tables +# +CREATE TABLE t1 (i int) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(2); +FLUSH TABLE t1; +SET use_stat_tables='never'; +EXPLAIN SELECT * FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 2 +FLUSH TABLES; +SET use_stat_tables='preferably'; +EXPLAIN SELECT * FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 2 +DROP TABLE t1; +set use_stat_tables=@save_use_stat_tables; +set optimizer_switch=@save_optimizer_switch_for_stat_tables_test; +SET SESSION STORAGE_ENGINE=DEFAULT; diff --git a/mysql-test/r/stat_tables_par.result b/mysql-test/r/stat_tables_par.result new file mode 100644 index 00000000000..a98f934fa96 --- /dev/null +++ b/mysql-test/r/stat_tables_par.result @@ -0,0 +1,242 @@ +set @save_use_stat_tables=@@use_stat_tables; +set use_stat_tables='preferably'; +DROP DATABASE IF EXISTS dbt3_s001; +CREATE DATABASE dbt3_s001; +use dbt3_s001; +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='extended_keys=off'; +select * from mysql.table_stats; +db_name table_name cardinality +dbt3_s001 customer 150 +dbt3_s001 lineitem 6005 +dbt3_s001 nation 25 +dbt3_s001 orders 1500 +dbt3_s001 part 200 +dbt3_s001 partsupp 700 +dbt3_s001 region 5 +dbt3_s001 supplier 10 +select * from mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 customer PRIMARY 1 1.0000 +dbt3_s001 customer i_c_nationkey 1 6.0000 +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 nation PRIMARY 1 1.0000 +dbt3_s001 nation i_n_regionkey 1 5.0000 +dbt3_s001 orders PRIMARY 1 1.0000 +dbt3_s001 orders i_o_orderdate 1 1.3321 +dbt3_s001 orders i_o_custkey 1 15.0000 +dbt3_s001 part PRIMARY 1 1.0000 +dbt3_s001 partsupp PRIMARY 1 3.5000 +dbt3_s001 partsupp PRIMARY 2 1.0000 +dbt3_s001 partsupp i_ps_partkey 1 3.5000 +dbt3_s001 partsupp i_ps_suppkey 1 70.0000 +dbt3_s001 region PRIMARY 1 1.0000 +dbt3_s001 supplier PRIMARY 1 1.0000 +dbt3_s001 supplier i_s_nationkey 1 1.1111 +flush table lineitem; +set use_stat_tables='never'; +select sum(l_extendedprice*l_discount) as revenue +from lineitem +where l_shipdate >= date '1994-01-01' +and l_shipdate < date '1994-01-01' + interval '1' year +and l_discount between 0.06 - 0.01 and 0.06 + 0.01 +and l_quantity < 24; +revenue +77949.91860000002 +set debug_sync='statistics_mem_alloc_start1 WAIT_FOR second_thread_started_too'; +set debug_sync='statistics_mem_alloc_start2 SIGNAL first_thread_working'; +use dbt3_s001; +set use_stat_tables='preferably'; +select sum(l_extendedprice*l_discount) as revenue +from lineitem +where l_shipdate >= date '1994-01-01' +and l_shipdate < date '1994-01-01' + interval '1' year +and l_discount between 0.06 - 0.01 and 0.06 + 0.01 +and l_quantity < 24 ; +set debug_sync='statistics_mem_alloc_start1 SIGNAL second_thread_started_too'; +set debug_sync='statistics_mem_alloc_start2 WAIT_FOR first_thread_working'; +use dbt3_s001; +set use_stat_tables='preferably'; +select sum(l_extendedprice*l_discount) as revenue +from lineitem +where l_shipdate >= date '1994-01-01' +and l_shipdate < date '1994-01-01' + interval '1' year +and l_discount between 0.06 - 0.01 and 0.06 + 0.01 +and l_quantity < 24; +revenue +77949.91860000002 +revenue +77949.91860000002 +set use_stat_tables='preferably'; +set debug_sync='RESET'; +select * from mysql.index_stats where table_name='lineitem' order by index_name; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +delete from mysql.index_stats +where table_name='lineitem' and +index_name in ('i_l_shipdate', 'i_l_receiptdate'); +select * from mysql.index_stats where table_name='lineitem' order by index_name; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +analyze table lineitem persistent for columns() indexes (i_l_shipdate); +select * from mysql.index_stats where table_name='lineitem' order by index_name; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +delete from mysql.index_stats +where table_name='lineitem' and index_name= 'i_l_shipdate'; +select * from mysql.index_stats where table_name='lineitem' order by index_name; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +set debug_sync='statistics_collection_start1 WAIT_FOR second_thread_started_too'; +set debug_sync='statistics_collection_start2 SIGNAL first_thread_working'; +use dbt3_s001; +set use_stat_tables='preferably'; +analyze table lineitem persistent for columns() indexes (i_l_shipdate); +set debug_sync='statistics_collection_start1 SIGNAL second_thread_started_too'; +set debug_sync='statistics_collection_start2 WAIT_FOR first_thread_working'; +use dbt3_s001; +set use_stat_tables='preferably'; +analyze table lineitem persistent for columns() indexes (i_l_receiptdate); +set debug_sync='RESET'; +select * from mysql.index_stats where table_name='lineitem' order by index_name; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +select * from mysql.index_stats where table_name='lineitem' + order by index_name, prefix_arity; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +set debug_sync='RESET'; +set debug_sync='statistics_collection_start SIGNAL parked WAIT_FOR finish'; +use dbt3_s001; +set use_stat_tables='preferably'; +analyze table lineitem persistent for all; +set debug_sync='now WAIT_FOR parked'; +use dbt3_s001; +set use_stat_tables='never'; +set debug_sync='now SIGNAL finish'; +set debug_sync='RESET'; +select * from mysql.index_stats where table_name='lineitem' + order by index_name, prefix_arity; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +set @save_global_use_stat_tables=@@global.use_stat_tables; +set global use_stat_tables='preferably'; +set debug_sync='RESET'; +set debug_sync='statistics_update_start SIGNAL parker WAIT_FOR go1 EXECUTE 1'; +set debug_sync='thr_multi_lock_after_thr_lock SIGNAL go2 EXECUTE 2'; +use dbt3_s001; +analyze table lineitem persistent for all; +set debug_sync='open_and_process_table WAIT_FOR parker'; +set debug_sync='statistics_read_start SIGNAL go1 WAIT_FOR go2'; +use dbt3_s001; +select * from mysql.index_stats, lineitem where index_name= 'i_l_shipdate' and l_orderkey=1 and l_partkey=68; +db_name table_name index_name prefix_arity avg_frequency l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +dbt3_s001 lineitem i_l_shipdate 1 2.6500 1 68 9 2 36 34850.16 0.09 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +set debug_sync='RESET'; +set global use_stat_tables=@save_global_use_stat_tables; +DROP DATABASE dbt3_s001; +use test; +set @save_global_use_stat_tables=@@global.use_stat_tables; +set global use_stat_tables='preferably'; +set debug_sync='RESET'; +create table t1 (a int, b int, key(a)); +insert t1 values (1,1),(2,2); +analyze table t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +SET debug_sync='after_open_table_ignore_flush WAIT_FOR go'; +select * from information_schema.statistics where table_schema='test'; +select * from t1; +a b +1 1 +2 2 +SET DEBUG_SYNC= "now SIGNAL go"; +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 1 test a 1 a A 2 NULL NULL YES BTREE +set debug_sync='RESET'; +drop table t1; +set global use_stat_tables=@save_global_use_stat_tables; +set use_stat_tables=@save_use_stat_tables; diff --git a/mysql-test/r/stat_tables_par_innodb.result b/mysql-test/r/stat_tables_par_innodb.result new file mode 100644 index 00000000000..e0c00c482a0 --- /dev/null +++ b/mysql-test/r/stat_tables_par_innodb.result @@ -0,0 +1,253 @@ +SET SESSION STORAGE_ENGINE='InnoDB'; +set @save_optimizer_switch_for_stat_tables_test=@@optimizer_switch; +set optimizer_switch='extended_keys=on'; +set @save_use_stat_tables=@@use_stat_tables; +set use_stat_tables='preferably'; +DROP DATABASE IF EXISTS dbt3_s001; +CREATE DATABASE dbt3_s001; +use dbt3_s001; +set @save_optimizer_switch=@@optimizer_switch; +set optimizer_switch='extended_keys=off'; +select * from mysql.table_stats; +db_name table_name cardinality +dbt3_s001 customer 150 +dbt3_s001 lineitem 6005 +dbt3_s001 nation 25 +dbt3_s001 orders 1500 +dbt3_s001 part 200 +dbt3_s001 partsupp 700 +dbt3_s001 region 5 +dbt3_s001 supplier 10 +select * from mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 customer PRIMARY 1 1.0000 +dbt3_s001 customer i_c_nationkey 1 6.0000 +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 nation PRIMARY 1 1.0000 +dbt3_s001 nation i_n_regionkey 1 5.0000 +dbt3_s001 orders PRIMARY 1 1.0000 +dbt3_s001 orders i_o_orderdate 1 1.3321 +dbt3_s001 orders i_o_custkey 1 15.0000 +dbt3_s001 part PRIMARY 1 1.0000 +dbt3_s001 partsupp PRIMARY 1 3.5000 +dbt3_s001 partsupp PRIMARY 2 1.0000 +dbt3_s001 partsupp i_ps_partkey 1 3.5000 +dbt3_s001 partsupp i_ps_suppkey 1 70.0000 +dbt3_s001 region PRIMARY 1 1.0000 +dbt3_s001 supplier PRIMARY 1 1.0000 +dbt3_s001 supplier i_s_nationkey 1 1.1111 +flush table lineitem; +set use_stat_tables='never'; +select sum(l_extendedprice*l_discount) as revenue +from lineitem +where l_shipdate >= date '1994-01-01' +and l_shipdate < date '1994-01-01' + interval '1' year +and l_discount between 0.06 - 0.01 and 0.06 + 0.01 +and l_quantity < 24; +revenue +77949.91860000002 +set debug_sync='statistics_mem_alloc_start1 WAIT_FOR second_thread_started_too'; +set debug_sync='statistics_mem_alloc_start2 SIGNAL first_thread_working'; +use dbt3_s001; +set use_stat_tables='preferably'; +select sum(l_extendedprice*l_discount) as revenue +from lineitem +where l_shipdate >= date '1994-01-01' +and l_shipdate < date '1994-01-01' + interval '1' year +and l_discount between 0.06 - 0.01 and 0.06 + 0.01 +and l_quantity < 24 ; +set debug_sync='statistics_mem_alloc_start1 SIGNAL second_thread_started_too'; +set debug_sync='statistics_mem_alloc_start2 WAIT_FOR first_thread_working'; +use dbt3_s001; +set use_stat_tables='preferably'; +select sum(l_extendedprice*l_discount) as revenue +from lineitem +where l_shipdate >= date '1994-01-01' +and l_shipdate < date '1994-01-01' + interval '1' year +and l_discount between 0.06 - 0.01 and 0.06 + 0.01 +and l_quantity < 24; +revenue +77949.91860000002 +revenue +77949.91860000002 +set use_stat_tables='preferably'; +set debug_sync='RESET'; +select * from mysql.index_stats where table_name='lineitem' order by index_name; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +delete from mysql.index_stats +where table_name='lineitem' and +index_name in ('i_l_shipdate', 'i_l_receiptdate'); +select * from mysql.index_stats where table_name='lineitem' order by index_name; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +analyze table lineitem persistent for columns() indexes (i_l_shipdate); +select * from mysql.index_stats where table_name='lineitem' order by index_name; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +delete from mysql.index_stats +where table_name='lineitem' and index_name= 'i_l_shipdate'; +select * from mysql.index_stats where table_name='lineitem' order by index_name; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +set debug_sync='statistics_collection_start1 WAIT_FOR second_thread_started_too'; +set debug_sync='statistics_collection_start2 SIGNAL first_thread_working'; +use dbt3_s001; +set use_stat_tables='preferably'; +analyze table lineitem persistent for columns() indexes (i_l_shipdate); +set debug_sync='statistics_collection_start1 SIGNAL second_thread_started_too'; +set debug_sync='statistics_collection_start2 WAIT_FOR first_thread_working'; +use dbt3_s001; +set use_stat_tables='preferably'; +analyze table lineitem persistent for columns() indexes (i_l_receiptdate); +set debug_sync='RESET'; +select * from mysql.index_stats where table_name='lineitem' order by index_name; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +select * from mysql.index_stats where table_name='lineitem' + order by index_name, prefix_arity; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0033 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7160 +dbt3_s001 lineitem i_l_orderkey 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0250 +dbt3_s001 lineitem i_l_receiptdate 1 2.6477 +dbt3_s001 lineitem i_l_shipdate 1 2.6500 +dbt3_s001 lineitem i_l_suppkey 1 600.5000 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786 +set debug_sync='RESET'; +set debug_sync='statistics_collection_start SIGNAL parked WAIT_FOR finish'; +use dbt3_s001; +set use_stat_tables='preferably'; +analyze table lineitem persistent for all; +set debug_sync='now WAIT_FOR parked'; +use dbt3_s001; +set use_stat_tables='never'; +select * from lineitem where l_orderkey=1 and l_partkey=156; +l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +1 156 4 1 17 17954.55 0.04 0.02 N O 1996-03-13 1996-02-12 1996-03-22 DELIVER IN PERSON TRUCK blithely regular ideas caj +delete from lineitem where l_orderkey=1 and l_partkey=156; +select * from lineitem where l_orderkey=1 and l_partkey=156; +l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +set debug_sync='now SIGNAL finish'; +set debug_sync='RESET'; +select * from mysql.index_stats where table_name='lineitem' + order by index_name, prefix_arity; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 lineitem PRIMARY 1 4.0027 +dbt3_s001 lineitem PRIMARY 2 1.0000 +dbt3_s001 lineitem i_l_commitdate 1 2.7155 +dbt3_s001 lineitem i_l_orderkey 1 4.0027 +dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0027 +dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404 +dbt3_s001 lineitem i_l_partkey 1 30.0200 +dbt3_s001 lineitem i_l_receiptdate 1 2.6473 +dbt3_s001 lineitem i_l_shipdate 1 2.6496 +dbt3_s001 lineitem i_l_suppkey 1 600.4000 +dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0200 +dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5771 +set @save_global_use_stat_tables=@@global.use_stat_tables; +set global use_stat_tables='preferably'; +set debug_sync='RESET'; +set debug_sync='statistics_update_start SIGNAL parker WAIT_FOR go1 EXECUTE 1'; +set debug_sync='thr_multi_lock_after_thr_lock SIGNAL go2 EXECUTE 2'; +use dbt3_s001; +analyze table lineitem persistent for all; +set debug_sync='open_and_process_table WAIT_FOR parker'; +set debug_sync='statistics_read_start SIGNAL go1 WAIT_FOR go2'; +use dbt3_s001; +select * from mysql.index_stats, lineitem where index_name= 'i_l_shipdate' and l_orderkey=1 and l_partkey=68; +db_name table_name index_name prefix_arity avg_frequency l_orderkey l_partkey l_suppkey l_linenumber l_quantity l_extendedprice l_discount l_tax l_returnflag l_linestatus l_shipDATE l_commitDATE l_receiptDATE l_shipinstruct l_shipmode l_comment +dbt3_s001 lineitem i_l_shipdate 1 2.6496 1 68 9 2 36 34850.16 0.09 0.06 N O 1996-04-12 1996-02-28 1996-04-20 TAKE BACK RETURN MAIL slyly bold pinto beans detect s +set debug_sync='RESET'; +set global use_stat_tables=@save_global_use_stat_tables; +DROP DATABASE dbt3_s001; +use test; +set @save_global_use_stat_tables=@@global.use_stat_tables; +set global use_stat_tables='preferably'; +set debug_sync='RESET'; +create table t1 (a int, b int, key(a)); +insert t1 values (1,1),(2,2); +analyze table t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +SET debug_sync='after_open_table_ignore_flush WAIT_FOR go'; +select * from information_schema.statistics where table_schema='test'; +select * from t1; +a b +1 1 +2 2 +SET DEBUG_SYNC= "now SIGNAL go"; +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 1 test a 1 a A 2 NULL NULL YES BTREE +set debug_sync='RESET'; +drop table t1; +set global use_stat_tables=@save_global_use_stat_tables; +set use_stat_tables=@save_use_stat_tables; +set optimizer_switch=@save_optimizer_switch_for_stat_tables_test; +SET SESSION STORAGE_ENGINE=DEFAULT; diff --git a/mysql-test/r/stat_tables_partition.result b/mysql-test/r/stat_tables_partition.result new file mode 100644 index 00000000000..75454b61fe0 --- /dev/null +++ b/mysql-test/r/stat_tables_partition.result @@ -0,0 +1,12 @@ +# +# Bug mdev-3866: valgrind complain from ANALYZE on a table with BIT field +# +SET use_stat_tables = 'preferably'; +CREATE TABLE t1 (pk int PRIMARY KEY, a bit(1), INDEX idx(a) +) ENGINE=MyISAM PARTITION BY KEY(pk) PARTITIONS 2; +INSERT INTO t1 VALUES (1,1),(2,0),(3,0),(4,1); +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +SET use_stat_tables = DEFAULT; +DROP TABLE t1; diff --git a/mysql-test/r/stat_tables_rbr.result b/mysql-test/r/stat_tables_rbr.result new file mode 100644 index 00000000000..9e236f424bf --- /dev/null +++ b/mysql-test/r/stat_tables_rbr.result @@ -0,0 +1,26 @@ +# +# Bug mdev-463: assertion failure when running ANALYZE with RBR on +# +SET GLOBAL use_stat_tables = PREFERABLY; +CREATE TABLE t1 (i INT) ENGINE=InnoDB; +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +DROP TABLE t1; +SET GLOBAL use_stat_tables = DEFAULT; +SET use_stat_tables = PREFERABLY; +CREATE TABLE t1 ( a INT ) ENGINE=MyISAM PARTITION BY HASH(a) PARTITIONS 2; +ALTER TABLE t1 ANALYZE PARTITION p1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +SHOW BINLOG EVENTS; +Log_name Pos Event_type Server_id End_log_pos Info +master-bin.000001 4 Format_desc 1 246 Server ver: #, Binlog ver: # +master-bin.000001 246 Binlog_checkpoint 1 286 master-bin.000001 +master-bin.000001 286 Query 1 386 use `test`; CREATE TABLE t1 (i INT) ENGINE=InnoDB +master-bin.000001 386 Query 1 465 use `test`; ANALYZE TABLE t1 +master-bin.000001 465 Query 1 569 use `test`; DROP TABLE `t1` /* generated by server */ +master-bin.000001 569 Query 1 705 use `test`; CREATE TABLE t1 ( a INT ) ENGINE=MyISAM PARTITION BY HASH(a) PARTITIONS 2 +master-bin.000001 705 Query 1 803 use `test`; ALTER TABLE t1 ANALYZE PARTITION p1 +SET use_stat_tables = DEFAULT; +DROP TABLE t1; diff --git a/mysql-test/r/stat_tables_repl.result b/mysql-test/r/stat_tables_repl.result new file mode 100644 index 00000000000..370b8c59db1 --- /dev/null +++ b/mysql-test/r/stat_tables_repl.result @@ -0,0 +1,41 @@ +include/master-slave.inc +[connection master] +# +# Bug mdev-485: unexpected failure with replication of DROP/ALTER table +# when RBR is on +# +CREATE TABLE t1 ( a int, b int ) ENGINE=MyISAM; +INSERT INTO t1 VALUES (2,20), (1,20), (3,30); +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +DROP TABLE t1; +CREATE TABLE t1 ( a int, b int, INDEX idx1(b) ) ENGINE=MyISAM; +INSERT INTO t1 VALUES (2,20), (1,20), (3,30); +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +DROP INDEX idx1 ON t1; +DROP TABLE t1; +CREATE TABLE t1 ( a int, b int, INDEX idx1(b) ) ENGINE=MyISAM; +INSERT INTO t1 VALUES (2,20), (1,20), (3,30); +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +ALTER TABLE t1 DROP COLUMN b; +DROP TABLE t1; +CREATE TABLE t1 ( a int, b int, INDEX idx1(b) ) ENGINE=MyISAM; +INSERT INTO t1 VALUES (2,20), (1,20), (3,30); +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +ALTER TABLE t1 RENAME to s; +DROP TABLE s; +CREATE TABLE t1 ( a int, b int, INDEX idx1(b) ) ENGINE=MyISAM; +INSERT INTO t1 VALUES (2,20), (1,20), (3,30); +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +ALTER TABLE t1 CHANGE COLUMN b c int ; +DROP TABLE t1; +include/rpl_end.inc diff --git a/mysql-test/r/statistics.result b/mysql-test/r/statistics.result new file mode 100644 index 00000000000..ba0390f98db --- /dev/null +++ b/mysql-test/r/statistics.result @@ -0,0 +1,1429 @@ +drop table if exists t1,t2; +set @save_use_stat_tables=@@use_stat_tables; +DELETE FROM mysql.table_stats; +DELETE FROM mysql.column_stats; +DELETE FROM mysql.index_stats; +set use_stat_tables='preferably'; +CREATE TABLE t1 ( +a int NOT NULL PRIMARY KEY, +b varchar(32), +c char(16), +d date, +e double, +f bit(3), +INDEX idx1 (b, e), +INDEX idx2 (c, d), +INDEX idx3 (d), +INDEX idx4 (e, b, d) +) ENGINE= MYISAM; +INSERT INTO t1 VALUES +(0, NULL, NULL, NULL, NULL, NULL), +(7, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'dddddddd', '1990-05-15', 0.1, b'100'), +(17, 'vvvvvvvvvvvvv', 'aaaa', '1989-03-12', 0.01, b'101'), +(1, 'vvvvvvvvvvvvv', NULL, '1989-03-12', 0.01, b'100'), +(12, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd', '1999-07-23', 0.112, b'001'), +(23, 'vvvvvvvvvvvvv', 'dddddddd', '1999-07-23', 0.1, b'100'), +(8, 'vvvvvvvvvvvvv', 'aaaa', '1999-07-23', 0.1, b'100'), +(22, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'aaaa', '1989-03-12', 0.112, b'001'), +(31, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'aaaa', '1999-07-23', 0.01, b'001'), +(10, NULL, 'aaaa', NULL, 0.01, b'010'), +(5, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd', '1999-07-23', 0.1, b'100'), +(15, 'vvvvvvvvvvvvv', 'ccccccccc', '1990-05-15', 0.1, b'010'), +(30, NULL, 'bbbbbb', NULL, NULL, b'100'), +(38, 'zzzzzzzzzzzzzzzzzz', 'bbbbbb', NULL, NULL, NULL), +(18, 'zzzzzzzzzzzzzzzzzz', 'ccccccccc', '1990-05-15', 0.01, b'010'), +(9, 'yyy', 'bbbbbb', '1998-08-28', 0.01, NULL), +(29, 'vvvvvvvvvvvvv', 'dddddddd', '1999-07-23', 0.012, b'010'), +(3, 'yyy', 'dddddddd', '1990-05-15', 0.112, b'010'), +(39, 'zzzzzzzzzzzzzzzzzz', 'bbbbbb', NULL, 0.01, b'100'), +(14, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'ccccccccc', '1990-05-15', 0.1, b'100'), +(40, 'zzzzzzzzzzzzzzzzzz', 'bbbbbb', '1989-03-12', NULL, NULL), +(44, NULL, 'aaaa', '1989-03-12', NULL, b'010'), +(19, 'vvvvvvvvvvvvv', 'ccccccccc', '1990-05-15', 0.012, b'011'), +(21, 'zzzzzzzzzzzzzzzzzz', 'dddddddd', '1989-03-12', 0.112, b'100'), +(45, NULL, NULL, '1989-03-12', NULL, b'011'), +(2, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'ccccccccc', '1990-05-15', 0.1, b'001'), +(35, 'yyy', 'aaaa', '1990-05-15', 0.05, b'011'), +(4, 'vvvvvvvvvvvvv', 'dddddddd', '1999-07-23', 0.01, b'101'), +(47, NULL, 'aaaa', '1990-05-15', 0.05, b'010'), +(42, NULL, 'ccccccccc', '1989-03-12', 0.01, b'010'), +(32, NULL, 'bbbbbb', '1990-05-15', 0.01, b'011'), +(49, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww' , 'aaaa', '1990-05-15', NULL, NULL), +(43, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww' , 'bbbbbb', '1990-05-15', NULL, b'100'), +(37, 'yyy', NULL, '1989-03-12', 0.05, b'011'), +(41, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'ccccccccc', '1990-05-15', 0.05, NULL), +(34, 'yyy', NULL, NULL, NULL, NULL), +(33, 'zzzzzzzzzzzzzzzzzz', 'dddddddd', '1989-03-12', 0.05, b'011'), +(24, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd', '1990-05-15', 0.01, b'101'), +(11, 'yyy', 'ccccccccc', '1999-07-23', 0.1, NULL), +(25, 'zzzzzzzzzzzzzzzzzz', 'bbb', '1989-03-12', 0.01, b'101'); +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t1 40 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx1 1 6.4000 +test t1 idx1 2 1.6875 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +SELECT COUNT(*) FROM t1; +COUNT(*) +40 +SELECT * FROM mysql.column_stats +WHERE db_name='test' AND table_name='t1' AND column_name='a'; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +SELECT MIN(t1.a), MAX(t1.a), +(SELECT COUNT(*) FROM t1 WHERE t1.b IS NULL) / +(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.a)", +(SELECT COUNT(t1.a) FROM t1) / +(SELECT COUNT(DISTINCT t1.a) FROM t1) AS "AVG_FREQUENCY(t1.a)" +FROM t1; +MIN(t1.a) MAX(t1.a) NULLS_RATIO(t1.a) AVG_FREQUENCY(t1.a) +0 49 0.2000 1.0000 +SELECT * FROM mysql.column_stats +WHERE db_name='test' AND table_name='t1' AND column_name='b'; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +SELECT MIN(t1.b), MAX(t1.b), +(SELECT COUNT(*) FROM t1 WHERE t1.b IS NULL) / +(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.b)", +(SELECT COUNT(t1.b) FROM t1) / +(SELECT COUNT(DISTINCT t1.b) FROM t1) AS "AVG_FREQUENCY(t1.b)" +FROM t1; +MIN(t1.b) MAX(t1.b) NULLS_RATIO(t1.b) AVG_FREQUENCY(t1.b) +vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 6.4000 +SELECT * FROM mysql.column_stats +WHERE db_name='test' AND table_name='t1' AND column_name='c'; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +SELECT MIN(t1.c), MAX(t1.c), +(SELECT COUNT(*) FROM t1 WHERE t1.c IS NULL) / +(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.c)", +(SELECT COUNT(t1.c) FROM t1) / +(SELECT COUNT(DISTINCT t1.c) FROM t1) AS "AVG_FREQUENCY(t1.c)" +FROM t1; +MIN(t1.c) MAX(t1.c) NULLS_RATIO(t1.c) AVG_FREQUENCY(t1.c) +aaaa dddddddd 0.1250 7.0000 +SELECT * FROM mysql.column_stats +WHERE db_name='test' AND table_name='t1' AND column_name='d'; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +SELECT MIN(t1.d), MAX(t1.d), +(SELECT COUNT(*) FROM t1 WHERE t1.d IS NULL) / +(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.d)", +(SELECT COUNT(t1.d) FROM t1) / +(SELECT COUNT(DISTINCT t1.d) FROM t1) AS "AVG_FREQUENCY(t1.d)" +FROM t1; +MIN(t1.d) MAX(t1.d) NULLS_RATIO(t1.d) AVG_FREQUENCY(t1.d) +1989-03-12 1999-07-23 0.1500 8.5000 +SELECT * FROM mysql.column_stats +WHERE db_name='test' AND table_name='t1' AND column_name='e'; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +SELECT MIN(t1.e), MAX(t1.e), +(SELECT COUNT(*) FROM t1 WHERE t1.e IS NULL) / +(SELECT COUNT(*) FROM t1) AS "NULLS_RATIO(t1.e)", +(SELECT COUNT(t1.e) FROM t1) / +(SELECT COUNT(DISTINCT t1.e) FROM t1) AS "AVG_FREQUENCY(t1.e)" +FROM t1; +MIN(t1.e) MAX(t1.e) NULLS_RATIO(t1.e) AVG_FREQUENCY(t1.e) +0.01 0.112 0.2250 6.2000 +SELECT * FROM mysql.index_stats +WHERE db_name='test' AND table_name='t1' AND index_name='idx1'; +db_name table_name index_name prefix_arity avg_frequency +test t1 idx1 1 6.4000 +test t1 idx1 2 1.6875 +SELECT +(SELECT COUNT(*) FROM t1 WHERE t1.b IS NOT NULL) / +(SELECT COUNT(DISTINCT t1.b) FROM t1 WHERE t1.b IS NOT NULL) +AS 'ARITY 1', +(SELECT COUNT(*) FROM t1 WHERE t1.b IS NOT NULL AND t1.e IS NOT NULL) / +(SELECT COUNT(DISTINCT t1.b, t1.e) FROM t1 +WHERE t1.b IS NOT NULL AND t1.e IS NOT NULL) +AS 'ARITY 2'; +ARITY 1 ARITY 2 +6.4000 1.6875 +SELECT * FROM mysql.index_stats +WHERE db_name='test' AND table_name='t1' AND index_name='idx2'; +db_name table_name index_name prefix_arity avg_frequency +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +SELECT +(SELECT COUNT(*) FROM t1 WHERE t1.c IS NOT NULL) / +(SELECT COUNT(DISTINCT t1.c) FROM t1 WHERE t1.c IS NOT NULL) +AS 'ARITY 1', +(SELECT COUNT(*) FROM t1 WHERE t1.c IS NOT NULL AND t1.d IS NOT NULL) / +(SELECT COUNT(DISTINCT t1.c, t1.d) FROM t1 +WHERE t1.c IS NOT NULL AND t1.d IS NOT NULL) +AS 'ARITY 2'; +ARITY 1 ARITY 2 +7.0000 2.3846 +SELECT * FROM mysql.index_stats +WHERE db_name='test' AND table_name='t1' AND index_name='idx3'; +db_name table_name index_name prefix_arity avg_frequency +test t1 idx3 1 8.5000 +SELECT +(SELECT COUNT(*) FROM t1 WHERE t1.d IS NOT NULL) / +(SELECT COUNT(DISTINCT t1.d) FROM t1 WHERE t1.d IS NOT NULL) +AS 'ARITY 1'; +ARITY 1 +8.5000 +SELECT * FROM mysql.index_stats +WHERE db_name='test' AND table_name='t1' AND index_name='idx4'; +db_name table_name index_name prefix_arity avg_frequency +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +SELECT +(SELECT COUNT(*) FROM t1 WHERE t1.e IS NOT NULL) / +(SELECT COUNT(DISTINCT t1.e) FROM t1 WHERE t1.e IS NOT NULL) +AS 'ARITY 1', +(SELECT COUNT(*) FROM t1 WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL) / +(SELECT COUNT(DISTINCT t1.e, t1.b) FROM t1 +WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL) +AS 'ARITY 2', +(SELECT COUNT(*) FROM t1 +WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL AND t1.d IS NOT NULL) / +(SELECT COUNT(DISTINCT t1.e, t1.b, t1.d) FROM t1 +WHERE t1.e IS NOT NULL AND t1.b IS NOT NULL AND t1.d IS NOT NULL) +AS 'ARITY 3'; +ARITY 1 ARITY 2 ARITY 3 +6.2000 1.6875 1.1304 +CREATE TABLE t3 ( +a int NOT NULL PRIMARY KEY, +b varchar(32), +c char(16), +INDEX idx (c) +) ENGINE=MYISAM; +INSERT INTO t3 VALUES +(0, NULL, NULL), +(7, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'dddddddd'), +(17, 'vvvvvvvvvvvvv', 'aaaa'), +(1, 'vvvvvvvvvvvvv', NULL), +(12, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd'), +(23, 'vvvvvvvvvvvvv', 'dddddddd'), +(8, 'vvvvvvvvvvvvv', 'aaaa'), +(22, 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'aaaa'), +(31, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'aaaa'), +(10, NULL, 'aaaa'), +(5, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww', 'dddddddd'), +(15, 'vvvvvvvvvvvvv', 'ccccccccc'), +(30, NULL, 'bbbbbb'), +(38, 'zzzzzzzzzzzzzzzzzz', 'bbbbbb'), +(18, 'zzzzzzzzzzzzzzzzzz', 'ccccccccc'), +(9, 'yyy', 'bbbbbb'), +(29, 'vvvvvvvvvvvvv', 'dddddddd'); +ANALYZE TABLE t3; +Table Op Msg_type Msg_text +test.t3 analyze status OK +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t1 40 +test t3 17 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +test t3 a 0 38 0.0000 4.0000 1.0000 +test t3 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.1765 18.0714 2.8000 +test t3 c aaaa dddddddd 0.1176 6.4000 3.7500 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx1 1 6.4000 +test t1 idx1 2 1.6875 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +test t3 PRIMARY 1 1.0000 +test t3 idx 1 3.7500 +ALTER TABLE t1 RENAME TO s1; +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test s1 40 +test t3 17 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test s1 a 0 49 0.0000 4.0000 1.0000 +test s1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test s1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test s1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test s1 e 0.01 0.112 0.2250 8.0000 6.2000 +test s1 f 1 5 0.2000 1.0000 6.4000 +test t3 a 0 38 0.0000 4.0000 1.0000 +test t3 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.1765 18.0714 2.8000 +test t3 c aaaa dddddddd 0.1176 6.4000 3.7500 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test s1 PRIMARY 1 1.0000 +test s1 idx1 1 6.4000 +test s1 idx1 2 1.6875 +test s1 idx2 1 7.0000 +test s1 idx2 2 2.3846 +test s1 idx3 1 8.5000 +test s1 idx4 1 6.2000 +test s1 idx4 2 1.6875 +test s1 idx4 3 1.1304 +test t3 PRIMARY 1 1.0000 +test t3 idx 1 3.7500 +RENAME TABLE s1 TO t1; +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t1 40 +test t3 17 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +test t3 a 0 38 0.0000 4.0000 1.0000 +test t3 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.1765 18.0714 2.8000 +test t3 c aaaa dddddddd 0.1176 6.4000 3.7500 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx1 1 6.4000 +test t1 idx1 2 1.6875 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +test t3 PRIMARY 1 1.0000 +test t3 idx 1 3.7500 +DROP TABLE t3; +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t1 40 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx1 1 6.4000 +test t1 idx1 2 1.6875 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +CREATE TEMPORARY TABLE t0 ( +a int NOT NULL PRIMARY KEY, +b varchar(32) +); +INSERT INTO t0 SELECT a,b FROM t1; +ALTER TABLE t1 CHANGE COLUMN b x varchar(32), +CHANGE COLUMN e y double; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `x` varchar(32) DEFAULT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `y` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx1` (`x`,`y`), + KEY `idx2` (`c`,`d`), + KEY `idx3` (`d`), + KEY `idx4` (`y`,`x`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 x vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 y 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +ALTER TABLE t1 CHANGE COLUMN x b varchar(32), +CHANGE COLUMN y e double; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `b` varchar(32) DEFAULT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx1` (`b`,`e`), + KEY `idx2` (`c`,`d`), + KEY `idx3` (`d`), + KEY `idx4` (`e`,`b`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +ALTER TABLE t1 RENAME TO s1, CHANGE COLUMN b x varchar(32); +SHOW CREATE TABLE s1; +Table Create Table +s1 CREATE TABLE `s1` ( + `a` int(11) NOT NULL, + `x` varchar(32) DEFAULT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx1` (`x`,`e`), + KEY `idx2` (`c`,`d`), + KEY `idx3` (`d`), + KEY `idx4` (`e`,`x`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test s1 40 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test s1 a 0 49 0.0000 4.0000 1.0000 +test s1 x vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test s1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test s1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test s1 e 0.01 0.112 0.2250 8.0000 6.2000 +test s1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test s1 PRIMARY 1 1.0000 +test s1 idx1 1 6.4000 +test s1 idx1 2 1.6875 +test s1 idx2 1 7.0000 +test s1 idx2 2 2.3846 +test s1 idx3 1 8.5000 +test s1 idx4 1 6.2000 +test s1 idx4 2 1.6875 +test s1 idx4 3 1.1304 +ALTER TABLE s1 RENAME TO t1, CHANGE COLUMN x b varchar(32); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `b` varchar(32) DEFAULT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx1` (`b`,`e`), + KEY `idx2` (`c`,`d`), + KEY `idx3` (`d`), + KEY `idx4` (`e`,`b`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t1 40 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx1 1 6.4000 +test t1 idx1 2 1.6875 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +ALTER TABLE t1 CHANGE COLUMN b x varchar(30); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `x` varchar(30) DEFAULT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx1` (`x`,`e`), + KEY `idx2` (`c`,`d`), + KEY `idx3` (`d`), + KEY `idx4` (`e`,`x`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +ALTER TABLE t1 CHANGE COLUMN x b varchar(32); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `b` varchar(32) DEFAULT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx1` (`b`,`e`), + KEY `idx2` (`c`,`d`), + KEY `idx3` (`d`), + KEY `idx4` (`e`,`b`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +ANALYZE TABLE t1 PERSISTENT FOR COLUMNS(b) INDEXES(idx1, idx4); +Table Op Msg_type Msg_text +test.t1 analyze status OK +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx4 3 1.1304 +test t1 idx4 2 1.6875 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +test t1 idx4 1 6.2000 +test t1 idx1 2 1.6875 +test t1 idx1 1 6.4000 +SELECT * INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/save_column_stats' + FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' + FROM mysql.column_stats WHERE column_name='b'; +SELECT * INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/save_index_stats' + FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' + FROM mysql.index_stats WHERE index_name IN ('idx1', 'idx4'); +ALTER TABLE t1 CHANGE COLUMN b x varchar(30); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `x` varchar(30) DEFAULT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx1` (`x`,`e`), + KEY `idx2` (`c`,`d`), + KEY `idx3` (`d`), + KEY `idx4` (`e`,`x`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +ALTER TABLE t1 CHANGE COLUMN x b varchar(32); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `b` varchar(32) DEFAULT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx1` (`b`,`e`), + KEY `idx2` (`c`,`d`), + KEY `idx3` (`d`), + KEY `idx4` (`e`,`b`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/save_column_stats' + INTO TABLE mysql.column_stats +FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'; +LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/save_index_stats' + INTO TABLE mysql.index_stats +FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'; +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx4 3 1.1304 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +test t1 idx4 2 1.6875 +test t1 idx4 1 6.2000 +test t1 idx1 2 1.6875 +test t1 idx1 1 6.4000 +ALTER TABLE t1 DROP COLUMN b; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx1` (`e`), + KEY `idx2` (`c`,`d`), + KEY `idx3` (`d`), + KEY `idx4` (`e`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx3 1 8.5000 +DROP INDEX idx2 ON t1; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx1` (`e`), + KEY `idx3` (`d`), + KEY `idx4` (`e`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx3 1 8.5000 +DROP INDEX idx1 ON t1; +DROP INDEX idx4 ON t1; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx3` (`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +ALTER TABLE t1 ADD COLUMN b varchar(32); +CREATE INDEX idx1 ON t1(b, e); +CREATE INDEX idx2 ON t1(c, d); +CREATE INDEX idx4 ON t1(e, b, d); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + `b` varchar(32) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx3` (`d`), + KEY `idx1` (`b`,`e`), + KEY `idx2` (`c`,`d`), + KEY `idx4` (`e`,`b`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx3 1 8.5000 +ANALYZE TABLE t1 PERSISTENT FOR COLUMNS(b) INDEXES(idx1, idx2, idx4); +Table Op Msg_type Msg_text +test.t1 analyze status OK +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b NULL NULL 1.0000 NULL NULL +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx2 1 7.0000 +test t1 idx1 2 NULL +test t1 idx1 1 NULL +test t1 idx3 1 8.5000 +test t1 idx2 2 2.3846 +test t1 idx4 1 6.2000 +test t1 idx4 2 NULL +test t1 idx4 3 NULL +UPDATE t1 SET b=(SELECT b FROM t0 WHERE t0.a= t1.a); +ANALYZE TABLE t1 PERSISTENT FOR COLUMNS(b) INDEXES(idx1, idx2, idx4); +Table Op Msg_type Msg_text +test.t1 analyze status OK +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx2 1 7.0000 +test t1 idx1 2 1.6875 +test t1 idx1 1 6.4000 +test t1 idx3 1 8.5000 +test t1 idx2 2 2.3846 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +ALTER TABLE t1 DROP COLUMN b, +DROP INDEX idx1, DROP INDEX idx2, DROP INDEX idx4; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx3` (`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx3 1 8.5000 +ALTER TABLE t1 ADD COLUMN b varchar(32); +ALTER TABLE t1 +ADD INDEX idx1 (b, e), ADD INDEX idx2 (c, d), ADD INDEX idx4 (e, b, d); +UPDATE t1 SET b=(SELECT b FROM t0 WHERE t0.a= t1.a); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) NOT NULL, + `c` char(16) DEFAULT NULL, + `d` date DEFAULT NULL, + `e` double DEFAULT NULL, + `f` bit(3) DEFAULT NULL, + `b` varchar(32) DEFAULT NULL, + PRIMARY KEY (`a`), + KEY `idx3` (`d`), + KEY `idx1` (`b`,`e`), + KEY `idx2` (`c`,`d`), + KEY `idx4` (`e`,`b`,`d`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx3 1 8.5000 +ANALYZE TABLE t1 PERSISTENT FOR COLUMNS(b) INDEXES(idx1, idx2, idx4); +Table Op Msg_type Msg_text +test.t1 analyze status OK +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx3 1 8.5000 +test t1 idx2 2 2.3846 +test t1 idx2 1 7.0000 +test t1 idx1 2 1.6875 +test t1 idx1 1 6.4000 +test t1 idx4 3 1.1304 +DELETE FROM mysql.table_stats; +DELETE FROM mysql.column_stats; +DELETE FROM mysql.index_stats; +ANALYZE TABLE t1 PERSISTENT FOR COLUMNS() INDEXES(); +Table Op Msg_type Msg_text +test.t1 analyze status Table is already up to date +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t1 40 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +ANALYZE TABLE t1 PERSISTENT FOR COLUMNS(c,e,b) INDEXES(idx2,idx4); +Table Op Msg_type Msg_text +test.t1 analyze status Table is already up to date +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t1 40 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +DELETE FROM mysql.index_stats WHERE table_name='t1' AND index_name='primary'; +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +ANALYZE TABLE t1 PERSISTENT FOR COLUMNS() INDEXES(primary); +Table Op Msg_type Msg_text +test.t1 analyze status Table is already up to date +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +test t1 PRIMARY 1 1.0000 +DELETE FROM mysql.table_stats; +DELETE FROM mysql.column_stats; +DELETE FROM mysql.index_stats; +ANALYZE TABLE t1 PERSISTENT FOR COLUMNS ALL INDEXES ALL; +Table Op Msg_type Msg_text +test.t1 analyze status Table is already up to date +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t1 40 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx3 1 8.5000 +test t1 idx1 1 6.4000 +test t1 idx1 2 1.6875 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t1 idx4 3 1.1304 +CREATE TABLE t2 LIKE t1; +ALTER TABLE t2 ENGINE=InnoDB; +INSERT INTO t2 SELECT * FROM t1; +set optimizer_switch='extended_keys=off'; +ANALYZE TABLE t2; +Table Op Msg_type Msg_text +test.t2 analyze status OK +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t1 40 +test t2 40 +SELECT * FROM mysql.column_stats ORDER BY column_name; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t2 a 0 49 0.0000 4.0000 1.0000 +test t1 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t2 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t2 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t2 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t2 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +test t2 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t2 PRIMARY 1 1.0000 +test t1 idx1 1 6.4000 +test t2 idx1 1 6.4000 +test t1 idx1 2 1.6875 +test t2 idx1 2 1.6875 +test t1 idx2 1 7.0000 +test t2 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t2 idx2 2 2.3846 +test t1 idx3 1 8.5000 +test t2 idx3 1 8.5000 +test t1 idx4 1 6.2000 +test t2 idx4 1 6.2000 +test t1 idx4 2 1.6875 +test t2 idx4 2 1.6875 +test t1 idx4 3 1.1304 +test t2 idx4 3 1.1304 +DELETE FROM mysql.table_stats; +DELETE FROM mysql.column_stats; +DELETE FROM mysql.index_stats; +set optimizer_switch='extended_keys=on'; +ANALYZE TABLE t2; +Table Op Msg_type Msg_text +test.t2 analyze status OK +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t2 40 +SELECT * FROM mysql.column_stats ORDER BY column_name; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t2 a 0 49 0.0000 4.0000 1.0000 +test t2 b vvvvvvvvvvvvv zzzzzzzzzzzzzzzzzz 0.2000 17.1250 6.4000 +test t2 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t2 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t2 e 0.01 0.112 0.2250 8.0000 6.2000 +test t2 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t2 PRIMARY 1 1.0000 +test t2 idx1 1 6.4000 +test t2 idx1 2 1.6875 +test t2 idx1 3 1.0000 +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx2 3 1.0000 +test t2 idx3 1 8.5000 +test t2 idx3 2 1.0000 +test t2 idx4 1 6.2000 +test t2 idx4 2 1.6875 +test t2 idx4 3 1.1304 +test t2 idx4 4 1.0000 +ALTER TABLE t2 DROP PRIMARY KEY, DROP INDEX idx1; +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx3 1 8.5000 +test t2 idx4 1 6.2000 +test t2 idx4 2 1.6875 +test t2 idx4 3 1.1304 +UPDATE t2 SET b=0 WHERE b IS NULL; +ALTER TABLE t2 ADD PRIMARY KEY (a,b); +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx3 1 8.5000 +test t2 idx4 1 6.2000 +test t2 idx4 2 1.6875 +test t2 idx4 3 1.1304 +ANALYZE TABLE t2 PERSISTENT FOR COLUMNS() INDEXES ALL; +Table Op Msg_type Msg_text +test.t2 analyze status OK +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t2 PRIMARY 1 1.0000 +test t2 PRIMARY 2 1.0000 +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx2 3 1.0000 +test t2 idx2 4 1.0000 +test t2 idx3 1 8.5000 +test t2 idx3 2 1.0000 +test t2 idx3 3 1.0000 +test t2 idx4 1 6.2000 +test t2 idx4 2 1.7222 +test t2 idx4 3 1.1154 +test t2 idx4 4 1.0000 +ALTER TABLE t2 CHANGE COLUMN b b varchar(30); +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx3 1 8.5000 +ANALYZE TABLE t2 PERSISTENT FOR COLUMNS ALL INDEXES ALL; +Table Op Msg_type Msg_text +test.t2 analyze status OK +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t2 PRIMARY 1 1.0000 +test t2 PRIMARY 2 1.0000 +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx2 3 1.0000 +test t2 idx2 4 1.0000 +test t2 idx3 1 8.5000 +test t2 idx3 2 1.0000 +test t2 idx3 3 1.0000 +test t2 idx4 1 6.2000 +test t2 idx4 2 1.7222 +test t2 idx4 3 1.1154 +test t2 idx4 4 1.0000 +ALTER TABLE t2 CHANGE COLUMN b b varchar(32); +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx3 1 8.5000 +ANALYZE TABLE t2 PERSISTENT FOR COLUMNS ALL INDEXES ALL; +Table Op Msg_type Msg_text +test.t2 analyze status OK +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t2 PRIMARY 1 1.0000 +test t2 PRIMARY 2 1.0000 +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx2 3 1.0000 +test t2 idx2 4 1.0000 +test t2 idx3 1 8.5000 +test t2 idx3 2 1.0000 +test t2 idx3 3 1.0000 +test t2 idx4 1 6.2000 +test t2 idx4 2 1.7222 +test t2 idx4 3 1.1154 +test t2 idx4 4 1.0000 +ALTER TABLE t2 DROP COLUMN b; +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx3 1 8.5000 +ANALYZE TABLE t2 PERSISTENT FOR COLUMNS() INDEXES ALL; +Table Op Msg_type Msg_text +test.t2 analyze status OK +SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; +db_name table_name index_name prefix_arity avg_frequency +test t2 PRIMARY 1 1.0000 +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx2 3 1.0000 +test t2 idx3 1 8.5000 +test t2 idx3 2 1.0000 +test t2 idx4 1 6.2000 +test t2 idx4 2 2.2308 +test t2 idx4 3 1.0000 +set optimizer_switch='extended_keys=off'; +ALTER TABLE t1 +DROP INDEX idx1, +DROP INDEX idx4; +ALTER TABLE t1 +MODIFY COLUMN b text, +ADD INDEX idx1 (b(4), e), +ADD INDEX idx4 (e, b(4), d); +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t2 a 0 49 0.0000 4.0000 1.0000 +test t2 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t2 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t2 e 0.01 0.112 0.2250 8.0000 6.2000 +test t2 f 1 5 0.2000 1.0000 6.4000 +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t2 idx3 1 8.5000 +test t2 idx3 2 1.0000 +test t2 idx2 3 1.0000 +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx4 3 1.0000 +test t2 idx4 1 6.2000 +test t2 idx4 2 2.2308 +test t2 PRIMARY 1 1.0000 +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t2 a 0 49 0.0000 4.0000 1.0000 +test t2 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t2 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t2 e 0.01 0.112 0.2250 8.0000 6.2000 +test t2 f 1 5 0.2000 1.0000 6.4000 +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +test t1 b NULL NULL 0.2000 17.1250 NULL +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t2 idx3 1 8.5000 +test t2 idx3 2 1.0000 +test t2 idx2 3 1.0000 +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t1 idx2 1 7.0000 +test t1 idx3 1 8.5000 +test t1 PRIMARY 1 1.0000 +test t2 idx4 3 1.0000 +test t2 idx4 1 6.2000 +test t2 idx4 2 2.2308 +test t2 PRIMARY 1 1.0000 +test t1 idx2 2 2.3846 +test t1 idx1 1 NULL +test t1 idx1 2 NULL +test t1 idx4 1 6.2000 +test t1 idx4 2 NULL +test t1 idx4 3 NULL +DELETE FROM mysql.table_stats; +DELETE FROM mysql.column_stats; +DELETE FROM mysql.index_stats; +ANALYZE TABLE mysql.column_stats PERSISTENT FOR ALL; +Table Op Msg_type Msg_text +mysql.column_stats analyze error Invalid argument +ANALYZE TABLE mysql.column_stats; +Table Op Msg_type Msg_text +mysql.column_stats analyze status OK +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +set use_stat_tables='never'; +ANALYZE TABLE t1 PERSISTENT FOR ALL; +Table Op Msg_type Msg_text +test.t1 analyze status Table is already up to date +SELECT * FROM mysql.table_stats; +db_name table_name cardinality +test t1 40 +SELECT * FROM mysql.column_stats; +db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency +test t1 a 0 49 0.0000 4.0000 1.0000 +test t1 c aaaa dddddddd 0.1250 6.6571 7.0000 +test t1 d 1989-03-12 1999-07-23 0.1500 3.0000 8.5000 +test t1 e 0.01 0.112 0.2250 8.0000 6.2000 +test t1 f 1 5 0.2000 1.0000 6.4000 +test t1 b NULL NULL 0.2000 17.1250 NULL +SELECT * FROM mysql.index_stats; +db_name table_name index_name prefix_arity avg_frequency +test t1 PRIMARY 1 1.0000 +test t1 idx3 1 8.5000 +test t1 idx2 1 7.0000 +test t1 idx2 2 2.3846 +test t1 idx1 1 NULL +test t1 idx1 2 NULL +test t1 idx4 1 6.2000 +test t1 idx4 2 NULL +test t1 idx4 3 NULL +DELETE FROM mysql.table_stats; +DELETE FROM mysql.column_stats; +DELETE FROM mysql.index_stats; +DROP TABLE t1,t2; +set names utf8; +CREATE DATABASE world; +use world; +CREATE TABLE Country ( +Code char(3) NOT NULL default '', +Name char(52) NOT NULL default '', +SurfaceArea float(10,2) NOT NULL default '0.00', +Population int(11) NOT NULL default '0', +Capital int(11) default NULL, +PRIMARY KEY (Code), +UNIQUE INDEX (Name) +) CHARACTER SET utf8 COLLATE utf8_bin; +CREATE TABLE City ( +ID int(11) NOT NULL auto_increment, +Name char(35) NOT NULL default '', +Country char(3) NOT NULL default '', +Population int(11) NOT NULL default '0', +PRIMARY KEY (ID), +INDEX (Population), +INDEX (Country) +) CHARACTER SET utf8 COLLATE utf8_bin; +CREATE TABLE CountryLanguage ( +Country char(3) NOT NULL default '', +Language char(30) NOT NULL default '', +Percentage float(3,1) NOT NULL default '0.0', +PRIMARY KEY (Country, Language), +INDEX (Percentage) +) CHARACTER SET utf8 COLLATE utf8_bin; +set use_stat_tables='preferably'; +ANALYZE TABLE Country, City, CountryLanguage; +SELECT UPPER(db_name), UPPER(table_name), cardinality +FROM mysql.table_stats; +UPPER(db_name) UPPER(table_name) cardinality +WORLD COUNTRY 239 +WORLD CITY 4079 +WORLD COUNTRYLANGUAGE 984 +SELECT UPPER(db_name), UPPER(table_name), +column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency +FROM mysql.column_stats; +UPPER(db_name) UPPER(table_name) column_name min_value max_value nulls_ratio avg_length avg_frequency +WORLD COUNTRY Code ABW ZWE 0.0000 3.0000 1.0000 +WORLD COUNTRY Name Afghanistan Zimbabwe 0.0000 10.1088 1.0000 +WORLD COUNTRY SurfaceArea 0.40 17075400.00 0.0000 4.0000 1.0042 +WORLD COUNTRY Population 0 1277558000 0.0000 4.0000 1.0575 +WORLD COUNTRY Capital 1 4074 0.0293 4.0000 1.0000 +WORLD CITY ID 1 4079 0.0000 4.0000 1.0000 +WORLD CITY Name A Coruña (La Coruña) Ãœrgenc 0.0000 8.6416 1.0195 +WORLD CITY Country ABW ZWE 0.0000 3.0000 17.5819 +WORLD CITY Population 42 10500000 0.0000 4.0000 1.0467 +WORLD COUNTRYLANGUAGE Country ABW ZWE 0.0000 3.0000 4.2232 +WORLD COUNTRYLANGUAGE Language Abhyasi [South]Mande 0.0000 7.1778 2.1532 +WORLD COUNTRYLANGUAGE Percentage 0.0 99.9 0.0000 4.0000 2.7640 +SELECT UPPER(db_name), UPPER(table_name), +index_name, prefix_arity, avg_frequency +FROM mysql.index_stats; +UPPER(db_name) UPPER(table_name) index_name prefix_arity avg_frequency +WORLD COUNTRY PRIMARY 1 1.0000 +WORLD COUNTRY Name 1 1.0000 +WORLD CITY PRIMARY 1 1.0000 +WORLD CITY Population 1 1.0467 +WORLD CITY Country 1 17.5819 +WORLD COUNTRYLANGUAGE PRIMARY 1 4.2232 +WORLD COUNTRYLANGUAGE PRIMARY 2 1.0000 +WORLD COUNTRYLANGUAGE Percentage 1 2.7640 +use test; +set use_stat_tables='never'; +CREATE DATABASE world_innodb; +use world_innodb; +CREATE TABLE Country ( +Code char(3) NOT NULL default '', +Name char(52) NOT NULL default '', +SurfaceArea float(10,2) NOT NULL default '0.00', +Population int(11) NOT NULL default '0', +Capital int(11) default NULL, +PRIMARY KEY (Code), +UNIQUE INDEX (Name) +) CHARACTER SET utf8 COLLATE utf8_bin; +CREATE TABLE City ( +ID int(11) NOT NULL auto_increment, +Name char(35) NOT NULL default '', +Country char(3) NOT NULL default '', +Population int(11) NOT NULL default '0', +PRIMARY KEY (ID), +INDEX (Population), +INDEX (Country) +) CHARACTER SET utf8 COLLATE utf8_bin; +CREATE TABLE CountryLanguage ( +Country char(3) NOT NULL default '', +Language char(30) NOT NULL default '', +Percentage float(3,1) NOT NULL default '0.0', +PRIMARY KEY (Country, Language), +INDEX (Percentage) +) CHARACTER SET utf8 COLLATE utf8_bin; +ALTER TABLE Country ENGINE=InnoDB; +ALTER TABLE City ENGINE=InnoDB; +ALTER TABLE CountryLanguage ENGINE=InnoDB; +set use_stat_tables='preferably'; +ANALYZE TABLE Country, City, CountryLanguage; +SELECT UPPER(db_name), UPPER(table_name), cardinality +FROM mysql.table_stats; +UPPER(db_name) UPPER(table_name) cardinality +WORLD COUNTRY 239 +WORLD CITY 4079 +WORLD COUNTRYLANGUAGE 984 +WORLD_INNODB COUNTRY 239 +WORLD_INNODB CITY 4079 +WORLD_INNODB COUNTRYLANGUAGE 984 +SELECT UPPER(db_name), UPPER(table_name), +column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency +FROM mysql.column_stats; +UPPER(db_name) UPPER(table_name) column_name min_value max_value nulls_ratio avg_length avg_frequency +WORLD COUNTRY Code ABW ZWE 0.0000 3.0000 1.0000 +WORLD COUNTRY Name Afghanistan Zimbabwe 0.0000 10.1088 1.0000 +WORLD COUNTRY SurfaceArea 0.40 17075400.00 0.0000 4.0000 1.0042 +WORLD COUNTRY Population 0 1277558000 0.0000 4.0000 1.0575 +WORLD COUNTRY Capital 1 4074 0.0293 4.0000 1.0000 +WORLD CITY ID 1 4079 0.0000 4.0000 1.0000 +WORLD CITY Name A Coruña (La Coruña) Ãœrgenc 0.0000 8.6416 1.0195 +WORLD CITY Country ABW ZWE 0.0000 3.0000 17.5819 +WORLD CITY Population 42 10500000 0.0000 4.0000 1.0467 +WORLD COUNTRYLANGUAGE Country ABW ZWE 0.0000 3.0000 4.2232 +WORLD COUNTRYLANGUAGE Language Abhyasi [South]Mande 0.0000 7.1778 2.1532 +WORLD COUNTRYLANGUAGE Percentage 0.0 99.9 0.0000 4.0000 2.7640 +WORLD_INNODB COUNTRY Code ABW ZWE 0.0000 3.0000 1.0000 +WORLD_INNODB COUNTRY Name Afghanistan Zimbabwe 0.0000 10.1088 1.0000 +WORLD_INNODB COUNTRY SurfaceArea 0.40 17075400.00 0.0000 4.0000 1.0042 +WORLD_INNODB COUNTRY Population 0 1277558000 0.0000 4.0000 1.0575 +WORLD_INNODB COUNTRY Capital 1 4074 0.0293 4.0000 1.0000 +WORLD_INNODB CITY ID 1 4079 0.0000 4.0000 1.0000 +WORLD_INNODB CITY Name A Coruña (La Coruña) Ãœrgenc 0.0000 8.6416 1.0195 +WORLD_INNODB CITY Country ABW ZWE 0.0000 3.0000 17.5819 +WORLD_INNODB CITY Population 42 10500000 0.0000 4.0000 1.0467 +WORLD_INNODB COUNTRYLANGUAGE Country ABW ZWE 0.0000 3.0000 4.2232 +WORLD_INNODB COUNTRYLANGUAGE Language Abhyasi [South]Mande 0.0000 7.1778 2.1532 +WORLD_INNODB COUNTRYLANGUAGE Percentage 0.0 99.9 0.0000 4.0000 2.7640 +SELECT UPPER(db_name), UPPER(table_name), +index_name, prefix_arity, avg_frequency +FROM mysql.index_stats; +UPPER(db_name) UPPER(table_name) index_name prefix_arity avg_frequency +WORLD COUNTRY PRIMARY 1 1.0000 +WORLD COUNTRY Name 1 1.0000 +WORLD CITY PRIMARY 1 1.0000 +WORLD CITY Population 1 1.0467 +WORLD CITY Country 1 17.5819 +WORLD COUNTRYLANGUAGE PRIMARY 1 4.2232 +WORLD COUNTRYLANGUAGE PRIMARY 2 1.0000 +WORLD COUNTRYLANGUAGE Percentage 1 2.7640 +WORLD_INNODB COUNTRY PRIMARY 1 1.0000 +WORLD_INNODB COUNTRY Name 1 1.0000 +WORLD_INNODB CITY PRIMARY 1 1.0000 +WORLD_INNODB CITY Population 1 1.0467 +WORLD_INNODB CITY Country 1 17.5819 +WORLD_INNODB COUNTRYLANGUAGE PRIMARY 1 4.2232 +WORLD_INNODB COUNTRYLANGUAGE PRIMARY 2 1.0000 +WORLD_INNODB COUNTRYLANGUAGE Percentage 1 2.7640 +use test; +DROP DATABASE world; +SELECT UPPER(db_name), UPPER(table_name), cardinality +FROM mysql.table_stats; +UPPER(db_name) UPPER(table_name) cardinality +WORLD_INNODB COUNTRY 239 +WORLD_INNODB CITY 4079 +WORLD_INNODB COUNTRYLANGUAGE 984 +SELECT UPPER(db_name), UPPER(table_name), +column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency +FROM mysql.column_stats; +UPPER(db_name) UPPER(table_name) column_name min_value max_value nulls_ratio avg_length avg_frequency +WORLD_INNODB COUNTRY Code ABW ZWE 0.0000 3.0000 1.0000 +WORLD_INNODB COUNTRY Name Afghanistan Zimbabwe 0.0000 10.1088 1.0000 +WORLD_INNODB COUNTRY SurfaceArea 0.40 17075400.00 0.0000 4.0000 1.0042 +WORLD_INNODB COUNTRY Population 0 1277558000 0.0000 4.0000 1.0575 +WORLD_INNODB COUNTRY Capital 1 4074 0.0293 4.0000 1.0000 +WORLD_INNODB CITY ID 1 4079 0.0000 4.0000 1.0000 +WORLD_INNODB CITY Name A Coruña (La Coruña) Ãœrgenc 0.0000 8.6416 1.0195 +WORLD_INNODB CITY Country ABW ZWE 0.0000 3.0000 17.5819 +WORLD_INNODB CITY Population 42 10500000 0.0000 4.0000 1.0467 +WORLD_INNODB COUNTRYLANGUAGE Country ABW ZWE 0.0000 3.0000 4.2232 +WORLD_INNODB COUNTRYLANGUAGE Language Abhyasi [South]Mande 0.0000 7.1778 2.1532 +WORLD_INNODB COUNTRYLANGUAGE Percentage 0.0 99.9 0.0000 4.0000 2.7640 +SELECT UPPER(db_name), UPPER(table_name), +index_name, prefix_arity, avg_frequency +FROM mysql.index_stats; +UPPER(db_name) UPPER(table_name) index_name prefix_arity avg_frequency +WORLD_INNODB COUNTRY PRIMARY 1 1.0000 +WORLD_INNODB COUNTRY Name 1 1.0000 +WORLD_INNODB CITY PRIMARY 1 1.0000 +WORLD_INNODB CITY Population 1 1.0467 +WORLD_INNODB CITY Country 1 17.5819 +WORLD_INNODB COUNTRYLANGUAGE PRIMARY 1 4.2232 +WORLD_INNODB COUNTRYLANGUAGE PRIMARY 2 1.0000 +WORLD_INNODB COUNTRYLANGUAGE Percentage 1 2.7640 +DROP DATABASE world_innodb; +SELECT UPPER(db_name), UPPER(table_name), cardinality +FROM mysql.table_stats; +UPPER(db_name) UPPER(table_name) cardinality +SELECT UPPER(db_name), UPPER(table_name), +column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency +FROM mysql.column_stats; +UPPER(db_name) UPPER(table_name) column_name min_value max_value nulls_ratio avg_length avg_frequency +SELECT UPPER(db_name), UPPER(table_name), +index_name, prefix_arity, avg_frequency +FROM mysql.index_stats; +UPPER(db_name) UPPER(table_name) index_name prefix_arity avg_frequency +DELETE FROM mysql.table_stats; +DELETE FROM mysql.column_stats; +DELETE FROM mysql.index_stats; +set use_stat_tables=@save_use_stat_tables; diff --git a/mysql-test/r/status_user.result b/mysql-test/r/status_user.result index f43e217b8a5..040b2d85a51 100644 --- a/mysql-test/r/status_user.result +++ b/mysql-test/r/status_user.result @@ -107,7 +107,7 @@ Handler_mrr_key_refills 0 Handler_mrr_rowid_refills 0 Handler_prepare 18 Handler_read_first 0 -Handler_read_key 3 +Handler_read_key 9 Handler_read_last 0 Handler_read_next 0 Handler_read_prev 0 @@ -123,7 +123,7 @@ Handler_update 5 Handler_write 7 select variable_value - @global_read_key as "handler_read_key" from information_schema.global_status where variable_name="handler_read_key"; handler_read_key -3 +9 set @@global.userstat=0; select * from information_schema.index_statistics; TABLE_SCHEMA TABLE_NAME INDEX_NAME ROWS_READ diff --git a/mysql-test/r/str_to_datetime_457.result b/mysql-test/r/str_to_datetime_457.result new file mode 100644 index 00000000000..630be9ffbaa --- /dev/null +++ b/mysql-test/r/str_to_datetime_457.result @@ -0,0 +1,51 @@ +select cast('01:02:03 ' as time), cast('01:02:03 ' as time); +cast('01:02:03 ' as time) cast('01:02:03 ' as time) +01:02:03 00:00:00 +select cast('2002-011-012' as date), cast('2002.11.12' as date), cast('2002.011.012' as date); +cast('2002-011-012' as date) cast('2002.11.12' as date) cast('2002.011.012' as date) +2002-11-12 2002-11-12 2002-11-12 +select cast('2012103123595912' as datetime(6)), cast('20121031235959123' as datetime(6)); +cast('2012103123595912' as datetime(6)) cast('20121031235959123' as datetime(6)) +2012-10-31 23:59:59.000000 2012-10-31 23:59:59.000000 +Warnings: +Warning 1292 Truncated incorrect datetime value: '2012103123595912' +Warning 1292 Truncated incorrect datetime value: '20121031235959123' +select cast(0 as date), cast('0000-00-00' as date), cast('0' as date); +cast(0 as date) cast('0000-00-00' as date) cast('0' as date) +0000-00-00 0000-00-00 0000-00-00 +Warnings: +Warning 1292 Incorrect datetime value: '0' +select extract(hour from '100000:02:03'), extract(hour from '100000:02:03 '); +extract(hour from '100000:02:03') extract(hour from '100000:02:03 ') +0 0 +Warnings: +Warning 1292 Truncated incorrect time value: '100000:02:03' +Warning 1292 Truncated incorrect time value: '100000:02:03 ' +# +# backward compatibility craziness +# +select cast('12:00:00.12.34.56' as time); +cast('12:00:00.12.34.56' as time) +12:00:00 +Warnings: +Warning 1292 Truncated incorrect time value: '12:00:00.12.34.56' +select cast('12:00:00 12.34.56' as time); +cast('12:00:00 12.34.56' as time) +12:34:56 +select cast('12:00:00-12.34.56' as time); +cast('12:00:00-12.34.56' as time) +12:00:00 +Warnings: +Warning 1292 Truncated incorrect time value: '12:00:00-12.34.56' +select cast('12:00:00.12.34.56' as datetime); +cast('12:00:00.12.34.56' as datetime) +2012-00-00 12:34:56 +select cast('12:00:00-12.34.56' as datetime); +cast('12:00:00-12.34.56' as datetime) +2012-00-00 12:34:56 +select cast('12:00:00 12.34.56' as datetime); +cast('12:00:00 12.34.56' as datetime) +2012-00-00 12:34:56 +select cast('12:00:00.123456' as time); +cast('12:00:00.123456' as time) +12:00:00 diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 1977fbaccee..96a20a3ea27 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -2966,7 +2966,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where Warnings: -Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1` +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)))) and trigcond(trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1` explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 @@ -2979,7 +2979,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where Warnings: -Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1` +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)))) and trigcond(trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1` DROP TABLE t1,t2; set optimizer_switch=@tmp11867_optimizer_switch; CREATE TABLE t1 (a char(5), b char(5)); diff --git a/mysql-test/r/subselect3.result b/mysql-test/r/subselect3.result index 487ffe655c1..1a33fb0c6a3 100644 --- a/mysql-test/r/subselect3.result +++ b/mysql-test/r/subselect3.result @@ -252,7 +252,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 2 100.00 Using where; Full scan on NULL key Warnings: Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`oref` AS `oref`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a checking NULL where ((`test`.`t1`.`c` = `test`.`t2`.`oref`) and trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`) or isnull(`test`.`t1`.`a`))) and trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`b`) or isnull(`test`.`t1`.`b`)))) having (trigcond(<is_not_null_test>(`test`.`t1`.`a`)) and trigcond(<is_not_null_test>(`test`.`t1`.`b`))))))) AS `Z` from `test`.`t2` +Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`oref` AS `oref`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a checking NULL where ((`test`.`t1`.`c` = `test`.`t2`.`oref`) and trigcond(trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`) or isnull(`test`.`t1`.`a`)))) and trigcond(trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`b`) or isnull(`test`.`t1`.`b`))))) having (trigcond(<is_not_null_test>(`test`.`t1`.`a`)) and trigcond(<is_not_null_test>(`test`.`t1`.`b`))))))) AS `Z` from `test`.`t2` select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2; a b oref Z NULL 1 100 0 @@ -269,7 +269,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t4 ALL NULL NULL NULL NULL 100 100.00 Using where; Using join buffer (flat, BNL join) Warnings: Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`oref` AS `oref`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(select `test`.`t1`.`a`,`test`.`t1`.`b` from `test`.`t1` join `test`.`t4` where ((`test`.`t1`.`c` = `test`.`t2`.`oref`) and trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`) or isnull(`test`.`t1`.`a`))) and trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`b`) or isnull(`test`.`t1`.`b`)))) having (trigcond(<is_not_null_test>(`test`.`t1`.`a`)) and trigcond(<is_not_null_test>(`test`.`t1`.`b`)))))) AS `Z` from `test`.`t2` +Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`oref` AS `oref`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(select `test`.`t1`.`a`,`test`.`t1`.`b` from `test`.`t1` join `test`.`t4` where ((`test`.`t1`.`c` = `test`.`t2`.`oref`) and trigcond(trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`) or isnull(`test`.`t1`.`a`)))) and trigcond(trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`b`) or isnull(`test`.`t1`.`b`))))) having (trigcond(<is_not_null_test>(`test`.`t1`.`a`)) and trigcond(<is_not_null_test>(`test`.`t1`.`b`)))))) AS `Z` from `test`.`t2` select a,b, oref, (a,b) in (select a,b from t1,t4 where c=t2.oref) Z from t2; @@ -314,7 +314,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 index_subquery idx idx 5 func 4 100.00 Using where; Full scan on NULL key Warnings: Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t2`.`oref` AS `oref`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on idx checking NULL where ((`test`.`t1`.`oref` = `test`.`t2`.`oref`) and trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`ie1`) or isnull(`test`.`t1`.`ie1`))) and trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`ie2`) or isnull(`test`.`t1`.`ie2`)))) having (trigcond(<is_not_null_test>(`test`.`t1`.`ie1`)) and trigcond(<is_not_null_test>(`test`.`t1`.`ie2`))))))) AS `Z` from `test`.`t2` where ((`test`.`t2`.`a` = 10) and (`test`.`t2`.`b` = 10)) +Note 1003 select `test`.`t2`.`oref` AS `oref`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on idx checking NULL where ((`test`.`t1`.`oref` = `test`.`t2`.`oref`) and trigcond(trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`ie1`) or isnull(`test`.`t1`.`ie1`)))) and trigcond(trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`ie2`) or isnull(`test`.`t1`.`ie2`))))) having (trigcond(<is_not_null_test>(`test`.`t1`.`ie1`)) and trigcond(<is_not_null_test>(`test`.`t1`.`ie2`))))))) AS `Z` from `test`.`t2` where ((`test`.`t2`.`a` = 10) and (`test`.`t2`.`b` = 10)) drop table t1, t2; create table t1 (oref char(4), grp int, ie int); insert into t1 (oref, grp, ie) values @@ -584,7 +584,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 index_subquery idx idx 5 func 4 100.00 Using where; Full scan on NULL key Warnings: Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t2`.`oref` AS `oref`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on idx checking NULL where ((`test`.`t1`.`oref` = `test`.`t2`.`oref`) and trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`ie1`) or isnull(`test`.`t1`.`ie1`))) and trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`ie2`) or isnull(`test`.`t1`.`ie2`)))) having (trigcond(<is_not_null_test>(`test`.`t1`.`ie1`)) and trigcond(<is_not_null_test>(`test`.`t1`.`ie2`))))))) AS `Z` from `test`.`t2` +Note 1003 select `test`.`t2`.`oref` AS `oref`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on idx checking NULL where ((`test`.`t1`.`oref` = `test`.`t2`.`oref`) and trigcond(trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`ie1`) or isnull(`test`.`t1`.`ie1`)))) and trigcond(trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`ie2`) or isnull(`test`.`t1`.`ie2`))))) having (trigcond(<is_not_null_test>(`test`.`t1`.`ie1`)) and trigcond(<is_not_null_test>(`test`.`t1`.`ie2`))))))) AS `Z` from `test`.`t2` drop table t1,t2; create table t1 (oref char(4), grp int, ie int primary key); insert into t1 (oref, grp, ie) values diff --git a/mysql-test/r/subselect3_jcl6.result b/mysql-test/r/subselect3_jcl6.result index 9cd3018718c..5bae1453a5e 100644 --- a/mysql-test/r/subselect3_jcl6.result +++ b/mysql-test/r/subselect3_jcl6.result @@ -262,7 +262,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 2 100.00 Using where; Full scan on NULL key Warnings: Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`oref` AS `oref`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a checking NULL where ((`test`.`t1`.`c` = `test`.`t2`.`oref`) and trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`) or isnull(`test`.`t1`.`a`))) and trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`b`) or isnull(`test`.`t1`.`b`)))) having (trigcond(<is_not_null_test>(`test`.`t1`.`a`)) and trigcond(<is_not_null_test>(`test`.`t1`.`b`))))))) AS `Z` from `test`.`t2` +Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`oref` AS `oref`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a checking NULL where ((`test`.`t1`.`c` = `test`.`t2`.`oref`) and trigcond(trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`) or isnull(`test`.`t1`.`a`)))) and trigcond(trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`b`) or isnull(`test`.`t1`.`b`))))) having (trigcond(<is_not_null_test>(`test`.`t1`.`a`)) and trigcond(<is_not_null_test>(`test`.`t1`.`b`))))))) AS `Z` from `test`.`t2` select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2; a b oref Z NULL 1 100 0 @@ -279,7 +279,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t4 ALL NULL NULL NULL NULL 100 100.00 Using where; Using join buffer (flat, BNL join) Warnings: Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`oref` AS `oref`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(select `test`.`t1`.`a`,`test`.`t1`.`b` from `test`.`t1` join `test`.`t4` where ((`test`.`t1`.`c` = `test`.`t2`.`oref`) and trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`) or isnull(`test`.`t1`.`a`))) and trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`b`) or isnull(`test`.`t1`.`b`)))) having (trigcond(<is_not_null_test>(`test`.`t1`.`a`)) and trigcond(<is_not_null_test>(`test`.`t1`.`b`)))))) AS `Z` from `test`.`t2` +Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`oref` AS `oref`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(select `test`.`t1`.`a`,`test`.`t1`.`b` from `test`.`t1` join `test`.`t4` where ((`test`.`t1`.`c` = `test`.`t2`.`oref`) and trigcond(trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`) or isnull(`test`.`t1`.`a`)))) and trigcond(trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`b`) or isnull(`test`.`t1`.`b`))))) having (trigcond(<is_not_null_test>(`test`.`t1`.`a`)) and trigcond(<is_not_null_test>(`test`.`t1`.`b`)))))) AS `Z` from `test`.`t2` select a,b, oref, (a,b) in (select a,b from t1,t4 where c=t2.oref) Z from t2; @@ -324,7 +324,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 index_subquery idx idx 5 func 4 100.00 Using where; Full scan on NULL key Warnings: Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t2`.`oref` AS `oref`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on idx checking NULL where ((`test`.`t1`.`oref` = `test`.`t2`.`oref`) and trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`ie1`) or isnull(`test`.`t1`.`ie1`))) and trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`ie2`) or isnull(`test`.`t1`.`ie2`)))) having (trigcond(<is_not_null_test>(`test`.`t1`.`ie1`)) and trigcond(<is_not_null_test>(`test`.`t1`.`ie2`))))))) AS `Z` from `test`.`t2` where ((`test`.`t2`.`a` = 10) and (`test`.`t2`.`b` = 10)) +Note 1003 select `test`.`t2`.`oref` AS `oref`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on idx checking NULL where ((`test`.`t1`.`oref` = `test`.`t2`.`oref`) and trigcond(trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`ie1`) or isnull(`test`.`t1`.`ie1`)))) and trigcond(trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`ie2`) or isnull(`test`.`t1`.`ie2`))))) having (trigcond(<is_not_null_test>(`test`.`t1`.`ie1`)) and trigcond(<is_not_null_test>(`test`.`t1`.`ie2`))))))) AS `Z` from `test`.`t2` where ((`test`.`t2`.`a` = 10) and (`test`.`t2`.`b` = 10)) drop table t1, t2; create table t1 (oref char(4), grp int, ie int); insert into t1 (oref, grp, ie) values @@ -594,7 +594,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 index_subquery idx idx 5 func 4 100.00 Using where; Full scan on NULL key Warnings: Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t2`.`oref` AS `oref`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on idx checking NULL where ((`test`.`t1`.`oref` = `test`.`t2`.`oref`) and trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`ie1`) or isnull(`test`.`t1`.`ie1`))) and trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`ie2`) or isnull(`test`.`t1`.`ie2`)))) having (trigcond(<is_not_null_test>(`test`.`t1`.`ie1`)) and trigcond(<is_not_null_test>(`test`.`t1`.`ie2`))))))) AS `Z` from `test`.`t2` +Note 1003 select `test`.`t2`.`oref` AS `oref`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,<expr_cache><`test`.`t2`.`a`,`test`.`t2`.`b`,`test`.`t2`.`oref`>(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`b`),<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on idx checking NULL where ((`test`.`t1`.`oref` = `test`.`t2`.`oref`) and trigcond(trigcond(((<cache>(`test`.`t2`.`a`) = `test`.`t1`.`ie1`) or isnull(`test`.`t1`.`ie1`)))) and trigcond(trigcond(((<cache>(`test`.`t2`.`b`) = `test`.`t1`.`ie2`) or isnull(`test`.`t1`.`ie2`))))) having (trigcond(<is_not_null_test>(`test`.`t1`.`ie1`)) and trigcond(<is_not_null_test>(`test`.`t1`.`ie2`))))))) AS `Z` from `test`.`t2` drop table t1,t2; create table t1 (oref char(4), grp int, ie int primary key); insert into t1 (oref, grp, ie) values diff --git a/mysql-test/r/subselect4.result b/mysql-test/r/subselect4.result index 2814e5a6dcd..0d5209d0ec2 100644 --- a/mysql-test/r/subselect4.result +++ b/mysql-test/r/subselect4.result @@ -303,7 +303,7 @@ EXPLAIN SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2a.i, t2a.pk FROM t2a WHERE t2a.pk = t1.pk); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where -2 DEPENDENT SUBQUERY t2a eq_ref PRIMARY PRIMARY 8 const,test.t1.pk 1 Using where; Using index; Full scan on NULL key +2 DEPENDENT SUBQUERY t2a unique_subquery PRIMARY PRIMARY 8 const,test.t1.pk 1 Using index; Using where; Full scan on NULL key SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2a.i, t2a.pk FROM t2a WHERE t2a.pk = t1.pk); pk i 0 10 @@ -335,7 +335,7 @@ EXPLAIN SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2c.i, t2c.pk FROM t2c WHERE t2c.pk = t1.pk); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where -2 DEPENDENT SUBQUERY t2c ref it2c it2c 8 const,test.t1.pk 2 Using where; Using index; Full scan on NULL key +2 DEPENDENT SUBQUERY t2c index_subquery it2c it2c 8 const,test.t1.pk 2 Using index; Using where; Full scan on NULL key SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2c.i, t2c.pk FROM t2c WHERE t2c.pk = t1.pk); pk i 0 10 diff --git a/mysql-test/r/subselect_exists2in.result b/mysql-test/r/subselect_exists2in.result new file mode 100644 index 00000000000..efbbe08cede --- /dev/null +++ b/mysql-test/r/subselect_exists2in.result @@ -0,0 +1,865 @@ +drop table if exists t1,t2,t3; +set optimizer_switch='exists_to_in=on'; +# +# LP BUG#884644 exists2in broke name resolution +# +CREATE TABLE t1 (f1 integer); +SELECT * FROM t1 WHERE EXISTS (SELECT NO_SUCH_TABLE.NO_SUCH_FIELD FROM t1); +ERROR 42S22: Unknown column 'NO_SUCH_TABLE.NO_SUCH_FIELD' in 'field list' +drop table t1; +# +# LP BUG#884657 Wrong result with exists2in , correlated subquery +# +CREATE TABLE t1 ( a varchar(1)) ; +INSERT INTO t1 VALUES ('c'),('b'); +CREATE TABLE t2 ( b varchar(1)) ; +INSERT INTO t2 VALUES ('v'),('v'),('c'),(NULL),('x'),('i'),('e'),('p'),('s'),('j'),('z'),('c'),('a'),('q'),('y'),(NULL),('r'),('v'),(NULL),('r'); +CREATE TABLE t3 ( a int NOT NULL , b varchar(1)) ; +INSERT INTO t3 VALUES (29,'c'); +SELECT * +FROM t1, t2 +WHERE EXISTS ( +SELECT a +FROM t3 +WHERE t3.b = t1.a +AND t3.b <> t2.b +); +a b +c v +c v +c x +c i +c e +c p +c s +c j +c z +c a +c q +c y +c r +c v +c r +INSERT INTO t3 VALUES (2,'c'); +alter table t1 add index aa (a); +alter table t3 add index bb (b); +-- EXIST to IN then semijoin (has priority over IN to EXISTS) +set optimizer_switch='exists_to_in=on,in_to_exists=on,semijoin=on,materialization=off,subquery_cache=off'; +SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a); +a +c +explain extended +SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index aa aa 4 NULL 2 100.00 Using index +1 PRIMARY t3 ALL bb NULL NULL NULL 2 100.00 Using where; FirstMatch(t1); Using join buffer (flat, BNL join) +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t3`) where (`test`.`t3`.`b` = `test`.`t1`.`a`) +-- EXIST to IN then IN to EXISTS +set optimizer_switch='exists_to_in=on,in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off'; +SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a); +a +c +explain extended +SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL aa 4 NULL 2 100.00 Using where; Using index +2 DEPENDENT SUBQUERY t3 ALL bb NULL NULL NULL 2 100.00 Using where +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t3`.`b` from `test`.`t3` where (<cache>(`test`.`t1`.`a`) = `test`.`t3`.`b`))) +-- EXIST2IN then MATERIALIZATION +set optimizer_switch='exists_to_in=on,in_to_exists=off,semijoin=off,materialization=on,subquery_cache=off'; +SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a); +a +c +explain extended +SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL aa 4 NULL 2 100.00 Using where; Using index +2 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 100.00 +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (select `test`.`t3`.`b` from `test`.`t3` where 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where ((`test`.`t1`.`a` = `<subquery2>`.`b`))))) +-- NO EXIST2IN +set optimizer_switch='exists_to_in=off,subquery_cache=off'; +SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a); +a +c +explain extended +SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL aa 4 NULL 2 100.00 Using where; Using index +2 DEPENDENT SUBQUERY t3 ALL bb NULL NULL NULL 2 100.00 Using where +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select `test`.`t3`.`a` from `test`.`t3` where (`test`.`t3`.`b` = `test`.`t1`.`a`)) +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +drop table t1,t2,t3; +# +# From group_min_max.test +# +create table t1 ( +a1 char(64), a2 char(64), b char(16), c char(16) not null, d char(16), dummy char(64) default ' ' +); +insert into t1 (a1, a2, b, c, d) values +('a','a','a','a111','xy1'),('a','a','a','b111','xy2'),('a','a','a','c111','xy3'),('a','a','a','d111','xy4'), +('a','a','b','e112','xy1'),('a','a','b','f112','xy2'),('a','a','b','g112','xy3'),('a','a','b','h112','xy4'), +('a','b','a','i121','xy1'),('a','b','a','j121','xy2'),('a','b','a','k121','xy3'),('a','b','a','l121','xy4'), +('a','b','b','m122','xy1'),('a','b','b','n122','xy2'),('a','b','b','o122','xy3'),('a','b','b','p122','xy4'), +('b','a','a','a211','xy1'),('b','a','a','b211','xy2'),('b','a','a','c211','xy3'),('b','a','a','d211','xy4'), +('b','a','b','e212','xy1'),('b','a','b','f212','xy2'),('b','a','b','g212','xy3'),('b','a','b','h212','xy4'), +('b','b','a','i221','xy1'),('b','b','a','j221','xy2'),('b','b','a','k221','xy3'),('b','b','a','l221','xy4'), +('b','b','b','m222','xy1'),('b','b','b','n222','xy2'),('b','b','b','o222','xy3'),('b','b','b','p222','xy4'), +('c','a','a','a311','xy1'),('c','a','a','b311','xy2'),('c','a','a','c311','xy3'),('c','a','a','d311','xy4'), +('c','a','b','e312','xy1'),('c','a','b','f312','xy2'),('c','a','b','g312','xy3'),('c','a','b','h312','xy4'), +('c','b','a','i321','xy1'),('c','b','a','j321','xy2'),('c','b','a','k321','xy3'),('c','b','a','l321','xy4'), +('c','b','b','m322','xy1'),('c','b','b','n322','xy2'),('c','b','b','o322','xy3'),('c','b','b','p322','xy4'), +('d','a','a','a411','xy1'),('d','a','a','b411','xy2'),('d','a','a','c411','xy3'),('d','a','a','d411','xy4'), +('d','a','b','e412','xy1'),('d','a','b','f412','xy2'),('d','a','b','g412','xy3'),('d','a','b','h412','xy4'), +('d','b','a','i421','xy1'),('d','b','a','j421','xy2'),('d','b','a','k421','xy3'),('d','b','a','l421','xy4'), +('d','b','b','m422','xy1'),('d','b','b','n422','xy2'),('d','b','b','o422','xy3'),('d','b','b','p422','xy4'), +('a','a','a','a111','xy1'),('a','a','a','b111','xy2'),('a','a','a','c111','xy3'),('a','a','a','d111','xy4'), +('a','a','b','e112','xy1'),('a','a','b','f112','xy2'),('a','a','b','g112','xy3'),('a','a','b','h112','xy4'), +('a','b','a','i121','xy1'),('a','b','a','j121','xy2'),('a','b','a','k121','xy3'),('a','b','a','l121','xy4'), +('a','b','b','m122','xy1'),('a','b','b','n122','xy2'),('a','b','b','o122','xy3'),('a','b','b','p122','xy4'), +('b','a','a','a211','xy1'),('b','a','a','b211','xy2'),('b','a','a','c211','xy3'),('b','a','a','d211','xy4'), +('b','a','b','e212','xy1'),('b','a','b','f212','xy2'),('b','a','b','g212','xy3'),('b','a','b','h212','xy4'), +('b','b','a','i221','xy1'),('b','b','a','j221','xy2'),('b','b','a','k221','xy3'),('b','b','a','l221','xy4'), +('b','b','b','m222','xy1'),('b','b','b','n222','xy2'),('b','b','b','o222','xy3'),('b','b','b','p222','xy4'), +('c','a','a','a311','xy1'),('c','a','a','b311','xy2'),('c','a','a','c311','xy3'),('c','a','a','d311','xy4'), +('c','a','b','e312','xy1'),('c','a','b','f312','xy2'),('c','a','b','g312','xy3'),('c','a','b','h312','xy4'), +('c','b','a','i321','xy1'),('c','b','a','j321','xy2'),('c','b','a','k321','xy3'),('c','b','a','l321','xy4'), +('c','b','b','m322','xy1'),('c','b','b','n322','xy2'),('c','b','b','o322','xy3'),('c','b','b','p322','xy4'), +('d','a','a','a411','xy1'),('d','a','a','b411','xy2'),('d','a','a','c411','xy3'),('d','a','a','d411','xy4'), +('d','a','b','e412','xy1'),('d','a','b','f412','xy2'),('d','a','b','g412','xy3'),('d','a','b','h412','xy4'), +('d','b','a','i421','xy1'),('d','b','a','j421','xy2'),('d','b','a','k421','xy3'),('d','b','a','l421','xy4'), +('d','b','b','m422','xy1'),('d','b','b','n422','xy2'),('d','b','b','o422','xy3'),('d','b','b','p422','xy4'); +create index idx_t1_0 on t1 (a1); +create index idx_t1_1 on t1 (a1,a2,b,c); +create index idx_t1_2 on t1 (a1,a2,b); +analyze table t1; +Table Op Msg_type Msg_text +test.t1 analyze status Table is already up to date +create table t2 ( +a1 char(64), a2 char(64) not null, b char(16), c char(16), d char(16), dummy char(64) default ' ' +); +insert into t2 select * from t1; +insert into t2 (a1, a2, b, c, d) values +('a','a',NULL,'a777','xyz'),('a','a',NULL,'a888','xyz'),('a','a',NULL,'a999','xyz'), +('a','a','a',NULL,'xyz'), +('a','a','b',NULL,'xyz'), +('a','b','a',NULL,'xyz'), +('c','a',NULL,'c777','xyz'),('c','a',NULL,'c888','xyz'),('c','a',NULL,'c999','xyz'), +('d','b','b',NULL,'xyz'), +('e','a','a',NULL,'xyz'),('e','a','a',NULL,'xyz'),('e','a','a',NULL,'xyz'),('e','a','a',NULL,'xyz'), +('e','a','b',NULL,'xyz'),('e','a','b',NULL,'xyz'),('e','a','b',NULL,'xyz'),('e','a','b',NULL,'xyz'), +('a','a',NULL,'a777','xyz'),('a','a',NULL,'a888','xyz'),('a','a',NULL,'a999','xyz'), +('a','a','a',NULL,'xyz'), +('a','a','b',NULL,'xyz'), +('a','b','a',NULL,'xyz'), +('c','a',NULL,'c777','xyz'),('c','a',NULL,'c888','xyz'),('c','a',NULL,'c999','xyz'), +('d','b','b',NULL,'xyz'), +('e','a','a',NULL,'xyz'),('e','a','a',NULL,'xyz'),('e','a','a',NULL,'xyz'),('e','a','a',NULL,'xyz'), +('e','a','b',NULL,'xyz'),('e','a','b',NULL,'xyz'),('e','a','b',NULL,'xyz'),('e','a','b',NULL,'xyz'); +create index idx_t2_0 on t2 (a1); +create index idx_t2_1 on t2 (a1,a2,b,c); +create index idx_t2_2 on t2 (a1,a2,b); +analyze table t2; +Table Op Msg_type Msg_text +test.t2 analyze status Table is already up to date +create table t3 ( +a1 char(1), a2 char(1), b char(1), c char(4) not null, d char(3), dummy char(1) default ' ' +); +insert into t3 (a1, a2, b, c, d) values +('a','a','a','a111','xy1'),('a','a','a','b111','xy2'),('a','a','a','c111','xy3'),('a','a','a','d111','xy4'), +('a','a','b','e112','xy1'),('a','a','b','f112','xy2'),('a','a','b','g112','xy3'),('a','a','b','h112','xy4'), +('a','b','a','i121','xy1'),('a','b','a','j121','xy2'),('a','b','a','k121','xy3'),('a','b','a','l121','xy4'), +('a','b','b','m122','xy1'),('a','b','b','n122','xy2'),('a','b','b','o122','xy3'),('a','b','b','p122','xy4'), +('b','a','a','a211','xy1'),('b','a','a','b211','xy2'),('b','a','a','c211','xy3'),('b','a','a','d211','xy4'), +('b','a','b','e212','xy1'),('b','a','b','f212','xy2'),('b','a','b','g212','xy3'),('b','a','b','h212','xy4'), +('b','b','a','i221','xy1'),('b','b','a','j221','xy2'),('b','b','a','k221','xy3'),('b','b','a','l221','xy4'), +('b','b','b','m222','xy1'),('b','b','b','n222','xy2'),('b','b','b','o222','xy3'),('b','b','b','p222','xy4'), +('c','a','a','a311','xy1'),('c','a','a','b311','xy2'),('c','a','a','c311','xy3'),('c','a','a','d311','xy4'), +('c','a','b','e312','xy1'),('c','a','b','f312','xy2'),('c','a','b','g312','xy3'),('c','a','b','h312','xy4'), +('c','b','a','i321','xy1'),('c','b','a','j321','xy2'),('c','b','a','k321','xy3'),('c','b','a','l321','xy4'), +('c','b','b','m322','xy1'),('c','b','b','n322','xy2'),('c','b','b','o322','xy3'),('c','b','b','p322','xy4'); +insert into t3 (a1, a2, b, c, d) values +('a','a','a','a111','xy1'),('a','a','a','b111','xy2'),('a','a','a','c111','xy3'),('a','a','a','d111','xy4'), +('a','a','b','e112','xy1'),('a','a','b','f112','xy2'),('a','a','b','g112','xy3'),('a','a','b','h112','xy4'), +('a','b','a','i121','xy1'),('a','b','a','j121','xy2'),('a','b','a','k121','xy3'),('a','b','a','l121','xy4'), +('a','b','b','m122','xy1'),('a','b','b','n122','xy2'),('a','b','b','o122','xy3'),('a','b','b','p122','xy4'), +('b','a','a','a211','xy1'),('b','a','a','b211','xy2'),('b','a','a','c211','xy3'),('b','a','a','d211','xy4'), +('b','a','b','e212','xy1'),('b','a','b','f212','xy2'),('b','a','b','g212','xy3'),('b','a','b','h212','xy4'), +('b','b','a','i221','xy1'),('b','b','a','j221','xy2'),('b','b','a','k221','xy3'),('b','b','a','l221','xy4'), +('b','b','b','m222','xy1'),('b','b','b','n222','xy2'),('b','b','b','o222','xy3'),('b','b','b','p222','xy4'), +('c','a','a','a311','xy1'),('c','a','a','b311','xy2'),('c','a','a','c311','xy3'),('c','a','a','d311','xy4'), +('c','a','b','e312','xy1'),('c','a','b','f312','xy2'),('c','a','b','g312','xy3'),('c','a','b','h312','xy4'), +('c','b','a','i321','xy1'),('c','b','a','j321','xy2'),('c','b','a','k321','xy3'),('c','b','a','l321','xy4'), +('c','b','b','m322','xy1'),('c','b','b','n322','xy2'),('c','b','b','o322','xy3'),('c','b','b','p322','xy4'); +insert into t3 (a1, a2, b, c, d) values +('a','a','a','a111','xy1'),('a','a','a','b111','xy2'),('a','a','a','c111','xy3'),('a','a','a','d111','xy4'), +('a','a','b','e112','xy1'),('a','a','b','f112','xy2'),('a','a','b','g112','xy3'),('a','a','b','h112','xy4'), +('a','b','a','i121','xy1'),('a','b','a','j121','xy2'),('a','b','a','k121','xy3'),('a','b','a','l121','xy4'), +('a','b','b','m122','xy1'),('a','b','b','n122','xy2'),('a','b','b','o122','xy3'),('a','b','b','p122','xy4'), +('b','a','a','a211','xy1'),('b','a','a','b211','xy2'),('b','a','a','c211','xy3'),('b','a','a','d211','xy4'), +('b','a','b','e212','xy1'),('b','a','b','f212','xy2'),('b','a','b','g212','xy3'),('b','a','b','h212','xy4'), +('b','b','a','i221','xy1'),('b','b','a','j221','xy2'),('b','b','a','k221','xy3'),('b','b','a','l221','xy4'), +('b','b','b','m222','xy1'),('b','b','b','n222','xy2'),('b','b','b','o222','xy3'),('b','b','b','p222','xy4'), +('c','a','a','a311','xy1'),('c','a','a','b311','xy2'),('c','a','a','c311','xy3'),('c','a','a','d311','xy4'), +('c','a','b','e312','xy1'),('c','a','b','f312','xy2'),('c','a','b','g312','xy3'),('c','a','b','h312','xy4'), +('c','b','a','i321','xy1'),('c','b','a','j321','xy2'),('c','b','a','k321','xy3'),('c','b','a','l321','xy4'), +('c','b','b','m322','xy1'),('c','b','b','n322','xy2'),('c','b','b','o322','xy3'),('c','b','b','p322','xy4'); +insert into t3 (a1, a2, b, c, d) values +('a','a','a','a111','xy1'),('a','a','a','b111','xy2'),('a','a','a','c111','xy3'),('a','a','a','d111','xy4'), +('a','a','b','e112','xy1'),('a','a','b','f112','xy2'),('a','a','b','g112','xy3'),('a','a','b','h112','xy4'), +('a','b','a','i121','xy1'),('a','b','a','j121','xy2'),('a','b','a','k121','xy3'),('a','b','a','l121','xy4'), +('a','b','b','m122','xy1'),('a','b','b','n122','xy2'),('a','b','b','o122','xy3'),('a','b','b','p122','xy4'), +('b','a','a','a211','xy1'),('b','a','a','b211','xy2'),('b','a','a','c211','xy3'),('b','a','a','d211','xy4'), +('b','a','b','e212','xy1'),('b','a','b','f212','xy2'),('b','a','b','g212','xy3'),('b','a','b','h212','xy4'), +('b','b','a','i221','xy1'),('b','b','a','j221','xy2'),('b','b','a','k221','xy3'),('b','b','a','l221','xy4'), +('b','b','b','m222','xy1'),('b','b','b','n222','xy2'),('b','b','b','o222','xy3'),('b','b','b','p222','xy4'), +('c','a','a','a311','xy1'),('c','a','a','b311','xy2'),('c','a','a','c311','xy3'),('c','a','a','d311','xy4'), +('c','a','b','e312','xy1'),('c','a','b','f312','xy2'),('c','a','b','g312','xy3'),('c','a','b','h312','xy4'), +('c','b','a','i321','xy1'),('c','b','a','j321','xy2'),('c','b','a','k321','xy3'),('c','b','a','l321','xy4'), +('c','b','b','m322','xy1'),('c','b','b','n322','xy2'),('c','b','b','o322','xy3'),('c','b','b','p322','xy4'); +create index idx_t3_0 on t3 (a1); +create index idx_t3_1 on t3 (a1,a2,b,c); +create index idx_t3_2 on t3 (a1,a2,b); +analyze table t3; +Table Op Msg_type Msg_text +test.t3 analyze status Table is already up to date +explain select a1,a2,b,c,min(c), max(c) from t1 +where exists ( select * from t2 +where t2.c in (select c from t3 where t3.c > t1.b) and +t2.c > 'b1' ) +group by a1,a2,b; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 index NULL idx_t1_1 163 NULL 128 Using where; Using index +2 DEPENDENT SUBQUERY t2 index NULL idx_t2_1 163 NULL 164 Using where; Using index +2 DEPENDENT SUBQUERY t3 index NULL idx_t3_1 10 NULL 192 Using where; Using index; FirstMatch(t2); Using join buffer (flat, BNL join) +select a1,a2,b,c,min(c), max(c) from t1 +where exists ( select * from t2 +where t2.c in (select c from t3 where t3.c > t1.b) and +t2.c > 'b1' ) +group by a1,a2,b; +a1 a2 b c min(c) max(c) +a a a a111 a111 d111 +a a b e112 e112 h112 +a b a i121 i121 l121 +a b b m122 m122 p122 +b a a a211 a211 d211 +b a b e212 e212 h212 +b b a i221 i221 l221 +b b b m222 m222 p222 +c a a a311 a311 d311 +c a b e312 e312 h312 +c b a i321 i321 l321 +c b b m322 m322 p322 +d a a a411 a411 d411 +d a b e412 e412 h412 +d b a i421 i421 l421 +d b b m422 m422 p422 +explain select a1,a2,b,c,min(c), max(c) from t1 +where exists ( select * from t2 +where t2.c in (select c from t3 where t3.c > t1.c) and +t2.c > 'b1' ) +group by a1,a2,b; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 index NULL idx_t1_1 163 NULL 128 Using where; Using index +2 DEPENDENT SUBQUERY t2 index NULL idx_t2_1 163 NULL 164 Using where; Using index +2 DEPENDENT SUBQUERY t3 index NULL idx_t3_1 10 NULL 192 Using where; Using index; FirstMatch(t2); Using join buffer (flat, BNL join) +select a1,a2,b,c,min(c), max(c) from t1 +where exists ( select * from t2 +where t2.c in (select c from t3 where t3.c > t1.c) and +t2.c > 'b1' ) +group by a1,a2,b; +a1 a2 b c min(c) max(c) +a a a a111 a111 d111 +a a b e112 e112 h112 +a b a i121 i121 l121 +a b b m122 m122 p122 +b a a a211 a211 d211 +b a b e212 e212 h212 +b b a i221 i221 l221 +b b b m222 m222 p222 +c a a a311 a311 d311 +c a b e312 e312 h312 +c b a i321 i321 l321 +c b b m322 m322 o322 +d a a a411 a411 d411 +d a b e412 e412 h412 +d b a i421 i421 l421 +d b b m422 m422 o422 +drop table t1, t2, t3; +CREATE TABLE t1 ( a INT ); +INSERT INTO t1 VALUES (7),(0); +CREATE TABLE t2 ( b INT ); +INSERT INTO t2 VALUES (0),(8); +SELECT * FROM t1 WHERE +EXISTS ( SELECT * FROM t2 WHERE b = a ) +OR a > 0; +a +7 +0 +explain extended +SELECT * FROM t1 WHERE +EXISTS ( SELECT * FROM t2 WHERE b = a ) +OR a > 0; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where +2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 100.00 +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (select `test`.`t2`.`b` from `test`.`t2` where 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where ((`test`.`t1`.`a` = `<subquery2>`.`b`)))))) or (`test`.`t1`.`a` > 0)) +drop tables t1,t2; +CREATE TABLE t1 ( a INT ); +INSERT INTO t1 VALUES (1),(5); +CREATE TABLE t2 ( b INT ) ENGINE=MyISAM; +INSERT INTO t2 VALUES (1); +CREATE TABLE t3 ( c INT ); +INSERT INTO t3 VALUES (4),(5); +SET optimizer_switch='exists_to_in=on,subquery_cache=off,materialization=on,in_to_exists=off,semijoin=off'; +explain extended +SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 +2 SUBQUERY t2 system NULL NULL NULL NULL 1 100.00 +3 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 100.00 Using where +Warnings: +Note 1276 Field or reference 'test.t2.b' of SELECT #3 was resolved in SELECT #2 +Note 1003 select (select 1 from dual where (not(((1 is not null) and <in_optimizer>(1,1 in ( <materialize> (select `test`.`t3`.`c` from `test`.`t3` where (`test`.`t3`.`c` is not null) ), <primary_index_lookup>(1 in <temporary table> on distinct_key where ((1 = `<subquery3>`.`c`))))))))) AS `( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) )` from `test`.`t1` +SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1; +( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) +1 +1 +SET optimizer_switch='exists_to_in=on,subquery_cache=off'; +explain extended +SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 +2 SUBQUERY t2 system NULL NULL NULL NULL 1 100.00 +3 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 100.00 Using where +Warnings: +Note 1276 Field or reference 'test.t2.b' of SELECT #3 was resolved in SELECT #2 +Note 1003 select (select 1 from dual where (not(((1 is not null) and <in_optimizer>(1,1 in ( <materialize> (select `test`.`t3`.`c` from `test`.`t3` where (`test`.`t3`.`c` is not null) ), <primary_index_lookup>(1 in <temporary table> on distinct_key where ((1 = `<subquery3>`.`c`))))))))) AS `( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) )` from `test`.`t1` +SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1; +( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) +1 +1 +SET optimizer_switch='exists_to_in=off,subquery_cache=off'; +explain extended +SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 +2 SUBQUERY t2 system NULL NULL NULL NULL 1 100.00 +3 SUBQUERY t3 ALL NULL NULL NULL NULL 2 100.00 Using where +Warnings: +Note 1276 Field or reference 'test.t2.b' of SELECT #3 was resolved in SELECT #2 +Note 1003 select (select 1 from dual where (not(exists(select `test`.`t3`.`c` from `test`.`t3` where (`test`.`t3`.`c` = 1))))) AS `( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) )` from `test`.`t1` +SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1; +( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) +1 +1 +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +drop table t1,t2,t3; +# multi condition test +CREATE TABLE t1 ( a varchar(1), a1 varchar(1)) ; +INSERT INTO t1 VALUES ('c', 'c'), ('b', 'b'); +CREATE TABLE t3 ( a int NOT NULL , b varchar(1), b1 varchar(1)) ; +INSERT INTO t3 VALUES (29,'c','c'); +INSERT INTO t3 VALUES (2,'c','c'); +alter table t1 add index aa (a,a1); +alter table t3 add index bb (b,b1); +-- EXIST to IN then semijoin (has priority over IN to EXISTS) +set optimizer_switch='exists_to_in=on,in_to_exists=on,semijoin=on,materialization=off,subquery_cache=off'; +SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1); +a a1 +c c +explain extended +SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t3 index bb bb 8 NULL 2 100.00 Using where; Using index; LooseScan +1 PRIMARY t1 ref aa aa 8 test.t3.b,test.t3.b1 2 100.00 Using index +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.t1.a1' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`a1` AS `a1` from `test`.`t1` semi join (`test`.`t3`) where ((`test`.`t1`.`a` = `test`.`t3`.`b`) and (`test`.`t1`.`a1` = `test`.`t3`.`b1`)) +-- EXIST to IN then IN to EXISTS +set optimizer_switch='exists_to_in=on,in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off'; +SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1); +a a1 +c c +explain extended +SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL aa 8 NULL 2 100.00 Using where; Using index +2 DEPENDENT SUBQUERY t3 index_subquery bb bb 8 func,func 2 100.00 Using index; Using where +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.t1.a1' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`a1` AS `a1` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`a`,`test`.`t1`.`a1`),<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t3 on bb where ((<cache>(`test`.`t1`.`a`) = `test`.`t3`.`b`) and (<cache>(`test`.`t1`.`a1`) = `test`.`t3`.`b1`))))) +-- EXIST2IN then MATERIALIZATION +set optimizer_switch='exists_to_in=on,in_to_exists=off,semijoin=off,materialization=on,subquery_cache=off'; +SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1); +a a1 +c c +explain extended +SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL aa 8 NULL 2 100.00 Using where; Using index +2 MATERIALIZED t3 index NULL bb 8 NULL 2 100.00 Using index +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.t1.a1' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`a1` AS `a1` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`a`,`test`.`t1`.`a1`),(`test`.`t1`.`a`,`test`.`t1`.`a1`) in ( <materialize> (select `test`.`t3`.`b`,`test`.`t3`.`b1` from `test`.`t3` where 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where ((`test`.`t1`.`a` = `<subquery2>`.`b`) and (`test`.`t1`.`a1` = `<subquery2>`.`b1`))))) +-- NO EXIST2IN +set optimizer_switch='exists_to_in=off,subquery_cache=off'; +SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1); +a a1 +c c +explain extended +SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL aa 8 NULL 2 100.00 Using where; Using index +2 DEPENDENT SUBQUERY t3 ref bb bb 8 test.t1.a,test.t1.a1 2 100.00 Using index +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.t1.a1' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`a1` AS `a1` from `test`.`t1` where exists(select 1 from `test`.`t3` where ((`test`.`t3`.`b` = `test`.`t1`.`a`) and (`test`.`t3`.`b1` = `test`.`t1`.`a1`))) +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +drop table t1,t3; +# +# MDEV-159 Assertion about not marked for read failed in +# String* Field_varstring::val_str(String*, String*) +# +SET optimizer_switch = REPLACE( @@optimizer_switch, '=on', '=off' ); +SET optimizer_switch='in_to_exists=on,exists_to_in=on'; +CREATE TABLE t1 ( a VARCHAR(1) ); +INSERT INTO t1 VALUES ('k'),('m'); +CREATE TABLE t2 ( b INT, +c VARCHAR(1), +d VARCHAR(1) NOT NULL ); +INSERT INTO t2 VALUES +(4,'j','j'),(6,'v','v'); +CREATE ALGORITHM=MERGE VIEW v AS SELECT * FROM t2 WHERE b < 1; +SELECT c FROM v +WHERE EXISTS ( +SELECT * FROM t1, t2 +WHERE a <= v.d AND b = v.b +); +c +explain extended +SELECT c FROM v +WHERE EXISTS ( +SELECT * FROM t1, t2 +WHERE a <= v.d AND b = v.b +); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join) +Warnings: +Note 1276 Field or reference 'v.d' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'v.b' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t2`.`c` AS `c` from `test`.`t2` where (<in_optimizer>(`test`.`t2`.`b`,<exists>(select `test`.`t2`.`b` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` <= `test`.`t2`.`d`) and (<cache>(`test`.`t2`.`b`) = `test`.`t2`.`b`)))) and (`test`.`t2`.`b` < 1)) +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +drop view v; +drop table t1,t2; +# +# MDEV-160 Exists2In: Crash in in hp_movelink with subquery_cache=ON +# +SET optimizer_switch = 'in_to_exists=on,subquery_cache=on,exists_to_in=on'; +CREATE TABLE t1 ( +a VARCHAR(3) NOT NULL, +b VARCHAR(50) +); +INSERT INTO t1 VALUES +('USA','Chinese'),('USA','English'), +('FRA','French'),('ITA','Italian'); +CREATE TABLE t2 ( c VARCHAR(3) ); +INSERT INTO t2 VALUES ('USA'),('FRA'); +SELECT * FROM t1 AS alias1, t1 AS alias2 +WHERE EXISTS ( +SELECT * FROM t1, t2 +WHERE a <= alias2.a AND c = alias1.b +) OR alias1 .a = 'foo'; +a b a b +SELECT * FROM t1 AS alias1, t1 AS alias2 +WHERE EXISTS ( +SELECT * FROM t1, t2 +WHERE a <= alias2.a AND c = alias1.a +) OR alias1 .a = 'foo'; +a b a b +USA Chinese USA Chinese +USA English USA Chinese +FRA French USA Chinese +USA Chinese USA English +USA English USA English +FRA French USA English +USA Chinese FRA French +USA English FRA French +FRA French FRA French +USA Chinese ITA Italian +USA English ITA Italian +FRA French ITA Italian +drop table t1,t2; +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +# +# MDEV-160 Exists2In: Crash in in hp_movelink with subquery_cache=ON +# +SET optimizer_switch = 'in_to_exists=on,subquery_cache=on,exists_to_in=on'; +CREATE TABLE t1 ( +a VARCHAR(3) NOT NULL, +b VARCHAR(50) +); +INSERT INTO t1 VALUES +('USA','Chinese'),('USA','English'), +('FRA','French'),('ITA','Italian'); +CREATE TABLE t2 ( c VARCHAR(3) ); +INSERT INTO t2 VALUES ('USA'),('FRA'); +SELECT * FROM t1 AS alias1, t1 AS alias2 +WHERE EXISTS ( +SELECT * FROM t1, t2 +WHERE a <= alias2.a AND c = alias1.b +) OR alias1 .a = 'foo'; +a b a b +explain extended +SELECT * FROM t1 AS alias1, t1 AS alias2 +WHERE EXISTS ( +SELECT * FROM t1, t2 +WHERE a <= alias2.a AND c = alias1.b +) OR alias1 .a = 'foo'; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY alias1 ALL NULL NULL NULL NULL 4 100.00 +1 PRIMARY alias2 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join) +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join) +Warnings: +Note 1276 Field or reference 'test.alias2.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.alias1.b' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`alias1`.`a` AS `a`,`test`.`alias1`.`b` AS `b`,`test`.`alias2`.`a` AS `a`,`test`.`alias2`.`b` AS `b` from `test`.`t1` `alias1` join `test`.`t1` `alias2` where (<expr_cache><`test`.`alias1`.`b`,`test`.`alias2`.`a`>(<in_optimizer>(`test`.`alias1`.`b`,<exists>(select `test`.`t2`.`c` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` <= `test`.`alias2`.`a`) and (<cache>(`test`.`alias1`.`b`) = `test`.`t2`.`c`))))) or (`test`.`alias1`.`a` = 'foo')) +drop table t1,t2; +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +# +# MDEV-245 Exists2In: Wrong result (extra rows) with +# exists_to_in=ON, materialization=OFF, NOT EXISTS subquery +# +SET optimizer_switch='materialization=off,exists_to_in=on'; +CREATE TABLE t1 ( a INT ) ; +INSERT INTO t1 VALUES (0),(8),(1); +CREATE TABLE t2 ( b INT ) ; +INSERT INTO t2 VALUES (1),(2),(3); +SELECT * FROM t1 WHERE NOT EXISTS ( SELECT * FROM t2 WHERE b = a ); +a +0 +8 +explain extended +SELECT * FROM t1 WHERE NOT EXISTS ( SELECT * FROM t2 WHERE b = a ); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (not(((`test`.`t1`.`a` is not null) and <expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t2`.`b` from `test`.`t2` where ((`test`.`t2`.`b` is not null) and (<cache>(`test`.`t1`.`a`) = `test`.`t2`.`b`)))))))) +drop table t1,t2; +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +# +# MDEV-243 Wrong result (extra or missing rows) with +# exists_to_in + materialization, EXISTS subquery +# +SET optimizer_switch='index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=off,exists_to_in=on'; +CREATE TABLE t1 ( a VARCHAR(1), b VARCHAR(1) ); +INSERT INTO t1 VALUES ('v','v'),('s','v'); +SELECT * FROM t1 AS alias +WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b ); +a b +s v +explain extended +SELECT * FROM t1 AS alias +WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b ); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY alias ALL NULL NULL NULL NULL 2 100.00 Using where +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where; FirstMatch(alias); Using join buffer (flat, BNL join) +Warnings: +Note 1276 Field or reference 'test.alias.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.alias.b' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`alias`.`a` AS `a`,`test`.`alias`.`b` AS `b` from `test`.`t1` `alias` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`alias`.`b`) and (`test`.`alias`.`b` > `test`.`alias`.`a`)) +SET optimizer_switch = REPLACE( @@optimizer_switch, '=on', '=off' ); +SET optimizer_switch = 'exists_to_in=on,materialization=on,semijoin=off'; +SELECT * FROM t1 AS alias +WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b ); +a b +s v +explain extended +SELECT * FROM t1 AS alias +WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b ); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY alias ALL NULL NULL NULL NULL 2 100.00 Using where +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where +Warnings: +Note 1276 Field or reference 'test.alias.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.alias.b' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`alias`.`a` AS `a`,`test`.`alias`.`b` AS `b` from `test`.`t1` `alias` where <in_optimizer>(`test`.`alias`.`b`,<exists>(select `test`.`t1`.`a` from `test`.`t1` where ((`test`.`t1`.`a` > `test`.`alias`.`a`) and (<cache>(`test`.`alias`.`b`) = `test`.`t1`.`a`)))) +SET optimizer_switch = 'exists_to_in=on,materialization=on,semijoin=on'; +SELECT * FROM t1 AS alias +WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b ); +a b +s v +explain extended +SELECT * FROM t1 AS alias +WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b ); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY alias ALL NULL NULL NULL NULL 2 100.00 Using where +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Start temporary; End temporary +Warnings: +Note 1276 Field or reference 'test.alias.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.alias.b' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`alias`.`a` AS `a`,`test`.`alias`.`b` AS `b` from `test`.`t1` `alias` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`alias`.`b`) and (`test`.`alias`.`b` > `test`.`alias`.`a`)) +drop table t1; +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +# +# MDEV-403 Wrong result (missing rows) with subquery in +# EXISTS and an OR condition outside +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (2),(3); +CREATE TABLE t2 (b INT); +INSERT INTO t2 VALUES (1),(3); +SET optimizer_switch = 'exists_to_in=off,in_to_exists=on'; +SELECT * FROM t1 AS alias1, t2 AS alias2 +WHERE EXISTS ( +SELECT 1 FROM t2 WHERE b = alias1.a AND b > alias2.b +) OR a = 5; +a b +3 1 +SET optimizer_switch = 'exists_to_in=on,in_to_exists=on'; +SELECT * FROM t1 AS alias1, t2 AS alias2 +WHERE EXISTS ( +SELECT 1 FROM t2 WHERE b = alias1.a AND b > alias2.b +) OR a = 5; +a b +3 1 +explain extended +SELECT * FROM t1 AS alias1, t2 AS alias2 +WHERE EXISTS ( +SELECT 1 FROM t2 WHERE b = alias1.a AND b > alias2.b +) OR a = 5; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY alias1 ALL NULL NULL NULL NULL 2 100.00 +1 PRIMARY alias2 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join) +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where +Warnings: +Note 1276 Field or reference 'test.alias1.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.alias2.b' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`alias1`.`a` AS `a`,`test`.`alias2`.`b` AS `b` from `test`.`t1` `alias1` join `test`.`t2` `alias2` where (<expr_cache><`test`.`alias1`.`a`,`test`.`alias2`.`b`>(<in_optimizer>(`test`.`alias1`.`a`,<exists>(select `test`.`t2`.`b` from `test`.`t2` where ((`test`.`t2`.`b` > `test`.`alias2`.`b`) and (<cache>(`test`.`alias1`.`a`) = `test`.`t2`.`b`))))) or (`test`.`alias1`.`a` = 5)) +drop table t1, t2; +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +# +# MDEV-404: Wrong result (extra rows) with STRAIGHT_JOIN, +# EXISTS subquery, NOT NULL column +# (same as above) +# +SET optimizer_switch = 'exists_to_in=on,in_to_exists=on'; +CREATE TABLE t1 (a INT, b VARCHAR(1) NOT NULL); +INSERT INTO t1 VALUES (1,'s'),(2,'e'); +SELECT STRAIGHT_JOIN * FROM t1 AS alias1, t1 AS alias2 +WHERE EXISTS ( SELECT 1 FROM t1 WHERE b < alias2.b AND a = alias1.a ); +a b a b +2 e 1 s +drop table t1; +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +# +# MDEV-3800: ORDER BY doesn't work with exists_to_in=ON on +# a query with EXISTS subquery and OR condition +# +SET optimizer_switch = 'in_to_exists=on,exists_to_in=on'; +CREATE TABLE t1 (a INT, b VARCHAR(1)) ENGINE=MyISAM; +INSERT INTO t1 VALUES (4,'j'),(6,'v'),(3,'c'); +CREATE TABLE t2 (c VARCHAR(1)) ENGINE=MyISAM; +INSERT INTO t2 VALUES ('b'),('y'); +SELECT a FROM t1 +WHERE EXISTS ( +SELECT 1 FROM t2 WHERE c = b +) OR b NOT IN ('U') +ORDER BY a; +a +3 +4 +6 +select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`b` in (select `test`.`t2`.`c` from `test`.`t2` where 1 ) or (`test`.`t1`.`b` <> 'U') order by `test`.`t1`.`a`; +a +3 +4 +6 +drop table t1,t2; +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +# +# correct calculation of reserved items (postreview-fix) +# +create table t1 (col1 int, col2 int, col3 int); +insert into t1 values (1,2,3),(2,3,4),(4,5,6); +create table t2 as select * from t1; +explain extended +select * from t1 where exists (select col2 from t2 where t2.col1=t1.col1 and t2.col2=t1.col2); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 +1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 100.00 +2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 +Warnings: +Note 1276 Field or reference 'test.t1.col1' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.t1.col2' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t1`.`col1` AS `col1`,`test`.`t1`.`col2` AS `col2`,`test`.`t1`.`col3` AS `col3` from `test`.`t1` semi join (`test`.`t2`) where 1 +select * from t1 where exists (select col2 from t2 where t2.col1=t1.col1 and t2.col2=t1.col2); +col1 col2 col3 +1 2 3 +2 3 4 +4 5 6 +drop table t1,t2; +# +# MDEV-3879: Exists2In: Wrong result (extra row) and unexpected +# warning with exists_to_in=on and a NOT EXISTS subquery +# +SET optimizer_switch = 'exists_to_in=on'; +CREATE TABLE t1 (a1 INT, b1 CHAR(1)) ENGINE=MyISAM; +INSERT INTO t1 VALUES (3,'y'),(6,'w'); +CREATE TABLE t2 (a2 INT, b2 CHAR(1)) ENGINE=MyISAM; +INSERT INTO t2 VALUES (3,'y'),(6,'d'); +SELECT * FROM t1 +WHERE NOT EXISTS ( SELECT * FROM t2 WHERE b2 = b1 AND a2 = a1 ); +a1 b1 +6 w +drop table t1, t2; +# +# MDEV-3880: Wrong result (missing rows) with exists_to_in=on, +# LEFT JOIN and NOT EXISTS subquery. +# (Duplicate of above MDEV-3879). +# +SET optimizer_switch = 'exists_to_in=on'; +CREATE TABLE t1 (a1 INT, b1 CHAR(1)) ENGINE=MyISAM; +INSERT INTO t1 VALUES (4,'b'),(5,'y'); +CREATE TABLE t2 (b2 CHAR(1)) ENGINE=MyISAM; +INSERT INTO t2 VALUES ('z'),('b'); +CREATE TABLE t3 (a3 INT, b3 CHAR(1)) ENGINE=MyISAM; +INSERT INTO t3 VALUES (4,'j'),(6,'v'); +SELECT * FROM t1 LEFT JOIN t2 ON ( b2 = b1 ) +WHERE NOT EXISTS ( SELECT * FROM t3 WHERE b3 = b2 AND a3 = a1 ) ; +a1 b1 b2 +4 b b +5 y NULL +drop table t1, t2, t3; +# +# MDEV-3881: Endless loop and crash in Item_ref::real_item with +# exists_to_in=on, NOT EXISTS subquery, merge view or from subquery, +# constant table +# +SET optimizer_switch = 'exists_to_in=on'; +CREATE TABLE t1 (a INT) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(7); +CREATE TABLE t2 (b INT) ENGINE=MyISAM; +INSERT INTO t2 VALUES (8); +CREATE ALGORITHM=MERGE VIEW v1 AS SELECT * FROM t2; +CREATE TABLE t3 (c INT) ENGINE=MyISAM; +INSERT INTO t3 VALUES (4),(6); +SELECT * FROM t1, v1 WHERE NOT EXISTS ( SELECT * FROM t3 WHERE c = b ) AND a = b; +a b +drop view v1; +drop table t1, t2, t3; +CREATE TABLE t1 (a INT) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(7); +CREATE TABLE t2 (b INT) ENGINE=MyISAM; +INSERT INTO t2 VALUES (8); +CREATE TABLE t3 (c INT) ENGINE=MyISAM; +INSERT INTO t3 VALUES (4),(6); +SELECT * FROM t1, ( SELECT * FROM t2 ) alias WHERE NOT EXISTS ( SELECT * FROM t3 WHERE c = b ) AND a = b; +a b +drop table t1, t2, t3; +# +# MDEV-3906: Server crashes in Dependency_marker::visit_field +# on 2nd execution of PS with exists_to_in and NOT EXISTS subquery +# +SET optimizer_switch='exists_to_in=on'; +CREATE TABLE t1 (a INT) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(7); +PREPARE stmt FROM ' +SELECT * FROM t1 AS alias +WHERE NOT EXISTS ( SELECT * FROM t1 WHERE t1.a = alias.a ) +'; +EXECUTE stmt; +a +EXECUTE stmt; +a +drop table t1; +# +# MDEV-3904: Assertion `in_subs->has_strategy()' failed in +# JOIN::choose_subquery_plan on 2nd execution of PS with +# exists_to_in+semijoin, EXISTS subquery, MERGE view or FROM subquery +# +SET optimizer_switch='in_to_exists=on,semijoin=on,exists_to_in=on'; +CREATE TABLE t1 (a INT) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(2); +CREATE TABLE t2 (b INT) ENGINE=MyISAM; +INSERT INTO t2 VALUES (4),(6); +CREATE ALGORITHM=MERGE VIEW v AS +SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t2 WHERE b = a ); +PREPARE stmt FROM ' SELECT * FROM v '; +EXECUTE stmt; +a +EXECUTE stmt; +a +drop view v; +drop table t1,t2; +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +# +# MDEV-3903: Server crashes in Item_cond::fix_fields on 2nd execution +# of a prepared stmt with exists_to_in+materialization+semijoin, +# EXISTS subquery, STRAIGHT_JOIN +# +SET optimizer_switch='materialization=on,semijoin=on,exists_to_in=on'; +CREATE TABLE t1 (a INT) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(2); +CREATE TABLE t2 (b INT) ENGINE=MyISAM; +INSERT INTO t2 VALUES (3),(4); +PREPARE stmt FROM +'SELECT STRAIGHT_JOIN * FROM t1 +WHERE EXISTS ( SELECT * FROM t2 WHERE b = a )'; +EXECUTE stmt; +a +EXECUTE stmt; +a +drop table t1,t2; +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +# +# MDEV-4152: Wrong result (missing rows) with exists_to_in=on, +# inner joins +# +SET optimizer_switch='materialization=on,semijoin=on,exists_to_in=on'; +CREATE TABLE t1 (i INT, c1 CHAR(5), c2 CHAR(5), t1_field VARCHAR(1)) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1,'test1','test2','f'), (2,'test3','test4','d'); +CREATE TABLE t2 (t2_field VARCHAR(1)) ENGINE=MyISAM; +INSERT INTO t2 VALUES ('m'), ('b'); +CREATE TABLE t3 (t3_field VARCHAR(1)) ENGINE=MyISAM; +INSERT INTO t3 VALUES ('b'),('c'); +SELECT * FROM t1, t2 outer_t2 +WHERE EXISTS ( SELECT 1 FROM t2, t3 WHERE t3_field = outer_t2.t2_field AND t2_field <= t1_field ); +i c1 c2 t1_field t2_field +1 test1 test2 f b +2 test3 test4 d b +drop table t1,t2,t3; +set optimizer_switch=default; +set optimizer_switch='exists_to_in=on'; +set optimizer_switch=default; diff --git a/mysql-test/r/subselect_exists2in_costmat.result b/mysql-test/r/subselect_exists2in_costmat.result new file mode 100644 index 00000000000..a46996d9bad --- /dev/null +++ b/mysql-test/r/subselect_exists2in_costmat.result @@ -0,0 +1,103 @@ +set @subselect_mat_cost=@@optimizer_switch; +set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on'; +set long_query_time=0.1; +drop database if exists world; +set names utf8; +create database world; +use world; +CREATE TABLE Country ( +Code char(3) NOT NULL default '', +Name char(52) NOT NULL default '', +SurfaceArea float(10,2) NOT NULL default '0.00', +Population int(11) NOT NULL default '0', +Capital int(11) default NULL, +PRIMARY KEY (Code), +UNIQUE INDEX (Name) +); +CREATE TABLE City ( +ID int(11) NOT NULL auto_increment, +Name char(35) NOT NULL default '', +Country char(3) NOT NULL default '', +Population int(11) NOT NULL default '0', +PRIMARY KEY (ID), +INDEX (Population), +INDEX (Country) +); +CREATE TABLE CountryLanguage ( +Country char(3) NOT NULL default '', +Language char(30) NOT NULL default '', +Percentage float(3,1) NOT NULL default '0.0', +PRIMARY KEY (Country, Language), +INDEX (Percentage) +); +Make the schema and data more diverse by adding more indexes, nullable +columns, and NULL data. +create index SurfaceArea on Country(SurfaceArea); +create index Language on CountryLanguage(Language); +create index CityName on City(Name); +alter table City change population population int(11) null default 0; +select max(id) from City into @max_city_id; +insert into City values (@max_city_id + 1,'Kilifarevo','BGR',NULL); +SELECT COUNT(*) FROM Country; +COUNT(*) +239 +SELECT COUNT(*) FROM City; +COUNT(*) +4080 +SELECT COUNT(*) FROM CountryLanguage; +COUNT(*) +984 +set @@optimizer_switch = 'exists_to_in=on,in_to_exists=on,semijoin=on,materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on'; + +1. Subquery in a disjunctive WHERE clause of the outer query. + + +Q1.1m: +MATERIALIZATION: there are too many rows in the outer query +to be looked up in the inner table. +EXPLAIN +SELECT Name FROM Country +WHERE (EXISTS (select 1 from City where City.Population > 100000 and +Code = Country) OR +Name LIKE 'L%') AND +surfacearea > 1000000; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY Country ALL Name,SurfaceArea NULL NULL NULL 239 Using where +2 MATERIALIZED City ALL Population,Country NULL NULL NULL 4080 Using where +SELECT Name FROM Country +WHERE (EXISTS (select 1 from City where City.Population > 100000 and +Code = Country) OR +Name LIKE 'L%') AND +surfacearea > 1000000; +Name +Algeria +Angola +Argentina +Australia +Bolivia +Brazil +Egypt +South Africa +Ethiopia +Indonesia +India +Iran +Canada +Kazakstan +China +Colombia +Congo, The Democratic Republic of the +Libyan Arab Jamahiriya +Mali +Mauritania +Mexico +Mongolia +Niger +Peru +Saudi Arabia +Sudan +Chad +Russian Federation +United States +drop database world; +set optimizer_switch=@subselect_mat_cost; diff --git a/mysql-test/r/subselect_exists_to_in.result b/mysql-test/r/subselect_exists_to_in.result new file mode 100644 index 00000000000..b639fc7eca0 --- /dev/null +++ b/mysql-test/r/subselect_exists_to_in.result @@ -0,0 +1,7021 @@ +select @@optimizer_switch like '%exists_to_in=on%'; +@@optimizer_switch like '%exists_to_in=on%' +0 +set optimizer_switch='exists_to_in=on'; +drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12; +drop view if exists v2; +set @subselect_tmp=@@optimizer_switch; +set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test, +"semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off"); +set join_cache_level=1; +set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on'; +select (select 2); +(select 2) +2 +explain extended select (select 2); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used +Warnings: +Note 1249 Select 2 was reduced during optimization +Note 1003 select 2 AS `(select 2)` +SELECT (SELECT 1) UNION SELECT (SELECT 2); +(SELECT 1) +1 +2 +explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used +3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used +NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL +Warnings: +Note 1249 Select 2 was reduced during optimization +Note 1249 Select 4 was reduced during optimization +Note 1003 select 1 AS `(SELECT 1)` union select 2 AS `(SELECT 2)` +SELECT (SELECT (SELECT 0 UNION SELECT 0)); +(SELECT (SELECT 0 UNION SELECT 0)) +0 +explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0)); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used +3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used +4 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used +NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL NULL +Warnings: +Note 1249 Select 2 was reduced during optimization +Note 1003 select (select 0 union select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))` +SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a; +ERROR 42S22: Reference 'a' not supported (forward reference in item list) +SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b; +ERROR 42S22: Reference 'b' not supported (forward reference in item list) +SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a; +(SELECT 1) MAX(1) +1 1 +SELECT (SELECT a) as a; +ERROR 42S22: Reference 'a' not supported (forward reference in item list) +EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY <derived2> system NULL NULL NULL NULL 1 100.00 +3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used +2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used +Warnings: +Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1 +Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1 +Note 1003 select 1 AS `1` from dual having ((select 1) = 1) +SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1; +1 +1 +SELECT (SELECT 1), a; +ERROR 42S22: Unknown column 'a' in 'field list' +SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1; +a +1 +SELECT 1 FROM (SELECT (SELECT a) b) c; +ERROR 42S22: Unknown column 'a' in 'field list' +SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id); +id +1 +SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1); +ERROR 21000: Operand should contain 1 column(s) +SELECT 1 IN (SELECT 1); +1 IN (SELECT 1) +1 +SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a)); +1 +1 +select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1)); +ERROR HY000: Incorrect usage of PROCEDURE and subquery +SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1)); +ERROR HY000: Incorrect parameters to procedure 'ANALYSE' +SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL; +ERROR 42S22: Unknown column 'a' in 'field list' +SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL; +ERROR 42S22: Unknown column 'a' in 'field list' +SELECT (SELECT 1,2,3) = ROW(1,2,3); +(SELECT 1,2,3) = ROW(1,2,3) +1 +SELECT (SELECT 1,2,3) = ROW(1,2,1); +(SELECT 1,2,3) = ROW(1,2,1) +0 +SELECT (SELECT 1,2,3) < ROW(1,2,1); +(SELECT 1,2,3) < ROW(1,2,1) +0 +SELECT (SELECT 1,2,3) > ROW(1,2,1); +(SELECT 1,2,3) > ROW(1,2,1) +1 +SELECT (SELECT 1,2,3) = ROW(1,2,NULL); +(SELECT 1,2,3) = ROW(1,2,NULL) +NULL +SELECT ROW(1,2,3) = (SELECT 1,2,3); +ROW(1,2,3) = (SELECT 1,2,3) +1 +SELECT ROW(1,2,3) = (SELECT 1,2,1); +ROW(1,2,3) = (SELECT 1,2,1) +0 +SELECT ROW(1,2,3) < (SELECT 1,2,1); +ROW(1,2,3) < (SELECT 1,2,1) +0 +SELECT ROW(1,2,3) > (SELECT 1,2,1); +ROW(1,2,3) > (SELECT 1,2,1) +1 +SELECT ROW(1,2,3) = (SELECT 1,2,NULL); +ROW(1,2,3) = (SELECT 1,2,NULL) +NULL +SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a'); +(SELECT 1.5,2,'a') = ROW(1.5,2,'a') +1 +SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b'); +(SELECT 1.5,2,'a') = ROW(1.5,2,'b') +0 +SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b'); +(SELECT 1.5,2,'a') = ROW('1.5b',2,'b') +0 +Warnings: +Warning 1292 Truncated incorrect DOUBLE value: '1.5b' +SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a'); +(SELECT 'b',2,'a') = ROW(1.5,2,'a') +0 +SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a'); +(SELECT 1.5,2,'a') = ROW(1.5,'2','a') +1 +SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a'); +(SELECT 1.5,'c','a') = ROW(1.5,2,'a') +0 +SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a); +ERROR 21000: Operand should contain 1 column(s) +SELECT 1 as a,(SELECT a+a) b,(SELECT b); +a b (SELECT b) +1 2 2 +create table t1 (a int); +create table t2 (a int, b int); +create table t3 (a int); +create table t4 (a int not null, b int not null); +insert into t1 values (2); +insert into t2 values (1,7),(2,7); +insert into t4 values (4,8),(3,8),(5,9); +select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1; +ERROR 42S22: Reference 'a1' not supported (forward reference in item list) +select (select a from t1 where t1.a=t2.a), a from t2; +(select a from t1 where t1.a=t2.a) a +NULL 1 +2 2 +select (select a from t1 where t1.a=t2.b), a from t2; +(select a from t1 where t1.a=t2.b) a +NULL 1 +NULL 2 +select (select a from t1), a, (select 1 union select 2 limit 1) from t2; +(select a from t1) a (select 1 union select 2 limit 1) +2 1 1 +2 2 1 +select (select a from t3), a from t2; +(select a from t3) a +NULL 1 +NULL 2 +select * from t2 where t2.a=(select a from t1); +a b +2 7 +insert into t3 values (6),(7),(3); +select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1); +a b +1 7 +2 7 +(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 order by a limit 2) limit 3; +a b +1 7 +2 7 +3 8 +(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a); +a b +1 7 +2 7 +4 8 +3 8 +explain extended (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where +2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using filesort +3 UNION t4 ALL NULL NULL NULL NULL 3 100.00 Using where +4 SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 +NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL +Warnings: +Note 1003 (select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`b` = (select `test`.`t3`.`a` from `test`.`t3` order by 1 desc limit 1))) union (select `test`.`t4`.`a` AS `a`,`test`.`t4`.`b` AS `b` from `test`.`t4` where (`test`.`t4`.`b` = (select (max(`test`.`t2`.`a`) * 4) from `test`.`t2`)) order by `a`) +select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2; +(select a from t3 where a<t2.a*4 order by 1 desc limit 1) a +3 1 +7 2 +select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from +(select * from t2 where a>1) as tt; +(select t3.a from t3 where a<8 order by 1 desc limit 1) a +7 2 +set @tmp_optimizer_switch=@@optimizer_switch; +set optimizer_switch='derived_merge=off,derived_with_keys=off'; +explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from +(select * from t2 where a>1) as tt; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY <derived3> ALL NULL NULL NULL NULL 2 100.00 +3 DERIVED t2 ALL NULL NULL NULL NULL 2 100.00 Using where +2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using filesort +Warnings: +Note 1003 select (select `test`.`t3`.`a` from `test`.`t3` where (`test`.`t3`.`a` < 8) order by 1 desc limit 1) AS `(select t3.a from t3 where a<8 order by 1 desc limit 1)`,`tt`.`a` AS `a` from (select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`a` > 1)) `tt` +set optimizer_switch=@tmp_optimizer_switch; +select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1); +a +2 +select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a > t1.a) order by 1 desc limit 1); +a +2 +select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1); +a +select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4; +b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) +8 7.5000 +8 4.5000 +9 7.5000 +explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00 +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 +3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where +Warnings: +Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1 +Note 1003 select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((select avg((`test`.`t2`.`a` + (select min(`test`.`t3`.`a`) from `test`.`t3` where (`test`.`t3`.`a` >= `test`.`t4`.`a`)))) from `test`.`t2`)) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4` +select * from t3 where exists (select * from t2 where t2.b=t3.a); +a +7 +select * from t3 where not exists (select * from t2 where t2.b=t3.a); +a +6 +3 +select * from t3 where a in (select b from t2); +a +7 +select * from t3 where a not in (select b from t2); +a +6 +3 +select * from t3 where a = some (select b from t2); +a +7 +select * from t3 where a <> any (select b from t2); +a +6 +3 +select * from t3 where a = all (select b from t2); +a +7 +select * from t3 where a <> all (select b from t2); +a +6 +3 +insert into t2 values (100, 5); +select * from t3 where a < any (select b from t2); +a +6 +3 +select * from t3 where a < all (select b from t2); +a +3 +select * from t3 where a >= any (select b from t2); +a +6 +7 +explain extended select * from t3 where a >= any (select b from t2); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where +2 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(`test`.`t2`.`b`) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`)))) +select * from t3 where a >= all (select b from t2); +a +7 +delete from t2 where a=100; +select * from t3 where a in (select a,b from t2); +ERROR 21000: Operand should contain 1 column(s) +select * from t3 where a in (select * from t2); +ERROR 21000: Operand should contain 1 column(s) +insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10); +select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b); +b ma +insert into t2 values (2,10); +select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b); +b ma +10 1 +delete from t2 where a=2 and b=10; +select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b); +b ma +7 12 +create table t5 (a int); +select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; +(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a +NULL 1 +2 2 +insert into t5 values (5); +select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; +(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a +NULL 1 +2 2 +insert into t5 values (2); +select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; +(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a +NULL 1 +2 2 +explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 +2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00 +3 DEPENDENT UNION t5 ALL NULL NULL NULL NULL 2 100.00 Using where +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL +Warnings: +Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1 +Note 1003 select <expr_cache><`test`.`t2`.`a`>((select 2 from dual where (2 = `test`.`t2`.`a`) union select `test`.`t5`.`a` from `test`.`t5` where (`test`.`t5`.`a` = `test`.`t2`.`a`))) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2` +select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2; +ERROR 21000: Subquery returns more than 1 row +create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq)); +create table t7( uq int primary key, name char(25)); +insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta"); +insert into t6 values (1,1),(1,2),(2,2),(1,3); +select * from t6 where exists (select * from t7 where uq = clinic_uq); +patient_uq clinic_uq +1 1 +1 2 +2 2 +explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t7 index PRIMARY PRIMARY 4 NULL 2 100.00 Using index +1 PRIMARY t6 ALL i1 NULL NULL NULL 4 75.00 Using where; Using join buffer (flat, BNL join) +Warnings: +Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t7` join `test`.`t6` where (`test`.`t6`.`clinic_uq` = `test`.`t7`.`uq`) +select * from t1 where a= (select a from t2,t4 where t2.b=t4.b); +ERROR 23000: Column 'a' in field list is ambiguous +drop table t1,t2,t3; +CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0'); +INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b'); +CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0'); +INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2'); +CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00'); +INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13'); +SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1); +a b +W 1732-02-22 +SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2); +a b +W 1 +SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3); +a b +W a +CREATE TABLE `t8` ( +`pseudo` varchar(35) character set latin1 NOT NULL default '', +`email` varchar(60) character set latin1 NOT NULL default '', +PRIMARY KEY (`pseudo`), +UNIQUE KEY `email` (`email`) +) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC; +INSERT INTO t8 (pseudo,email) VALUES ('joce','test'); +INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1'); +INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1'); +EXPLAIN EXTENDED SELECT pseudo,(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce')) FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index +4 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index +2 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 +3 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index +Warnings: +Note 1003 select 'joce' AS `pseudo`,(select 'test' from `test`.`t8` where 1) AS `(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'))` from `test`.`t8` where 1 +SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM +t8 WHERE pseudo='joce'); +ERROR 21000: Operand should contain 1 column(s) +SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE +pseudo='joce'); +ERROR 21000: Operand should contain 1 column(s) +SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'); +pseudo +joce +SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%'); +ERROR 21000: Subquery returns more than 1 row +drop table if exists t1,t2,t3,t4,t5,t6,t7,t8; +CREATE TABLE `t1` ( +`topic` mediumint(8) unsigned NOT NULL default '0', +`date` date NOT NULL default '0000-00-00', +`pseudo` varchar(35) character set latin1 NOT NULL default '', +PRIMARY KEY (`pseudo`,`date`,`topic`), +KEY `topic` (`topic`) +) ENGINE=MyISAM ROW_FORMAT=DYNAMIC; +INSERT INTO t1 (topic,date,pseudo) VALUES +('43506','2002-10-02','joce'),('40143','2002-08-03','joce'); +EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index +Warnings: +Note 1003 select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where (`test`.`t1`.`date` = '2002-08-03') +EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used +2 SUBQUERY t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index +Warnings: +Note 1003 select (select distinct `test`.`t1`.`date` from `test`.`t1` where (`test`.`t1`.`date` = '2002-08-03')) AS `(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')` +SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'; +date +2002-08-03 +SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'); +(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03') +2002-08-03 +SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1; +1 +1 +1 +1 +SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1; +ERROR 21000: Subquery returns more than 1 row +EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL topic 3 NULL 2 100.00 Using index +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used +3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL +Warnings: +Note 1003 select 1 AS `1` from `test`.`t1` where (1 = (select 1 union select 1)) +drop table t1; +CREATE TABLE `t1` ( +`numeropost` mediumint(8) unsigned NOT NULL auto_increment, +`maxnumrep` int(10) unsigned NOT NULL default '0', +PRIMARY KEY (`numeropost`), +UNIQUE KEY `maxnumrep` (`maxnumrep`) +) ENGINE=MyISAM ROW_FORMAT=FIXED; +INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2); +CREATE TABLE `t2` ( +`mot` varchar(30) NOT NULL default '', +`topic` mediumint(8) unsigned NOT NULL default '0', +`date` date NOT NULL default '0000-00-00', +`pseudo` varchar(35) NOT NULL default '', +PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`) +) ENGINE=MyISAM ROW_FORMAT=DYNAMIC; +INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce'); +select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1); +a +40143 +SELECT numeropost,maxnumrep FROM t1 WHERE exists (SELECT 1 FROM t2 WHERE (mot='joce') AND date >= '2002-10-21' AND t1.numeropost = t2.topic) ORDER BY maxnumrep DESC LIMIT 0, 20; +numeropost maxnumrep +43506 2 +40143 1 +SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b; +ERROR 42S22: Unknown column 'a' in 'having clause' +SELECT 1 IN (SELECT 1 FROM t2 HAVING a); +ERROR 42S22: Unknown column 'a' in 'having clause' +SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic); +mot topic date pseudo +joce 40143 2002-10-22 joce +joce 43506 2002-10-22 joce +SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); +mot topic date pseudo +SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1); +mot topic date pseudo +SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic); +mot topic date pseudo +joce 40143 2002-10-22 joce +joce 43506 2002-10-22 joce +SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); +mot topic date pseudo +SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1); +mot topic date pseudo +SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic); +mot topic date pseudo +SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); +mot topic date pseudo +joce 40143 2002-10-22 joce +joce 43506 2002-10-22 joce +SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2; +mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) +joce 40143 2002-10-22 joce 1 +joce 43506 2002-10-22 joce 1 +SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2); +mot topic date pseudo +SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2); +mot topic date pseudo +joce 40143 2002-10-22 joce +joce 43506 2002-10-22 joce +SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); +mot topic date pseudo +joce 40143 2002-10-22 joce +SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); +mot topic date pseudo +joce 40143 2002-10-22 joce +SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); +mot topic date pseudo +joce 40143 2002-10-22 joce +SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2; +mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) +joce 40143 2002-10-22 joce 1 +joce 43506 2002-10-22 joce 0 +drop table t1,t2; +CREATE TABLE `t1` ( +`numeropost` mediumint(8) unsigned NOT NULL auto_increment, +`maxnumrep` int(10) unsigned NOT NULL default '0', +PRIMARY KEY (`numeropost`), +UNIQUE KEY `maxnumrep` (`maxnumrep`) +) ENGINE=MyISAM ROW_FORMAT=FIXED; +INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1); +select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1); +ERROR 21000: Subquery returns more than 1 row +select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1); +ERROR 21000: Subquery returns more than 1 row +show warnings; +Level Code Message +Error 1242 Subquery returns more than 1 row +Error 1028 Sort aborted: Subquery returns more than 1 row +drop table t1; +create table t1 (a int); +insert into t1 values (1),(2),(3); +(select * from t1) union (select * from t1) order by (select a from t1 limit 1); +a +1 +2 +3 +drop table t1; +CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b'); +INSERT INTO t1 VALUES (); +SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b'); +ERROR 21000: Subquery returns more than 1 row +drop table t1; +CREATE TABLE `t1` ( +`numeropost` mediumint(8) unsigned NOT NULL default '0', +`numreponse` int(10) unsigned NOT NULL auto_increment, +`pseudo` varchar(35) NOT NULL default '', +PRIMARY KEY (`numeropost`,`numreponse`), +UNIQUE KEY `numreponse` (`numreponse`), +KEY `pseudo` (`pseudo`,`numeropost`) +) ENGINE=MyISAM; +SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a; +ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list) +SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a; +ERROR 42S22: Unknown column 'a' in 'having clause' +SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a; +numreponse (SELECT numeropost FROM t1 HAVING numreponse=1) +INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test'); +EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1'); +ERROR 21000: Subquery returns more than 1 row +SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1'); +ERROR 21000: Subquery returns more than 1 row +EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1'; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +Warnings: +Note 1003 select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where (`test`.`t1`.`numeropost` = '1') +EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1'); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 const PRIMARY,numreponse PRIMARY 7 const,const 1 100.00 Using where; Using index +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +Warnings: +Note 1003 select `test`.`t1`.`numreponse` AS `numreponse` from `test`.`t1` where ((`test`.`t1`.`numeropost` = '1') and (`test`.`t1`.`numreponse` = (select max(`test`.`t1`.`numreponse`) from `test`.`t1` where (`test`.`t1`.`numeropost` = '1')))) +drop table t1; +CREATE TABLE t1 (a int(1)); +INSERT INTO t1 VALUES (1); +SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1; +1 +1 +drop table t1; +create table t1 (a int NOT NULL, b int, primary key (a)); +create table t2 (a int NOT NULL, b int, primary key (a)); +insert into t1 values (0, 10),(1, 11),(2, 12); +insert into t2 values (1, 21),(2, 22),(3, 23); +select * from t1; +a b +0 10 +1 11 +2 12 +update t1 set b= (select b from t1); +ERROR HY000: You can't specify target table 't1' for update in FROM clause +update t1 set b= (select b from t2); +ERROR 21000: Subquery returns more than 1 row +update t1 set b= (select b from t2 where t1.a = t2.a); +select * from t1; +a b +0 NULL +1 21 +2 22 +drop table t1, t2; +create table t1 (a int NOT NULL, b int, primary key (a)); +create table t2 (a int NOT NULL, b int, primary key (a)); +insert into t1 values (0, 10),(1, 11),(2, 12); +insert into t2 values (1, 21),(2, 12),(3, 23); +select * from t1; +a b +0 10 +1 11 +2 12 +select * from t1 where b = (select b from t2 where t1.a = t2.a); +a b +2 12 +delete from t1 where b in (select b from t1); +ERROR HY000: You can't specify target table 't1' for update in FROM clause +delete from t1 where b = (select b from t2); +ERROR 21000: Subquery returns more than 1 row +delete from t1 where b = (select b from t2 where t1.a = t2.a); +select * from t1; +a b +0 10 +1 11 +drop table t1, t2; +create table t11 (a int NOT NULL, b int, primary key (a)); +create table t12 (a int NOT NULL, b int, primary key (a)); +create table t2 (a int NOT NULL, b int, primary key (a)); +insert into t11 values (0, 10),(1, 11),(2, 12); +insert into t12 values (33, 10),(22, 11),(2, 12); +insert into t2 values (1, 21),(2, 12),(3, 23); +select * from t11; +a b +0 10 +1 11 +2 12 +select * from t12; +a b +33 10 +22 11 +2 12 +delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a); +ERROR HY000: You can't specify target table 't12' for update in FROM clause +delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2); +ERROR 21000: Subquery returns more than 1 row +delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a); +select * from t11; +a b +0 10 +1 11 +select * from t12; +a b +33 10 +22 11 +drop table t11, t12, t2; +CREATE TABLE t1 (x int) ENGINE=MyISAM; +create table t2 (a int) ENGINE=MyISAM; +create table t3 (b int); +insert into t2 values (1); +insert into t3 values (1),(2); +INSERT INTO t1 (x) VALUES ((SELECT x FROM t1)); +ERROR HY000: You can't specify target table 't1' for update in FROM clause +INSERT INTO t1 (x) VALUES ((SELECT b FROM t3)); +ERROR 21000: Subquery returns more than 1 row +INSERT INTO t1 (x) VALUES ((SELECT a FROM t2)); +select * from t1; +x +1 +insert into t2 values (1); +INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2)); +select * from t1; +x +1 +2 +INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2; +select * from t1; +x +1 +2 +3 +3 +INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2; +select * from t1; +x +1 +2 +3 +3 +11 +11 +INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2)); +ERROR 42S22: Unknown column 'x' in 'field list' +INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2)); +select * from t1; +x +1 +2 +3 +3 +11 +11 +2 +drop table t1, t2, t3; +CREATE TABLE t1 (x int not null, y int, primary key (x)) ENGINE=MyISAM; +create table t2 (a int); +create table t3 (a int); +insert into t2 values (1); +insert into t3 values (1),(2); +select * from t1; +x y +replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2)); +ERROR HY000: You can't specify target table 't1' for update in FROM clause +replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2)); +ERROR 21000: Subquery returns more than 1 row +replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2)); +select * from t1; +x y +1 2 +replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2)); +select * from t1; +x y +1 3 +replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2)); +select * from t1; +x y +1 3 +4 1 +replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2)); +select * from t1; +x y +1 3 +4 2 +replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2)); +select * from t1; +x y +1 3 +4 2 +2 1 +drop table t1, t2, t3; +SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *); +ERROR HY000: No tables used +CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t2 VALUES (1),(2); +SELECT * FROM t2 WHERE id IN (SELECT 1); +id +1 +EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 ref id id 5 const 1 100.00 Using index +Warnings: +Note 1249 Select 2 was reduced during optimization +Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = 1) +SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3); +id +1 +SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1)); +id +2 +EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1)); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 ref id id 5 const 1 100.00 Using where; Using index +Warnings: +Note 1249 Select 3 was reduced during optimization +Note 1249 Select 2 was reduced during optimization +Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = <cache>((1 + 1))) +EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 index NULL id 5 NULL 2 100.00 Using where; Using index +2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used +3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL +Warnings: +Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where <expr_cache><`test`.`t2`.`id`>(<in_optimizer>(`test`.`t2`.`id`,<exists>(select 1 having (<cache>(`test`.`t2`.`id`) = <ref_null_helper>(1)) union select 3 having (<cache>(`test`.`t2`.`id`) = <ref_null_helper>(3))))) +SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3); +id +SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2); +id +2 +INSERT INTO t2 VALUES ((SELECT * FROM t2)); +ERROR HY000: You can't specify target table 't2' for update in FROM clause +INSERT INTO t2 VALUES ((SELECT id FROM t2)); +ERROR HY000: You can't specify target table 't2' for update in FROM clause +SELECT * FROM t2; +id +1 +2 +CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t1 values (1),(1); +UPDATE t2 SET id=(SELECT * FROM t1); +ERROR 21000: Subquery returns more than 1 row +drop table t2, t1; +create table t1 (a int); +insert into t1 values (1),(2),(3); +select 1 IN (SELECT * from t1); +1 IN (SELECT * from t1) +1 +select 10 IN (SELECT * from t1); +10 IN (SELECT * from t1) +0 +select NULL IN (SELECT * from t1); +NULL IN (SELECT * from t1) +NULL +update t1 set a=NULL where a=2; +select 1 IN (SELECT * from t1); +1 IN (SELECT * from t1) +1 +select 3 IN (SELECT * from t1); +3 IN (SELECT * from t1) +1 +select 10 IN (SELECT * from t1); +10 IN (SELECT * from t1) +NULL +select 1 > ALL (SELECT * from t1); +1 > ALL (SELECT * from t1) +0 +select 10 > ALL (SELECT * from t1); +10 > ALL (SELECT * from t1) +NULL +select 1 > ANY (SELECT * from t1); +1 > ANY (SELECT * from t1) +NULL +select 10 > ANY (SELECT * from t1); +10 > ANY (SELECT * from t1) +1 +drop table t1; +create table t1 (a varchar(20)); +insert into t1 values ('A'),('BC'),('DEF'); +select 'A' IN (SELECT * from t1); +'A' IN (SELECT * from t1) +1 +select 'XYZS' IN (SELECT * from t1); +'XYZS' IN (SELECT * from t1) +0 +select NULL IN (SELECT * from t1); +NULL IN (SELECT * from t1) +NULL +update t1 set a=NULL where a='BC'; +select 'A' IN (SELECT * from t1); +'A' IN (SELECT * from t1) +1 +select 'DEF' IN (SELECT * from t1); +'DEF' IN (SELECT * from t1) +1 +select 'XYZS' IN (SELECT * from t1); +'XYZS' IN (SELECT * from t1) +NULL +select 'A' > ALL (SELECT * from t1); +'A' > ALL (SELECT * from t1) +0 +select 'XYZS' > ALL (SELECT * from t1); +'XYZS' > ALL (SELECT * from t1) +NULL +select 'A' > ANY (SELECT * from t1); +'A' > ANY (SELECT * from t1) +NULL +select 'XYZS' > ANY (SELECT * from t1); +'XYZS' > ANY (SELECT * from t1) +1 +drop table t1; +create table t1 (a float); +insert into t1 values (1.5),(2.5),(3.5); +select 1.5 IN (SELECT * from t1); +1.5 IN (SELECT * from t1) +1 +select 10.5 IN (SELECT * from t1); +10.5 IN (SELECT * from t1) +0 +select NULL IN (SELECT * from t1); +NULL IN (SELECT * from t1) +NULL +update t1 set a=NULL where a=2.5; +select 1.5 IN (SELECT * from t1); +1.5 IN (SELECT * from t1) +1 +select 3.5 IN (SELECT * from t1); +3.5 IN (SELECT * from t1) +1 +select 10.5 IN (SELECT * from t1); +10.5 IN (SELECT * from t1) +NULL +select 1.5 > ALL (SELECT * from t1); +1.5 > ALL (SELECT * from t1) +0 +select 10.5 > ALL (SELECT * from t1); +10.5 > ALL (SELECT * from t1) +NULL +select 1.5 > ANY (SELECT * from t1); +1.5 > ANY (SELECT * from t1) +NULL +select 10.5 > ANY (SELECT * from t1); +10.5 > ANY (SELECT * from t1) +1 +explain extended select (select a+1) from t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1249 Select 2 was reduced during optimization +Note 1003 select (`test`.`t1`.`a` + 1) AS `(select a+1)` from `test`.`t1` +select (select a+1) from t1; +(select a+1) +2.5 +NULL +4.5 +drop table t1; +CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY (a)); +CREATE TABLE t2 (a int(11) default '0', INDEX (a)); +INSERT INTO t1 VALUES (1),(2),(3),(4); +INSERT INTO t2 VALUES (1),(2),(3); +SELECT t1.a, t1.a in (select t2.a from t2) FROM t1; +a t1.a in (select t2.a from t2) +1 1 +2 1 +3 1 +4 0 +explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index +2 SUBQUERY t2 index_subquery a a 5 func 2 100.00 Using index +Warnings: +Note 1003 select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t2 on a checking NULL having <is_not_null_test>(`test`.`t2`.`a`))))) AS `t1.a in (select t2.a from t2)` from `test`.`t1` +CREATE TABLE t3 (a int(11) default '0'); +INSERT INTO t3 VALUES (1),(2),(3); +SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1; +a t1.a in (select t2.a from t2,t3 where t3.a=t2.a) +1 1 +2 1 +3 1 +4 0 +explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index +2 DEPENDENT SUBQUERY t2 ref_or_null a a 5 func 2 100.00 Using where; Using index +2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t2`.`a` from `test`.`t2` join `test`.`t3` where ((`test`.`t3`.`a` = `test`.`t2`.`a`) and ((<cache>(`test`.`t1`.`a`) = `test`.`t2`.`a`) or isnull(`test`.`t2`.`a`))) having <is_not_null_test>(`test`.`t2`.`a`)))) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1` +drop table t1,t2,t3; +# check correct NULL Processing for normal IN/ALL/ANY +# and 2 ways of max/min optimization +create table t1 (a int); +insert into t1 values (1), (100), (NULL), (1000); +create table t2 (a int not null); +# subselect returns empty set (for NULL and non-NULL left part) +select a, a in (select * from t2) from t1; +a a in (select * from t2) +1 0 +100 0 +NULL 0 +1000 0 +select a, a > any (select * from t2) from t1; +a a > any (select * from t2) +1 0 +100 0 +NULL 0 +1000 0 +select a, a > all (select * from t2) from t1; +a a > all (select * from t2) +1 1 +100 1 +NULL 1 +1000 1 +select a from t1 where a in (select * from t2); +a +select a from t1 where a > any (select * from t2); +a +select a from t1 where a > all (select * from t2); +a +1 +100 +NULL +1000 +select a from t1 where a in (select * from t2 group by a); +a +select a from t1 where a > any (select * from t2 group by a); +a +select a from t1 where a > all (select * from t2 group by a); +a +1 +100 +NULL +1000 +insert into t2 values (1),(200); +# sebselect returns non-empty set without NULLs +select a, a in (select * from t2) from t1; +a a in (select * from t2) +1 1 +100 0 +NULL NULL +1000 0 +select a, a > any (select * from t2) from t1; +a a > any (select * from t2) +1 0 +100 1 +NULL NULL +1000 1 +select a, a > all (select * from t2) from t1; +a a > all (select * from t2) +1 0 +100 0 +NULL NULL +1000 1 +select a from t1 where a in (select * from t2); +a +1 +select a from t1 where a > any (select * from t2); +a +100 +1000 +select a from t1 where a > all (select * from t2); +a +1000 +select a from t1 where a in (select * from t2 group by a); +a +1 +select a from t1 where a > any (select * from t2 group by a); +a +100 +1000 +select a from t1 where a > all (select * from t2 group by a); +a +1000 +drop table t2; +create table t2 (a int); +insert into t2 values (1),(NULL),(200); +# sebselect returns non-empty set with NULLs +select a, a in (select * from t2) from t1; +a a in (select * from t2) +1 1 +100 NULL +NULL NULL +1000 NULL +select a, a > any (select * from t2) from t1; +a a > any (select * from t2) +1 NULL +100 1 +NULL NULL +1000 1 +select a, a > all (select * from t2) from t1; +a a > all (select * from t2) +1 0 +100 0 +NULL NULL +1000 NULL +select a from t1 where a in (select * from t2); +a +1 +select a from t1 where a > any (select * from t2); +a +100 +1000 +select a from t1 where a > all (select * from t2); +a +select a from t1 where a in (select * from t2 group by a); +a +1 +select a from t1 where a > any (select * from t2 group by a); +a +100 +1000 +select a from t1 where a > all (select * from t2 group by a); +a +drop table t1, t2; +create table t1 (a float); +select 10.5 IN (SELECT * from t1 LIMIT 1); +ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' +select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5); +ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' +drop table t1; +create table t1 (a int, b int, c varchar(10)); +create table t2 (a int); +insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c'); +insert into t2 values (1),(2),(NULL); +select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),(select c from t1 where a=t2.a) from t2; +a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a') (select c from t1 where a=t2.a) +1 1 a +2 0 b +NULL NULL NULL +select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b'),(select c from t1 where a=t2.a) from t2; +a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b') (select c from t1 where a=t2.a) +1 0 a +2 1 b +NULL NULL NULL +select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c'),(select c from t1 where a=t2.a) from t2; +a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c') (select c from t1 where a=t2.a) +1 0 a +2 0 b +NULL NULL NULL +drop table t1,t2; +create table t1 (a int, b real, c varchar(10)); +insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b'); +select ROW(1, 1, 'a') IN (select a,b,c from t1); +ROW(1, 1, 'a') IN (select a,b,c from t1) +1 +select ROW(1, 2, 'a') IN (select a,b,c from t1); +ROW(1, 2, 'a') IN (select a,b,c from t1) +0 +select ROW(1, 1, 'a') IN (select b,a,c from t1); +ROW(1, 1, 'a') IN (select b,a,c from t1) +1 +select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null); +ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null) +1 +select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null); +ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null) +0 +select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null); +ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null) +1 +select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a'); +ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a') +1 +select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a'); +ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a') +0 +select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a'); +ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a') +1 +select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2); +ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' +drop table t1; +create table t1 (a int); +insert into t1 values (1); +do @a:=(SELECT a from t1); +select @a; +@a +1 +set @a:=2; +set @a:=(SELECT a from t1); +select @a; +@a +1 +drop table t1; +do (SELECT a from t1); +ERROR 42S02: Table 'test.t1' doesn't exist +set @a:=(SELECT a from t1); +ERROR 42S02: Table 'test.t1' doesn't exist +CREATE TABLE t1 (a int, KEY(a)); +HANDLER t1 OPEN; +HANDLER t1 READ a=((SELECT 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 'SELECT 1))' at line 1 +HANDLER t1 CLOSE; +drop table t1; +create table t1 (a int); +create table t2 (b int); +insert into t1 values (1),(2); +insert into t2 values (1); +select a from t1 where a in (select a from t1 where a in (select b from t2)); +a +1 +drop table t1, t2; +create table t1 (a int, b int); +create table t2 like t1; +insert into t1 values (1,2),(1,3),(1,4),(1,5); +insert into t2 values (1,2),(1,3); +select * from t1 where row(a,b) in (select a,b from t2); +a b +1 2 +1 3 +drop table t1, t2; +CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t1 VALUES (1); +UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); +select * from t1; +i +2 +drop table t1; +CREATE TABLE t1 (a int(1)); +EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found +2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found +Warnings: +Note 1003 select (select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1` +EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found +2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found +Warnings: +Note 1003 select (select encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1` +EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found +2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found +Warnings: +Note 1003 select (select benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1` +drop table t1; +CREATE TABLE `t1` ( +`mot` varchar(30) character set latin1 NOT NULL default '', +`topic` mediumint(8) unsigned NOT NULL default '0', +`date` date NOT NULL default '0000-00-00', +`pseudo` varchar(35) character set latin1 NOT NULL default '', +PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`), +KEY `pseudo` (`pseudo`,`date`,`topic`), +KEY `topic` (`topic`) +) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC; +CREATE TABLE `t2` ( +`mot` varchar(30) character set latin1 NOT NULL default '', +`topic` mediumint(8) unsigned NOT NULL default '0', +`date` date NOT NULL default '0000-00-00', +`pseudo` varchar(35) character set latin1 NOT NULL default '', +PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`), +KEY `pseudo` (`pseudo`,`date`,`topic`), +KEY `topic` (`topic`) +) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC; +CREATE TABLE `t3` ( +`numeropost` mediumint(8) unsigned NOT NULL auto_increment, +`maxnumrep` int(10) unsigned NOT NULL default '0', +PRIMARY KEY (`numeropost`), +UNIQUE KEY `maxnumrep` (`maxnumrep`) +) ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t1 VALUES ('joce','1','','joce'),('test','2','','test'); +Warnings: +Warning 1265 Data truncated for column 'date' at row 1 +Warning 1265 Data truncated for column 'date' at row 2 +INSERT INTO t2 VALUES ('joce','1','','joce'),('test','2','','test'); +Warnings: +Warning 1265 Data truncated for column 'date' at row 1 +Warning 1265 Data truncated for column 'date' at row 2 +INSERT INTO t3 VALUES (1,1); +SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE +numeropost=topic); +topic +2 +select * from t1; +mot topic date pseudo +joce 1 0000-00-00 joce +test 2 0000-00-00 test +DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT +EXISTS(SELECT * FROM t3 WHERE numeropost=topic)); +select * from t1; +mot topic date pseudo +joce 1 0000-00-00 joce +drop table t1, t2, t3; +SELECT * FROM (SELECT 1 as a,(SELECT a)) a; +a (SELECT a) +1 1 +CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(1) NOT NULL DEFAULT '0', + `(SELECT 1)` int(1) NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(1) NOT NULL DEFAULT '0', + `(SELECT a)` int(1) NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(1) NOT NULL DEFAULT '0', + `(SELECT a+0)` int(3) NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a; +select * from t1; +a +2 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` bigint(20) NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +create table t1 (a int); +insert into t1 values (1), (2), (3); +explain extended select a,(select (select rand() from t1 limit 1) from t1 limit 1) +from t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 +2 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00 +3 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00 +Warnings: +Note 1003 select `test`.`t1`.`a` AS `a`,(select (select rand() from `test`.`t1` limit 1) from `test`.`t1` limit 1) AS `(select (select rand() from t1 limit 1) from t1 limit 1)` from `test`.`t1` +drop table t1; +select t1.Continent, t2.Name, t2.Population from t1 LEFT JOIN t2 ON t1.Code = t2.Country where t2.Population IN (select max(t2.Population) AS Population from t2, t1 where t2.Country = t1.Code group by Continent); +ERROR 42S02: Table 'test.t1' doesn't exist +CREATE TABLE t1 ( +ID int(11) NOT NULL auto_increment, +name char(35) NOT NULL default '', +t2 char(3) NOT NULL default '', +District char(20) NOT NULL default '', +Population int(11) NOT NULL default '0', +PRIMARY KEY (ID) +) ENGINE=MyISAM; +INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207); +INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329); +INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117); +CREATE TABLE t2 ( +Code char(3) NOT NULL default '', +Name char(52) NOT NULL default '', +Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia', +Region char(26) NOT NULL default '', +SurfaceArea float(10,2) NOT NULL default '0.00', +IndepYear smallint(6) default NULL, +Population int(11) NOT NULL default '0', +LifeExpectancy float(3,1) default NULL, +GNP float(10,2) default NULL, +GNPOld float(10,2) default NULL, +LocalName char(45) NOT NULL default '', +GovernmentForm char(45) NOT NULL default '', +HeadOfState char(60) default NULL, +Capital int(11) default NULL, +Code2 char(2) NOT NULL default '', +PRIMARY KEY (Code) +) ENGINE=MyISAM; +INSERT INTO t2 VALUES ('AUS','Australia','Oceania','Australia and New Zealand',7741220.00,1901,18886000,79.8,351182.00,392911.00,'Australia','Constitutional Monarchy, Federation','Elisabeth II',135,'AU'); +INSERT INTO t2 VALUES ('AZE','Azerbaijan','Asia','Middle East',86600.00,1991,7734000,62.9,4127.00,4100.00,'Azärbaycan','Federal Republic','Heydär Äliyev',144,'AZ'); +select t2.Continent, t1.Name, t1.Population from t2 LEFT JOIN t1 ON t2.Code = t1.t2 where t1.Population IN (select max(t1.Population) AS Population from t1, t2 where t1.t2 = t2.Code group by Continent); +Continent Name Population +Oceania Sydney 3276207 +drop table t1, t2; +CREATE TABLE `t1` ( +`id` mediumint(8) unsigned NOT NULL auto_increment, +`pseudo` varchar(35) character set latin1 NOT NULL default '', +PRIMARY KEY (`id`), +UNIQUE KEY `pseudo` (`pseudo`) +) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC; +INSERT INTO t1 (pseudo) VALUES ('test'); +SELECT 0 IN (SELECT 1 FROM t1 a); +0 IN (SELECT 1 FROM t1 a) +0 +EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +Warnings: +Note 1003 select <in_optimizer>(0,<exists>(select 1 from dual where (0 = 1))) AS `0 IN (SELECT 1 FROM t1 a)` +INSERT INTO t1 (pseudo) VALUES ('test1'); +SELECT 0 IN (SELECT 1 FROM t1 a); +0 IN (SELECT 1 FROM t1 a) +0 +EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +Warnings: +Note 1003 select <in_optimizer>(0,<exists>(select 1 from `test`.`t1` `a` where (0 = 1))) AS `0 IN (SELECT 1 FROM t1 a)` +drop table t1; +CREATE TABLE `t1` ( +`i` int(11) NOT NULL default '0', +PRIMARY KEY (`i`) +) ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t1 VALUES (1); +UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i)); +UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); +UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); +ERROR 42S22: Unknown column 't.i' in 'field list' +select * from t1; +i +3 +drop table t1; +CREATE TABLE t1 ( +id int(11) default NULL +) ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3); +CREATE TABLE t2 ( +id int(11) default NULL, +name varchar(15) default NULL +) ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita'); +update t1, t2 set t2.name='lenka' where t2.id in (select id from t1); +select * from t2; +id name +4 vita +1 lenka +2 lenka +1 lenka +drop table t1,t2; +create table t1 (a int, unique index indexa (a)); +insert into t1 values (-1), (-4), (-2), (NULL); +select -10 IN (select a from t1 FORCE INDEX (indexa)); +-10 IN (select a from t1 FORCE INDEX (indexa)) +NULL +drop table t1; +create table t1 (id int not null auto_increment primary key, salary int, key(salary)); +insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000); +explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ref salary salary 5 const 0 0.00 Using where +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +Warnings: +Note 1003 select `test`.`t1`.`id` AS `id` from `test`.`t1` where (`test`.`t1`.`salary` = (select max(`test`.`t1`.`salary`) from `test`.`t1`)) +drop table t1; +CREATE TABLE t1 ( +ID int(10) unsigned NOT NULL auto_increment, +SUB_ID int(3) unsigned NOT NULL default '0', +REF_ID int(10) unsigned default NULL, +REF_SUB int(3) unsigned default '0', +PRIMARY KEY (ID,SUB_ID), +UNIQUE KEY t1_PK (ID,SUB_ID), +KEY t1_FK (REF_ID,REF_SUB), +KEY t1_REFID (REF_ID) +) ENGINE=MyISAM CHARSET=cp1251; +INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL); +SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2); +REF_ID +DROP TABLE t1; +create table t1 (a int, b int); +create table t2 (a int, b int); +insert into t1 values (1,0), (2,0), (3,0); +insert into t2 values (1,1), (2,1), (3,1), (2,2); +update ignore t1 set b=(select b from t2 where t1.a=t2.a); +Warnings: +Warning 1242 Subquery returns more than 1 row +select * from t1; +a b +1 1 +2 NULL +3 1 +drop table t1, t2; +CREATE TABLE `t1` ( +`id` mediumint(8) unsigned NOT NULL auto_increment, +`pseudo` varchar(35) NOT NULL default '', +`email` varchar(60) NOT NULL default '', +PRIMARY KEY (`id`), +UNIQUE KEY `email` (`email`), +UNIQUE KEY `pseudo` (`pseudo`) +) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC; +INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1'); +SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1); +a b +test test +test1 test1 +drop table if exists t1; +(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0); +a +1 +create table t1 (a int not null, b int, primary key (a)); +create table t2 (a int not null, primary key (a)); +create table t3 (a int not null, b int, primary key (a)); +insert into t1 values (1,10), (2,20), (3,30), (4,40); +insert into t2 values (2), (3), (4), (5); +insert into t3 values (10,3), (20,4), (30,5); +select * from t2 where t2.a in (select a from t1); +a +2 +3 +4 +explain extended select * from t2 where t2.a in (select a from t1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index +1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 4 75.00 Using where; Using index; Using join buffer (flat, BNL join) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`) +select * from t2 where t2.a in (select a from t1 where t1.b <> 30); +a +2 +4 +explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index +1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 4 75.00 Using where; Using join buffer (flat, BNL join) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30)) +select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); +a +2 +3 +explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index +1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 4 75.00 Using where; Using join buffer (flat, BNL join) +1 PRIMARY t3 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 100.00 Using index +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t3` join `test`.`t2` where ((`test`.`t3`.`a` = `test`.`t1`.`b`) and (`test`.`t1`.`a` = `test`.`t2`.`a`)) +drop table t1, t2, t3; +create table t1 (a int, b int, index a (a,b)); +create table t2 (a int, index a (a)); +create table t3 (a int, b int, index a (a)); +insert into t1 values (1,10), (2,20), (3,30), (4,40); +create table t0(a int); +insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); +insert into t1 +select rand()*100000+200,rand()*100000 from t0 A, t0 B, t0 C, t0 D; +insert into t2 values (2), (3), (4), (5); +insert into t3 values (10,3), (20,4), (30,5); +select * from t2 where t2.a in (select a from t1); +a +2 +3 +4 +explain extended select * from t2 where t2.a in (select a from t1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index +1 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using index; FirstMatch(t2) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where (`test`.`t1`.`a` = `test`.`t2`.`a`) +select * from t2 where t2.a in (select a from t1 where t1.b <> 30); +a +2 +4 +explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index +1 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using where; Using index; FirstMatch(t2) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30)) +select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); +a +2 +3 +explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index +1 PRIMARY t3 index a a 5 NULL 3 100.00 Using where; Using index +1 PRIMARY t1 ref a a 10 test.t2.a,test.t3.a 116 100.00 Using index; FirstMatch(t2) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1` join `test`.`t3`) where ((`test`.`t1`.`b` = `test`.`t3`.`a`) and (`test`.`t1`.`a` = `test`.`t2`.`a`)) +insert into t1 values (3,31); +select * from t2 where t2.a in (select a from t1 where t1.b <> 30); +a +2 +3 +4 +select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31); +a +2 +4 +explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index +1 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using where; Using index; FirstMatch(t2) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30)) +drop table t0, t1, t2, t3; +create table t1 (a int, b int); +create table t2 (a int, b int); +create table t3 (a int, b int); +insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10); +insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1); +insert into t3 values (3,3), (2,2), (1,1); +select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3; +a (select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) +3 1 +2 2 +1 2 +drop table t1,t2,t3; +create table t1 (s1 int); +create table t2 (s1 int); +insert into t1 values (1); +insert into t2 values (1); +select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1); +s1 +1 +drop table t1,t2; +create table t1 (s1 int); +create table t2 (s1 int); +insert into t1 values (1); +insert into t2 values (1); +update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A); +ERROR 42S22: Unknown column 'x.s1' in 'field list' +DROP TABLE t1, t2; +CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci, +s2 CHAR(5) COLLATE latin1_swedish_ci); +INSERT INTO t1 VALUES ('z','?'); +select * from t1 where s1 > (select max(s2) from t1); +ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>' +select * from t1 where s1 > any (select max(s2) from t1); +ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<' +drop table t1; +create table t1(toid int,rd int); +create table t2(userid int,pmnew int,pmtotal int); +insert into t2 values(1,0,0),(2,0,0); +insert into t1 values(1,0),(1,0),(1,0),(1,12),(1,15),(1,123),(1,12312),(1,12312),(1,123),(2,0),(2,0),(2,1),(2,2); +select userid,pmtotal,pmnew, (select count(rd) from t1 where toid=t2.userid) calc_total, (select count(rd) from t1 where rd=0 and toid=t2.userid) calc_new from t2 where userid in (select distinct toid from t1); +userid pmtotal pmnew calc_total calc_new +1 0 0 9 3 +2 0 0 4 2 +drop table t1, t2; +create table t1 (s1 char(5)); +select (select 'a','b' from t1 union select 'a','b' from t1) from t1; +ERROR 21000: Operand should contain 1 column(s) +insert into t1 values ('tttt'); +select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1); +s1 +tttt +explain extended (select * from t1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 system NULL NULL NULL NULL 1 100.00 +Warnings: +Note 1003 (select 'tttt' AS `s1` from dual) +(select * from t1); +s1 +tttt +drop table t1; +create table t1 (s1 char(5), index s1(s1)); +create table t2 (s1 char(5), index s1(s1)); +insert into t1 values ('a1'),('a2'),('a3'); +insert into t2 values ('a1'),('a2'); +select s1, s1 NOT IN (SELECT s1 FROM t2) from t1; +s1 s1 NOT IN (SELECT s1 FROM t2) +a1 0 +a2 0 +a3 1 +select s1, s1 = ANY (SELECT s1 FROM t2) from t1; +s1 s1 = ANY (SELECT s1 FROM t2) +a1 1 +a2 1 +a3 0 +select s1, s1 <> ALL (SELECT s1 FROM t2) from t1; +s1 s1 <> ALL (SELECT s1 FROM t2) +a1 0 +a2 0 +a3 1 +select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1; +s1 s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') +a1 0 +a2 1 +a3 1 +explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index +2 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key +Warnings: +Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1` +explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index +2 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key +Warnings: +Note 1003 select `test`.`t1`.`s1` AS `s1`,<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1` +explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index +2 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key +Warnings: +Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1` +explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index +2 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Using where; Full scan on NULL key +Warnings: +Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL where (`test`.`t2`.`s1` < 'a2') having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1` +drop table t1,t2; +create table t2 (a int, b int not null); +create table t3 (a int); +insert into t3 values (6),(7),(3); +select * from t3 where a >= all (select b from t2); +a +6 +7 +3 +explain extended select * from t3 where a >= all (select b from t2); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where +2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,((select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`)))) +select * from t3 where a >= some (select b from t2); +a +explain extended select * from t3 where a >= some (select b from t2); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where +2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`)))) +select * from t3 where a >= all (select b from t2 group by 1); +a +6 +7 +3 +explain extended select * from t3 where a >= all (select b from t2 group by 1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where +2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,((select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`)))) +select * from t3 where a >= some (select b from t2 group by 1); +a +explain extended select * from t3 where a >= some (select b from t2 group by 1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where +2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`)))) +select * from t3 where NULL >= any (select b from t2); +a +explain extended select * from t3 where NULL >= any (select b from t2); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 +select * from t3 where NULL >= any (select b from t2 group by 1); +a +explain extended select * from t3 where NULL >= any (select b from t2 group by 1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 +select * from t3 where NULL >= some (select b from t2); +a +explain extended select * from t3 where NULL >= some (select b from t2); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 +select * from t3 where NULL >= some (select b from t2 group by 1); +a +explain extended select * from t3 where NULL >= some (select b from t2 group by 1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 +insert into t2 values (2,2), (2,1), (3,3), (3,1); +select * from t3 where a > all (select max(b) from t2 group by a); +a +6 +7 +explain extended select * from t3 where a > all (select max(b) from t2 group by a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where +2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 100.00 Using temporary +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,(<max>(select max(`test`.`t2`.`b`) from `test`.`t2` group by `test`.`t2`.`a`) >= <cache>(`test`.`t3`.`a`)))) +drop table t2, t3; +CREATE TABLE `t1` ( `id` mediumint(9) NOT NULL auto_increment, `taskid` bigint(20) NOT NULL default '0', `dbid` int(11) NOT NULL default '0', `create_date` datetime NOT NULL default '0000-00-00 00:00:00', `last_update` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (`id`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=3 ; +INSERT INTO `t1` (`id`, `taskid`, `dbid`, `create_date`,`last_update`) VALUES (1, 1, 15, '2003-09-29 10:31:36', '2003-09-29 10:31:36'), (2, 1, 21, now(), now()); +CREATE TABLE `t2` (`db_id` int(11) NOT NULL auto_increment,`name` varchar(200) NOT NULL default '',`primary_uid` smallint(6) NOT NULL default '0',`secondary_uid` smallint(6) NOT NULL default '0',PRIMARY KEY (`db_id`),UNIQUE KEY `name_2` (`name`),FULLTEXT KEY `name` (`name`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2147483647; +INSERT INTO `t2` (`db_id`, `name`, `primary_uid`, `secondary_uid`) VALUES (18, 'Not Set 1', 0, 0),(19, 'Valid', 1, 2),(20, 'Valid 2', 1, 2),(21, 'Should Not Return', 1, 2),(26, 'Not Set 2', 0, 0),(-1, 'ALL DB\'S', 0, 0); +CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(11) NOT NULL default '0',`taskid` int(11) NOT NULL default '0',`mon` tinyint(4) NOT NULL default '1',`tues` tinyint(4) NOT NULL default '1',`wed` tinyint(4) NOT NULL default '1',`thur` tinyint(4) NOT NULL default '1',`fri` tinyint(4) NOT NULL default '1',`sat` tinyint(4) NOT NULL default '0',`sun` tinyint(4) NOT NULL default '0',`how_often` smallint(6) NOT NULL default '1',`userid` smallint(6) NOT NULL default '0',`active` tinyint(4) NOT NULL default '1',PRIMARY KEY (`taskgenid`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2 ; +INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1); +CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1; +INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status'); +select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid; +dbid name (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') +-1 Valid 1 +-1 Valid 2 1 +-1 Should Not Return 0 +SELECT dbid, name FROM t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND ((date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')) AND t4.task_id = taskid; +dbid name +-1 Valid +-1 Valid 2 +drop table t1,t2,t3,t4; +CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t1 VALUES (1),(5); +CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t2 VALUES (2),(6); +select * from t1 where (1,2,6) in (select * from t2); +ERROR 21000: Operand should contain 3 column(s) +DROP TABLE t1,t2; +create table t1 (s1 int); +insert into t1 values (1); +insert into t1 values (2); +set sort_buffer_size = (select s1 from t1); +ERROR 21000: Subquery returns more than 1 row +do (select * from t1); +Warnings: +Error 1242 Subquery returns more than 1 row +drop table t1; +create table t1 (s1 char); +insert into t1 values ('e'); +select * from t1 where 'f' > any (select s1 from t1); +s1 +e +select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1); +s1 +e +explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00 +2 SUBQUERY t1 system NULL NULL NULL NULL 1 100.00 +3 UNION t1 system NULL NULL NULL NULL 1 100.00 +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL +Warnings: +Note 1003 select 'e' AS `s1` from dual where 1 +drop table t1; +CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874'); +CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1; +INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6'); +select c.number as phone,(select p.code from t2 p where c.number like concat(p.code, '%') order by length(p.code) desc limit 1) as code from t1 c; +phone code +69294728265 6 +18621828126 1862 +89356874041 NULL +95895001874 NULL +drop table t1, t2; +create table t1 (s1 int); +create table t2 (s1 int); +select * from t1 where (select count(*) from t2 where t1.s2) = 1; +ERROR 42S22: Unknown column 't1.s2' in 'where clause' +select * from t1 where (select count(*) from t2 group by t1.s2) = 1; +ERROR 42S22: Unknown column 't1.s2' in 'group statement' +select count(*) from t2 group by t1.s2; +ERROR 42S22: Unknown column 't1.s2' in 'group statement' +drop table t1, t2; +CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB)); +CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA)); +INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365'); +INSERT INTO t2 VALUES (100, 200, 'C'); +SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1); +COLC +DROP TABLE t1, t2; +CREATE TABLE t1 (a int(1)); +INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5); +SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100; +(SELECT a) +1 +2 +3 +4 +5 +DROP TABLE t1; +create table t1 (a int, b decimal(13, 3)); +insert into t1 values (1, 0.123); +select a, (select max(b) from t1) into outfile "../../tmp/subselect.out.file.1" from t1; +delete from t1; +load data infile "../../tmp/subselect.out.file.1" into table t1; +select * from t1; +a b +1 0.123 +drop table t1; +CREATE TABLE `t1` ( +`id` int(11) NOT NULL auto_increment, +`id_cns` tinyint(3) unsigned NOT NULL default '0', +`tipo` enum('','UNO','DUE') NOT NULL default '', +`anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000', +`particolare` mediumint(8) unsigned NOT NULL default '0', +`generale` mediumint(8) unsigned NOT NULL default '0', +`bis` tinyint(3) unsigned NOT NULL default '0', +PRIMARY KEY (`id`), +UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`), +UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`) +); +INSERT INTO `t1` VALUES (1,16,'UNO',1987,2048,9681,0),(2,50,'UNO',1987,1536,13987,0),(3,16,'UNO',1987,2432,14594,0),(4,16,'UNO',1987,1792,13422,0),(5,16,'UNO',1987,1025,10240,0),(6,16,'UNO',1987,1026,7089,0); +CREATE TABLE `t2` ( +`id` tinyint(3) unsigned NOT NULL auto_increment, +`max_anno_dep` smallint(6) unsigned NOT NULL default '0', +PRIMARY KEY (`id`) +); +INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990); +SELECT cns.id, cns.max_anno_dep, cns.max_anno_dep = (SELECT s.anno_dep FROM t1 AS s WHERE s.id_cns = cns.id ORDER BY s.anno_dep DESC LIMIT 1) AS PIPPO FROM t2 AS cns; +id max_anno_dep PIPPO +16 1987 1 +50 1990 0 +51 1990 NULL +DROP TABLE t1, t2; +create table t1 (a int); +insert into t1 values (1), (2), (3); +SET SQL_SELECT_LIMIT=1; +select sum(a) from (select * from t1) as a; +sum(a) +6 +select 2 in (select * from t1); +2 in (select * from t1) +1 +SET SQL_SELECT_LIMIT=default; +drop table t1; +CREATE TABLE t1 (a int, b int, INDEX (a)); +INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3); +SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b; +a b +1 1 +1 2 +1 3 +DROP TABLE t1; +create table t1(val varchar(10)); +insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp'); +select count(*) from t1 as w1 where w1.val in (select w2.val from t1 as w2 where w2.val like 'm%') and w1.val in (select w3.val from t1 as w3 where w3.val like 'e%'); +count(*) +0 +drop table t1; +create table t1 (id int not null, text varchar(20) not null default '', primary key (id)); +insert into t1 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text11'), (12, 'text12'); +select * from t1 where id not in (select id from t1 where id < 8); +id text +8 text8 +9 text9 +10 text10 +11 text11 +12 text12 +select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null); +id text +8 text8 +9 text9 +10 text10 +11 text11 +12 text12 +explain extended select * from t1 where id not in (select id from t1 where id < 8); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 12 100.00 Using where +2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 100.00 Using index; Using where +Warnings: +Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`text` AS `text` from `test`.`t1` where (not(<expr_cache><`test`.`t1`.`id`>(<in_optimizer>(`test`.`t1`.`id`,<exists>(<primary_index_lookup>(<cache>(`test`.`t1`.`id`) in t1 on PRIMARY where ((`test`.`t1`.`id` < 8) and (<cache>(`test`.`t1`.`id`) = `test`.`t1`.`id`)))))))) +explain extended select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY tt ALL NULL NULL NULL NULL 12 100.00 Using where +2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index +Warnings: +Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<in_optimizer>(1,<expr_cache><`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null)))))) +insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001'); +create table t2 (id int not null, text varchar(20) not null default '', primary key (id)); +insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10'); +select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id); +id text id text id text +1 text1 1 text1 1 text1 +2 text2 2 text2 2 text2 +3 text3 3 text3 3 text3 +4 text4 4 text4 4 text4 +5 text5 5 text5 5 text5 +6 text6 6 text6 6 text6 +7 text7 7 text7 7 text7 +8 text8 8 text8 8 text8 +9 text9 9 text9 9 text9 +10 text10 10 text10 10 text10 +11 text11 11 text1 11 text11 +12 text12 12 text2 12 text12 +1000 text1000 NULL NULL 1000 text1000 +1001 text1001 NULL NULL 1000 text1000 +explain extended select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE a ALL NULL NULL NULL NULL 14 100.00 +1 SIMPLE b eq_ref PRIMARY PRIMARY 4 test.a.id 2 100.00 +1 SIMPLE c eq_ref PRIMARY PRIMARY 4 func 1 100.00 Using index condition +Warnings: +Note 1003 select `test`.`a`.`id` AS `id`,`test`.`a`.`text` AS `text`,`test`.`b`.`id` AS `id`,`test`.`b`.`text` AS `text`,`test`.`c`.`id` AS `id`,`test`.`c`.`text` AS `text` from `test`.`t1` `a` left join `test`.`t2` `b` on(((`test`.`b`.`id` = `test`.`a`.`id`) or isnull(`test`.`b`.`id`))) join `test`.`t1` `c` where (if(isnull(`test`.`b`.`id`),1000,`test`.`b`.`id`) = `test`.`c`.`id`) +drop table t1,t2; +create table t1 (a int); +insert into t1 values (1); +explain select benchmark(1000, (select a from t1 where a=sha(rand()))); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used +2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 1 +drop table t1; +create table t1(id int); +create table t2(id int); +create table t3(flag int); +select (select * from t3 where id not null) from t1, t2; +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 'null) from t1, t2' at line 1 +drop table t1,t2,t3; +CREATE TABLE t1 (id INT); +CREATE TABLE t2 (id INT); +INSERT INTO t1 VALUES (1), (2); +INSERT INTO t2 VALUES (1); +SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id); +id c +1 1 +2 0 +SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id); +id c +1 1 +2 0 +SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY t1.id; +id c +1 1 +2 0 +SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY id; +id c +1 1 +2 0 +DROP TABLE t1,t2; +CREATE TABLE t1 ( a int, b int ); +INSERT INTO t1 VALUES (1,1),(2,2),(3,3); +SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +3 +SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +2 +SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +2 +3 +SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +2 +SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +3 +SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +3 +SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +2 +SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +2 +3 +SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +2 +SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +3 +ALTER TABLE t1 ADD INDEX (a); +SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +3 +SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +2 +SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +2 +3 +SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +2 +SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +3 +SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +3 +SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +2 +SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +2 +3 +SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +2 +SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 ); +a +1 +3 +SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2); +a +3 +SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2); +a +1 +SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2); +a +2 +SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2); +a +2 +3 +SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2); +a +1 +2 +SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2); +a +1 +3 +SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2); +a +3 +SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2); +a +1 +SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2); +a +2 +SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2); +a +2 +3 +SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2); +a +1 +2 +SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2); +a +1 +3 +SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +3 +SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +1 +SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +2 +SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +2 +3 +SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +1 +2 +SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +1 +3 +SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +3 +SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +1 +SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +2 +SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +2 +3 +SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +1 +2 +SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); +a +1 +3 +SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +3 +SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +1 +SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +2 +SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +2 +3 +SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +1 +2 +SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +1 +3 +SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +3 +SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +1 +SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +2 +SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +2 +3 +SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +1 +2 +SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); +a +1 +3 +SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 1 column(s) +SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 1 column(s) +SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 1 column(s) +SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 1 column(s) +SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 1 column(s) +SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 1 column(s) +SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 1 column(s) +SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 1 column(s) +SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 2 column(s) +SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 1 column(s) +SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2); +a +SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 2 column(s) +SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2); +ERROR 21000: Operand should contain 1 column(s) +SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2); +a +1 +2 +3 +SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2); +a +2 +SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2); +a +1 +3 +SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2); +a +2 +SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2); +a +1 +3 +SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2); +a +2 +SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2); +a +1 +3 +SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2); +a +2 +SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2); +a +1 +3 +SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a); +a +3 +SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a); +a +1 +SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a); +a +2 +SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a); +a +2 +3 +SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a); +a +1 +2 +SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a); +a +1 +3 +SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a); +a +3 +SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a); +a +1 +SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a); +a +2 +SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a); +a +2 +3 +SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a); +a +1 +2 +SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a); +a +1 +3 +SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2); +a +3 +SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2); +a +1 +SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2); +a +2 +SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2); +a +2 +3 +SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2); +a +1 +2 +SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2); +a +1 +3 +SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2); +a +3 +SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2); +a +1 +SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2); +a +2 +SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2); +a +2 +3 +SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2); +a +1 +2 +SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2); +a +1 +3 +SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a; +concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') +0- +0- +1- +SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a; +concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') +1- +0- +0- +SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a; +concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') +0- +1- +0- +DROP TABLE t1; +CREATE TABLE t1 ( a double, b double ); +INSERT INTO t1 VALUES (1,1),(2,2),(3,3); +SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0); +a +3 +SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0); +a +1 +SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0); +a +2 +SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0); +a +2 +3 +SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0); +a +1 +2 +SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0); +a +1 +3 +SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0); +a +3 +SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0); +a +1 +SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0); +a +2 +SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0); +a +2 +3 +SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0); +a +1 +2 +SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0); +a +1 +3 +DROP TABLE t1; +CREATE TABLE t1 ( a char(1), b char(1)); +INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3'); +SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2'); +a +3 +SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2'); +a +1 +SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2'); +a +2 +SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2'); +a +2 +3 +SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2'); +a +1 +2 +SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2'); +a +1 +3 +SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2'); +a +3 +SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2'); +a +1 +SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2'); +a +2 +SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2'); +a +2 +3 +SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2'); +a +1 +2 +SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2'); +a +1 +3 +DROP TABLE t1; +create table t1 (a int, b int); +insert into t1 values (1,2),(3,4); +select * from t1 up where exists (select * from t1 where t1.a=up.a); +a b +1 2 +3 4 +explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY up ALL NULL NULL NULL NULL 2 100.00 +1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 +2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 +Warnings: +Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1 +Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` semi join (`test`.`t1`) where 1 +drop table t1; +CREATE TABLE t1 (t1_a int); +INSERT INTO t1 VALUES (1); +CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b)); +INSERT INTO t2 VALUES (1, 1), (1, 2); +SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1 +HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a); +t1_a t2_a t2_b +1 1 2 +DROP TABLE t1, t2; +CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL); +INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL); +CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL); +INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix'); +SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id; +id name id pet +1 Tim 1 Fido +2 Rebecca 2 Spot +3 NULL 3 Felix +drop table t1,t2; +CREATE TABLE t1 ( a int, b int ); +CREATE TABLE t2 ( c int, d int ); +INSERT INTO t1 VALUES (1,2), (2,3), (3,4); +SELECT a AS abc, b FROM t1 outr WHERE b = +(SELECT MIN(b) FROM t1 WHERE a=outr.a); +abc b +1 2 +2 3 +3 4 +INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = +(SELECT MIN(b) FROM t1 WHERE a=outr.a); +select * from t2; +c d +1 2 +2 3 +3 4 +CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = +(SELECT MIN(b) FROM t1 WHERE a=outr.a); +select * from t3; +abc b +1 2 +2 3 +3 4 +prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);"; +execute stmt1; +deallocate prepare stmt1; +select * from t2; +c d +1 2 +2 3 +3 4 +1 2 +2 3 +3 4 +drop table t3; +prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);"; +execute stmt1; +select * from t3; +abc b +1 2 +2 3 +3 4 +deallocate prepare stmt1; +DROP TABLE t1, t2, t3; +CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1; +insert into t1 values (1); +CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1; +insert into t2 values (1,2); +select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1); +a C +1 1 +drop table t1,t2; +create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b)); +insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan'); +create table t2 (a int); +insert into t2 values (1),(3),(2),(7); +select a,b from t1 where match(b) against ('Ball') > 0; +a b +1 ball +2 ball games +select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0); +a +1 +2 +drop table t1,t2; +CREATE TABLE t1(`IZAVORGANG_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin,`KUERZEL` VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_bin,`IZAANALYSEART_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin,`IZAPMKZ_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin); +CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel); +INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001'); +INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001'); +INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001'); +INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001'); +SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001'); +IZAVORGANG_ID +D0000000001 +drop table t1; +CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`)); +CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`)); +insert into t1 values (1,1),(1,2),(2,1),(2,2); +insert into t2 values (1,2),(2,2); +select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); +aid bid +1 1 +2 1 +alter table t2 drop primary key; +alter table t2 add key KEY1 (aid, bid); +select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); +aid bid +1 1 +2 1 +alter table t2 drop key KEY1; +alter table t2 add primary key (bid, aid); +select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); +aid bid +1 1 +2 1 +drop table t1,t2; +CREATE TABLE t1 (howmanyvalues bigint, avalue int); +INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4); +SELECT howmanyvalues, count(*) from t1 group by howmanyvalues; +howmanyvalues count(*) +1 1 +2 2 +3 3 +4 4 +SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues; +howmanyvalues mycount +1 1 +2 2 +3 3 +4 4 +CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues); +SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues; +howmanyvalues mycount +1 1 +2 2 +3 3 +4 4 +SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues; +howmanyvalues mycount +1 1 +2 2 +3 3 +4 4 +SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues; +howmanyvalues mycount +1 1 +2 1 +3 1 +4 1 +drop table t1; +create table t1 (x int); +select (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x; +(select b.x from t1 as b where b.x=a.x) +drop table t1; +CREATE TABLE `t1` ( `master` int(10) unsigned NOT NULL default '0', `map` smallint(6) unsigned NOT NULL default '0', `slave` int(10) unsigned NOT NULL default '0', `access` int(10) unsigned NOT NULL default '0', UNIQUE KEY `access_u` (`master`,`map`,`slave`)); +INSERT INTO `t1` VALUES (1,0,0,700),(1,1,1,400),(1,5,5,400),(1,12,12,400),(1,12,32,400),(4,12,32,400); +CREATE TABLE `t2` ( `id` int(10) unsigned NOT NULL default '0', `pid` int(10) unsigned NOT NULL default '0', `map` smallint(6) unsigned NOT NULL default '0', `level` tinyint(4) unsigned NOT NULL default '0', `title` varchar(255) default NULL, PRIMARY KEY (`id`,`pid`,`map`), KEY `level` (`level`), KEY `id` (`id`,`map`)) ; +INSERT INTO `t2` VALUES (6,5,12,7,'a'),(12,0,0,7,'a'),(12,1,0,7,'a'),(12,5,5,7,'a'),(12,5,12,7,'a'); +SELECT b.sc FROM (SELECT (SELECT a.access FROM t1 a WHERE a.map = op.map AND a.slave = op.pid AND a.master = 1) ac FROM t2 op WHERE op.id = 12 AND op.map = 0) b; +ERROR 42S22: Unknown column 'b.sc' in 'field list' +SELECT b.ac FROM (SELECT (SELECT a.access FROM t1 a WHERE a.map = op.map AND a.slave = op.pid AND a.master = 1) ac FROM t2 op WHERE op.id = 12 AND op.map = 0) b; +ac +700 +NULL +drop tables t1,t2; +create table t1 (a int not null, b int not null, c int, primary key (a,b)); +insert into t1 values (1,1,1), (2,2,2), (3,3,3); +set @b:= 0; +explain select sum(a) from t1 where b > @b; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index NULL PRIMARY 8 NULL 3 Using where; Using index +set @a:= (select sum(a) from t1 where b > @b); +explain select a from t1 where c=2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where +do @a:= (select sum(a) from t1 where b > @b); +explain select a from t1 where c=2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where +drop table t1; +set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ; +create table t1 (a int, b int); +create table t2 (a int, b int); +insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5); +insert into t2 values (1,3),(2,1); +select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b; +a b (select max(b) from t2 where t1.b=t2.a) +1 1 3 +1 2 1 +1 3 NULL +2 4 NULL +2 5 NULL +drop table t1, t2; +create table t1 (id int); +create table t2 (id int, body text, fulltext (body)); +insert into t1 values(1),(2),(3); +insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test'); +select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode)); +count(distinct id) +1 +drop table t2,t1; +create table t1 (s1 int,s2 int); +insert into t1 values (20,15); +select * from t1 where (('a',null) <=> (select 'a',s2 from t1 where s1 = 0)); +s1 s2 +drop table t1; +create table t1 (s1 int); +insert into t1 values (1),(null); +select * from t1 where s1 < all (select s1 from t1); +s1 +select s1, s1 < all (select s1 from t1) from t1; +s1 s1 < all (select s1 from t1) +1 0 +NULL NULL +drop table t1; +CREATE TABLE t1 ( +Code char(3) NOT NULL default '', +Name char(52) NOT NULL default '', +Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia', +Region char(26) NOT NULL default '', +SurfaceArea float(10,2) NOT NULL default '0.00', +IndepYear smallint(6) default NULL, +Population int(11) NOT NULL default '0', +LifeExpectancy float(3,1) default NULL, +GNP float(10,2) default NULL, +GNPOld float(10,2) default NULL, +LocalName char(45) NOT NULL default '', +GovernmentForm char(45) NOT NULL default '', +HeadOfState char(60) default NULL, +Capital int(11) default NULL, +Code2 char(2) NOT NULL default '' +) ENGINE=MyISAM; +INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX'); +INSERT INTO t1 VALUES ('ASM','American Samoa','Oceania','Polynesia',199.00,0,68000,75.1,334.00,NULL,'Amerika Samoa','US Territory','George W. Bush',54,'AS'); +INSERT INTO t1 VALUES ('ATF','French Southern territories','Antarctica','Antarctica',7780.00,0,0,NULL,0.00,NULL,'Terres australes françaises','Nonmetropolitan Territory of France','Jacques Chirac',NULL,'TF'); +INSERT INTO t1 VALUES ('UMI','United States Minor Outlying Islands','Oceania','Micronesia/Caribbean',16.00,0,0,NULL,0.00,NULL,'United States Minor Outlying Islands','Dependent Territory of the US','George W. Bush',NULL,'UM'); +/*!40000 ALTER TABLE t1 ENABLE KEYS */; +SELECT DISTINCT Continent AS c FROM t1 outr WHERE +Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND +Population < 200); +c +Oceania +drop table t1; +create table t1 (a1 int); +create table t2 (b1 int); +select * from t1 where a2 > any(select b1 from t2); +ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery' +select * from t1 where a1 > any(select b1 from t2); +a1 +drop table t1,t2; +create table t1 (a integer, b integer); +select (select * from t1) = (select 1,2); +(select * from t1) = (select 1,2) +NULL +select (select 1,2) = (select * from t1); +(select 1,2) = (select * from t1) +NULL +select row(1,2) = ANY (select * from t1); +row(1,2) = ANY (select * from t1) +0 +select row(1,2) != ALL (select * from t1); +row(1,2) != ALL (select * from t1) +1 +drop table t1; +create table t1 (a integer, b integer); +select row(1,(2,2)) in (select * from t1 ); +ERROR 21000: Operand should contain 2 column(s) +select row(1,(2,2)) = (select * from t1 ); +ERROR 21000: Operand should contain 2 column(s) +select (select * from t1) = row(1,(2,2)); +ERROR 21000: Operand should contain 1 column(s) +drop table t1; +create table t1 (a integer); +insert into t1 values (1); +select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ; +ERROR 42S22: Reference 'xx' not supported (forward reference in item list) +select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx; +ERROR 42S22: Reference 'xx' not supported (forward reference in item list) +select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx ); +xx 1 = ALL ( select 1 from t1 where 1 = xx ) +1 1 +select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx; +ERROR 42S22: Reference 'xx' not supported (forward reference in item list) +select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL; +ERROR 42S22: Reference 'xx' not supported (forward reference in item list) +drop table t1; +CREATE TABLE t1 ( +categoryId int(11) NOT NULL, +courseId int(11) NOT NULL, +startDate datetime NOT NULL, +endDate datetime NOT NULL, +createDate datetime NOT NULL, +modifyDate timestamp NOT NULL, +attributes text NOT NULL +); +INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''), +(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), +(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), +(2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''), +(2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''), +(2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), +(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), +(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''), +(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18',''); +CREATE TABLE t2 ( +userId int(11) NOT NULL, +courseId int(11) NOT NULL, +date datetime NOT NULL +); +INSERT INTO t2 VALUES (5141,71,'2003-11-18'), +(5141,72,'2003-11-25'),(5141,41,'2004-08-06'), +(5141,52,'2004-08-06'),(5141,53,'2004-08-06'), +(5141,12,'2004-08-06'),(5141,86,'2004-10-21'), +(5141,87,'2004-10-21'),(5141,88,'2004-10-21'), +(5141,89,'2004-10-22'),(5141,51,'2004-10-26'); +CREATE TABLE t3 ( +groupId int(11) NOT NULL, +parentId int(11) NOT NULL, +startDate datetime NOT NULL, +endDate datetime NOT NULL, +createDate datetime NOT NULL, +modifyDate timestamp NOT NULL, +ordering int(11) +); +INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL); +CREATE TABLE t4 ( +id int(11) NOT NULL, +groupTypeId int(11) NOT NULL, +groupKey varchar(50) NOT NULL, +name text, +ordering int(11), +description text, +createDate datetime NOT NULL, +modifyDate timestamp NOT NULL +); +INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'), +(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29'); +CREATE TABLE t5 ( +userId int(11) NOT NULL, +groupId int(11) NOT NULL, +createDate datetime NOT NULL, +modifyDate timestamp NOT NULL +); +INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06'); +select +count(distinct t2.userid) pass, +groupstuff.*, +count(t2.courseid) crse, +t1.categoryid, +t2.courseid, +date_format(date, '%b%y') as colhead +from t2 +join t1 on t2.courseid=t1.courseid +join +( +select +t5.userid, +parentid, +parentgroup, +childid, +groupname, +grouptypeid +from t5 +join +( +select t4.id as parentid, +t4.name as parentgroup, +t4.id as childid, +t4.name as groupname, +t4.grouptypeid +from t4 +) as gin on t5.groupid=gin.childid +) as groupstuff on t2.userid = groupstuff.userid +group by +groupstuff.groupname, colhead , t2.courseid; +pass userid parentid parentgroup childid groupname grouptypeid crse categoryid courseid colhead +1 5141 12 group2 12 group2 5 1 5 12 Aug04 +1 5141 12 group2 12 group2 5 1 1 41 Aug04 +1 5141 12 group2 12 group2 5 1 2 52 Aug04 +1 5141 12 group2 12 group2 5 1 2 53 Aug04 +1 5141 12 group2 12 group2 5 1 3 51 Oct04 +1 5141 12 group2 12 group2 5 1 1 86 Oct04 +1 5141 12 group2 12 group2 5 1 1 87 Oct04 +1 5141 12 group2 12 group2 5 1 2 88 Oct04 +1 5141 12 group2 12 group2 5 1 2 89 Oct04 +drop table t1, t2, t3, t4, t5; +create table t1 (a int); +insert into t1 values (1), (2), (3); +SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1); +1 +1 +1 +1 +drop table t1; +create table t1 (a int); +create table t2 (a int); +insert into t1 values (1),(2); +insert into t2 values (0),(1),(2),(3); +select a from t2 where a in (select a from t1); +a +1 +2 +select a from t2 having a in (select a from t1); +a +1 +2 +prepare stmt1 from "select a from t2 where a in (select a from t1)"; +execute stmt1; +a +1 +2 +execute stmt1; +a +1 +2 +deallocate prepare stmt1; +prepare stmt1 from "select a from t2 having a in (select a from t1)"; +execute stmt1; +a +1 +2 +execute stmt1; +a +1 +2 +deallocate prepare stmt1; +drop table t1, t2; +create table t1 (a int, b int); +insert into t1 values (1,2); +select 1 = (select * from t1); +ERROR 21000: Operand should contain 1 column(s) +select (select * from t1) = 1; +ERROR 21000: Operand should contain 2 column(s) +select (1,2) = (select a from t1); +ERROR 21000: Operand should contain 2 column(s) +select (select a from t1) = (1,2); +ERROR 21000: Operand should contain 1 column(s) +select (1,2,3) = (select * from t1); +ERROR 21000: Operand should contain 3 column(s) +select (select * from t1) = (1,2,3); +ERROR 21000: Operand should contain 2 column(s) +drop table t1; +CREATE TABLE `t1` ( +`itemid` bigint(20) unsigned NOT NULL auto_increment, +`sessionid` bigint(20) unsigned default NULL, +`time` int(10) unsigned NOT NULL default '0', +`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT +NULL default '', +`data` text collate latin1_general_ci NOT NULL, +PRIMARY KEY (`itemid`) +) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; +INSERT INTO `t1` VALUES (1, 1, 1, 'D', ''); +CREATE TABLE `t2` ( +`sessionid` bigint(20) unsigned NOT NULL auto_increment, +`pid` int(10) unsigned NOT NULL default '0', +`date` int(10) unsigned NOT NULL default '0', +`ip` varchar(15) collate latin1_general_ci NOT NULL default '', +PRIMARY KEY (`sessionid`) +) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; +INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1'); +SELECT s.ip, count( e.itemid ) FROM `t1` e JOIN t2 s ON s.sessionid = e.sessionid WHERE e.sessionid = ( SELECT sessionid FROM t2 ORDER BY sessionid DESC LIMIT 1 ) GROUP BY s.ip HAVING count( e.itemid ) >0 LIMIT 0 , 30; +ip count( e.itemid ) +10.10.10.1 1 +drop tables t1,t2; +create table t1 (fld enum('0','1')); +insert into t1 values ('1'); +select * from (select max(fld) from t1) as foo; +max(fld) +1 +drop table t1; +set @tmp11867_optimizer_switch=@@optimizer_switch; +set optimizer_switch='semijoin_with_cache=off'; +CREATE TABLE t1 (one int, two int, flag char(1)); +CREATE TABLE t2 (one int, two int, flag char(1)); +INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N'); +INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N'); +SELECT * FROM t1 +WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N'); +one two flag +5 6 N +7 8 N +SELECT * FROM t1 +WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N'); +one two flag +5 6 N +7 8 N +insert into t2 values (null,null,'N'); +insert into t2 values (null,3,'0'); +insert into t2 values (null,5,'0'); +insert into t2 values (10,null,'0'); +insert into t1 values (10,3,'0'); +insert into t1 values (10,5,'0'); +insert into t1 values (10,10,'0'); +SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1; +one two test +1 2 NULL +2 3 NULL +3 4 NULL +5 6 1 +7 8 1 +10 3 NULL +10 5 NULL +10 10 NULL +SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); +one two +5 6 +7 8 +SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1; +one two test +1 2 NULL +2 3 NULL +3 4 NULL +5 6 1 +7 8 1 +10 3 NULL +10 5 NULL +10 10 NULL +SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1; +one two test +1 2 0 +2 3 NULL +3 4 0 +5 6 0 +7 8 0 +10 3 NULL +10 5 NULL +10 10 NULL +SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1; +one two test +1 2 0 +2 3 NULL +3 4 0 +5 6 0 +7 8 0 +10 3 NULL +10 5 NULL +10 10 NULL +explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where +Warnings: +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)))) and trigcond(trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1` +explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 +1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 100.00 +2 MATERIALIZED t2 ALL NULL NULL NULL NULL 9 100.00 Using where +Warnings: +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` semi join (`test`.`t2`) where (`test`.`t2`.`flag` = 'N') +explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where +Warnings: +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)))) and trigcond(trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1` +DROP TABLE t1,t2; +set optimizer_switch=@tmp11867_optimizer_switch; +CREATE TABLE t1 (a char(5), b char(5)); +INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa'); +SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb')); +a b +aaa aaa +DROP TABLE t1; +CREATE TABLE t1 (a int); +CREATE TABLE t2 (a int, b int); +CREATE TABLE t3 (b int NOT NULL); +INSERT INTO t1 VALUES (1), (2), (3), (4); +INSERT INTO t2 VALUES (1,10), (3,30); +SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b +WHERE t3.b IS NOT NULL OR t2.a > 10; +a b b +SELECT * FROM t1 +WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b +WHERE t3.b IS NOT NULL OR t2.a > 10); +a +1 +2 +3 +4 +DROP TABLE t1,t2,t3; +CREATE TABLE t1 (f1 INT); +CREATE TABLE t2 (f2 INT); +INSERT INTO t1 VALUES (1); +SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2); +f1 +1 +SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0); +f1 +1 +INSERT INTO t2 VALUES (1); +INSERT INTO t2 VALUES (2); +SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0); +f1 +1 +DROP TABLE t1, t2; +select 1 from dual where 1 < any (select 2); +1 +1 +select 1 from dual where 1 < all (select 2); +1 +1 +select 1 from dual where 2 > any (select 1); +1 +1 +select 1 from dual where 2 > all (select 1); +1 +1 +select 1 from dual where 1 < any (select 2 from dual); +1 +1 +select 1 from dual where 1 < all (select 2 from dual where 1!=1); +1 +1 +create table t1 (s1 char); +insert into t1 values (1),(2); +select * from t1 where (s1 < any (select s1 from t1)); +s1 +1 +select * from t1 where not (s1 < any (select s1 from t1)); +s1 +2 +select * from t1 where (s1 < ALL (select s1+1 from t1)); +s1 +1 +select * from t1 where not(s1 < ALL (select s1+1 from t1)); +s1 +2 +select * from t1 where (s1+1 = ANY (select s1 from t1)); +s1 +1 +select * from t1 where NOT(s1+1 = ANY (select s1 from t1)); +s1 +2 +select * from t1 where (s1 = ALL (select s1/s1 from t1)); +s1 +1 +select * from t1 where NOT(s1 = ALL (select s1/s1 from t1)); +s1 +2 +drop table t1; +create table t1 ( +retailerID varchar(8) NOT NULL, +statusID int(10) unsigned NOT NULL, +changed datetime NOT NULL, +UNIQUE KEY retailerID (retailerID, statusID, changed) +); +INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56"); +INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53"); +INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56"); +INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53"); +INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50"); +INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50"); +select * from t1 r1 +where (r1.retailerID,(r1.changed)) in +(SELECT r2.retailerId,(max(changed)) from t1 r2 +group by r2.retailerId); +retailerID statusID changed +0026 2 2006-01-06 12:25:53 +0037 2 2006-01-06 12:25:53 +0048 1 2006-01-06 12:37:50 +0059 1 2006-01-06 12:37:50 +drop table t1; +create table t1(a int, primary key (a)); +insert into t1 values (10); +create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b)); +insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989'); +explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r +ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' + ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 system PRIMARY NULL NULL NULL 1 +1 PRIMARY r const PRIMARY PRIMARY 4 const 1 +2 SUBQUERY t2 range b b 40 NULL 2 Using where +SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r +ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' + ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10; +a a b +10 3 35989 +explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r +ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' + ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 system PRIMARY NULL NULL NULL 1 +1 PRIMARY r const PRIMARY PRIMARY 4 const 1 +2 SUBQUERY t2 range b b 40 NULL 2 Using index condition +SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r +ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' + ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10; +a a b +10 1 359 +drop table t1,t2; +CREATE TABLE t1 ( +field1 int NOT NULL, +field2 int NOT NULL, +field3 int NOT NULL, +PRIMARY KEY (field1,field2,field3) +); +CREATE TABLE t2 ( +fieldA int NOT NULL, +fieldB int NOT NULL, +PRIMARY KEY (fieldA,fieldB) +); +INSERT INTO t1 VALUES +(1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1); +INSERT INTO t2 VALUES (1,1), (1,2), (1,3); +SELECT field1, field2, COUNT(*) +FROM t1 GROUP BY field1, field2; +field1 field2 COUNT(*) +1 1 2 +1 2 3 +1 3 1 +SELECT field1, field2 +FROM t1 +GROUP BY field1, field2 +HAVING COUNT(*) >= ALL (SELECT fieldB +FROM t2 WHERE fieldA = field1); +field1 field2 +1 2 +SELECT field1, field2 +FROM t1 +GROUP BY field1, field2 +HAVING COUNT(*) < ANY (SELECT fieldB +FROM t2 WHERE fieldA = field1); +field1 field2 +1 1 +1 3 +DROP TABLE t1, t2; +CREATE TABLE t1(a int, INDEX (a)); +INSERT INTO t1 VALUES (1), (3), (5), (7); +INSERT INTO t1 VALUES (NULL); +CREATE TABLE t2(a int); +INSERT INTO t2 VALUES (1),(2),(3); +EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 3 +2 SUBQUERY t1 index_subquery a a 5 func 2 Using index; Full scan on NULL key +SELECT a, a IN (SELECT a FROM t1) FROM t2; +a a IN (SELECT a FROM t1) +1 1 +2 NULL +3 1 +DROP TABLE t1,t2; +CREATE TABLE t1 (a DATETIME); +INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25'); +CREATE TABLE t2 AS SELECT +(SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a +FROM t1 WHERE a > '2000-01-01'; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `sub_a` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01'); +SHOW CREATE TABLE t3; +Table Create Table +t3 CREATE TABLE `t3` ( + `a` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1,t2,t3; +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (1), (2); +SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0; +a +SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; +a +1 +2 +EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +DROP TABLE t1; +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (2), (4), (1), (3); +CREATE TABLE t2 (b int, c int); +INSERT INTO t2 VALUES +(2,1), (1,3), (2,1), (4,4), (2,2), (1,4); +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 ); +a +2 +4 +1 +3 +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1); +ERROR 21000: Subquery returns more than 1 row +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a; +a +1 +2 +3 +4 +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a; +ERROR 21000: Subquery returns more than 1 row +SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2); +b MAX(c) +1 4 +2 2 +4 4 +SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1); +ERROR 21000: Subquery returns more than 1 row +SELECT a FROM t1 GROUP BY a +HAVING IFNULL((SELECT b FROM t2 WHERE b > 2), +(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; +a +1 +2 +3 +4 +SELECT a FROM t1 GROUP BY a +HAVING IFNULL((SELECT b FROM t2 WHERE b > 1), +(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; +ERROR 21000: Subquery returns more than 1 row +SELECT a FROM t1 GROUP BY a +HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), +(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; +a +4 +SELECT a FROM t1 GROUP BY a +HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), +(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3; +ERROR 21000: Subquery returns more than 1 row +SELECT a FROM t1 +ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2), +(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); +a +1 +2 +3 +4 +SELECT a FROM t1 +ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1), +(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); +ERROR 21000: Subquery returns more than 1 row +SELECT a FROM t1 +ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), +(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); +a +1 +2 +3 +4 +SELECT a FROM t1 +ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), +(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); +ERROR 21000: Subquery returns more than 1 row +DROP TABLE t1,t2; +create table t1 (df decimal(5,1)); +insert into t1 values(1.1); +insert into t1 values(2.2); +select * from t1 where df <= all (select avg(df) from t1 group by df); +df +1.1 +select * from t1 where df >= all (select avg(df) from t1 group by df); +df +2.2 +drop table t1; +create table t1 (df decimal(5,1)); +insert into t1 values(1.1); +select 1.1 * exists(select * from t1); +1.1 * exists(select * from t1) +1.1 +drop table t1; +CREATE TABLE t1 ( +grp int(11) default NULL, +a decimal(10,2) default NULL); +insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL); +select * from t1; +grp a +1 1.00 +2 2.00 +2 3.00 +3 4.00 +3 5.00 +3 6.00 +NULL NULL +select min(a) from t1 group by grp; +min(a) +NULL +1.00 +2.00 +4.00 +drop table t1; +CREATE table t1 ( c1 integer ); +INSERT INTO t1 VALUES ( 1 ); +INSERT INTO t1 VALUES ( 2 ); +INSERT INTO t1 VALUES ( 3 ); +CREATE TABLE t2 ( c2 integer ); +INSERT INTO t2 VALUES ( 1 ); +INSERT INTO t2 VALUES ( 4 ); +INSERT INTO t2 VALUES ( 5 ); +SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1); +c1 c2 +1 1 +SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 +WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) ); +c1 c2 +1 1 +DROP TABLE t1,t2; +CREATE TABLE t1 ( c1 integer ); +INSERT INTO t1 VALUES ( 1 ); +INSERT INTO t1 VALUES ( 2 ); +INSERT INTO t1 VALUES ( 3 ); +INSERT INTO t1 VALUES ( 6 ); +CREATE TABLE t2 ( c2 integer ); +INSERT INTO t2 VALUES ( 1 ); +INSERT INTO t2 VALUES ( 4 ); +INSERT INTO t2 VALUES ( 5 ); +INSERT INTO t2 VALUES ( 6 ); +CREATE TABLE t3 ( c3 integer ); +INSERT INTO t3 VALUES ( 7 ); +INSERT INTO t3 VALUES ( 8 ); +SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2 +WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL ); +c1 c2 +2 NULL +3 NULL +DROP TABLE t1,t2,t3; +CREATE TABLE `t1` ( +`itemid` bigint(20) unsigned NOT NULL auto_increment, +`sessionid` bigint(20) unsigned default NULL, +`time` int(10) unsigned NOT NULL default '0', +`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT +NULL default '', +`data` text collate latin1_general_ci NOT NULL, +PRIMARY KEY (`itemid`) +) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; +INSERT INTO `t1` VALUES (1, 1, 1, 'D', ''); +CREATE TABLE `t2` ( +`sessionid` bigint(20) unsigned NOT NULL auto_increment, +`pid` int(10) unsigned NOT NULL default '0', +`date` int(10) unsigned NOT NULL default '0', +`ip` varchar(15) collate latin1_general_ci NOT NULL default '', +PRIMARY KEY (`sessionid`) +) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; +INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1'); +SELECT s.ip, count( e.itemid ) FROM `t1` e JOIN t2 s ON s.sessionid = e.sessionid WHERE e.sessionid = ( SELECT sessionid FROM t2 ORDER BY sessionid DESC LIMIT 1 ) GROUP BY s.ip HAVING count( e.itemid ) >0 LIMIT 0 , 30; +ip count( e.itemid ) +10.10.10.1 1 +drop tables t1,t2; +CREATE TABLE t1 (EMPNUM CHAR(3)); +CREATE TABLE t2 (EMPNUM CHAR(3) ); +INSERT INTO t1 VALUES ('E1'),('E2'); +INSERT INTO t2 VALUES ('E1'); +DELETE FROM t1 +WHERE t1.EMPNUM NOT IN +(SELECT t2.EMPNUM +FROM t2 +WHERE t1.EMPNUM = t2.EMPNUM); +select * from t1; +EMPNUM +E1 +DROP TABLE t1,t2; +CREATE TABLE t1(select_id BIGINT, values_id BIGINT); +INSERT INTO t1 VALUES (1, 1); +CREATE TABLE t2 (select_id BIGINT, values_id BIGINT, +PRIMARY KEY(select_id,values_id)); +INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5); +SELECT values_id FROM t1 +WHERE values_id IN (SELECT values_id FROM t2 +WHERE select_id IN (1, 0)); +values_id +1 +SELECT values_id FROM t1 +WHERE values_id IN (SELECT values_id FROM t2 +WHERE select_id BETWEEN 0 AND 1); +values_id +1 +SELECT values_id FROM t1 +WHERE values_id IN (SELECT values_id FROM t2 +WHERE select_id = 0 OR select_id = 1); +values_id +1 +DROP TABLE t1, t2; +create table t1 (fld enum('0','1')); +insert into t1 values ('1'); +select * from (select max(fld) from t1) as foo; +max(fld) +1 +drop table t1; +CREATE TABLE t1 (a int, b int); +CREATE TABLE t2 (c int, d int); +CREATE TABLE t3 (e int); +INSERT INTO t1 VALUES +(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40); +INSERT INTO t2 VALUES +(2,10), (2,20), (4,10), (5,10), (3,20), (2,40); +INSERT INTO t3 VALUES (10), (30), (10), (20) ; +SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a; +a MAX(b) MIN(b) +1 20 10 +2 30 10 +3 20 20 +4 40 40 +SELECT * FROM t2; +c d +2 10 +2 20 +4 10 +5 10 +3 20 +2 40 +SELECT * FROM t3; +e +10 +30 +10 +20 +SELECT a FROM t1 GROUP BY a +HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20); +a +2 +4 +SELECT a FROM t1 GROUP BY a +HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d); +a +2 +SELECT a FROM t1 GROUP BY a +HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d); +a +2 +4 +SELECT a FROM t1 GROUP BY a +HAVING a IN (SELECT c FROM t2 +WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e)); +a +2 +3 +SELECT a FROM t1 GROUP BY a +HAVING a IN (SELECT c FROM t2 +WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d)); +a +2 +3 +SELECT a FROM t1 GROUP BY a +HAVING a IN (SELECT c FROM t2 +WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e)); +a +2 +SELECT a FROM t1 GROUP BY a +HAVING a IN (SELECT c FROM t2 +WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d)); +a +2 +SELECT a FROM t1 GROUP BY a +HAVING a IN (SELECT c FROM t2 +WHERE MIN(b) < d AND +EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d)); +a +2 +SELECT a, SUM(a) FROM t1 GROUP BY a; +a SUM(a) +1 2 +2 6 +3 3 +4 4 +SELECT a FROM t1 +WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a; +a +3 +4 +SELECT a FROM t1 GROUP BY a +HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c); +a +1 +3 +4 +SELECT a FROM t1 +WHERE a < 3 AND +EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a; +a +1 +2 +SELECT a FROM t1 +WHERE a < 3 AND +EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c); +a +1 +2 +1 +2 +2 +SELECT t1.a FROM t1 GROUP BY t1.a +HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c +HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e +HAVING SUM(t1.a+t2.c) < t3.e/4)); +a +1 +2 +SELECT t1.a FROM t1 GROUP BY t1.a +HAVING t1.a > ALL(SELECT t2.c FROM t2 +WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e +HAVING SUM(t1.a+t2.c) < t3.e/4)); +a +4 +SELECT t1.a FROM t1 GROUP BY t1.a +HAVING t1.a > ALL(SELECT t2.c FROM t2 +WHERE EXISTS(SELECT t3.e FROM t3 +WHERE SUM(t1.a+t2.c) < t3.e/4)); +ERROR HY000: Invalid use of group function +SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20; +ERROR HY000: Invalid use of group function +SELECT t1.a FROM t1 GROUP BY t1.a +HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c +HAVING AVG(t2.c+SUM(t1.b)) > 20); +a +2 +3 +4 +SELECT t1.a FROM t1 GROUP BY t1.a +HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c +HAVING AVG(SUM(t1.b)) > 20); +a +2 +4 +SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a +HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c +HAVING t2.c+sum > 20); +a sum +2 60 +3 20 +4 40 +DROP TABLE t1,t2,t3; +CREATE TABLE t1 (a varchar(5), b varchar(10)); +INSERT INTO t1 VALUES +('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2), +('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8); +SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); +a b +BBB 4 +CCC 7 +AAA 8 +EXPLAIN +SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 9 Using where +1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1 +2 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary +ALTER TABLE t1 ADD INDEX(a); +SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); +a b +BBB 4 +CCC 7 +AAA 8 +EXPLAIN +SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL a NULL NULL NULL 9 Using where +1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1 +2 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary +DROP TABLE t1; +create table t1( f1 int,f2 int); +insert into t1 values (1,1),(2,2); +select tt.t from (select 'crash1' as t, f2 from t1) as tt left join t1 on tt.t = 'crash2' and tt.f2 = t1.f2 where tt.t = 'crash1'; +t +crash1 +crash1 +drop table t1; +create table t1 (c int, key(c)); +insert into t1 values (1142477582), (1142455969); +create table t2 (a int, b int); +insert into t2 values (2, 1), (1, 0); +delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1; +drop table t1, t2; +CREATE TABLE t1 (a INT); +CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1); +ERROR 42S22: Unknown column 'no_such_column' in 'where clause' +CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1); +ERROR 42S22: Unknown column 'no_such_column' in 'where clause' +SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1); +ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery' +DROP TABLE t1; +create table t1 (i int, j bigint); +insert into t1 values (1, 2), (2, 2), (3, 2); +select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3; +min(i) +1 +drop table t1; +CREATE TABLE t1 (i BIGINT UNSIGNED); +INSERT INTO t1 VALUES (10000000000000000000); +INSERT INTO t1 VALUES (1); +CREATE TABLE t2 (i BIGINT UNSIGNED); +INSERT INTO t2 VALUES (10000000000000000000); +INSERT INTO t2 VALUES (1); +/* simple test */ +SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i; +i +10000000000000000000 +1 +/* subquery test */ +SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2); +i +10000000000000000000 +/* subquery test with cast*/ +SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED); +i +10000000000000000000 +DROP TABLE t1; +DROP TABLE t2; +CREATE TABLE t1 ( +id bigint(20) unsigned NOT NULL auto_increment, +name varchar(255) NOT NULL, +PRIMARY KEY (id) +); +INSERT INTO t1 VALUES +(1, 'Balazs'), (2, 'Joe'), (3, 'Frank'); +CREATE TABLE t2 ( +id bigint(20) unsigned NOT NULL auto_increment, +mid bigint(20) unsigned NOT NULL, +date date NOT NULL, +PRIMARY KEY (id) +); +INSERT INTO t2 VALUES +(1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'), +(4, 2, '2006-04-20'), (5, 1, '2006-05-01'); +SELECT *, +(SELECT date FROM t2 WHERE mid = t1.id +ORDER BY date DESC LIMIT 0, 1) AS date_last, +(SELECT date FROM t2 WHERE mid = t1.id +ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last +FROM t1; +id name date_last date_next_to_last +1 Balazs 2006-05-01 NULL +2 Joe 2006-04-20 NULL +3 Frank 2006-04-13 NULL +SELECT *, +(SELECT COUNT(*) FROM t2 WHERE mid = t1.id +ORDER BY date DESC LIMIT 1, 1) AS date_count +FROM t1; +id name date_count +1 Balazs NULL +2 Joe NULL +3 Frank NULL +SELECT *, +(SELECT date FROM t2 WHERE mid = t1.id +ORDER BY date DESC LIMIT 0, 1) AS date_last, +(SELECT date FROM t2 WHERE mid = t1.id +ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last +FROM t1; +id name date_last date_next_to_last +1 Balazs 2006-05-01 2006-03-30 +2 Joe 2006-04-20 2006-04-06 +3 Frank 2006-04-13 NULL +DROP TABLE t1,t2; +CREATE TABLE t1 ( +i1 int(11) NOT NULL default '0', +i2 int(11) NOT NULL default '0', +t datetime NOT NULL default '0000-00-00 00:00:00', +PRIMARY KEY (i1,i2,t) +); +INSERT INTO t1 VALUES +(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'), +(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'), +(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'), +(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'), +(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'), +(24,2,'2005-05-27 12:40:06'); +CREATE TABLE t2 ( +i1 int(11) NOT NULL default '0', +i2 int(11) NOT NULL default '0', +t datetime default NULL, +PRIMARY KEY (i1) +); +INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40'); +EXPLAIN +SELECT * FROM t1,t2 +WHERE t1.t = (SELECT t1.t FROM t1 +WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1 +ORDER BY t1.t DESC LIMIT 1); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 system NULL NULL NULL NULL 1 +1 PRIMARY t1 index NULL PRIMARY 16 NULL 11 Using where; Using index +2 SUBQUERY t1 range PRIMARY PRIMARY 16 NULL 5 Using where; Using index +SELECT * FROM t1,t2 +WHERE t1.t = (SELECT t1.t FROM t1 +WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1 +ORDER BY t1.t DESC LIMIT 1); +i1 i2 t i1 i2 t +24 1 2005-05-27 12:40:30 24 1 2006-06-20 12:29:40 +DROP TABLE t1, t2; +CREATE TABLE t1 (i INT); +(SELECT i FROM t1) UNION (SELECT i FROM t1); +i +SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS +( +(SELECT i FROM t1) UNION +(SELECT i FROM t1) +); +i +SELECT * FROM t1 +WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM 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 'UNION (SELECT i FROM t1)))' at line 2 +explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12)) +from 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 'union (select t12.i from t1 t12)) +from t1' at line 1 +explain select * from t1 where not exists +((select t11.i from t1 t11) union (select t12.i from t1 t12)); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table +3 UNION NULL NULL NULL NULL NULL NULL NULL no matching row in const table +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL +DROP TABLE t1; +CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b)); +insert into t1 (a) values (FLOOR(rand() * 100)); +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +insert into t1 (a) select FLOOR(rand() * 100) from t1; +SELECT a, +(SELECT REPEAT(' ',250) FROM t1 i1 +WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a +FROM t1 ORDER BY a LIMIT 5; +a a +0 NULL +0 NULL +0 NULL +0 NULL +0 NULL +DROP TABLE t1; +CREATE TABLE t1 (a INT, b INT); +CREATE TABLE t2 (a INT); +INSERT INTO t2 values (1); +INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4); +SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a; +(SELECT COUNT(DISTINCT t1.b) from t2) +2 +1 +1 +SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) +FROM t1 GROUP BY t1.a; +(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) +2 +1 +1 +SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a; +COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b)) +2 2 +1 1 +1 1 +SELECT COUNT(DISTINCT t1.b), +(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) +FROM t1 GROUP BY t1.a; +COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) +2 2 +1 1 +1 1 +SELECT ( +SELECT ( +SELECT COUNT(DISTINCT t1.b) +) +) +FROM t1 GROUP BY t1.a; +( +SELECT ( +SELECT COUNT(DISTINCT t1.b) +) +) +2 +1 +1 +SELECT ( +SELECT ( +SELECT ( +SELECT COUNT(DISTINCT t1.b) +) +) +FROM t1 GROUP BY t1.a LIMIT 1) +FROM t1 t2 +GROUP BY t2.a; +( +SELECT ( +SELECT ( +SELECT COUNT(DISTINCT t1.b) +) +) +FROM t1 GROUP BY t1.a LIMIT 1) +2 +2 +2 +DROP TABLE t1,t2; +CREATE TABLE t1 (a int, b int, PRIMARY KEY (b)); +CREATE TABLE t2 (x int auto_increment, y int, z int, +PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b)); +create table t3 (a int); +insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); +insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a)) +from t3 A, t3 B, t3 C, t3 D where D.a<3; +insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3; +SET SESSION sort_buffer_size = 32 * 1024; +SELECT SQL_NO_CACHE COUNT(*) +FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c +FROM t1) t; +COUNT(*) +3000 +SET SESSION sort_buffer_size = 8 * 1024 * 1024; +SELECT SQL_NO_CACHE COUNT(*) +FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c +FROM t1) t; +COUNT(*) +3000 +DROP TABLE t1,t2,t3; +CREATE TABLE t1 (id char(4) PRIMARY KEY, c int); +CREATE TABLE t2 (c int); +INSERT INTO t1 VALUES ('aa', 1); +INSERT INTO t2 VALUES (1); +SELECT * FROM t1 +WHERE EXISTS (SELECT c FROM t2 WHERE c=1 +UNION +SELECT c from t2 WHERE c=t1.c); +id c +aa 1 +INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1); +SELECT * FROM t1 +WHERE EXISTS (SELECT c FROM t2 WHERE c=1 +UNION +SELECT c from t2 WHERE c=t1.c); +id c +aa 1 +bb 2 +cc 3 +dd 1 +INSERT INTO t2 VALUES (2); +CREATE TABLE t3 (c int); +INSERT INTO t3 VALUES (1); +SELECT * FROM t1 +WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1 +UNION +SELECT c from t2 WHERE c=t1.c); +id c +aa 1 +bb 2 +cc 3 +dd 1 +DROP TABLE t1,t2,t3; +CREATE TABLE t1(f1 int); +CREATE TABLE t2(f2 int, f21 int, f3 timestamp); +INSERT INTO t1 VALUES (1),(1),(2),(2); +INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11"); +SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1; +sq +2 +4 +SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2; +tt +2 +2 +PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1'; +EXECUTE stmt1; +sq +2 +4 +EXECUTE stmt1; +sq +2 +4 +DEALLOCATE PREPARE stmt1; +SELECT f2, AVG(f21), +(SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test +FROM t2 GROUP BY f2; +f2 AVG(f21) test +1 1.0000 2004-02-29 11:11:11 +2 2.0000 2004-02-29 11:11:11 +DROP TABLE t1,t2; +CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL); +INSERT INTO t1 VALUES +(1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'), +(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'), +(3,2,'k'), (3,1,'l'), (1,9,'m'); +SELECT a, MAX(b), +(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test +FROM t1 GROUP BY a; +a MAX(b) test +1 9 m +2 3 h +3 4 i +DROP TABLE t1; +DROP TABLE IF EXISTS t1; +DROP TABLE IF EXISTS t2; +DROP TABLE IF EXISTS t1xt2; +CREATE TABLE t1 ( +id_1 int(5) NOT NULL, +t varchar(4) DEFAULT NULL +); +CREATE TABLE t2 ( +id_2 int(5) NOT NULL, +t varchar(4) DEFAULT NULL +); +CREATE TABLE t1xt2 ( +id_1 int(5) NOT NULL, +id_2 int(5) NOT NULL +); +INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'); +INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa'); +INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4); +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); +id_1 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); +id_1 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); +id_1 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); +id_1 +1 +2 +3 +4 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))); +id_1 +1 +2 +3 +4 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)))); +id_1 +1 +2 +3 +4 +insert INTO t1xt2 VALUES (1, 12); +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); +id_1 +1 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); +id_1 +1 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); +id_1 +1 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); +id_1 +2 +3 +4 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); +id_1 +2 +3 +4 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); +id_1 +2 +3 +4 +insert INTO t1xt2 VALUES (2, 12); +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); +id_1 +1 +2 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); +id_1 +1 +2 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); +id_1 +1 +2 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); +id_1 +3 +4 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); +id_1 +3 +4 +SELECT DISTINCT t1.id_1 FROM t1 WHERE +(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); +id_1 +3 +4 +DROP TABLE t1; +DROP TABLE t2; +DROP TABLE t1xt2; +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (3), (1), (2); +SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1; +col1 col2 +this is a test. 3 +this is a test. 1 +this is a test. 2 +SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t; +col1 t2 +this is a test. 3 +this is a test. 1 +this is a test. 2 +DROP table t1; +CREATE TABLE t1 (a int, b int); +CREATE TABLE t2 (m int, n int); +INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4); +INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44); +SELECT COUNT(*), a, +(SELECT m FROM t2 WHERE m = count(*) LIMIT 1) +FROM t1 GROUP BY a; +COUNT(*) a (SELECT m FROM t2 WHERE m = count(*) LIMIT 1) +2 2 2 +3 3 3 +1 4 1 +SELECT COUNT(*), a, +(SELECT MIN(m) FROM t2 WHERE m = count(*)) +FROM t1 GROUP BY a; +COUNT(*) a (SELECT MIN(m) FROM t2 WHERE m = count(*)) +2 2 2 +3 3 3 +1 4 1 +SELECT COUNT(*), a +FROM t1 GROUP BY a +HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1; +COUNT(*) a +2 2 +3 3 +DROP TABLE t1,t2; +CREATE TABLE t1 (a int, b int); +CREATE TABLE t2 (m int, n int); +INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4); +INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44); +SELECT COUNT(*) c, a, +(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) +FROM t1 GROUP BY a; +c a (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) +2 2 2 +3 3 3 +1 4 1,1 +SELECT COUNT(*) c, a, +(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a) +FROM t1 GROUP BY a; +c a (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a) +2 2 3 +3 3 4 +1 4 2,2 +DROP table t1,t2; +CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b)); +INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'), +(1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'), +(3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p'); +SELECT a, MAX(b), +(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test +FROM t1 GROUP BY a; +a MAX(b) test +1 9 m +2 3 h +3 4 i +SELECT a x, MAX(b), +(SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test +FROM t1 GROUP BY a; +x MAX(b) test +1 9 m +2 3 h +3 4 i +SELECT a, AVG(b), +(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test +FROM t1 WHERE t1.d=0 GROUP BY a; +a AVG(b) test +1 4.0000 d +2 2.0000 g +3 2.5000 NULL +SELECT tt.a, +(SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) +LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test +FROM t1 as tt; +a test +1 n +1 n +1 n +1 n +1 n +1 n +1 n +2 o +2 o +2 o +2 o +3 p +3 p +3 p +3 p +3 p +SELECT tt.a, +(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) +LIMIT 1) +FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test +FROM t1 as tt GROUP BY tt.a; +a test +1 n +2 o +3 p +SELECT tt.a, MAX( +(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) +LIMIT 1) +FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test +FROM t1 as tt GROUP BY tt.a; +a test +1 n +2 o +3 p +DROP TABLE t1; +CREATE TABLE t1 (a int, b int); +INSERT INTO t1 VALUES (2,22),(1,11),(2,22); +SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; +a +1 +2 +SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; +a +SELECT a FROM t1 t0 +WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; +a +1 +2 +SET @@sql_mode='ansi'; +SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; +ERROR HY000: Invalid use of group function +SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; +ERROR HY000: Invalid use of group function +SELECT a FROM t1 t0 +WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; +ERROR HY000: Invalid use of group function +SET @@sql_mode=default; +DROP TABLE t1; +CREATE TABLE t1 (a INT); +INSERT INTO t1 values (1),(1),(1),(1); +CREATE TABLE t2 (x INT); +INSERT INTO t1 values (1000),(1001),(1002); +SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1; +ERROR HY000: Invalid use of group function +SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1; +ERROR HY000: Invalid use of group function +SELECT COUNT(1) FROM DUAL; +COUNT(1) +1 +SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1; +ERROR HY000: Invalid use of group function +SELECT +SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) ) +FROM t1; +ERROR HY000: Invalid use of group function +SELECT t1.a as XXA, +SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) ) +FROM t1; +ERROR HY000: Invalid use of group function +DROP TABLE t1,t2; +CREATE TABLE t1 (a int, b int, KEY (a)); +INSERT INTO t1 VALUES (1,1),(2,1); +EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ref a a 5 const 1 Using where; Using index +2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort +DROP TABLE t1; +CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id)); +INSERT INTO t1 VALUES +(3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY'); +CREATE TABLE t2 (id int NOT NULL, INDEX idx(id)); +INSERT INTO t2 VALUES (7), (5), (1), (3); +SELECT id, st FROM t1 +WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id); +id st +3 FL +1 GA +7 FL +SELECT id, st FROM t1 +WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id) +GROUP BY id; +id st +1 GA +3 FL +7 FL +SELECT id, st FROM t1 +WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id); +id st +2 GA +4 FL +SELECT id, st FROM t1 +WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id) +GROUP BY id; +id st +2 GA +4 FL +DROP TABLE t1,t2; +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (1), (2); +EXPLAIN EXTENDED +SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 100.00 +2 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort +Warnings: +Note 1003 select `res`.`count(*)` AS `count(*)` from (select count(0) AS `count(*)` from `test`.`t1` group by `test`.`t1`.`a`) `res` +DROP TABLE t1; +CREATE TABLE t1 ( +a varchar(255) default NULL, +b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, +INDEX idx(a,b) +); +CREATE TABLE t2 ( +a varchar(255) default NULL +); +INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24'); +INSERT INTO t1 SELECT * FROM t1; +INSERT INTO t1 SELECT * FROM t1; +INSERT INTO t1 SELECT * FROM t1; +INSERT INTO t1 SELECT * FROM t1; +INSERT INTO t1 SELECT * FROM t1; +INSERT INTO t1 SELECT * FROM t1; +INSERT INTO t1 SELECT * FROM t1; +INSERT INTO t1 SELECT * FROM t1; +INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26'); +INSERT INTO `t2` VALUES ('abcdefghijk'); +INSERT INTO `t2` VALUES ('asdf'); +SET session sort_buffer_size=8192; +SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2; +d1 +1 +1 +DROP TABLE t1,t2; +CREATE TABLE t1 (a INTEGER, b INTEGER); +CREATE TABLE t2 (x INTEGER); +INSERT INTO t1 VALUES (1,11), (2,22), (2,22); +INSERT INTO t2 VALUES (1), (2); +SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a; +ERROR 21000: Subquery returns more than 1 row +SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a; +ERROR 21000: Subquery returns more than 1 row +SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1; +(SELECT SUM(t1.a)/AVG(t2.x) FROM t2) +3.3333 +DROP TABLE t1,t2; +CREATE TABLE t1 (a INT, b INT); +INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2); +SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1 +AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a) +GROUP BY a1.a; +a COUNT(*) +1 3 +DROP TABLE t1; +CREATE TABLE t1 (a INT); +CREATE TABLE t2 (a INT); +INSERT INTO t1 VALUES (1),(2); +INSERT INTO t2 VALUES (1),(2); +SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1; +(SELECT SUM(t1.a) FROM t2 WHERE a=0) +NULL +SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1; +ERROR 21000: Subquery returns more than 1 row +SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1; +(SELECT SUM(t1.a) FROM t2 WHERE a=1) +3 +DROP TABLE t1,t2; +CREATE TABLE t1 (a1 INT, a2 INT); +CREATE TABLE t2 (b1 INT, b2 INT); +INSERT INTO t1 VALUES (100, 200); +INSERT INTO t1 VALUES (101, 201); +INSERT INTO t2 VALUES (101, 201); +INSERT INTO t2 VALUES (103, 203); +SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1; +((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL +0 +0 +DROP TABLE t1, t2; +CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5)); +INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43); +SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1); +s1 s2 +SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1); +s1 s2 +CREATE INDEX I1 ON t1 (s1); +CREATE INDEX I2 ON t1 (s2); +SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1); +s1 s2 +SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1); +s1 s2 +TRUNCATE t1; +INSERT INTO t1 VALUES (0x41,0x41); +SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1); +s1 s2 +DROP TABLE t1; +CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1)); +CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2)); +CREATE TABLE t3 (a3 BINARY(2) default '0'); +INSERT INTO t1 VALUES (1),(2),(3),(4); +INSERT INTO t2 VALUES (1),(2),(3); +INSERT INTO t3 VALUES (1),(2),(3); +SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2; +LEFT(t2.a2, 1) +1 +2 +3 +SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1; +a1 t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) +1 0 +2 0 +3 0 +4 0 +DROP TABLE t1,t2,t3; +CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3)); +CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY); +CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY); +INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40); +INSERT INTO t2 VALUES (2), (3), (4), (5); +INSERT INTO t3 VALUES (10), (20), (30); +SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3; +LEFT(t1.a1,1) +1 +2 +3 +SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3); +a2 +DROP TABLE t1, t2, t3; +SET @save_optimizer_switch=@@optimizer_switch; +SET optimizer_switch='semijoin_with_cache=off'; +SET optimizer_switch='materialization=off'; +CREATE TABLE t1 (a CHAR(1), b VARCHAR(10)); +INSERT INTO t1 VALUES ('a', 'aa'); +INSERT INTO t1 VALUES ('a', 'aaa'); +SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); +a b +CREATE INDEX I1 ON t1 (a); +CREATE INDEX I2 ON t1 (b); +EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where +1 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1) +SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); +a b +CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10)); +INSERT INTO t2 SELECT * FROM t1; +CREATE INDEX I1 ON t2 (a); +CREATE INDEX I2 ON t2 (b); +EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 ALL I2 NULL NULL NULL 2 Using where +1 PRIMARY t2 ref I1 I1 4 test.t2.b 2 Using where; Using index; FirstMatch(t2) +SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2); +a b +EXPLAIN +SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where +1 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1) +SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500); +a b +DROP TABLE t1,t2; +SET optimizer_switch= @save_optimizer_switch; +CREATE TABLE t1(a INT, b INT); +INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4); +EXPLAIN +SELECT a AS out_a, MIN(b) FROM t1 +WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a) +GROUP BY a; +ERROR 42S22: Unknown column 'out_a' in 'where clause' +SELECT a AS out_a, MIN(b) FROM t1 +WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a) +GROUP BY a; +ERROR 42S22: Unknown column 'out_a' in 'where clause' +EXPLAIN +SELECT a AS out_a, MIN(b) FROM t1 t1_outer +WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a) +GROUP BY a; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1_outer ALL NULL NULL NULL NULL 4 Using where; Using temporary; Using filesort +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 4 Using where +SELECT a AS out_a, MIN(b) FROM t1 t1_outer +WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a) +GROUP BY a; +out_a MIN(b) +1 2 +2 4 +DROP TABLE t1; +CREATE TABLE t1 (a INT); +CREATE TABLE t2 (a INT); +INSERT INTO t1 VALUES (1),(2); +INSERT INTO t2 VALUES (1),(2); +SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a)); +2 +2 +2 +EXPLAIN EXTENDED +SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a)); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 +1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 +2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 100.00 +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1003 select 2 AS `2` from `test`.`t1` semi join (`test`.`t2`) where 1 +EXPLAIN EXTENDED +SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION +(SELECT 1 FROM t2 WHERE t1.a = t2.a)); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where +3 DEPENDENT UNION t2 ALL NULL NULL NULL NULL 2 100.00 Using where +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL +Warnings: +Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 +Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1 +Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))) +DROP TABLE t1,t2; +create table t0(a int); +insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); +create table t1(f11 int, f12 int); +create table t2(f21 int unsigned not null, f22 int, f23 varchar(10)); +insert into t1 values(1,1),(2,2), (3, 3); +insert into t2 +select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a +from t0 A, t0 B, t0 C, t0 D; +set session sort_buffer_size= 33*1024; +select count(*) from t1 where f12 = +(select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1); +count(*) +3 +drop table t0,t1,t2; +CREATE TABLE t4 ( +f7 varchar(32) collate utf8_bin NOT NULL default '', +f10 varchar(32) collate utf8_bin default NULL, +PRIMARY KEY (f7) +); +INSERT INTO t4 VALUES(1,1), (2,null); +CREATE TABLE t2 ( +f4 varchar(32) collate utf8_bin NOT NULL default '', +f2 varchar(50) collate utf8_bin default NULL, +f3 varchar(10) collate utf8_bin default NULL, +PRIMARY KEY (f4), +UNIQUE KEY uk1 (f2) +); +INSERT INTO t2 VALUES(1,1,null), (2,2,null); +CREATE TABLE t1 ( +f8 varchar(32) collate utf8_bin NOT NULL default '', +f1 varchar(10) collate utf8_bin default NULL, +f9 varchar(32) collate utf8_bin default NULL, +PRIMARY KEY (f8) +); +INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2); +CREATE TABLE t3 ( +f6 varchar(32) collate utf8_bin NOT NULL default '', +f5 varchar(50) collate utf8_bin default NULL, +PRIMARY KEY (f6) +); +INSERT INTO t3 VALUES (1,null), (2,null); +SELECT +IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4, +IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3, +SUM( +IF( +(SELECT VPC.f2 +FROM t2 VPC, t4 a2, t2 a3 +WHERE +VPC.f4 = a2.f10 AND a3.f2 = a4 +LIMIT 1) IS NULL, +0, +t3.f5 +) +) AS a6 +FROM +t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4 +GROUP BY a4; +a4 f3 a6 +1 NULL NULL +2 NULL NULL +DROP TABLE t1, t2, t3, t4; +create table t1 (a float(5,4) zerofill); +create table t2 (a float(5,4),b float(2,0)); +select t1.a from t1 where +t1.a= (select b from t2 limit 1) and not +t1.a= (select a from t2 limit 1) ; +a +drop table t1, t2; +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2); +SET @save_join_cache_level=@@join_cache_level; +SET join_cache_level=0; +EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00 +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 +2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary +Warnings: +Note 1003 select 1 AS `1` from <materialize> (select min(`test`.`t1`.`a`) from `test`.`t1` group by `test`.`t1`.`a`) join `test`.`t1` where (`<subquery2>`.`min(a)` = 1) +EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 WHERE a > 3 GROUP BY a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00 +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 +2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Using temporary +Warnings: +Note 1003 select 1 AS `1` from <materialize> (select min(`test`.`t1`.`a`) from `test`.`t1` where (`test`.`t1`.`a` > 3) group by `test`.`t1`.`a`) join `test`.`t1` where (`<subquery2>`.`min(a)` = 1) +SET join_cache_level=@save_join_cache_level; +DROP TABLE t1; +# +# Bug#45061: Incorrectly market field caused wrong result. +# +CREATE TABLE `C` ( +`int_nokey` int(11) NOT NULL, +`int_key` int(11) NOT NULL, +KEY `int_key` (`int_key`) +); +INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4), +(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7), +(5,2), (1,8), (7,0), (0,9), (9,5); +SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); +int_nokey int_key +9 9 +0 0 +5 5 +0 0 +EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY C ALL NULL NULL NULL NULL 20 100.00 Using where +DROP TABLE C; +# End of test for bug#45061. +# +# Bug #46749: Segfault in add_key_fields() with outer subquery level +# field references +# +CREATE TABLE t1 ( +a int, +b int, +UNIQUE (a), KEY (b) +); +INSERT INTO t1 VALUES (1,1), (2,1); +CREATE TABLE st1 like t1; +INSERT INTO st1 VALUES (1,1), (2,1); +CREATE TABLE st2 like t1; +INSERT INTO st2 VALUES (1,1), (2,1); +EXPLAIN +SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) +FROM t1 +WHERE a = 230; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) +FROM t1 +WHERE a = 230; +MAX(b) (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) +NULL NULL +DROP TABLE t1, st1, st2; +# +# Bug #48709: Assertion failed in sql_select.cc:11782: +# int join_read_key(JOIN_TAB*) +# +CREATE TABLE t1 (pk int PRIMARY KEY, int_key int); +INSERT INTO t1 VALUES (10,1), (14,1); +CREATE TABLE t2 (pk int PRIMARY KEY, int_key int); +INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3); +# should have eq_ref for t1 +EXPLAIN +SELECT * FROM t2 outr +WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2) +ORDER BY outr.pk; +id select_type table type possible_keys key key_len ref rows Extra +x x outr ALL x x x x x x +x x t1 eq_ref x x x x x x +x x t2 index x x x x x x +# should not crash on debug binaries +SELECT * FROM t2 outr +WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2) +ORDER BY outr.pk; +pk int_key +3 3 +7 3 +DROP TABLE t1,t2; +# +# Bug#12329653 +# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY +# +CREATE TABLE t1(a1 int); +INSERT INTO t1 VALUES (1),(2); +SELECT @@session.sql_mode INTO @old_sql_mode; +SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; +EXPLAIN EXTENDED +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 +2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 +Warnings: +Note 1003 select 1 AS `1` from `test`.`t1` where 1 +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); +1 +1 +1 +PREPARE stmt FROM +'SELECT 1 UNION ALL +SELECT 1 FROM t1 +ORDER BY +(SELECT 1 FROM t1 AS t1_0 + WHERE 1 < SOME (SELECT a1 FROM t1) +)' ; +EXECUTE stmt ; +ERROR 21000: Subquery returns more than 1 row +EXECUTE stmt ; +ERROR 21000: Subquery returns more than 1 row +SET SESSION sql_mode=@old_sql_mode; +DEALLOCATE PREPARE stmt; +DROP TABLE t1; +# +# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER +# +CREATE TABLE t1(a1 int); +INSERT INTO t1 VALUES (1),(2); +CREATE TABLE t2(a1 int); +INSERT INTO t2 VALUES (3); +SELECT @@session.sql_mode INTO @old_sql_mode; +SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2); +1 +1 +1 +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2); +1 +1 +1 +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2); +1 +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2); +1 +1 +1 +SET SESSION sql_mode=@old_sql_mode; +DROP TABLE t1, t2; +# +# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER +# +create table t2(i int); +insert into t2 values(0); +SELECT @@session.sql_mode INTO @old_sql_mode; +SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; +CREATE VIEW v1 AS +SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2 +; +CREATE TABLE t1 ( +pk int NOT NULL, +col_varchar_key varchar(1) DEFAULT NULL, +PRIMARY KEY (pk), +KEY col_varchar_key (col_varchar_key) +); +SELECT t1.pk +FROM t1 +WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 ) +; +pk +SET SESSION sql_mode=@old_sql_mode; +drop table t2, t1; +drop view v1; +End of 5.0 tests. +create table t_out (subcase char(3), +a1 char(2), b1 char(2), c1 char(2)); +create table t_in (a2 char(2), b2 char(2), c2 char(2)); +insert into t_out values ('A.1','2a', NULL, '2a'); +insert into t_out values ('A.3', '2a', NULL, '2a'); +insert into t_out values ('A.4', '2a', NULL, 'xx'); +insert into t_out values ('B.1', '2a', '2a', '2a'); +insert into t_out values ('B.2', '2a', '2a', '2a'); +insert into t_out values ('B.3', '3a', 'xx', '3a'); +insert into t_out values ('B.4', 'xx', '3a', '3a'); +insert into t_in values ('1a', '1a', '1a'); +insert into t_in values ('2a', '2a', '2a'); +insert into t_in values (NULL, '2a', '2a'); +insert into t_in values ('3a', NULL, '3a'); + +Test general IN semantics (not top-level) + +case A.1 +select subcase, +(a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in, +(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in +from t_out where subcase = 'A.1'; +subcase pred_in pred_not_in +A.1 0 1 +case A.2 - impossible +case A.3 +select subcase, +(a1, b1, c1) IN (select * from t_in) pred_in, +(a1, b1, c1) NOT IN (select * from t_in) pred_not_in +from t_out where subcase = 'A.3'; +subcase pred_in pred_not_in +A.3 NULL NULL +case A.4 +select subcase, +(a1, b1, c1) IN (select * from t_in) pred_in, +(a1, b1, c1) NOT IN (select * from t_in) pred_not_in +from t_out where subcase = 'A.4'; +subcase pred_in pred_not_in +A.4 0 1 +case B.1 +select subcase, +(a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in, +(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in +from t_out where subcase = 'B.1'; +subcase pred_in pred_not_in +B.1 0 1 +case B.2 +select subcase, +(a1, b1, c1) IN (select * from t_in) pred_in, +(a1, b1, c1) NOT IN (select * from t_in) pred_not_in +from t_out where subcase = 'B.2'; +subcase pred_in pred_not_in +B.2 1 0 +case B.3 +select subcase, +(a1, b1, c1) IN (select * from t_in) pred_in, +(a1, b1, c1) NOT IN (select * from t_in) pred_not_in +from t_out where subcase = 'B.3'; +subcase pred_in pred_not_in +B.3 NULL NULL +case B.4 +select subcase, +(a1, b1, c1) IN (select * from t_in) pred_in, +(a1, b1, c1) NOT IN (select * from t_in) pred_not_in +from t_out where subcase = 'B.4'; +subcase pred_in pred_not_in +B.4 0 1 + +Test IN as top-level predicate, and +as non-top level for cases A.3, B.3 (the only cases with NULL result). + +case A.1 +select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out +where subcase = 'A.1' and +(a1, b1, c1) IN (select * from t_in where a1 = 'no_match'); +pred_in +F +select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out +where subcase = 'A.1' and +(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match'); +pred_not_in +T +select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out +where subcase = 'A.1' and +NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match')); +not_pred_in +T +case A.3 +select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out +where subcase = 'A.3' and +(a1, b1, c1) IN (select * from t_in); +pred_in +F +select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out +where subcase = 'A.3' and +(a1, b1, c1) NOT IN (select * from t_in); +pred_not_in +F +select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out +where subcase = 'A.3' and +NOT((a1, b1, c1) IN (select * from t_in)); +not_pred_in +F +select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out +where subcase = 'A.3' and +((a1, b1, c1) IN (select * from t_in)) is NULL and +((a1, b1, c1) NOT IN (select * from t_in)) is NULL; +pred_in +N +case A.4 +select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out +where subcase = 'A.4' and +(a1, b1, c1) IN (select * from t_in); +pred_in +F +select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out +where subcase = 'A.4' and +(a1, b1, c1) NOT IN (select * from t_in); +pred_not_in +T +select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out +where subcase = 'A.4' and +NOT((a1, b1, c1) IN (select * from t_in)); +not_pred_in +T +case B.1 +select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out +where subcase = 'B.1' and +(a1, b1, c1) IN (select * from t_in where a1 = 'no_match'); +pred_in +F +select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out +where subcase = 'B.1' and +(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match'); +pred_not_in +T +select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out +where subcase = 'B.1' and +NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match')); +not_pred_in +T +case B.2 +select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out +where subcase = 'B.2' and +(a1, b1, c1) IN (select * from t_in); +pred_in +T +select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out +where subcase = 'B.2' and +(a1, b1, c1) NOT IN (select * from t_in); +pred_not_in +F +select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out +where subcase = 'B.2' and +NOT((a1, b1, c1) IN (select * from t_in)); +not_pred_in +F +case B.3 +select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out +where subcase = 'B.3' and +(a1, b1, c1) IN (select * from t_in); +pred_in +F +select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out +where subcase = 'B.3' and +(a1, b1, c1) NOT IN (select * from t_in); +pred_not_in +F +select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out +where subcase = 'B.3' and +NOT((a1, b1, c1) IN (select * from t_in)); +not_pred_in +F +select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out +where subcase = 'B.3' and +((a1, b1, c1) IN (select * from t_in)) is NULL and +((a1, b1, c1) NOT IN (select * from t_in)) is NULL; +pred_in +N +case B.4 +select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out +where subcase = 'B.4' and +(a1, b1, c1) IN (select * from t_in); +pred_in +F +select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out +where subcase = 'B.4' and +(a1, b1, c1) NOT IN (select * from t_in); +pred_not_in +T +select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out +where subcase = 'B.4' and +NOT((a1, b1, c1) IN (select * from t_in)); +not_pred_in +T +drop table t_out; +drop table t_in; +CREATE TABLE t1 (a INT, b INT); +INSERT INTO t1 VALUES (2,22),(1,11),(2,22); +SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; +a +1 +2 +SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; +a +SELECT a FROM t1 t0 +WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; +a +1 +2 +SET @@sql_mode='ansi'; +SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; +ERROR HY000: Invalid use of group function +SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; +ERROR HY000: Invalid use of group function +SELECT a FROM t1 t0 +WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; +ERROR HY000: Invalid use of group function +SET @@sql_mode=default; +DROP TABLE t1; +CREATE TABLE t1 (s1 CHAR(1)); +INSERT INTO t1 VALUES ('a'); +SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1); +s1 +a +DROP TABLE t1; +CREATE TABLE t1(c INT, KEY(c)); +CREATE TABLE t2(a INT, b INT); +INSERT INTO t2 VALUES (1, 10), (2, NULL); +INSERT INTO t1 VALUES (1), (3); +SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10); +a b +DROP TABLE t1,t2; +CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a)); +INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20); +CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a)); +INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100); +SELECT * FROM t1 +WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b); +pk a +1 10 +DROP TABLE t1,t2; +CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b)); +INSERT INTO t1 VALUES (1,NULL), (9,NULL); +CREATE TABLE t2 ( +a INT, +b INT, +c INT, +d INT, +PRIMARY KEY (a), +UNIQUE KEY b (b,c,d), +KEY b_2 (b), +KEY c (c), +KEY d (d) +); +INSERT INTO t2 VALUES +(43, 2, 11 ,30), +(44, 2, 12 ,30), +(45, 1, 1 ,10000), +(46, 1, 2 ,10000), +(556,1, 32 ,10000); +CREATE TABLE t3 ( +a INT, +b INT, +c INT, +PRIMARY KEY (a), +UNIQUE KEY b (b,c), +KEY c (c), +KEY b_2 (b) +); +INSERT INTO t3 VALUES (1,1,1), (2,32,1); +explain +SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index +1 PRIMARY t3 ref b,b_2 b 5 test.t1.a 1 Using index +2 DEPENDENT SUBQUERY t2 ref b,b_2,c b 10 test.t3.c,test.t1.a 1 Using where; Using index; Using filesort +SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a; +a incorrect +1 1 +DROP TABLE t1,t2,t3; +CREATE TABLE t1 (id int); +CREATE TABLE t2 (id int, c int); +INSERT INTO t1 (id) VALUES (1); +INSERT INTO t2 (id) VALUES (1); +INSERT INTO t1 (id) VALUES (1); +INSERT INTO t2 (id) VALUES (1); +CREATE VIEW v1 AS +SELECT t2.c AS c FROM t1, t2 +WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION; +UPDATE v1 SET c=1; +CREATE VIEW v2 (a,b) AS +SELECT t2.id, t2.c AS c FROM t1, t2 +WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION; +INSERT INTO v2(a,b) VALUES (2,2); +ERROR HY000: CHECK OPTION failed 'test.v2' +SELECT * FROM v1; +c +1 +1 +1 +1 +CREATE VIEW v3 AS +SELECT t2.c AS c FROM t2 +WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION; +DELETE FROM v3; +DROP VIEW v1,v2,v3; +DROP TABLE t1,t2; +# +# BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result +# +create table t1(id integer primary key, g integer, v integer, s char(1)); +create table t2(id integer primary key, g integer, v integer, s char(1)); +insert into t1 values +(10, 10, 10, 'l'), +(20, 20, 20, 'l'), +(40, 40, 40, 'l'), +(41, 40, null, 'l'), +(50, 50, 50, 'l'), +(51, 50, null, 'l'), +(60, 60, 60, 'l'), +(61, 60, null, 'l'), +(70, 70, 70, 'l'), +(90, 90, null, 'l'); +insert into t2 values +(10, 10, 10, 'r'), +(30, 30, 30, 'r'), +(50, 50, 50, 'r'), +(60, 60, 60, 'r'), +(61, 60, null, 'r'), +(70, 70, 70, 'r'), +(71, 70, null, 'r'), +(80, 80, 80, 'r'), +(81, 80, null, 'r'), +(100,100,null, 'r'); +select * +from t1 +where v in(select v +from t2 +where t1.g=t2.g) is unknown; +id g v s +51 50 NULL l +61 60 NULL l +drop table t1, t2; +# +# Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result +# +create table t1(id integer primary key, g integer, v integer, s char(1)); +create table t2(id integer primary key, g integer, v integer, s char(1)); +insert into t1 values +(10, 10, 10, 'l'), +(20, 20, 20, 'l'), +(40, 40, 40, 'l'), +(41, 40, null, 'l'), +(50, 50, 50, 'l'), +(51, 50, null, 'l'), +(60, 60, 60, 'l'), +(61, 60, null, 'l'), +(70, 70, 70, 'l'), +(90, 90, null, 'l'); +insert into t2 values +(10, 10, 10, 'r'), +(30, 30, 30, 'r'), +(50, 50, 50, 'r'), +(60, 60, 60, 'r'), +(61, 60, null, 'r'), +(70, 70, 70, 'r'), +(71, 70, null, 'r'), +(80, 80, 80, 'r'), +(81, 80, null, 'r'), +(100,100,null, 'r'); +select * +from t1 +where v in(select v +from t2 +where t1.g=t2.g) is unknown; +id g v s +51 50 NULL l +61 60 NULL l +drop table t1, t2; +# +# Bug#33204: INTO is allowed in subselect, causing inconsistent results +# +CREATE TABLE t1( a INT ); +INSERT INTO t1 VALUES (1),(2); +CREATE TABLE t2( a INT, b INT ); +SELECT * +FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a; +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 'INTO @var FROM t1 WHERE a = 2) t1a' at line 2 +SELECT * +FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a; +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 'INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2 +SELECT * +FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a; +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 'INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2 +SELECT * FROM ( +SELECT 1 a +UNION +SELECT a INTO @var FROM t1 WHERE a = 2 +) t1a; +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 'INTO @var FROM t1 WHERE a = 2 +) t1a' at line 4 +SELECT * FROM ( +SELECT 1 a +UNION +SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2 +) t1a; +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 'INTO OUTFILE 'file' FROM t1 WHERE a = 2 +) t1a' at line 4 +SELECT * FROM ( +SELECT 1 a +UNION +SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2 +) t1a; +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 'INTO DUMPFILE 'file' FROM t1 WHERE a = 2 +) t1a' at line 4 +SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a; +a +2 +SELECT * FROM ( +SELECT a FROM t1 WHERE a = 2 +UNION +SELECT a FROM t1 WHERE a = 2 +) t1a; +a +2 +SELECT * FROM ( +SELECT 1 a +UNION +SELECT a FROM t1 WHERE a = 2 +UNION +SELECT a FROM t1 WHERE a = 2 +) t1a; +a +1 +2 +SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a); +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 +SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; +a +1 +SELECT * FROM (SELECT 1 UNION SELECT 1) t1a; +1 +1 +SELECT * FROM ((SELECT 1 a INTO @a)) t1a; +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 'INTO @a)) t1a' at line 1 +SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) t1a; +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 'INTO OUTFILE 'file' )) t1a' at line 1 +SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a; +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 'INTO DUMPFILE 'file' )) t1a' at line 1 +SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) t1a; +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 'INTO @a)) t1a' at line 1 +SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE 'file' )) t1a; +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 'INTO DUMPFILE 'file' )) t1a' at line 1 +SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE 'file' )) t1a; +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 'INTO OUTFILE 'file' )) t1a' at line 1 +SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) t1a; +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 'INTO @a))) t1a' at line 1 +SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE 'file' ))) t1a; +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 'INTO DUMPFILE 'file' ))) t1a' at line 1 +SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a; +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 'INTO OUTFILE 'file' ))) t1a' at line 1 +SELECT * FROM (SELECT 1 a ORDER BY a) t1a; +a +1 +SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a; +a +1 +SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a; +a +1 +SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a; +a +1 +SELECT * FROM t1 JOIN (SELECT 1 UNION SELECT 1) alias ON 1; +a 1 +1 1 +2 1 +SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 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 ')) ON 1' at line 1 +SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 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 'ON 1' at line 1 +SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 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 ') ON 1' at line 1 +SELECT * FROM t1 JOIN (t1 t1a) t1a ON 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 't1a ON 1' at line 1 +SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 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 't1a ON 1' at line 1 +SELECT * FROM t1 JOIN (t1 t1a) ON 1; +a a +1 1 +2 1 +1 2 +2 2 +SELECT * FROM t1 JOIN ((t1 t1a)) ON 1; +a a +1 1 +2 1 +1 2 +2 2 +SELECT * FROM (t1 t1a); +a +1 +2 +SELECT * FROM ((t1 t1a)); +a +1 +2 +SELECT * FROM t1 JOIN (SELECT 1 t1a) alias ON 1; +a t1a +1 1 +2 1 +SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1; +a t1a +1 1 +2 1 +SELECT * FROM t1 JOIN (SELECT 1 a) a ON 1; +a a +1 1 +2 1 +SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1; +a a +1 1 +2 1 +SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2; +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 't1a2' at line 1 +SELECT * FROM t1 WHERE a = ALL ( SELECT 1 ); +a +1 +SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 ); +a +1 +SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 ); +a +1 +SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a); +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 'INTO @a)' at line 1 +SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' ); +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 'INTO OUTFILE 'file' )' at line 1 +SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' ); +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 'INTO DUMPFILE 'file' )' at line 1 +SELECT * FROM t1 WHERE a = ( SELECT 1 ); +a +1 +SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 ); +a +1 +SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a); +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 'INTO @a)' at line 1 +SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' ); +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 'INTO OUTFILE 'file' )' at line 1 +SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' ); +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 'INTO DUMPFILE 'file' )' at line 1 +SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a); +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 'INTO @a)' at line 1 +SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' ); +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 'INTO OUTFILE 'file' )' at line 1 +SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' ); +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 'INTO DUMPFILE 'file' )' at line 1 +SELECT ( SELECT 1 INTO @v ); +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 'INTO @v )' at line 1 +SELECT ( SELECT 1 INTO OUTFILE 'file' ); +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 'INTO OUTFILE 'file' )' at line 1 +SELECT ( SELECT 1 INTO DUMPFILE 'file' ); +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 'INTO DUMPFILE 'file' )' at line 1 +SELECT ( SELECT 1 UNION SELECT 1 INTO @v ); +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 'INTO @v )' at line 1 +SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' ); +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 'INTO OUTFILE 'file' )' at line 1 +SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' ); +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 'INTO DUMPFILE 'file' )' at line 1 +SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1; +( SELECT a FROM t1 WHERE a = 1 ) a +1 1 +1 2 +SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1; +( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ) a +1 1 +1 2 +SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2); +a b +SELECT 1 UNION ( SELECT 1 UNION SELECT 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 'UNION SELECT 1 )' at line 1 +( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 'UNION SELECT 1 ) UNION SELECT 1' at line 1 +SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 'UNION SELECT 1 ) )' at line 1 +SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 'UNION SELECT 1' at line 1 +SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); +( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) +1 +SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1)); +((SELECT 1 UNION SELECT 1 UNION SELECT 1)) +1 +SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 'UNION SELECT 1 ) )' at line 1 +SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 ') UNION SELECT 1 )' at line 1 +SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a; +1 +1 +SELECT * FROM t1 WHERE a = ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 'UNION SELECT 1 ) )' at line 1 +SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 'UNION SELECT 1 ) )' at line 1 +SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 'UNION SELECT 1 ) )' at line 1 +SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 'UNION SELECT 1 ) )' at line 1 +SELECT * FROM t1 WHERE a = ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 'UNION SELECT 1 )' at line 1 +SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 'UNION SELECT 1 ) UNION SELECT 1 )' at line 1 +SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 'UNION SELECT 1 ) UNION SELECT 1 )' at line 1 +SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 'UNION SELECT 1 )' at line 1 +SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); +a +1 +SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); +a +1 +SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); +a +1 +SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); +a +1 +SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v ); +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 'INTO @v )' at line 1 +SELECT EXISTS(SELECT 1+1); +EXISTS(SELECT 1+1) +1 +SELECT EXISTS(SELECT 1+1 INTO @test); +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 'INTO @test)' at line 1 +SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 INTO @v ); +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 'INTO @v )' at line 1 +SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v ); +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 'INTO @v )' at line 1 +SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v ); +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 'INTO @v )' at line 1 +DROP TABLE t1, t2; +CREATE TABLE t1 (a ENUM('rainbow')); +INSERT INTO t1 VALUES (),(),(),(),(); +SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID())); +1 +1 +DROP TABLE t1; +CREATE TABLE t1 (a LONGBLOB); +INSERT INTO t1 SET a = 'aaaa'; +INSERT INTO t1 SET a = 'aaaa'; +SELECT 1 FROM t1 GROUP BY +(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1); +1 +1 +DROP TABLE t1; +# +# Bug #49512 : subquery with aggregate function crash +# subselect_single_select_engine::exec() +CREATE TABLE t1(a INT); +INSERT INTO t1 VALUES(); +# should not crash +SELECT 1 FROM t1 WHERE a <> SOME +( +SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d +FROM t1,t1 a +); +1 +DROP TABLE t1; +# +# Bug #45989 take 2 : memory leak after explain encounters an +# error in the query +# +CREATE TABLE t1(a LONGTEXT); +INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet)); +INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet)); +EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1, +(SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1 +WHERE t1.a = d1.a; +ERROR 42S22: Unknown column 'd1.a' in 'where clause' +DROP TABLE t1; +Set up test tables. +CREATE TABLE t1 ( +t1_id INT UNSIGNED, +PRIMARY KEY(t1_id) +) Engine=MyISAM; +INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5); +CREATE TABLE t2 SELECT * FROM t1; +CREATE TABLE t3 ( +t3_id INT UNSIGNED AUTO_INCREMENT, +t1_id INT UNSIGNED, +amount DECIMAL(16,2), +PRIMARY KEY(t3_id), +KEY(t1_id) +) Engine=MyISAM; +INSERT INTO t3 (t1_id, t3_id, amount) +VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00); +This is the 'inner query' running by itself. +Produces correct results. +SELECT +t1.t1_id, +IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount +FROM +t1 +LEFT JOIN t2 ON t2.t1_id=t1.t1_id +GROUP BY +t1.t1_id +; +t1_id total_amount +1 100.00 +2 200.00 +3 0.00 +4 400.00 +5 0.00 +SELECT * FROM (the same inner query) +Produces correct results. +SELECT * FROM ( +SELECT +t1.t1_id, +IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount +FROM +t1 +LEFT JOIN t2 ON t2.t1_id=t1.t1_id +GROUP BY +t1.t1_id +) AS t; +t1_id total_amount +1 100.00 +2 200.00 +3 0.00 +4 400.00 +5 0.00 +Now make t2.t1_id part of a key. +ALTER TABLE t2 ADD PRIMARY KEY(t1_id); +Same inner query by itself. +Still correct results. +SELECT +t1.t1_id, +IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount +FROM +t1 +LEFT JOIN t2 ON t2.t1_id=t1.t1_id +GROUP BY +t1.t1_id; +t1_id total_amount +1 100.00 +2 200.00 +3 0.00 +4 400.00 +5 0.00 +SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN +SELECT * FROM ( +SELECT +t1.t1_id, +IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount +FROM +t1 +LEFT JOIN t2 ON t2.t1_id=t1.t1_id +GROUP BY +t1.t1_id +) AS t; +t1_id total_amount +1 100.00 +2 200.00 +3 0.00 +4 400.00 +5 0.00 +DROP TABLE t3; +DROP TABLE t2; +DROP TABLE t1; +# +# Bug #52711: Segfault when doing EXPLAIN SELECT with +# union...order by (select... where...) +# +CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a)); +INSERT INTO t1 VALUES (1),(2); +CREATE TABLE t2 (b INT); +INSERT INTO t2 VALUES (1),(2); +# Should not crash +EXPLAIN +SELECT * FROM t2 UNION SELECT * FROM t2 +ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE)); +# Should not crash +SELECT * FROM t2 UNION SELECT * FROM t2 +ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE)); +DROP TABLE t1,t2; +# +# Bug #58818: Incorrect result for IN/ANY subquery +# with HAVING condition +# +CREATE TABLE t1(i INT); +INSERT INTO t1 VALUES (1), (2), (3); +CREATE TABLE t1s(i INT); +INSERT INTO t1s VALUES (10), (20), (30); +CREATE TABLE t2s(i INT); +INSERT INTO t2s VALUES (100), (200), (300); +SELECT * FROM t1 +WHERE t1.i NOT IN +( +SELECT STRAIGHT_JOIN t2s.i +FROM +t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i +HAVING t2s.i = 999 +); +i +1 +2 +3 +SELECT * FROM t1 +WHERE t1.I IN +( +SELECT STRAIGHT_JOIN t2s.i +FROM +t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i +HAVING t2s.i = 999 +) IS UNKNOWN; +i +SELECT * FROM t1 +WHERE NOT t1.I = ANY +( +SELECT STRAIGHT_JOIN t2s.i +FROM +t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i +HAVING t2s.i = 999 +); +i +1 +2 +3 +SELECT * FROM t1 +WHERE t1.i = ANY ( +SELECT STRAIGHT_JOIN t2s.i +FROM +t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i +HAVING t2s.i = 999 +) IS UNKNOWN; +i +DROP TABLE t1,t1s,t2s; +# LP BUG#675248 - select->prep_where references on freed memory +CREATE TABLE t1 (a int, b int); +insert into t1 values (1,1),(0,0); +CREATE TABLE t2 (c int); +insert into t2 values (1),(2); +prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub +from t1 as table1 group by sub"; +execute stmt1; +sum(a) sub +0 NULL +1 3 +deallocate prepare stmt1; +prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub +from t1 as table1"; +execute stmt1; +sum(a) sub +1 3 +deallocate prepare stmt1; +drop table t1,t2; +# +# Bug LP#693935/#58727: Assertion failure with +# a single row subquery returning more than one row +# +create table t1 (a char(1) charset utf8); +insert into t1 values ('a'), ('b'); +create table t2 (a binary(1)); +insert into t2 values ('x'), ('y'); +select * from t2 where a=(select a from t1) and a='x'; +ERROR 21000: Subquery returns more than 1 row +drop table t1,t2; +End of 5.1 tests +# +# Bug #11765713 58705: +# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES +# CREATED BY OPT_SUM_QUERY +# +CREATE TABLE t1(a INT NOT NULL, KEY (a)); +INSERT INTO t1 VALUES (0), (1); +SELECT 1 as foo FROM t1 WHERE a < SOME +(SELECT a FROM t1 WHERE a <=> +(SELECT a FROM t1) +); +ERROR 21000: Subquery returns more than 1 row +SELECT 1 as foo FROM t1 WHERE a < SOME +(SELECT a FROM t1 WHERE a <=> +(SELECT a FROM t1 where a is null) +); +foo +DROP TABLE t1; +# +# Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool): +# Assertion `file' failed. +# +CREATE TABLE t1 (a INT); +SELECT 1 FROM +(SELECT ROW( +(SELECT 1 FROM t1 RIGHT JOIN +(SELECT 1 FROM t1, t1 t2) AS d ON 1), +1) FROM t1) AS e; +ERROR 21000: Operand should contain 1 column(s) +DROP TABLE t1; +# +# Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY +# +CREATE TABLE t1(a TIME NOT NULL); +INSERT INTO t1 VALUES ('00:00:32'); +SELECT 1 FROM t1 WHERE a > +(SELECT timestamp(a) AS a FROM t1); +1 +DROP TABLE t1; +# +# No BUG#, a case brought from 5.2's innodb_mysql_lock.test +# +create table t1 (i int not null primary key); +insert into t1 values (1),(2),(3),(4),(5); +create table t2 (j int not null primary key); +insert into t2 values (1),(2),(3),(4),(5); +create table t3 (k int not null primary key); +insert into t3 values (1),(2),(3); +create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1); +select * from t3 where k in (select j from v2); +k +1 +2 +3 +drop table t1,t2,t3; +drop view v2; +# +# Bug#52068: Optimizer generates invalid semijoin materialization plan +# +drop table if exists ot1, ot2, it1, it2; +CREATE TABLE ot1(a INTEGER); +INSERT INTO ot1 VALUES(5), (8); +CREATE TABLE it2(a INTEGER); +INSERT INTO it2 VALUES(9), (5), (1), (8); +CREATE TABLE it3(a INTEGER); +INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4); +CREATE TABLE ot4(a INTEGER); +INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1); +SELECT * FROM ot1,ot4 +WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a +FROM it2,it3); +a a +5 1 +8 1 +5 5 +8 5 +5 7 +8 7 +5 7 +8 7 +5 1 +8 1 +explain SELECT * FROM ot1,ot4 +WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a +FROM it2,it3); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY ot1 ALL NULL NULL NULL NULL 2 +1 PRIMARY ot4 ALL NULL NULL NULL NULL 8 Using join buffer (flat, BNL join) +1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 +2 MATERIALIZED it2 ALL NULL NULL NULL NULL 4 +2 MATERIALIZED it3 ALL NULL NULL NULL NULL 6 Using join buffer (flat, BNL join) +DROP TABLE IF EXISTS ot1, ot4, it2, it3; +# +# Bug#729039: NULL keys used to evaluate subquery +# +CREATE TABLE t1 (a int) ; +INSERT INTO t1 VALUES (NULL), (1), (NULL), (2); +CREATE TABLE t2 (a int, INDEX idx(a)) ; +INSERT INTO t2 VALUES (NULL), (1), (NULL); +SELECT * FROM t1 +WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a); +a +1 +EXPLAIN +SELECT * FROM t1 +WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 3 +1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Using join buffer (flat, BNL join) +2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 +SELECT * FROM t1 +WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a); +a +1 +EXPLAIN +SELECT * FROM t1 +WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 3 +1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Using join buffer (flat, BNL join) +2 MATERIALIZED t2 index idx idx 5 NULL 3 Using index +DROP TABLE t1,t2; +# +# BUG#752992: Wrong results for a subquery with 'semijoin=on' +# +CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL); +INSERT INTO t1 VALUES (11,0); +INSERT INTO t1 VALUES (12,5); +INSERT INTO t1 VALUES (15,0); +CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL); +INSERT INTO t2 VALUES (11,1); +INSERT INTO t2 VALUES (12,2); +INSERT INTO t2 VALUES (15,4); +SET @save_join_cache_level=@@join_cache_level; +SET join_cache_level=0; +EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 3 +1 PRIMARY it eq_ref PRIMARY PRIMARY 4 test.t1.pk 1 Using index +1 PRIMARY t2 index NULL PRIMARY 4 NULL 3 Using index; FirstMatch(it) +SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1); +pk i +11 0 +12 5 +15 0 +SET join_cache_level=@save_join_cache_level; +DROP table t1,t2; +# +# Bug#751350: crash with pushed condition for outer references when +# there should be none of such conditions +# +CREATE TABLE t1 (a int, b int) ; +INSERT INTO t1 VALUES (0,0),(0,0); +set @optimizer_switch_save=@@optimizer_switch; +set @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=on'; +EXPLAIN +SELECT b FROM t1 +WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a ) +GROUP BY b; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort +2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where +SELECT b FROM t1 +WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a ) +GROUP BY b; +b +0 +set @@optimizer_switch=@optimizer_switch_save; +DROP TABLE t1; +# +# Bug #11765713 58705: +# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES +# CREATED BY OPT_SUM_QUERY +# +CREATE TABLE t1(a INT NOT NULL, KEY (a)); +INSERT INTO t1 VALUES (0), (1); +SELECT 1 as foo FROM t1 WHERE a < SOME +(SELECT a FROM t1 WHERE a <=> +(SELECT a FROM t1) +); +ERROR 21000: Subquery returns more than 1 row +SELECT 1 as foo FROM t1 WHERE a < SOME +(SELECT a FROM t1 WHERE a <=> +(SELECT a FROM t1 where a is null) +); +foo +DROP TABLE t1; +CREATE TABLE t1 (a int(11), b varchar(1)); +INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g'); +SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b ); +a +5 +SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 ); +a +5 +SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b ); +a +7 +SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 ); +a +7 +SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b ); +a +5 +7 +SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 ); +a +5 +7 +SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b ); +a +5 +7 +SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 ); +a +5 +7 +SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 ); +a +5 +7 +SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b ); +a +5 +7 +SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 ); +a +5 +7 +SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b ); +a +5 +7 +SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b ); +a +SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b ); +a +SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b ); +a +SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b ); +a +SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b ); +a +SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b ); +a +delete from t1; +INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g'); +SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b ); +a +5 +SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 ); +a +5 +SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b ); +a +7 +SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 ); +a +7 +SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b ); +a +5 +7 +SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 ); +a +5 +7 +SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b ); +a +5 +7 +SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 ); +a +5 +7 +SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 ); +a +5 +7 +SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b ); +a +5 +7 +SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 ); +a +5 +7 +SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b ); +a +5 +7 +SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b ); +a +SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b ); +a +SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b ); +a +SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b ); +a +SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b ); +a +SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 ); +a +SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b ); +a +drop table t1; +# +# Fix of lp:780386 (NULL left part with empty ALL subquery). +# +CREATE TABLE t1 ( f11 int) ; +INSERT IGNORE INTO t1 VALUES (0),(0); +CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3)) ; +INSERT IGNORE INTO t2 VALUES (NULL,NULL),(5,0); +DROP TABLE IF EXISTS t3; +Warnings: +Note 1051 Unknown table 't3' +CREATE TABLE t3 ( f3 int) ; +INSERT INTO t3 VALUES (0),(0); +SELECT a1.f3 AS r FROM t2 AS a1 , t1 WHERE a1.f3 < ALL ( SELECT f3 FROM t3 WHERE f3 = 1 ) ; +r +NULL +5 +NULL +5 +DROP TABLE t1, t2, t3; +# +# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER +# +CREATE TABLE t1(a1 int); +INSERT INTO t1 VALUES (1),(2); +CREATE TABLE t2(a1 int); +INSERT INTO t2 VALUES (3); +SELECT @@session.sql_mode INTO @old_sql_mode; +SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2); +1 +1 +1 +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2); +1 +1 +1 +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2); +1 +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2); +1 +1 +1 +SET SESSION sql_mode=@old_sql_mode; +DROP TABLE t1, t2; +create table t2(i int); +insert into t2 values(0); +SELECT @@session.sql_mode INTO @old_sql_mode; +SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; +CREATE VIEW v1 AS +SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2 +; +CREATE TABLE t1 ( +pk int NOT NULL, +col_varchar_key varchar(1) DEFAULT NULL, +PRIMARY KEY (pk), +KEY col_varchar_key (col_varchar_key) +); +SELECT t1.pk +FROM t1 +WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 ) +; +pk +SET SESSION sql_mode=@old_sql_mode; +drop table t2, t1; +drop view v1; +# +# BUG#50257: Missing info in REF column of the EXPLAIN +# lines for subselects +# +CREATE TABLE t1 (a INT, b INT, INDEX (a)); +INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20); +EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref a a 5 const 1 +EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 4 +2 SUBQUERY t1 ref a a 5 const 1 Using index +DROP TABLE t1; +# +# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS) +# (duplicate of LP bug #888456) +# +CREATE TABLE t1 (f1 varchar(1)); +INSERT INTO t1 VALUES ('v'),('s'); +CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key)); +INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'), +('d'),('y'),('t'),('d'),('s'); +EXPLAIN +SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2 +WHERE EXISTS (SELECT DISTINCT f1_key FROM t2 +WHERE f1_key != table2.f1_key AND f1_key >= table1.f1); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY table1 ALL NULL NULL NULL NULL 2 +1 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join) +2 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index +SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2 +WHERE EXISTS (SELECT DISTINCT f1_key FROM t2 +WHERE f1_key != table2.f1_key AND f1_key >= table1.f1); +f1 f1_key +v j +s j +v v +s v +v c +s c +v m +s m +v d +s d +v d +s d +v y +s y +v t +s t +v d +s d +v s +s s +DROP TABLE t1,t2; +# +# LP bug 919427: EXPLAIN for a query over a single-row table +# with IN subquery in WHERE condition +# +CREATE TABLE ot ( +col_int_nokey int(11), +col_varchar_nokey varchar(1) +) ; +INSERT INTO ot VALUES (1,'x'); +CREATE TABLE it1( +col_int_key int(11), +col_varchar_key varchar(1), +KEY idx_cvk_cik (col_varchar_key,col_int_key) +); +INSERT INTO it1 VALUES (NULL,'x'), (NULL,'f'); +CREATE TABLE it2 ( +col_int_key int(11), +col_varchar_key varchar(1), +col_varchar_key2 varchar(1), +KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key), +KEY idx_cvk_cik (col_varchar_key, col_int_key) +); +INSERT INTO it2 VALUES (NULL,'x','x'), (NULL,'f','f'); +EXPLAIN +SELECT col_int_nokey FROM ot +WHERE col_varchar_nokey IN +(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY ot system NULL NULL NULL NULL 1 +1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 +2 MATERIALIZED it1 ref idx_cvk_cik idx_cvk_cik 9 const,const 1 Using where; Using index +SELECT col_int_nokey FROM ot +WHERE col_varchar_nokey IN +(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL); +col_int_nokey +1 +EXPLAIN +SELECT col_int_nokey FROM ot +WHERE (col_varchar_nokey, 'x') IN +(SELECT col_varchar_key, col_varchar_key2 FROM it2); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY ot system NULL NULL NULL NULL 1 +1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 +2 MATERIALIZED it2 ref idx_cvk_cvk2_cik,idx_cvk_cik idx_cvk_cvk2_cik 8 const,const 1 Using where; Using index +SELECT col_int_nokey FROM ot +WHERE (col_varchar_nokey, 'x') IN +(SELECT col_varchar_key, col_varchar_key2 FROM it2); +col_int_nokey +1 +DROP TABLE ot,it1,it2; +# +# MDEV-746 +# Bug#13651009 WRONG RESULT FROM DERIVED TABLE IF THE SUBQUERY +# HAS AN EMPTY RESULT +# +CREATE TABLE t1 ( +pk int NOT NULL, +col_int_nokey int NOT NULL, +col_int_key int NOT NULL, +col_time_key time NOT NULL, +col_varchar_key varchar(1) NOT NULL, +col_varchar_nokey varchar(1) NOT NULL, +PRIMARY KEY (pk), +KEY col_int_key (col_int_key), +KEY col_time_key (col_time_key), +KEY col_varchar_key (col_varchar_key,col_int_key) +) ENGINE=MyISAM; +CREATE TABLE t2 ( +pk int NOT NULL AUTO_INCREMENT, +col_int_nokey int NOT NULL, +col_int_key int NOT NULL, +col_time_key time NOT NULL, +col_varchar_key varchar(1) NOT NULL, +col_varchar_nokey varchar(1) NOT NULL, +PRIMARY KEY (pk), +KEY col_int_key (col_int_key), +KEY col_time_key (col_time_key), +KEY col_varchar_key (col_varchar_key,col_int_key) +) ENGINE=MyISAM; +INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b'); +SET @var2:=4, @var3:=8; + +Testcase without inner subquery +EXPLAIN SELECT @var3:=12, sq4_alias1.* +FROM t1 AS sq4_alias1 +WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR +sq4_alias1.col_varchar_key = @var3; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +SELECT @var3:=12, sq4_alias1.* +FROM t1 AS sq4_alias1 +WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR +sq4_alias1.col_varchar_key = @var3; +@var3:=12 pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey +SELECT @var3; +@var3 +8 +EXPLAIN SELECT * FROM ( SELECT @var3:=12, sq4_alias1.* +FROM t1 AS sq4_alias1 +WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR +sq4_alias1.col_varchar_key = @var3 ) AS alias3; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE <derived2> system NULL NULL NULL NULL 0 const row not found +2 DERIVED NULL NULL NULL NULL NULL NULL NULL no matching row in const table +SELECT * FROM ( SELECT @var3:=12, sq4_alias1.* +FROM t1 AS sq4_alias1 +WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR +sq4_alias1.col_varchar_key = @var3 ) AS alias3; +@var3:=12 pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey +SELECT @var3; +@var3 +8 + +Testcase with inner subquery; crashed WL#6095 +SET @var3=8; +EXPLAIN SELECT sq4_alias1.* +FROM t1 AS sq4_alias1 +WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) +NOT IN +(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, +c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 +FROM t2 AS c_sq1_alias1 +WHERE (c_sq1_alias1.col_int_nokey != @var2 +OR c_sq1_alias1.pk != @var3)); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +2 DEPENDENT SUBQUERY c_sq1_alias1 system PRIMARY NULL NULL NULL 1 +SELECT sq4_alias1.* +FROM t1 AS sq4_alias1 +WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) +NOT IN +(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, +c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 +FROM t2 AS c_sq1_alias1 +WHERE (c_sq1_alias1.col_int_nokey != @var2 +OR c_sq1_alias1.pk != @var3)); +pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey +EXPLAIN SELECT * FROM ( SELECT sq4_alias1.* +FROM t1 AS sq4_alias1 +WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) +NOT IN +(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, +c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 +FROM t2 AS c_sq1_alias1 +WHERE (c_sq1_alias1.col_int_nokey != @var2 +OR c_sq1_alias1.pk != @var3)) ) AS alias3; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +3 DEPENDENT SUBQUERY c_sq1_alias1 system PRIMARY NULL NULL NULL 1 +SELECT * FROM ( SELECT sq4_alias1.* +FROM t1 AS sq4_alias1 +WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) +NOT IN +(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, +c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 +FROM t2 AS c_sq1_alias1 +WHERE (c_sq1_alias1.col_int_nokey != @var2 +OR c_sq1_alias1.pk != @var3)) ) AS alias3; +pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey +DROP TABLE t1,t2; +End of 5.2 tests +# +# BUG#779885: Crash in eliminate_item_equal with materialization=on in +# +CREATE TABLE t1 ( f1 int ); +INSERT INTO t1 VALUES (19), (20); +CREATE TABLE t2 ( f10 varchar(32) ); +INSERT INTO t2 VALUES ('c'),('d'); +CREATE TABLE t3 ( f10 varchar(32) ); +INSERT INTO t3 VALUES ('a'),('b'); +SELECT * +FROM t1 +WHERE +( 't' ) IN ( +SELECT t3.f10 +FROM t3 +JOIN t2 +ON t2.f10 = t3.f10 +); +f1 +DROP TABLE t1,t2,t3; +# +# BUG lp:813473: Wrong result with outer join + NOT IN subquery +# This bug is a duplicate of Bug#11764086 whose test case is added below +# +CREATE TABLE t1 (c int) ; +INSERT INTO t1 VALUES (5),(6); +CREATE TABLE t2 (a int, b int) ; +INSERT INTO t2 VALUES (20,9),(20,9); +create table t3 (d int, e int); +insert into t3 values (2, 9), (3,10); +SET @save_optimizer_switch=@@optimizer_switch; +SET optimizer_switch='outer_join_with_cache=off'; +EXPLAIN +SELECT t2.b , t1.c +FROM t2 LEFT JOIN t1 ON t1.c < 3 +WHERE (t2.b , t1.c) NOT IN (SELECT * from t3); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 ALL NULL NULL NULL NULL 2 +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where +2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where +SELECT t2.b , t1.c +FROM t2 LEFT JOIN t1 ON t1.c < 3 +WHERE (t2.b, t1.c) NOT IN (SELECT * from t3); +b c +9 NULL +9 NULL +SET optimizer_switch=@save_optimizer_switch; +drop table t1, t2, t3; +# +# BUG#50257: Missing info in REF column of the EXPLAIN +# lines for subselects +# +CREATE TABLE t1 (a INT, b INT, INDEX (a)); +INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20); + +set @tmp_optimizer_switch=@@optimizer_switch; +set optimizer_switch='derived_merge=off,derived_with_keys=off'; +EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 +2 DERIVED t1 ref a a 5 const 1 +set optimizer_switch=@tmp_optimizer_switch; + +EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 4 +2 SUBQUERY t1 ref a a 5 const 1 Using index + +DROP TABLE t1; +# +# Bug#11764086: Null left operand to NOT IN in WHERE clause +# behaves differently than real NULL +# +CREATE TABLE parent (id int); +INSERT INTO parent VALUES (1), (2); +CREATE TABLE child (parent_id int, other int); +INSERT INTO child VALUES (1,NULL); +# Offending query (c.parent_id is NULL for null-complemented rows only) +SELECT p.id, c.parent_id +FROM parent p +LEFT JOIN child c +ON p.id = c.parent_id +WHERE c.parent_id NOT IN ( +SELECT parent_id +FROM child +WHERE parent_id = 3 +); +id parent_id +1 1 +2 NULL +# Some syntactic variations with IS FALSE and IS NOT TRUE +SELECT p.id, c.parent_id +FROM parent p +LEFT JOIN child c +ON p.id = c.parent_id +WHERE c.parent_id IN ( +SELECT parent_id +FROM child +WHERE parent_id = 3 +) IS NOT TRUE; +id parent_id +1 1 +2 NULL +SELECT p.id, c.parent_id +FROM parent p +LEFT JOIN child c +ON p.id = c.parent_id +WHERE c.parent_id IN ( +SELECT parent_id +FROM child +WHERE parent_id = 3 +) IS FALSE; +id parent_id +1 1 +2 NULL +DROP TABLE parent, child; +# End of test for bug#11764086. +# +# Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET || +# BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX +# +CREATE TABLE t1(a INT); +INSERT INTO t1 VALUES (0), (1); +CREATE TABLE t2( +b TEXT, +c INT, +PRIMARY KEY (b(1)) +); +INSERT INTO t2 VALUES ('a', 2), ('b', 3); +SELECT 1 FROM t1 WHERE a = +(SELECT 1 FROM t2 WHERE b = +(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2) +ORDER BY b +); +1 +SELECT 1 FROM t1 WHERE a = +(SELECT 1 FROM t2 WHERE b = +(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2) +GROUP BY b +); +1 +DROP TABLE t1, t2; +# +# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS) +# +CREATE TABLE t1 (f1 varchar(1)); +INSERT INTO t1 VALUES ('v'),('s'); +CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key)); +INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'), +('d'),('y'),('t'),('d'),('s'); +SELECT table1.f1, table2.f1_key +FROM t1 AS table1, t2 AS table2 +WHERE EXISTS +( +SELECT DISTINCT f1_key +FROM t2 +WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 ); +f1 f1_key +v j +s j +v v +s v +v c +s c +v m +s m +v d +s d +v d +s d +v y +s y +v t +s t +v d +s d +v s +s s +explain SELECT table1.f1, table2.f1_key +FROM t1 AS table1, t2 AS table2 +WHERE EXISTS +( +SELECT DISTINCT f1_key +FROM t2 +WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 ); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY table1 ALL NULL NULL NULL NULL 2 +1 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join) +2 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index +DROP TABLE t1,t2; +# +# lp:826279: assertion failure with GROUP BY a result of subquery +# +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (0), (0); +CREATE TABLE t2 (a int, b int, c int); +INSERT INTO t2 VALUES (10,7,0), (0,7,0); +CREATE TABLE t3 (a int, b int); +INSERT INTO t3 VALUES (10,7), (0,7); +SELECT SUM(DISTINCT b), +(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0 +WHERE t.a != 0 AND t2.a != 0) +FROM (SELECT * FROM t3) AS t +GROUP BY 2; +SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0 +WHERE t.a != 0 AND t2.a != 0) +7 NULL +SELECT SUM(DISTINCT b), +(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1) +FROM (SELECT * FROM t3) AS t +GROUP BY 2; +SUM(DISTINCT b) (SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1) +7 NULL +7 10 +DROP TABLE t1,t2,t3; +# +# Bug#12329653 +# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY +# +CREATE TABLE t1(a1 int); +INSERT INTO t1 VALUES (1),(2); +SELECT @@session.sql_mode INTO @old_sql_mode; +SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; +SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); +1 +1 +1 +PREPARE stmt FROM +'SELECT 1 UNION ALL +SELECT 1 FROM t1 +ORDER BY +(SELECT 1 FROM t1 AS t1_0 + WHERE 1 < SOME (SELECT a1 FROM t1) +)' ; +EXECUTE stmt ; +ERROR 21000: Subquery returns more than 1 row +EXECUTE stmt ; +ERROR 21000: Subquery returns more than 1 row +SET SESSION sql_mode=@old_sql_mode; +DEALLOCATE PREPARE stmt; +DROP TABLE t1; +# +# LP BUG#833777 Performance regression with deeply nested subqueries +# +create table t1 (a int not null, b char(10) not null); +insert into t1 values (1, 'a'); +set @@optimizer_switch='in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off'; +select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1))))))))))))))))))))))))))))); +a +1 +set @@optimizer_switch=@subselect_tmp; +drop table t1; +# +# LP BUG#894397 Wrong result with in_to_exists, constant table , semijoin=OFF,materialization=OFF +# +CREATE TABLE t1 (a varchar(3)); +INSERT INTO t1 VALUES ('AAA'),('BBB'); +CREATE TABLE t2 (a varchar(3)); +INSERT INTO t2 VALUES ('CCC'); +set @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off'; +SELECT * FROM t1 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a < 'ZZZ'); +a +set @@optimizer_switch=@subselect_tmp; +drop table t1, t2; +# +# LP bug #859375: Assertion `0' failed in st_select_lex_unit::optimize +# with view , UNION and prepared statement (rewriting fake_select +# condition). +# +CREATE TABLE t1 ( f1 int NOT NULL, f4 varchar(1) NOT NULL) ; +INSERT INTO t1 VALUES (6,'d'),(7,'y'); +CREATE TABLE t2 ( f1 int NOT NULL, f2 int NOT NULL) ; +INSERT INTO t2 VALUES (10,7); +CREATE VIEW v2 AS SELECT * FROM t2; +PREPARE st1 FROM " + SELECT * + FROM t1 + LEFT JOIN v2 ON ( v2.f2 = t1.f1 ) + WHERE v2.f1 NOT IN ( + SELECT 1 UNION + SELECT 247 + ) +"; +EXECUTE st1; +f1 f4 f1 f2 +7 y 10 7 +deallocate prepare st1; +DROP VIEW v2; +DROP TABLE t1,t2; +# +# LP bug #887458 Crash in subselect_union_engine::no_rows with +# double UNION and join_cache_level=3,8 +# (IN/ALL/ANY optimizations should not be applied to fake_select) +CREATE TABLE t2 ( a int, b varchar(1)) ; +INSERT IGNORE INTO t2 VALUES (8,'y'),(8,'y'); +CREATE TABLE t1 ( b varchar(1)) ; +INSERT IGNORE INTO t1 VALUES (NULL),(NULL); +set @save_join_cache_level=@@join_cache_level; +SET SESSION join_cache_level=3; +SELECT * +FROM t1, t2 +WHERE t2.b IN ( +SELECT 'm' UNION +SELECT 'm' +) OR t1.b <> SOME ( +SELECT 'v' UNION +SELECT 't' +); +b a b +set @@join_cache_level= @save_join_cache_level; +drop table t1,t2; +# +# LP bug #885162 Got error 124 from storage engine with UNION inside +# subquery and join_cache_level=3..8 +# (IN/ALL/ANY optimizations should not be applied to fake_select) +# +CREATE TABLE t1 ( +f1 varchar(1) DEFAULT NULL +); +INSERT INTO t1 VALUES ('c'); +set @save_join_cache_level=@@join_cache_level; +SET SESSION join_cache_level=8; +SELECT * FROM t1 WHERE t1.f1 IN ( SELECT 'k' UNION SELECT 'e' ); +f1 +set @@join_cache_level= @save_join_cache_level; +drop table t1; +# +# LP BUG#747278 incorrect values of the NULL (no rows) single +# row subquery requested via element_index() interface +# +CREATE TABLE t1 (f1a int, f1b int) ; +INSERT IGNORE INTO t1 VALUES (1,1),(2,2); +CREATE TABLE t2 ( f2 int); +INSERT IGNORE INTO t2 VALUES (3),(4); +CREATE TABLE t3 (f3a int default 1, f3b int default 2); +INSERT INTO t3 VALUES (1,1),(2,2); +set @old_optimizer_switch = @@session.optimizer_switch; +set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off'; +SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2; +(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) +NULL +NULL +SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2; +(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) +NULL +NULL +SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2; +(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) +NULL +NULL +SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1); +(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) +NULL +SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2; +(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) +NULL +NULL +SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2; +(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) +NULL +NULL +SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2; +(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) +NULL +NULL +SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1); +(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) +NULL +set @@session.optimizer_switch=@old_optimizer_switch; +SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2; +(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) +NULL +NULL +SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2; +(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) +NULL +NULL +SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2; +(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) +NULL +NULL +SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1); +(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) +NULL +SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2; +(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) +NULL +NULL +SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2; +(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) +NULL +NULL +SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2; +(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) +NULL +NULL +SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1); +(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) +NULL +select (null, null) = (null, null); +(null, null) = (null, null) +NULL +SELECT (SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0); +(SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0) +NULL +drop tables t1,t2,t3; +# +# LP BUG#825051 Wrong result with date/datetime and subquery with GROUP BY and in_to_exists +# +CREATE TABLE t1 (a date, KEY (a)) ; +INSERT INTO t1 VALUES ('2009-01-01'),('2009-02-02'); +set @old_optimizer_switch = @@optimizer_switch; +SET @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off'; +EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index +2 DEPENDENT SUBQUERY t1 index_subquery a a 4 func 2 Using index +SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); +a +2009-01-01 +2009-02-02 +SET @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=off,subquery_cache=off'; +EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index +2 MATERIALIZED t1 index NULL a 4 NULL 2 Using index +SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); +a +2009-01-01 +2009-02-02 +set @@optimizer_switch=@old_optimizer_switch; +drop table t1; +# +# LP BUG#908269 incorrect condition in case of subqueries depending +# on constant tables +# +CREATE TABLE t1 ( a INT ); +INSERT INTO t1 VALUES (1),(5); +CREATE TABLE t2 ( b INT ) ENGINE=MyISAM; +INSERT INTO t2 VALUES (1); +CREATE TABLE t3 ( c INT ); +INSERT INTO t3 VALUES (4),(5); +SET optimizer_switch='subquery_cache=off'; +SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1; +( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) +1 +NULL +SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1; +( SELECT b FROM t2 WHERE b = a OR b * 0) +1 +NULL +SELECT ( SELECT b FROM t2 WHERE b = a OR rand() * 0) FROM t1; +( SELECT b FROM t2 WHERE b = a OR rand() * 0) +1 +NULL +drop table t1,t2,t3; +set optimizer_switch=@subselect_tmp; +# +# LP BUG#905353 Wrong non-empty result with a constant table, +# aggregate function in subquery, MyISAM or Aria +# +CREATE TABLE t1 ( a INT ) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1); +SELECT a FROM t1 WHERE ( SELECT MIN(a) = 100 ); +a +drop table t1; +# +# LP BUG#985667 Wrong result with subquery in SELECT clause, and constant table in +# main query and implicit grouping +# +CREATE TABLE t1 (f1 int) engine=MyISAM; +INSERT INTO t1 VALUES (7),(8); +CREATE TABLE t2 (f2 int, f3 varchar(1)) engine=MyISAM; +INSERT INTO t2 VALUES (3,'f'); +EXPLAIN +SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 +SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; +COUNT(f1) f4 +0 NULL +EXPLAIN +SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 +SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; +COUNT(f1) f4 +0 0 +EXPLAIN +SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where +SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; +COUNT(f1) f4 +0 1 +EXPLAIN +SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where +SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; +COUNT(f1) f4 +0 0 +drop table t1,t2; +# +# LP BUG#1002079 Server crashes in Item_singlerow_subselect::val_int with constant table, +# HAVING, UNION in subquery +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (7),(0); +CREATE TABLE t2 (b INT); +EXPLAIN +SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t2 system NULL NULL NULL NULL 0 const row not found +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used +3 UNION NULL NULL NULL NULL NULL NULL NULL No tables used +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL +SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7; +f1 f2 +drop table t1,t2; +# +# LP BUG#1008686 Server crashes in subselect_union_engine::no_rows on SELECT with impossible +# WHERE and UNION in HAVING +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(7); +EXPLAIN +SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used +3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL +SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1; +min_a a +EXPLAIN +SELECT MIN(a) AS min_a, a FROM t1 WHERE 1=2 HAVING a NOT IN ( SELECT a from t1 UNION select a+1 from t1 ) OR min_a != 1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where +3 DEPENDENT UNION t1 ALL NULL NULL NULL NULL 2 Using where +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL +SELECT MIN(a) AS min_a, a FROM t1 WHERE 1=2 HAVING a NOT IN ( SELECT a from t1 UNION select a+1 from t1 ) OR min_a != 1; +min_a a +drop table t1; +# +# MDEV-367: Different results with and without subquery_cache on +# a query with a constant NOT IN condition +# +CREATE TABLE t1 (a INT) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(2),(3); +set @mdev367_optimizer_switch = @@optimizer_switch; +set optimizer_switch = 'subquery_cache=on'; +SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100; +a +SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1; +a ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) +1 NULL +2 NULL +3 NULL +set optimizer_switch=@mdev367_optimizer_switch; +set optimizer_switch = 'subquery_cache=off'; +SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100; +a +SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1; +a ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) +1 NULL +2 NULL +3 NULL +set optimizer_switch=@mdev367_optimizer_switch; +DROP TABLE t1; +# +# MDEV-521 single value subselect transformation problem +# +CREATE TABLE t1 (f1 char(2), PRIMARY KEY (f1)) ENGINE=MyISAM; +INSERT INTO t1 VALUES ('u1'),('u2'); +SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) ); +f1 +u1 +u2 +FLUSH TABLES; +SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) ); +f1 +u1 +u2 +DROP TABLE t1; +# return optimizer switch changed in the beginning of this test +set optimizer_switch=@subselect_tmp; +# +# lp:944706 Query with impossible or constant subquery in WHERE or HAVING is not +# precomputed and thus not part of optimization +# +CREATE TABLE t1 ( a VARCHAR(16), KEY (a) ); +INSERT INTO t1 VALUES ('Abilene'),('Akron'),('Albany'),('Albuquerque'),('Alexandria'),('Allentown'), +('Amarillo'),('Anaheim'),('Anchorage'),('Ann Arbor'),('Arden-Arcade'); +EXPLAIN +SELECT MAX( alias2.a ) AS field +FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 +WHERE alias1.a = alias2.a OR alias1.a = 'y' +HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 ); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible HAVING +2 SUBQUERY t1 index_subquery a a 19 const 1 Using index; Using where +SELECT MAX( alias2.a ) AS field +FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 +WHERE alias1.a = alias2.a OR alias1.a = 'y' +HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 ); +field +EXPLAIN +SELECT MAX( alias2.a ) +FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 +WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 ); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY alias1 index a a 19 NULL 11 Using where; Using index +1 PRIMARY alias2 ref a a 19 test.alias1.a 2 Using index +1 PRIMARY alias3 index NULL a 19 NULL 11 Using index; Using join buffer (flat, BNL join) +2 SUBQUERY t1 index_subquery a a 19 const 1 Using index; Using where +SELECT MAX( alias2.a ) +FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 +WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 ); +MAX( alias2.a ) +Arden-Arcade +drop table t1; +# +# MDEV-277 CHEAP SQ: Server crashes in st_join_table::get_examined_rows +# with semijoin+materialization, IN and = subqueries +# +CREATE TABLE t1 (a1 INT); +INSERT INTO t1 VALUES (4),(6); +CREATE TABLE t2 (b1 INT); +INSERT INTO t2 VALUES (1),(7); +EXPLAIN +SELECT * FROM t1 +WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2)); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where +2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 +2 SUBQUERY <subquery3> eq_ref distinct_key distinct_key 4 func 1 +3 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 +3 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) +SELECT * FROM t1 +WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2)); +a1 +drop table t1, t2; +# +# MDEV-287 CHEAP SQ: A query with subquery in SELECT list, EXISTS, +# inner joins takes hundreds times longer +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(7); +CREATE TABLE t2 (b INT); +INSERT INTO t2 VALUES (4),(5); +CREATE TABLE t3 (c INT); +INSERT INTO t3 VALUES (8),(3); +set @@expensive_subquery_limit= 0; +EXPLAIN +SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) +FROM t2 alias1, t1 alias2, t1 alias3; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY alias1 ALL NULL NULL NULL NULL 2 +1 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) +1 PRIMARY alias3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where +2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) +3 SUBQUERY t3 ALL NULL NULL NULL NULL 2 +flush status; +SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) +FROM t2 alias1, t1 alias2, t1 alias3; +(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) +NULL +NULL +NULL +NULL +NULL +NULL +NULL +NULL +show status like "subquery_cache%"; +Variable_name Value +Subquery_cache_hit 6 +Subquery_cache_miss 2 +show status like '%Handler_read%'; +Variable_name Value +Handler_read_first 0 +Handler_read_key 8 +Handler_read_last 0 +Handler_read_next 0 +Handler_read_prev 0 +Handler_read_rnd 0 +Handler_read_rnd_deleted 0 +Handler_read_rnd_next 22 +set @@expensive_subquery_limit= default; +EXPLAIN +SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) +FROM t2 alias1, t1 alias2, t1 alias3; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY alias1 ALL NULL NULL NULL NULL 2 +1 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) +1 PRIMARY alias3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) +2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 +2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) +3 SUBQUERY t3 ALL NULL NULL NULL NULL 2 +flush status; +SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) +FROM t2 alias1, t1 alias2, t1 alias3; +(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) +NULL +NULL +NULL +NULL +NULL +NULL +NULL +NULL +show status like "subquery_cache%"; +Variable_name Value +Subquery_cache_hit 0 +Subquery_cache_miss 0 +show status like '%Handler_read%'; +Variable_name Value +Handler_read_first 0 +Handler_read_key 0 +Handler_read_last 0 +Handler_read_next 0 +Handler_read_prev 0 +Handler_read_rnd 0 +Handler_read_rnd_deleted 0 +Handler_read_rnd_next 16 +drop table t1, t2, t3; +# +# MDEV-288 CHEAP SQ: Valgrind warnings "Memory lost" with IN and EXISTS nested subquery, materialization+semijoin +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (0),(8); +CREATE TABLE t2 (b INT PRIMARY KEY); +INSERT INTO t2 VALUES (1),(2); +EXPLAIN +SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +3 SUBQUERY t1 ALL NULL NULL NULL NULL 2 +SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); +a +drop table t1,t2; +# +# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used +# +CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM; +INSERT INTO t1 VALUES ('USA'); +CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM; +INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia'); +CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM; +INSERT INTO t3 VALUES (12),(22),(9),(45); +create table t4 like t3; +insert into t4 select * from t3; +# This should not show range access for table t2 +explain +SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1 +WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 ); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 system NULL NULL NULL NULL 1 +1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where +1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index +3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +set @tmp_mdev410=@@global.userstat; +set global userstat=on; +flush table_statistics; +flush index_statistics; +SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1 +WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 ); +MIN(b) +NULL +# The following shows that t2 was indeed scanned with a full scan. +show table_statistics; +Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes +test t1 2 0 0 +test t2 3 0 0 +show index_statistics; +Table_schema Table_name Index_name Rows_read +test t2 b 1 +set global userstat=@tmp_mdev410; +DROP TABLE t1,t2,t3,t4; +# +# MDEV-430: Server crashes in select_describe on EXPLAIN with +# materialization+semijoin, 2 nested subqueries, aggregate functions +# +CREATE TABLE t1 (a INT, KEY(a)); +INSERT INTO t1 VALUES (1),(8); +CREATE TABLE t2 (b INT, KEY(b)); +INSERT INTO t2 VALUES (45),(17),(20); +EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index +2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index +DROP TABLE t1,t2; +# +# MDEV-435: Expensive subqueries may be evaluated during optimization in merge_key_fields +# +CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM; +INSERT INTO t1 VALUES (8),(0); +CREATE TABLE t2 (b INT, c VARCHAR(1)) ENGINE=MyISAM; +INSERT INTO t2 VALUES (4,'j'),(6,'v'); +CREATE TABLE t3 (d VARCHAR(1)) ENGINE=MyISAM; +INSERT INTO t3 VALUES ('b'),('c'); +EXPLAIN +SELECT * FROM t1 +WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 index a a 5 NULL 2 Using where; Using index +2 SUBQUERY <subquery3> ALL distinct_key NULL NULL NULL 1 +2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) +3 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 +SELECT * FROM t1 +WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10; +a +drop table t1, t2, t3; +# +# MDEV-405: Server crashes in test_if_skip_sort_order on EXPLAIN with GROUP BY and HAVING in EXISTS subquery +# +CREATE TABLE t1 (a INT, KEY(a)); +INSERT INTO t1 VALUES (1),(8); +CREATE TABLE t2 (b INT, KEY(b)); +INSERT INTO t2 VALUES (45),(17),(20); +EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index +2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index +DROP TABLE t1,t2; +set optimizer_switch=default; +select @@optimizer_switch like '%exists_to_in=on%'; +@@optimizer_switch like '%exists_to_in=on%' +0 diff --git a/mysql-test/r/subselect_innodb.result b/mysql-test/r/subselect_innodb.result index a6967527a2d..8932acf8ffd 100644 --- a/mysql-test/r/subselect_innodb.result +++ b/mysql-test/r/subselect_innodb.result @@ -248,6 +248,47 @@ NULL drop procedure p1; drop tables t1,t2,t3; # +# Bug #58756 +# Crash in heap_rrnd on query with HAVING ... IN (subquery) + LIMIT +# +CREATE TABLE t1 ( +col_time_key time DEFAULT NULL, +col_datetime_key datetime DEFAULT NULL, +col_varchar_nokey varchar(1) DEFAULT NULL, +KEY col_time_key (col_time_key), +KEY col_datetime_key (col_datetime_key) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; +INSERT INTO t1 VALUES ('17:53:30','2005-11-10 12:40:29','h'); +INSERT INTO t1 VALUES ('11:35:49','2009-04-25 00:00:00','b'); +INSERT INTO t1 VALUES (NULL,'2002-11-27 00:00:00','s'); +INSERT INTO t1 VALUES ('06:01:40','2004-01-26 20:32:32','e'); +INSERT INTO t1 VALUES ('05:45:11','2007-10-26 11:41:40','j'); +INSERT INTO t1 VALUES ('00:00:00','2005-10-07 00:00:00','e'); +INSERT INTO t1 VALUES ('00:00:00','2000-07-15 05:00:34','f'); +INSERT INTO t1 VALUES ('06:11:01','2000-04-03 16:33:32','v'); +INSERT INTO t1 VALUES ('13:02:46',NULL,'x'); +INSERT INTO t1 VALUES ('21:44:25','2001-04-25 01:26:12','m'); +INSERT INTO t1 VALUES ('22:43:58','2000-12-27 00:00:00','c'); +CREATE TABLE t2 ( +col_time_key time DEFAULT NULL, +col_datetime_key datetime DEFAULT NULL, +col_varchar_nokey varchar(1) DEFAULT NULL, +KEY col_time_key (col_time_key), +KEY col_datetime_key (col_datetime_key) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; +INSERT INTO t2 VALUES ('11:28:45','2004-10-11 18:13:16','w'); +SELECT col_time_key, col_datetime_key +FROM +( SELECT * FROM t1 ) AS table1 +HAVING ( 'r' , 'e' ) IN +( SELECT col_varchar_nokey , col_varchar_nokey FROM t2 ) +ORDER BY col_datetime_key +LIMIT 10; +col_time_key col_datetime_key +DROP TABLE t1; +DROP TABLE t2; +# End of Bug #58756 +# # Bug#60085 crash in Item::save_in_field() with time data type # CREATE TABLE t1(a date, b int, unique(b), unique(a), key(b)) engine=innodb; diff --git a/mysql-test/r/subselect_mat_cost.result b/mysql-test/r/subselect_mat_cost.result index 081196a227b..a9f980fff5d 100644 --- a/mysql-test/r/subselect_mat_cost.result +++ b/mysql-test/r/subselect_mat_cost.result @@ -348,7 +348,7 @@ FROM City LEFT JOIN Country ON (Country = Code and City.Population < 10000)) AND Language IN ('English','Spanish'); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY CountryLanguage range Language Language 30 NULL 72 Using index condition; Using where; Rowid-ordered scan -2 DEPENDENT SUBQUERY City ref CityName CityName 35 func 1 Using index condition; Using where +2 DEPENDENT SUBQUERY City ref CityName CityName 35 func 1 Using index condition 2 DEPENDENT SUBQUERY Country eq_ref PRIMARY PRIMARY 3 world.City.Country 1 Using where; Using index select count(*) from CountryLanguage @@ -399,10 +399,10 @@ WHERE Code = Country GROUP BY Code) order by Country; id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY CountryLanguage index NULL PRIMARY 33 NULL 984 Using where; Using index -3 MATERIALIZED CountryLanguage index PRIMARY PRIMARY 33 NULL 984 Using index; Using temporary -3 MATERIALIZED Country eq_ref PRIMARY PRIMARY 3 world.CountryLanguage.Country 1 Using index -2 MATERIALIZED CountryLanguage index PRIMARY PRIMARY 33 NULL 984 Using index; Using temporary -2 MATERIALIZED Country eq_ref PRIMARY PRIMARY 3 world.CountryLanguage.Country 1 Using index +3 MATERIALIZED Country index PRIMARY PRIMARY 3 NULL 239 Using index +3 MATERIALIZED CountryLanguage ref PRIMARY PRIMARY 3 world.Country.Code 4 Using index +2 MATERIALIZED Country index PRIMARY PRIMARY 3 NULL 239 Using index +2 MATERIALIZED CountryLanguage ref PRIMARY PRIMARY 3 world.Country.Code 4 Using index select count(*) from CountryLanguage where diff --git a/mysql-test/r/subselect_no_mat.result b/mysql-test/r/subselect_no_mat.result index feaeff50f7d..8a5016d47ee 100644 --- a/mysql-test/r/subselect_no_mat.result +++ b/mysql-test/r/subselect_no_mat.result @@ -2973,7 +2973,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where Warnings: -Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1` +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)))) and trigcond(trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1` explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 @@ -2985,7 +2985,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where Warnings: -Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1` +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)))) and trigcond(trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1` DROP TABLE t1,t2; set optimizer_switch=@tmp11867_optimizer_switch; CREATE TABLE t1 (a char(5), b char(5)); diff --git a/mysql-test/r/subselect_no_opts.result b/mysql-test/r/subselect_no_opts.result index d1590b0df51..13467916039 100644 --- a/mysql-test/r/subselect_no_opts.result +++ b/mysql-test/r/subselect_no_opts.result @@ -2969,7 +2969,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where Warnings: -Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1` +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)))) and trigcond(trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1` explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 Using where @@ -2981,7 +2981,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where Warnings: -Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1` +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)))) and trigcond(trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1` DROP TABLE t1,t2; set optimizer_switch=@tmp11867_optimizer_switch; CREATE TABLE t1 (a char(5), b char(5)); diff --git a/mysql-test/r/subselect_no_scache.result b/mysql-test/r/subselect_no_scache.result index 2fd58c075d2..b53405ad8bd 100644 --- a/mysql-test/r/subselect_no_scache.result +++ b/mysql-test/r/subselect_no_scache.result @@ -2972,7 +2972,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where Warnings: -Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1` +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)))) and trigcond(trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1` explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 @@ -2985,7 +2985,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where Warnings: -Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1` +Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`)))) and trigcond(trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`))))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1` DROP TABLE t1,t2; set optimizer_switch=@tmp11867_optimizer_switch; CREATE TABLE t1 (a char(5), b char(5)); diff --git a/mysql-test/r/symlink.result b/mysql-test/r/symlink.result index 20736aec47f..279af1e44d2 100644 --- a/mysql-test/r/symlink.result +++ b/mysql-test/r/symlink.result @@ -120,7 +120,7 @@ CREATE TABLE t2(a INT) DATA DIRECTORY='TEST_DIR/tmp' INDEX DIRECTORY='TEST_DIR/tmp'; RENAME TABLE t2 TO t1; -ERROR HY000: Can't create/write to file 'TEST_DIR/tmp/t1.MYI' (Errcode: 17) +ERROR HY000: Can't create/write to file 'TEST_DIR/tmp/t1.MYI' (Errcode: 17 "File exists") DROP TABLE t2; create temporary table t1 (a int) engine=myisam data directory="MYSQLTEST_VARDIR/log" select 9 a; show create table t1; @@ -167,7 +167,7 @@ INDEX DIRECTORY='MYSQLD_DATADIR'; DROP TABLE IF EXISTS t1; CREATE TABLE t1(a INT) INDEX DIRECTORY='TEST_DIR/master-data_var'; -ERROR HY000: Can't create/write to file 'TEST_DIR/master-data_var/t1.MYI' (Errcode: 2) +ERROR HY000: Can't create/write to file 'TEST_DIR/master-data_var/t1.MYI' (Errcode: 2 "No such file or directory") SET @OLD_SQL_MODE=@@SQL_MODE, @@SQL_MODE='NO_DIR_IN_CREATE'; CREATE TABLE t1(a INT) DATA DIRECTORY='MYSQLTEST_VARDIR/tmp' INDEX DIRECTORY='MYSQLTEST_VARDIR/tmp'; Warnings: diff --git a/mysql-test/r/system_mysql_db.result b/mysql-test/r/system_mysql_db.result index d5409136ed4..4bd5c45d944 100644 --- a/mysql-test/r/system_mysql_db.result +++ b/mysql-test/r/system_mysql_db.result @@ -1,5 +1,6 @@ show tables; Tables_in_db +column_stats columns_priv db event @@ -10,6 +11,7 @@ help_keyword help_relation help_topic host +index_stats ndb_binlog_index plugin proc @@ -17,6 +19,7 @@ procs_priv proxies_priv servers slow_log +table_stats tables_priv time_zone time_zone_leap_second @@ -242,7 +245,7 @@ event CREATE TABLE `event` ( show create table general_log; Table Create Table general_log CREATE TABLE `general_log` ( - `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `thread_id` int(11) NOT NULL, `server_id` int(10) unsigned NOT NULL, @@ -252,7 +255,7 @@ general_log CREATE TABLE `general_log` ( show create table slow_log; Table Create Table slow_log CREATE TABLE `slow_log` ( - `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `query_time` time(6) NOT NULL, `lock_time` time(6) NOT NULL, @@ -264,5 +267,36 @@ slow_log CREATE TABLE `slow_log` ( `server_id` int(10) unsigned NOT NULL, `sql_text` mediumtext NOT NULL ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log' +show create table table_stats; +Table Create Table +table_stats CREATE TABLE `table_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `cardinality` bigint(21) unsigned DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Tables' +show create table column_stats; +Table Create Table +column_stats CREATE TABLE `column_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `column_name` varchar(64) COLLATE utf8_bin NOT NULL, + `min_value` varchar(255) COLLATE utf8_bin DEFAULT NULL, + `max_value` varchar(255) COLLATE utf8_bin DEFAULT NULL, + `nulls_ratio` decimal(12,4) DEFAULT NULL, + `avg_length` decimal(12,4) DEFAULT NULL, + `avg_frequency` decimal(12,4) DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`,`column_name`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Columns' +show create table index_stats; +Table Create Table +index_stats CREATE TABLE `index_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `index_name` varchar(64) COLLATE utf8_bin NOT NULL, + `prefix_arity` int(11) unsigned NOT NULL, + `avg_frequency` decimal(12,4) DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`,`index_name`,`prefix_arity`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Indexes' show tables; Tables_in_test diff --git a/mysql-test/r/system_mysql_db_fix40123.result b/mysql-test/r/system_mysql_db_fix40123.result index d5409136ed4..51def0fae6c 100644 --- a/mysql-test/r/system_mysql_db_fix40123.result +++ b/mysql-test/r/system_mysql_db_fix40123.result @@ -1,5 +1,6 @@ show tables; Tables_in_db +column_stats columns_priv db event @@ -10,6 +11,7 @@ help_keyword help_relation help_topic host +index_stats ndb_binlog_index plugin proc @@ -17,6 +19,7 @@ procs_priv proxies_priv servers slow_log +table_stats tables_priv time_zone time_zone_leap_second @@ -242,7 +245,7 @@ event CREATE TABLE `event` ( show create table general_log; Table Create Table general_log CREATE TABLE `general_log` ( - `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `thread_id` int(11) NOT NULL, `server_id` int(10) unsigned NOT NULL, @@ -252,7 +255,7 @@ general_log CREATE TABLE `general_log` ( show create table slow_log; Table Create Table slow_log CREATE TABLE `slow_log` ( - `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `query_time` time(6) NOT NULL, `lock_time` time(6) NOT NULL, @@ -264,5 +267,36 @@ slow_log CREATE TABLE `slow_log` ( `server_id` int(10) unsigned NOT NULL, `sql_text` mediumtext NOT NULL ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log' +show create table table_stats; +Table Create Table +table_stats CREATE TABLE `table_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `cardinality` bigint(21) unsigned DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Tables' +show create table column_stats; +Table Create Table +column_stats CREATE TABLE `column_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `column_name` varchar(64) COLLATE utf8_bin NOT NULL, + `min_value` varchar(255) COLLATE utf8_bin DEFAULT NULL, + `max_value` varchar(255) COLLATE utf8_bin DEFAULT NULL, + `nulls_ratio` double DEFAULT NULL, + `avg_length` double DEFAULT NULL, + `avg_frequency` double DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`,`column_name`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Columns' +show create table index_stats; +Table Create Table +index_stats CREATE TABLE `index_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `index_name` varchar(64) COLLATE utf8_bin NOT NULL, + `prefix_arity` int(11) unsigned NOT NULL, + `avg_frequency` double DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`,`index_name`,`prefix_arity`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Indexes' show tables; Tables_in_test diff --git a/mysql-test/r/system_mysql_db_fix50030.result b/mysql-test/r/system_mysql_db_fix50030.result index d5409136ed4..51def0fae6c 100644 --- a/mysql-test/r/system_mysql_db_fix50030.result +++ b/mysql-test/r/system_mysql_db_fix50030.result @@ -1,5 +1,6 @@ show tables; Tables_in_db +column_stats columns_priv db event @@ -10,6 +11,7 @@ help_keyword help_relation help_topic host +index_stats ndb_binlog_index plugin proc @@ -17,6 +19,7 @@ procs_priv proxies_priv servers slow_log +table_stats tables_priv time_zone time_zone_leap_second @@ -242,7 +245,7 @@ event CREATE TABLE `event` ( show create table general_log; Table Create Table general_log CREATE TABLE `general_log` ( - `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `thread_id` int(11) NOT NULL, `server_id` int(10) unsigned NOT NULL, @@ -252,7 +255,7 @@ general_log CREATE TABLE `general_log` ( show create table slow_log; Table Create Table slow_log CREATE TABLE `slow_log` ( - `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `query_time` time(6) NOT NULL, `lock_time` time(6) NOT NULL, @@ -264,5 +267,36 @@ slow_log CREATE TABLE `slow_log` ( `server_id` int(10) unsigned NOT NULL, `sql_text` mediumtext NOT NULL ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log' +show create table table_stats; +Table Create Table +table_stats CREATE TABLE `table_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `cardinality` bigint(21) unsigned DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Tables' +show create table column_stats; +Table Create Table +column_stats CREATE TABLE `column_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `column_name` varchar(64) COLLATE utf8_bin NOT NULL, + `min_value` varchar(255) COLLATE utf8_bin DEFAULT NULL, + `max_value` varchar(255) COLLATE utf8_bin DEFAULT NULL, + `nulls_ratio` double DEFAULT NULL, + `avg_length` double DEFAULT NULL, + `avg_frequency` double DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`,`column_name`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Columns' +show create table index_stats; +Table Create Table +index_stats CREATE TABLE `index_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `index_name` varchar(64) COLLATE utf8_bin NOT NULL, + `prefix_arity` int(11) unsigned NOT NULL, + `avg_frequency` double DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`,`index_name`,`prefix_arity`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Indexes' show tables; Tables_in_test diff --git a/mysql-test/r/system_mysql_db_fix50117.result b/mysql-test/r/system_mysql_db_fix50117.result index d5409136ed4..51def0fae6c 100644 --- a/mysql-test/r/system_mysql_db_fix50117.result +++ b/mysql-test/r/system_mysql_db_fix50117.result @@ -1,5 +1,6 @@ show tables; Tables_in_db +column_stats columns_priv db event @@ -10,6 +11,7 @@ help_keyword help_relation help_topic host +index_stats ndb_binlog_index plugin proc @@ -17,6 +19,7 @@ procs_priv proxies_priv servers slow_log +table_stats tables_priv time_zone time_zone_leap_second @@ -242,7 +245,7 @@ event CREATE TABLE `event` ( show create table general_log; Table Create Table general_log CREATE TABLE `general_log` ( - `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `thread_id` int(11) NOT NULL, `server_id` int(10) unsigned NOT NULL, @@ -252,7 +255,7 @@ general_log CREATE TABLE `general_log` ( show create table slow_log; Table Create Table slow_log CREATE TABLE `slow_log` ( - `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `query_time` time(6) NOT NULL, `lock_time` time(6) NOT NULL, @@ -264,5 +267,36 @@ slow_log CREATE TABLE `slow_log` ( `server_id` int(10) unsigned NOT NULL, `sql_text` mediumtext NOT NULL ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log' +show create table table_stats; +Table Create Table +table_stats CREATE TABLE `table_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `cardinality` bigint(21) unsigned DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Tables' +show create table column_stats; +Table Create Table +column_stats CREATE TABLE `column_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `column_name` varchar(64) COLLATE utf8_bin NOT NULL, + `min_value` varchar(255) COLLATE utf8_bin DEFAULT NULL, + `max_value` varchar(255) COLLATE utf8_bin DEFAULT NULL, + `nulls_ratio` double DEFAULT NULL, + `avg_length` double DEFAULT NULL, + `avg_frequency` double DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`,`column_name`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Columns' +show create table index_stats; +Table Create Table +index_stats CREATE TABLE `index_stats` ( + `db_name` varchar(64) COLLATE utf8_bin NOT NULL, + `table_name` varchar(64) COLLATE utf8_bin NOT NULL, + `index_name` varchar(64) COLLATE utf8_bin NOT NULL, + `prefix_arity` int(11) unsigned NOT NULL, + `avg_frequency` double DEFAULT NULL, + PRIMARY KEY (`db_name`,`table_name`,`index_name`,`prefix_arity`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Statistics on Indexes' show tables; Tables_in_test diff --git a/mysql-test/r/testdb_only.require b/mysql-test/r/testdb_only.require deleted file mode 100644 index e717418fdb6..00000000000 --- a/mysql-test/r/testdb_only.require +++ /dev/null @@ -1,2 +0,0 @@ -Variable_name Value -use extern server NO diff --git a/mysql-test/r/trigger.result b/mysql-test/r/trigger.result index 6047d00c858..3310209df58 100644 --- a/mysql-test/r/trigger.result +++ b/mysql-test/r/trigger.result @@ -969,7 +969,7 @@ trigger_schema trigger_name event_object_schema event_object_table action_statem test t1_bi test t1 set @a:=new.id test t1_ai test t1 set @b:=new.id rename table t1 to t2; -ERROR HY000: Can't create/write to file './test/t1_ai.TRN~' (Errcode: 13) +ERROR HY000: Can't create/write to file './test/t1_ai.TRN~' (Errcode: 13 "Permission denied") insert into t1 values (102); select @a, @b; @a @b diff --git a/mysql-test/r/type_timestamp.result b/mysql-test/r/type_timestamp.result index 52c7f05839e..e7add0d80a7 100644 --- a/mysql-test/r/type_timestamp.result +++ b/mysql-test/r/type_timestamp.result @@ -148,15 +148,15 @@ ix+0 20030101000000 drop table t1; create table t1 (t1 timestamp, t2 timestamp default now()); -ERROR HY000: Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause +drop table t1; create table t1 (t1 timestamp, t2 timestamp on update now()); -ERROR HY000: Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause +drop table t1; create table t1 (t1 timestamp, t2 timestamp default now() on update now()); -ERROR HY000: Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause +drop table t1; create table t1 (t1 timestamp default now(), t2 timestamp on update now()); -ERROR HY000: Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause +drop table t1; create table t1 (t1 timestamp on update now(), t2 timestamp default now() on update now()); -ERROR HY000: Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause +drop table t1; create table t1 (t1 timestamp default '2003-01-01 00:00:00', t2 datetime, t3 timestamp); SET TIMESTAMP=1000000000; insert into t1 values (); diff --git a/mysql-test/r/type_timestamp_hires.result b/mysql-test/r/type_timestamp_hires.result index 3f1e05f4870..cc2cb6a403d 100644 --- a/mysql-test/r/type_timestamp_hires.result +++ b/mysql-test/r/type_timestamp_hires.result @@ -63,15 +63,15 @@ a show create table t1; Table Create Table t1 CREATE TABLE `t1` ( - `a` timestamp(4) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP + `a` timestamp(4) NOT NULL DEFAULT CURRENT_TIMESTAMP(4) ON UPDATE CURRENT_TIMESTAMP(4) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 show columns from t1; Field Type Null Key Default Extra -a timestamp(4) NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP +a timestamp(4) NO CURRENT_TIMESTAMP(4) on update CURRENT_TIMESTAMP select table_name, column_name, 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 from information_schema.columns where table_name='t1'; table_name t1 column_name a -column_default CURRENT_TIMESTAMP +column_default CURRENT_TIMESTAMP(4) is_nullable NO data_type timestamp character_maximum_length NULL @@ -113,7 +113,7 @@ t2 CREATE TABLE `t2` ( show create table t3; Table Create Table t3 CREATE TABLE `t3` ( - `a` timestamp(4) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP + `a` timestamp(4) NOT NULL DEFAULT CURRENT_TIMESTAMP(4) ON UPDATE CURRENT_TIMESTAMP(4) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 drop table t2, t3; insert t1 values ('2010-12-13 14:15:16.222222'); @@ -278,3 +278,23 @@ select * from t1; a 2011-01-01 01:01:01.12345 drop table t1; +create table t1 (a timestamp(5) default current_timestamp); +drop table t1; +create table t1 (a timestamp(5) default current_timestamp()); +drop table t1; +create table t1 (a timestamp(5) default current_timestamp(2)); +ERROR 42000: Invalid default value for 'a' +create table t1 (a timestamp(5) default current_timestamp(5)); +drop table t1; +create table t1 (a timestamp(5) default current_timestamp(6)); +drop table t1; +create table t1 (a timestamp(5) on update current_timestamp); +drop table t1; +create table t1 (a timestamp(5) on update current_timestamp()); +drop table t1; +create table t1 (a timestamp(5) on update current_timestamp(3)); +ERROR HY000: Invalid ON UPDATE clause for 'a' column +create table t1 (a timestamp(5) on update current_timestamp(5)); +drop table t1; +create table t1 (a timestamp(5) on update current_timestamp(6)); +drop table t1; diff --git a/mysql-test/r/user_var.result b/mysql-test/r/user_var.result index 2a3cdc49802..78ed5bb65d5 100644 --- a/mysql-test/r/user_var.result +++ b/mysql-test/r/user_var.result @@ -526,4 +526,47 @@ f1 f2 1 4 DROP TRIGGER trg1; DROP TABLE t1; +# +# Bug #12408412: GROUP_CONCAT + ORDER BY + INPUT/OUTPUT +# SAME USER VARIABLE = CRASH +# +SET @bug12408412=1; +SELECT GROUP_CONCAT(@bug12408412 ORDER BY 1) INTO @bug12408412; +CREATE TABLE t1(a INT); +INSERT INTO t1 VALUES (0); +SELECT DISTINCT POW(COUNT(*), @a:=(SELECT 1 FROM t1 LEFT JOIN t1 AS t2 ON @a)) +AS b FROM t1 GROUP BY a; +b +1 +SELECT @a; +@a +1 +DROP TABLE t1; +CREATE TABLE t1(f1 INT, f2 INT); +INSERT INTO t1 VALUES (1,2),(2,3),(3,1); +CREATE TABLE t2(a INT); +INSERT INTO t2 VALUES (1); +SET @var=NULL; +SELECT @var:=(SELECT f2 FROM t2 WHERE @var) FROM t1 GROUP BY f1 ORDER BY f2 DESC +LIMIT 1; +@var:=(SELECT f2 FROM t2 WHERE @var) +NULL +SELECT @var; +@var +NULL +DROP TABLE t1, t2; +CREATE TABLE t1(a INT); +INSERT INTO t1 VALUES (0),(1),(3); +SELECT DISTINCT POW(COUNT(distinct a), @a:=(SELECT 1 FROM t1 LEFT JOIN t1 AS t2 ON @a limit 1)) AS b FROM t1 GROUP BY a; +b +1 +SELECT @a; +@a +1 +DROP TABLE t1; End of 5.5 tests +# +# Check that used memory extends if we set a variable +# +set @var= repeat('a',20000); +1 diff --git a/mysql-test/r/variables.result b/mysql-test/r/variables.result index e055a333faf..6ec9d77230b 100644 --- a/mysql-test/r/variables.result +++ b/mysql-test/r/variables.result @@ -211,10 +211,10 @@ VARIABLE_NAME VARIABLE_VALUE DEFAULT_STORAGE_ENGINE MEMORY show global variables like 'default_storage_engine'; Variable_name Value -default_storage_engine MRG_MYISAM +default_storage_engine MRG_MyISAM select * from information_schema.global_variables where variable_name like 'default_storage_engine'; VARIABLE_NAME VARIABLE_VALUE -DEFAULT_STORAGE_ENGINE MRG_MYISAM +DEFAULT_STORAGE_ENGINE MRG_MyISAM set GLOBAL myisam_max_sort_file_size=2000000; Warnings: Warning 1292 Truncated incorrect myisam_max_sort_file_size value: '2000000' diff --git a/mysql-test/r/windows.require b/mysql-test/r/windows.require deleted file mode 100644 index 09aae1ed1d0..00000000000 --- a/mysql-test/r/windows.require +++ /dev/null @@ -1,2 +0,0 @@ -TRUE -1 diff --git a/mysql-test/r/xa_binlog.result b/mysql-test/r/xa_binlog.result index 3ce64953902..395f0dc62a4 100644 --- a/mysql-test/r/xa_binlog.result +++ b/mysql-test/r/xa_binlog.result @@ -18,7 +18,7 @@ a 1 2 3 -SHOW BINLOG EVENTS LIMIT 1,9; +SHOW BINLOG EVENTS LIMIT 2,9; Log_name Pos Event_type Server_id End_log_pos Info master-bin.000001 # Query 1 # BEGIN master-bin.000001 # Query 1 # use `test`; INSERT INTO t1 VALUES (1) |