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

builtin: add json quote #7832

Merged
merged 4 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 29 additions & 1 deletion expression/builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
_ functionClass = &jsonTypeFunctionClass{}
_ functionClass = &jsonExtractFunctionClass{}
_ functionClass = &jsonUnquoteFunctionClass{}
_ functionClass = &jsonQuoteFunctionClass{}
_ functionClass = &jsonSetFunctionClass{}
_ functionClass = &jsonInsertFunctionClass{}
_ functionClass = &jsonReplaceFunctionClass{}
Expand All @@ -50,6 +51,7 @@ var (
_ functionClass = &jsonLengthFunctionClass{}

_ builtinFunc = &builtinJSONTypeSig{}
_ builtinFunc = &builtinJSONQuoteSig{}
_ builtinFunc = &builtinJSONUnquoteSig{}
_ builtinFunc = &builtinJSONArraySig{}
_ builtinFunc = &builtinJSONObjectSig{}
Expand Down Expand Up @@ -735,8 +737,34 @@ type jsonQuoteFunctionClass struct {
baseFunctionClass
}

type builtinJSONQuoteSig struct {
baseBuiltinFunc
}

func (b *builtinJSONQuoteSig) Clone() builtinFunc {
newSig := &builtinJSONQuoteSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

func (c *jsonQuoteFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "JSON_QUOTE")
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETString, types.ETJson)
DisableParseJSONFlag4Expr(args[0])
sig := &builtinJSONQuoteSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_JsonQuoteSig)
return sig, nil
}

func (b *builtinJSONQuoteSig) evalString(row chunk.Row) (res string, isNull bool, err error) {
var j json.BinaryJSON
j, isNull, err = b.args[0].EvalJSON(b.ctx, row)
if isNull || err != nil {
return "", isNull, errors.Trace(err)
}
return j.Quote(), false, nil
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
}

type jsonSearchFunctionClass struct {
Expand Down
29 changes: 29 additions & 0 deletions expression/builtin_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,35 @@ func (s *testEvaluatorSuite) TestJSONType(c *C) {
}
}

func (s *testEvaluatorSuite) TestJSONQuote(c *C) {
defer testleak.AfterTest(c)()
fc := funcs[ast.JSONQuote]
tbl := []struct {
Input interface{}
Expected interface{}
}{
{nil, nil},
{``, `""`},
{`""`, `"\"\""`},
{`a`, `"a"`},
{`3`, `"3"`},
{`{"a": "b"}`, `"{\"a\": \"b\"}"`},
{`{"a": "b"}`, `"{\"a\": \"b\"}"`},
{`hello,"quoted string",world`, `"hello,\"quoted string\",world"`},
{`hello,"宽字符",world`, `"hello,\"宽字符\",world"`},
{`Invalid Json string is OK`, `"Invalid Json string\tis OK"`},
{`1\u2232\u22322`, `"1\\u2232\\u22322"`},
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
}
dtbl := tblToDtbl(tbl)
for _, t := range dtbl {
f, err := fc.getFunction(s.ctx, s.datumsToConstants(t["Input"]))
c.Assert(err, IsNil)
d, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(d, testutil.DatumEquals, t["Expected"][0])
}
}

func (s *testEvaluatorSuite) TestJSONUnquote(c *C) {
defer testleak.AfterTest(c)()
fc := funcs[ast.JSONUnquote]
Expand Down
2 changes: 2 additions & 0 deletions expression/distsql_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ func getSignatureByPB(ctx sessionctx.Context, sigCode tipb.ScalarFuncSig, tp *ti

case tipb.ScalarFuncSig_JsonTypeSig:
f = &builtinJSONTypeSig{base}
case tipb.ScalarFuncSig_JsonQuoteSig:
f = &builtinQuoteSig{base}
case tipb.ScalarFuncSig_JsonUnquoteSig:
f = &builtinJSONUnquoteSig{base}
case tipb.ScalarFuncSig_JsonArraySig:
Expand Down
16 changes: 16 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3333,6 +3333,22 @@ func (s *testIntegrationSuite) TestFuncJSON(c *C) {
r = tk.MustQuery(`select json_unquote('hello'), json_unquote('world')`)
r.Check(testkit.Rows("hello world"))

r = tk.MustQuery(`select
json_quote(''),
json_quote('""'),
json_quote('a'),
json_quote('3'),
json_quote('{"a": "b"}'),
json_quote('{"a": "b"}'),
json_quote('hello,"quoted string",world'),
json_quote('hello,"宽字符",world'),
json_quote('Invalid Json string is OK'),
json_quote('1\u2232\u22322')
`)
r.Check(testkit.Rows(
`"" "\"\"" "a" "3" "{\"a\": \"b\"}" "{\"a\": \"b\"}" "hello,\"quoted string\",world" "hello,\"宽字符\",world" "Invalid Json string\tis OK" "1u2232u22322"`,
))

r = tk.MustQuery(`select json_extract(a, '$.a[1]'), json_extract(b, '$.b') from table_json`)
r.Check(testkit.Rows("\"2\" true", "<nil> <nil>"))

Expand Down
6 changes: 6 additions & 0 deletions types/json/binary_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/hex"
"fmt"
"sort"
"strconv"
"unicode/utf8"
"unsafe"

Expand Down Expand Up @@ -54,6 +55,11 @@ func (bj BinaryJSON) Type() string {
}
}

// Quote is for JSON_QUOTE
func (bj BinaryJSON) Quote() string {
return strconv.Quote(hack.String(bj.GetString()))
}

// Unquote is for JSON_UNQUOTE.
func (bj BinaryJSON) Unquote() (string, error) {
switch bj.TypeCode {
Expand Down