-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
json_array.go
421 lines (367 loc) · 13.7 KB
/
json_array.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package invertedidx
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/json"
"github.com/cockroachdb/errors"
)
type jsonOrArrayJoinPlanner struct {
tabID opt.TableID
index cat.Index
inputCols opt.ColSet
}
var _ invertedJoinPlanner = &jsonOrArrayJoinPlanner{}
// extractInvertedJoinConditionFromLeaf is part of the invertedJoinPlanner interface.
func (j *jsonOrArrayJoinPlanner) extractInvertedJoinConditionFromLeaf(
ctx context.Context, expr opt.ScalarExpr,
) opt.ScalarExpr {
switch t := expr.(type) {
case *memo.ContainsExpr:
if j.canExtractJSONOrArrayJoinCondition(t.Left, t.Right) {
return t
}
return nil
default:
return nil
}
}
// canExtractJSONOrArrayJoinCondition returns true if it is possible to extract
// an inverted join condition from the given left and right expression
// arguments. Returns false otherwise.
func (j *jsonOrArrayJoinPlanner) canExtractJSONOrArrayJoinCondition(
left, right opt.ScalarExpr,
) bool {
// The first argument should be a variable or expression corresponding to
// the index column.
// TODO(mgartner): The first argument could be an expression that matches a
// computed column expression if the computed column is indexed. Pass
// computedColumns to enable this.
if !isIndexColumn(j.tabID, j.index, left, nil /* computedColumns */) {
return false
}
if left.DataType().Family() == types.ArrayFamily &&
j.index.Version() < descpb.EmptyArraysInInvertedIndexesVersion {
// We cannot plan inverted joins on array indexes that do not include
// keys for empty arrays.
return false
}
// The second argument should either come from the input or be a constant.
var p props.Shared
memo.BuildSharedProps(right, &p)
if !p.OuterCols.Empty() {
if !p.OuterCols.SubsetOf(j.inputCols) {
return false
}
} else if !memo.CanExtractConstDatum(right) {
return false
}
return true
}
// getSpanExprForJSONOrArrayIndex gets a SpanExpression that constrains a
// json or array index according to the given constant.
func getSpanExprForJSONOrArrayIndex(
evalCtx *tree.EvalContext, d tree.Datum,
) *invertedexpr.SpanExpression {
spanExpr, err := invertedexpr.JSONOrArrayToContainingSpanExpr(evalCtx, d)
if err != nil {
panic(err)
}
return spanExpr
}
type jsonOrArrayInvertedExpr struct {
tree.ComparisonExpr
nonIndexParam tree.TypedExpr
// spanExpr is the result of evaluating the comparison expression represented
// by this jsonOrArrayInvertedExpr. It is nil prior to evaluation.
spanExpr *invertedexpr.SpanExpression
}
var _ tree.TypedExpr = &jsonOrArrayInvertedExpr{}
// jsonOrArrayDatumsToInvertedExpr implements invertedexpr.DatumsToInvertedExpr for
// JSON and Array columns.
type jsonOrArrayDatumsToInvertedExpr struct {
evalCtx *tree.EvalContext
colTypes []*types.T
invertedExpr tree.TypedExpr
row rowenc.EncDatumRow
alloc rowenc.DatumAlloc
}
var _ invertedexpr.DatumsToInvertedExpr = &jsonOrArrayDatumsToInvertedExpr{}
var _ tree.IndexedVarContainer = &jsonOrArrayDatumsToInvertedExpr{}
// IndexedVarEval is part of the IndexedVarContainer interface.
func (g *jsonOrArrayDatumsToInvertedExpr) IndexedVarEval(
idx int, ctx *tree.EvalContext,
) (tree.Datum, error) {
err := g.row[idx].EnsureDecoded(g.colTypes[idx], &g.alloc)
if err != nil {
return nil, err
}
return g.row[idx].Datum.Eval(ctx)
}
// IndexedVarResolvedType is part of the IndexedVarContainer interface.
func (g *jsonOrArrayDatumsToInvertedExpr) IndexedVarResolvedType(idx int) *types.T {
return g.colTypes[idx]
}
// IndexedVarNodeFormatter is part of the IndexedVarContainer interface.
func (g *jsonOrArrayDatumsToInvertedExpr) IndexedVarNodeFormatter(idx int) tree.NodeFormatter {
n := tree.Name(fmt.Sprintf("$%d", idx))
return &n
}
// NewJSONOrArrayDatumsToInvertedExpr returns a new
// jsonOrArrayDatumsToInvertedExpr.
func NewJSONOrArrayDatumsToInvertedExpr(
evalCtx *tree.EvalContext, colTypes []*types.T, expr tree.TypedExpr,
) (invertedexpr.DatumsToInvertedExpr, error) {
g := &jsonOrArrayDatumsToInvertedExpr{
evalCtx: evalCtx,
colTypes: colTypes,
}
// getInvertedExprLeaf takes a TypedExpr consisting of a ComparisonExpr and
// constructs a new TypedExpr tree consisting of a jsonOrArrayInvertedExpr.
// The jsonOrArrayInvertedExpr serves to improve the performance of
// jsonOrArrayDatumsToInvertedExpr.Convert by reducing the amount of
// computation needed to convert an input row to a SpanExpression. It does
// this by pre-computing and caching the SpanExpressions for any comparison
// expressions that have a constant as the non-indexed argument.
getInvertedExprLeaf := func(expr tree.TypedExpr) (tree.TypedExpr, error) {
switch t := expr.(type) {
case *tree.ComparisonExpr:
if t.Operator != tree.Contains {
return nil, fmt.Errorf("%s cannot be index-accelerated", t)
}
// We know that the non-index param is the second param.
nonIndexParam := t.Right.(tree.TypedExpr)
// If possible, get the span expression now so we don't need to recompute
// it for every row.
var spanExpr *invertedexpr.SpanExpression
if d, ok := nonIndexParam.(tree.Datum); ok {
spanExpr = getSpanExprForJSONOrArrayIndex(evalCtx, d)
}
return &jsonOrArrayInvertedExpr{
ComparisonExpr: *t,
nonIndexParam: nonIndexParam,
spanExpr: spanExpr,
}, nil
default:
return nil, fmt.Errorf("unsupported expression %v", t)
}
}
var err error
g.invertedExpr, err = getInvertedExpr(expr, getInvertedExprLeaf)
if err != nil {
return nil, err
}
return g, nil
}
// Convert implements the invertedexpr.DatumsToInvertedExpr interface.
func (g *jsonOrArrayDatumsToInvertedExpr) Convert(
ctx context.Context, datums rowenc.EncDatumRow,
) (*invertedexpr.SpanExpressionProto, interface{}, error) {
g.row = datums
g.evalCtx.PushIVarContainer(g)
defer g.evalCtx.PopIVarContainer()
evalInvertedExprLeaf := func(expr tree.TypedExpr) (invertedexpr.InvertedExpression, error) {
switch t := expr.(type) {
case *jsonOrArrayInvertedExpr:
if t.spanExpr != nil {
// We call Copy so the caller can modify the returned expression.
return t.spanExpr.Copy(), nil
}
d, err := t.nonIndexParam.Eval(g.evalCtx)
if err != nil {
return nil, err
}
if d == tree.DNull {
return nil, nil
}
return getSpanExprForJSONOrArrayIndex(g.evalCtx, d), nil
default:
return nil, fmt.Errorf("unsupported expression %v", t)
}
}
invertedExpr, err := evalInvertedExpr(g.invertedExpr, evalInvertedExprLeaf)
if err != nil {
return nil, nil, err
}
if invertedExpr == nil {
return nil, nil, nil
}
spanExpr, ok := invertedExpr.(*invertedexpr.SpanExpression)
if !ok {
return nil, nil, fmt.Errorf("unable to construct span expression")
}
return spanExpr.ToProto(), nil, nil
}
func (g *jsonOrArrayDatumsToInvertedExpr) CanPreFilter() bool {
return false
}
func (g *jsonOrArrayDatumsToInvertedExpr) PreFilter(
enc invertedexpr.EncInvertedVal, preFilters []interface{}, result []bool,
) (bool, error) {
return false, errors.AssertionFailedf("PreFilter called on jsonOrArrayDatumsToInvertedExpr")
}
type jsonOrArrayFilterPlanner struct {
tabID opt.TableID
index cat.Index
computedColumns map[opt.ColumnID]opt.ScalarExpr
}
var _ invertedFilterPlanner = &jsonOrArrayFilterPlanner{}
// extractInvertedFilterConditionFromLeaf is part of the invertedFilterPlanner
// interface.
func (j *jsonOrArrayFilterPlanner) extractInvertedFilterConditionFromLeaf(
evalCtx *tree.EvalContext, expr opt.ScalarExpr,
) (
invertedExpr invertedexpr.InvertedExpression,
remainingFilters opt.ScalarExpr,
_ *invertedexpr.PreFiltererStateForInvertedFilterer,
) {
switch t := expr.(type) {
case *memo.ContainsExpr:
invertedExpr = j.extractJSONOrArrayContainsCondition(evalCtx, t.Left, t.Right)
case *memo.EqExpr:
if fetch, ok := t.Left.(*memo.FetchValExpr); ok {
invertedExpr = j.extractJSONFetchValEqCondition(evalCtx, fetch, t.Right)
}
}
if invertedExpr == nil {
// An inverted expression could not be extracted.
return invertedexpr.NonInvertedColExpression{}, expr, nil
}
// If the extracted inverted expression is not tight then remaining filters
// must be applied after the inverted index scan.
if !invertedExpr.IsTight() {
remainingFilters = expr
}
// We do not currently support pre-filtering for JSON and Array indexes, so
// the returned pre-filter state is nil.
return invertedExpr, remainingFilters, nil
}
// extractJSONOrArrayContainsCondition extracts an InvertedExpression
// representing an inverted filter over the planner's inverted index, based
// on the given left and right expression arguments. Returns an empty
// InvertedExpression if no inverted filter could be extracted.
func (j *jsonOrArrayFilterPlanner) extractJSONOrArrayContainsCondition(
evalCtx *tree.EvalContext, left, right opt.ScalarExpr,
) invertedexpr.InvertedExpression {
// The first argument should be a variable or expression corresponding to
// the index column.
if !isIndexColumn(j.tabID, j.index, left, j.computedColumns) {
return invertedexpr.NonInvertedColExpression{}
}
// The second argument should be a constant.
if !memo.CanExtractConstDatum(right) {
return invertedexpr.NonInvertedColExpression{}
}
d := memo.ExtractConstDatum(right)
if left.DataType().Family() == types.ArrayFamily &&
j.index.Version() < descpb.EmptyArraysInInvertedIndexesVersion {
if arr, ok := d.(*tree.DArray); ok && arr.Len() == 0 {
// We cannot constrain array indexes that do not include
// keys for empty arrays.
return invertedexpr.NonInvertedColExpression{}
}
}
return getSpanExprForJSONOrArrayIndex(evalCtx, d)
}
// extractJSONFetchValEqCondition extracts an InvertedExpression representing an
// inverted filter over the planner's inverted index, based on equality between
// a chain of fetch val expressions and a right scalar expression. If an
// InvertedExpression cannot be generated from the expression, an empty
// InvertedExpression is returned.
//
// In order to generate a non-empty InvertedExpression, left must be a fetch val
// expression in the form [col]->[index0]->[index1]->...->[indexN] where col is
// a variable or expression referencing the inverted column in the inverted
// index and each index is a constant string. The right expression must be a
// constant JSON value that is not an object or an array.
func (j *jsonOrArrayFilterPlanner) extractJSONFetchValEqCondition(
evalCtx *tree.EvalContext, left *memo.FetchValExpr, right opt.ScalarExpr,
) invertedexpr.InvertedExpression {
// The right side of the equals expression should be a constant JSON value
// that is not an object or array.
if !memo.CanExtractConstDatum(right) {
return invertedexpr.NonInvertedColExpression{}
}
val, ok := memo.ExtractConstDatum(right).(*tree.DJSON)
if !ok {
return invertedexpr.NonInvertedColExpression{}
}
typ := val.JSON.Type()
if typ == json.ObjectJSONType || typ == json.ArrayJSONType {
return invertedexpr.NonInvertedColExpression{}
}
// Recursively traverse fetch val expressions and collect keys with which to
// build the InvertedExpression. If it is not possible to build an inverted
// expression from the tree of fetch val expressions, collectKeys returns
// early and foundKeys remains false. If successful, foundKeys is set to
// true and keys are collected and ordered by the outer-most fetch val index
// first. For example, the expression j->'a'->'b' results in the keys
// {"b", "a"}.
foundKeys := false
var keys []string
var collectKeys func(fetch *memo.FetchValExpr)
collectKeys = func(fetch *memo.FetchValExpr) {
// The right side of the fetch val expression, the Index field, must be
// a constant string. If not, then we cannot build an inverted
// expression.
if !memo.CanExtractConstDatum(fetch.Index) {
return
}
key, ok := memo.ExtractConstDatum(fetch.Index).(*tree.DString)
if !ok {
return
}
// Append the key to the list of keys.
keys = append(keys, string(*key))
// If the left side of the fetch val expression, the Json field, is a
// variable or expression corresponding to the index column, then we
// have found a valid list of keys to build an inverted expression.
// an inverted
if isIndexColumn(j.tabID, j.index, fetch.Json, j.computedColumns) {
foundKeys = true
return
}
// If the left side of the fetch val expression is another fetch val
// expression, recursively collect its keys.
if innerFetch, ok := fetch.Json.(*memo.FetchValExpr); ok {
collectKeys(innerFetch)
}
// Otherwise, we cannot build an inverted expression.
}
collectKeys(left)
if !foundKeys {
return invertedexpr.NonInvertedColExpression{}
}
// Build a new JSON object of the form:
// {<keyN>: ... {<key1>: {key0: <val>}}}
// Note that key0 is the outer-most fetch val index, so the expression
// j->'a'->'b' = 1 results in {"a": {"b": 1}}.
var obj json.JSON
for i := 0; i < len(keys); i++ {
b := json.NewObjectBuilder(1)
if i == 0 {
b.Add(keys[i], val.JSON)
} else {
b.Add(keys[i], obj)
}
obj = b.Build()
}
return getSpanExprForJSONOrArrayIndex(evalCtx, tree.NewDJSON(obj))
}