github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_remove.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_REMOVE(json_doc, path[, path] ...)
    26  //
    27  // JSONRemove Removes data from a JSON document and returns the result. Returns NULL if any argument is NULL. An error
    28  // occurs if the json_doc argument is not a valid JSON document or any path argument is not a valid path expression or
    29  // is $ or contains a * or ** wildcard. The path arguments are evaluated left to right. The document produced by
    30  // evaluating one path becomes the new value against which the next path is evaluated. It is not an error if the element
    31  // to be removed does not exist in the document; in that case, the path does not affect the document.
    32  //
    33  // https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html#function_json-remove
    34  type JSONRemove struct {
    35  	doc   sql.Expression
    36  	paths []sql.Expression
    37  }
    38  
    39  func (j JSONRemove) Resolved() bool {
    40  	for _, child := range j.Children() {
    41  		if child != nil && !child.Resolved() {
    42  			return false
    43  		}
    44  	}
    45  	return true
    46  }
    47  
    48  func (j JSONRemove) String() string {
    49  	children := j.Children()
    50  	var parts = make([]string, len(children))
    51  
    52  	for i, c := range children {
    53  		parts[i] = c.String()
    54  	}
    55  
    56  	return fmt.Sprintf("%s(%s)", j.FunctionName(), strings.Join(parts, ","))
    57  }
    58  
    59  func (j JSONRemove) Type() sql.Type {
    60  	return types.JSON
    61  }
    62  
    63  func (j JSONRemove) IsNullable() bool {
    64  	for _, path := range j.paths {
    65  		if path.IsNullable() {
    66  			return true
    67  		}
    68  	}
    69  	return j.doc.IsNullable()
    70  }
    71  
    72  func (j JSONRemove) Children() []sql.Expression {
    73  	return append([]sql.Expression{j.doc}, j.paths...)
    74  }
    75  
    76  func (j JSONRemove) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    77  	if len(j.Children()) != len(children) {
    78  		return nil, fmt.Errorf("json_remove did not receive the correct amount of args")
    79  	}
    80  	return NewJSONRemove(children...)
    81  }
    82  
    83  var _ sql.FunctionExpression = JSONRemove{}
    84  
    85  // NewJSONRemove creates a new JSONRemove function.
    86  func NewJSONRemove(args ...sql.Expression) (sql.Expression, error) {
    87  	if len(args) < 2 {
    88  		return nil, sql.ErrInvalidArgumentNumber.New("JSON_REMOVE", "2 or more", len(args))
    89  	}
    90  
    91  	return JSONRemove{args[0], args[1:]}, nil
    92  }
    93  
    94  // FunctionName implements sql.FunctionExpression
    95  func (j JSONRemove) FunctionName() string {
    96  	return "json_remove"
    97  }
    98  
    99  // Description implements sql.FunctionExpression
   100  func (j JSONRemove) Description() string {
   101  	return "removes data from JSON document."
   102  }
   103  
   104  func (j JSONRemove) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
   105  	doc, err := getMutableJSONVal(ctx, row, j.doc)
   106  	if err != nil || doc == nil {
   107  		return nil, err
   108  	}
   109  
   110  	for _, path := range j.paths {
   111  		path, err := buildPath(ctx, path, row)
   112  		if err != nil {
   113  			return nil, err
   114  		}
   115  		if path == nil {
   116  			return nil, nil
   117  		}
   118  
   119  		doc, _, err = doc.Remove(path.(string))
   120  		if err != nil {
   121  			return nil, err
   122  		}
   123  	}
   124  	return doc, nil
   125  }
   126  
   127  // IsUnsupported implements sql.UnsupportedFunctionStub
   128  func (j JSONRemove) IsUnsupported() bool {
   129  	return false
   130  }