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

pkg/errctx, *: use errctx to handle TruncateError #48970

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions br/pkg/lightning/backend/kv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_library(
"//br/pkg/logutil",
"//br/pkg/redact",
"//br/pkg/utils",
"//pkg/errctx",
"//pkg/expression",
"//pkg/kv",
"//pkg/meta/autoid",
Expand Down
6 changes: 5 additions & 1 deletion br/pkg/lightning/backend/kv/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/pingcap/tidb/br/pkg/lightning/manual"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/pkg/errctx"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/sessionctx"
Expand Down Expand Up @@ -291,10 +292,13 @@ func NewSession(options *encode.SessionOptions, logger log.Logger) *Session {
vars.SQLMode = sqlMode

typeFlags := vars.StmtCtx.TypeFlags().
WithTruncateAsWarning(!sqlMode.HasStrictMode()).
WithIgnoreInvalidDateErr(sqlMode.HasAllowInvalidDatesMode()).
WithIgnoreZeroInDate(!sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode())
vars.StmtCtx.SetTypeFlags(typeFlags)

if !sqlMode.HasStrictMode() {
vars.StmtCtx.SetErrGroupLevel(errctx.ErrGroupTruncate, errctx.LevelWarn)
}
if options.SysVars != nil {
for k, v := range options.SysVars {
// since 6.3(current master) tidb checks whether we can set a system variable
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/lightning/backend/tidb/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func (enc *tidbEncoder) appendSQL(sb *strings.Builder, datum *types.Datum, _ *ta

case types.KindMysqlBit:
var buffer [20]byte
intValue, err := datum.GetBinaryLiteral().ToInt(types.DefaultStmtNoWarningContext)
intValue, err := datum.GetBinaryLiteral().ToInt()
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/ddl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ go_library(
"//pkg/disttask/operator",
"//pkg/domain/infosync",
"//pkg/domain/resourcegroup",
"//pkg/errctx",
"//pkg/expression",
"//pkg/infoschema",
"//pkg/kv",
Expand Down
6 changes: 5 additions & 1 deletion pkg/ddl/backfilling_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/tidb/pkg/ddl/copr"
"github.com/pingcap/tidb/pkg/ddl/ingest"
sess "github.com/pingcap/tidb/pkg/ddl/internal/session"
"github.com/pingcap/tidb/pkg/errctx"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser/model"
Expand Down Expand Up @@ -169,12 +170,15 @@ func initSessCtx(
sessCtx.GetSessionVars().StmtCtx.DividedByZeroAsWarning = !sqlMode.HasStrictMode()

typeFlags := types.StrictFlags.
WithTruncateAsWarning(!sqlMode.HasStrictMode()).
WithIgnoreInvalidDateErr(sqlMode.HasAllowInvalidDatesMode()).
WithIgnoreZeroInDate(!sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode()).
WithCastTimeToYearThroughConcat(true)
sessCtx.GetSessionVars().StmtCtx.SetTypeFlags(typeFlags)

if !sqlMode.HasStrictMode() {
sessCtx.GetSessionVars().StmtCtx.SetErrGroupLevel(errctx.ErrGroupTruncate, errctx.LevelWarn)
}

sessCtx.GetSessionVars().ResourceGroupName = resGroupName

// Prevent initializing the mock context in the workers concurrently.
Expand Down
23 changes: 15 additions & 8 deletions pkg/ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/pingcap/tidb/pkg/ddl/resourcegroup"
ddlutil "github.com/pingcap/tidb/pkg/ddl/util"
rg "github.com/pingcap/tidb/pkg/domain/resourcegroup"
"github.com/pingcap/tidb/pkg/errctx"
"github.com/pingcap/tidb/pkg/expression"
"github.com/pingcap/tidb/pkg/infoschema"
"github.com/pingcap/tidb/pkg/kv"
Expand Down Expand Up @@ -1353,7 +1354,8 @@ func getDefaultValue(ctx sessionctx.Context, col *table.Column, option *ast.Colu
return str, false, err
}
// For other kind of fields (e.g. INT), we supply its integer as string value.
value, err := v.GetBinaryLiteral().ToInt(ctx.GetSessionVars().StmtCtx.TypeCtx())
value, err := v.GetBinaryLiteral().ToInt()
err = ctx.GetSessionVars().StmtCtx.HandleError(err)
if err != nil {
return nil, false, err
}
Expand All @@ -1368,7 +1370,9 @@ func getDefaultValue(ctx sessionctx.Context, col *table.Column, option *ast.Colu
val, err := getEnumDefaultValue(v, col)
return val, false, err
case mysql.TypeDuration, mysql.TypeDate:
if v, err = v.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &col.FieldType); err != nil {
v, err = v.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &col.FieldType)
err = ctx.GetSessionVars().StmtCtx.HandleError(err)
if err != nil {
return "", false, errors.Trace(err)
}
case mysql.TypeBit:
Expand All @@ -1380,7 +1384,9 @@ func getDefaultValue(ctx sessionctx.Context, col *table.Column, option *ast.Colu
// For these types, convert it to standard format firstly.
// like integer fields, convert it into integer string literals. like convert "1.25" into "1" and "2.8" into "3".
// if raise a error, we will use original expression. We will handle it in check phase
if temp, err := v.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &col.FieldType); err == nil {
temp, err := v.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &col.FieldType)
err = ctx.GetSessionVars().StmtCtx.HandleError(err)
if err == nil {
v = temp
}
}
Expand Down Expand Up @@ -5628,12 +5634,12 @@ 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)
sc := sctx.GetSessionVars().StmtCtx
errCtx := sc.ErrCtx()
oldTruncateLevel := errCtx.GetLevel(errctx.ErrGroupTruncate)
sc.SetErrGroupLevel(errctx.ErrGroupTruncate, errctx.LevelError)
_, err = buildPartitionDefinitionsInfo(sctx, pAst.Definitions, &newTblInfo, uint64(len(newTblInfo.Partition.Definitions)))
sv.SetTypeFlags(oldTypeFlags)
sc.SetErrGroupLevel(errctx.ErrGroupTruncate, oldTruncateLevel)
if err != nil {
return nil, dbterror.ErrUnsupportedModifyColumn.GenWithStack("New column does not match partition definitions: %s", err.Error())
}
Expand Down Expand Up @@ -7873,6 +7879,7 @@ func checkAndGetColumnsTypeAndValuesMatch(ctx sessionctx.Context, colTypes []typ
}
}
newVal, err := val.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &colType)
// Don't need to handle errors here. Truncated values are not allowed even if the SQL_MODE is set to non-strict.
if err != nil {
return nil, dbterror.ErrWrongTypeColumnValue.GenWithStackByArgs()
}
Expand Down
21 changes: 17 additions & 4 deletions pkg/ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,12 +778,16 @@ func comparePartitionAstAndModel(ctx sessionctx.Context, pAst *ast.PartitionOpti
return dbterror.ErrGeneralUnsupportedDDL.GenWithStackByArgs("INTERVAL partitioning: number of partitions generated != partition defined (%d != %d)", len(a), len(m))
}

sc := ctx.GetSessionVars().StmtCtx

evalFn := func(expr ast.ExprNode) (types.Datum, error) {
val, err := expression.EvalAstExpr(ctx, ast.NewValueExpr(expr, "", ""))
if err != nil || partCol == nil {
return val, err
}
return val.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &partCol.FieldType)
result, err := val.ConvertTo(sc.TypeCtx(), &partCol.FieldType)
err = sc.HandleError(err)
return result, err
}
for i := range pAst.Definitions {
// Allow options to differ! (like Placement Rules)
Expand Down Expand Up @@ -816,6 +820,7 @@ func comparePartitionAstAndModel(ctx sessionctx.Context, pAst *ast.PartitionOpti
return err
}
cmp, err := lessThanVal.Compare(ctx.GetSessionVars().StmtCtx.TypeCtx(), &generatedExprVal, collate.GetBinaryCollator())
err = sc.HandleError(err)
if err != nil {
return err
}
Expand Down Expand Up @@ -1069,7 +1074,9 @@ func GeneratePartDefsFromInterval(ctx sessionctx.Context, tp ast.AlterTableType,
return err
}
if partCol != nil {
lastVal, err = lastVal.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &partCol.FieldType)
sc := ctx.GetSessionVars().StmtCtx
lastVal, err = lastVal.ConvertTo(sc.TypeCtx(), &partCol.FieldType)
err = sc.HandleError(err)
if err != nil {
return err
}
Expand Down Expand Up @@ -1117,13 +1124,17 @@ func GeneratePartDefsFromInterval(ctx sessionctx.Context, tp ast.AlterTableType,
if err != nil {
return err
}

sc := ctx.GetSessionVars().StmtCtx
if partCol != nil {
currVal, err = currVal.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &partCol.FieldType)
currVal, err = currVal.ConvertTo(sc.TypeCtx(), &partCol.FieldType)
err = sc.HandleError(err)
if err != nil {
return err
}
}
cmp, err := currVal.Compare(ctx.GetSessionVars().StmtCtx.TypeCtx(), &lastVal, collate.GetBinaryCollator())
err = sc.HandleError(err)
if err != nil {
return err
}
Expand Down Expand Up @@ -1453,7 +1464,9 @@ func checkPartitionValuesIsInt(ctx sessionctx.Context, defName interface{}, expr
return dbterror.ErrValuesIsNotIntType.GenWithStackByArgs(defName)
}

_, err = val.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), tp)
sc := ctx.GetSessionVars().StmtCtx
_, err = val.ConvertTo(sc.TypeCtx(), tp)
err = sc.HandleError(err)
if err != nil && !types.ErrOverflow.Equal(err) {
return dbterror.ErrWrongTypeColumnValue.GenWithStackByArgs()
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/errctx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func (ctx *Context) WithErrGroupLevel(eg ErrGroup, l Level) Context {
return newCtx
}

// GetLevel returns a level of the given `ErrGroup`
func (ctx *Context) GetLevel(eg ErrGroup) Level {
return ctx.levelMap[eg]
}

// appendWarning appends the error to warning. If the inner `appendWarningFn` is nil, do nothing.
func (ctx *Context) appendWarning(err error) {
intest.Assert(ctx.appendWarningFn != nil)
Expand Down Expand Up @@ -177,6 +182,4 @@ func init() {
for _, errCode := range truncateErrCodes {
errGroupMap[errCode] = ErrGroupTruncate
}

errGroupMap[errno.ErrDataOutOfRange] = ErrGroupOverflow
}
2 changes: 1 addition & 1 deletion pkg/errctx/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestContext(t *testing.T) {
require.Equal(t, ctx.HandleErrorWithAlias(testInternalErr, testErr, testWarn), testErr)

// set level to "warn"
newCtx := ctx.WithErrGroupLevel(errctx.ErrGroupOverflow, errctx.LevelWarn)
newCtx := ctx.WithErrGroupLevel(errctx.ErrGroupTruncate, errctx.LevelWarn)
// ctx is not affected
require.Equal(t, ctx.HandleErrorWithAlias(testInternalErr, testErr, testWarn), testErr)
// newCtx will handle the error as a warn
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ go_library(
"//pkg/domain",
"//pkg/domain/infosync",
"//pkg/domain/resourcegroup",
"//pkg/errctx",
"//pkg/errno",
"//pkg/executor/aggfuncs",
"//pkg/executor/aggregate",
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/aggfuncs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ go_library(
importpath = "github.com/pingcap/tidb/pkg/executor/aggfuncs",
visibility = ["//visibility:public"],
deps = [
"//pkg/errctx",
"//pkg/expression",
"//pkg/expression/aggregation",
"//pkg/parser/ast",
Expand Down
4 changes: 3 additions & 1 deletion pkg/executor/aggfuncs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,9 @@ func buildLeadLag(ctx sessionctx.Context, aggFuncDesc *aggregation.AggFuncDesc,
if len(aggFuncDesc.Args) == 3 {
defaultExpr = aggFuncDesc.Args[2]
if et, ok := defaultExpr.(*expression.Constant); ok {
res, err1 := et.Value.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), aggFuncDesc.RetTp)
sc := ctx.GetSessionVars().StmtCtx
res, err1 := et.Value.ConvertTo(sc.TypeCtx(), aggFuncDesc.RetTp)
err1 = sc.HandleError(err1)
if err1 == nil {
defaultExpr = &expression.Constant{Value: res, RetType: aggFuncDesc.RetTp}
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/executor/aggfuncs/func_group_concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sync/atomic"
"unsafe"

"github.com/pingcap/tidb/pkg/errctx"
"github.com/pingcap/tidb/pkg/expression"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/util"
Expand Down Expand Up @@ -72,7 +73,9 @@ func (e *baseGroupConcat4String) AppendFinalResult2Chunk(_ sessionctx.Context, p

func (e *baseGroupConcat4String) handleTruncateError(sctx sessionctx.Context) (err error) {
if atomic.CompareAndSwapInt32(e.truncated, 0, 1) {
if !sctx.GetSessionVars().StmtCtx.TypeFlags().TruncateAsWarning() {
errCtx := sctx.GetSessionVars().StmtCtx.ErrCtx()

if errCtx.GetLevel(errctx.ErrGroupTruncate) != errctx.LevelWarn {
return expression.ErrCutValueGroupConcat.GenWithStackByArgs(e.args[0].String())
}
sctx.GetSessionVars().StmtCtx.AppendWarning(expression.ErrCutValueGroupConcat.GenWithStackByArgs(e.args[0].String()))
Expand Down Expand Up @@ -303,8 +306,10 @@ func (h topNRows) Len() int {

func (h topNRows) Less(i, j int) bool {
n := len(h.rows[i].byItems)
sc := h.sctx.GetSessionVars().StmtCtx
for k := 0; k < n; k++ {
ret, err := h.rows[i].byItems[k].Compare(h.sctx.GetSessionVars().StmtCtx.TypeCtx(), h.rows[j].byItems[k], h.collators[k])
ret, err := h.rows[i].byItems[k].Compare(sc.TypeCtx(), h.rows[j].byItems[k], h.collators[k])
err = sc.HandleError(err)
if err != nil {
h.err = err
return false
Expand Down
2 changes: 2 additions & 0 deletions pkg/executor/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func checkHistogram(sc *stmtctx.StatementContext, hg *statistics.Histogram) (boo
for i := 0; i < len(hg.Buckets); i++ {
lower, upper := hg.GetLower(i), hg.GetUpper(i)
cmp, err := upper.Compare(sc.TypeCtx(), lower, collate.GetBinaryCollator())
err = sc.HandleError(err)
if cmp < 0 || err != nil {
return false, err
}
Expand All @@ -46,6 +47,7 @@ func checkHistogram(sc *stmtctx.StatementContext, hg *statistics.Histogram) (boo
}
previousUpper := hg.GetUpper(i - 1)
cmp, err = lower.Compare(sc.TypeCtx(), previousUpper, collate.GetBinaryCollator())
err = sc.HandleError(err)
if cmp <= 0 || err != nil {
return false, err
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/executor/cte.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,11 @@ func (p *cteProducer) computeChunkHash(chk *chunk.Chunk) (sel []int, err error)
}

for i := 0; i < chk.NumCols(); i++ {
if err = codec.HashChunkSelected(p.ctx.GetSessionVars().StmtCtx.TypeCtx(), p.hCtx.hashVals,
err := codec.HashChunkSelected(p.hCtx.hashVals,
chk, p.hCtx.allTypes[i], i, p.hCtx.buf, p.hCtx.hasNull,
hashBitMap, false); err != nil {
hashBitMap, false)
err = p.ctx.GetSessionVars().StmtCtx.HandleError(err)
if err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -657,9 +659,10 @@ func (p *cteProducer) checkHasDup(probeKey uint64,
if err != nil {
return false, err
}
isEqual, err := codec.EqualChunkRow(p.ctx.GetSessionVars().StmtCtx.TypeCtx(),
isEqual, err := codec.EqualChunkRow(
row, p.hCtx.allTypes, p.hCtx.keyColIdx,
matchedRow, p.hCtx.allTypes, p.hCtx.keyColIdx)
err = p.ctx.GetSessionVars().StmtCtx.HandleError(err)
if err != nil {
return false, err
}
Expand Down
Loading
Loading