MCPcopy Index your code
hub / github.com/pingcap/tidb / appendJSONArray

Method appendJSONArray

pkg/expression/builtin_json.go:1131–1160  ·  view source on GitHub ↗
(res types.BinaryJSON, p string, v types.BinaryJSON)

Source from the content-addressed store, hash-verified

1129}
1130
1131func (b *builtinJSONArrayAppendSig) appendJSONArray(res types.BinaryJSON, p string, v types.BinaryJSON) (types.BinaryJSON, bool, error) {
1132 // We should do the following checks to get correct values in res.Extract
1133 pathExpr, err := types.ParseJSONPathExpr(p)
1134 if err != nil {
1135 return res, true, err
1136 }
1137 if pathExpr.CouldMatchMultipleValues() {
1138 return res, true, types.ErrInvalidJSONPathMultipleSelection
1139 }
1140
1141 obj, exists := res.Extract([]types.JSONPathExpression{pathExpr})
1142 if !exists {
1143 // If path not exists, just do nothing and no errors.
1144 return res, false, nil
1145 }
1146
1147 if obj.TypeCode != types.JSONTypeCodeArray {
1148 // res.Extract will return a json object instead of an array if there is an object at path pathExpr.
1149 // JSON_ARRAY_APPEND({"a": "b"}, "$", {"b": "c"}) => [{"a": "b"}, {"b", "c"}]
1150 // We should wrap them to a single array first.
1151 obj, err = types.CreateBinaryJSONWithCheck([]any{obj})
1152 if err != nil {
1153 return res, true, err
1154 }
1155 }
1156
1157 obj = types.MergeBinaryJSON([]types.BinaryJSON{obj, v})
1158 res, err = res.Modify([]types.JSONPathExpression{pathExpr}, []types.BinaryJSON{obj}, types.JSONModifySet)
1159 return res, false, err
1160}
1161
1162type jsonArrayInsertFunctionClass struct {
1163 baseFunctionClass

Callers 2

vecEvalJSONMethod · 0.95
evalJSONMethod · 0.95

Calls 6

ParseJSONPathExprFunction · 0.92
MergeBinaryJSONFunction · 0.92
ModifyMethod · 0.80
ExtractMethod · 0.65

Tested by

no test coverage detected