Skip to content

Commit

Permalink
expression: Fix JSON quote (#53961)
Browse files Browse the repository at this point in the history
close #37294
  • Loading branch information
dveeden authored Jun 13, 2024
1 parent 7b8c91d commit 25d1c83
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
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
12 changes: 12 additions & 0 deletions tests/integrationtest/r/expression/json.result
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,15 @@ 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"
6 changes: 6 additions & 0 deletions tests/integrationtest/t/expression/json.test
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,9 @@ 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");

0 comments on commit 25d1c83

Please sign in to comment.