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

executor: add JSON opaque value condition to everywhere #37390

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7435,3 +7435,15 @@ func TestIssue36358(t *testing.T) {
tk.MustQuery("select extract(day_microsecond from cast('2001-01-01 02:03:04.050607' as datetime(6))) from t").Check(testkit.Rows("1020304050607"))
tk.MustQuery("select extract(day_microsecond from c) from t").Check(testkit.Rows("1020304050607"))
}

func TestCastJSONOpaqueValueToNumeric(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustQuery("select cast(cast(b'010101' as json) as signed);").Check(testkit.Rows("0"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect INTEGER value: '\"base64:type253:FQ==\"'"))
tk.MustQuery("select cast(json_extract(json_objectagg('a', b'010101'), '$.a') as signed);").Check(testkit.Rows("0"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect INTEGER value: '\"base64:type253:FQ==\"'"))
tk.MustQuery("select cast(json_extract(json_objectagg('a', b'010101'), '$.a') as double);").Check(testkit.Rows("0"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect FLOAT value: '\"base64:type253:FQ==\"'"))
}
6 changes: 3 additions & 3 deletions types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func ConvertJSONToInt64(sc *stmtctx.StatementContext, j json.BinaryJSON, unsigne
// ConvertJSONToInt casts JSON into int by type.
func ConvertJSONToInt(sc *stmtctx.StatementContext, j json.BinaryJSON, unsigned bool, tp byte) (int64, error) {
switch j.TypeCode {
case json.TypeCodeObject, json.TypeCodeArray:
case json.TypeCodeObject, json.TypeCodeArray, json.TypeCodeOpaque:
return 0, sc.HandleTruncate(ErrTruncatedWrongVal.GenWithStackByArgs("INTEGER", j.String()))
case json.TypeCodeLiteral:
switch j.Value[0] {
Expand Down Expand Up @@ -615,7 +615,7 @@ func ConvertJSONToInt(sc *stmtctx.StatementContext, j json.BinaryJSON, unsigned
// ConvertJSONToFloat casts JSON into float64.
func ConvertJSONToFloat(sc *stmtctx.StatementContext, j json.BinaryJSON) (float64, error) {
switch j.TypeCode {
case json.TypeCodeObject, json.TypeCodeArray:
case json.TypeCodeObject, json.TypeCodeArray, json.TypeCodeOpaque:
return 0, sc.HandleTruncate(ErrTruncatedWrongVal.GenWithStackByArgs("FLOAT", j.String()))
case json.TypeCodeLiteral:
switch j.Value[0] {
Expand Down Expand Up @@ -644,7 +644,7 @@ func ConvertJSONToDecimal(sc *stmtctx.StatementContext, j json.BinaryJSON) (*MyD
var err error = nil
res := new(MyDecimal)
switch j.TypeCode {
case json.TypeCodeObject, json.TypeCodeArray:
case json.TypeCodeObject, json.TypeCodeArray, json.TypeCodeOpaque:
err = ErrTruncatedWrongVal.GenWithStackByArgs("DECIMAL", j.String())
case json.TypeCodeLiteral:
switch j.Value[0] {
Expand Down
3 changes: 3 additions & 0 deletions types/json/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ func (bj BinaryJSON) IsZero() bool {
isZero = false
case TypeCodeObject:
isZero = false
// FIXME: TiDB always casts the json to double BINARY so this function will never be called.
case TypeCodeOpaque:
isZero = false
}
return isZero
}
Expand Down
5 changes: 4 additions & 1 deletion types/json/binary_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func (bm *binaryModifier) rebuildTo(buf []byte) ([]byte, TypeCode) {
}
bj := bm.bj
switch bj.TypeCode {
case TypeCodeLiteral, TypeCodeInt64, TypeCodeUint64, TypeCodeFloat64, TypeCodeString:
case TypeCodeLiteral, TypeCodeInt64, TypeCodeUint64, TypeCodeFloat64, TypeCodeString, TypeCodeOpaque:
return append(buf, bj.Value...), bj.TypeCode
}
docOff := len(buf)
Expand Down Expand Up @@ -1008,6 +1008,9 @@ func PeekBytesAsJSON(b []byte) (n int, err error) {
case TypeCodeLiteral:
n = valTypeSize + 1
return
case TypeCodeOpaque:
xiongjiwei marked this conversation as resolved.
Show resolved Hide resolved
bufLen, lenLen := binary.Uvarint(b[valTypeSize+1:])
return valTypeSize + 1 + int(bufLen) + lenLen, nil
}
err = errors.New("Invalid JSON bytes")
return
Expand Down
4 changes: 4 additions & 0 deletions util/codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,10 @@ func TestCut(t *testing.T) {
types.MakeDatums(json.CreateBinary("abc")),
types.MakeDatums(json.CreateBinary("abc")),
},
{
types.MakeDatums(json.CreateBinary(json.Opaque{TypeCode: mysql.TypeString, Buf: []byte("abc")})),
types.MakeDatums(json.CreateBinary(json.Opaque{TypeCode: mysql.TypeString, Buf: []byte("abc")})),
},
}
sc := &stmtctx.StatementContext{TimeZone: time.Local}
for i, datums := range table {
Expand Down