Skip to content

Commit e69a30e

Browse files
authored
planner: Fix vector not truncated after CBO (#58809) (#58844)
close #58837
1 parent 0005334 commit e69a30e

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

pkg/expression/integration_test/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ go_test(
88
"main_test.go",
99
],
1010
flaky = True,
11-
shard_count = 49,
11+
shard_count = 50,
1212
deps = [
1313
"//pkg/config",
1414
"//pkg/domain",

pkg/expression/integration_test/integration_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,24 @@ func TestVectorColumnInfo(t *testing.T) {
418418
tk.MustGetErrMsg("create table t(embedding VECTOR(16384))", "vector cannot have more than 16383 dimensions")
419419
}
420420

421+
func TestVectorExplainTruncate(t *testing.T) {
422+
store := testkit.CreateMockStore(t)
423+
tk := testkit.NewTestKit(t, store)
424+
tk.MustExec("use test")
425+
tk.MustExec("CREATE TABLE t(c VECTOR);")
426+
427+
// TODO: The output can be improved.
428+
tk.MustQuery(`EXPLAIN format='brief' SELECT
429+
VEC_COSINE_DISTANCE(c, '[3,100,12345,10000]'),
430+
VEC_COSINE_DISTANCE(c, '[11111111111,11111111111.23456789,3.1,5.12456]'),
431+
VEC_COSINE_DISTANCE(c, '[-11111111111,-11111111111.23456789,-3.1,-5.12456]')
432+
FROM t;`).Check(testkit.Rows(
433+
`Projection 10000.00 root vec_cosine_distance(test.t.c, [3,1e+02,1.2e+04,1e+04])->Column#3, vec_cosine_distance(test.t.c, [1.1e+10,1.1e+10,3.1,5.1])->Column#4, vec_cosine_distance(test.t.c, [-1.1e+10,-1.1e+10,-3.1,-5.1])->Column#5`,
434+
`└─TableReader 10000.00 root data:TableFullScan`,
435+
` └─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`,
436+
))
437+
}
438+
421439
func TestVectorConstantExplain(t *testing.T) {
422440
store := testkit.CreateMockStore(t)
423441
tk := testkit.NewTestKit(t, store)
@@ -479,6 +497,62 @@ func TestVectorConstantExplain(t *testing.T) {
479497
tk.ResultSetToResult(rs, fmt.Sprintf("%v", rs))
480498
}
481499

500+
func TestVectorIndexExplain(t *testing.T) {
501+
store := testkit.CreateMockStoreWithSchemaLease(t, 1*time.Second, mockstore.WithMockTiFlash(2))
502+
503+
tk := testkit.NewTestKit(t, store)
504+
505+
tiflash := infosync.NewMockTiFlash()
506+
infosync.SetMockTiFlash(tiflash)
507+
defer func() {
508+
tiflash.Lock()
509+
tiflash.StatusServer.Close()
510+
tiflash.Unlock()
511+
}()
512+
513+
failpoint.Enable("github.com/pingcap/tidb/pkg/ddl/MockCheckVectorIndexProcess", `return(1)`)
514+
defer func() {
515+
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/ddl/MockCheckVectorIndexProcess"))
516+
}()
517+
518+
tk.MustExec("use test")
519+
tk.MustExec("drop table if exists t1")
520+
tk.MustExec(`
521+
create table t1 (
522+
vec vector(100)
523+
)
524+
`)
525+
tk.MustExec("alter table t1 set tiflash replica 1;")
526+
tk.MustExec("alter table t1 add vector index ((vec_cosine_distance(vec))) USING HNSW;")
527+
tbl, _ := domain.GetDomain(tk.Session()).InfoSchema().TableByName(context.Background(), pmodel.NewCIStr("test"), pmodel.NewCIStr("t1"))
528+
tbl.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{
529+
Count: 1,
530+
Available: true,
531+
}
532+
533+
vb := strings.Builder{}
534+
vb.WriteString("[")
535+
for i := 0; i < 100; i++ {
536+
if i > 0 {
537+
vb.WriteString(",")
538+
}
539+
vb.WriteString("100")
540+
}
541+
vb.WriteString("]")
542+
543+
tk.MustQuery(fmt.Sprintf("explain format = 'brief' select * from t1 order by vec_cosine_distance(vec, '%s') limit 1", vb.String())).Check(testkit.Rows(
544+
`Projection 1.00 root test.t1.vec`,
545+
`└─TopN 1.00 root Column#4, offset:0, count:1`,
546+
` └─Projection 1.00 root test.t1.vec, vec_cosine_distance(test.t1.vec, [1e+02,1e+02,1e+02,1e+02,1e+02,(95 more)...])->Column#4`,
547+
` └─TableReader 1.00 root MppVersion: 2, data:ExchangeSender`,
548+
` └─ExchangeSender 1.00 mpp[tiflash] ExchangeType: PassThrough`,
549+
` └─Projection 1.00 mpp[tiflash] test.t1.vec`,
550+
` └─TopN 1.00 mpp[tiflash] Column#3, offset:0, count:1`,
551+
` └─Projection 1.00 mpp[tiflash] test.t1.vec, vec_cosine_distance(test.t1.vec, [1e+02,1e+02,1e+02,1e+02,1e+02,(95 more)...])->Column#3`,
552+
` └─TableFullScan 1.00 mpp[tiflash] table:t1, index:vector_index(vec) keep order:false, stats:pseudo, annIndex:COSINE(vec..[1e+02,1e+02,1e+02,1e+02,1e+02,(95 more)...], limit:1)`,
553+
))
554+
}
555+
482556
func TestFixedVector(t *testing.T) {
483557
store := testkit.CreateMockStore(t)
484558
tk := testkit.NewTestKit(t, store)

pkg/planner/core/explain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (p *PhysicalTableScan) OperatorInfo(normalized bool) string {
283283
if err != nil {
284284
buffer.WriteString("[?]")
285285
} else {
286-
buffer.WriteString(v.String())
286+
buffer.WriteString(v.TruncatedString())
287287
}
288288
}
289289
buffer.WriteString(", limit:")

0 commit comments

Comments
 (0)