@@ -780,6 +780,83 @@ func (s *testSerialSuite) TestIssue28782(c *C) {
780
780
tk .MustQuery ("select @@last_plan_from_cache;" ).Check (testkit .Rows ("1" ))
781
781
}
782
782
783
+ func (s * testSerialSuite ) TestIssue29850 (c * C ) {
784
+ store , dom , err := newStoreWithBootstrap ()
785
+ c .Assert (err , IsNil )
786
+ tk := testkit .NewTestKit (c , store )
787
+ defer func () {
788
+ dom .Close ()
789
+ store .Close ()
790
+ }()
791
+ orgEnable := plannercore .PreparedPlanCacheEnabled ()
792
+ defer func () {
793
+ plannercore .SetPreparedPlanCache (orgEnable )
794
+ }()
795
+ plannercore .SetPreparedPlanCache (true )
796
+
797
+ tk .MustExec (`set tidb_enable_clustered_index=on` )
798
+ tk .MustExec ("set @@tidb_enable_collect_execution_info=0" )
799
+ tk .MustExec (`use test` )
800
+ tk .MustExec (`CREATE TABLE customer (
801
+ c_id int(11) NOT NULL,
802
+ c_d_id int(11) NOT NULL,
803
+ c_first varchar(16) DEFAULT NULL,
804
+ c_w_id int(11) NOT NULL,
805
+ c_last varchar(16) DEFAULT NULL,
806
+ c_credit char(2) DEFAULT NULL,
807
+ c_discount decimal(4,4) DEFAULT NULL,
808
+ PRIMARY KEY (c_w_id,c_d_id,c_id),
809
+ KEY idx_customer (c_w_id,c_d_id,c_last,c_first))` )
810
+ tk .MustExec (`CREATE TABLE warehouse (
811
+ w_id int(11) NOT NULL,
812
+ w_tax decimal(4,4) DEFAULT NULL,
813
+ PRIMARY KEY (w_id))` )
814
+ tk .MustExec (`prepare stmt from 'SELECT c_discount, c_last, c_credit, w_tax
815
+ FROM customer, warehouse
816
+ WHERE w_id = ? AND c_w_id = w_id AND c_d_id = ? AND c_id = ?'` )
817
+ tk .MustExec (`set @w_id=1262` )
818
+ tk .MustExec (`set @c_d_id=7` )
819
+ tk .MustExec (`set @c_id=1549` )
820
+ tk .MustQuery (`execute stmt using @w_id, @c_d_id, @c_id` ).Check (testkit .Rows ())
821
+ tkProcess := tk .Se .ShowProcess ()
822
+ ps := []* util.ProcessInfo {tkProcess }
823
+ tk .Se .SetSessionManager (& mockSessionManager1 {PS : ps })
824
+ tk .MustQuery (fmt .Sprintf ("explain for connection %d" , tkProcess .ID )).Check (testkit .Rows ( // can use PointGet
825
+ `Projection_7 0.00 root test.customer.c_discount, test.customer.c_last, test.customer.c_credit, test.warehouse.w_tax` ,
826
+ `└─MergeJoin_8 0.00 root inner join, left key:test.customer.c_w_id, right key:test.warehouse.w_id` ,
827
+ ` ├─Point_Get_34(Build) 1.00 root table:warehouse handle:1262` ,
828
+ ` └─Point_Get_33(Probe) 1.00 root table:customer, clustered index:PRIMARY(c_w_id, c_d_id, c_id) ` ))
829
+ tk .MustQuery (`execute stmt using @w_id, @c_d_id, @c_id` ).Check (testkit .Rows ())
830
+ tk .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" )) // can use the cached plan
831
+
832
+ tk .MustExec (`create table t (a int primary key)` )
833
+ tk .MustExec (`insert into t values (1), (2)` )
834
+ tk .MustExec (`prepare stmt from 'select * from t where a>=? and a<=?'` )
835
+ tk .MustExec (`set @a1=1, @a2=2` )
836
+ tk .MustQuery (`execute stmt using @a1, @a1` ).Check (testkit .Rows ("1" ))
837
+ tkProcess = tk .Se .ShowProcess ()
838
+ ps = []* util.ProcessInfo {tkProcess }
839
+ tk .Se .SetSessionManager (& mockSessionManager1 {PS : ps })
840
+ tk .MustQuery (fmt .Sprintf ("explain for connection %d" , tkProcess .ID )).Check (testkit .Rows ( // cannot use PointGet since it contains a range condition
841
+ `Selection_7 1.00 root ge(test.t.a, 1), le(test.t.a, 1)` ,
842
+ `└─TableReader_6 1.00 root data:TableRangeScan_5` ,
843
+ ` └─TableRangeScan_5 1.00 cop[tikv] table:t range:[1,1], keep order:false, stats:pseudo` ))
844
+ tk .MustQuery (`execute stmt using @a1, @a2` ).Check (testkit .Rows ("1" , "2" ))
845
+ tk .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" ))
846
+
847
+ tk .MustExec (`prepare stmt from 'select * from t where a=? or a=?'` )
848
+ tk .MustQuery (`execute stmt using @a1, @a1` ).Check (testkit .Rows ("1" ))
849
+ tkProcess = tk .Se .ShowProcess ()
850
+ ps = []* util.ProcessInfo {tkProcess }
851
+ tk .Se .SetSessionManager (& mockSessionManager1 {PS : ps })
852
+ tk .MustQuery (fmt .Sprintf ("explain for connection %d" , tkProcess .ID )).Check (testkit .Rows ( // cannot use PointGet since it contains a or condition
853
+ `Selection_7 1.00 root or(eq(test.t.a, 1), eq(test.t.a, 1))` ,
854
+ `└─TableReader_6 1.00 root data:TableRangeScan_5` ,
855
+ ` └─TableRangeScan_5 1.00 cop[tikv] table:t range:[1,1], keep order:false, stats:pseudo` ))
856
+ tk .MustQuery (`execute stmt using @a1, @a2` ).Check (testkit .Rows ("1" , "2" ))
857
+
858
+ }
859
+
783
860
func (s * testSerialSuite ) TestIssue29101 (c * C ) {
784
861
store , dom , err := newStoreWithBootstrap ()
785
862
c .Assert (err , IsNil )
@@ -821,10 +898,7 @@ func (s *testSerialSuite) TestIssue29101(c *C) {
821
898
tk .MustQuery (fmt .Sprintf ("explain for connection %d" , tkProcess .ID )).Check (testkit .Rows ( // can use IndexJoin
822
899
`Projection_6 1.00 root test.customer.c_discount, test.customer.c_last, test.customer.c_credit, test.warehouse.w_tax` ,
823
900
`└─IndexJoin_14 1.00 root inner join, inner:TableReader_10, outer key:test.customer.c_w_id, inner key:test.warehouse.w_id, equal cond:eq(test.customer.c_w_id, test.warehouse.w_id)` ,
824
- ` ├─Selection_36(Build) 1.00 root eq(test.customer.c_d_id, 7), eq(test.customer.c_id, 158), eq(test.customer.c_w_id, 936)` ,
825
- ` │ └─IndexLookUp_35 1.00 root ` ,
826
- ` │ ├─IndexRangeScan_33(Build) 1.00 cop[tikv] table:customer, index:PRIMARY(c_w_id, c_d_id, c_id) range:[936 7 158,936 7 158], keep order:false, stats:pseudo` ,
827
- ` │ └─TableRowIDScan_34(Probe) 1.00 cop[tikv] table:customer keep order:false, stats:pseudo` ,
901
+ ` ├─Point_Get_33(Build) 1.00 root table:customer, index:PRIMARY(c_w_id, c_d_id, c_id) ` ,
828
902
` └─TableReader_10(Probe) 0.00 root data:Selection_9` ,
829
903
` └─Selection_9 0.00 cop[tikv] eq(test.warehouse.w_id, 936)` ,
830
904
` └─TableRangeScan_8 1.00 cop[tikv] table:warehouse range: decided by [test.customer.c_w_id], keep order:false, stats:pseudo` ))
0 commit comments