Skip to content
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
131 changes: 131 additions & 0 deletions sql/expression/function/json/json_array_append.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// 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 (
"fmt"
"strings"

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

// JSON_ARRAY_APPEND(json_doc, path, val[, path, val] ...)
//
// JSONArrayAppend Appends values to the end of the indicated arrays within a JSON document and returns the result.
// Returns NULL if any argument is NULL. An error occurs if the json_doc argument is not a valid JSON document or any
// path argument is not a valid path expression or contains a * or ** wildcard. The path-value pairs are evaluated left
// to right. The document produced by evaluating one pair becomes the new value against which the next pair is
// evaluated. If a path selects a scalar or object value, that value is autowrapped within an array and the new value is
// added to that array. Pairs for which the path does not identify any value in the JSON document are ignored.
//
// https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html#function_json-array-append
type JSONArrayAppend struct {
doc sql.Expression
pathVals []sql.Expression
}

func (j JSONArrayAppend) Resolved() bool {
for _, child := range j.Children() {
if child != nil && !child.Resolved() {
return false
}
}
return true
}

func (j JSONArrayAppend) String() string {
children := j.Children()
var parts = make([]string, len(children))

for i, c := range children {
parts[i] = c.String()
}

return fmt.Sprintf("%s(%s)", j.FunctionName(), strings.Join(parts, ","))
}

func (j JSONArrayAppend) Type() sql.Type {
return types.JSON
}

func (j JSONArrayAppend) IsNullable() bool {
for _, arg := range j.pathVals {
if arg.IsNullable() {
return true
}
}
return j.doc.IsNullable()
}

func (j JSONArrayAppend) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
doc, err := getMutableJSONVal(ctx, row, j.doc)
if err != nil || doc == nil {
return nil, err
}

pairs := make([]pathValPair, 0, len(j.pathVals)/2)
for i := 0; i < len(j.pathVals); i += 2 {
argPair, err := buildPathValue(ctx, j.pathVals[i], j.pathVals[i+1], row)
if argPair == nil || err != nil {
return nil, err
}
pairs = append(pairs, *argPair)
}

// Apply the path-value pairs to the document.
for _, pair := range pairs {
doc, _, err = doc.ArrayAppend(ctx, pair.path, pair.val)
if err != nil {
return nil, err
}
}

return doc, nil
}

func (j JSONArrayAppend) Children() []sql.Expression {
return append([]sql.Expression{j.doc}, j.pathVals...)
}

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

var _ sql.FunctionExpression = JSONArrayAppend{}

// NewJSONArrayAppend creates a new JSONArrayAppend function.
func NewJSONArrayAppend(args ...sql.Expression) (sql.Expression, error) {
if len(args) <= 1 {
return nil, sql.ErrInvalidArgumentNumber.New("JSON_ARRAY_APPEND", "more than 1", len(args))
} else if (len(args)-1)%2 == 1 {
return nil, sql.ErrInvalidArgumentNumber.New("JSON_ARRAY_APPEND", "even number of path/val", len(args)-1)
}

return JSONArrayAppend{args[0], args[1:]}, nil
}

// FunctionName implements sql.FunctionExpression
func (j JSONArrayAppend) FunctionName() string {
return "json_array_append"
}

// Description implements sql.FunctionExpression
func (j JSONArrayAppend) Description() string {
return "appends data to JSON document."
}
110 changes: 110 additions & 0 deletions sql/expression/function/json/json_array_append_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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 (
"fmt"
"strings"
"testing"

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

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

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

f1 := buildGetFieldExpressions(t, NewJSONArrayAppend, 3)
f2 := buildGetFieldExpressions(t, NewJSONArrayAppend, 5)

json := `{"a": 1, "b": [2, 3], "c": {"d": "foo"}}`

testCases := []struct {
f sql.Expression
row sql.Row
expected interface{}
err error
}{

{f1, sql.Row{json, "$.b[0]", 4.1}, `{"a": 1, "b": [[2,4.1], 3], "c": {"d": "foo"}}`, nil},
{f1, sql.Row{json, "$.a", 4.1}, `{"a": [1, 4.1], "b": [2, 3], "c": {"d": "foo"}}`, nil},
{f1, sql.Row{json, "$.e", "new"}, json, nil},
{f1, sql.Row{json, "$.c.d", "test"}, `{"a": 1, "b": [2, 3], "c": {"d": ["foo", "test"]}}`, nil},
{f2, sql.Row{json, "$.b[0]", 4.1, "$.c.d", "test"}, `{"a": 1, "b": [[2, 4.1], 3], "c": {"d": ["foo", "test"]}}`, nil},
{f1, sql.Row{json, "$.b[5]", 4.1}, json, nil},
{f1, sql.Row{json, "$.b.c", 4}, json, nil},
{f1, sql.Row{json, "$.a[51]", 4.1}, json, nil},
{f1, sql.Row{json, "$.a[last-1]", 4.1}, json, nil},
{f1, sql.Row{json, "$.a[0]", 4.1}, `{"a": [1, 4.1], "b": [2, 3], "c": {"d": "foo"}}`, nil},
{f1, sql.Row{json, "$.a[last]", 4.1}, `{"a": [1, 4.1], "b": [2, 3], "c": {"d": "foo"}}`, nil},
{f1, sql.Row{json, "$[0]", 4.1}, `[{"a": 1, "b": [2, 3], "c": {"d": "foo"}}, 4.1]`, nil},
{f1, sql.Row{json, "$.[0]", 4.1}, nil, fmt.Errorf("Invalid JSON path expression")},
{f1, sql.Row{json, "foo", "test"}, nil, fmt.Errorf("Invalid JSON path expression")},
{f1, sql.Row{json, "$.c.*", "test"}, nil, fmt.Errorf("Path expressions may not contain the * and ** tokens")},
{f1, sql.Row{json, "$.c.**", "test"}, nil, fmt.Errorf("Path expressions may not contain the * and ** tokens")},
{f1, sql.Row{json, "$", 10.1}, `[{"a": 1, "b": [2, 3], "c": {"d": "foo"}}, 10.1]`, nil},
{f1, sql.Row{nil, "$", 42.7}, nil, nil},
{f1, sql.Row{json, nil, 10}, nil, nil},

// mysql> select JSON_ARRAY_APPEND(JSON_ARRAY(1,2,3), "$[1]", 51, "$[1]", 52, "$[1]", 53);
// +--------------------------------------------------------------------------+
// | JSON_ARRAY_APPEND(JSON_ARRAY(1,2,3), "$[1]", 51, "$[1]", 52, "$[1]", 53) |
// +--------------------------------------------------------------------------+
// | [1, [2, 51, 52, 53], 3] |
// +--------------------------------------------------------------------------+
{buildGetFieldExpressions(t, NewJSONArrayAppend, 7),
sql.Row{`[1.0,2.0,3.0]`,
"$[1]", 51.0, // [1, 2, 3] -> [1, [2, 51], 3]
"$[1]", 52.0, // [1, [2, 51], 2, 3] -> [1, [2, 51, 52] 3]
"$[1]", 53.0, // [1, [2, 51, 52], 3] -> [1, [2, 51, 52, 53], 3]
},
`[1,[2, 51, 52, 53], 3]`, nil},
}

for _, tstC := range testCases {
var paths []string
for _, path := range tstC.row[1:] {
if _, ok := path.(string); ok {
paths = append(paths, path.(string))
}
}

t.Run(tstC.f.String()+"."+strings.Join(paths, ","), func(t *testing.T) {
req := require.New(t)
result, err := tstC.f.Eval(sql.NewEmptyContext(), tstC.row)
if tstC.err == nil {
req.NoError(err)

var expect interface{}
if tstC.expected != nil {
expect, _, err = types.JSON.Convert(tstC.expected)
if err != nil {
panic("Bad test string. Can't convert string to JSONDocument: " + tstC.expected.(string))
}
}

req.Equal(expect, result)
} else {
req.Nil(result)
req.Error(tstC.err, err)
}
})
}

}
133 changes: 133 additions & 0 deletions sql/expression/function/json/json_array_insert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// 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 (
"fmt"
"strings"

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

// JSON_ARRAY_INSERT(json_doc, path, val[, path, val] ...)
//
// JSONArrayInsert Updates a JSON document, inserting into an array within the document and returning the modified
// document. Returns NULL if any argument is NULL. An error occurs if the json_doc argument is not a valid JSON document
// or any path argument is not a valid path expression or contains a * or ** wildcard or does not end with an array
// element identifier. The path-value pairs are evaluated left to right. The document produced by evaluating one pair
// becomes the new value against which the next pair is evaluated. Pairs for which the path does not identify any array
// in the JSON document are ignored. If a path identifies an array element, the corresponding value is inserted at that
// element position, shifting any following values to the right. If a path identifies an array position past the end of
// an array, the value is inserted at the end of the array.
//
// https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html#function_json-array-insert
type JSONArrayInsert struct {
doc sql.Expression
pathVals []sql.Expression
}

func (j JSONArrayInsert) Resolved() bool {
for _, child := range j.Children() {
if child != nil && !child.Resolved() {
return false
}
}
return true
}

func (j JSONArrayInsert) String() string {
children := j.Children()
var parts = make([]string, len(children))

for i, c := range children {
parts[i] = c.String()
}

return fmt.Sprintf("%s(%s)", j.FunctionName(), strings.Join(parts, ","))
}

func (j JSONArrayInsert) Type() sql.Type {
return types.JSON
}

func (j JSONArrayInsert) IsNullable() bool {
for _, arg := range j.pathVals {
if arg.IsNullable() {
return true
}
}
return j.doc.IsNullable()
}

func (j JSONArrayInsert) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
doc, err := getMutableJSONVal(ctx, row, j.doc)
if err != nil || doc == nil {
return nil, err
}

pairs := make([]pathValPair, 0, len(j.pathVals)/2)
for i := 0; i < len(j.pathVals); i += 2 {
argPair, err := buildPathValue(ctx, j.pathVals[i], j.pathVals[i+1], row)
if argPair == nil || err != nil {
return nil, err
}
pairs = append(pairs, *argPair)
}

// Apply the path-value pairs to the document.
for _, pair := range pairs {
doc, _, err = doc.ArrayInsert(ctx, pair.path, pair.val)
if err != nil {
return nil, err
}
}

return doc, nil
}

func (j JSONArrayInsert) Children() []sql.Expression {
return append([]sql.Expression{j.doc}, j.pathVals...)
}

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

var _ sql.FunctionExpression = JSONArrayInsert{}

// NewJSONArrayInsert creates a new JSONArrayInsert function.
func NewJSONArrayInsert(args ...sql.Expression) (sql.Expression, error) {
if len(args) <= 1 {
return nil, sql.ErrInvalidArgumentNumber.New("JSON_ARRAY_INSERT", "more than 1", len(args))
} else if (len(args)-1)%2 == 1 {
return nil, sql.ErrInvalidArgumentNumber.New("JSON_ARRAY_INSERT", "even number of path/val", len(args)-1)
}

return JSONArrayInsert{args[0], args[1:]}, nil
}

// FunctionName implements sql.FunctionExpression
func (j JSONArrayInsert) FunctionName() string {
return "json_array_insert"
}

// Description implements sql.FunctionExpression
func (j JSONArrayInsert) Description() string {
return "inserts into JSON array."
}
Loading