Skip to content

Commit

Permalink
Merge pull request #756 from fulghum/fulghum/json_object_key_types
Browse files Browse the repository at this point in the history
Adding support for non-string key types in JSON_OBJECT
  • Loading branch information
fulghum authored Jan 25, 2022
2 parents 4bce537 + 1157992 commit ffbf7f4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
35 changes: 31 additions & 4 deletions enginetest/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -6184,6 +6184,37 @@ var QueryTests = []QueryTest{
{3, 30},
},
},
{
Query: `SELECT JSON_OBJECT(1000000, 10);`,
Expected: []sql.Row{
{sql.MustJSON(`{"1000000": 10}`)},
},
},
{
Query: `SELECT JSON_OBJECT(DATE('1981-02-16'), 10);`,
Expected: []sql.Row{
{sql.MustJSON(`{"1981-02-16": 10}`)},
},
},
{
Query: `SELECT JSON_OBJECT(JSON_OBJECT("foo", "bar"), 10);`,
Expected: []sql.Row{
{sql.MustJSON(`{"{\"foo\":\"bar\"}": 10}`)},
},
},
{
Query: `SELECT JSON_OBJECT(true, 10);`,
Expected: []sql.Row{
{sql.MustJSON(`{"true": 10}`)},
},
},
{
Query: `SELECT JSON_OBJECT(10.1, 10);`,
Expected: []sql.Row{
{sql.MustJSON(`{"10.1": 10}`)},
},
},

{
Query: `SELECT JSON_OBJECT("i",i,"s",s) as js FROM mytable;`,
Expected: []sql.Row{
Expand Down Expand Up @@ -7703,10 +7734,6 @@ var errorQueries = []QueryErrorTest{
Query: `SELECT JSON_OBJECT("a","b","c") FROM dual`,
ExpectedErr: sql.ErrInvalidArgumentNumber,
},
{
Query: `SELECT JSON_OBJECT(1, 2) FROM dual`,
ExpectedErr: sql.ErrInvalidType,
},
{
Query: `select JSON_EXTRACT('{"id":"abc"}', '$.id')-1;`,
ExpectedErrStr: `error: 'abc' is not a valid value for 'DOUBLE'`,
Expand Down
7 changes: 4 additions & 3 deletions sql/expression/function/json_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ func (j JSONObject) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return nil, err
}
if i%2 == 0 {
var ok bool
if key, ok = val.(string); !ok {
return nil, sql.ErrInvalidType.New(expr.Type())
val, err := sql.LongText.Convert(val)
if err != nil {
return nil, err
}
key = val.(string)
} else {
if json, ok := val.(sql.JSONValue); ok {
doc, err := json.Unmarshall(ctx)
Expand Down

0 comments on commit ffbf7f4

Please sign in to comment.