Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

sql/expression: relaxed json_extract #682

Merged
merged 1 commit into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion sql/expression/function/json_extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ func (j *JSONExtract) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return nil, err
}

result[i], err = jsonpath.JsonPathLookup(doc, path.(string))
c, err := jsonpath.Compile(path.(string))
if err != nil {
return nil, err
}

result[i], _ = c.Lookup(doc) // err ignored
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's worth it to give some info to the user in this case using Warn:

ctx.Warn(0, err.Error())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that will be noise, because can be very common, since for example in my LOC if the language is not supported will be empty and then a warm

}

if len(result) == 1 {
Expand Down
19 changes: 14 additions & 5 deletions sql/expression/function/json_extract_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package function

import (
"errors"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -46,22 +47,30 @@ func TestJSONExtract(t *testing.T) {
f sql.Expression
row sql.Row
expected interface{}
err error
}{
{f2, sql.Row{json, "$.b.c"}, "foo"},
{f3, sql.Row{json, "$.b.c", "$.b.d"}, []interface{}{"foo", true}},
{f2, sql.Row{json, "FOO"}, nil, errors.New("should start with '$'")},
{f2, sql.Row{nil, "$.b.c"}, nil, nil},
{f2, sql.Row{json, "$.foo"}, nil, nil},
{f2, sql.Row{json, "$.b.c"}, "foo", nil},
{f3, sql.Row{json, "$.b.c", "$.b.d"}, []interface{}{"foo", true}, nil},
{f4, sql.Row{json, "$.b.c", "$.b.d", "$.e[0][*]"}, []interface{}{
"foo",
true,
[]interface{}{1., 2.},
}},
}, nil},
}

for _, tt := range testCases {
t.Run(tt.f.String(), func(t *testing.T) {
require := require.New(t)

result, err := tt.f.Eval(sql.NewEmptyContext(), tt.row)
require.NoError(err)
if tt.err == nil {
require.NoError(err)
} else {
require.Equal(err.Error(), tt.err.Error())
}

require.Equal(tt.expected, result)
})
}
Expand Down