Skip to content

Commit e316107

Browse files
authored
ddl: fix a bug that create table with vector index and gbk charset column (#56585) (#56637)
close #56549
1 parent 5e933ba commit e316107

File tree

7 files changed

+27
-13
lines changed

7 files changed

+27
-13
lines changed

pkg/ddl/create_table.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func buildTableInfoWithCheck(ctx *metabuild.Context, store kv.Storage, s *ast.Cr
410410
if err = checkTableInfoValidWithStmt(ctx, tbInfo, s); err != nil {
411411
return nil, err
412412
}
413-
if err = checkTableInfoValidExtra(ctx.GetExprCtx().GetEvalCtx().ErrCtx(), store, tbInfo); err != nil {
413+
if err = checkTableInfoValidExtra(ctx.GetExprCtx().GetEvalCtx().ErrCtx(), store, s.Table.Schema, tbInfo); err != nil {
414414
return nil, err
415415
}
416416
return tbInfo, nil
@@ -511,7 +511,7 @@ func checkGeneratedColumn(ctx *metabuild.Context, schemaName pmodel.CIStr, table
511511
return nil
512512
}
513513

514-
func checkVectorIndexIfNeedTiFlashReplica(store kv.Storage, tblInfo *model.TableInfo) error {
514+
func checkVectorIndexIfNeedTiFlashReplica(store kv.Storage, dbName pmodel.CIStr, tblInfo *model.TableInfo) error {
515515
var hasVectorIndex bool
516516
for _, idx := range tblInfo.Indices {
517517
if idx.VectorInfo != nil {
@@ -525,6 +525,9 @@ func checkVectorIndexIfNeedTiFlashReplica(store kv.Storage, tblInfo *model.Table
525525
if store == nil {
526526
return errors.New("the store is nil")
527527
}
528+
if err := isTableTiFlashSupported(dbName, tblInfo); err != nil {
529+
return errors.Trace(err)
530+
}
528531

529532
if tblInfo.TiFlashReplica == nil || tblInfo.TiFlashReplica.Count == 0 {
530533
replicas, err := infoschema.GetTiFlashStoreCount(store)
@@ -551,7 +554,7 @@ func checkVectorIndexIfNeedTiFlashReplica(store kv.Storage, tblInfo *model.Table
551554
// name length and column count.
552555
// (checkTableInfoValid is also used in repairing objects which don't perform
553556
// these checks. Perhaps the two functions should be merged together regardless?)
554-
func checkTableInfoValidExtra(ec errctx.Context, store kv.Storage, tbInfo *model.TableInfo) error {
557+
func checkTableInfoValidExtra(ec errctx.Context, store kv.Storage, dbName pmodel.CIStr, tbInfo *model.TableInfo) error {
555558
if err := checkTooLongTable(tbInfo.Name); err != nil {
556559
return err
557560
}
@@ -574,7 +577,7 @@ func checkTableInfoValidExtra(ec errctx.Context, store kv.Storage, tbInfo *model
574577
if err := checkGlobalIndexes(ec, tbInfo); err != nil {
575578
return errors.Trace(err)
576579
}
577-
if err := checkVectorIndexIfNeedTiFlashReplica(store, tbInfo); err != nil {
580+
if err := checkVectorIndexIfNeedTiFlashReplica(store, dbName, tbInfo); err != nil {
578581
return errors.Trace(err)
579582
}
580583

pkg/ddl/executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ func (e *executor) createTableWithInfoJob(
10891089
}
10901090
}
10911091

1092-
if err := checkTableInfoValidExtra(ctx.GetSessionVars().StmtCtx.ErrCtx(), ctx.GetStore(), tbInfo); err != nil {
1092+
if err := checkTableInfoValidExtra(ctx.GetSessionVars().StmtCtx.ErrCtx(), ctx.GetStore(), dbName, tbInfo); err != nil {
10931093
return nil, err
10941094
}
10951095

@@ -3795,7 +3795,7 @@ func isTableTiFlashSupported(dbName pmodel.CIStr, tbl *model.TableInfo) error {
37953795
if util.IsMemOrSysDB(dbName.L) {
37963796
return errors.Trace(dbterror.ErrUnsupportedTiFlashOperationForSysOrMemTable)
37973797
} else if tbl.TempTableType != model.TempTableNone {
3798-
return dbterror.ErrOptOnTemporaryTable.GenWithStackByArgs("set on tiflash")
3798+
return dbterror.ErrOptOnTemporaryTable.GenWithStackByArgs("set TiFlash replica")
37993799
} else if tbl.IsView() || tbl.IsSequence() {
38003800
return dbterror.ErrWrongObject.GenWithStackByArgs(dbName, tbl.Name, "BASE TABLE")
38013801
}

pkg/ddl/index_modify_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,10 @@ func TestCreateTableWithVectorIndex(t *testing.T) {
11051105
require.Equal(t, model.DistanceMetricCosine, indexes[0].VectorInfo.DistanceMetric)
11061106
require.Equal(t, "vector_index", tbl.Meta().Indices[0].Name.O)
11071107
require.Equal(t, "vector_index_2", tbl.Meta().Indices[1].Name.O)
1108+
tk.MustExec("insert into t values (1, '[1,2.1,3.3]');")
1109+
tk.MustQuery("select * from t;").Check(testkit.Rows("1 [1,2.1,3.3]"))
1110+
tk.MustExec("create view v as select * from t;")
1111+
tk.MustQuery("select * from v;").Check(testkit.Rows("1 [1,2.1,3.3]"))
11081112
tk.MustExec(`DROP TABLE t`)
11091113
}
11101114

@@ -1124,13 +1128,20 @@ func TestCreateTableWithVectorIndex(t *testing.T) {
11241128

11251129
// test unsupported table types
11261130
tk.MustContainErrMsg("create temporary table t(a int, b vector(3), vector index((VEC_COSINE_DISTANCE(b))) USING HNSW)",
1127-
"`vector index` is unsupported on temporary tables.")
1131+
"`set TiFlash replica` is unsupported on temporary tables.")
11281132
// global and local temporary table using different way to handle, so we have two test cases.
11291133
tk.MustContainErrMsg("create global temporary table t(a int, b vector(3), vector index((VEC_COSINE_DISTANCE(b))) USING HNSW) on commit delete rows;",
1130-
"`vector index` is unsupported on temporary tables.")
1134+
"`set TiFlash replica` is unsupported on temporary tables.")
11311135
tk.MustContainErrMsg("create table pt(id bigint, b vector(3), vector index((VEC_COSINE_DISTANCE(b))) USING HNSW) "+
11321136
"partition by range(id) (partition p0 values less than (20), partition p1 values less than (100));",
11331137
"Unsupported add vector index: unsupported partition table")
1138+
tk.MustContainErrMsg("create table t(a int, b vector(3), c char(210) CHARACTER SET gbk COLLATE gbk_bin, vector index((VEC_COSINE_DISTANCE(b))));",
1139+
"Unsupported `set TiFlash replica` settings for table contains gbk charset")
1140+
tk.MustContainErrMsg("create table mysql.t(a int, b vector(3), vector index((VEC_COSINE_DISTANCE(b))));",
1141+
"Unsupported `set TiFlash replica` settings for system table and memory table")
1142+
tk.MustContainErrMsg("create table information_schema.t(a int, b vector(3), vector index((VEC_COSINE_DISTANCE(b))));",
1143+
"Unsupported `set TiFlash replica` settings for system table and memory table")
1144+
11341145
// a vector index with invisible
11351146
tk.MustContainErrMsg("create table t(a int, b vector(3), vector index((VEC_COSINE_DISTANCE(b))) USING HNSW INVISIBLE)",
11361147
"Unsupported set vector index invisible")

pkg/ddl/tests/tiflash/ddl_tiflash_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ func TestAlterDatabaseBasic(t *testing.T) {
816816
tk.MustQuery(`show warnings;`).Sort().Check(testkit.Rows(
817817
"Note 1347 'tiflash_ddl_skip.t_seq' is not BASE TABLE",
818818
"Note 1347 'tiflash_ddl_skip.t_view' is not BASE TABLE",
819-
"Note 8006 `set on tiflash` is unsupported on temporary tables."))
819+
"Note 8006 `set TiFlash replica` is unsupported on temporary tables."))
820820
}
821821

822822
func execWithTimeout(t *testing.T, tk *testkit.TestKit, to time.Duration, sql string) (bool, error) {

pkg/ddl/tiflash_replica_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func TestSetTableFlashReplicaForSystemTable(t *testing.T) {
246246
if tbl.Meta().View != nil {
247247
require.ErrorIs(t, err, dbterror.ErrWrongObject)
248248
} else {
249-
require.Equal(t, "[ddl:8200]Unsupported ALTER TiFlash settings for system table and memory table", err.Error())
249+
require.Equal(t, "[ddl:8200]Unsupported `set TiFlash replica` settings for system table and memory table", err.Error())
250250
}
251251
} else {
252252
require.Equal(t, fmt.Sprintf("[planner:1142]ALTER command denied to user 'root'@'%%' for table '%s'", strings.ToLower(one)), err.Error())

pkg/executor/test/tiflashtest/tiflash_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestNonsupportCharsetTable(t *testing.T) {
7171
tk.MustExec("create table t(a int, b char(10) charset gbk collate gbk_bin)")
7272
err := tk.ExecToErr("alter table t set tiflash replica 1")
7373
require.Error(t, err)
74-
require.Equal(t, "[ddl:8200]Unsupported ALTER TiFlash settings for tables not supported by TiFlash: table contains gbk charset", err.Error())
74+
require.Equal(t, "[ddl:8200]Unsupported `set TiFlash replica` settings for table contains gbk charset", err.Error())
7575

7676
tk.MustExec("drop table if exists t")
7777
tk.MustExec("create table t(a char(10) charset utf8)")

pkg/util/dbterror/ddl_terror.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,9 @@ var (
419419
// ErrAlterTiFlashModeForTableWithoutTiFlashReplica returns when set tiflash mode on table whose tiflash_replica is null or tiflash_replica_count = 0
420420
ErrAlterTiFlashModeForTableWithoutTiFlashReplica = ClassDDL.NewStdErr(0, parser_mysql.Message("TiFlash mode will take effect after at least one TiFlash replica is set for the table", nil))
421421
// ErrUnsupportedTiFlashOperationForSysOrMemTable means we don't support the alter tiflash related action(e.g. set tiflash mode, set tiflash replica) for system table.
422-
ErrUnsupportedTiFlashOperationForSysOrMemTable = ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message(fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUnsupportedDDLOperation].Raw, "ALTER TiFlash settings for system table and memory table"), nil))
422+
ErrUnsupportedTiFlashOperationForSysOrMemTable = ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message(fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUnsupportedDDLOperation].Raw, "`set TiFlash replica` settings for system table and memory table"), nil))
423423
// ErrUnsupportedTiFlashOperationForUnsupportedCharsetTable is used when alter alter tiflash related action(e.g. set tiflash mode, set tiflash replica) with unsupported charset.
424-
ErrUnsupportedTiFlashOperationForUnsupportedCharsetTable = ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message(fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUnsupportedDDLOperation].Raw, "ALTER TiFlash settings for tables not supported by TiFlash: table contains %s charset"), nil))
424+
ErrUnsupportedTiFlashOperationForUnsupportedCharsetTable = ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message(fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUnsupportedDDLOperation].Raw, "`set TiFlash replica` settings for table contains %s charset"), nil))
425425
// ErrTiFlashBackfillIndex is the error that tiflash backfill the index failed.
426426
ErrTiFlashBackfillIndex = ClassDDL.NewStdErr(mysql.ErrTiFlashBackfillIndex,
427427
parser_mysql.Message(mysql.MySQLErrName[mysql.ErrTiFlashBackfillIndex].Raw, nil))

0 commit comments

Comments
 (0)