@@ -24,12 +24,16 @@ import (
24
24
"github.com/pingcap/tidb/pkg/domain/infosync"
25
25
"github.com/pingcap/tidb/pkg/meta/model"
26
26
pmodel "github.com/pingcap/tidb/pkg/parser/model"
27
+ "github.com/pingcap/tidb/pkg/planner"
27
28
"github.com/pingcap/tidb/pkg/planner/core"
28
29
"github.com/pingcap/tidb/pkg/planner/core/base"
30
+ "github.com/pingcap/tidb/pkg/planner/core/resolve"
31
+ "github.com/pingcap/tidb/pkg/session"
29
32
"github.com/pingcap/tidb/pkg/store/mockstore"
30
33
"github.com/pingcap/tidb/pkg/testkit"
31
34
"github.com/pingcap/tidb/pkg/testkit/testdata"
32
35
"github.com/pingcap/tidb/pkg/testkit/testfailpoint"
36
+ "github.com/pingcap/tidb/pkg/types"
33
37
"github.com/pingcap/tidb/pkg/util/plancodec"
34
38
"github.com/pingcap/tipb/go-tipb"
35
39
"github.com/stretchr/testify/require"
@@ -244,3 +248,65 @@ func TestANNInexWithSimpleCBO(t *testing.T) {
244
248
testkit .SetTiFlashReplica (t , dom , "test" , "t1" )
245
249
tk .MustUseIndex ("select * from t1 order by vec_cosine_distance(vec, '[1,1,1]') limit 1" , "vector_index" )
246
250
}
251
+
252
+ func TestANNIndexWithNonIntClusteredPk (t * testing.T ) {
253
+ store := testkit .CreateMockStoreWithSchemaLease (t , 1 * time .Second , mockstore .WithMockTiFlash (2 ))
254
+
255
+ tk := testkit .NewTestKit (t , store )
256
+
257
+ tiflash := infosync .NewMockTiFlash ()
258
+ infosync .SetMockTiFlash (tiflash )
259
+ defer func () {
260
+ tiflash .Lock ()
261
+ tiflash .StatusServer .Close ()
262
+ tiflash .Unlock ()
263
+ }()
264
+
265
+ testfailpoint .Enable (t , "github.com/pingcap/tidb/pkg/ddl/MockCheckVectorIndexProcess" , `return(1)` )
266
+
267
+ tk .MustExec ("use test" )
268
+ tk .MustExec ("drop table if exists t1" )
269
+ tk .MustExec (`
270
+ create table t1 (
271
+ vec vector(3),
272
+ a int,
273
+ b int,
274
+ c vector(3),
275
+ d vector,
276
+ primary key (a, b)
277
+ )
278
+ ` )
279
+ tk .MustExec ("alter table t1 set tiflash replica 1;" )
280
+ tk .MustExec ("alter table t1 add vector index ((vec_cosine_distance(vec))) USING HNSW;" )
281
+ tk .MustExec ("insert into t1 values ('[1,1,1]', 1, 1, '[1,1,1]', '[1,1,1]')" )
282
+ dom := domain .GetDomain (tk .Session ())
283
+ testkit .SetTiFlashReplica (t , dom , "test" , "t1" )
284
+ sctx := tk .Session ()
285
+ stmts , err := session .Parse (sctx , "select * from t1 use index(vector_index) order by vec_cosine_distance(vec, '[1,1,1]') limit 1" )
286
+ require .NoError (t , err )
287
+ require .Len (t , stmts , 1 )
288
+ stmt := stmts [0 ]
289
+ ret := & core.PreprocessorReturn {}
290
+ nodeW := resolve .NewNodeW (stmt )
291
+ err = core .Preprocess (context .Background (), sctx , nodeW , core .WithPreprocessorReturn (ret ))
292
+ require .NoError (t , err )
293
+ finalPlanTree , _ , err := planner .Optimize (context .Background (), sctx , nodeW , ret .InfoSchema )
294
+ physicalTree , ok := finalPlanTree .(base.PhysicalPlan )
295
+ require .True (t , ok )
296
+ // Find the PhysicalTableReader node.
297
+ tableReader := physicalTree
298
+ for ; len (tableReader .Children ()) > 0 ; tableReader = tableReader .Children ()[0 ] {
299
+ }
300
+ castedTableReader , ok := tableReader .(* core.PhysicalTableReader )
301
+ require .True (t , ok )
302
+ tableScan , err := castedTableReader .GetTableScan ()
303
+ require .NoError (t , err )
304
+ // Check that it has the extra vector index information.
305
+ require .NotNil (t , tableScan .AnnIndexExtra )
306
+ require .Len (t , tableScan .Ranges , 1 )
307
+ // Check that it's full scan.
308
+ require .Equal (t , "[-inf,+inf]" , tableScan .Ranges [0 ].String ())
309
+ // Check that the -inf and +inf are the correct types.
310
+ require .Equal (t , types .KindMinNotNull , tableScan .Ranges [0 ].LowVal [0 ].Kind ())
311
+ require .Equal (t , types .KindMaxValue , tableScan .Ranges [0 ].HighVal [0 ].Kind ())
312
+ }
0 commit comments