From e53591038a34e1afde0207e15333848147bbad4b Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Tue, 3 Jan 2023 16:19:57 +0800 Subject: [PATCH 1/3] fix data race in cast as array Signed-off-by: xiongjiwei --- expression/builtin_cast.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/expression/builtin_cast.go b/expression/builtin_cast.go index 545abd497a2da..49d8f2c2b29f0 100644 --- a/expression/builtin_cast.go +++ b/expression/builtin_cast.go @@ -458,6 +458,8 @@ func (b *castJSONAsArrayFunctionSig) Clone() builtinFunc { return newSig } +var fakeSctx = &stmtctx.StatementContext{} + func (b *castJSONAsArrayFunctionSig) evalJSON(row chunk.Row) (res types.BinaryJSON, isNull bool, err error) { val, isNull, err := b.args[0].EvalJSON(b.ctx, row) if isNull || err != nil { @@ -474,20 +476,8 @@ func (b *castJSONAsArrayFunctionSig) evalJSON(row chunk.Row) (res types.BinaryJS if f == nil { return types.BinaryJSON{}, false, ErrNotSupportedYet.GenWithStackByArgs("CAS-ing JSON to the target type") } - sc := b.ctx.GetSessionVars().StmtCtx - originalOverflowAsWarning := sc.OverflowAsWarning - originIgnoreTruncate := sc.IgnoreTruncate - originTruncateAsWarning := sc.TruncateAsWarning - sc.OverflowAsWarning = false - sc.IgnoreTruncate = false - sc.TruncateAsWarning = false - defer func() { - sc.OverflowAsWarning = originalOverflowAsWarning - sc.IgnoreTruncate = originIgnoreTruncate - sc.TruncateAsWarning = originTruncateAsWarning - }() for i := 0; i < val.GetElemCount(); i++ { - item, err := f(sc, val.ArrayGetElem(i), ft) + item, err := f(fakeSctx, val.ArrayGetElem(i), ft) if err != nil { return types.BinaryJSON{}, false, err } From 88b2bbfb7094f32769eb24df3b2b560eb3248c93 Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Tue, 3 Jan 2023 16:53:10 +0800 Subject: [PATCH 2/3] fix test Signed-off-by: xiongjiwei --- expression/builtin_cast.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/builtin_cast.go b/expression/builtin_cast.go index 49d8f2c2b29f0..158684cb5edb8 100644 --- a/expression/builtin_cast.go +++ b/expression/builtin_cast.go @@ -458,7 +458,7 @@ func (b *castJSONAsArrayFunctionSig) Clone() builtinFunc { return newSig } -var fakeSctx = &stmtctx.StatementContext{} +var fakeSctx = &stmtctx.StatementContext{InInsertStmt: true} func (b *castJSONAsArrayFunctionSig) evalJSON(row chunk.Row) (res types.BinaryJSON, isNull bool, err error) { val, isNull, err := b.args[0].EvalJSON(b.ctx, row) From 840d0c424f4498fa6bac0c4fe9fb3edafbe6a7ab Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Tue, 3 Jan 2023 17:39:03 +0800 Subject: [PATCH 3/3] add some comment Signed-off-by: xiongjiwei --- expression/builtin_cast.go | 1 + 1 file changed, 1 insertion(+) diff --git a/expression/builtin_cast.go b/expression/builtin_cast.go index 158684cb5edb8..b6fc16cda2ef4 100644 --- a/expression/builtin_cast.go +++ b/expression/builtin_cast.go @@ -458,6 +458,7 @@ func (b *castJSONAsArrayFunctionSig) Clone() builtinFunc { return newSig } +// fakeSctx is used to ignore the sql mode, `cast as array` should always return error if any. var fakeSctx = &stmtctx.StatementContext{InInsertStmt: true} func (b *castJSONAsArrayFunctionSig) evalJSON(row chunk.Row) (res types.BinaryJSON, isNull bool, err error) {