github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_array_append.go (about)

     1  // Copyright 2023 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package json
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  	"github.com/dolthub/go-mysql-server/sql/types"
    23  )
    24  
    25  // JSON_ARRAY_APPEND(json_doc, path, val[, path, val] ...)
    26  //
    27  // JSONArrayAppend Appends values to the end of the indicated arrays within a JSON document and returns the result.
    28  // Returns NULL if any argument is NULL. An error occurs if the json_doc argument is not a valid JSON document or any
    29  // path argument is not a valid path expression or contains a * or ** wildcard. The path-value pairs are evaluated left
    30  // to right. The document produced by evaluating one pair becomes the new value against which the next pair is
    31  // evaluated. If a path selects a scalar or object value, that value is autowrapped within an array and the new value is
    32  // added to that array. Pairs for which the path does not identify any value in the JSON document are ignored.
    33  //
    34  // https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html#function_json-array-append
    35  type JSONArrayAppend struct {
    36  	doc      sql.Expression
    37  	pathVals []sql.Expression
    38  }
    39  
    40  func (j JSONArrayAppend) Resolved() bool {
    41  	for _, child := range j.Children() {
    42  		if child != nil && !child.Resolved() {
    43  			return false
    44  		}
    45  	}
    46  	return true
    47  }
    48  
    49  func (j JSONArrayAppend) String() string {
    50  	children := j.Children()
    51  	var parts = make([]string, len(children))
    52  
    53  	for i, c := range children {
    54  		parts[i] = c.String()
    55  	}
    56  
    57  	return fmt.Sprintf("%s(%s)", j.FunctionName(), strings.Join(parts, ","))
    58  }
    59  
    60  func (j JSONArrayAppend) Type() sql.Type {
    61  	return types.JSON
    62  }
    63  
    64  func (j JSONArrayAppend) IsNullable() bool {
    65  	for _, arg := range j.pathVals {
    66  		if arg.IsNullable() {
    67  			return true
    68  		}
    69  	}
    70  	return j.doc.IsNullable()
    71  }
    72  
    73  func (j JSONArrayAppend) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    74  	doc, err := getMutableJSONVal(ctx, row, j.doc)
    75  	if err != nil || doc == nil {
    76  		return nil, err
    77  	}
    78  
    79  	pairs := make([]pathValPair, 0, len(j.pathVals)/2)
    80  	for i := 0; i < len(j.pathVals); i += 2 {
    81  		argPair, err := buildPathValue(ctx, j.pathVals[i], j.pathVals[i+1], row)
    82  		if argPair == nil || err != nil {
    83  			return nil, err
    84  		}
    85  		pairs = append(pairs, *argPair)
    86  	}
    87  
    88  	// Apply the path-value pairs to the document.
    89  	for _, pair := range pairs {
    90  		doc, _, err = doc.ArrayAppend(pair.path, pair.val)
    91  		if err != nil {
    92  			return nil, err
    93  		}
    94  	}
    95  
    96  	return doc, nil
    97  }
    98  
    99  func (j JSONArrayAppend) Children() []sql.Expression {
   100  	return append([]sql.Expression{j.doc}, j.pathVals...)
   101  }
   102  
   103  func (j JSONArrayAppend) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   104  	if len(j.Children()) != len(children) {
   105  		return nil, fmt.Errorf("json_array_append did not receive the correct number of args")
   106  	}
   107  	return NewJSONArrayAppend(children...)
   108  }
   109  
   110  var _ sql.FunctionExpression = JSONArrayAppend{}
   111  
   112  // NewJSONArrayAppend creates a new JSONArrayAppend function.
   113  func NewJSONArrayAppend(args ...sql.Expression) (sql.Expression, error) {
   114  	if len(args) <= 1 {
   115  		return nil, sql.ErrInvalidArgumentNumber.New("JSON_ARRAY_APPEND", "more than 1", len(args))
   116  	} else if (len(args)-1)%2 == 1 {
   117  		return nil, sql.ErrInvalidArgumentNumber.New("JSON_ARRAY_APPEND", "even number of path/val", len(args)-1)
   118  	}
   119  
   120  	return JSONArrayAppend{args[0], args[1:]}, nil
   121  }
   122  
   123  // FunctionName implements sql.FunctionExpression
   124  func (j JSONArrayAppend) FunctionName() string {
   125  	return "json_array_append"
   126  }
   127  
   128  // Description implements sql.FunctionExpression
   129  func (j JSONArrayAppend) Description() string {
   130  	return "appends data to JSON document."
   131  }
   132  
   133  // IsUnsupported implements sql.UnsupportedFunctionStub
   134  func (j JSONArrayAppend) IsUnsupported() bool {
   135  	return false
   136  }