github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_array.go (about) 1 // Copyright 2022 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([val[, val] ...]) 26 // 27 // JSONArray Evaluates a (possibly empty) list of values and returns a JSON array containing those values. 28 // 29 // https://dev.mysql.com/doc/refman/8.0/en/json-creation-functions.html#function_json-array 30 31 type JSONArray struct { 32 vals []sql.Expression 33 } 34 35 var _ sql.FunctionExpression = (*JSONArray)(nil) 36 var _ sql.CollationCoercible = (*JSONArray)(nil) 37 38 // NewJSONArray creates a new JSONArray function. 39 func NewJSONArray(args ...sql.Expression) (sql.Expression, error) { 40 return &JSONArray{vals: args}, nil 41 } 42 43 // FunctionName implements sql.FunctionExpression 44 func (j JSONArray) FunctionName() string { 45 return "json_array" 46 } 47 48 // Description implements sql.FunctionExpression 49 func (j JSONArray) Description() string { 50 return "creates JSON array." 51 } 52 53 // IsUnsupported implements sql.UnsupportedFunctionStub 54 func (j JSONArray) IsUnsupported() bool { 55 return false 56 } 57 58 // Resolved implements the Expression interface. 59 func (j *JSONArray) Resolved() bool { 60 for _, d := range j.vals { 61 if !d.Resolved() { 62 return false 63 } 64 } 65 return true 66 } 67 68 // String implements the Expression interface. 69 func (j *JSONArray) String() string { 70 children := j.Children() 71 var parts = make([]string, len(children)) 72 73 for i, c := range children { 74 parts[i] = c.String() 75 } 76 77 return fmt.Sprintf("%s(%s)", j.FunctionName(), strings.Join(parts, ",")) 78 } 79 80 // Type implements the Expression interface. 81 func (j *JSONArray) Type() sql.Type { 82 return types.JSON 83 } 84 85 // CollationCoercibility implements the interface sql.CollationCoercible. 86 func (JSONArray) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 87 return ctx.GetCharacterSet().BinaryCollation(), 2 88 } 89 90 // IsNullable implements the Expression interface. 91 func (j *JSONArray) IsNullable() bool { 92 for _, d := range j.vals { 93 if d.IsNullable() { 94 return true 95 } 96 } 97 return false 98 } 99 100 // Eval implements the Expression interface. 101 func (j *JSONArray) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 102 if len(j.vals) == 0 { 103 return types.JSONDocument{Val: make([]interface{}, 0)}, nil 104 } 105 106 var resultArray = make([]interface{}, len(j.vals)) 107 108 for i, vs := range j.vals { 109 val, err := vs.Eval(ctx, row) 110 if err != nil { 111 return nil, err 112 } 113 114 switch v := val.(type) { 115 case sql.JSONWrapper: 116 val = v.ToInterface() 117 case []byte: 118 val = string(v) 119 } 120 121 resultArray[i] = val 122 } 123 124 return types.JSONDocument{Val: resultArray}, nil 125 } 126 127 // Children implements the Expression interface. 128 func (j *JSONArray) Children() []sql.Expression { 129 return j.vals 130 } 131 132 // WithChildren implements the Expression interface. 133 func (j *JSONArray) WithChildren(children ...sql.Expression) (sql.Expression, error) { 134 if len(j.Children()) != len(children) { 135 return nil, fmt.Errorf("json_array did not receive the correct amount of args") 136 } 137 138 return NewJSONArray(children...) 139 }