@@ -26,7 +26,9 @@ import (
26
26
"github.com/pingcap/tidb/expression"
27
27
"github.com/pingcap/tidb/kv"
28
28
"github.com/pingcap/tidb/parser/mysql"
29
+ "github.com/pingcap/tidb/planner"
29
30
plannercore "github.com/pingcap/tidb/planner/core"
31
+ "github.com/pingcap/tidb/session"
30
32
"github.com/pingcap/tidb/testkit"
31
33
"github.com/pingcap/tidb/types"
32
34
"github.com/pingcap/tidb/util"
@@ -136,6 +138,35 @@ func TestIssue40296(t *testing.T) {
136
138
tk .MustQuery ("select @@last_plan_from_cache" ).Check (testkit .Rows ("0" )) // unary operator '-' is not supported now.
137
139
}
138
140
141
+ func TestNonPreparedPlanCachePlanString (t * testing.T ) {
142
+ store := testkit .CreateMockStore (t )
143
+ tk := testkit .NewTestKit (t , store )
144
+ tk .MustExec (`use test` )
145
+ tk .MustExec (`create table t (a int, b int, key(a))` )
146
+ tk .MustExec (`set @@tidb_enable_non_prepared_plan_cache=1` )
147
+
148
+ ctx := tk .Session ()
149
+ planString := func (sql string ) string {
150
+ stmts , err := session .Parse (ctx , sql )
151
+ require .NoError (t , err )
152
+ stmt := stmts [0 ]
153
+ ret := & plannercore.PreprocessorReturn {}
154
+ err = plannercore .Preprocess (context .Background (), ctx , stmt , plannercore .WithPreprocessorReturn (ret ))
155
+ require .NoError (t , err )
156
+ p , _ , err := planner .Optimize (context .TODO (), ctx , stmt , ret .InfoSchema )
157
+ require .NoError (t , err )
158
+ return plannercore .ToString (p )
159
+ }
160
+
161
+ require .Equal (t , planString ("select a from t where a < 1" ), "IndexReader(Index(t.a)[[-inf,1)])" )
162
+ require .Equal (t , planString ("select a from t where a < 10" ), "IndexReader(Index(t.a)[[-inf,10)])" ) // range 1 -> 10
163
+ tk .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" ))
164
+
165
+ require .Equal (t , planString ("select * from t where b < 1" ), "TableReader(Table(t)->Sel([lt(test.t.b, 1)]))" )
166
+ require .Equal (t , planString ("select * from t where b < 10" ), "TableReader(Table(t)->Sel([lt(test.t.b, 10)]))" ) // filter 1 -> 10
167
+ tk .MustQuery (`select @@last_plan_from_cache` ).Check (testkit .Rows ("1" ))
168
+ }
169
+
139
170
func TestNonPreparedPlanCacheJSONFilter (t * testing.T ) {
140
171
store := testkit .CreateMockStore (t )
141
172
tk := testkit .NewTestKit (t , store )
0 commit comments