@@ -688,3 +688,60 @@ func (s *testSuite) TestPrepareDealloc(c *C) {
688
688
tk .MustExec ("deallocate prepare stmt4" )
689
689
c .Assert (tk .Se .PreparedPlanCache ().Size (), Equals , 0 )
690
690
}
691
+
692
+ func (s * testSuite ) TestPreparedIssue8153 (c * C ) {
693
+ orgEnable := plannercore .PreparedPlanCacheEnabled ()
694
+ orgCapacity := plannercore .PreparedPlanCacheCapacity
695
+ orgMemGuardRatio := plannercore .PreparedPlanCacheMemoryGuardRatio
696
+ orgMaxMemory := plannercore .PreparedPlanCacheMaxMemory
697
+ defer func () {
698
+ plannercore .SetPreparedPlanCache (orgEnable )
699
+ plannercore .PreparedPlanCacheCapacity = orgCapacity
700
+ plannercore .PreparedPlanCacheMemoryGuardRatio = orgMemGuardRatio
701
+ plannercore .PreparedPlanCacheMaxMemory = orgMaxMemory
702
+ }()
703
+ flags := []bool {false , true }
704
+ for _ , flag := range flags {
705
+ var err error
706
+ plannercore .SetPreparedPlanCache (flag )
707
+ plannercore .PreparedPlanCacheCapacity = 100
708
+ plannercore .PreparedPlanCacheMemoryGuardRatio = 0.1
709
+ plannercore .PreparedPlanCacheMaxMemory , err = memory .MemTotal ()
710
+ tk := testkit .NewTestKit (c , s .store )
711
+ tk .MustExec ("use test" )
712
+ tk .MustExec ("drop table if exists t" )
713
+ tk .MustExec ("create table t (a int, b int)" )
714
+ tk .MustExec ("insert into t (a, b) values (1,3), (2,2), (3,1)" )
715
+
716
+ tk .MustExec (`prepare stmt from 'select * from t order by ? asc'` )
717
+ r := tk .MustQuery (`execute stmt using @param;` )
718
+ r .Check (testkit .Rows ("1 3" , "2 2" , "3 1" ))
719
+
720
+ tk .MustExec (`set @param = 1` )
721
+ r = tk .MustQuery (`execute stmt using @param;` )
722
+ r .Check (testkit .Rows ("1 3" , "2 2" , "3 1" ))
723
+
724
+ tk .MustExec (`set @param = 2` )
725
+ r = tk .MustQuery (`execute stmt using @param;` )
726
+ r .Check (testkit .Rows ("3 1" , "2 2" , "1 3" ))
727
+
728
+ tk .MustExec (`set @param = 3` )
729
+ _ , err = tk .Exec (`execute stmt using @param;` )
730
+ c .Assert (err .Error (), Equals , "[planner:1054]Unknown column '?' in 'order clause'" )
731
+
732
+ tk .MustExec (`set @param = '##'` )
733
+ r = tk .MustQuery (`execute stmt using @param;` )
734
+ r .Check (testkit .Rows ("1 3" , "2 2" , "3 1" ))
735
+
736
+ tk .MustExec ("insert into t (a, b) values (1,1), (1,2), (2,1), (2,3), (3,2), (3,3)" )
737
+ tk .MustExec (`prepare stmt from 'select ?, sum(a) from t group by ?'` )
738
+
739
+ tk .MustExec (`set @a=1,@b=1` )
740
+ r = tk .MustQuery (`execute stmt using @a,@b;` )
741
+ r .Check (testkit .Rows ("1 18" ))
742
+
743
+ tk .MustExec (`set @a=1,@b=2` )
744
+ _ , err = tk .Exec (`execute stmt using @a,@b;` )
745
+ c .Assert (err .Error (), Equals , "[planner:1056]Can't group on 'sum(a)'" )
746
+ }
747
+ }
0 commit comments