-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
json_contains_path.go
172 lines (147 loc) · 5.24 KB
/
json_contains_path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 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_CONTAINS_PATH(json_doc, one_or_all, path[, path] ...)
//
// JSONContainsPath Returns 0 or 1 to indicate whether a JSON document contains data at a given path or paths. Returns
// NULL if any argument is NULL. An error occurs if the json_doc argument is not a valid JSON document, any path
// argument is not a valid path expression, or one_or_all is not 'one' or 'all'. To check for a specific value at a
// path, use JSON_CONTAINS() instead.
//
// The return value is 0 if no specified path exists within the document. Otherwise, the return value depends on the
// one_or_all argument:
// - 'one': 1 if at least one path exists within the document, 0 otherwise.
// - 'all': 1 if all paths exist within the document, 0 otherwise.
//
// https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#function_json-contains-path
//
// Above is the documentation from MySQL's documentation. Minor Nit - the observed behavior for NULL
// paths is that if a NULL path is found before the search can terminate, then NULL is returned.
type JSONContainsPath struct {
doc sql.Expression
all sql.Expression
paths []sql.Expression
}
func (j JSONContainsPath) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
target, err := getSearchableJSONVal(ctx, row, j.doc)
if err != nil || target == nil {
return nil, err
}
oneOrAll, err := j.all.Eval(ctx, row)
if err != nil || oneOrAll == nil {
return nil, err
}
oneOrAll, _, err = types.LongText.Convert(oneOrAll)
if err != nil {
return nil, err
}
if !strings.EqualFold(oneOrAll.(string), "one") && !strings.EqualFold(oneOrAll.(string), "all") {
return nil, fmt.Errorf("The oneOrAll argument to json_contains_path may take these values: 'one' or 'all'")
}
findAllPaths := strings.EqualFold(oneOrAll.(string), "all")
// MySQL Behavior differs from their docs. The docs say that if any path is NULL, the result is NULL. However,
// they only return NULL when they search far enough to find one, so we match that behavior.
for _, path := range j.paths {
path, err := path.Eval(ctx, row)
if err != nil || path == nil {
return nil, err
}
path, _, err = types.LongText.Convert(path)
if err != nil {
return nil, err
}
result, err := types.LookupJSONValue(target, path.(string))
if err != nil {
return nil, err
}
if result == nil && findAllPaths {
return false, nil
}
if result != nil && !findAllPaths {
return true, nil
}
}
// If we got this far, then we had no reason to terminate the search. For all, that means they all matched.
// For one, that means none matched. The result is the value of findAllPaths.
return findAllPaths, nil
}
func (j JSONContainsPath) Resolved() bool {
for _, child := range j.Children() {
if child != nil && !child.Resolved() {
return false
}
}
return true
}
func (j JSONContainsPath) 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 JSONContainsPath) Type() sql.Type {
return types.Boolean
}
func (j JSONContainsPath) IsNullable() bool {
for _, path := range j.paths {
if path.IsNullable() {
return true
}
}
if j.all.IsNullable() {
return true
}
return j.doc.IsNullable()
}
func (j JSONContainsPath) Children() []sql.Expression {
answer := make([]sql.Expression, 0, len(j.paths)+2)
answer = append(answer, j.doc)
answer = append(answer, j.all)
answer = append(answer, j.paths...)
return answer
}
func (j JSONContainsPath) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(j.Children()) != len(children) {
return nil, fmt.Errorf("json_contains_path did not receive the correct amount of args")
}
return NewJSONContainsPath(children...)
}
var _ sql.FunctionExpression = JSONContainsPath{}
// NewJSONContainsPath creates a new JSONContainsPath function.
func NewJSONContainsPath(args ...sql.Expression) (sql.Expression, error) {
if len(args) < 3 {
return nil, sql.ErrInvalidArgumentNumber.New("JSON_CONTAINS_PATH", "3 or more", len(args))
}
return &JSONContainsPath{args[0], args[1], args[2:]}, nil
}
// FunctionName implements sql.FunctionExpression
func (j JSONContainsPath) FunctionName() string {
return "json_contains_path"
}
// Description implements sql.FunctionExpression
func (j JSONContainsPath) Description() string {
return "returns whether JSON document contains any data at path."
}
// IsUnsupported implements sql.UnsupportedFunctionStub
func (j JSONContainsPath) IsUnsupported() bool {
return false
}