Skip to content

Commit eca8aca

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

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

pkg/planner/core/casetest/vectorsearch/BUILD.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,25 @@ go_test(
99
],
1010
data = glob(["testdata/**"]),
1111
flaky = True,
12-
shard_count = 4,
12+
shard_count = 5,
1313
deps = [
1414
"//pkg/config",
1515
"//pkg/domain",
1616
"//pkg/domain/infosync",
1717
"//pkg/meta/model",
1818
"//pkg/parser/model",
19+
"//pkg/planner",
1920
"//pkg/planner/core",
2021
"//pkg/planner/core/base",
22+
"//pkg/planner/core/resolve",
23+
"//pkg/session",
2124
"//pkg/store/mockstore",
2225
"//pkg/testkit",
2326
"//pkg/testkit/testdata",
2427
"//pkg/testkit/testfailpoint",
2528
"//pkg/testkit/testmain",
2629
"//pkg/testkit/testsetup",
30+
"//pkg/types",
2731
"//pkg/util/plancodec",
2832
"@com_github_pingcap_tipb//go-tipb",
2933
"@com_github_stretchr_testify//require",

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)