Skip to content

Commit

Permalink
ddl: fix the issue about create columns with wrong default when sql_m…
Browse files Browse the repository at this point in the history
…ode is empty. (#52988) (#53002)

close #51320, close #52972
  • Loading branch information
ti-chi-bot authored Apr 30, 2024
1 parent 1f1544b commit 9930a02
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
35 changes: 24 additions & 11 deletions pkg/ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ func setNoDefaultValueFlag(c *table.Column, hasDefaultValue bool) {
}
}

func checkDefaultValue(ctx sessionctx.Context, c *table.Column, hasDefaultValue bool) error {
func checkDefaultValue(ctx sessionctx.Context, c *table.Column, hasDefaultValue bool) (err error) {
if !hasDefaultValue {
return nil
}
Expand All @@ -1680,7 +1680,10 @@ func checkDefaultValue(ctx sessionctx.Context, c *table.Column, hasDefaultValue
}
return nil
}
if _, err := table.GetColDefaultValue(ctx.GetExprCtx(), c.ToInfo()); err != nil {
handleWithTruncateErr(ctx, func() {
_, err = table.GetColDefaultValue(ctx.GetExprCtx(), c.ToInfo())
})
if err != nil {
return types.ErrInvalidDefault.GenWithStackByArgs(c.Name)
}
return nil
Expand Down Expand Up @@ -5518,10 +5521,23 @@ func checkModifyTypes(origin *types.FieldType, to *types.FieldType, needRewriteC
return errors.Trace(err)
}

// handleWithTruncateErr handles the doFunc with FlagTruncateAsWarning and FlagIgnoreTruncateErr flags, both of which are false.
func handleWithTruncateErr(ctx sessionctx.Context, doFunc func()) {
sv := ctx.GetSessionVars().StmtCtx
oldTypeFlags := sv.TypeFlags()
newTypeFlags := oldTypeFlags.WithTruncateAsWarning(false).WithIgnoreTruncateErr(false)
sv.SetTypeFlags(newTypeFlags)
doFunc()
sv.SetTypeFlags(oldTypeFlags)
}

// SetDefaultValue sets the default value of the column.
func SetDefaultValue(ctx sessionctx.Context, col *table.Column, option *ast.ColumnOption) (bool, error) {
hasDefaultValue := false
value, isSeqExpr, err := getDefaultValue(ctx, col, option)
func SetDefaultValue(ctx sessionctx.Context, col *table.Column, option *ast.ColumnOption) (hasDefaultValue bool, err error) {
var value any
var isSeqExpr bool
handleWithTruncateErr(ctx, func() {
value, isSeqExpr, err = getDefaultValue(ctx, col, option)
})
if err != nil {
return false, errors.Trace(err)
}
Expand Down Expand Up @@ -5928,12 +5944,9 @@ func GetModifiableColumnJob(
return nil, dbterror.ErrUnsupportedModifyColumn.GenWithStack("cannot parse generated PartitionInfo")
}
pAst := at.Specs[0].Partition
sv := sctx.GetSessionVars().StmtCtx
oldTypeFlags := sv.TypeFlags()
newTypeFlags := oldTypeFlags.WithTruncateAsWarning(false).WithIgnoreTruncateErr(false)
sv.SetTypeFlags(newTypeFlags)
_, err = buildPartitionDefinitionsInfo(sctx.GetExprCtx(), pAst.Definitions, &newTblInfo, uint64(len(newTblInfo.Partition.Definitions)))
sv.SetTypeFlags(oldTypeFlags)
handleWithTruncateErr(sctx, func() {
_, err = buildPartitionDefinitionsInfo(sctx.GetExprCtx(), pAst.Definitions, &newTblInfo, uint64(len(newTblInfo.Partition.Definitions)))
})
if err != nil {
return nil, dbterror.ErrUnsupportedModifyColumn.GenWithStack("New column does not match partition definitions: %s", err.Error())
}
Expand Down
19 changes: 19 additions & 0 deletions tests/integrationtest/r/ddl/column.result
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ t2 CREATE TABLE `t2` (
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
UNIQUE KEY `authorIdx` (`authorId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
set @@sql_mode='';
drop table if exists t1;
create table t1(b tinyint default '11111111');
Error 1067 (42000): Invalid default value for 'b'
create table t1(b tinyint default '11abc');
Error 1067 (42000): Invalid default value for 'b'
create table t1(b datetime default '11abc');
Error 1067 (42000): Invalid default value for 'b'
create table t1(b date default '2024-10');
Error 1067 (42000): Invalid default value for 'b'
create table t1(a tinyint, b date default '2024-10-24 12:20');
alter table t1 add column(c tinyint default '11111111');
Error 1067 (42000): Invalid default value for 'c'
alter table t1 add column(c tinyint default '11abc');
Error 1067 (42000): Invalid default value for 'c'
alter table t1 add column(c datetime default '11abc');
Error 1067 (42000): Invalid default value for 'c'
alter table t1 add column d date default '2024-10';
Error 1067 (42000): Invalid default value for 'd'
3 changes: 1 addition & 2 deletions tests/integrationtest/r/ddl/integration.result
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ create table t (d int default '18446744073709551616' );
Error 1067 (42000): Invalid default value for 'd'
set sql_mode='';
create table t (d int default '18446744073709551616' );
Level Code Message
Warning 1690 BIGINT value is out of range in '18446744073709551616'
Error 1067 (42000): Invalid default value for 'd'
set sql_mode=DEFAULT;
drop table if exists t;
create table t(a int not null, b int, primary key(a), unique idx_b(b));
Expand Down
21 changes: 21 additions & 0 deletions tests/integrationtest/t/ddl/column.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ show create table t1;
CREATE TABLE `t2`( `id` INTEGER PRIMARY KEY, `authorId` int(11) AUTO_INCREMENT, UNIQUE KEY `authorIdx` (`authorId`));
show create table t2;

# TestIssue52972
# Test creating columns with error defaults when sql_mode is empty.
set @@sql_mode='';
drop table if exists t1;
--error 1067
create table t1(b tinyint default '11111111');
--error 1067
create table t1(b tinyint default '11abc');
--error 1067
create table t1(b datetime default '11abc');
--error 1067
create table t1(b date default '2024-10');
create table t1(a tinyint, b date default '2024-10-24 12:20');
--error 1067
alter table t1 add column(c tinyint default '11111111');
--error 1067
alter table t1 add column(c tinyint default '11abc');
--error 1067
alter table t1 add column(c datetime default '11abc');
--error 1067
alter table t1 add column d date default '2024-10';
3 changes: 1 addition & 2 deletions tests/integrationtest/t/ddl/integration.test
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ drop table if exists t;
-- error 1067
create table t (d int default '18446744073709551616' );
set sql_mode='';
-- enable_warnings
-- error 1067
create table t (d int default '18446744073709551616' );
-- disable_warnings
set sql_mode=DEFAULT;

# Test alter non-partition table to partition with global index needed.
Expand Down

0 comments on commit 9930a02

Please sign in to comment.