Skip to content

Commit

Permalink
*: replace JSON with BinaryJSON (pingcap#5460)
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored Dec 21, 2017
1 parent 3c418c5 commit 970e5b5
Show file tree
Hide file tree
Showing 31 changed files with 265 additions and 255 deletions.
4 changes: 2 additions & 2 deletions expression/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (b *baseBuiltinFunc) evalDuration(row types.Row) (types.Duration, bool, err
panic("baseBuiltinFunc.evalDuration() should never be called.")
}

func (b *baseBuiltinFunc) evalJSON(row types.Row) (json.JSON, bool, error) {
func (b *baseBuiltinFunc) evalJSON(row types.Row) (json.BinaryJSON, bool, error) {
panic("baseBuiltinFunc.evalJSON() should never be called.")
}

Expand Down Expand Up @@ -230,7 +230,7 @@ type builtinFunc interface {
// evalDuration evaluates duration representation of builtinFunc by given row.
evalDuration(row types.Row) (val types.Duration, isNull bool, err error)
// evalJSON evaluates JSON representation of builtinFunc by given row.
evalJSON(row types.Row) (val json.JSON, isNull bool, err error)
evalJSON(row types.Row) (val json.BinaryJSON, isNull bool, err error)
// getArgs returns the arguments expressions.
getArgs() []Expression
// equal check if this function equals to another function.
Expand Down
36 changes: 18 additions & 18 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,17 +552,17 @@ type builtinCastIntAsJSONSig struct {
baseBuiltinFunc
}

func (b *builtinCastIntAsJSONSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinCastIntAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
val, isNull, err := b.args[0].EvalInt(row, b.getCtx().GetSessionVars().StmtCtx)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if mysql.HasIsBooleanFlag(b.args[0].GetType().Flag) {
res = json.CreateJSON(val != 0)
res = json.CreateBinary(val != 0)
} else if mysql.HasUnsignedFlag(b.args[0].GetType().Flag) {
res = json.CreateJSON(uint64(val))
res = json.CreateBinary(uint64(val))
} else {
res = json.CreateJSON(val)
res = json.CreateBinary(val)
}
return res, false, nil
}
Expand All @@ -571,42 +571,42 @@ type builtinCastRealAsJSONSig struct {
baseBuiltinFunc
}

func (b *builtinCastRealAsJSONSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinCastRealAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
val, isNull, err := b.args[0].EvalReal(row, b.getCtx().GetSessionVars().StmtCtx)
// FIXME: `select json_type(cast(1111.11 as json))` should return `DECIMAL`, we return `DOUBLE` now.
return json.CreateJSON(val), isNull, errors.Trace(err)
return json.CreateBinary(val), isNull, errors.Trace(err)
}

type builtinCastDecimalAsJSONSig struct {
baseBuiltinFunc
}

func (b *builtinCastDecimalAsJSONSig) evalJSON(row types.Row) (json.JSON, bool, error) {
func (b *builtinCastDecimalAsJSONSig) evalJSON(row types.Row) (json.BinaryJSON, bool, error) {
val, isNull, err := b.args[0].EvalDecimal(row, b.getCtx().GetSessionVars().StmtCtx)
if isNull || err != nil {
return json.JSON{}, true, errors.Trace(err)
return json.BinaryJSON{}, true, errors.Trace(err)
}
// FIXME: `select json_type(cast(1111.11 as json))` should return `DECIMAL`, we return `DOUBLE` now.
f64, err := val.ToFloat64()
if err != nil {
return json.JSON{}, true, errors.Trace(err)
return json.BinaryJSON{}, true, errors.Trace(err)
}
return json.CreateJSON(f64), isNull, errors.Trace(err)
return json.CreateBinary(f64), isNull, errors.Trace(err)
}

type builtinCastStringAsJSONSig struct {
baseBuiltinFunc
}

func (b *builtinCastStringAsJSONSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinCastStringAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
val, isNull, err := b.args[0].EvalString(row, b.getCtx().GetSessionVars().StmtCtx)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if mysql.HasParseToJSONFlag(b.tp.Flag) {
res, err = json.ParseFromString(val)
res, err = json.ParseBinaryFromString(val)
} else {
res = json.CreateJSON(val)
res = json.CreateBinary(val)
}
return res, false, errors.Trace(err)
}
Expand All @@ -615,28 +615,28 @@ type builtinCastDurationAsJSONSig struct {
baseBuiltinFunc
}

func (b *builtinCastDurationAsJSONSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinCastDurationAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDuration(row, b.getCtx().GetSessionVars().StmtCtx)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
val.Fsp = types.MaxFsp
return json.CreateJSON(val.String()), false, nil
return json.CreateBinary(val.String()), false, nil
}

type builtinCastTimeAsJSONSig struct {
baseBuiltinFunc
}

func (b *builtinCastTimeAsJSONSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinCastTimeAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
val, isNull, err := b.args[0].EvalTime(row, b.getCtx().GetSessionVars().StmtCtx)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if val.Type == mysql.TypeDatetime || val.Type == mysql.TypeTimestamp {
val.Fsp = types.MaxFsp
}
return json.CreateJSON(val.String()), false, nil
return json.CreateBinary(val.String()), false, nil
}

type builtinCastRealAsRealSig struct {
Expand Down Expand Up @@ -1211,7 +1211,7 @@ type builtinCastJSONAsJSONSig struct {
baseBuiltinFunc
}

func (b *builtinCastJSONAsJSONSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinCastJSONAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
return b.args[0].EvalJSON(row, b.ctx.GetSessionVars().StmtCtx)
}

Expand Down
6 changes: 3 additions & 3 deletions expression/builtin_cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ var (
Fsp: types.DefaultFsp}

// jsonInt indicates json(3)
jsonInt = types.NewDatum(json.CreateJSON(int64(3)))
jsonInt = types.NewDatum(json.CreateBinary(int64(3)))

// jsonTime indicates "CURRENT_DAY 12:59:59"
jsonTime = types.NewDatum(json.CreateJSON(tm.String()))
jsonTime = types.NewDatum(json.CreateBinary(tm.String()))

// jsonDuration indicates
jsonDuration = types.NewDatum(json.CreateJSON(duration.String()))
jsonDuration = types.NewDatum(json.CreateBinary(duration.String()))
)

func (s *testEvaluatorSuite) TestCastFuncSig(c *C) {
Expand Down
8 changes: 2 additions & 6 deletions expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -1729,10 +1729,7 @@ func (s *builtinNullEQJSONSig) evalInt(row types.Row) (val int64, isNull bool, e
case isNull0 != isNull1:
break
default:
cmpRes, err := json.CompareJSON(arg0, arg1)
if err != nil {
return 0, true, errors.Trace(err)
}
cmpRes := json.CompareBinary(arg0, arg1)
if cmpRes == 0 {
res = 1
}
Expand Down Expand Up @@ -1923,6 +1920,5 @@ func compareJSON(args []Expression, row types.Row, ctx context.Context) (int64,
if isNull1 || err != nil {
return 0, isNull1, errors.Trace(err)
}
res, err := json.CompareJSON(arg0, arg1)
return int64(res), err != nil, errors.Trace(err)
return int64(json.CompareBinary(arg0, arg1)), false, nil
}
2 changes: 1 addition & 1 deletion expression/builtin_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s *testEvaluatorSuite) TestCompare(c *C) {
intVal, uintVal, realVal, stringVal, decimalVal := 1, uint64(1), 1.1, "123", types.NewDecFromFloatForTest(123.123)
timeVal := types.Time{Time: types.FromGoTime(time.Now()), Fsp: 6, Type: mysql.TypeDatetime}
durationVal := types.Duration{Duration: time.Duration(12*time.Hour + 1*time.Minute + 1*time.Second)}
jsonVal := json.CreateJSON("123")
jsonVal := json.CreateBinary("123")
// test cases for generating function signatures.
tests := []struct {
arg0 interface{}
Expand Down
4 changes: 2 additions & 2 deletions expression/builtin_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ type builtinIfJSONSig struct {
baseBuiltinFunc
}

func (b *builtinIfJSONSig) evalJSON(row types.Row) (ret json.JSON, isNull bool, err error) {
func (b *builtinIfJSONSig) evalJSON(row types.Row) (ret json.BinaryJSON, isNull bool, err error) {
sc := b.ctx.GetSessionVars().StmtCtx
arg0, isNull0, err := b.args[0].EvalInt(row, sc)
if err != nil {
Expand Down Expand Up @@ -696,7 +696,7 @@ type builtinIfNullJSONSig struct {
baseBuiltinFunc
}

func (b *builtinIfNullJSONSig) evalJSON(row types.Row) (json.JSON, bool, error) {
func (b *builtinIfNullJSONSig) evalJSON(row types.Row) (json.BinaryJSON, bool, error) {
sc := b.ctx.GetSessionVars().StmtCtx
arg0, isNull, err := b.args[0].EvalJSON(row, sc)
if !isNull {
Expand Down
48 changes: 24 additions & 24 deletions expression/builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *jsonTypeFunctionClass) getFunction(ctx context.Context, args []Expressi
}

func (b *builtinJSONTypeSig) evalString(row types.Row) (res string, isNull bool, err error) {
var j json.JSON
var j json.BinaryJSON
j, isNull, err = b.args[0].EvalJSON(row, b.getCtx().GetSessionVars().StmtCtx)
if isNull || err != nil {
return "", isNull, errors.Trace(err)
Expand Down Expand Up @@ -125,7 +125,7 @@ func (c *jsonExtractFunctionClass) getFunction(ctx context.Context, args []Expre
return sig, nil
}

func (b *builtinJSONExtractSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinJSONExtractSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
sc := b.getCtx().GetSessionVars().StmtCtx
res, isNull, err = b.args[0].EvalJSON(row, sc)
if isNull || err != nil {
Expand Down Expand Up @@ -171,7 +171,7 @@ func (c *jsonUnquoteFunctionClass) getFunction(ctx context.Context, args []Expre
}

func (b *builtinJSONUnquoteSig) evalString(row types.Row) (res string, isNull bool, err error) {
var j json.JSON
var j json.BinaryJSON
j, isNull, err = b.args[0].EvalJSON(row, b.getCtx().GetSessionVars().StmtCtx)
if isNull || err != nil {
return "", isNull, errors.Trace(err)
Expand Down Expand Up @@ -209,7 +209,7 @@ func (c *jsonSetFunctionClass) getFunction(ctx context.Context, args []Expressio
return sig, nil
}

func (b *builtinJSONSetSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinJSONSetSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
sc := b.getCtx().GetSessionVars().StmtCtx
res, isNull, err = jsonModify(b.args, row, json.ModifySet, sc)
return res, isNull, errors.Trace(err)
Expand Down Expand Up @@ -244,7 +244,7 @@ func (c *jsonInsertFunctionClass) getFunction(ctx context.Context, args []Expres
return sig, nil
}

func (b *builtinJSONInsertSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinJSONInsertSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
sc := b.getCtx().GetSessionVars().StmtCtx
res, isNull, err = jsonModify(b.args, row, json.ModifyInsert, sc)
return res, isNull, errors.Trace(err)
Expand Down Expand Up @@ -279,7 +279,7 @@ func (c *jsonReplaceFunctionClass) getFunction(ctx context.Context, args []Expre
return sig, nil
}

func (b *builtinJSONReplaceSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinJSONReplaceSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
sc := b.getCtx().GetSessionVars().StmtCtx
res, isNull, err = jsonModify(b.args, row, json.ModifyReplace, sc)
return res, isNull, errors.Trace(err)
Expand Down Expand Up @@ -308,7 +308,7 @@ func (c *jsonRemoveFunctionClass) getFunction(ctx context.Context, args []Expres
return sig, nil
}

func (b *builtinJSONRemoveSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinJSONRemoveSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
sc := b.getCtx().GetSessionVars().StmtCtx
res, isNull, err = b.args[0].EvalJSON(row, sc)
if isNull || err != nil {
Expand Down Expand Up @@ -357,18 +357,18 @@ func (c *jsonMergeFunctionClass) getFunction(ctx context.Context, args []Express
return sig, nil
}

func (b *builtinJSONMergeSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinJSONMergeSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
sc := b.getCtx().GetSessionVars().StmtCtx
values := make([]json.JSON, 0, len(b.args))
values := make([]json.BinaryJSON, 0, len(b.args))
for _, arg := range b.args {
var value json.JSON
var value json.BinaryJSON
value, isNull, err = arg.EvalJSON(row, sc)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
values = append(values, value)
}
res = values[0].Merge(values[1:])
res = json.MergeBinary(values)
return res, false, nil
}

Expand Down Expand Up @@ -400,15 +400,15 @@ func (c *jsonObjectFunctionClass) getFunction(ctx context.Context, args []Expres
return sig, nil
}

func (b *builtinJSONObjectSig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
func (b *builtinJSONObjectSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
if len(b.args)&1 == 1 {
err = ErrIncorrectParameterCount.GenByArgs(ast.JSONObject)
return res, true, errors.Trace(err)
}
sc := b.getCtx().GetSessionVars().StmtCtx
jsons := make(map[string]json.JSON, len(b.args)>>1)
jsons := make(map[string]interface{}, len(b.args)>>1)
var key string
var value json.JSON
var value json.BinaryJSON
for i, arg := range b.args {
if i&1 == 0 {
key, isNull, err = arg.EvalString(row, sc)
Expand All @@ -425,12 +425,12 @@ func (b *builtinJSONObjectSig) evalJSON(row types.Row) (res json.JSON, isNull bo
return res, true, errors.Trace(err)
}
if isNull {
value = json.CreateJSON(nil)
value = json.CreateBinary(nil)
}
jsons[key] = value
}
}
return json.CreateJSON(jsons), false, nil
return json.CreateBinary(jsons), false, nil
}

type jsonArrayFunctionClass struct {
Expand Down Expand Up @@ -458,22 +458,22 @@ func (c *jsonArrayFunctionClass) getFunction(ctx context.Context, args []Express
return sig, nil
}

func (b *builtinJSONArraySig) evalJSON(row types.Row) (res json.JSON, isNull bool, err error) {
jsons := make([]json.JSON, 0, len(b.args))
func (b *builtinJSONArraySig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
jsons := make([]interface{}, 0, len(b.args))
for _, arg := range b.args {
j, isNull, err := arg.EvalJSON(row, b.getCtx().GetSessionVars().StmtCtx)
if err != nil {
return res, true, errors.Trace(err)
}
if isNull {
j = json.CreateJSON(nil)
j = json.CreateBinary(nil)
}
jsons = append(jsons, j)
}
return json.CreateJSON(jsons), false, nil
return json.CreateBinary(jsons), false, nil
}

func jsonModify(args []Expression, row types.Row, mt json.ModifyType, sc *stmtctx.StatementContext) (res json.JSON, isNull bool, err error) {
func jsonModify(args []Expression, row types.Row, mt json.ModifyType, sc *stmtctx.StatementContext) (res json.BinaryJSON, isNull bool, err error) {
res, isNull, err = args[0].EvalJSON(row, sc)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
Expand All @@ -493,15 +493,15 @@ func jsonModify(args []Expression, row types.Row, mt json.ModifyType, sc *stmtct
}
pathExprs = append(pathExprs, pathExpr)
}
values := make([]json.JSON, 0, (len(args)-1)/2+1)
values := make([]json.BinaryJSON, 0, (len(args)-1)/2+1)
for i := 2; i < len(args); i += 2 {
var value json.JSON
var value json.BinaryJSON
value, isNull, err = args[i].EvalJSON(row, sc)
if err != nil {
return res, true, errors.Trace(err)
}
if isNull {
value = json.CreateJSON(nil)
value = json.CreateBinary(nil)
}
values = append(values, value)
}
Expand Down
Loading

0 comments on commit 970e5b5

Please sign in to comment.