Skip to content

Commit a38ca34

Browse files
authored
fix coalesce with JSON types (#2947)
1 parent b02d6d5 commit a38ca34

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

enginetest/queries/json_scripts.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ var JsonScripts = []ScriptTest{
627627
SetUpScript: []string{
628628
"create table t (pk int primary key, col1 JSON, col2 JSON);",
629629
`insert into t values (1, JSON_OBJECT('key1', 1, 'key2', '"abc"'), JSON_ARRAY(3,10,5,17,"z"));`,
630-
`insert into t values (2, JSON_OBJECT('key1', 100, 'key2', '"ghi"'), JSON_ARRAY(3,10,5,17,JSON_ARRAY(22,"y",66)));`,
630+
`insert into t values (2, JSON_OBJECT('key1', 100, 'key2', 'ghi'), JSON_ARRAY(3,10,5,17,JSON_ARRAY(22,"y",66)));`,
631631
`CREATE TABLE t2 (i INT PRIMARY KEY, j JSON);`,
632632
`INSERT INTO t2 VALUES (0, '{"a": "123", "outer": {"inner": 456}}');`,
633633
},
@@ -636,16 +636,23 @@ var JsonScripts = []ScriptTest{
636636
Query: `select col1->'$.key1' from t;`,
637637
Expected: []sql.Row{{types.MustJSON("1")}, {types.MustJSON("100")}},
638638
},
639+
{
640+
Query: `select col1->'$.key2' from t;`,
641+
Expected: []sql.Row{
642+
{types.JSONDocument{Val: "\"abc\""}},
643+
{types.JSONDocument{Val: "ghi"}},
644+
},
645+
},
639646
{
640647
Query: `select col1->>'$.key2' from t;`,
641-
Expected: []sql.Row{{"abc"}, {"ghi"}},
648+
Expected: []sql.Row{{"\"abc\""}, {"ghi"}},
642649
},
643650
{
644651
Query: `select pk, col1 from t where col1->'$.key1' = 1;`,
645652
Expected: []sql.Row{{1, types.MustJSON(`{"key1":1, "key2":"\"abc\""}`)}},
646653
},
647654
{
648-
Query: `select pk, col1 from t where col1->>'$.key2' = 'abc';`,
655+
Query: `select pk, col1 from t where col1->>'$.key2' = '"abc"';`,
649656
Expected: []sql.Row{{1, types.MustJSON(`{"key1":1, "key2":"\"abc\""}`)}},
650657
},
651658
{

enginetest/queries/queries.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5412,6 +5412,12 @@ SELECT * FROM cte WHERE d = 2;`,
54125412
},
54135413
},
54145414
},
5415+
{
5416+
Query: `SELECT COALESCE(CAST('{"a": "one \\n two"}' as json), '');`,
5417+
Expected: []sql.Row{
5418+
{"{\"a\": \"one \\n two\"}"},
5419+
},
5420+
},
54155421
{
54165422
Query: "SELECT concat(s, i) FROM mytable",
54175423
Expected: []sql.Row{

sql/expression/function/coalesce_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ func TestCoalesce(t *testing.T) {
152152
typ: types.Float64,
153153
nullable: false,
154154
},
155+
{
156+
name: "coalesce(json({'a': 'a \n b'}), '')",
157+
input: []sql.Expression{
158+
expression.NewLiteral("{\"a\": \"one \\n two\"}", types.JSON),
159+
expression.NewLiteral("", types.LongText),
160+
},
161+
expected: "{\"a\": \"one \\n two\"}",
162+
typ: types.LongText,
163+
nullable: false,
164+
},
155165
{
156166
name: "coalesce(sysInt, sysInt)",
157167
input: []sql.Expression{

sql/types/json_value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func JsonToMySqlString(jsonWrapper sql.JSONWrapper) (string, error) {
4343
return marshalToMySqlString(val)
4444
}
4545

46-
// JsonToMySqlString generates a byte slice representation of a sql.JSONWrapper that is compatible with MySQL's JSON output, including spaces.
46+
// JsonToMySqlBytes generates a byte slice representation of a sql.JSONWrapper that is compatible with MySQL's JSON output, including spaces.
4747
func JsonToMySqlBytes(jsonWrapper sql.JSONWrapper) ([]byte, error) {
4848
val, err := jsonWrapper.ToInterface()
4949
if err != nil {

sql/types/strings.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/shopspring/decimal"
3030
"gopkg.in/src-d/go-errors.v1"
3131

32-
"github.com/dolthub/go-mysql-server/internal/strings"
3332
"github.com/dolthub/go-mysql-server/sql"
3433
"github.com/dolthub/go-mysql-server/sql/encodings"
3534
)
@@ -416,10 +415,6 @@ func ConvertToBytes(ctx context.Context, v interface{}, t sql.StringType, dest [
416415
if err != nil {
417416
return nil, err
418417
}
419-
val, err = strings.UnquoteBytes(val)
420-
if err != nil {
421-
return nil, err
422-
}
423418
start = 0
424419
case sql.Wrapper[string]:
425420
unwrapped, err := s.Unwrap(ctx)

0 commit comments

Comments
 (0)