Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vector: shorten explain output for vector constants #55406

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/expression/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ func (c *Constant) StringWithCtx(ctx ParamValues, redact string) string {
return c.DeferredExpr.StringWithCtx(ctx, redact)
}
if redact == perrors.RedactLogDisable {
EricZequan marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf("%v", c.Value.GetValue())
return fmt.Sprintf("%v", c.Value.StringTruncate())
} else if redact == perrors.RedactLogMarker {
return fmt.Sprintf("‹%v›", c.Value.GetValue())
return fmt.Sprintf("‹%v›", c.Value.StringTruncate())
}
return "?"
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/expression/integration_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_test(
"main_test.go",
],
flaky = True,
shard_count = 41,
shard_count = 42,
deps = [
"//pkg/config",
"//pkg/domain",
Expand Down
25 changes: 25 additions & 0 deletions pkg/expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,31 @@ func TestVectorColumnInfo(t *testing.T) {
tk.MustGetErrMsg("create table t(embedding VECTOR(16384))", "vector cannot have more than 16383 dimensions")
}

func TestVectorConstantExplain(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("CREATE TABLE t(c VECTOR);")
tk.MustExec("set session tidb_redact_log=off")
tk.MustQuery(`EXPLAIN SELECT VEC_COSINE_DISTANCE(c, '[1,2,3,4,5,6,7,8,9,10,11]') FROM t;`).Check(testkit.Rows(
"Projection_3 10000.00 root vec_cosine_distance(test.t.c, [1,2,3,4,5,(6 more)...])->Column#3",
"└─TableReader_5 10000.00 root data:TableFullScan_4",
" └─TableFullScan_4 10000.00 cop[tikv] table:t keep order:false, stats:pseudo",
))
tk.MustExec("set session tidb_redact_log=on")
tk.MustQuery(`EXPLAIN SELECT VEC_COSINE_DISTANCE(c, VEC_FROM_TEXT('[1,2,3,4,5,6,7,8,9,10,11]')) FROM t;`).Check(testkit.Rows(
"Projection_3 10000.00 root vec_cosine_distance(test.t.c, ?)->Column#3",
"└─TableReader_5 10000.00 root data:TableFullScan_4",
" └─TableFullScan_4 10000.00 cop[tikv] table:t keep order:false, stats:pseudo",
))
tk.MustExec("set session tidb_redact_log=marker")
tk.MustQuery(`EXPLAIN SELECT VEC_COSINE_DISTANCE(c, VEC_FROM_TEXT('[1,2,3,4,5,6,7,8,9,10,11]')) FROM t;`).Check(testkit.Rows(
"Projection_3 10000.00 root vec_cosine_distance(test.t.c, ‹[1,2,3,4,5,(6 more)...]›)->Column#3",
"└─TableReader_5 10000.00 root data:TableFullScan_4",
" └─TableFullScan_4 10000.00 cop[tikv] table:t keep order:false, stats:pseudo",
))
}

func TestFixedVector(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
12 changes: 11 additions & 1 deletion pkg/types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ func (d *Datum) compareMysqlTime(ctx Context, time Time) (int, error) {
}
}

func (d *Datum) compareVectorFloat32(ctx Context, vec VectorFloat32) (int, error) {
func (d *Datum) compareVectorFloat32(_ Context, vec VectorFloat32) (int, error) {
switch d.k {
case KindNull, KindMinNotNull:
return -1, nil
Expand Down Expand Up @@ -2082,6 +2082,16 @@ func (d *Datum) ToString() (string, error) {
}
}

// StringTruncate truncate long string.
func (d *Datum) StringTruncate() string {
switch d.k {
case KindVectorFloat32:
return d.GetVectorFloat32().StringTruncate()
default:
return fmt.Sprintf("%v", d.GetValue())
EricZequan marked this conversation as resolved.
Show resolved Hide resolved
}
}

// ToBytes gets the bytes representation of the datum.
func (d *Datum) ToBytes() ([]byte, error) {
switch d.k {
Expand Down
35 changes: 34 additions & 1 deletion pkg/types/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package types

import (
"encoding/binary"
"fmt"
"math"
"strconv"
"unsafe"
Expand Down Expand Up @@ -93,7 +94,39 @@ func (v VectorFloat32) Elements() []float32 {
return unsafe.Slice((*float32)(unsafe.Pointer(&v.data[4])), l)
}

// String returns a string representation of the vector, which can be parsed later.
// StringTruncate truncate vector to a readable format.
func (v VectorFloat32) StringTruncate() string {
const (
maxDisplayElements = 5
)

truncatedElements := 0
elements := v.Elements()

if len(elements) > maxDisplayElements {
truncatedElements = len(elements) - maxDisplayElements
elements = elements[:maxDisplayElements]
}

buf := make([]byte, 0, 2+v.Len()*2)
buf = append(buf, '[')
for i, v := range elements {
if i > 0 {
buf = append(buf, ","...)
}
buf = strconv.AppendFloat(buf, float64(v), 'g', 2, 32)
}
if truncatedElements > 0 {
buf = append(buf, fmt.Sprintf(",(%d more)...", truncatedElements)...)
}
buf = append(buf, ']')

// buf is not used elsewhere, so it's safe to just cast to String
return unsafe.String(unsafe.SliceData(buf), len(buf))
}

// String implements the fmt.Stringer interface.
// It returns a string representation of the vector which can be parsed later.
func (v VectorFloat32) String() string {
elements := v.Elements()

Expand Down