-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
json_overlaps.go
213 lines (190 loc) · 5.79 KB
/
json_overlaps.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2024 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"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
)
// JSONOverlaps (json_doc1, json_doc2)
//
// JSONOverlaps Compares two JSON documents. Returns true (1) if the two document have any key-value pairs or array
// elements in common. If both arguments are scalars, the function performs a simple equality test.
//
// This function serves as counterpart to JSON_CONTAINS(), which requires all elements of the array searched for to be
// present in the array searched in. Thus, JSON_CONTAINS() performs an AND operation on search keys, while
// JSON_OVERLAPS() performs an OR operation.
//
// Queries on JSON columns of InnoDB tables using JSON_OVERLAPS() in the WHERE clause can be optimized using
// multi-valued indexes. Multi-Valued Indexes, provides detailed information and examples.
//
// https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#function_json-overlaps
type JSONOverlaps struct {
Left sql.Expression
Right sql.Expression
}
var _ sql.FunctionExpression = &JSONOverlaps{}
// NewJSONOverlaps creates a new JSONOverlaps function.
func NewJSONOverlaps(args ...sql.Expression) (sql.Expression, error) {
if len(args) != 2 {
return nil, sql.ErrInvalidArgumentNumber.New("JSON_OVERLAPS", "2", len(args))
}
return &JSONOverlaps{Left: args[0], Right: args[1]}, nil
}
// FunctionName implements sql.FunctionExpression
func (j *JSONOverlaps) FunctionName() string {
return "json_overlaps"
}
// Description implements sql.FunctionExpression
func (j *JSONOverlaps) Description() string {
return "compares two JSON documents, returns TRUE (1) if these have any key-value pairs or array elements in common, otherwise FALSE (0)."
}
// Resolved implements sql.Expression
func (j *JSONOverlaps) Resolved() bool {
return j.Left.Resolved() && j.Right.Resolved()
}
// String implements sql.Expression
func (j *JSONOverlaps) String() string {
return fmt.Sprintf("%s(%s, %s)", j.FunctionName(), j.Left.String(), j.Right.String())
}
// Type implements sql.Expression
func (j *JSONOverlaps) Type() sql.Type {
return types.Boolean
}
// IsNullable implements sql.Expression
func (j *JSONOverlaps) IsNullable() bool {
return j.Left.IsNullable() || j.Right.IsNullable()
}
// jsonEquals compares two JSON values.
// It returns true if the two values are exactly equal (type and order are important).
// It will recursively unwrap arrays and objects to compare their contents.
func jsonEquals(left, right interface{}) bool {
lArr, lIsArr := left.([]interface{})
rArr, rIsArr := right.([]interface{})
if lIsArr && rIsArr {
if len(lArr) != len(rArr) {
return false
}
for i := range lArr {
if !jsonEquals(lArr[i], rArr[i]) {
return false
}
}
return true
}
lMap, lIsMap := left.(map[string]interface{})
rMap, rIsMap := right.(map[string]interface{})
if lIsMap && rIsMap {
if len(lMap) != len(rMap) {
return false
}
for k := range lMap {
if _, ok := rMap[k]; !ok {
return false
}
if !jsonEquals(lMap[k], rMap[k]) {
return false
}
}
return true
}
if (lIsArr != rIsArr) || (lIsMap != rIsMap) {
return false
}
return left == right
}
func overlaps(left, right interface{}) bool {
switch lVal := left.(type) {
case nil, bool, string, float64:
switch rVal := right.(type) {
case nil, bool, string, float64, map[string]interface{}:
return jsonEquals(left, right)
case []interface{}:
// scalar must be in array
for _, r := range rVal {
if jsonEquals(left, r) {
return true
}
}
}
case map[string]interface{}:
switch rVal := right.(type) {
case nil, bool, string, float64:
return overlaps(right, left)
case map[string]interface{}:
// objects must have at least one key-value pair in common
for k := range lVal {
if _, ok := rVal[k]; !ok {
continue
}
if jsonEquals(lVal[k], rVal[k]) {
return true
}
}
case []interface{}:
// object must be in array
for _, r := range rVal {
if jsonEquals(lVal, r) {
return true
}
}
}
case []interface{}:
switch rVal := right.(type) {
case nil, bool, string, float64:
return overlaps(right, left)
case map[string]interface{}:
return overlaps(right, left)
case []interface{}:
// arrays must have at least one element in common
// TODO: use maps for improved runtime?
for _, l := range lVal {
for _, r := range rVal {
if jsonEquals(l, r) {
return true
}
}
}
}
}
return false
}
// Eval implements sql.Expression
func (j *JSONOverlaps) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
span, ctx := ctx.Span(fmt.Sprintf("function.%s", j.FunctionName()))
defer span.End()
left, err := getJSONDocumentFromRow(ctx, row, j.Left)
if err != nil {
return nil, err
}
if left == nil {
return nil, nil
}
right, err := getJSONDocumentFromRow(ctx, row, j.Right)
if err != nil {
return nil, err
}
if right == nil {
return nil, nil
}
return overlaps(left.Val, right.Val), nil
}
// Children implements sql.Expression
func (j *JSONOverlaps) Children() []sql.Expression {
return []sql.Expression{j.Left, j.Right}
}
// WithChildren implements sql.Expression
func (j *JSONOverlaps) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewJSONOverlaps(children...)
}