Skip to content

Commit

Permalink
ddl: refactor ddl pkg's warning and note generation (#49581)
Browse files Browse the repository at this point in the history
close #49291
  • Loading branch information
AilinKid authored Dec 19, 2023
1 parent b8fe33a commit 57c26c0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
58 changes: 30 additions & 28 deletions pkg/ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (d *ddl) CreateSchemaWithInfo(
is := d.GetInfoSchemaWithInterceptor(ctx)
_, ok := is.SchemaByName(dbInfo.Name)
if ok {
// since this error may be seen as error, keep it stack info.
err := infoschema.ErrDatabaseExists.GenWithStackByArgs(dbInfo.Name)
switch onExist {
case OnExistIgnore:
Expand Down Expand Up @@ -775,13 +776,13 @@ func buildColumnsAndConstraints(
// No warning for BOOL-like tinyint(1)
if colDef.Tp.GetFlen() != types.UnspecifiedLength && colDef.Tp.GetFlen() != 1 {
ctx.GetSessionVars().StmtCtx.AppendWarning(
dbterror.ErrWarnDeprecatedIntegerDisplayWidth.GenWithStackByArgs(),
dbterror.ErrWarnDeprecatedIntegerDisplayWidth.FastGenByArgs(),
)
}
case mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
if colDef.Tp.GetFlen() != types.UnspecifiedLength {
ctx.GetSessionVars().StmtCtx.AppendWarning(
dbterror.ErrWarnDeprecatedIntegerDisplayWidth.GenWithStackByArgs(),
dbterror.ErrWarnDeprecatedIntegerDisplayWidth.FastGenByArgs(),
)
}
}
Expand All @@ -793,7 +794,7 @@ func buildColumnsAndConstraints(
col.State = model.StatePublic
if mysql.HasZerofillFlag(col.GetFlag()) {
ctx.GetSessionVars().StmtCtx.AppendWarning(
dbterror.ErrWarnDeprecatedZerofill.GenWithStackByArgs(),
dbterror.ErrWarnDeprecatedZerofill.FastGenByArgs(),
)
}
constraints = append(constraints, cts...)
Expand Down Expand Up @@ -1009,7 +1010,7 @@ func checkColumnDefaultValue(ctx sessionctx.Context, col *table.Column, value in
value = `null`
}
sc := ctx.GetSessionVars().StmtCtx
sc.AppendWarning(dbterror.ErrBlobCantHaveDefault.GenWithStackByArgs(col.Name.O))
sc.AppendWarning(dbterror.ErrBlobCantHaveDefault.FastGenByArgs(col.Name.O))
return hasDefaultValue, value, nil
}
// In strict SQL mode or default value is not an empty string.
Expand Down Expand Up @@ -1223,10 +1224,10 @@ func columnDefToCol(ctx sessionctx.Context, offset int, colDef *ast.ColumnDef, o
col.FieldType.SetCollate(v.StrValue)
}
case ast.ColumnOptionFulltext:
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrTableCantHandleFt.GenWithStackByArgs())
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrTableCantHandleFt.FastGenByArgs())
case ast.ColumnOptionCheck:
if !variable.EnableCheckConstraint.Load() {
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("the switch of check constraint is off"))
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackError("the switch of check constraint is off"))
} else {
// Check the column CHECK constraint dependency lazily, after fill all the name.
// Extract column constraint from column option.
Expand Down Expand Up @@ -1922,7 +1923,7 @@ func setTableAutoRandomBits(ctx sessionctx.Context, tbInfo *model.TableInfo, col
return dbterror.ErrInvalidAutoRandom.FastGenByArgs(autoid.AutoRandomIncrementalBitsTooSmall)
}
msg := fmt.Sprintf(autoid.AutoRandomAvailableAllocTimesNote, shardFmt.IncrementalBitsCapacity())
ctx.GetSessionVars().StmtCtx.AppendNote(errors.Errorf(msg))
ctx.GetSessionVars().StmtCtx.AppendNote(errors.NewNoStackError(msg))
}
}
return nil
Expand Down Expand Up @@ -2033,7 +2034,7 @@ func BuildTableInfo(
}

if constr.Tp == ast.ConstraintFulltext {
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrTableCantHandleFt.GenWithStackByArgs())
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrTableCantHandleFt.FastGenByArgs())
continue
}

Expand All @@ -2055,7 +2056,7 @@ func BuildTableInfo(
// check constraint
if constr.Tp == ast.ConstraintCheck {
if !variable.EnableCheckConstraint.Load() {
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("the switch of check constraint is off"))
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackError("the switch of check constraint is off"))
continue
}
// Since column check constraint dependency has been done in columnDefToCol.
Expand Down Expand Up @@ -2938,7 +2939,7 @@ func (d *ddl) FlashbackCluster(ctx sessionctx.Context, flashbackTS uint64) error
}
gap := time.Until(oracle.GetTimeFromTS(nowTS)).Abs()
if gap > 1*time.Second {
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("Gap between local time and PD TSO is %s, please check PD/system time", gap))
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackErrorf("Gap between local time and PD TSO is %s, please check PD/system time", gap))
}
job := &model.Job{
Type: model.ActionFlashbackCluster,
Expand Down Expand Up @@ -3729,7 +3730,7 @@ func (d *ddl) AlterTable(ctx context.Context, sctx sessionctx.Context, stmt *ast
sctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrTableCantHandleFt)
case ast.ConstraintCheck:
if !variable.EnableCheckConstraint.Load() {
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("the switch of check constraint is off"))
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackError("the switch of check constraint is off"))
} else {
err = d.CreateCheckConstraint(sctx, ident, model.NewCIStr(constr.Name), spec.Constraint)
}
Expand Down Expand Up @@ -3830,13 +3831,13 @@ func (d *ddl) AlterTable(ctx context.Context, sctx sessionctx.Context, stmt *ast
err = d.AlterIndexVisibility(sctx, ident, spec.IndexName, spec.Visibility)
case ast.AlterTableAlterCheck:
if !variable.EnableCheckConstraint.Load() {
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("the switch of check constraint is off"))
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackError("the switch of check constraint is off"))
} else {
err = d.AlterCheckConstraint(sctx, ident, model.NewCIStr(spec.Constraint.Name), spec.Constraint.Enforced)
}
case ast.AlterTableDropCheck:
if !variable.EnableCheckConstraint.Load() {
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("the switch of check constraint is off"))
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackError("the switch of check constraint is off"))
} else {
err = d.DropCheckConstraint(sctx, ident, model.NewCIStr(spec.Constraint.Name))
}
Expand Down Expand Up @@ -3917,7 +3918,7 @@ func (d *ddl) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase int6
}
if newBase != newBaseTemp {
ctx.GetSessionVars().StmtCtx.AppendWarning(
fmt.Errorf("Can't reset AUTO_INCREMENT to %d without FORCE option, using %d instead",
errors.NewNoStackErrorf("Can't reset AUTO_INCREMENT to %d without FORCE option, using %d instead",
newBase, newBaseTemp,
))
}
Expand Down Expand Up @@ -4411,7 +4412,7 @@ func (d *ddl) AlterTablePartitioning(ctx sessionctx.Context, ident ast.Ident, sp
err = d.DoDDLJob(ctx, job)
err = d.callHookOnChanged(job, err)
if err == nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("The statistics of new partitions will be outdated after reorganizing partitions. Please use 'ANALYZE TABLE' statement if you want to update it now"))
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackError("The statistics of new partitions will be outdated after reorganizing partitions. Please use 'ANALYZE TABLE' statement if you want to update it now"))
}
return errors.Trace(err)
}
Expand Down Expand Up @@ -4475,7 +4476,7 @@ func (d *ddl) ReorganizePartitions(ctx sessionctx.Context, ident ast.Ident, spec
err = d.DoDDLJob(ctx, job)
err = d.callHookOnChanged(job, err)
if err == nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("The statistics of related partitions will be outdated after reorganizing partitions. Please use 'ANALYZE TABLE' statement if you want to update it now"))
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackError("The statistics of related partitions will be outdated after reorganizing partitions. Please use 'ANALYZE TABLE' statement if you want to update it now"))
}
return errors.Trace(err)
}
Expand Down Expand Up @@ -5049,7 +5050,7 @@ func (d *ddl) ExchangeTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
if err != nil {
return errors.Trace(err)
}
ctx.GetSessionVars().StmtCtx.AppendWarning(fmt.Errorf("after the exchange, please analyze related table of the exchange to update statistics"))
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackError("after the exchange, please analyze related table of the exchange to update statistics"))
err = d.callHookOnChanged(job, err)
return errors.Trace(err)
}
Expand Down Expand Up @@ -5890,7 +5891,7 @@ func (d *ddl) ChangeColumn(ctx context.Context, sctx sessionctx.Context, ident a
job, err := d.getModifiableColumnJob(ctx, sctx, ident, spec.OldColumnName.Name, spec)
if err != nil {
if infoschema.ErrColumnNotExists.Equal(err) && spec.IfExists {
sctx.GetSessionVars().StmtCtx.AppendNote(infoschema.ErrColumnNotExists.GenWithStackByArgs(spec.OldColumnName.Name, ident.Name))
sctx.GetSessionVars().StmtCtx.AppendNote(infoschema.ErrColumnNotExists.FastGenByArgs(spec.OldColumnName.Name, ident.Name))
return nil
}
return errors.Trace(err)
Expand Down Expand Up @@ -5981,7 +5982,7 @@ func (d *ddl) ModifyColumn(ctx context.Context, sctx sessionctx.Context, ident a
job, err := d.getModifiableColumnJob(ctx, sctx, ident, originalColName, spec)
if err != nil {
if infoschema.ErrColumnNotExists.Equal(err) && spec.IfExists {
sctx.GetSessionVars().StmtCtx.AppendNote(infoschema.ErrColumnNotExists.GenWithStackByArgs(originalColName, ident.Name))
sctx.GetSessionVars().StmtCtx.AppendNote(infoschema.ErrColumnNotExists.FastGenByArgs(originalColName, ident.Name))
return nil
}
return errors.Trace(err)
Expand Down Expand Up @@ -6381,7 +6382,7 @@ func (d *ddl) AlterTableAddStatistics(ctx sessionctx.Context, ident ast.Ident, s
return infoschema.ErrColumnNotExists.GenWithStackByArgs(colName.Name, ident.Name)
}
if stats.StatsType == ast.StatsTypeCorrelation && tblInfo.PKIsHandle && mysql.HasPriKeyFlag(col.GetFlag()) {
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("No need to create correlation statistics on the integer primary key column"))
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackError("No need to create correlation statistics on the integer primary key column"))
return nil
}
if _, exist := colIDSet[col.ID]; exist {
Expand Down Expand Up @@ -7735,6 +7736,7 @@ func validateCommentLength(vars *variable.SessionVars, name string, comment *str
if len(*comment) > maxLen {
err := errTooLongComment.GenWithStackByArgs(name, maxLen)
if vars.StrictSQLMode {
// may be treated like an error.
return "", err
}
vars.StmtCtx.AppendWarning(err)
Expand Down Expand Up @@ -8163,7 +8165,7 @@ func (d *ddl) OrderByColumns(ctx sessionctx.Context, ident ast.Ident) error {
return errors.Trace(err)
}
if tb.Meta().GetPkColInfo() != nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("ORDER BY ignored as there is a user-defined clustered index in the table '%s'", ident.Name))
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackErrorf("ORDER BY ignored as there is a user-defined clustered index in the table '%s'", ident.Name))
}
return nil
}
Expand Down Expand Up @@ -8463,7 +8465,7 @@ func handleDatabasePlacement(ctx sessionctx.Context, dbInfo *model.DBInfo) error
if sessVars.PlacementMode == variable.PlacementModeIgnore {
dbInfo.PlacementPolicyRef = nil
sessVars.StmtCtx.AppendNote(
fmt.Errorf("Placement is ignored when TIDB_PLACEMENT_MODE is '%s'", variable.PlacementModeIgnore),
errors.NewNoStackErrorf("Placement is ignored when TIDB_PLACEMENT_MODE is '%s'", variable.PlacementModeIgnore),
)
return nil
}
Expand All @@ -8477,7 +8479,7 @@ func handleTablePlacement(ctx sessionctx.Context, tbInfo *model.TableInfo) error
sessVars := ctx.GetSessionVars()
if sessVars.PlacementMode == variable.PlacementModeIgnore && removeTablePlacement(tbInfo) {
sessVars.StmtCtx.AppendNote(
fmt.Errorf("Placement is ignored when TIDB_PLACEMENT_MODE is '%s'", variable.PlacementModeIgnore),
errors.NewNoStackErrorf("Placement is ignored when TIDB_PLACEMENT_MODE is '%s'", variable.PlacementModeIgnore),
)
return nil
}
Expand All @@ -8504,7 +8506,7 @@ func handlePartitionPlacement(ctx sessionctx.Context, partInfo *model.PartitionI
sessVars := ctx.GetSessionVars()
if sessVars.PlacementMode == variable.PlacementModeIgnore && removePartitionPlacement(partInfo) {
sessVars.StmtCtx.AppendNote(
fmt.Errorf("Placement is ignored when TIDB_PLACEMENT_MODE is '%s'", variable.PlacementModeIgnore),
errors.NewNoStackErrorf("Placement is ignored when TIDB_PLACEMENT_MODE is '%s'", variable.PlacementModeIgnore),
)
return nil
}
Expand All @@ -8524,7 +8526,7 @@ func checkIgnorePlacementDDL(ctx sessionctx.Context) bool {
sessVars := ctx.GetSessionVars()
if sessVars.PlacementMode == variable.PlacementModeIgnore {
sessVars.StmtCtx.AppendNote(
fmt.Errorf("Placement is ignored when TIDB_PLACEMENT_MODE is '%s'", variable.PlacementModeIgnore),
errors.NewNoStackErrorf("Placement is ignored when TIDB_PLACEMENT_MODE is '%s'", variable.PlacementModeIgnore),
)
return true
}
Expand All @@ -8542,7 +8544,7 @@ func (d *ddl) AddResourceGroup(ctx sessionctx.Context, stmt *ast.CreateResourceG

if _, ok := d.GetInfoSchemaWithInterceptor(ctx).ResourceGroupByName(groupName); ok {
if stmt.IfNotExists {
err = infoschema.ErrResourceGroupExists.GenWithStackByArgs(groupName)
err = infoschema.ErrResourceGroupExists.FastGenByArgs(groupName)
ctx.GetSessionVars().StmtCtx.AppendNote(err)
return nil
}
Expand Down Expand Up @@ -8889,9 +8891,9 @@ func checkTooBigFieldLengthAndTryAutoConvert(tp *types.FieldType, colName string
return err
}
if tp.GetCharset() == charset.CharsetBin {
sessVars.StmtCtx.AppendWarning(dbterror.ErrAutoConvert.GenWithStackByArgs(colName, "VARBINARY", "BLOB"))
sessVars.StmtCtx.AppendWarning(dbterror.ErrAutoConvert.FastGenByArgs(colName, "VARBINARY", "BLOB"))
} else {
sessVars.StmtCtx.AppendWarning(dbterror.ErrAutoConvert.GenWithStackByArgs(colName, "VARCHAR", "TEXT"))
sessVars.StmtCtx.AppendWarning(dbterror.ErrAutoConvert.FastGenByArgs(colName, "VARCHAR", "TEXT"))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,19 +534,19 @@ func buildTablePartitionInfo(ctx sessionctx.Context, s *ast.PartitionOptions, tb
}
// Note that linear hash is simply ignored, and creates non-linear hash/key.
if s.Linear {
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.GenWithStack(fmt.Sprintf("LINEAR %s is not supported, using non-linear %s instead", s.Tp.String(), s.Tp.String())))
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.FastGen(fmt.Sprintf("LINEAR %s is not supported, using non-linear %s instead", s.Tp.String(), s.Tp.String())))
}
if s.Tp == model.PartitionTypeHash || len(s.ColumnNames) != 0 {
enable = true
}
}

if !enable {
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.GenWithStack(fmt.Sprintf("Unsupported partition type %v, treat as normal table", s.Tp)))
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.FastGen(fmt.Sprintf("Unsupported partition type %v, treat as normal table", s.Tp)))
return nil
}
if s.Sub != nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.GenWithStack(fmt.Sprintf("Unsupported subpartitioning, only using %v partitioning", s.Tp)))
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.FastGen(fmt.Sprintf("Unsupported subpartitioning, only using %v partitioning", s.Tp)))
}

pi := &model.PartitionInfo{
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/schematracker/dm_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ func (d SchemaTracker) handleModifyColumn(
job, err := ddl.GetModifiableColumnJob(ctx, sctx, nil, ident, originalColName, schema, t, spec)
if err != nil {
if infoschema.ErrColumnNotExists.Equal(err) && spec.IfExists {
sctx.GetSessionVars().StmtCtx.AppendNote(infoschema.ErrColumnNotExists.GenWithStackByArgs(originalColName, ident.Name))
sctx.GetSessionVars().StmtCtx.AppendNote(infoschema.ErrColumnNotExists.FastGenByArgs(originalColName, ident.Name))
return nil
}
return errors.Trace(err)
Expand Down

0 comments on commit 57c26c0

Please sign in to comment.