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

fix insert into enum type with select clause (#1.1-dev) #14921

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions pkg/sql/plan/build_constraint_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/util/eval_expr_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions test/distributed/cases/dtype/enum.result
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'));
Expand Down
42 changes: 40 additions & 2 deletions test/distributed/cases/dtype/enum_1.result
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
31 changes: 30 additions & 1 deletion test/distributed/cases/dtype/enum_1.test
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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;