From c7c6057013918838a6c3828f6901d942ee435fbc Mon Sep 17 00:00:00 2001 From: CbcWestwolf <1004626265@qq.com> Date: Tue, 28 Nov 2023 19:19:18 +0800 Subject: [PATCH 1/2] This is an automated cherry-pick of #42789 Signed-off-by: ti-chi-bot --- planner/core/logical_plan_builder.go | 2 + .../r/planner/core/tests/prepare/issue.result | 584 ++++++++++++++++++ .../t/planner/core/tests/prepare/issue.test | 428 +++++++++++++ 3 files changed, 1014 insertions(+) create mode 100644 tests/integrationtest/r/planner/core/tests/prepare/issue.result create mode 100644 tests/integrationtest/t/planner/core/tests/prepare/issue.test diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 81e9252db685b..a7438bb8acd9c 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -287,6 +287,7 @@ func (b *PlanBuilder) buildAggregation(ctx context.Context, p LogicalPlan, aggFu if _, ok = b.correlatedAggMapper[aggFuncList[j]]; !ok { b.correlatedAggMapper[aggFuncList[j]] = &expression.CorrelatedColumn{ Column: *schema4Agg.Columns[aggIndexMap[j]], + Data: new(types.Datum), } } b.correlatedAggMapper[aggFunc] = b.correlatedAggMapper[aggFuncList[j]] @@ -308,6 +309,7 @@ func (b *PlanBuilder) buildAggregation(ctx context.Context, p LogicalPlan, aggFu if _, ok := correlatedAggMap[aggFunc]; ok { b.correlatedAggMapper[aggFunc] = &expression.CorrelatedColumn{ Column: column, + Data: new(types.Datum), } } } diff --git a/tests/integrationtest/r/planner/core/tests/prepare/issue.result b/tests/integrationtest/r/planner/core/tests/prepare/issue.result new file mode 100644 index 0000000000000..f1885588db9f4 --- /dev/null +++ b/tests/integrationtest/r/planner/core/tests/prepare/issue.result @@ -0,0 +1,584 @@ +set tidb_enable_prepared_plan_cache=1; +drop table if exists UK_MU15569; +CREATE TABLE `UK_MU15569` (`COL1` varbinary(20) DEFAULT NULL,`COL2` bit(16) DEFAULT NULL,`COL3` time DEFAULT NULL,`COL4` int(11) DEFAULT NULL,UNIQUE KEY `U_M_COL4` (`COL1`(10),`COL2`),UNIQUE KEY `U_M_COL5` (`COL3`,`COL2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into UK_MU15569 values(0x1C4FDBA09B42D999AC3019B6A9C0C787FBA08446, 0xCA74, '-836:46:08', 735655453); +prepare stmt from 'select * from UK_MU15569 where col2 >= ? and col1 is not null and col3 = ?;'; +set @a=-32373, @b='545:50:46.85487'; +execute stmt using @a,@b; +COL1 COL2 COL3 COL4 +set @a=-27225, @b='-836:46:08'; +execute stmt using @a,@b; +COL1 COL2 COL3 COL4 +O۠�Bٙ�0���LJ���F �t -836:46:08 735655453 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @a,@b; +COL1 COL2 COL3 COL4 +O۠�Bٙ�0���LJ���F �t -836:46:08 735655453 +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +select * from UK_MU15569 where col2 >= -27225 and col1 is not null and col3 = '-836:46:08'; +COL1 COL2 COL3 COL4 +O۠�Bٙ�0���LJ���F �t -836:46:08 735655453 +set tidb_enable_prepared_plan_cache=1; +drop table if exists IDT_MULTI15844STROBJSTROBJ; +CREATE TABLE `IDT_MULTI15844STROBJSTROBJ` (`COL1` enum('bb','aa') DEFAULT NULL,`COL2` smallint(41) DEFAULT NULL,`COL3` year(4) DEFAULT NULL,KEY `U_M_COL4` (`COL1`,`COL2`),KEY `U_M_COL5` (`COL3`,`COL2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into IDT_MULTI15844STROBJSTROBJ values('bb', -16994, 1987); +prepare stmt from 'SELECT/*+ HASH_JOIN(t1, t2) */ t2.* FROM IDT_MULTI15844STROBJSTROBJ t1 LEFT JOIN IDT_MULTI15844STROBJSTROBJ t2 ON t1.col1 = t2.col1 WHERE t1.col2 BETWEEN ? AND ? AND t1.col1 >= ?;'; +set @a=752400293960, @b=258241896853, @c='none'; +execute stmt using @a,@b,@c; +COL1 COL2 COL3 +set @a=-170756280585, @b=3756, @c='aa'; +execute stmt using @a,@b,@c; +COL1 COL2 COL3 +bb -16994 1987 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @a,@b,@c; +COL1 COL2 COL3 +bb -16994 1987 +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +set tidb_enable_prepared_plan_cache=1; +drop table if exists PK_MULTI_COL_360; +CREATE TABLE PK_MULTI_COL_360 ( +COL1 blob NOT NULL, +COL2 char(1) NOT NULL, +PRIMARY KEY (COL1(5),COL2) /*T![clustered_index] CLUSTERED */); +INSERT INTO PK_MULTI_COL_360 VALUES ('�', '龂'); +prepare stmt from 'SELECT/*+ INL_JOIN(t1, t2) */ * FROM PK_MULTI_COL_360 t1 JOIN PK_MULTI_COL_360 t2 ON t1.col1 = t2.col1 WHERE t2.col2 BETWEEN ? AND ? AND t1.col2 BETWEEN ? AND ?'; +set @a="捲", @b="颽", @c="睭", @d="詼"; +execute stmt using @a,@b,@c,@d; +COL1 COL2 COL1 COL2 +set @a="龂", @b="龂", @c="龂", @d="龂"; +execute stmt using @a,@b,@c,@d; +COL1 COL2 COL1 COL2 +� 龂 � 龂 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +drop table if exists t; +CREATE TABLE t ( +a int(11) DEFAULT NULL, +b int(11) GENERATED ALWAYS AS (a) STORED NOT NULL, +PRIMARY KEY (b)); +insert into t(a) values(102); +prepare stmt from "select * from t where b in (?, ?, ?)"; +set @a=102, @b=102, @c=102; +execute stmt using @a,@b,@c; +a b +102 102 +set @a=-97, @b=-97, @c=-97; +execute stmt using @a,@b,@c; +a b +drop table if exists IDT_MULTI15853STROBJSTROBJ; +CREATE TABLE IDT_MULTI15853STROBJSTROBJ ( +COL1 enum('aa','bb','cc') DEFAULT NULL, +COL2 mediumint(41) DEFAULT NULL, +KEY U_M_COL4 (COL1,COL2) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into IDT_MULTI15853STROBJSTROBJ values("aa", 1); +prepare stmt from 'SELECT * FROM IDT_MULTI15853STROBJSTROBJ WHERE col1 = ? AND col1 != ?'; +set @a="mm", @b="aa"; +execute stmt using @a,@b; +COL1 COL2 +set @a="aa", @b="aa"; +execute stmt using @a,@b; +COL1 COL2 +drop table if exists PK_GCOL_STORED9816; +CREATE TABLE `PK_GCOL_STORED9816` (`COL102` decimal(55,0) DEFAULT NULL); +insert into PK_GCOL_STORED9816 values(9710290195629059011); +prepare stmt from 'select count(*) from PK_GCOL_STORED9816 where col102 > ?'; +set @a=9860178624005968368; +execute stmt using @a; +count(*) +0 +set @a=-7235178122860450591; +execute stmt using @a; +count(*) +1 +set @a=9860178624005968368; +execute stmt using @a; +count(*) +0 +set @a=-7235178122860450591; +execute stmt using @a; +count(*) +1 +DROP TABLE IF EXISTS `t`; +CREATE TABLE `t` (`COL1` char(20) DEFAULT NULL, `COL2` bit(16),`COL3` date, KEY `U_M_COL5` (`COL3`,`COL2`)); +insert into t values ('','>d','9901-06-17'); +prepare stmt from 'select * from t where col1 is not null and col2 not in (?, ?, ?) and col3 in (?, ?, ?)'; +set @a=-21188, @b=26824, @c=31855, @d="5597-1-4", @e="5755-12-6", @f="1253-7-12"; +execute stmt using @a,@b,@c,@d,@e,@f; +COL1 COL2 COL3 +set @a=-5360, @b=-11715, @c=9399, @d="9213-09-13", @e="4705-12-24", @f="9901-06-17"; +execute stmt using @a,@b,@c,@d,@e,@f; +COL1 COL2 COL3 + >d 9901-06-17 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +set tidb_enable_prepared_plan_cache=0; +drop table if exists t; +create table t (a int primary key, b int); +prepare stmt from "select * from t where a=10"; +execute stmt; +a b +execute stmt; +a b +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +set tidb_enable_prepared_plan_cache=DEFAULT; +CREATE TABLE UK_MU16407 (COL3 timestamp NULL DEFAULT NULL, UNIQUE KEY U3(COL3)); +insert into UK_MU16407 values("1985-08-31 18:03:27"); +PREPARE st FROM 'SELECT COL3 FROM UK_MU16407 WHERE COL3>?'; +set @a='2039-1-19 3:14:40'; +execute st using @a; +COL3 +set @a='1950-1-19 3:14:40'; +execute st using @a; +COL3 +1985-08-31 18:03:27 +set tidb_enable_prepared_plan_cache=1; +drop table if exists UK_MULTI_COL_11691; +CREATE TABLE UK_MULTI_COL_11691 ( +COL1 binary(20) DEFAULT NULL, +COL2 tinyint(16) DEFAULT NULL, +COL3 time DEFAULT NULL, +UNIQUE KEY U_M_COL (COL1(10),COL2,COL3)); +insert into UK_MULTI_COL_11691 values(0x340C604874B52E8D30440E8DC2BB170621D8A088, 126, "-105:17:32"), +(0x28EC2EDBAC7DF99045BDD0FCEAADAFBAC2ACF76F, 126, "102:54:04"), +(0x11C38221B3B1E463C94EC39F0D481303A58A50DC, 118, "599:13:47"), +(0x03E2FC9E0C846FF1A926BF829FA9D7BAED3FD7B1, 118, "-257:45:13"); +prepare stmt from 'SELECT/*+ INL_JOIN(t1, t2) */ t2.COL2 FROM UK_MULTI_COL_11691 t1 JOIN UK_MULTI_COL_11691 t2 ON t1.col1 = t2.col1 WHERE t1.col2 BETWEEN ? AND ? AND t2.col2 BETWEEN ? AND ?'; +set @a=-29408, @b=-9254, @c=-1849, @d=-2346; +execute stmt using @a,@b,@c,@d; +COL2 +set @a=126, @b=126, @c=-125, @d=707; +execute stmt using @a,@b,@c,@d; +COL2 +126 +126 +set tidb_enable_prepared_plan_cache=DEFAULT; +set tidb_enable_prepared_plan_cache=1; +drop table if exists t1, t2; +CREATE TABLE t1 (c_int int, c_str varchar(40), PRIMARY KEY (c_int, c_str)); +CREATE TABLE t2 (c_str varchar(40), PRIMARY KEY (c_str)); +insert into t1 values (1, '1'); +insert into t2 values ('1'); +prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1 join t2 on t1.c_str <= t2.c_str where t1.c_int in (?,?)'; +set @a=10, @b=20; +execute stmt using @a, @b; +c_int c_str c_str +set @a=1, @b=2; +execute stmt using @a, @b; +c_int c_str c_str +1 1 1 +drop table t1, t2; +create table t1 (a int, b int, c int, index idxab(a, b, c)); +create table t2 (a int, b int); +prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1, t2 where t1.a=t2.a and t1.b=?'; +set @a=1; +execute stmt using @a; +a b c a b +execute stmt using @a; +a b c a b +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1, t2 where t1.a=t2.a and t1.c=?'; +set @a=1; +execute stmt using @a; +a b c a b +execute stmt using @a; +a b c a b +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +set tidb_enable_prepared_plan_cache=DEFAULT; +set tidb_enable_prepared_plan_cache=1; +drop table if exists PK_SIGNED_10094; +CREATE TABLE PK_SIGNED_10094 (COL1 decimal(55,0) NOT NULL, PRIMARY KEY (COL1)); +insert into PK_SIGNED_10094 values(-9999999999999999999999999999999999999999999999999999999); +prepare stmt from 'select * from PK_SIGNED_10094 where col1 != ? and col1 + 10 <=> ? + 10'; +set @a=7309027171262036496, @b=-9798213896406520625; +execute stmt using @a,@b; +COL1 +set @a=5408499810319315618, @b=-9999999999999999999999999999999999999999999999999999999; +execute stmt using @a,@b; +COL1 +-9999999999999999999999999999999999999999999999999999999 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +set @a=7309027171262036496, @b=-9798213896406520625; +execute stmt using @a,@b; +COL1 +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +set tidb_enable_prepared_plan_cache=DEFAULT; +set tidb_enable_prepared_plan_cache=1; +drop table if exists PK_S_MULTI_37; +CREATE TABLE PK_S_MULTI_37 (COL1 decimal(55,0) NOT NULL, COL2 decimal(55,0) NOT NULL,PRIMARY KEY (COL1, COL2) /*T![clustered_index] NONCLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into PK_S_MULTI_37 values(-9999999999999999999999999999999999999999999999, 1); +prepare stmt from 'SELECT SUM(COL1+?), col2 FROM PK_S_MULTI_37 GROUP BY col2'; +set @a=1; +execute stmt using @a; +SUM(COL1+?) col2 +-9999999999999999999999999999999999999999999998 1 +set tidb_enable_prepared_plan_cache=DEFAULT; +set tidb_enable_prepared_plan_cache=1; +set @@tidb_enable_collect_execution_info=0; +drop table if exists t; +CREATE TABLE t (id bigint(20) NOT NULL,audit_id bigint(20) NOT NULL,PRIMARY KEY (id) /*T![clustered_index] CLUSTERED */,KEY index_audit_id (audit_id)); +insert into t values(1,9941971237863475), (2,9941971237863476), (3, 0); +prepare stmt from 'select * from t where audit_id=?'; +set @a='9941971237863475', @b=9941971237863475, @c='xayh7vrWVNqZtzlJmdJQUwAHnkI8Ec', @d='0.0', @e='0.1', @f = '9941971237863476'; +execute stmt using @a; +id audit_id +1 9941971237863475 +execute stmt using @b; +id audit_id +1 9941971237863475 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @c; +id audit_id +3 0 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @d; +id audit_id +3 0 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @e; +id audit_id +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @d; +id audit_id +3 0 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @f; +id audit_id +2 9941971237863476 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +prepare stmt from 'select count(*) from t where audit_id in (?, ?, ?, ?, ?)'; +execute stmt using @a, @b, @c, @d, @e; +count(*) +2 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @f, @b, @c, @d, @e; +count(*) +3 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +set tidb_enable_prepared_plan_cache=DEFAULT; +set @@tidb_enable_collect_execution_info=DEFAULT; +set tidb_enable_prepared_plan_cache=1; +set tidb_enable_clustered_index=on; +drop table if exists PK_TCOLLATION10197; +CREATE TABLE `PK_TCOLLATION10197` (`COL1` char(1) NOT NULL, PRIMARY KEY (`COL1`(1)) /*T![clustered_index] CLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into PK_TCOLLATION10197 values('龺'); +set @a='畻', @b='龺'; +prepare stmt from 'select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > ?;'; +execute stmt using @a; +count(distinct col1) +1 +execute stmt using @b; +count(distinct col1) +0 +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +prepare stmt from 'select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > ?;'; +execute stmt using @b; +count(distinct col1) +0 +select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > '龺'; +count(distinct col1) +0 +set tidb_enable_prepared_plan_cache=DEFAULT; +set tidb_enable_clustered_index=DEFAULT; +set tidb_enable_prepared_plan_cache=1; +set tidb_enable_clustered_index=on; +drop table if exists t; +CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL PRIMARY KEY, col2 int) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into t values('a', 1), ('b', 2); +set @a='a', @b='b', @z='z'; +prepare stmt from 'select col1 from t where col1 = ? and col2 in (1, 2);'; +execute stmt using @a; +col1 +a +execute stmt using @b; +col1 +b +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +execute stmt using @z; +col1 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @z; +col1 +drop table if exists t; +CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL, col2 int, PRIMARY KEY(col1, col2)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into t values('a', 1), ('b', 2); +set @a='a', @b='b', @z='z'; +prepare stmt from 'select col1 from t where (col1, col2) in ((?, 1));'; +execute stmt using @a; +col1 +a +execute stmt using @b; +col1 +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +execute stmt using @z; +col1 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @z; +col1 +set tidb_enable_clustered_index=off; +drop table if exists t; +CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL PRIMARY KEY, col2 int) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into t values('a', 1), ('b', 2); +set @a='a', @b='b', @z='z'; +prepare stmt from 'select col1 from t where col1 = ? and col2 in (1, 2);'; +execute stmt using @a; +col1 +a +execute stmt using @b; +col1 +b +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +execute stmt using @z; +col1 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @z; +col1 +drop table if exists t; +CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL, col2 int, PRIMARY KEY(col1, col2)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into t values('a', 1), ('b', 2); +set @a='a', @b='b', @z='z'; +prepare stmt from 'select col1 from t where (col1, col2) in ((?, 1));'; +execute stmt using @a; +col1 +a +execute stmt using @b; +col1 +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +execute stmt using @z; +col1 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @z; +col1 +set tidb_enable_prepared_plan_cache=DEFAULT; +set tidb_enable_clustered_index=DEFAULT; +set tidb_enable_prepared_plan_cache=1; +drop table if exists t; +create table t(col1 enum('aa', 'bb'), col2 int, index(col1, col2)); +insert into t values('aa', 333); +prepare stmt from 'SELECT * FROM t t1 JOIN t t2 ON t1.col1 = t2.col1 WHERE t1.col1 <=> NULL'; +execute stmt; +col1 col2 col1 col2 +execute stmt; +col1 col2 col1 col2 +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +prepare stmt from 'SELECT * FROM t t1 JOIN t t2 ON t1.col1 = t2.col1 WHERE t1.col1 <=> NULL and t2.col2 > ?'; +set @a=0; +execute stmt using @a; +col1 col2 col1 col2 +execute stmt using @a; +col1 col2 col1 col2 +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +set tidb_enable_prepared_plan_cache=DEFAULT; +drop table if exists t4; +create table t4 (a date); +prepare st1 from "insert into t4(a) select dt from (select ? as dt from dual union all select sysdate() ) a"; +set @t='2022-01-01 00:00:00.000000'; +execute st1 using @t; +select count(*) from t4; +count(*) +2 +set tidb_enable_prepared_plan_cache=1; +drop table if exists t; +create table t(a int); +prepare stmt from 'select * from t'; +execute stmt; +a +select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; +EXEC_COUNT plan_cache_hits plan_in_cache +1 0 0 +execute stmt; +a +select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; +EXEC_COUNT plan_cache_hits plan_in_cache +2 1 1 +prepare stmt from 'select * from t'; +execute stmt; +a +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +select EXEC_COUNT, plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; +EXEC_COUNT plan_cache_hits plan_in_cache +3 2 1 +drop table if exists t; +create table t(a int primary key, b int, c int); +prepare stmt from 'select * from t where a = 2 or a = ?'; +set @p = 3; +execute stmt using @p; +a b c +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @p; +a b c +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +drop table if exists t; +CREATE TABLE `t` (`COL1` bigint(20) DEFAULT NULL COMMENT 'WITH DEFAULT', UNIQUE KEY `UK_COL1` (`COL1`)); +insert into t values(-3865356285544170443),(9223372036854775807); +prepare stmt from 'select/*+ hash_agg() */ max(col1) from t where col1 = ? and col1 > ?;'; +set @a=-3865356285544170443, @b=-4055949188488870713; +execute stmt using @a,@b; +max(col1) +-3865356285544170443 +drop table if exists t; +create table t (a int, b int, index ab(a, b)); +insert into t values (1, 1), (2, 2); +prepare s1 from 'select * from t use index(ab) where a>=? and b>=? and b<=?'; +set @a=1, @b=1, @c=1; +execute s1 using @a, @b, @c; +a b +1 1 +set @a=1, @b=1, @c=10; +execute s1 using @a, @b, @c; +a b +1 1 +2 2 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +drop table if exists UK_GCOL_VIRTUAL_18928; +CREATE TABLE UK_GCOL_VIRTUAL_18928 ( +COL102 bigint(20) DEFAULT NULL, +COL103 bigint(20) DEFAULT NULL, +COL1 bigint(20) GENERATED ALWAYS AS (COL102 & 10) VIRTUAL, +COL2 varchar(20) DEFAULT NULL, +COL4 datetime DEFAULT NULL, +COL3 bigint(20) DEFAULT NULL, +COL5 float DEFAULT NULL, +UNIQUE KEY UK_COL1 (COL1)); +insert into UK_GCOL_VIRTUAL_18928(col102,col2) values("-5175976006730879891", "屘厒镇览錻碛斵大擔觏譨頙硺箄魨搝珄鋧扭趖"); +prepare stmt from 'SELECT * FROM UK_GCOL_VIRTUAL_18928 WHERE col1 < ? AND col2 != ?'; +set @a=10, @b="aa"; +execute stmt using @a, @b; +COL102 COL103 COL1 COL2 COL4 COL3 COL5 +-5175976006730879891 NULL 8 屘厒镇览錻碛斵大擔觏譨頙硺箄魨搝珄鋧扭趖 NULL NULL NULL +drop table if exists UK_MU14722; +CREATE TABLE UK_MU14722 ( +COL1 tinytext DEFAULT NULL, +COL2 tinyint(16) DEFAULT NULL, +COL3 datetime DEFAULT NULL, +COL4 int(11) DEFAULT NULL, +UNIQUE KEY U_M_COL (COL1(10)), +UNIQUE KEY U_M_COL2 (COL2), +UNIQUE KEY U_M_COL3 (COL3)); +insert into UK_MU14722 values("輮睅麤敜溺她晁瀪襄頮鹛涓誗钷廔筪惌嶙鎢塴", -121, "3383-02-19 07:58:28" , -639457963), +("偧孇鱓鼂瘠钻篝醗時鷷聽箌磇砀玸眞扦鸇祈灇", 127, "7902-03-05 08:54:04", -1094128660), +("浀玡慃淛漉围甧鴎史嬙砊齄w章炢忲噑硓哈樘", -127, "5813-04-16 03:07:20", -333397107), +("鑝粼啎鸼贖桖弦簼赭蠅鏪鐥蕿捐榥疗耹岜鬓槊", -117, "7753-11-24 10:14:24", 654872077); +prepare stmt from 'SELECT * FROM UK_MU14722 WHERE col2 > ? OR col2 BETWEEN ? AND ? ORDER BY COL2 + ? LIMIT 3'; +set @a=30410, @b=3937, @c=22045, @d=-4374; +execute stmt using @a,@b,@c,@d; +COL1 COL2 COL3 COL4 +set @a=127, @b=127, @c=127, @d=127; +execute stmt using @a,@b,@c,@d; +COL1 COL2 COL3 COL4 +偧孇鱓鼂瘠钻篝醗時鷷聽箌磇砀玸眞扦鸇祈灇 127 7902-03-05 08:54:04 -1094128660 +drop table if exists PK_AUTO_RANDOM9111; +CREATE TABLE `PK_AUTO_RANDOM9111` ( `COL1` bigint(45) NOT NULL , `COL2` varchar(20) DEFAULT NULL, `COL4` datetime DEFAULT NULL, `COL3` bigint(20) DEFAULT NULL, `COL5` float DEFAULT NULL, PRIMARY KEY (`COL1`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into PK_AUTO_RANDOM9111(col1) values (-9223372036854775808), (9223372036854775807); +set @a=9223372036854775807, @b=1; +prepare stmt from 'select min(col1) from PK_AUTO_RANDOM9111 where col1 > ?;'; +execute stmt using @a; +min(col1) +NULL +execute stmt using @a; +min(col1) +NULL +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @b; +min(col1) +9223372036854775807 +select @@last_plan_from_cache; +@@last_plan_from_cache +0 +execute stmt using @a; +min(col1) +NULL +select @@last_plan_from_cache; +@@last_plan_from_cache +1 +set tidb_enable_prepared_plan_cache=DEFAULT; +use test; +drop table if exists t0; +CREATE TABLE t0 (c1 double, c2 double); +select +exists ( +select +subq_2.c0 as c8 +from +(select +ref_153.c1 as c0 +from +t0 as ref_153 +group by ref_153.c1 having 0 <> ( +select 1 +from +t0 as ref_173 +where count(ref_153.c2) = avg(ref_153.c2) +order by c1 desc limit 1)) as subq_2 +) as c10; +c10 +0 diff --git a/tests/integrationtest/t/planner/core/tests/prepare/issue.test b/tests/integrationtest/t/planner/core/tests/prepare/issue.test new file mode 100644 index 0000000000000..50a7448d34eb1 --- /dev/null +++ b/tests/integrationtest/t/planner/core/tests/prepare/issue.test @@ -0,0 +1,428 @@ +# TestIssue31280 +set tidb_enable_prepared_plan_cache=1; +drop table if exists UK_MU15569; +CREATE TABLE `UK_MU15569` (`COL1` varbinary(20) DEFAULT NULL,`COL2` bit(16) DEFAULT NULL,`COL3` time DEFAULT NULL,`COL4` int(11) DEFAULT NULL,UNIQUE KEY `U_M_COL4` (`COL1`(10),`COL2`),UNIQUE KEY `U_M_COL5` (`COL3`,`COL2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into UK_MU15569 values(0x1C4FDBA09B42D999AC3019B6A9C0C787FBA08446, 0xCA74, '-836:46:08', 735655453); +prepare stmt from 'select * from UK_MU15569 where col2 >= ? and col1 is not null and col3 = ?;'; +set @a=-32373, @b='545:50:46.85487'; +execute stmt using @a,@b; +set @a=-27225, @b='-836:46:08'; +execute stmt using @a,@b; +select @@last_plan_from_cache; +execute stmt using @a,@b; +select @@last_plan_from_cache; +select * from UK_MU15569 where col2 >= -27225 and col1 is not null and col3 = '-836:46:08'; + +# TestIssue31375 +set tidb_enable_prepared_plan_cache=1; +drop table if exists IDT_MULTI15844STROBJSTROBJ; +CREATE TABLE `IDT_MULTI15844STROBJSTROBJ` (`COL1` enum('bb','aa') DEFAULT NULL,`COL2` smallint(41) DEFAULT NULL,`COL3` year(4) DEFAULT NULL,KEY `U_M_COL4` (`COL1`,`COL2`),KEY `U_M_COL5` (`COL3`,`COL2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into IDT_MULTI15844STROBJSTROBJ values('bb', -16994, 1987); +prepare stmt from 'SELECT/*+ HASH_JOIN(t1, t2) */ t2.* FROM IDT_MULTI15844STROBJSTROBJ t1 LEFT JOIN IDT_MULTI15844STROBJSTROBJ t2 ON t1.col1 = t2.col1 WHERE t1.col2 BETWEEN ? AND ? AND t1.col1 >= ?;'; +set @a=752400293960, @b=258241896853, @c='none'; +execute stmt using @a,@b,@c; +set @a=-170756280585, @b=3756, @c='aa'; +execute stmt using @a,@b,@c; +select @@last_plan_from_cache; +execute stmt using @a,@b,@c; +select @@last_plan_from_cache; + +# TestIssueEnablePreparedPlanCache +set tidb_enable_prepared_plan_cache=1; +drop table if exists PK_MULTI_COL_360; +CREATE TABLE PK_MULTI_COL_360 ( + COL1 blob NOT NULL, + COL2 char(1) NOT NULL, + PRIMARY KEY (COL1(5),COL2) /*T![clustered_index] CLUSTERED */); +INSERT INTO PK_MULTI_COL_360 VALUES ('�', '龂'); +prepare stmt from 'SELECT/*+ INL_JOIN(t1, t2) */ * FROM PK_MULTI_COL_360 t1 JOIN PK_MULTI_COL_360 t2 ON t1.col1 = t2.col1 WHERE t2.col2 BETWEEN ? AND ? AND t1.col2 BETWEEN ? AND ?'; +set @a="捲", @b="颽", @c="睭", @d="詼"; +execute stmt using @a,@b,@c,@d; +set @a="龂", @b="龂", @c="龂", @d="龂"; +execute stmt using @a,@b,@c,@d; +select @@last_plan_from_cache; +drop table if exists t; +CREATE TABLE t ( + a int(11) DEFAULT NULL, + b int(11) GENERATED ALWAYS AS (a) STORED NOT NULL, + PRIMARY KEY (b)); +insert into t(a) values(102); +prepare stmt from "select * from t where b in (?, ?, ?)"; +set @a=102, @b=102, @c=102; +execute stmt using @a,@b,@c; +set @a=-97, @b=-97, @c=-97; +execute stmt using @a,@b,@c; +drop table if exists IDT_MULTI15853STROBJSTROBJ; + + CREATE TABLE IDT_MULTI15853STROBJSTROBJ ( + COL1 enum('aa','bb','cc') DEFAULT NULL, + COL2 mediumint(41) DEFAULT NULL, + KEY U_M_COL4 (COL1,COL2) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into IDT_MULTI15853STROBJSTROBJ values("aa", 1); +prepare stmt from 'SELECT * FROM IDT_MULTI15853STROBJSTROBJ WHERE col1 = ? AND col1 != ?'; +set @a="mm", @b="aa"; +execute stmt using @a,@b; +set @a="aa", @b="aa"; +execute stmt using @a,@b; +drop table if exists PK_GCOL_STORED9816; +CREATE TABLE `PK_GCOL_STORED9816` (`COL102` decimal(55,0) DEFAULT NULL); +insert into PK_GCOL_STORED9816 values(9710290195629059011); +prepare stmt from 'select count(*) from PK_GCOL_STORED9816 where col102 > ?'; +set @a=9860178624005968368; +execute stmt using @a; +set @a=-7235178122860450591; +execute stmt using @a; +set @a=9860178624005968368; +execute stmt using @a; +set @a=-7235178122860450591; +execute stmt using @a; +DROP TABLE IF EXISTS `t`; +CREATE TABLE `t` (`COL1` char(20) DEFAULT NULL, `COL2` bit(16),`COL3` date, KEY `U_M_COL5` (`COL3`,`COL2`)); +insert into t values ('','>d','9901-06-17'); +prepare stmt from 'select * from t where col1 is not null and col2 not in (?, ?, ?) and col3 in (?, ?, ?)'; +set @a=-21188, @b=26824, @c=31855, @d="5597-1-4", @e="5755-12-6", @f="1253-7-12"; +execute stmt using @a,@b,@c,@d,@e,@f; +set @a=-5360, @b=-11715, @c=9399, @d="9213-09-13", @e="4705-12-24", @f="9901-06-17"; +execute stmt using @a,@b,@c,@d,@e,@f; +select @@last_plan_from_cache; + +# TestIssue33628 +set tidb_enable_prepared_plan_cache=0; +drop table if exists t; +create table t (a int primary key, b int); +prepare stmt from "select * from t where a=10"; +execute stmt; +execute stmt; +select @@last_plan_from_cache; +set tidb_enable_prepared_plan_cache=DEFAULT; + +# TestIssue42439 +CREATE TABLE UK_MU16407 (COL3 timestamp NULL DEFAULT NULL, UNIQUE KEY U3(COL3)); +insert into UK_MU16407 values("1985-08-31 18:03:27"); +PREPARE st FROM 'SELECT COL3 FROM UK_MU16407 WHERE COL3>?'; +set @a='2039-1-19 3:14:40'; +execute st using @a; +set @a='1950-1-19 3:14:40'; +execute st using @a; + +# TestIssue29486 +set tidb_enable_prepared_plan_cache=1; +drop table if exists UK_MULTI_COL_11691; +CREATE TABLE UK_MULTI_COL_11691 ( + COL1 binary(20) DEFAULT NULL, + COL2 tinyint(16) DEFAULT NULL, + COL3 time DEFAULT NULL, + UNIQUE KEY U_M_COL (COL1(10),COL2,COL3)); +insert into UK_MULTI_COL_11691 values(0x340C604874B52E8D30440E8DC2BB170621D8A088, 126, "-105:17:32"), + (0x28EC2EDBAC7DF99045BDD0FCEAADAFBAC2ACF76F, 126, "102:54:04"), + (0x11C38221B3B1E463C94EC39F0D481303A58A50DC, 118, "599:13:47"), + (0x03E2FC9E0C846FF1A926BF829FA9D7BAED3FD7B1, 118, "-257:45:13"); +prepare stmt from 'SELECT/*+ INL_JOIN(t1, t2) */ t2.COL2 FROM UK_MULTI_COL_11691 t1 JOIN UK_MULTI_COL_11691 t2 ON t1.col1 = t2.col1 WHERE t1.col2 BETWEEN ? AND ? AND t2.col2 BETWEEN ? AND ?'; +set @a=-29408, @b=-9254, @c=-1849, @d=-2346; +execute stmt using @a,@b,@c,@d; +set @a=126, @b=126, @c=-125, @d=707; +execute stmt using @a,@b,@c,@d; +set tidb_enable_prepared_plan_cache=DEFAULT; + + +# TestIssue28867 +set tidb_enable_prepared_plan_cache=1; +drop table if exists t1, t2; +CREATE TABLE t1 (c_int int, c_str varchar(40), PRIMARY KEY (c_int, c_str)); +CREATE TABLE t2 (c_str varchar(40), PRIMARY KEY (c_str)); +insert into t1 values (1, '1'); +insert into t2 values ('1'); +prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1 join t2 on t1.c_str <= t2.c_str where t1.c_int in (?,?)'; +set @a=10, @b=20; +execute stmt using @a, @b; +set @a=1, @b=2; +execute stmt using @a, @b; +drop table t1, t2; +create table t1 (a int, b int, c int, index idxab(a, b, c)); +create table t2 (a int, b int); +prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1, t2 where t1.a=t2.a and t1.b=?'; +set @a=1; +execute stmt using @a; +execute stmt using @a; +select @@last_plan_from_cache; +prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1, t2 where t1.a=t2.a and t1.c=?'; +set @a=1; +execute stmt using @a; +execute stmt using @a; +select @@last_plan_from_cache; +set tidb_enable_prepared_plan_cache=DEFAULT; + + +# TestIssue29565 +set tidb_enable_prepared_plan_cache=1; +drop table if exists PK_SIGNED_10094; +CREATE TABLE PK_SIGNED_10094 (COL1 decimal(55,0) NOT NULL, PRIMARY KEY (COL1)); +insert into PK_SIGNED_10094 values(-9999999999999999999999999999999999999999999999999999999); +prepare stmt from 'select * from PK_SIGNED_10094 where col1 != ? and col1 + 10 <=> ? + 10'; +set @a=7309027171262036496, @b=-9798213896406520625; +execute stmt using @a,@b; +set @a=5408499810319315618, @b=-9999999999999999999999999999999999999999999999999999999; +execute stmt using @a,@b; +select @@last_plan_from_cache; +set @a=7309027171262036496, @b=-9798213896406520625; +execute stmt using @a,@b; +select @@last_plan_from_cache; +set tidb_enable_prepared_plan_cache=DEFAULT; + + +# TestIssue31730 +set tidb_enable_prepared_plan_cache=1; +drop table if exists PK_S_MULTI_37; +CREATE TABLE PK_S_MULTI_37 (COL1 decimal(55,0) NOT NULL, COL2 decimal(55,0) NOT NULL,PRIMARY KEY (COL1, COL2) /*T![clustered_index] NONCLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into PK_S_MULTI_37 values(-9999999999999999999999999999999999999999999999, 1); +prepare stmt from 'SELECT SUM(COL1+?), col2 FROM PK_S_MULTI_37 GROUP BY col2'; +set @a=1; +execute stmt using @a; +set tidb_enable_prepared_plan_cache=DEFAULT; + + +# TestIssue28828 +set tidb_enable_prepared_plan_cache=1; +set @@tidb_enable_collect_execution_info=0; +drop table if exists t; +CREATE TABLE t (id bigint(20) NOT NULL,audit_id bigint(20) NOT NULL,PRIMARY KEY (id) /*T![clustered_index] CLUSTERED */,KEY index_audit_id (audit_id)); +insert into t values(1,9941971237863475), (2,9941971237863476), (3, 0); +prepare stmt from 'select * from t where audit_id=?'; +set @a='9941971237863475', @b=9941971237863475, @c='xayh7vrWVNqZtzlJmdJQUwAHnkI8Ec', @d='0.0', @e='0.1', @f = '9941971237863476'; +execute stmt using @a; +execute stmt using @b; +select @@last_plan_from_cache; +execute stmt using @c; +select @@last_plan_from_cache; +execute stmt using @d; +select @@last_plan_from_cache; +execute stmt using @e; +select @@last_plan_from_cache; +execute stmt using @d; +select @@last_plan_from_cache; +execute stmt using @f; +select @@last_plan_from_cache; +prepare stmt from 'select count(*) from t where audit_id in (?, ?, ?, ?, ?)'; +execute stmt using @a, @b, @c, @d, @e; +select @@last_plan_from_cache; +execute stmt using @f, @b, @c, @d, @e; +select @@last_plan_from_cache; +set tidb_enable_prepared_plan_cache=DEFAULT; +set @@tidb_enable_collect_execution_info=DEFAULT; + +# TestIssue29805 +set tidb_enable_prepared_plan_cache=1; +set tidb_enable_clustered_index=on; +drop table if exists PK_TCOLLATION10197; +CREATE TABLE `PK_TCOLLATION10197` (`COL1` char(1) NOT NULL, PRIMARY KEY (`COL1`(1)) /*T![clustered_index] CLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into PK_TCOLLATION10197 values('龺'); +set @a='畻', @b='龺'; +prepare stmt from 'select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > ?;'; +execute stmt using @a; +execute stmt using @b; +select @@last_plan_from_cache; +prepare stmt from 'select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > ?;'; +execute stmt using @b; +select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > '龺'; +set tidb_enable_prepared_plan_cache=DEFAULT; +set tidb_enable_clustered_index=DEFAULT; + + +# TestIssue29993 +set tidb_enable_prepared_plan_cache=1; +set tidb_enable_clustered_index=on; +drop table if exists t; +CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL PRIMARY KEY, col2 int) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into t values('a', 1), ('b', 2); +set @a='a', @b='b', @z='z'; +prepare stmt from 'select col1 from t where col1 = ? and col2 in (1, 2);'; +execute stmt using @a; +execute stmt using @b; +select @@last_plan_from_cache; +execute stmt using @z; +select @@last_plan_from_cache; +execute stmt using @z; +drop table if exists t; +CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL, col2 int, PRIMARY KEY(col1, col2)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into t values('a', 1), ('b', 2); +set @a='a', @b='b', @z='z'; +prepare stmt from 'select col1 from t where (col1, col2) in ((?, 1));'; +execute stmt using @a; +execute stmt using @b; +select @@last_plan_from_cache; +execute stmt using @z; +select @@last_plan_from_cache; +execute stmt using @z; +set tidb_enable_clustered_index=off; +drop table if exists t; +CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL PRIMARY KEY, col2 int) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into t values('a', 1), ('b', 2); +set @a='a', @b='b', @z='z'; +prepare stmt from 'select col1 from t where col1 = ? and col2 in (1, 2);'; +execute stmt using @a; +execute stmt using @b; +select @@last_plan_from_cache; +execute stmt using @z; +select @@last_plan_from_cache; +execute stmt using @z; +drop table if exists t; +CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL, col2 int, PRIMARY KEY(col1, col2)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into t values('a', 1), ('b', 2); +set @a='a', @b='b', @z='z'; +prepare stmt from 'select col1 from t where (col1, col2) in ((?, 1));'; +execute stmt using @a; +execute stmt using @b; +select @@last_plan_from_cache; +execute stmt using @z; +select @@last_plan_from_cache; +execute stmt using @z; +set tidb_enable_prepared_plan_cache=DEFAULT; +set tidb_enable_clustered_index=DEFAULT; + + +# TestIssue30100 +set tidb_enable_prepared_plan_cache=1; +drop table if exists t; +create table t(col1 enum('aa', 'bb'), col2 int, index(col1, col2)); +insert into t values('aa', 333); +prepare stmt from 'SELECT * FROM t t1 JOIN t t2 ON t1.col1 = t2.col1 WHERE t1.col1 <=> NULL'; +execute stmt; +execute stmt; +select @@last_plan_from_cache; +prepare stmt from 'SELECT * FROM t t1 JOIN t t2 ON t1.col1 = t2.col1 WHERE t1.col1 <=> NULL and t2.col2 > ?'; +set @a=0; +execute stmt using @a; +execute stmt using @a; +select @@last_plan_from_cache; +set tidb_enable_prepared_plan_cache=DEFAULT; + + +# TestIssue37901 +drop table if exists t4; +create table t4 (a date); +prepare st1 from "insert into t4(a) select dt from (select ? as dt from dual union all select sysdate() ) a"; +set @t='2022-01-01 00:00:00.000000'; +execute st1 using @t; +select count(*) from t4; + + +# TestIssueEnablePreparedPlanCache2 +set tidb_enable_prepared_plan_cache=1; + +## Issue18066 +drop table if exists t; +create table t(a int); +prepare stmt from 'select * from t'; +execute stmt; +select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; +execute stmt; +select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; +prepare stmt from 'select * from t'; +execute stmt; +select @@last_plan_from_cache; +select EXEC_COUNT, plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; + +## TestIssue26873 +drop table if exists t; +create table t(a int primary key, b int, c int); +prepare stmt from 'select * from t where a = 2 or a = ?'; +set @p = 3; +execute stmt using @p; +select @@last_plan_from_cache; +execute stmt using @p; +select @@last_plan_from_cache; + +## TestIssue29511 +drop table if exists t; +CREATE TABLE `t` (`COL1` bigint(20) DEFAULT NULL COMMENT 'WITH DEFAULT', UNIQUE KEY `UK_COL1` (`COL1`)); +insert into t values(-3865356285544170443),(9223372036854775807); +prepare stmt from 'select/*+ hash_agg() */ max(col1) from t where col1 = ? and col1 > ?;'; +set @a=-3865356285544170443, @b=-4055949188488870713; +execute stmt using @a,@b; + +## TestIssue23671 +drop table if exists t; +create table t (a int, b int, index ab(a, b)); +insert into t values (1, 1), (2, 2); +prepare s1 from 'select * from t use index(ab) where a>=? and b>=? and b<=?'; +set @a=1, @b=1, @c=1; +execute s1 using @a, @b, @c; +set @a=1, @b=1, @c=10; +execute s1 using @a, @b, @c; +select @@last_plan_from_cache; + +## TestIssue28920 +drop table if exists UK_GCOL_VIRTUAL_18928; +CREATE TABLE UK_GCOL_VIRTUAL_18928 ( + COL102 bigint(20) DEFAULT NULL, + COL103 bigint(20) DEFAULT NULL, + COL1 bigint(20) GENERATED ALWAYS AS (COL102 & 10) VIRTUAL, + COL2 varchar(20) DEFAULT NULL, + COL4 datetime DEFAULT NULL, + COL3 bigint(20) DEFAULT NULL, + COL5 float DEFAULT NULL, + UNIQUE KEY UK_COL1 (COL1)); +insert into UK_GCOL_VIRTUAL_18928(col102,col2) values("-5175976006730879891", "屘厒镇览錻碛斵大擔觏譨頙硺箄魨搝珄鋧扭趖"); +prepare stmt from 'SELECT * FROM UK_GCOL_VIRTUAL_18928 WHERE col1 < ? AND col2 != ?'; +set @a=10, @b="aa"; +execute stmt using @a, @b; + +## Issue29296 +drop table if exists UK_MU14722; +CREATE TABLE UK_MU14722 ( + COL1 tinytext DEFAULT NULL, + COL2 tinyint(16) DEFAULT NULL, + COL3 datetime DEFAULT NULL, + COL4 int(11) DEFAULT NULL, + UNIQUE KEY U_M_COL (COL1(10)), + UNIQUE KEY U_M_COL2 (COL2), + UNIQUE KEY U_M_COL3 (COL3)); +insert into UK_MU14722 values("輮睅麤敜溺她晁瀪襄頮鹛涓誗钷廔筪惌嶙鎢塴", -121, "3383-02-19 07:58:28" , -639457963), + ("偧孇鱓鼂瘠钻篝醗時鷷聽箌磇砀玸眞扦鸇祈灇", 127, "7902-03-05 08:54:04", -1094128660), + ("浀玡慃淛漉围甧鴎史嬙砊齄w章炢忲噑硓哈樘", -127, "5813-04-16 03:07:20", -333397107), + ("鑝粼啎鸼贖桖弦簼赭蠅鏪鐥蕿捐榥疗耹岜鬓槊", -117, "7753-11-24 10:14:24", 654872077); +prepare stmt from 'SELECT * FROM UK_MU14722 WHERE col2 > ? OR col2 BETWEEN ? AND ? ORDER BY COL2 + ? LIMIT 3'; +set @a=30410, @b=3937, @c=22045, @d=-4374; +execute stmt using @a,@b,@c,@d; +set @a=127, @b=127, @c=127, @d=127; +execute stmt using @a,@b,@c,@d; + +## TestIssue28246 +drop table if exists PK_AUTO_RANDOM9111; +CREATE TABLE `PK_AUTO_RANDOM9111` ( `COL1` bigint(45) NOT NULL , `COL2` varchar(20) DEFAULT NULL, `COL4` datetime DEFAULT NULL, `COL3` bigint(20) DEFAULT NULL, `COL5` float DEFAULT NULL, PRIMARY KEY (`COL1`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +insert into PK_AUTO_RANDOM9111(col1) values (-9223372036854775808), (9223372036854775807); +set @a=9223372036854775807, @b=1; +prepare stmt from 'select min(col1) from PK_AUTO_RANDOM9111 where col1 > ?;'; +## The plan contains the tableDual, so it will not be cached. +execute stmt using @a; +execute stmt using @a; +select @@last_plan_from_cache; +execute stmt using @b; +select @@last_plan_from_cache; +execute stmt using @a; +select @@last_plan_from_cache; + +set tidb_enable_prepared_plan_cache=DEFAULT; + +# TestIssue42739 + +use test; +drop table if exists t0; +CREATE TABLE t0 (c1 double, c2 double); +select + exists ( + select + subq_2.c0 as c8 + from + (select + ref_153.c1 as c0 + from + t0 as ref_153 + group by ref_153.c1 having 0 <> ( + select 1 + from + t0 as ref_173 + where count(ref_153.c2) = avg(ref_153.c2) + order by c1 desc limit 1)) as subq_2 + ) as c10; From 26bbb921218f07b1500db8717e0f460d88913b23 Mon Sep 17 00:00:00 2001 From: cbcwestwolf <1004626265@qq.com> Date: Tue, 28 Nov 2023 20:17:50 +0800 Subject: [PATCH 2/2] fix --- planner/core/prepare_test.go | 25 + .../r/planner/core/tests/prepare/issue.result | 584 ------------------ .../t/planner/core/tests/prepare/issue.test | 428 ------------- 3 files changed, 25 insertions(+), 1012 deletions(-) delete mode 100644 tests/integrationtest/r/planner/core/tests/prepare/issue.result delete mode 100644 tests/integrationtest/t/planner/core/tests/prepare/issue.test diff --git a/planner/core/prepare_test.go b/planner/core/prepare_test.go index 1c38442382a59..f9b651060f5db 100644 --- a/planner/core/prepare_test.go +++ b/planner/core/prepare_test.go @@ -2807,3 +2807,28 @@ func TestIssue37901(t *testing.T) { tk.MustExec(`execute st1 using @t`) tk.MustQuery(`select count(*) from t4`).Check(testkit.Rows("2")) } + +func TestIssue42739(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + + tk.MustExec(`use test`) + tk.MustExec(`drop table if exists t0;`) + tk.MustExec(`CREATE TABLE t0 (c1 double, c2 double);`) + tk.MustExec(`select + exists ( + select + subq_2.c0 as c8 + from + (select + ref_153.c1 as c0 + from + t0 as ref_153 + group by ref_153.c1 having 0 <> ( + select 1 + from + t0 as ref_173 + where count(ref_153.c2) = avg(ref_153.c2) + order by c1 desc limit 1)) as subq_2 + ) as c10;`) +} diff --git a/tests/integrationtest/r/planner/core/tests/prepare/issue.result b/tests/integrationtest/r/planner/core/tests/prepare/issue.result deleted file mode 100644 index f1885588db9f4..0000000000000 --- a/tests/integrationtest/r/planner/core/tests/prepare/issue.result +++ /dev/null @@ -1,584 +0,0 @@ -set tidb_enable_prepared_plan_cache=1; -drop table if exists UK_MU15569; -CREATE TABLE `UK_MU15569` (`COL1` varbinary(20) DEFAULT NULL,`COL2` bit(16) DEFAULT NULL,`COL3` time DEFAULT NULL,`COL4` int(11) DEFAULT NULL,UNIQUE KEY `U_M_COL4` (`COL1`(10),`COL2`),UNIQUE KEY `U_M_COL5` (`COL3`,`COL2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into UK_MU15569 values(0x1C4FDBA09B42D999AC3019B6A9C0C787FBA08446, 0xCA74, '-836:46:08', 735655453); -prepare stmt from 'select * from UK_MU15569 where col2 >= ? and col1 is not null and col3 = ?;'; -set @a=-32373, @b='545:50:46.85487'; -execute stmt using @a,@b; -COL1 COL2 COL3 COL4 -set @a=-27225, @b='-836:46:08'; -execute stmt using @a,@b; -COL1 COL2 COL3 COL4 -O۠�Bٙ�0���LJ���F �t -836:46:08 735655453 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @a,@b; -COL1 COL2 COL3 COL4 -O۠�Bٙ�0���LJ���F �t -836:46:08 735655453 -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -select * from UK_MU15569 where col2 >= -27225 and col1 is not null and col3 = '-836:46:08'; -COL1 COL2 COL3 COL4 -O۠�Bٙ�0���LJ���F �t -836:46:08 735655453 -set tidb_enable_prepared_plan_cache=1; -drop table if exists IDT_MULTI15844STROBJSTROBJ; -CREATE TABLE `IDT_MULTI15844STROBJSTROBJ` (`COL1` enum('bb','aa') DEFAULT NULL,`COL2` smallint(41) DEFAULT NULL,`COL3` year(4) DEFAULT NULL,KEY `U_M_COL4` (`COL1`,`COL2`),KEY `U_M_COL5` (`COL3`,`COL2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into IDT_MULTI15844STROBJSTROBJ values('bb', -16994, 1987); -prepare stmt from 'SELECT/*+ HASH_JOIN(t1, t2) */ t2.* FROM IDT_MULTI15844STROBJSTROBJ t1 LEFT JOIN IDT_MULTI15844STROBJSTROBJ t2 ON t1.col1 = t2.col1 WHERE t1.col2 BETWEEN ? AND ? AND t1.col1 >= ?;'; -set @a=752400293960, @b=258241896853, @c='none'; -execute stmt using @a,@b,@c; -COL1 COL2 COL3 -set @a=-170756280585, @b=3756, @c='aa'; -execute stmt using @a,@b,@c; -COL1 COL2 COL3 -bb -16994 1987 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @a,@b,@c; -COL1 COL2 COL3 -bb -16994 1987 -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -set tidb_enable_prepared_plan_cache=1; -drop table if exists PK_MULTI_COL_360; -CREATE TABLE PK_MULTI_COL_360 ( -COL1 blob NOT NULL, -COL2 char(1) NOT NULL, -PRIMARY KEY (COL1(5),COL2) /*T![clustered_index] CLUSTERED */); -INSERT INTO PK_MULTI_COL_360 VALUES ('�', '龂'); -prepare stmt from 'SELECT/*+ INL_JOIN(t1, t2) */ * FROM PK_MULTI_COL_360 t1 JOIN PK_MULTI_COL_360 t2 ON t1.col1 = t2.col1 WHERE t2.col2 BETWEEN ? AND ? AND t1.col2 BETWEEN ? AND ?'; -set @a="捲", @b="颽", @c="睭", @d="詼"; -execute stmt using @a,@b,@c,@d; -COL1 COL2 COL1 COL2 -set @a="龂", @b="龂", @c="龂", @d="龂"; -execute stmt using @a,@b,@c,@d; -COL1 COL2 COL1 COL2 -� 龂 � 龂 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -drop table if exists t; -CREATE TABLE t ( -a int(11) DEFAULT NULL, -b int(11) GENERATED ALWAYS AS (a) STORED NOT NULL, -PRIMARY KEY (b)); -insert into t(a) values(102); -prepare stmt from "select * from t where b in (?, ?, ?)"; -set @a=102, @b=102, @c=102; -execute stmt using @a,@b,@c; -a b -102 102 -set @a=-97, @b=-97, @c=-97; -execute stmt using @a,@b,@c; -a b -drop table if exists IDT_MULTI15853STROBJSTROBJ; -CREATE TABLE IDT_MULTI15853STROBJSTROBJ ( -COL1 enum('aa','bb','cc') DEFAULT NULL, -COL2 mediumint(41) DEFAULT NULL, -KEY U_M_COL4 (COL1,COL2) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into IDT_MULTI15853STROBJSTROBJ values("aa", 1); -prepare stmt from 'SELECT * FROM IDT_MULTI15853STROBJSTROBJ WHERE col1 = ? AND col1 != ?'; -set @a="mm", @b="aa"; -execute stmt using @a,@b; -COL1 COL2 -set @a="aa", @b="aa"; -execute stmt using @a,@b; -COL1 COL2 -drop table if exists PK_GCOL_STORED9816; -CREATE TABLE `PK_GCOL_STORED9816` (`COL102` decimal(55,0) DEFAULT NULL); -insert into PK_GCOL_STORED9816 values(9710290195629059011); -prepare stmt from 'select count(*) from PK_GCOL_STORED9816 where col102 > ?'; -set @a=9860178624005968368; -execute stmt using @a; -count(*) -0 -set @a=-7235178122860450591; -execute stmt using @a; -count(*) -1 -set @a=9860178624005968368; -execute stmt using @a; -count(*) -0 -set @a=-7235178122860450591; -execute stmt using @a; -count(*) -1 -DROP TABLE IF EXISTS `t`; -CREATE TABLE `t` (`COL1` char(20) DEFAULT NULL, `COL2` bit(16),`COL3` date, KEY `U_M_COL5` (`COL3`,`COL2`)); -insert into t values ('','>d','9901-06-17'); -prepare stmt from 'select * from t where col1 is not null and col2 not in (?, ?, ?) and col3 in (?, ?, ?)'; -set @a=-21188, @b=26824, @c=31855, @d="5597-1-4", @e="5755-12-6", @f="1253-7-12"; -execute stmt using @a,@b,@c,@d,@e,@f; -COL1 COL2 COL3 -set @a=-5360, @b=-11715, @c=9399, @d="9213-09-13", @e="4705-12-24", @f="9901-06-17"; -execute stmt using @a,@b,@c,@d,@e,@f; -COL1 COL2 COL3 - >d 9901-06-17 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -set tidb_enable_prepared_plan_cache=0; -drop table if exists t; -create table t (a int primary key, b int); -prepare stmt from "select * from t where a=10"; -execute stmt; -a b -execute stmt; -a b -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -set tidb_enable_prepared_plan_cache=DEFAULT; -CREATE TABLE UK_MU16407 (COL3 timestamp NULL DEFAULT NULL, UNIQUE KEY U3(COL3)); -insert into UK_MU16407 values("1985-08-31 18:03:27"); -PREPARE st FROM 'SELECT COL3 FROM UK_MU16407 WHERE COL3>?'; -set @a='2039-1-19 3:14:40'; -execute st using @a; -COL3 -set @a='1950-1-19 3:14:40'; -execute st using @a; -COL3 -1985-08-31 18:03:27 -set tidb_enable_prepared_plan_cache=1; -drop table if exists UK_MULTI_COL_11691; -CREATE TABLE UK_MULTI_COL_11691 ( -COL1 binary(20) DEFAULT NULL, -COL2 tinyint(16) DEFAULT NULL, -COL3 time DEFAULT NULL, -UNIQUE KEY U_M_COL (COL1(10),COL2,COL3)); -insert into UK_MULTI_COL_11691 values(0x340C604874B52E8D30440E8DC2BB170621D8A088, 126, "-105:17:32"), -(0x28EC2EDBAC7DF99045BDD0FCEAADAFBAC2ACF76F, 126, "102:54:04"), -(0x11C38221B3B1E463C94EC39F0D481303A58A50DC, 118, "599:13:47"), -(0x03E2FC9E0C846FF1A926BF829FA9D7BAED3FD7B1, 118, "-257:45:13"); -prepare stmt from 'SELECT/*+ INL_JOIN(t1, t2) */ t2.COL2 FROM UK_MULTI_COL_11691 t1 JOIN UK_MULTI_COL_11691 t2 ON t1.col1 = t2.col1 WHERE t1.col2 BETWEEN ? AND ? AND t2.col2 BETWEEN ? AND ?'; -set @a=-29408, @b=-9254, @c=-1849, @d=-2346; -execute stmt using @a,@b,@c,@d; -COL2 -set @a=126, @b=126, @c=-125, @d=707; -execute stmt using @a,@b,@c,@d; -COL2 -126 -126 -set tidb_enable_prepared_plan_cache=DEFAULT; -set tidb_enable_prepared_plan_cache=1; -drop table if exists t1, t2; -CREATE TABLE t1 (c_int int, c_str varchar(40), PRIMARY KEY (c_int, c_str)); -CREATE TABLE t2 (c_str varchar(40), PRIMARY KEY (c_str)); -insert into t1 values (1, '1'); -insert into t2 values ('1'); -prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1 join t2 on t1.c_str <= t2.c_str where t1.c_int in (?,?)'; -set @a=10, @b=20; -execute stmt using @a, @b; -c_int c_str c_str -set @a=1, @b=2; -execute stmt using @a, @b; -c_int c_str c_str -1 1 1 -drop table t1, t2; -create table t1 (a int, b int, c int, index idxab(a, b, c)); -create table t2 (a int, b int); -prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1, t2 where t1.a=t2.a and t1.b=?'; -set @a=1; -execute stmt using @a; -a b c a b -execute stmt using @a; -a b c a b -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1, t2 where t1.a=t2.a and t1.c=?'; -set @a=1; -execute stmt using @a; -a b c a b -execute stmt using @a; -a b c a b -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -set tidb_enable_prepared_plan_cache=DEFAULT; -set tidb_enable_prepared_plan_cache=1; -drop table if exists PK_SIGNED_10094; -CREATE TABLE PK_SIGNED_10094 (COL1 decimal(55,0) NOT NULL, PRIMARY KEY (COL1)); -insert into PK_SIGNED_10094 values(-9999999999999999999999999999999999999999999999999999999); -prepare stmt from 'select * from PK_SIGNED_10094 where col1 != ? and col1 + 10 <=> ? + 10'; -set @a=7309027171262036496, @b=-9798213896406520625; -execute stmt using @a,@b; -COL1 -set @a=5408499810319315618, @b=-9999999999999999999999999999999999999999999999999999999; -execute stmt using @a,@b; -COL1 --9999999999999999999999999999999999999999999999999999999 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -set @a=7309027171262036496, @b=-9798213896406520625; -execute stmt using @a,@b; -COL1 -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -set tidb_enable_prepared_plan_cache=DEFAULT; -set tidb_enable_prepared_plan_cache=1; -drop table if exists PK_S_MULTI_37; -CREATE TABLE PK_S_MULTI_37 (COL1 decimal(55,0) NOT NULL, COL2 decimal(55,0) NOT NULL,PRIMARY KEY (COL1, COL2) /*T![clustered_index] NONCLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into PK_S_MULTI_37 values(-9999999999999999999999999999999999999999999999, 1); -prepare stmt from 'SELECT SUM(COL1+?), col2 FROM PK_S_MULTI_37 GROUP BY col2'; -set @a=1; -execute stmt using @a; -SUM(COL1+?) col2 --9999999999999999999999999999999999999999999998 1 -set tidb_enable_prepared_plan_cache=DEFAULT; -set tidb_enable_prepared_plan_cache=1; -set @@tidb_enable_collect_execution_info=0; -drop table if exists t; -CREATE TABLE t (id bigint(20) NOT NULL,audit_id bigint(20) NOT NULL,PRIMARY KEY (id) /*T![clustered_index] CLUSTERED */,KEY index_audit_id (audit_id)); -insert into t values(1,9941971237863475), (2,9941971237863476), (3, 0); -prepare stmt from 'select * from t where audit_id=?'; -set @a='9941971237863475', @b=9941971237863475, @c='xayh7vrWVNqZtzlJmdJQUwAHnkI8Ec', @d='0.0', @e='0.1', @f = '9941971237863476'; -execute stmt using @a; -id audit_id -1 9941971237863475 -execute stmt using @b; -id audit_id -1 9941971237863475 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @c; -id audit_id -3 0 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @d; -id audit_id -3 0 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @e; -id audit_id -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @d; -id audit_id -3 0 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @f; -id audit_id -2 9941971237863476 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -prepare stmt from 'select count(*) from t where audit_id in (?, ?, ?, ?, ?)'; -execute stmt using @a, @b, @c, @d, @e; -count(*) -2 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @f, @b, @c, @d, @e; -count(*) -3 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -set tidb_enable_prepared_plan_cache=DEFAULT; -set @@tidb_enable_collect_execution_info=DEFAULT; -set tidb_enable_prepared_plan_cache=1; -set tidb_enable_clustered_index=on; -drop table if exists PK_TCOLLATION10197; -CREATE TABLE `PK_TCOLLATION10197` (`COL1` char(1) NOT NULL, PRIMARY KEY (`COL1`(1)) /*T![clustered_index] CLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into PK_TCOLLATION10197 values('龺'); -set @a='畻', @b='龺'; -prepare stmt from 'select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > ?;'; -execute stmt using @a; -count(distinct col1) -1 -execute stmt using @b; -count(distinct col1) -0 -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -prepare stmt from 'select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > ?;'; -execute stmt using @b; -count(distinct col1) -0 -select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > '龺'; -count(distinct col1) -0 -set tidb_enable_prepared_plan_cache=DEFAULT; -set tidb_enable_clustered_index=DEFAULT; -set tidb_enable_prepared_plan_cache=1; -set tidb_enable_clustered_index=on; -drop table if exists t; -CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL PRIMARY KEY, col2 int) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into t values('a', 1), ('b', 2); -set @a='a', @b='b', @z='z'; -prepare stmt from 'select col1 from t where col1 = ? and col2 in (1, 2);'; -execute stmt using @a; -col1 -a -execute stmt using @b; -col1 -b -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -execute stmt using @z; -col1 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @z; -col1 -drop table if exists t; -CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL, col2 int, PRIMARY KEY(col1, col2)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into t values('a', 1), ('b', 2); -set @a='a', @b='b', @z='z'; -prepare stmt from 'select col1 from t where (col1, col2) in ((?, 1));'; -execute stmt using @a; -col1 -a -execute stmt using @b; -col1 -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -execute stmt using @z; -col1 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @z; -col1 -set tidb_enable_clustered_index=off; -drop table if exists t; -CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL PRIMARY KEY, col2 int) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into t values('a', 1), ('b', 2); -set @a='a', @b='b', @z='z'; -prepare stmt from 'select col1 from t where col1 = ? and col2 in (1, 2);'; -execute stmt using @a; -col1 -a -execute stmt using @b; -col1 -b -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -execute stmt using @z; -col1 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @z; -col1 -drop table if exists t; -CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL, col2 int, PRIMARY KEY(col1, col2)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into t values('a', 1), ('b', 2); -set @a='a', @b='b', @z='z'; -prepare stmt from 'select col1 from t where (col1, col2) in ((?, 1));'; -execute stmt using @a; -col1 -a -execute stmt using @b; -col1 -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -execute stmt using @z; -col1 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @z; -col1 -set tidb_enable_prepared_plan_cache=DEFAULT; -set tidb_enable_clustered_index=DEFAULT; -set tidb_enable_prepared_plan_cache=1; -drop table if exists t; -create table t(col1 enum('aa', 'bb'), col2 int, index(col1, col2)); -insert into t values('aa', 333); -prepare stmt from 'SELECT * FROM t t1 JOIN t t2 ON t1.col1 = t2.col1 WHERE t1.col1 <=> NULL'; -execute stmt; -col1 col2 col1 col2 -execute stmt; -col1 col2 col1 col2 -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -prepare stmt from 'SELECT * FROM t t1 JOIN t t2 ON t1.col1 = t2.col1 WHERE t1.col1 <=> NULL and t2.col2 > ?'; -set @a=0; -execute stmt using @a; -col1 col2 col1 col2 -execute stmt using @a; -col1 col2 col1 col2 -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -set tidb_enable_prepared_plan_cache=DEFAULT; -drop table if exists t4; -create table t4 (a date); -prepare st1 from "insert into t4(a) select dt from (select ? as dt from dual union all select sysdate() ) a"; -set @t='2022-01-01 00:00:00.000000'; -execute st1 using @t; -select count(*) from t4; -count(*) -2 -set tidb_enable_prepared_plan_cache=1; -drop table if exists t; -create table t(a int); -prepare stmt from 'select * from t'; -execute stmt; -a -select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; -EXEC_COUNT plan_cache_hits plan_in_cache -1 0 0 -execute stmt; -a -select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; -EXEC_COUNT plan_cache_hits plan_in_cache -2 1 1 -prepare stmt from 'select * from t'; -execute stmt; -a -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -select EXEC_COUNT, plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; -EXEC_COUNT plan_cache_hits plan_in_cache -3 2 1 -drop table if exists t; -create table t(a int primary key, b int, c int); -prepare stmt from 'select * from t where a = 2 or a = ?'; -set @p = 3; -execute stmt using @p; -a b c -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @p; -a b c -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -drop table if exists t; -CREATE TABLE `t` (`COL1` bigint(20) DEFAULT NULL COMMENT 'WITH DEFAULT', UNIQUE KEY `UK_COL1` (`COL1`)); -insert into t values(-3865356285544170443),(9223372036854775807); -prepare stmt from 'select/*+ hash_agg() */ max(col1) from t where col1 = ? and col1 > ?;'; -set @a=-3865356285544170443, @b=-4055949188488870713; -execute stmt using @a,@b; -max(col1) --3865356285544170443 -drop table if exists t; -create table t (a int, b int, index ab(a, b)); -insert into t values (1, 1), (2, 2); -prepare s1 from 'select * from t use index(ab) where a>=? and b>=? and b<=?'; -set @a=1, @b=1, @c=1; -execute s1 using @a, @b, @c; -a b -1 1 -set @a=1, @b=1, @c=10; -execute s1 using @a, @b, @c; -a b -1 1 -2 2 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -drop table if exists UK_GCOL_VIRTUAL_18928; -CREATE TABLE UK_GCOL_VIRTUAL_18928 ( -COL102 bigint(20) DEFAULT NULL, -COL103 bigint(20) DEFAULT NULL, -COL1 bigint(20) GENERATED ALWAYS AS (COL102 & 10) VIRTUAL, -COL2 varchar(20) DEFAULT NULL, -COL4 datetime DEFAULT NULL, -COL3 bigint(20) DEFAULT NULL, -COL5 float DEFAULT NULL, -UNIQUE KEY UK_COL1 (COL1)); -insert into UK_GCOL_VIRTUAL_18928(col102,col2) values("-5175976006730879891", "屘厒镇览錻碛斵大擔觏譨頙硺箄魨搝珄鋧扭趖"); -prepare stmt from 'SELECT * FROM UK_GCOL_VIRTUAL_18928 WHERE col1 < ? AND col2 != ?'; -set @a=10, @b="aa"; -execute stmt using @a, @b; -COL102 COL103 COL1 COL2 COL4 COL3 COL5 --5175976006730879891 NULL 8 屘厒镇览錻碛斵大擔觏譨頙硺箄魨搝珄鋧扭趖 NULL NULL NULL -drop table if exists UK_MU14722; -CREATE TABLE UK_MU14722 ( -COL1 tinytext DEFAULT NULL, -COL2 tinyint(16) DEFAULT NULL, -COL3 datetime DEFAULT NULL, -COL4 int(11) DEFAULT NULL, -UNIQUE KEY U_M_COL (COL1(10)), -UNIQUE KEY U_M_COL2 (COL2), -UNIQUE KEY U_M_COL3 (COL3)); -insert into UK_MU14722 values("輮睅麤敜溺她晁瀪襄頮鹛涓誗钷廔筪惌嶙鎢塴", -121, "3383-02-19 07:58:28" , -639457963), -("偧孇鱓鼂瘠钻篝醗時鷷聽箌磇砀玸眞扦鸇祈灇", 127, "7902-03-05 08:54:04", -1094128660), -("浀玡慃淛漉围甧鴎史嬙砊齄w章炢忲噑硓哈樘", -127, "5813-04-16 03:07:20", -333397107), -("鑝粼啎鸼贖桖弦簼赭蠅鏪鐥蕿捐榥疗耹岜鬓槊", -117, "7753-11-24 10:14:24", 654872077); -prepare stmt from 'SELECT * FROM UK_MU14722 WHERE col2 > ? OR col2 BETWEEN ? AND ? ORDER BY COL2 + ? LIMIT 3'; -set @a=30410, @b=3937, @c=22045, @d=-4374; -execute stmt using @a,@b,@c,@d; -COL1 COL2 COL3 COL4 -set @a=127, @b=127, @c=127, @d=127; -execute stmt using @a,@b,@c,@d; -COL1 COL2 COL3 COL4 -偧孇鱓鼂瘠钻篝醗時鷷聽箌磇砀玸眞扦鸇祈灇 127 7902-03-05 08:54:04 -1094128660 -drop table if exists PK_AUTO_RANDOM9111; -CREATE TABLE `PK_AUTO_RANDOM9111` ( `COL1` bigint(45) NOT NULL , `COL2` varchar(20) DEFAULT NULL, `COL4` datetime DEFAULT NULL, `COL3` bigint(20) DEFAULT NULL, `COL5` float DEFAULT NULL, PRIMARY KEY (`COL1`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into PK_AUTO_RANDOM9111(col1) values (-9223372036854775808), (9223372036854775807); -set @a=9223372036854775807, @b=1; -prepare stmt from 'select min(col1) from PK_AUTO_RANDOM9111 where col1 > ?;'; -execute stmt using @a; -min(col1) -NULL -execute stmt using @a; -min(col1) -NULL -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @b; -min(col1) -9223372036854775807 -select @@last_plan_from_cache; -@@last_plan_from_cache -0 -execute stmt using @a; -min(col1) -NULL -select @@last_plan_from_cache; -@@last_plan_from_cache -1 -set tidb_enable_prepared_plan_cache=DEFAULT; -use test; -drop table if exists t0; -CREATE TABLE t0 (c1 double, c2 double); -select -exists ( -select -subq_2.c0 as c8 -from -(select -ref_153.c1 as c0 -from -t0 as ref_153 -group by ref_153.c1 having 0 <> ( -select 1 -from -t0 as ref_173 -where count(ref_153.c2) = avg(ref_153.c2) -order by c1 desc limit 1)) as subq_2 -) as c10; -c10 -0 diff --git a/tests/integrationtest/t/planner/core/tests/prepare/issue.test b/tests/integrationtest/t/planner/core/tests/prepare/issue.test deleted file mode 100644 index 50a7448d34eb1..0000000000000 --- a/tests/integrationtest/t/planner/core/tests/prepare/issue.test +++ /dev/null @@ -1,428 +0,0 @@ -# TestIssue31280 -set tidb_enable_prepared_plan_cache=1; -drop table if exists UK_MU15569; -CREATE TABLE `UK_MU15569` (`COL1` varbinary(20) DEFAULT NULL,`COL2` bit(16) DEFAULT NULL,`COL3` time DEFAULT NULL,`COL4` int(11) DEFAULT NULL,UNIQUE KEY `U_M_COL4` (`COL1`(10),`COL2`),UNIQUE KEY `U_M_COL5` (`COL3`,`COL2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into UK_MU15569 values(0x1C4FDBA09B42D999AC3019B6A9C0C787FBA08446, 0xCA74, '-836:46:08', 735655453); -prepare stmt from 'select * from UK_MU15569 where col2 >= ? and col1 is not null and col3 = ?;'; -set @a=-32373, @b='545:50:46.85487'; -execute stmt using @a,@b; -set @a=-27225, @b='-836:46:08'; -execute stmt using @a,@b; -select @@last_plan_from_cache; -execute stmt using @a,@b; -select @@last_plan_from_cache; -select * from UK_MU15569 where col2 >= -27225 and col1 is not null and col3 = '-836:46:08'; - -# TestIssue31375 -set tidb_enable_prepared_plan_cache=1; -drop table if exists IDT_MULTI15844STROBJSTROBJ; -CREATE TABLE `IDT_MULTI15844STROBJSTROBJ` (`COL1` enum('bb','aa') DEFAULT NULL,`COL2` smallint(41) DEFAULT NULL,`COL3` year(4) DEFAULT NULL,KEY `U_M_COL4` (`COL1`,`COL2`),KEY `U_M_COL5` (`COL3`,`COL2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into IDT_MULTI15844STROBJSTROBJ values('bb', -16994, 1987); -prepare stmt from 'SELECT/*+ HASH_JOIN(t1, t2) */ t2.* FROM IDT_MULTI15844STROBJSTROBJ t1 LEFT JOIN IDT_MULTI15844STROBJSTROBJ t2 ON t1.col1 = t2.col1 WHERE t1.col2 BETWEEN ? AND ? AND t1.col1 >= ?;'; -set @a=752400293960, @b=258241896853, @c='none'; -execute stmt using @a,@b,@c; -set @a=-170756280585, @b=3756, @c='aa'; -execute stmt using @a,@b,@c; -select @@last_plan_from_cache; -execute stmt using @a,@b,@c; -select @@last_plan_from_cache; - -# TestIssueEnablePreparedPlanCache -set tidb_enable_prepared_plan_cache=1; -drop table if exists PK_MULTI_COL_360; -CREATE TABLE PK_MULTI_COL_360 ( - COL1 blob NOT NULL, - COL2 char(1) NOT NULL, - PRIMARY KEY (COL1(5),COL2) /*T![clustered_index] CLUSTERED */); -INSERT INTO PK_MULTI_COL_360 VALUES ('�', '龂'); -prepare stmt from 'SELECT/*+ INL_JOIN(t1, t2) */ * FROM PK_MULTI_COL_360 t1 JOIN PK_MULTI_COL_360 t2 ON t1.col1 = t2.col1 WHERE t2.col2 BETWEEN ? AND ? AND t1.col2 BETWEEN ? AND ?'; -set @a="捲", @b="颽", @c="睭", @d="詼"; -execute stmt using @a,@b,@c,@d; -set @a="龂", @b="龂", @c="龂", @d="龂"; -execute stmt using @a,@b,@c,@d; -select @@last_plan_from_cache; -drop table if exists t; -CREATE TABLE t ( - a int(11) DEFAULT NULL, - b int(11) GENERATED ALWAYS AS (a) STORED NOT NULL, - PRIMARY KEY (b)); -insert into t(a) values(102); -prepare stmt from "select * from t where b in (?, ?, ?)"; -set @a=102, @b=102, @c=102; -execute stmt using @a,@b,@c; -set @a=-97, @b=-97, @c=-97; -execute stmt using @a,@b,@c; -drop table if exists IDT_MULTI15853STROBJSTROBJ; - - CREATE TABLE IDT_MULTI15853STROBJSTROBJ ( - COL1 enum('aa','bb','cc') DEFAULT NULL, - COL2 mediumint(41) DEFAULT NULL, - KEY U_M_COL4 (COL1,COL2) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into IDT_MULTI15853STROBJSTROBJ values("aa", 1); -prepare stmt from 'SELECT * FROM IDT_MULTI15853STROBJSTROBJ WHERE col1 = ? AND col1 != ?'; -set @a="mm", @b="aa"; -execute stmt using @a,@b; -set @a="aa", @b="aa"; -execute stmt using @a,@b; -drop table if exists PK_GCOL_STORED9816; -CREATE TABLE `PK_GCOL_STORED9816` (`COL102` decimal(55,0) DEFAULT NULL); -insert into PK_GCOL_STORED9816 values(9710290195629059011); -prepare stmt from 'select count(*) from PK_GCOL_STORED9816 where col102 > ?'; -set @a=9860178624005968368; -execute stmt using @a; -set @a=-7235178122860450591; -execute stmt using @a; -set @a=9860178624005968368; -execute stmt using @a; -set @a=-7235178122860450591; -execute stmt using @a; -DROP TABLE IF EXISTS `t`; -CREATE TABLE `t` (`COL1` char(20) DEFAULT NULL, `COL2` bit(16),`COL3` date, KEY `U_M_COL5` (`COL3`,`COL2`)); -insert into t values ('','>d','9901-06-17'); -prepare stmt from 'select * from t where col1 is not null and col2 not in (?, ?, ?) and col3 in (?, ?, ?)'; -set @a=-21188, @b=26824, @c=31855, @d="5597-1-4", @e="5755-12-6", @f="1253-7-12"; -execute stmt using @a,@b,@c,@d,@e,@f; -set @a=-5360, @b=-11715, @c=9399, @d="9213-09-13", @e="4705-12-24", @f="9901-06-17"; -execute stmt using @a,@b,@c,@d,@e,@f; -select @@last_plan_from_cache; - -# TestIssue33628 -set tidb_enable_prepared_plan_cache=0; -drop table if exists t; -create table t (a int primary key, b int); -prepare stmt from "select * from t where a=10"; -execute stmt; -execute stmt; -select @@last_plan_from_cache; -set tidb_enable_prepared_plan_cache=DEFAULT; - -# TestIssue42439 -CREATE TABLE UK_MU16407 (COL3 timestamp NULL DEFAULT NULL, UNIQUE KEY U3(COL3)); -insert into UK_MU16407 values("1985-08-31 18:03:27"); -PREPARE st FROM 'SELECT COL3 FROM UK_MU16407 WHERE COL3>?'; -set @a='2039-1-19 3:14:40'; -execute st using @a; -set @a='1950-1-19 3:14:40'; -execute st using @a; - -# TestIssue29486 -set tidb_enable_prepared_plan_cache=1; -drop table if exists UK_MULTI_COL_11691; -CREATE TABLE UK_MULTI_COL_11691 ( - COL1 binary(20) DEFAULT NULL, - COL2 tinyint(16) DEFAULT NULL, - COL3 time DEFAULT NULL, - UNIQUE KEY U_M_COL (COL1(10),COL2,COL3)); -insert into UK_MULTI_COL_11691 values(0x340C604874B52E8D30440E8DC2BB170621D8A088, 126, "-105:17:32"), - (0x28EC2EDBAC7DF99045BDD0FCEAADAFBAC2ACF76F, 126, "102:54:04"), - (0x11C38221B3B1E463C94EC39F0D481303A58A50DC, 118, "599:13:47"), - (0x03E2FC9E0C846FF1A926BF829FA9D7BAED3FD7B1, 118, "-257:45:13"); -prepare stmt from 'SELECT/*+ INL_JOIN(t1, t2) */ t2.COL2 FROM UK_MULTI_COL_11691 t1 JOIN UK_MULTI_COL_11691 t2 ON t1.col1 = t2.col1 WHERE t1.col2 BETWEEN ? AND ? AND t2.col2 BETWEEN ? AND ?'; -set @a=-29408, @b=-9254, @c=-1849, @d=-2346; -execute stmt using @a,@b,@c,@d; -set @a=126, @b=126, @c=-125, @d=707; -execute stmt using @a,@b,@c,@d; -set tidb_enable_prepared_plan_cache=DEFAULT; - - -# TestIssue28867 -set tidb_enable_prepared_plan_cache=1; -drop table if exists t1, t2; -CREATE TABLE t1 (c_int int, c_str varchar(40), PRIMARY KEY (c_int, c_str)); -CREATE TABLE t2 (c_str varchar(40), PRIMARY KEY (c_str)); -insert into t1 values (1, '1'); -insert into t2 values ('1'); -prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1 join t2 on t1.c_str <= t2.c_str where t1.c_int in (?,?)'; -set @a=10, @b=20; -execute stmt using @a, @b; -set @a=1, @b=2; -execute stmt using @a, @b; -drop table t1, t2; -create table t1 (a int, b int, c int, index idxab(a, b, c)); -create table t2 (a int, b int); -prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1, t2 where t1.a=t2.a and t1.b=?'; -set @a=1; -execute stmt using @a; -execute stmt using @a; -select @@last_plan_from_cache; -prepare stmt from 'select /*+ INL_JOIN(t1,t2) */ * from t1, t2 where t1.a=t2.a and t1.c=?'; -set @a=1; -execute stmt using @a; -execute stmt using @a; -select @@last_plan_from_cache; -set tidb_enable_prepared_plan_cache=DEFAULT; - - -# TestIssue29565 -set tidb_enable_prepared_plan_cache=1; -drop table if exists PK_SIGNED_10094; -CREATE TABLE PK_SIGNED_10094 (COL1 decimal(55,0) NOT NULL, PRIMARY KEY (COL1)); -insert into PK_SIGNED_10094 values(-9999999999999999999999999999999999999999999999999999999); -prepare stmt from 'select * from PK_SIGNED_10094 where col1 != ? and col1 + 10 <=> ? + 10'; -set @a=7309027171262036496, @b=-9798213896406520625; -execute stmt using @a,@b; -set @a=5408499810319315618, @b=-9999999999999999999999999999999999999999999999999999999; -execute stmt using @a,@b; -select @@last_plan_from_cache; -set @a=7309027171262036496, @b=-9798213896406520625; -execute stmt using @a,@b; -select @@last_plan_from_cache; -set tidb_enable_prepared_plan_cache=DEFAULT; - - -# TestIssue31730 -set tidb_enable_prepared_plan_cache=1; -drop table if exists PK_S_MULTI_37; -CREATE TABLE PK_S_MULTI_37 (COL1 decimal(55,0) NOT NULL, COL2 decimal(55,0) NOT NULL,PRIMARY KEY (COL1, COL2) /*T![clustered_index] NONCLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into PK_S_MULTI_37 values(-9999999999999999999999999999999999999999999999, 1); -prepare stmt from 'SELECT SUM(COL1+?), col2 FROM PK_S_MULTI_37 GROUP BY col2'; -set @a=1; -execute stmt using @a; -set tidb_enable_prepared_plan_cache=DEFAULT; - - -# TestIssue28828 -set tidb_enable_prepared_plan_cache=1; -set @@tidb_enable_collect_execution_info=0; -drop table if exists t; -CREATE TABLE t (id bigint(20) NOT NULL,audit_id bigint(20) NOT NULL,PRIMARY KEY (id) /*T![clustered_index] CLUSTERED */,KEY index_audit_id (audit_id)); -insert into t values(1,9941971237863475), (2,9941971237863476), (3, 0); -prepare stmt from 'select * from t where audit_id=?'; -set @a='9941971237863475', @b=9941971237863475, @c='xayh7vrWVNqZtzlJmdJQUwAHnkI8Ec', @d='0.0', @e='0.1', @f = '9941971237863476'; -execute stmt using @a; -execute stmt using @b; -select @@last_plan_from_cache; -execute stmt using @c; -select @@last_plan_from_cache; -execute stmt using @d; -select @@last_plan_from_cache; -execute stmt using @e; -select @@last_plan_from_cache; -execute stmt using @d; -select @@last_plan_from_cache; -execute stmt using @f; -select @@last_plan_from_cache; -prepare stmt from 'select count(*) from t where audit_id in (?, ?, ?, ?, ?)'; -execute stmt using @a, @b, @c, @d, @e; -select @@last_plan_from_cache; -execute stmt using @f, @b, @c, @d, @e; -select @@last_plan_from_cache; -set tidb_enable_prepared_plan_cache=DEFAULT; -set @@tidb_enable_collect_execution_info=DEFAULT; - -# TestIssue29805 -set tidb_enable_prepared_plan_cache=1; -set tidb_enable_clustered_index=on; -drop table if exists PK_TCOLLATION10197; -CREATE TABLE `PK_TCOLLATION10197` (`COL1` char(1) NOT NULL, PRIMARY KEY (`COL1`(1)) /*T![clustered_index] CLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into PK_TCOLLATION10197 values('龺'); -set @a='畻', @b='龺'; -prepare stmt from 'select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > ?;'; -execute stmt using @a; -execute stmt using @b; -select @@last_plan_from_cache; -prepare stmt from 'select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > ?;'; -execute stmt using @b; -select/*+ hash_agg() */ count(distinct col1) from PK_TCOLLATION10197 where col1 > '龺'; -set tidb_enable_prepared_plan_cache=DEFAULT; -set tidb_enable_clustered_index=DEFAULT; - - -# TestIssue29993 -set tidb_enable_prepared_plan_cache=1; -set tidb_enable_clustered_index=on; -drop table if exists t; -CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL PRIMARY KEY, col2 int) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into t values('a', 1), ('b', 2); -set @a='a', @b='b', @z='z'; -prepare stmt from 'select col1 from t where col1 = ? and col2 in (1, 2);'; -execute stmt using @a; -execute stmt using @b; -select @@last_plan_from_cache; -execute stmt using @z; -select @@last_plan_from_cache; -execute stmt using @z; -drop table if exists t; -CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL, col2 int, PRIMARY KEY(col1, col2)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into t values('a', 1), ('b', 2); -set @a='a', @b='b', @z='z'; -prepare stmt from 'select col1 from t where (col1, col2) in ((?, 1));'; -execute stmt using @a; -execute stmt using @b; -select @@last_plan_from_cache; -execute stmt using @z; -select @@last_plan_from_cache; -execute stmt using @z; -set tidb_enable_clustered_index=off; -drop table if exists t; -CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL PRIMARY KEY, col2 int) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into t values('a', 1), ('b', 2); -set @a='a', @b='b', @z='z'; -prepare stmt from 'select col1 from t where col1 = ? and col2 in (1, 2);'; -execute stmt using @a; -execute stmt using @b; -select @@last_plan_from_cache; -execute stmt using @z; -select @@last_plan_from_cache; -execute stmt using @z; -drop table if exists t; -CREATE TABLE `t` (`COL1` enum('a', 'b') NOT NULL, col2 int, PRIMARY KEY(col1, col2)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into t values('a', 1), ('b', 2); -set @a='a', @b='b', @z='z'; -prepare stmt from 'select col1 from t where (col1, col2) in ((?, 1));'; -execute stmt using @a; -execute stmt using @b; -select @@last_plan_from_cache; -execute stmt using @z; -select @@last_plan_from_cache; -execute stmt using @z; -set tidb_enable_prepared_plan_cache=DEFAULT; -set tidb_enable_clustered_index=DEFAULT; - - -# TestIssue30100 -set tidb_enable_prepared_plan_cache=1; -drop table if exists t; -create table t(col1 enum('aa', 'bb'), col2 int, index(col1, col2)); -insert into t values('aa', 333); -prepare stmt from 'SELECT * FROM t t1 JOIN t t2 ON t1.col1 = t2.col1 WHERE t1.col1 <=> NULL'; -execute stmt; -execute stmt; -select @@last_plan_from_cache; -prepare stmt from 'SELECT * FROM t t1 JOIN t t2 ON t1.col1 = t2.col1 WHERE t1.col1 <=> NULL and t2.col2 > ?'; -set @a=0; -execute stmt using @a; -execute stmt using @a; -select @@last_plan_from_cache; -set tidb_enable_prepared_plan_cache=DEFAULT; - - -# TestIssue37901 -drop table if exists t4; -create table t4 (a date); -prepare st1 from "insert into t4(a) select dt from (select ? as dt from dual union all select sysdate() ) a"; -set @t='2022-01-01 00:00:00.000000'; -execute st1 using @t; -select count(*) from t4; - - -# TestIssueEnablePreparedPlanCache2 -set tidb_enable_prepared_plan_cache=1; - -## Issue18066 -drop table if exists t; -create table t(a int); -prepare stmt from 'select * from t'; -execute stmt; -select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; -execute stmt; -select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; -prepare stmt from 'select * from t'; -execute stmt; -select @@last_plan_from_cache; -select EXEC_COUNT, plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from `t`' and TABLE_NAMES = 'planner__core__tests__prepare__issue.t'; - -## TestIssue26873 -drop table if exists t; -create table t(a int primary key, b int, c int); -prepare stmt from 'select * from t where a = 2 or a = ?'; -set @p = 3; -execute stmt using @p; -select @@last_plan_from_cache; -execute stmt using @p; -select @@last_plan_from_cache; - -## TestIssue29511 -drop table if exists t; -CREATE TABLE `t` (`COL1` bigint(20) DEFAULT NULL COMMENT 'WITH DEFAULT', UNIQUE KEY `UK_COL1` (`COL1`)); -insert into t values(-3865356285544170443),(9223372036854775807); -prepare stmt from 'select/*+ hash_agg() */ max(col1) from t where col1 = ? and col1 > ?;'; -set @a=-3865356285544170443, @b=-4055949188488870713; -execute stmt using @a,@b; - -## TestIssue23671 -drop table if exists t; -create table t (a int, b int, index ab(a, b)); -insert into t values (1, 1), (2, 2); -prepare s1 from 'select * from t use index(ab) where a>=? and b>=? and b<=?'; -set @a=1, @b=1, @c=1; -execute s1 using @a, @b, @c; -set @a=1, @b=1, @c=10; -execute s1 using @a, @b, @c; -select @@last_plan_from_cache; - -## TestIssue28920 -drop table if exists UK_GCOL_VIRTUAL_18928; -CREATE TABLE UK_GCOL_VIRTUAL_18928 ( - COL102 bigint(20) DEFAULT NULL, - COL103 bigint(20) DEFAULT NULL, - COL1 bigint(20) GENERATED ALWAYS AS (COL102 & 10) VIRTUAL, - COL2 varchar(20) DEFAULT NULL, - COL4 datetime DEFAULT NULL, - COL3 bigint(20) DEFAULT NULL, - COL5 float DEFAULT NULL, - UNIQUE KEY UK_COL1 (COL1)); -insert into UK_GCOL_VIRTUAL_18928(col102,col2) values("-5175976006730879891", "屘厒镇览錻碛斵大擔觏譨頙硺箄魨搝珄鋧扭趖"); -prepare stmt from 'SELECT * FROM UK_GCOL_VIRTUAL_18928 WHERE col1 < ? AND col2 != ?'; -set @a=10, @b="aa"; -execute stmt using @a, @b; - -## Issue29296 -drop table if exists UK_MU14722; -CREATE TABLE UK_MU14722 ( - COL1 tinytext DEFAULT NULL, - COL2 tinyint(16) DEFAULT NULL, - COL3 datetime DEFAULT NULL, - COL4 int(11) DEFAULT NULL, - UNIQUE KEY U_M_COL (COL1(10)), - UNIQUE KEY U_M_COL2 (COL2), - UNIQUE KEY U_M_COL3 (COL3)); -insert into UK_MU14722 values("輮睅麤敜溺她晁瀪襄頮鹛涓誗钷廔筪惌嶙鎢塴", -121, "3383-02-19 07:58:28" , -639457963), - ("偧孇鱓鼂瘠钻篝醗時鷷聽箌磇砀玸眞扦鸇祈灇", 127, "7902-03-05 08:54:04", -1094128660), - ("浀玡慃淛漉围甧鴎史嬙砊齄w章炢忲噑硓哈樘", -127, "5813-04-16 03:07:20", -333397107), - ("鑝粼啎鸼贖桖弦簼赭蠅鏪鐥蕿捐榥疗耹岜鬓槊", -117, "7753-11-24 10:14:24", 654872077); -prepare stmt from 'SELECT * FROM UK_MU14722 WHERE col2 > ? OR col2 BETWEEN ? AND ? ORDER BY COL2 + ? LIMIT 3'; -set @a=30410, @b=3937, @c=22045, @d=-4374; -execute stmt using @a,@b,@c,@d; -set @a=127, @b=127, @c=127, @d=127; -execute stmt using @a,@b,@c,@d; - -## TestIssue28246 -drop table if exists PK_AUTO_RANDOM9111; -CREATE TABLE `PK_AUTO_RANDOM9111` ( `COL1` bigint(45) NOT NULL , `COL2` varchar(20) DEFAULT NULL, `COL4` datetime DEFAULT NULL, `COL3` bigint(20) DEFAULT NULL, `COL5` float DEFAULT NULL, PRIMARY KEY (`COL1`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -insert into PK_AUTO_RANDOM9111(col1) values (-9223372036854775808), (9223372036854775807); -set @a=9223372036854775807, @b=1; -prepare stmt from 'select min(col1) from PK_AUTO_RANDOM9111 where col1 > ?;'; -## The plan contains the tableDual, so it will not be cached. -execute stmt using @a; -execute stmt using @a; -select @@last_plan_from_cache; -execute stmt using @b; -select @@last_plan_from_cache; -execute stmt using @a; -select @@last_plan_from_cache; - -set tidb_enable_prepared_plan_cache=DEFAULT; - -# TestIssue42739 - -use test; -drop table if exists t0; -CREATE TABLE t0 (c1 double, c2 double); -select - exists ( - select - subq_2.c0 as c8 - from - (select - ref_153.c1 as c0 - from - t0 as ref_153 - group by ref_153.c1 having 0 <> ( - select 1 - from - t0 as ref_173 - where count(ref_153.c2) = avg(ref_153.c2) - order by c1 desc limit 1)) as subq_2 - ) as c10;