From 57c26c0f9769275baefb9fb12e9c7eea5b5fb543 Mon Sep 17 00:00:00 2001 From: Arenatlx <314806019@qq.com> Date: Tue, 19 Dec 2023 19:22:53 +0800 Subject: [PATCH] ddl: refactor ddl pkg's warning and note generation (#49581) close pingcap/tidb#49291 --- pkg/ddl/ddl_api.go | 58 +++++++++++++++-------------- pkg/ddl/partition.go | 6 +-- pkg/ddl/schematracker/dm_tracker.go | 2 +- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/pkg/ddl/ddl_api.go b/pkg/ddl/ddl_api.go index 0d513fd2c9d6d..66b394a3c1172 100644 --- a/pkg/ddl/ddl_api.go +++ b/pkg/ddl/ddl_api.go @@ -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: @@ -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(), ) } } @@ -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...) @@ -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. @@ -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. @@ -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 @@ -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 } @@ -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. @@ -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, @@ -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) } @@ -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)) } @@ -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, )) } @@ -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) } @@ -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) } @@ -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) } @@ -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) @@ -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) @@ -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 { @@ -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) @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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")) } } } diff --git a/pkg/ddl/partition.go b/pkg/ddl/partition.go index 082c731c38c72..dfd7a27adf2c9 100644 --- a/pkg/ddl/partition.go +++ b/pkg/ddl/partition.go @@ -534,7 +534,7 @@ 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 @@ -542,11 +542,11 @@ func buildTablePartitionInfo(ctx sessionctx.Context, s *ast.PartitionOptions, tb } 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{ diff --git a/pkg/ddl/schematracker/dm_tracker.go b/pkg/ddl/schematracker/dm_tracker.go index 467f35ffcaad8..eb8076c4be0dd 100644 --- a/pkg/ddl/schematracker/dm_tracker.go +++ b/pkg/ddl/schematracker/dm_tracker.go @@ -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)