@@ -1082,6 +1082,48 @@ func (s *testEvaluatorSuite) TestCastFuncSig(c *C) {
1082
1082
c .Assert (iRes , Equals , int64 (0 ))
1083
1083
}
1084
1084
1085
+ func (s * testEvaluatorSuite ) TestCastJSONAsDecimalSig (c * C ) {
1086
+ ctx , sc := s .ctx , s .ctx .GetSessionVars ().StmtCtx
1087
+ originIgnoreTruncate := sc .IgnoreTruncate
1088
+ sc .IgnoreTruncate = true
1089
+ defer func () {
1090
+ sc .IgnoreTruncate = originIgnoreTruncate
1091
+ }()
1092
+
1093
+ col := & Column {RetType : types .NewFieldType (mysql .TypeJSON ), Index : 0 }
1094
+ decFunc := newBaseBuiltinCastFunc (newBaseBuiltinFunc (ctx , []Expression {col }), false )
1095
+ decFunc .tp = types .NewFieldType (mysql .TypeNewDecimal )
1096
+ decFunc .tp .Flen = 60
1097
+ decFunc .tp .Decimal = 2
1098
+ sig := & builtinCastJSONAsDecimalSig {decFunc }
1099
+
1100
+ var tests = []struct {
1101
+ In string
1102
+ Out * types.MyDecimal
1103
+ }{
1104
+ {`{}` , types .NewDecFromStringForTest ("0" )},
1105
+ {`[]` , types .NewDecFromStringForTest ("0" )},
1106
+ {`3` , types .NewDecFromStringForTest ("3" )},
1107
+ {`-3` , types .NewDecFromStringForTest ("-3" )},
1108
+ {`4.5` , types .NewDecFromStringForTest ("4.5" )},
1109
+ {`"1234"` , types .NewDecFromStringForTest ("1234" )},
1110
+ // test truncate
1111
+ {`"1234.1234"` , types .NewDecFromStringForTest ("1234.12" )},
1112
+ {`"1234.4567"` , types .NewDecFromStringForTest ("1234.46" )},
1113
+ // test big decimal
1114
+ {`"1234567890123456789012345678901234567890123456789012345"` , types .NewDecFromStringForTest ("1234567890123456789012345678901234567890123456789012345" )},
1115
+ }
1116
+ for _ , tt := range tests {
1117
+ j , err := json .ParseBinaryFromString (tt .In )
1118
+ c .Assert (err , IsNil )
1119
+ row := chunk .MutRowFromDatums ([]types.Datum {types .NewDatum (j )})
1120
+ res , isNull , err := sig .evalDecimal (row .ToRow ())
1121
+ c .Assert (isNull , Equals , false )
1122
+ c .Assert (err , IsNil )
1123
+ c .Assert (res .Compare (tt .Out ), Equals , 0 )
1124
+ }
1125
+ }
1126
+
1085
1127
// TestWrapWithCastAsTypesClasses tests WrapWithCastAsInt/Real/String/Decimal.
1086
1128
func (s * testEvaluatorSuite ) TestWrapWithCastAsTypesClasses (c * C ) {
1087
1129
ctx := s .ctx
0 commit comments