From d23e795bd4f12b1b49812e58051fe7831315f87d Mon Sep 17 00:00:00 2001 From: EricZequan <110292382+EricZequan@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:27:01 +0800 Subject: [PATCH] parser: Deprecate VECTOR. Use VECTOR instead. (#55134) ref pingcap/tidb#54245 --- pkg/expression/builtin_cast.go | 16 ++--- .../integration_test/integration_test.go | 38 ++++++------ pkg/parser/parser_test.go | 9 +-- pkg/parser/types/etc.go | 58 +++++++++---------- pkg/parser/types/field_type.go | 2 +- pkg/types/etc.go | 2 +- 6 files changed, 59 insertions(+), 66 deletions(-) diff --git a/pkg/expression/builtin_cast.go b/pkg/expression/builtin_cast.go index 16f7a4c2f44eb..fbdbba5d26033 100644 --- a/pkg/expression/builtin_cast.go +++ b/pkg/expression/builtin_cast.go @@ -710,7 +710,7 @@ func (b *builtinCastUnsupportedAsVectorFloat32Sig) Clone() builtinFunc { func (b *builtinCastUnsupportedAsVectorFloat32Sig) evalVectorFloat32(ctx EvalContext, _ chunk.Row) (res types.VectorFloat32, isNull bool, err error) { return types.ZeroVectorFloat32, false, errors.Errorf( - "cannot cast from %s to vector", + "cannot cast from %s to vector", types.TypeStr(b.args[0].GetType(ctx).GetType())) } @@ -726,43 +726,43 @@ func (b *builtinCastVectorFloat32AsUnsupportedSig) Clone() builtinFunc { func (b *builtinCastVectorFloat32AsUnsupportedSig) evalInt(_ EvalContext, _ chunk.Row) (int64, bool, error) { return 0, false, errors.Errorf( - "cannot cast from vector to %s", + "cannot cast from vector to %s", types.TypeStr(b.tp.GetType())) } func (b *builtinCastVectorFloat32AsUnsupportedSig) evalReal(_ EvalContext, _ chunk.Row) (float64, bool, error) { return 0, false, errors.Errorf( - "cannot cast from vector to %s", + "cannot cast from vector to %s", types.TypeStr(b.tp.GetType())) } func (b *builtinCastVectorFloat32AsUnsupportedSig) evalDecimal(_ EvalContext, _ chunk.Row) (*types.MyDecimal, bool, error) { return nil, false, errors.Errorf( - "cannot cast from vector to %s", + "cannot cast from vector to %s", types.TypeStr(b.tp.GetType())) } func (b *builtinCastVectorFloat32AsUnsupportedSig) evalString(_ EvalContext, _ chunk.Row) (string, bool, error) { return "", false, errors.Errorf( - "cannot cast from vector to %s", + "cannot cast from vector to %s", types.TypeStr(b.tp.GetType())) } func (b *builtinCastVectorFloat32AsUnsupportedSig) evalTime(_ EvalContext, _ chunk.Row) (types.Time, bool, error) { return types.ZeroTime, false, errors.Errorf( - "cannot cast from vector to %s", + "cannot cast from vector to %s", types.TypeStr(b.tp.GetType())) } func (b *builtinCastVectorFloat32AsUnsupportedSig) evalDuration(_ EvalContext, _ chunk.Row) (types.Duration, bool, error) { return types.ZeroDuration, false, errors.Errorf( - "cannot cast from vector to %s", + "cannot cast from vector to %s", types.TypeStr(b.tp.GetType())) } func (b *builtinCastVectorFloat32AsUnsupportedSig) evalJSON(_ EvalContext, _ chunk.Row) (types.BinaryJSON, bool, error) { return types.BinaryJSON{}, false, errors.Errorf( - "cannot cast from vector to %s", + "cannot cast from vector to %s", types.TypeStr(b.tp.GetType())) } diff --git a/pkg/expression/integration_test/integration_test.go b/pkg/expression/integration_test/integration_test.go index a557f8a942cbe..2cbfdf85d2496 100644 --- a/pkg/expression/integration_test/integration_test.go +++ b/pkg/expression/integration_test/integration_test.go @@ -64,51 +64,49 @@ func TestVectorColumnInfo(t *testing.T) { // Create vector type column without specified dimension. tk.MustExec("create table t(embedding VECTOR)") - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(embedding VECTOR)") // SHOW CREATE TABLE tk.MustQuery("show create table t").Check(testkit.Rows( "t CREATE TABLE `t` (\n" + - " `embedding` vector DEFAULT NULL\n" + + " `embedding` vector DEFAULT NULL\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) // SHOW COLUMNS tk.MustQuery("show columns from t").Check(testkit.Rows( - "embedding vector YES ", + "embedding vector YES ", )) // Create vector type column with specified dimension. tk.MustExec("drop table if exists t;") tk.MustExec("create table t(embedding VECTOR(3))") tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(embedding VECTOR(3))") + tk.MustExec("create table t(embedding VECTOR(3))") tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(embedding VECTOR(0))") + tk.MustExec("create table t(embedding VECTOR(0))") // SHOW CREATE TABLE tk.MustExec("drop table if exists t;") tk.MustExec("create table t(embedding VECTOR(3))") tk.MustQuery("show create table t").Check(testkit.Rows( "t CREATE TABLE `t` (\n" + - " `embedding` vector(3) DEFAULT NULL\n" + + " `embedding` vector(3) DEFAULT NULL\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) // SHOW COLUMNS tk.MustQuery("show columns from t").Check(testkit.Rows( - "embedding vector(3) YES ", + "embedding vector(3) YES ", )) // INFORMATION_SCHEMA.COLUMNS tk.MustQuery("SELECT data_type, column_type FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 't'").Check(testkit.Rows( - "vector vector(3)", + "vector vector(3)", )) // Vector dimension MUST be equal or less than 16383. tk.MustExec("drop table if exists t;") - tk.MustGetErrMsg("create table t(embedding VECTOR(16384))", "vector cannot have more than 16383 dimensions") + tk.MustGetErrMsg("create table t(embedding VECTOR(16384))", "vector cannot have more than 16383 dimensions") } func TestFixedVector(t *testing.T) { @@ -280,15 +278,15 @@ func TestVectorConversion(t *testing.T) { tk.MustQuery("SELECT CAST('[1,2,3]' AS VECTOR);").Check(testkit.Rows("[1,2,3]")) tk.MustQuery("SELECT CAST('[]' AS VECTOR);").Check(testkit.Rows("[]")) - tk.MustQuery("SELECT CAST('[1,2,3]' AS VECTOR);").Check(testkit.Rows("[1,2,3]")) + tk.MustQuery("SELECT CAST('[1,2,3]' AS VECTOR);").Check(testkit.Rows("[1,2,3]")) tk.MustContainErrMsg("SELECT CAST('[1,2,3]' AS VECTOR);", "Only VECTOR is supported for now") - tk.MustQuery("SELECT CAST('[1,2,3]' AS VECTOR(3));").Check(testkit.Rows("[1,2,3]")) - err := tk.QueryToErr("SELECT CAST('[1,2,3]' AS VECTOR(2));") + tk.MustQuery("SELECT CAST('[1,2,3]' AS VECTOR(3));").Check(testkit.Rows("[1,2,3]")) + err := tk.QueryToErr("SELECT CAST('[1,2,3]' AS VECTOR(2));") require.EqualError(t, err, "vector has 3 dimensions, does not fit VECTOR(2)") - tk.MustQuery("SELECT CAST(VEC_FROM_TEXT('[1,2,3]') AS VECTOR(3));").Check(testkit.Rows("[1,2,3]")) - err = tk.QueryToErr("SELECT CAST(VEC_FROM_TEXT('[1,2,3]') AS VECTOR(2));") + tk.MustQuery("SELECT CAST(VEC_FROM_TEXT('[1,2,3]') AS VECTOR(3));").Check(testkit.Rows("[1,2,3]")) + err = tk.QueryToErr("SELECT CAST(VEC_FROM_TEXT('[1,2,3]') AS VECTOR(2));") require.EqualError(t, err, "vector has 3 dimensions, does not fit VECTOR(2)") // CONVERT @@ -308,15 +306,15 @@ func TestVectorConversion(t *testing.T) { tk.MustQuery("SELECT CONVERT('[1,2,3]', VECTOR);").Check(testkit.Rows("[1,2,3]")) tk.MustQuery("SELECT CONVERT('[]', VECTOR);").Check(testkit.Rows("[]")) - tk.MustQuery("SELECT CONVERT('[1,2,3]', VECTOR);").Check(testkit.Rows("[1,2,3]")) + tk.MustQuery("SELECT CONVERT('[1,2,3]', VECTOR);").Check(testkit.Rows("[1,2,3]")) tk.MustContainErrMsg("SELECT CONVERT('[1,2,3]', VECTOR);", "Only VECTOR is supported for now") - tk.MustQuery("SELECT CONVERT('[1,2,3]', VECTOR(3));").Check(testkit.Rows("[1,2,3]")) - err = tk.QueryToErr("SELECT CONVERT('[1,2,3]', VECTOR(2));") + tk.MustQuery("SELECT CONVERT('[1,2,3]', VECTOR(3));").Check(testkit.Rows("[1,2,3]")) + err = tk.QueryToErr("SELECT CONVERT('[1,2,3]', VECTOR(2));") require.EqualError(t, err, "vector has 3 dimensions, does not fit VECTOR(2)") - tk.MustQuery("SELECT CONVERT(VEC_FROM_TEXT('[1,2,3]'), VECTOR(3));").Check(testkit.Rows("[1,2,3]")) - err = tk.QueryToErr("SELECT CONVERT(VEC_FROM_TEXT('[1,2,3]'), VECTOR(2));") + tk.MustQuery("SELECT CONVERT(VEC_FROM_TEXT('[1,2,3]'), VECTOR(3));").Check(testkit.Rows("[1,2,3]")) + err = tk.QueryToErr("SELECT CONVERT(VEC_FROM_TEXT('[1,2,3]'), VECTOR(2));") require.EqualError(t, err, "vector has 3 dimensions, does not fit VECTOR(2)") } diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go index f530318e3bec5..3ae1780af6ac5 100644 --- a/pkg/parser/parser_test.go +++ b/pkg/parser/parser_test.go @@ -635,11 +635,6 @@ func TestDMLStmt(t *testing.T) { {"CREATE VIEW v AS (TABLE t)", true, "CREATE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `v` AS (TABLE `t`)"}, {"SELECT * FROM t1 WHERE a IN (TABLE t2)", true, "SELECT * FROM `t1` WHERE `a` IN (TABLE `t2`)"}, - // vector type - {"CREATE TABLE foo (v VECTOR)", true, "CREATE TABLE `foo` (`v` VECTOR)"}, - {"CREATE TABLE foo (v VECTOR)", true, "CREATE TABLE `foo` (`v` VECTOR)"}, - {"CREATE TABLE foo (v VECTOR)", false, ""}, - // values statement {"VALUES ROW(1)", true, "VALUES ROW(1)"}, {"VALUES ROW()", true, "VALUES ROW()"}, @@ -7561,8 +7556,8 @@ func TestCompatTypes(t *testing.T) { func TestVector(t *testing.T) { table := []testCase{ - {"CREATE TABLE t (a VECTOR)", true, "CREATE TABLE `t` (`a` VECTOR)"}, - {"CREATE TABLE t (a VECTOR)", true, "CREATE TABLE `t` (`a` VECTOR)"}, + {"CREATE TABLE t (a VECTOR)", true, "CREATE TABLE `t` (`a` VECTOR)"}, + {"CREATE TABLE t (a VECTOR)", true, "CREATE TABLE `t` (`a` VECTOR)"}, {"CREATE TABLE t (a VECTOR)", false, ""}, {"CREATE TABLE t (a VECTOR)", false, ""}, {"CREATE TABLE t (a VECTOR)", false, ""}, diff --git a/pkg/parser/types/etc.go b/pkg/parser/types/etc.go index 493833823ab1e..74c89ac0d1185 100644 --- a/pkg/parser/types/etc.go +++ b/pkg/parser/types/etc.go @@ -56,7 +56,7 @@ var type2Str = map[byte]string{ mysql.TypeEnum: "enum", mysql.TypeFloat: "float", mysql.TypeGeometry: "geometry", - mysql.TypeTiDBVectorFloat32: "vector", + mysql.TypeTiDBVectorFloat32: "vector", mysql.TypeInt24: "mediumint", mysql.TypeJSON: "json", mysql.TypeLong: "int", @@ -77,34 +77,34 @@ var type2Str = map[byte]string{ } var str2Type = map[string]byte{ - "bit": mysql.TypeBit, - "text": mysql.TypeBlob, - "date": mysql.TypeDate, - "datetime": mysql.TypeDatetime, - "unspecified": mysql.TypeUnspecified, - "decimal": mysql.TypeNewDecimal, - "double": mysql.TypeDouble, - "enum": mysql.TypeEnum, - "float": mysql.TypeFloat, - "geometry": mysql.TypeGeometry, - "vector": mysql.TypeTiDBVectorFloat32, - "mediumint": mysql.TypeInt24, - "json": mysql.TypeJSON, - "int": mysql.TypeLong, - "bigint": mysql.TypeLonglong, - "longtext": mysql.TypeLongBlob, - "mediumtext": mysql.TypeMediumBlob, - "null": mysql.TypeNull, - "set": mysql.TypeSet, - "smallint": mysql.TypeShort, - "char": mysql.TypeString, - "time": mysql.TypeDuration, - "timestamp": mysql.TypeTimestamp, - "tinyint": mysql.TypeTiny, - "tinytext": mysql.TypeTinyBlob, - "varchar": mysql.TypeVarchar, - "var_string": mysql.TypeVarString, - "year": mysql.TypeYear, + "bit": mysql.TypeBit, + "text": mysql.TypeBlob, + "date": mysql.TypeDate, + "datetime": mysql.TypeDatetime, + "unspecified": mysql.TypeUnspecified, + "decimal": mysql.TypeNewDecimal, + "double": mysql.TypeDouble, + "enum": mysql.TypeEnum, + "float": mysql.TypeFloat, + "geometry": mysql.TypeGeometry, + "vector": mysql.TypeTiDBVectorFloat32, + "mediumint": mysql.TypeInt24, + "json": mysql.TypeJSON, + "int": mysql.TypeLong, + "bigint": mysql.TypeLonglong, + "longtext": mysql.TypeLongBlob, + "mediumtext": mysql.TypeMediumBlob, + "null": mysql.TypeNull, + "set": mysql.TypeSet, + "smallint": mysql.TypeShort, + "char": mysql.TypeString, + "time": mysql.TypeDuration, + "timestamp": mysql.TypeTimestamp, + "tinyint": mysql.TypeTiny, + "tinytext": mysql.TypeTinyBlob, + "varchar": mysql.TypeVarchar, + "var_string": mysql.TypeVarString, + "year": mysql.TypeYear, } // TypeStr converts tp to a string. diff --git a/pkg/parser/types/field_type.go b/pkg/parser/types/field_type.go index 839e0ae0871ac..da66f2b1b76bf 100644 --- a/pkg/parser/types/field_type.go +++ b/pkg/parser/types/field_type.go @@ -588,7 +588,7 @@ func (ft *FieldType) RestoreAsCastType(ctx *format.RestoreCtx, explicitCharset b case mysql.TypeYear: ctx.WriteKeyWord("YEAR") case mysql.TypeTiDBVectorFloat32: - ctx.WriteKeyWord("VECTOR") + ctx.WriteKeyWord("VECTOR") } if ft.array { ctx.WritePlain(" ") diff --git a/pkg/types/etc.go b/pkg/types/etc.go index 25e30e70bcc3f..baaa736a47d92 100644 --- a/pkg/types/etc.go +++ b/pkg/types/etc.go @@ -162,7 +162,7 @@ var kind2Str = map[byte]string{ KindMaxValue: "max_value", KindRaw: "raw", KindMysqlJSON: "json", - KindVectorFloat32: "vector", + KindVectorFloat32: "vector", } // TypeStr converts tp to a string.