Skip to content

Commit dead388

Browse files
committed
add unit test
1 parent 2ba6fda commit dead388

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

pkg/planner/core/casetest/vectorsearch/vector_index_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ import (
2424
"github.com/pingcap/tidb/pkg/domain/infosync"
2525
"github.com/pingcap/tidb/pkg/meta/model"
2626
pmodel "github.com/pingcap/tidb/pkg/parser/model"
27+
"github.com/pingcap/tidb/pkg/planner"
2728
"github.com/pingcap/tidb/pkg/planner/core"
2829
"github.com/pingcap/tidb/pkg/planner/core/base"
30+
"github.com/pingcap/tidb/pkg/planner/core/resolve"
31+
"github.com/pingcap/tidb/pkg/session"
2932
"github.com/pingcap/tidb/pkg/store/mockstore"
3033
"github.com/pingcap/tidb/pkg/testkit"
3134
"github.com/pingcap/tidb/pkg/testkit/testdata"
3235
"github.com/pingcap/tidb/pkg/testkit/testfailpoint"
36+
"github.com/pingcap/tidb/pkg/types"
3337
"github.com/pingcap/tidb/pkg/util/plancodec"
3438
"github.com/pingcap/tipb/go-tipb"
3539
"github.com/stretchr/testify/require"
@@ -244,3 +248,65 @@ func TestANNInexWithSimpleCBO(t *testing.T) {
244248
testkit.SetTiFlashReplica(t, dom, "test", "t1")
245249
tk.MustUseIndex("select * from t1 order by vec_cosine_distance(vec, '[1,1,1]') limit 1", "vector_index")
246250
}
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

Comments
 (0)