From c018efcf390731ab3ba9061807ba521d1c4a4b78 Mon Sep 17 00:00:00 2001 From: YANGGMM Date: Tue, 12 Mar 2024 16:32:26 +0800 Subject: [PATCH] fix insert into enum type with select clause (#14902) fix insert into enum type with select clause Approved by: @ouyuanning, @nnsgmsone, @heni02 --- pkg/sql/plan/build_constraint_util.go | 13 +++++-- pkg/sql/util/eval_expr_util.go | 2 +- test/distributed/cases/dtype/enum.result | 11 +++--- test/distributed/cases/dtype/enum_1.result | 42 ++++++++++++++++++++-- test/distributed/cases/dtype/enum_1.test | 31 +++++++++++++++- 5 files changed, 88 insertions(+), 11 deletions(-) diff --git a/pkg/sql/plan/build_constraint_util.go b/pkg/sql/plan/build_constraint_util.go index 6b5be76c6543..b1ae065c6719 100644 --- a/pkg/sql/plan/build_constraint_util.go +++ b/pkg/sql/plan/build_constraint_util.go @@ -529,9 +529,16 @@ func initInsertStmt(builder *QueryBuilder, bindCtx *BindContext, stmt *tree.Inse }, }, } - projExpr, err = forceCastExpr(builder.GetContext(), projExpr, tableDef.Cols[colIdx].Typ) - if err != nil { - return false, nil, false, nil, err + if tableDef.Cols[colIdx].Typ.Id == int32(types.T_enum) { + projExpr, err = funcCastForEnumType(builder.GetContext(), projExpr, tableDef.Cols[colIdx].Typ) + if err != nil { + return false, nil, false, nil, err + } + } else { + projExpr, err = forceCastExpr(builder.GetContext(), projExpr, tableDef.Cols[colIdx].Typ) + if err != nil { + return false, nil, false, nil, err + } } insertColToExpr[column] = projExpr } diff --git a/pkg/sql/util/eval_expr_util.go b/pkg/sql/util/eval_expr_util.go index b85cb9541f80..2da9a0a542c6 100644 --- a/pkg/sql/util/eval_expr_util.go +++ b/pkg/sql/util/eval_expr_util.go @@ -285,7 +285,7 @@ func SetInsertValue(proc *process.Process, numVal *tree.NumVal, vec *vector.Vect case types.T_timestamp: return setInsertValueTimeStamp(proc, numVal, vec) case types.T_enum: - return setInsertValueNumber[types.Enum](proc, numVal, vec) + return false, nil } return false, nil diff --git a/test/distributed/cases/dtype/enum.result b/test/distributed/cases/dtype/enum.result index 4e49061f8bd5..2cad2d77c041 100644 --- a/test/distributed/cases/dtype/enum.result +++ b/test/distributed/cases/dtype/enum.result @@ -107,9 +107,12 @@ col1 null create table enum03(col1 enum('数据库','数据库管理','数据库管理软件')); insert into enum03 select * from enum02; -invalid argument operator cast, bad value [VARCHAR ENUM] select * from enum03; col1 +数据库 +数据库管理 +数据库管理软件 +null drop table enum02; drop table enum03; drop table if exists enum04; @@ -128,12 +131,12 @@ create table enum05 (a int,b enum('4','3','2','1')); insert into enum05 values(1,1); select * from enum05; a b -1 4 +1 1 insert into enum05 values(2,'1'); select * from enum05; a b -1 4 -2 1 +1 1 +2 4 drop table enum05; drop table if exists pri01; create table pri01 (col1 enum('qy4iujd3wi4fu4h3f', '323242r34df432432', '32e3ewfdewrew')); diff --git a/test/distributed/cases/dtype/enum_1.result b/test/distributed/cases/dtype/enum_1.result index e0171dbea426..af88bc57acb2 100644 --- a/test/distributed/cases/dtype/enum_1.result +++ b/test/distributed/cases/dtype/enum_1.result @@ -3,8 +3,8 @@ insert into typec values(1,1); insert into typec values(2,'1'); select * from typec; a b -1 4 -2 1 +1 1 +2 4 drop table typec; CREATE TABLE orders ( id INT PRIMARY KEY, @@ -76,3 +76,41 @@ length(status) null null drop table orders; +create table t4 (a enum('abc', 'def')); +insert into t4 values (0); +internal error: convert to MySQL enum failed: number 0 overflow enum boundary [1, 2] +insert into t4 values (1); +insert into t4 values (2); +select * from t4; +a +abc +def +drop table t4; +create table t4 (a enum('abc', 'def')); +create table t5 (col1 int); +create table t6 (col1 char(10)); +insert into t5 values(1); +insert into t5 values(2); +insert into t4 select * from t5; +select * from t4; +a +abc +def +delete from t4; +insert into t6 values ('abc'); +insert into t6 values ('def'); +insert into t4 select * from t6; +select * from t4; +a +abc +def +delete from t4; +insert into t5 values(3); +insert into t4 select * from t5; +internal error: convert to MySQL enum failed: number 3 overflow enum boundary [1, 2] +insert into t6 values('pkg'); +insert into t4 select * from t6; +internal error: convert to MySQL enum failed: item pkg is not in enum [abc def] +drop table t4; +drop table t5; +drop table t6; diff --git a/test/distributed/cases/dtype/enum_1.test b/test/distributed/cases/dtype/enum_1.test index ac165f01bb5b..64108c539a6b 100644 --- a/test/distributed/cases/dtype/enum_1.test +++ b/test/distributed/cases/dtype/enum_1.test @@ -26,4 +26,33 @@ insert into orders values(6,'666','New'); select count(*),status from orders group by status; select substring(status,2,3) from orders; select length(status) from orders; -drop table orders; \ No newline at end of file +drop table orders; + + +create table t4 (a enum('abc', 'def')); +insert into t4 values (0); +insert into t4 values (1); +insert into t4 values (2); +select * from t4; +drop table t4; + +create table t4 (a enum('abc', 'def')); +create table t5 (col1 int); +create table t6 (col1 char(10)); +insert into t5 values(1); +insert into t5 values(2); +insert into t4 select * from t5; +select * from t4; +delete from t4; +insert into t6 values ('abc'); +insert into t6 values ('def'); +insert into t4 select * from t6; +select * from t4; +delete from t4; +insert into t5 values(3); +insert into t4 select * from t5; +insert into t6 values('pkg'); +insert into t4 select * from t6; +drop table t4; +drop table t5; +drop table t6; \ No newline at end of file