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

support json_valid() function #2072

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8981,7 +8981,14 @@ var ErrorQueries = []QueryErrorTest{
Query: "SELECT json_set() FROM dual;",
ExpectedErr: sql.ErrInvalidArgumentNumber,
},

{
Query: `SELECT JSON_VALID()`,
ExpectedErr: sql.ErrInvalidArgumentNumber,
},
{
Query: `SELECT JSON_VALID('{"a": 1}','[1]')`,
ExpectedErr: sql.ErrInvalidArgumentNumber,
},
// This gets an error "unable to cast "second row" of type string to int64"
// Should throw sql.ErrAmbiguousColumnInOrderBy
{
Expand Down
5 changes: 0 additions & 5 deletions sql/expression/function/aggregation/json_agg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ package aggregation
import (
"fmt"

"gopkg.in/src-d/go-errors.v1"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
)

// ErrUnsupportedJSONFunction is returned when a unsupported JSON function is called.
var ErrUnsupportedJSONFunction = errors.NewKind("unsupported JSON function: %s")

// JSON_OBJECTAGG(key, value) [over_clause]
//
// JSONObjectAgg Takes two column names or expressions as arguments, the first of these being used as a key and the
Expand Down
5 changes: 5 additions & 0 deletions sql/expression/function/json/json_array_append.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,8 @@ func (j JSONArrayAppend) FunctionName() string {
func (j JSONArrayAppend) Description() string {
return "appends data to JSON document."
}

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONArrayAppend) IsUnsupported() bool {
return false
}
5 changes: 5 additions & 0 deletions sql/expression/function/json/json_array_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,8 @@ func (j JSONArrayInsert) FunctionName() string {
func (j JSONArrayInsert) Description() string {
return "inserts into JSON array."
}

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONArrayInsert) IsUnsupported() bool {
return false
}
2 changes: 1 addition & 1 deletion sql/expression/function/json/json_contains.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (j *JSONContains) Description() string {

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONContains) IsUnsupported() bool {
return true
return false
}

func (j *JSONContains) Resolved() bool {
Expand Down
5 changes: 5 additions & 0 deletions sql/expression/function/json/json_extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,8 @@ func (j *JSONExtract) String() string {
}
return fmt.Sprintf("json_extract(%s)", strings.Join(parts, ", "))
}

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONExtract) IsUnsupported() bool {
return false
}
5 changes: 5 additions & 0 deletions sql/expression/function/json/json_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,8 @@ func (j JSONInsert) FunctionName() string {
func (j JSONInsert) Description() string {
return "inserts data into JSON document"
}

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONInsert) IsUnsupported() bool {
return false
}
5 changes: 0 additions & 5 deletions sql/expression/function/json/json_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ func (j JSONObject) Description() string {
return "creates JSON object."
}

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONObject) IsUnsupported() bool {
return true
}

func (j JSONObject) Resolved() bool {
for _, child := range j.Children() {
if child != nil && !child.Resolved() {
Expand Down
5 changes: 5 additions & 0 deletions sql/expression/function/json/json_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,8 @@ func (j JSONRemove) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
}
return doc, nil
}

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONRemove) IsUnsupported() bool {
return false
}
5 changes: 5 additions & 0 deletions sql/expression/function/json/json_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,8 @@ func (j JSONReplace) FunctionName() string {
func (j JSONReplace) Description() string {
return "replaces values in JSON document."
}

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONReplace) IsUnsupported() bool {
return false
}
5 changes: 5 additions & 0 deletions sql/expression/function/json/json_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ func (j *JSONSet) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {

return doc, nil
}

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONSet) IsUnsupported() bool {
return false
}
2 changes: 1 addition & 1 deletion sql/expression/function/json/json_unquote.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (js *JSONUnquote) Description() string {

// IsUnsupported implements sql.UnsupportedFunctionStub
func (js *JSONUnquote) IsUnsupported() bool {
return true
return false
}

func (js *JSONUnquote) String() string {
Expand Down
31 changes: 0 additions & 31 deletions sql/expression/function/json/json_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,37 +408,6 @@ func (j JSONType) IsUnsupported() bool {
return true
}

// JSON_VALID(val)
//
// Returns 0 or 1 to indicate whether a value is valid JSON. Returns NULL if the argument is NULL.
//
// https://dev.mysql.com/doc/refman/8.0/en/json-attribute-functions.html#function_json-valid
type JSONValid struct {
sql.Expression
}

var _ sql.FunctionExpression = JSONValid{}

// NewJSONValid creates a new JSONValid function.
func NewJSONValid(args ...sql.Expression) (sql.Expression, error) {
return nil, ErrUnsupportedJSONFunction.New(JSONValid{}.FunctionName())
}

// FunctionName implements sql.FunctionExpression
func (j JSONValid) FunctionName() string {
return "json_valid"
}

// Description implements sql.FunctionExpression
func (j JSONValid) Description() string {
return "returns whether JSON value is valid."
}

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONValid) IsUnsupported() bool {
return true
}

//////////////////////////
// JSON table functions //
//////////////////////////
Expand Down
80 changes: 80 additions & 0 deletions sql/expression/function/json/json_valid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package json

import (
"fmt"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
)

// JSON_VALID(val)
//
// Returns 0 or 1 to indicate whether a value is valid JSON. Returns NULL if the argument is NULL.
//
// https://dev.mysql.com/doc/refman/8.0/en/json-attribute-functions.html#function_json-valid
type JSONValid struct {
JSON sql.Expression
}

var _ sql.FunctionExpression = JSONValid{}

// NewJSONValid creates a new JSONValid function.
func NewJSONValid(args ...sql.Expression) (sql.Expression, error) {
if len(args) != 1 {
return nil, sql.ErrInvalidArgumentNumber.New("JSON_VALID", "1", len(args))
}
return &JSONValid{args[0]}, nil
}

// FunctionName implements sql.FunctionExpression
func (j JSONValid) FunctionName() string {
return "json_valid"
}

// Description implements sql.FunctionExpression
func (j JSONValid) Description() string {
return "returns whether JSON value is valid."
}

// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONValid) IsUnsupported() bool {
return false
}

func (j JSONValid) Resolved() bool {
return j.JSON.Resolved()
}

func (j JSONValid) String() string {
return fmt.Sprintf("%s(%s)", j.FunctionName(), j.JSON.String())
}

func (j JSONValid) Type() sql.Type {
return types.Boolean
}

func (j JSONValid) IsNullable() bool {
return j.JSON.IsNullable()
}

func (j JSONValid) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
doc, err := getJSONDocumentFromRow(ctx, row, j.JSON)
if err != nil {
return false, nil
}
if doc == nil {
return nil, nil
}
return true, nil
}

func (j JSONValid) Children() []sql.Expression {
return []sql.Expression{j.JSON}
}

func (j JSONValid) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(j.Children()) != len(children) {
return nil, fmt.Errorf("json_valid did not receive the correct amount of args")
}

return NewJSONValid(children...)
}
63 changes: 63 additions & 0 deletions sql/expression/function/json/json_valid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2023 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package json

import (
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/src-d/go-errors.v1"

"github.com/dolthub/go-mysql-server/sql"
)

func TestValid(t *testing.T) {
_, err := NewJSONValid()
require.True(t, errors.Is(err, sql.ErrInvalidArgumentNumber))

f1 := buildGetFieldExpressions(t, NewJSONValid, 1)

testCases := []struct {
f sql.Expression
row sql.Row
expected interface{}
}{
{f1, sql.Row{`null`}, true},
{f1, sql.Row{`1`}, true},
{f1, sql.Row{`[1]`}, true},
{f1, sql.Row{`[1, false]`}, true},
{f1, sql.Row{`[1, {"a": 1}]`}, true},
{f1, sql.Row{`{"a": 1}`}, true},
{f1, sql.Row{`{"a": [1, false]}`}, true},
{f1, sql.Row{`{"a": [1, {"a": 1}]}`}, true},
{f1, sql.Row{`{"a": 1, "b": [2, 3], "c": {"d": "foo"}}`}, true},
{f1, sql.Row{nil}, nil},
{f1, sql.Row{1}, false},
{f1, sql.Row{true}, false},
{f1, sql.Row{`incorrect`}, false},
{f1, sql.Row{`{"a": 1"}`}, false},
{f1, sql.Row{`{1}`}, false},
{f1, sql.Row{`[1, "a": 1]`}, false},
jennifersp marked this conversation as resolved.
Show resolved Hide resolved
}

for _, tt := range testCases {
t.Run(tt.f.String(), func(t *testing.T) {
require := require.New(t)
// any error case will result in output of 'false' value
result, _ := tt.f.Eval(sql.NewEmptyContext(), tt.row)
require.Equal(tt.expected, result)
})
}
}