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

expression: Fix JSON quote | tidb-test=pr/2343 #53961

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.28.6 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
Expand Down
10 changes: 8 additions & 2 deletions pkg/expression/builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"context"
goJSON "encoding/json"
"strconv"
"strings"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -1414,7 +1413,14 @@ func (b *builtinJSONQuoteSig) evalString(ctx EvalContext, row chunk.Row) (string
if isNull || err != nil {
return "", isNull, err
}
return strconv.Quote(str), false, nil
buffer := &bytes.Buffer{}
encoder := goJSON.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err = encoder.Encode(str)
if err != nil {
return "", isNull, err
}
return string(bytes.TrimSuffix(buffer.Bytes(), []byte("\n"))), false, nil
}

type jsonSearchFunctionClass struct {
Expand Down
10 changes: 8 additions & 2 deletions pkg/expression/builtin_json_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package expression
import (
"bytes"
goJSON "encoding/json"
"strconv"
"strings"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -477,7 +476,14 @@ func (b *builtinJSONQuoteSig) vecEvalString(ctx EvalContext, input *chunk.Chunk,
result.AppendNull()
continue
}
result.AppendString(strconv.Quote(buf.GetString(i)))
buffer := &bytes.Buffer{}
encoder := goJSON.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err = encoder.Encode(buf.GetString(i))
if err != nil {
return err
}
result.AppendString(string(bytes.TrimSuffix(buffer.Bytes(), []byte("\n"))))
}
return nil
}
Expand Down
39 changes: 39 additions & 0 deletions tests/integrationtest/r/expression/json.result
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,42 @@ JSON_SCHEMA_VALID('{"properties": {"a": {"pattern": "^a"}}}', '{"a": "abc"}')
SELECT JSON_SCHEMA_VALID('{"properties": {"a": {"pattern": "^a"}}}', '{"a": "cba"}');
JSON_SCHEMA_VALID('{"properties": {"a": {"pattern": "^a"}}}', '{"a": "cba"}')
0
SELECT JSON_QUOTE("<html>");
JSON_QUOTE("<html>")
"<html>"
SELECT JSON_QUOTE("&");
JSON_QUOTE("&")
"&"
SELECT JSON_QUOTE(CONVERT(0x10 USING utf8mb4));
JSON_QUOTE(CONVERT(0x10 USING utf8mb4))
"\u0010"
SELECT JSON_QUOTE("O'Neil");
JSON_QUOTE("O'Neil")
"O'Neil"
WITH RECURSIVE nr(n) AS (SELECT 1 n UNION ALL SELECT n+1 FROM nr WHERE n<25) SELECT JSON_QUOTE(CONVERT(UNHEX(n) USING utf8mb4)) FROM nr;
JSON_QUOTE(CONVERT(UNHEX(n) USING utf8mb4))
"\u0001"
"\u0002"
"\u0003"
"\u0004"
"\u0005"
"\u0006"
"\u0007"
"\b"
"\t"
"\u0010"
"\u0011"
"\u0012"
"\u0013"
"\u0014"
"\u0015"
"\u0016"
"\u0017"
"\u0018"
"\u0019"
" "
"!"
"\""
"#"
"$"
"%"
7 changes: 7 additions & 0 deletions tests/integrationtest/t/expression/json.test
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,10 @@ SELECT JSON_SCHEMA_VALID('{"properties": {"a": {"type": "number", "minimum": 5}}
SELECT JSON_SCHEMA_VALID('{"properties": {"a": {"type": "number", "minimum": 5}}}', '{"a": 6}');
SELECT JSON_SCHEMA_VALID('{"properties": {"a": {"pattern": "^a"}}}', '{"a": "abc"}');
SELECT JSON_SCHEMA_VALID('{"properties": {"a": {"pattern": "^a"}}}', '{"a": "cba"}');

# TestJSONQuote
SELECT JSON_QUOTE("<html>");
SELECT JSON_QUOTE("&");
SELECT JSON_QUOTE(CONVERT(0x10 USING utf8mb4));
SELECT JSON_QUOTE("O'Neil");
WITH RECURSIVE nr(n) AS (SELECT 1 n UNION ALL SELECT n+1 FROM nr WHERE n<25) SELECT JSON_QUOTE(CONVERT(UNHEX(n) USING utf8mb4)) FROM nr;