github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/json/json.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package json 12 13 import ( 14 "bytes" 15 "encoding/json" 16 "fmt" 17 "io" 18 "math/big" 19 "reflect" 20 "sort" 21 "strconv" 22 "strings" 23 "unicode/utf8" 24 "unsafe" 25 26 "github.com/cockroachdb/apd/v3" 27 "github.com/cockroachdb/cockroachdb-parser/pkg/geo" 28 "github.com/cockroachdb/cockroachdb-parser/pkg/geo/geopb" 29 "github.com/cockroachdb/cockroachdb-parser/pkg/keysbase" 30 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/inverted" 31 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/pgwire/pgcode" 32 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/pgwire/pgerror" 33 "github.com/cockroachdb/cockroachdb-parser/pkg/util/buildutil" 34 "github.com/cockroachdb/cockroachdb-parser/pkg/util/encoding" 35 "github.com/cockroachdb/cockroachdb-parser/pkg/util/intsets" 36 uniq "github.com/cockroachdb/cockroachdb-parser/pkg/util/unique" 37 "github.com/cockroachdb/errors" 38 ) 39 40 // Type represents a JSON type. 41 type Type int 42 43 // This enum defines the ordering of types. It should not be reordered. 44 const ( 45 _ Type = iota 46 NullJSONType 47 StringJSONType 48 NumberJSONType 49 FalseJSONType 50 TrueJSONType 51 ArrayJSONType 52 ObjectJSONType 53 ) 54 55 func (t Type) String() string { 56 switch t { 57 case NullJSONType: 58 return "null" 59 case StringJSONType: 60 return "string" 61 case NumberJSONType: 62 return "numeric" 63 case FalseJSONType, TrueJSONType: 64 return "boolean" 65 case ArrayJSONType: 66 return "array" 67 case ObjectJSONType: 68 return "object" 69 default: 70 panic(errors.AssertionFailedf("unknown JSON type %d", int(t))) 71 } 72 } 73 74 const ( 75 wordSize = unsafe.Sizeof(big.Word(0)) 76 decimalSize = unsafe.Sizeof(apd.Decimal{}) 77 stringHeaderSize = unsafe.Sizeof(reflect.StringHeader{}) //lint:ignore SA1019 deprecated, but no clear replacement 78 sliceHeaderSize = unsafe.Sizeof(reflect.SliceHeader{}) //lint:ignore SA1019 deprecated, but no clear replacement 79 keyValuePairSize = unsafe.Sizeof(jsonKeyValuePair{}) 80 jsonInterfaceSize = unsafe.Sizeof((JSON)(nil)) 81 ) 82 83 const ( 84 msgModifyAfterBuild = "modify after Build()" 85 ) 86 87 // JSON represents a JSON value. 88 type JSON interface { 89 fmt.Stringer 90 91 Compare(JSON) (int, error) 92 // Type returns the JSON type. 93 Type() Type 94 // Format writes out the JSON document to the specified buffer. 95 Format(buf *bytes.Buffer) 96 // Size returns the size of the JSON document in bytes. 97 Size() uintptr 98 99 // EncodeForwardIndex implements forward indexing for JSONB values. 100 // The encoding depends on the direction of the encoding 101 // specified, using `dir`, and is appended to `buf` and returned. 102 EncodeForwardIndex(buf []byte, dir encoding.Direction) ([]byte, error) 103 104 // encodeInvertedIndexKeys takes in a key prefix and returns a slice of 105 // inverted index keys, one per path through the receiver. 106 encodeInvertedIndexKeys(b []byte) ([][]byte, error) 107 108 // encodeContainingInvertedIndexSpans takes in a key prefix and returns the 109 // spans that must be scanned in the inverted index to evaluate a contains (@>) 110 // predicate with the given JSON (i.e., find the objects in the index that 111 // contain the given JSON). 112 // 113 // The spans are returned in an inverted.SpanExpression, which represents the 114 // set operations that must be applied on the spans read during execution. See 115 // comments in the SpanExpression definition for details. 116 // 117 // If isRoot is true, this function is being called at the root level of the 118 // JSON hierarchy. If isObjectValue is true, the given JSON is the value of a 119 // JSON object key. Note that isRoot and isObjectValue cannot both be true at 120 // the same time. 121 encodeContainingInvertedIndexSpans( 122 b []byte, isRoot, isObjectValue bool, 123 ) (invertedExpr inverted.Expression, err error) 124 125 // encodeContainedInvertedIndexSpans takes in a key prefix and returns the 126 // spans that must be scanned in the inverted index to evaluate a contained 127 // by (<@) predicate with the given JSON (i.e., find the objects in the index 128 // that are contained by the given JSON). 129 // 130 // The spans are returned in an inverted.SpanExpression, which represents the 131 // set operations that must be applied on the spans read during execution. See 132 // comments in the SpanExpression definition for details. 133 // 134 // If isRoot is true, this function is being called at the root level of the 135 // JSON hierarchy. If isObjectValue is true, the given JSON is the value of a 136 // JSON object key. Note that isRoot and isObjectValue cannot both be true at 137 // the same time. 138 encodeContainedInvertedIndexSpans( 139 b []byte, isRoot, isObjectValue bool, 140 ) (invertedExpr inverted.Expression, err error) 141 142 // numInvertedIndexEntries returns the number of entries that will be 143 // produced if this JSON gets included in an inverted index. 144 numInvertedIndexEntries() (int, error) 145 146 // allPaths returns a slice of new JSON documents, each a path to a leaf 147 // through the receiver. Note that leaves include the empty object and array 148 // in addition to scalars. 149 allPaths() ([]JSON, error) 150 151 // FetchValKey implements the `->` operator for strings, returning nil if the 152 // key is not found. 153 FetchValKey(key string) (JSON, error) 154 155 // FetchValIdx implements the `->` operator for ints, returning nil if the 156 // key is not found. 157 FetchValIdx(idx int) (JSON, error) 158 159 // FetchValKeyOrIdx is used for path access, if obj is an object, it tries to 160 // access the given field. If it's an array, it interprets the key as an int 161 // and tries to access the given index. 162 FetchValKeyOrIdx(key string) (JSON, error) 163 164 // RemoveString implements the `-` operator for strings, returning JSON after removal, 165 // whether removal is valid and error message. 166 RemoveString(s string) (JSON, bool, error) 167 168 // RemoveIndex implements the `-` operator for ints, returning JSON after removal, 169 // whether removal is valid and error message. 170 RemoveIndex(idx int) (JSON, bool, error) 171 172 // RemovePath and doRemovePath implement the `#-` operator for strings, returning JSON after removal, 173 // whether removal is valid and error message. 174 RemovePath(path []string) (JSON, bool, error) 175 doRemovePath(path []string) (JSON, bool, error) 176 177 // Concat implements the `||` operator. 178 Concat(other JSON) (JSON, error) 179 180 // AsText returns the JSON document as a string, with quotes around strings removed, and null as nil. 181 AsText() (*string, error) 182 183 // AsDecimal returns the JSON document as a apd.Decimal if it is a numeric 184 // type, and a boolean indicating if this JSON value is a numeric type. 185 AsDecimal() (*apd.Decimal, bool) 186 187 // AsBool returns the JSON document as a boolean if it is a boolean type, 188 // and a boolean indicating if this JSON value is a bool type. 189 AsBool() (bool, bool) 190 191 // AsArray returns the JSON document as an Array if it is a array type, 192 // and a boolean indicating if this JSON value is a array type. 193 AsArray() ([]JSON, bool) 194 195 // AreKeysSorted returns if the keys in a JSON Object are sorted by 196 // increasing order. It returns false if the underlying JSON value 197 // is not a JSON object. 198 AreKeysSorted() bool 199 200 // Exists implements the `?` operator: does the string exist as a top-level 201 // key within the JSON value? 202 // 203 // If the object is a JSON array, returns true when the key is a top-level 204 // element of the array. 205 Exists(string) (bool, error) 206 207 // StripNulls returns the JSON document with all object fields that have null values omitted 208 // and whether it needs to strip nulls. Stripping nulls is needed only if it contains some 209 // object fields having null values. 210 StripNulls() (JSON, bool, error) 211 212 // ObjectIter returns an *ObjectKeyIterator, nil if json is not an object. 213 ObjectIter() (*ObjectIterator, error) 214 215 // isScalar returns whether the JSON document is null, true, false, a string, 216 // or a number. 217 isScalar() bool 218 219 // preprocessForContains converts a JSON document to an internal interface 220 // which is used to efficiently implement the @> operator. 221 preprocessForContains() (containsable, error) 222 223 // encode appends the encoding of the JSON document to appendTo, returning 224 // the result alongside the JEntry for the document. Note that some values 225 // (true/false/null) are encoded with 0 bytes and are purely defined by their 226 // JEntry. 227 encode(appendTo []byte) (jEntry jEntry, b []byte, err error) 228 229 // MaybeDecode returns an equivalent JSON which is not a jsonEncoded. 230 MaybeDecode() JSON 231 232 // toGoRepr returns the Go-style representation of this JSON value 233 // (map[string]interface{} for objects, etc.). 234 toGoRepr() (interface{}, error) 235 236 // tryDecode returns an equivalent JSON which is not a jsonEncoded, returning 237 // an error if the encoded data was corrupt. 238 tryDecode() (JSON, error) 239 240 // Len returns the number of outermost elements in the JSON document if it is an object or an array. 241 // Otherwise, Len returns 0. 242 Len() int 243 244 // HasContainerLeaf returns whether this document contains in it somewhere 245 // either the empty array or the empty object. 246 HasContainerLeaf() (bool, error) 247 } 248 249 type jsonTrue struct{} 250 251 // TrueJSONValue is JSON `true` 252 var TrueJSONValue = jsonTrue{} 253 254 type jsonFalse struct{} 255 256 // FalseJSONValue is JSON `false` 257 var FalseJSONValue = jsonFalse{} 258 259 type jsonNull struct{} 260 261 // NullJSONValue is JSON `null` 262 var NullJSONValue = jsonNull{} 263 264 type jsonNumber apd.Decimal 265 type jsonString string 266 267 type jsonArray []JSON 268 269 type jsonKeyValuePair struct { 270 k jsonString 271 v JSON 272 } 273 274 // ArrayBuilder builds JSON Array by a JSON sequence. 275 type ArrayBuilder struct { 276 jsons []JSON 277 } 278 279 // NewArrayBuilder returns an ArrayBuilder. The builder will reserve spaces 280 // based on hint about number of adds to reduce times of growing capacity. 281 func NewArrayBuilder(numAddsHint int) *ArrayBuilder { 282 return &ArrayBuilder{ 283 jsons: make([]JSON, 0, numAddsHint), 284 } 285 } 286 287 // Add appends JSON to the sequence. 288 func (b *ArrayBuilder) Add(j JSON) { 289 b.jsons = append(b.jsons, j) 290 } 291 292 // Build returns the constructed JSON array. A caller may not modify the array, 293 // and the ArrayBuilder reserves the right to re-use the array returned (though 294 // the data will not be modified). This is important in the case of a window 295 // function, which might want to incrementally update an aggregation. 296 func (b *ArrayBuilder) Build() JSON { 297 return jsonArray(b.jsons) 298 } 299 300 // ArrayBuilderWithCounter builds JSON Array by a JSON sequence with a size counter. 301 type ArrayBuilderWithCounter struct { 302 ab *ArrayBuilder 303 size uintptr 304 } 305 306 // NewArrayBuilderWithCounter returns an ArrayBuilderWithCounter. 307 func NewArrayBuilderWithCounter() *ArrayBuilderWithCounter { 308 return &ArrayBuilderWithCounter{ 309 ab: NewArrayBuilder(0), 310 size: sliceHeaderSize, 311 } 312 } 313 314 // Add appends JSON to the sequence and updates the size counter. 315 func (b *ArrayBuilderWithCounter) Add(j JSON) { 316 oldCap := cap(b.ab.jsons) 317 b.ab.Add(j) 318 b.size += j.Size() + (uintptr)(cap(b.ab.jsons)-oldCap)*jsonInterfaceSize 319 } 320 321 // Build returns a JSON array built from a JSON sequence. After that, it should 322 // not be modified any longer. 323 func (b *ArrayBuilderWithCounter) Build() JSON { 324 return b.ab.Build() 325 } 326 327 // Size returns the size in bytes of the JSON Array the builder is going to build. 328 func (b *ArrayBuilderWithCounter) Size() uintptr { 329 return b.size 330 } 331 332 // ObjectBuilderWithCounter builds a JSON object a key/value pair at a time, keeping the memory usage of the object. 333 type ObjectBuilderWithCounter struct { 334 ob *ObjectBuilder 335 size uintptr 336 } 337 338 // NewObjectBuilderWithCounter creates and instantiates ObjectBuilder with memory counter. 339 func NewObjectBuilderWithCounter() *ObjectBuilderWithCounter { 340 ob := NewObjectBuilder(0) 341 return &ObjectBuilderWithCounter{ 342 ob: ob, 343 // initial memory allocation 344 size: unsafe.Sizeof(ob) + jsonInterfaceSize, 345 } 346 } 347 348 // Add appends key value pair to the sequence and updates 349 // amount of memory allocated for the overall keys and values. 350 func (b *ObjectBuilderWithCounter) Add(k string, v JSON) { 351 b.ob.Add(k, v) 352 // Size of added JSON + overhead of storing key/value pair + the size of the key. 353 b.size += v.Size() + keyValuePairSize + uintptr(len(k)) 354 } 355 356 // Build returns a JSON object built from a key value pair sequence. After that, 357 // it should not be modified any longer. 358 func (b *ObjectBuilderWithCounter) Build() JSON { 359 return b.ob.Build() 360 } 361 362 // Size returns the size in bytes of the JSON object the builder is going to build. 363 func (b *ObjectBuilderWithCounter) Size() uintptr { 364 return b.size 365 } 366 367 // ObjectBuilder builds JSON Object by a key value pair sequence. 368 type ObjectBuilder struct { 369 pairs []jsonKeyValuePair 370 unordered bool 371 built bool 372 } 373 374 // NewObjectBuilder returns an ObjectBuilder. The builder will reserve spaces 375 // based on hint about number of adds to reduce times of growing capacity. 376 func NewObjectBuilder(numAddsHint int) *ObjectBuilder { 377 return &ObjectBuilder{ 378 pairs: make([]jsonKeyValuePair, 0, numAddsHint), 379 } 380 } 381 382 // Add appends key value pair to the sequence. 383 func (b *ObjectBuilder) Add(k string, v JSON) { 384 if b.built { 385 panic(errors.AssertionFailedf(msgModifyAfterBuild)) 386 } 387 b.pairs = append(b.pairs, jsonKeyValuePair{k: jsonString(k), v: v}) 388 } 389 390 // Build returns a JSON object built from a key value pair sequence. After that, 391 // it should not be modified any longer. 392 func (b *ObjectBuilder) Build() JSON { 393 if b.built { 394 panic(errors.AssertionFailedf(msgModifyAfterBuild)) 395 } 396 b.built = true 397 if b.unordered { 398 return jsonObject(b.pairs) 399 } 400 401 orders := make([]int, len(b.pairs)) 402 for i := range orders { 403 orders[i] = i 404 } 405 sorter := pairSorter{ 406 pairs: b.pairs, 407 orders: orders, 408 hasNonUnique: false, 409 } 410 b.pairs = nil 411 sort.Sort(&sorter) 412 sorter.unique() 413 return jsonObject(sorter.pairs) 414 } 415 416 // FixedKeysObjectBuilder is a JSON object builder that builds 417 // an object for the specified fixed set of unique keys. 418 // This object can be reused to build multiple instances of JSON object. 419 type FixedKeysObjectBuilder struct { 420 pairs []jsonKeyValuePair 421 keyOrd map[string]int 422 updated intsets.Fast 423 } 424 425 // NewFixedKeysObjectBuilder creates JSON object builder for the specified 426 // set of fixed, unique keys. 427 func NewFixedKeysObjectBuilder(keys []string) (*FixedKeysObjectBuilder, error) { 428 sort.Strings(keys) 429 pairs := make([]jsonKeyValuePair, len(keys)) 430 keyOrd := make(map[string]int, len(keys)) 431 432 for i, k := range keys { 433 if _, exists := keyOrd[k]; exists { 434 return nil, errors.AssertionFailedf("expected unique keys, found %v", keys) 435 } 436 pairs[i] = jsonKeyValuePair{k: jsonString(keys[i])} 437 keyOrd[k] = i 438 } 439 440 return &FixedKeysObjectBuilder{ 441 pairs: pairs, 442 keyOrd: keyOrd, 443 }, nil 444 } 445 446 // Set sets the value for the specified key. 447 // All previously configured keys must be set before calling Build. 448 func (b *FixedKeysObjectBuilder) Set(k string, v JSON) error { 449 ord, ok := b.keyOrd[k] 450 if !ok { 451 return errors.AssertionFailedf("unknown key %s", k) 452 } 453 454 b.pairs[ord].v = v 455 b.updated.Add(ord) 456 return nil 457 } 458 459 // Build builds JSON object. 460 func (b *FixedKeysObjectBuilder) Build() (JSON, error) { 461 if b.updated.Len() != len(b.pairs) { 462 return nil, errors.AssertionFailedf( 463 "expected all %d keys to be updated, %d updated", 464 len(b.pairs), b.updated.Len()) 465 } 466 b.updated = intsets.Fast{} 467 // Must copy b.pairs in case builder is reused. 468 return jsonObject(append([]jsonKeyValuePair(nil), b.pairs...)), nil 469 } 470 471 // pairSorter sorts and uniqueifies JSON pairs. In order to keep 472 // the last one for pairs with the same key while sort.Sort is 473 // not stable, pairSorter uses []int orders to maintain order and 474 // bool hasNonUnique to skip unnecessary uniqueifying. 475 type pairSorter struct { 476 pairs []jsonKeyValuePair 477 orders []int 478 hasNonUnique bool 479 } 480 481 func (s *pairSorter) Len() int { 482 return len(s.pairs) 483 } 484 485 func (s *pairSorter) Less(i, j int) bool { 486 cmp := strings.Compare(string(s.pairs[i].k), string(s.pairs[j].k)) 487 if cmp != 0 { 488 return cmp == -1 489 } 490 s.hasNonUnique = true 491 // The element with greater order has lower rank when their keys 492 // are same, since unique algorithm will prefer first element. 493 return s.orders[i] > s.orders[j] 494 } 495 496 func (s *pairSorter) Swap(i, j int) { 497 s.pairs[i], s.orders[i], s.pairs[j], s.orders[j] = s.pairs[j], s.orders[j], s.pairs[i], s.orders[i] 498 } 499 500 func (s *pairSorter) unique() { 501 // If there are any duplicate keys, then in sorted order it will have 502 // two pairs with rank i and i + 1 whose keys are same. 503 // For sorting based on comparisons, if two unique elements (pair.k, order) 504 // have rank i and i + 1, they have to compare once to figure out their 505 // relative order in the final position i and i + 1. So if there are any 506 // equal elements, then the sort must have compared them at some point. 507 if !s.hasNonUnique { 508 return 509 } 510 top := 0 511 for i := 1; i < len(s.pairs); i++ { 512 if s.pairs[top].k != s.pairs[i].k { 513 top++ 514 if top != i { 515 s.pairs[top] = s.pairs[i] 516 } 517 } 518 } 519 s.pairs = s.pairs[:top+1] 520 } 521 522 // jsonObject represents a JSON object as a sorted-by-key list of key-value 523 // pairs, which are unique by key. 524 type jsonObject []jsonKeyValuePair 525 526 var emptyJSONObject = jsonObject(nil) 527 var emptyJSONArray = jsonArray(nil) 528 529 func (jsonNull) Type() Type { return NullJSONType } 530 func (jsonFalse) Type() Type { return FalseJSONType } 531 func (jsonTrue) Type() Type { return TrueJSONType } 532 func (jsonNumber) Type() Type { return NumberJSONType } 533 func (jsonString) Type() Type { return StringJSONType } 534 func (jsonArray) Type() Type { return ArrayJSONType } 535 func (jsonObject) Type() Type { return ObjectJSONType } 536 537 func (j jsonNull) MaybeDecode() JSON { return j } 538 func (j jsonFalse) MaybeDecode() JSON { return j } 539 func (j jsonTrue) MaybeDecode() JSON { return j } 540 func (j jsonNumber) MaybeDecode() JSON { return j } 541 func (j jsonString) MaybeDecode() JSON { return j } 542 func (j jsonArray) MaybeDecode() JSON { return j } 543 func (j jsonObject) MaybeDecode() JSON { return j } 544 545 func (j jsonNull) AsDecimal() (*apd.Decimal, bool) { return nil, false } 546 func (j jsonFalse) AsDecimal() (*apd.Decimal, bool) { return nil, false } 547 func (j jsonTrue) AsDecimal() (*apd.Decimal, bool) { return nil, false } 548 func (j jsonString) AsDecimal() (*apd.Decimal, bool) { return nil, false } 549 func (j jsonArray) AsDecimal() (*apd.Decimal, bool) { return nil, false } 550 func (j jsonObject) AsDecimal() (*apd.Decimal, bool) { return nil, false } 551 func (j jsonNumber) AsDecimal() (*apd.Decimal, bool) { 552 d := apd.Decimal(j) 553 return &d, true 554 } 555 556 func (j jsonNull) AsBool() (bool, bool) { return false, false } 557 func (j jsonFalse) AsBool() (bool, bool) { return false, true } 558 func (j jsonTrue) AsBool() (bool, bool) { return true, true } 559 func (j jsonString) AsBool() (bool, bool) { return false, false } 560 func (j jsonArray) AsBool() (bool, bool) { return false, false } 561 func (j jsonObject) AsBool() (bool, bool) { return false, false } 562 func (j jsonNumber) AsBool() (bool, bool) { return false, false } 563 564 func (j jsonNull) tryDecode() (JSON, error) { return j, nil } 565 func (j jsonFalse) tryDecode() (JSON, error) { return j, nil } 566 func (j jsonTrue) tryDecode() (JSON, error) { return j, nil } 567 func (j jsonNumber) tryDecode() (JSON, error) { return j, nil } 568 func (j jsonString) tryDecode() (JSON, error) { return j, nil } 569 func (j jsonArray) tryDecode() (JSON, error) { return j, nil } 570 func (j jsonObject) tryDecode() (JSON, error) { return j, nil } 571 572 func cmpJSONTypes(a Type, b Type) int { 573 if b > a { 574 return -1 575 } 576 if b < a { 577 return 1 578 } 579 return 0 580 } 581 582 // isEmptyArray returns true if j is a JSON array with length 0. 583 func isEmptyArray(j JSON) bool { 584 return j.Type() == ArrayJSONType && j.Len() == 0 585 } 586 587 func (j jsonNull) Compare(other JSON) (int, error) { 588 if isEmptyArray(other) { 589 return 1, nil 590 } 591 return cmpJSONTypes(j.Type(), other.Type()), nil 592 } 593 func (j jsonFalse) Compare(other JSON) (int, error) { 594 if isEmptyArray(other) { 595 return 1, nil 596 } 597 return cmpJSONTypes(j.Type(), other.Type()), nil 598 } 599 func (j jsonTrue) Compare(other JSON) (int, error) { 600 if isEmptyArray(other) { 601 return 1, nil 602 } 603 return cmpJSONTypes(j.Type(), other.Type()), nil 604 } 605 606 func decodeIfNeeded(j JSON) (JSON, error) { 607 if enc, ok := j.(*jsonEncoded); ok { 608 var err error 609 j, err = enc.decode() 610 if err != nil { 611 return nil, err 612 } 613 } 614 return j, nil 615 } 616 617 func (j jsonNumber) Compare(other JSON) (int, error) { 618 if isEmptyArray(other) { 619 return 1, nil 620 } 621 cmp := cmpJSONTypes(j.Type(), other.Type()) 622 if cmp != 0 { 623 return cmp, nil 624 } 625 var err error 626 if other, err = decodeIfNeeded(other); err != nil { 627 return 0, err 628 } 629 dec := apd.Decimal(j) 630 o := apd.Decimal(other.(jsonNumber)) 631 return dec.Cmp(&o), nil 632 } 633 634 func (j jsonString) Compare(other JSON) (int, error) { 635 if isEmptyArray(other) { 636 return 1, nil 637 } 638 cmp := cmpJSONTypes(j.Type(), other.Type()) 639 if cmp != 0 { 640 return cmp, nil 641 } 642 // TODO(justin): we should optimize this, we don't have to decode the whole thing. 643 var err error 644 if other, err = decodeIfNeeded(other); err != nil { 645 return 0, err 646 } 647 o := other.(jsonString) 648 if o > j { 649 return -1, nil 650 } 651 if o < j { 652 return 1, nil 653 } 654 return 0, nil 655 } 656 657 func (j jsonArray) Compare(other JSON) (int, error) { 658 switch { 659 case isEmptyArray(j) && isEmptyArray(other): 660 return 0, nil 661 case isEmptyArray(j): 662 return -1, nil 663 case isEmptyArray(other): 664 return 1, nil 665 } 666 cmp := cmpJSONTypes(j.Type(), other.Type()) 667 if cmp != 0 { 668 return cmp, nil 669 } 670 lenJ := j.Len() 671 lenO := other.Len() 672 if lenJ < lenO { 673 return -1, nil 674 } 675 if lenJ > lenO { 676 return 1, nil 677 } 678 // TODO(justin): we should optimize this, we don't have to decode the whole thing. 679 var err error 680 if other, err = decodeIfNeeded(other); err != nil { 681 return 0, err 682 } 683 o := other.(jsonArray) 684 for i := 0; i < lenJ; i++ { 685 cmp, err := j[i].Compare(o[i]) 686 if err != nil { 687 return 0, err 688 } 689 if cmp != 0 { 690 return cmp, nil 691 } 692 } 693 return 0, nil 694 } 695 696 func (j jsonObject) Compare(other JSON) (int, error) { 697 // NOTE: There is no need to check if other is an empty array because all 698 // arrays are less than all objects, so the type comparison is sufficient. 699 cmp := cmpJSONTypes(j.Type(), other.Type()) 700 if cmp != 0 { 701 return cmp, nil 702 } 703 lenJ := j.Len() 704 lenO := other.Len() 705 if lenJ < lenO { 706 return -1, nil 707 } 708 if lenJ > lenO { 709 return 1, nil 710 } 711 // TODO(justin): we should optimize this, we don't have to decode the whole thing. 712 var err error 713 if other, err = decodeIfNeeded(other); err != nil { 714 return 0, err 715 } 716 o := other.(jsonObject) 717 for i := 0; i < lenJ; i++ { 718 cmpKey, err := j[i].k.Compare(o[i].k) 719 if err != nil { 720 return 0, err 721 } 722 if cmpKey != 0 { 723 return cmpKey, nil 724 } 725 cmpVal, err := j[i].v.Compare(o[i].v) 726 if err != nil { 727 return 0, err 728 } 729 if cmpVal != 0 { 730 return cmpVal, nil 731 } 732 } 733 return 0, nil 734 } 735 736 var errTrailingCharacters = pgerror.WithCandidateCode(errors.New("trailing characters after JSON document"), pgcode.InvalidTextRepresentation) 737 738 func (jsonNull) Format(buf *bytes.Buffer) { buf.WriteString("null") } 739 740 func (jsonFalse) Format(buf *bytes.Buffer) { buf.WriteString("false") } 741 742 func (jsonTrue) Format(buf *bytes.Buffer) { buf.WriteString("true") } 743 744 func (j jsonNumber) Format(buf *bytes.Buffer) { 745 dec := apd.Decimal(j) 746 // Make sure non-finite values are encoded as valid strings by 747 // quoting them. Unfortunately, since this is JSON, there's no 748 // defined way to express the three special numeric values (+inf, 749 // -inf, nan) except as a string. This means that the decoding 750 // side can't tell whether the field should be a float or a 751 // string. Testing for exact types is thus tricky. As of this 752 // comment, our current tests for this behavior happen it the SQL 753 // package, not here in the JSON package. 754 nonfinite := dec.Form != apd.Finite 755 if nonfinite { 756 buf.WriteByte('"') 757 } 758 buf.WriteString(dec.String()) 759 if nonfinite { 760 buf.WriteByte('"') 761 } 762 } 763 764 func (j jsonString) Format(buf *bytes.Buffer) { 765 encodeJSONString(buf, string(j)) 766 } 767 768 func asString(j JSON) string { 769 var buf bytes.Buffer 770 j.Format(&buf) 771 return buf.String() 772 } 773 774 func (j jsonNull) String() string { return asString(j) } 775 func (j jsonTrue) String() string { return asString(j) } 776 func (j jsonFalse) String() string { return asString(j) } 777 func (j jsonString) String() string { return asString(j) } 778 func (j jsonNumber) String() string { return asString(j) } 779 func (j jsonArray) String() string { return asString(j) } 780 func (j jsonObject) String() string { return asString(j) } 781 782 const hexAlphabet = "0123456789abcdef" 783 784 // encodeJSONString writes a string literal to buf as a JSON string. 785 // Cribbed from https://github.com/golang/go/blob/7badae85f20f1bce4cc344f9202447618d45d414/src/encoding/json/encode.go. 786 func encodeJSONString(buf *bytes.Buffer, s string) { 787 buf.WriteByte('"') 788 start := 0 789 for i := 0; i < len(s); { 790 if b := s[i]; b < utf8.RuneSelf { 791 if safeSet[b] { 792 i++ 793 continue 794 } 795 if start < i { 796 buf.WriteString(s[start:i]) 797 } 798 switch b { 799 case '\\', '"': 800 buf.WriteByte('\\') 801 buf.WriteByte(b) 802 case '\n': 803 buf.WriteByte('\\') 804 buf.WriteByte('n') 805 case '\r': 806 buf.WriteByte('\\') 807 buf.WriteByte('r') 808 case '\t': 809 buf.WriteByte('\\') 810 buf.WriteByte('t') 811 default: 812 // This encodes bytes < 0x20 except for \t, \n and \r. 813 // If escapeHTML is set, it also escapes <, >, and & 814 // because they can lead to security holes when 815 // user-controlled strings are rendered into JSON 816 // and served to some browsers. 817 buf.WriteString(`\u00`) 818 buf.WriteByte(hexAlphabet[b>>4]) 819 buf.WriteByte(hexAlphabet[b&0xF]) 820 } 821 i++ 822 start = i 823 continue 824 } 825 c, size := utf8.DecodeRuneInString(s[i:]) 826 if c == utf8.RuneError && size == 1 { 827 if start < i { 828 buf.WriteString(s[start:i]) 829 } 830 buf.WriteString(`\ufffd`) 831 i += size 832 start = i 833 continue 834 } 835 i += size 836 } 837 if start < len(s) { 838 buf.WriteString(s[start:]) 839 } 840 buf.WriteByte('"') 841 } 842 843 func (j jsonArray) Format(buf *bytes.Buffer) { 844 buf.WriteByte('[') 845 for i := range j { 846 if i != 0 { 847 buf.WriteString(", ") 848 } 849 j[i].Format(buf) 850 } 851 buf.WriteByte(']') 852 } 853 854 func (j jsonObject) Format(buf *bytes.Buffer) { 855 buf.WriteByte('{') 856 for i := range j { 857 if i != 0 { 858 buf.WriteString(", ") 859 } 860 encodeJSONString(buf, string(j[i].k)) 861 buf.WriteString(": ") 862 j[i].v.Format(buf) 863 } 864 buf.WriteByte('}') 865 } 866 867 func (jsonNull) Size() uintptr { return 0 } 868 869 func (jsonFalse) Size() uintptr { return 0 } 870 871 func (jsonTrue) Size() uintptr { return 0 } 872 873 func (j jsonNumber) Size() uintptr { 874 return j.Coeff.Size() 875 } 876 877 func (j jsonString) Size() uintptr { 878 return stringHeaderSize + uintptr(len(j)) 879 } 880 881 func (j jsonArray) Size() uintptr { 882 valSize := sliceHeaderSize + uintptr(cap(j))*jsonInterfaceSize 883 for _, elem := range j { 884 valSize += elem.Size() 885 } 886 return valSize 887 } 888 889 func (j jsonObject) Size() uintptr { 890 valSize := sliceHeaderSize + uintptr(cap(j))*keyValuePairSize 891 // jsonKeyValuePair consists of jsonString(i.e. string header) k and JSON interface v. 892 // Since elem.k.Size() has already taken stringHeaderSize into account, we should 893 // reduce len(j) * stringHeaderSize to avoid counting the size of string headers twice 894 valSize -= uintptr(len(j)) * stringHeaderSize 895 for _, elem := range j { 896 valSize += elem.k.Size() 897 valSize += elem.v.Size() 898 } 899 return valSize 900 } 901 902 func (j jsonNull) AsArray() ([]JSON, bool) { 903 return nil, false 904 } 905 906 func (j jsonString) AsArray() ([]JSON, bool) { 907 return nil, false 908 } 909 910 func (j jsonFalse) AsArray() ([]JSON, bool) { 911 return nil, false 912 } 913 914 func (j jsonTrue) AsArray() ([]JSON, bool) { 915 return nil, false 916 } 917 918 func (j jsonObject) AsArray() ([]JSON, bool) { 919 return nil, false 920 } 921 922 func (j jsonArray) AsArray() ([]JSON, bool) { 923 return j, true 924 } 925 926 func (j jsonNumber) AsArray() ([]JSON, bool) { 927 return nil, false 928 } 929 930 func (j jsonNull) AreKeysSorted() bool { 931 return false 932 } 933 934 func (j jsonString) AreKeysSorted() bool { 935 return false 936 } 937 938 func (j jsonFalse) AreKeysSorted() bool { 939 return false 940 } 941 942 func (j jsonTrue) AreKeysSorted() bool { 943 return false 944 } 945 946 func (j jsonNumber) AreKeysSorted() bool { 947 return false 948 } 949 950 func (j jsonArray) AreKeysSorted() bool { 951 return false 952 } 953 954 func (j jsonObject) AreKeysSorted() bool { 955 keys := make([]string, 0, j.Len()) 956 for _, a := range j { 957 keys = append(keys, a.k.String()) 958 } 959 return sort.StringsAreSorted(keys) 960 } 961 962 // parseJSONGoStd parses json using encoding/json library. 963 // TODO(yevgeniy): Remove this code once we get more confidence in lexer implementation. 964 func parseJSONGoStd(s string, _ parseConfig) (JSON, error) { 965 // This goes in two phases - first it parses the string into raw interface{}s 966 // using the Go encoding/json package, then it transforms that into a JSON. 967 // This could be faster if we wrote a parser to go directly into the JSON. 968 // Note: a better way to unmarshal a single JSON value would be to use 969 // json.Unmarshal; however, we cannot do that because we want to get numbers 970 // as strings; Alas, json.Unmarshal does not support that, and so, we have 971 // to use json.Decoder. 972 var result interface{} 973 decoder := json.NewDecoder(strings.NewReader(s)) 974 // We want arbitrary size/precision decimals, so we call UseNumber() to tell 975 // the decoder to decode numbers into strings instead of float64s (which we 976 // later parse using apd). 977 decoder.UseNumber() 978 err := decoder.Decode(&result) 979 if err != nil { 980 return nil, jsonDecodeError(err) 981 } 982 983 // Check to see if input has more data. 984 // Note: using decoder.More() is wrong since it allows `]` and '}' 985 // characters (i.e. '{}]this is BAD' will be parsed fine, and More will return 986 // false -- thus allowing invalid JSON data to be parsed correctly). The 987 // decoder is meant to be used in the streaming applications; not in a one-off 988 // Unmarshal mode we are using here. Therefore, to check if input has 989 // trailing characters, we have to attempt to decode the next value. 990 var more interface{} 991 if err := decoder.Decode(&more); err != io.EOF { 992 return nil, errTrailingCharacters 993 } 994 return MakeJSON(result) 995 } 996 997 func jsonDecodeError(err error) error { 998 return pgerror.Wrapf( 999 errors.Handled(err), 1000 pgcode.InvalidTextRepresentation, 1001 "unable to decode JSON") 1002 } 1003 1004 // ParseJSON takes a string of JSON and returns a JSON value. 1005 func ParseJSON(s string, opts ...ParseOption) (JSON, error) { 1006 cfg := parseJSONDefaultConfig 1007 for _, o := range opts { 1008 o.apply(&cfg) 1009 } 1010 return cfg.parseJSON(s) 1011 } 1012 1013 // EncodeInvertedIndexKeys takes in a key prefix and returns a slice of inverted index keys, 1014 // one per unique path through the receiver. 1015 func EncodeInvertedIndexKeys(b []byte, json JSON) ([][]byte, error) { 1016 return json.encodeInvertedIndexKeys(encoding.EncodeJSONAscending(b)) 1017 } 1018 1019 // EncodeContainingInvertedIndexSpans takes in a key prefix and returns the 1020 // spans that must be scanned in the inverted index to evaluate a contains (@>) 1021 // predicate with the given JSON (i.e., find the objects in the index that 1022 // contain the given JSON). 1023 // 1024 // The spans are returned in an inverted.SpanExpression, which represents the 1025 // set operations that must be applied on the spans read during execution. See 1026 // comments in the SpanExpression definition for details. 1027 // 1028 // The input inKey is prefixed to the keys in all returned spans. 1029 func EncodeContainingInvertedIndexSpans( 1030 b []byte, json JSON, 1031 ) (invertedExpr inverted.Expression, err error) { 1032 return json.encodeContainingInvertedIndexSpans( 1033 encoding.EncodeJSONAscending(b), true /* isRoot */, false, /* isObjectValue */ 1034 ) 1035 } 1036 1037 // EncodeContainedInvertedIndexSpans takes in a key prefix and returns the 1038 // spans that must be scanned in the inverted index to evaluate a contained by 1039 // (<@) predicate with the given JSON (i.e., find the objects in the index that 1040 // could be contained by the given JSON). 1041 // 1042 // The spans are returned in an inverted.SpanExpression, which represents the 1043 // set operations that must be applied on the spans read during execution. See 1044 // comments in the SpanExpression definition for details. 1045 // 1046 // The input inKey is prefixed to the keys in all returned spans. 1047 func EncodeContainedInvertedIndexSpans( 1048 b []byte, json JSON, 1049 ) (invertedExpr inverted.Expression, err error) { 1050 invertedExpr, err = json.encodeContainedInvertedIndexSpans( 1051 encoding.EncodeJSONAscending(b), true /* isRoot */, false, /* isObjectValue */ 1052 ) 1053 if err != nil { 1054 return nil, err 1055 } 1056 // The produced inverted expression will never be tight. This is because the 1057 // span expression produced will match all objects that contain at least one 1058 // of the keys, which does not guarantee they only contain the keys. 1059 // In other words, there may be false positives included that will need to 1060 // pass through an additional filter. 1061 // For example, the spans produced for '{"a": "b"}' will include both 1062 // '{"a": "b"}' and '{"a": "b", "c", "d"}', but the second row should be 1063 // filtered out since '{"a": "b", "c", "d"}' <@ '{"a": "b"}' is false. 1064 invertedExpr.SetNotTight() 1065 return invertedExpr, nil 1066 } 1067 1068 // EncodeExistsInvertedIndexSpans takes in a key prefix and returns the 1069 // spans that must be scanned in the inverted index to evaluate an exists (?) 1070 // predicate with the given JSON (i.e., find the objects/arrays in the index 1071 // that contain the given string as a key, or *are* the given string). 1072 // 1073 // The spans are returned in an inverted.SpanExpression, which represents the 1074 // set operations that must be applied on the spans read during execution. See 1075 // comments in the SpanExpression definition for details. 1076 // 1077 // The input inKey is prefixed to the keys in all returned spans. 1078 func EncodeExistsInvertedIndexSpans( 1079 b []byte, s string, 1080 ) (invertedExpr inverted.Expression, err error) { 1081 b = encoding.EncodeJSONAscending(b) 1082 js := jsonString(s) 1083 // Make an inverted expression that contains both arrays containing the input 1084 // string and objects with keys that are the input string. 1085 builder := NewArrayBuilder(1) 1086 builder.Add(js) 1087 arrayKeys, err := builder.Build().encodeInvertedIndexKeys(b) 1088 if err != nil { 1089 return nil, err 1090 } 1091 scalarKeys, err := js.encodeInvertedIndexKeys(b[:len(b):len(b)]) 1092 if err != nil { 1093 return nil, err 1094 } 1095 if len(arrayKeys) != 1 || len(scalarKeys) != 1 { 1096 return nil, errors.AssertionFailedf("unexpectedly found more than 1 inverted index child in single-key array") 1097 } 1098 arrayKey := arrayKeys[0] 1099 scalarKey := scalarKeys[0] 1100 // We pass end=true so that we don't get an extra separator at the end of the 1101 // key, which would exclude keys that point to non-scalars like non-empty 1102 // arrays or non-empty objects. 1103 // However, if we don't limit the span we send, we'd also get all paths 1104 // that have keys that begin with `s`, and not just keys that exactly equal 1105 // `s`, so we have to explicitly define the span as being from `s` to the end 1106 // of the`s+sep` prefix. 1107 objectKey := encoding.EncodeJSONKeyStringAscending(b[:len(b):len(b)], s, true /* end */) 1108 objectSpan := inverted.Span{ 1109 Start: objectKey, 1110 End: keysbase.PrefixEnd(encoding.AddJSONPathSeparator(objectKey)), 1111 } 1112 return inverted.Or( 1113 inverted.Or( 1114 inverted.ExprForSpan(inverted.MakeSingleValSpan(arrayKey), true /* tight */), 1115 inverted.ExprForSpan(objectSpan, true /* tight */), 1116 ), 1117 inverted.ExprForSpan(inverted.MakeSingleValSpan(scalarKey), true /* tight */), 1118 ), nil 1119 } 1120 1121 func (j jsonNull) encodeInvertedIndexKeys(b []byte) ([][]byte, error) { 1122 b = encoding.AddJSONPathTerminator(b) 1123 return [][]byte{encoding.EncodeNullAscending(b)}, nil 1124 } 1125 1126 func (j jsonNull) encodeContainingInvertedIndexSpans( 1127 b []byte, isRoot, isObjectValue bool, 1128 ) (inverted.Expression, error) { 1129 return encodeContainingInvertedIndexSpansFromLeaf(j, b, isRoot, isObjectValue) 1130 } 1131 1132 func (j jsonNull) encodeContainedInvertedIndexSpans( 1133 b []byte, isRoot, isObjectValue bool, 1134 ) (inverted.Expression, error) { 1135 invertedExpr, err := encodeContainedInvertedIndexSpansFromLeaf(j, b, isRoot) 1136 return invertedExpr, err 1137 } 1138 1139 func (jsonTrue) encodeInvertedIndexKeys(b []byte) ([][]byte, error) { 1140 b = encoding.AddJSONPathTerminator(b) 1141 return [][]byte{encoding.EncodeTrueAscending(b)}, nil 1142 } 1143 1144 func (j jsonTrue) encodeContainingInvertedIndexSpans( 1145 b []byte, isRoot, isObjectValue bool, 1146 ) (inverted.Expression, error) { 1147 return encodeContainingInvertedIndexSpansFromLeaf(j, b, isRoot, isObjectValue) 1148 } 1149 1150 func (j jsonTrue) encodeContainedInvertedIndexSpans( 1151 b []byte, isRoot, isObjectValue bool, 1152 ) (inverted.Expression, error) { 1153 invertedExpr, err := encodeContainedInvertedIndexSpansFromLeaf(j, b, isRoot) 1154 return invertedExpr, err 1155 } 1156 1157 func (jsonFalse) encodeInvertedIndexKeys(b []byte) ([][]byte, error) { 1158 b = encoding.AddJSONPathTerminator(b) 1159 return [][]byte{encoding.EncodeFalseAscending(b)}, nil 1160 } 1161 1162 func (j jsonFalse) encodeContainingInvertedIndexSpans( 1163 b []byte, isRoot, isObjectValue bool, 1164 ) (inverted.Expression, error) { 1165 return encodeContainingInvertedIndexSpansFromLeaf(j, b, isRoot, isObjectValue) 1166 } 1167 1168 func (j jsonFalse) encodeContainedInvertedIndexSpans( 1169 b []byte, isRoot, isObjectValue bool, 1170 ) (inverted.Expression, error) { 1171 invertedExpr, err := encodeContainedInvertedIndexSpansFromLeaf(j, b, isRoot) 1172 return invertedExpr, err 1173 } 1174 1175 func (j jsonString) encodeInvertedIndexKeys(b []byte) ([][]byte, error) { 1176 b = encoding.AddJSONPathTerminator(b) 1177 return [][]byte{encoding.EncodeStringAscending(b, string(j))}, nil 1178 } 1179 1180 func (j jsonString) encodeContainingInvertedIndexSpans( 1181 b []byte, isRoot, isObjectValue bool, 1182 ) (inverted.Expression, error) { 1183 return encodeContainingInvertedIndexSpansFromLeaf(j, b, isRoot, isObjectValue) 1184 } 1185 1186 func (j jsonString) encodeContainedInvertedIndexSpans( 1187 b []byte, isRoot, isObjectValue bool, 1188 ) (inverted.Expression, error) { 1189 invertedExpr, err := encodeContainedInvertedIndexSpansFromLeaf(j, b, isRoot) 1190 return invertedExpr, err 1191 } 1192 1193 func (j jsonNumber) encodeInvertedIndexKeys(b []byte) ([][]byte, error) { 1194 b = encoding.AddJSONPathTerminator(b) 1195 var dec = apd.Decimal(j) 1196 return [][]byte{encoding.EncodeDecimalAscending(b, &dec)}, nil 1197 } 1198 1199 func (j jsonNumber) encodeContainingInvertedIndexSpans( 1200 b []byte, isRoot, isObjectValue bool, 1201 ) (inverted.Expression, error) { 1202 return encodeContainingInvertedIndexSpansFromLeaf(j, b, isRoot, isObjectValue) 1203 } 1204 1205 func (j jsonNumber) encodeContainedInvertedIndexSpans( 1206 b []byte, isRoot, isObjectValue bool, 1207 ) (inverted.Expression, error) { 1208 invertedExpr, err := encodeContainedInvertedIndexSpansFromLeaf(j, b, isRoot) 1209 return invertedExpr, err 1210 } 1211 1212 func (j jsonArray) encodeInvertedIndexKeys(b []byte) ([][]byte, error) { 1213 // Checking for an empty array. 1214 if len(j) == 0 { 1215 return [][]byte{encoding.EncodeJSONEmptyArray(b)}, nil 1216 } 1217 1218 prefix := encoding.EncodeArrayAscending(b) 1219 var outKeys [][]byte 1220 for i := range j { 1221 children, err := j[i].encodeInvertedIndexKeys(prefix[:len(prefix):len(prefix)]) 1222 if err != nil { 1223 return nil, err 1224 } 1225 outKeys = append(outKeys, children...) 1226 } 1227 1228 // Deduplicate the entries, since arrays can have duplicates - we don't want 1229 // to emit duplicate keys from this method, as it's more expensive to 1230 // deduplicate keys via KV (which will actually write the keys) than to do 1231 // it now (just an in-memory sort and distinct). 1232 outKeys = uniq.UniquifyByteSlices(outKeys) 1233 return outKeys, nil 1234 } 1235 1236 func (j jsonArray) encodeContainingInvertedIndexSpans( 1237 b []byte, isRoot, isObjectValue bool, 1238 ) (invertedExpr inverted.Expression, err error) { 1239 // Checking for an empty array. 1240 if len(j) == 0 { 1241 return encodeContainingInvertedIndexSpansFromLeaf(j, b, isRoot, isObjectValue) 1242 } 1243 1244 prefix := encoding.EncodeArrayAscending(b) 1245 for i := range j { 1246 child, err := j[i].encodeContainingInvertedIndexSpans( 1247 prefix[:len(prefix):len(prefix)], false /* isRoot */, false, /* isObjectValue */ 1248 ) 1249 if err != nil { 1250 return nil, err 1251 } 1252 1253 if invertedExpr == nil { 1254 invertedExpr = child 1255 } else { 1256 invertedExpr = inverted.And(invertedExpr, child) 1257 } 1258 } 1259 1260 // If this array is not at the root and has more than one element, 1261 // we cannot produce tight spans. This is because we cannot rely on the keys 1262 // alone to determine whether the top level JSON is contained in another JSON. 1263 // For example, '[[1], [2]]' and '[[1, 2]]' have exactly the same keys, but 1264 // '[[1, 2]]' @> '[[1], [2]]' is true, while '[[1], [2]]' @> '[[1, 2]]' is 1265 // false. We will return an expression with Tight=false for the second case, 1266 // which will signal the need to filter out false positives. 1267 // 1268 // Note that in addition to checking that the original array had length > 1, 1269 // we also check that the spanExpr is an intersection. The inverted.And 1270 // function performs some deduplication, so it's possible that the original 1271 // array had duplicates that were removed, causing the intersection to be 1272 // removed. 1273 if spanExpr, ok := invertedExpr.(*inverted.SpanExpression); ok && 1274 !isRoot && j.Len() > 1 && spanExpr.Operator == inverted.SetIntersection { 1275 invertedExpr.SetNotTight() 1276 } 1277 1278 return invertedExpr, nil 1279 } 1280 1281 func (j jsonArray) encodeContainedInvertedIndexSpans( 1282 b []byte, isRoot, isObjectValue bool, 1283 ) (invertedExpr inverted.Expression, err error) { 1284 if !isObjectValue || len(j) == 0 { 1285 // The empty array should always be added to the spans, since it is contained 1286 // by everything. Empty array values are already accounted for when getting 1287 // the spans for a non-empty object value, so they should be excluded. 1288 emptyArrSpanExpr := inverted.ExprForSpan( 1289 inverted.MakeSingleValSpan(encoding.EncodeJSONEmptyArray(b[:len(b):len(b)])), false, /* tight */ 1290 ) 1291 emptyArrSpanExpr.Unique = true 1292 invertedExpr = emptyArrSpanExpr 1293 } 1294 1295 // If the given jsonArray is empty, we return the SpanExpression. 1296 if len(j) == 0 { 1297 return invertedExpr, nil 1298 } 1299 1300 prefix := encoding.EncodeArrayAscending(b[:len(b):len(b)]) 1301 for i := range j { 1302 childWithPrefix, err := j[i].encodeContainedInvertedIndexSpans( 1303 prefix[:len(prefix):len(prefix)], false /* isRoot */, false, /* isObjectValue */ 1304 ) 1305 if err != nil { 1306 return nil, err 1307 } 1308 if invertedExpr == nil { 1309 invertedExpr = childWithPrefix 1310 } else { 1311 invertedExpr = inverted.Or(invertedExpr, childWithPrefix) 1312 } 1313 1314 // Scalars inside the array should also be included in the spans 1315 // without the array prefix, since they are contained by the array. This 1316 // only applies to arrays not nested inside other arrays or objects, so 1317 // isRoot must be true. 1318 // For example, '1' <@ '[1]' is true, but '1' <@ '[[1]]' is false. 1319 // However for non-scalar types, the structure of nesting must match. 1320 // For example, '{"a": "a"}' <@ '[{"a": "a"}, {"b": "c"}]' is false, and 1321 // '[{"a": "a"}]' <@ '[{"a": "a"}, {"b": "c"}]' is true. 1322 // Therefore, we only include childWithoutPrefix for non-nested primitives 1323 // and empty arrays/objects. 1324 if isRoot && isEnd(j[i]) { 1325 childWithoutPrefix, err := j[i].encodeContainedInvertedIndexSpans( 1326 b[:len(b):len(b)], false /* isRoot */, false, /* isObjectValue */ 1327 ) 1328 if err != nil { 1329 return nil, err 1330 } 1331 invertedExpr = inverted.Or(invertedExpr, childWithoutPrefix) 1332 } 1333 } 1334 1335 return invertedExpr, nil 1336 } 1337 1338 func (j jsonObject) encodeInvertedIndexKeys(b []byte) ([][]byte, error) { 1339 // Checking for an empty object. 1340 if len(j) == 0 { 1341 return [][]byte{encoding.EncodeJSONEmptyObject(b)}, nil 1342 } 1343 1344 var outKeys [][]byte 1345 for i := range j { 1346 children, err := j[i].v.encodeInvertedIndexKeys(nil) 1347 if err != nil { 1348 return nil, err 1349 } 1350 1351 // We're trying to see if this is the end of the JSON path. If it is, then we don't want to 1352 // add an extra separator. 1353 end := isEnd(j[i].v) 1354 1355 for _, childBytes := range children { 1356 encodedKey := bytes.Join([][]byte{b, 1357 encoding.EncodeJSONKeyStringAscending(nil, string(j[i].k), end), 1358 childBytes}, nil) 1359 1360 outKeys = append(outKeys, encodedKey) 1361 } 1362 } 1363 return outKeys, nil 1364 } 1365 1366 func (j jsonObject) encodeContainingInvertedIndexSpans( 1367 b []byte, isRoot, isObjectValue bool, 1368 ) (invertedExpr inverted.Expression, err error) { 1369 if len(j) == 0 { 1370 return encodeContainingInvertedIndexSpansFromLeaf(j, b, isRoot, isObjectValue) 1371 } 1372 1373 for i := range j { 1374 // We're trying to see if this is the end of the JSON path. If it is, then 1375 // we don't want to add an extra separator. 1376 end := isEnd(j[i].v) 1377 1378 prefix := encoding.EncodeJSONKeyStringAscending(b[:len(b):len(b)], string(j[i].k), end) 1379 child, err := j[i].v.encodeContainingInvertedIndexSpans( 1380 prefix, false /* isRoot */, true, /* isObjectValue */ 1381 ) 1382 if err != nil { 1383 return nil, err 1384 } 1385 1386 if invertedExpr == nil { 1387 invertedExpr = child 1388 } else { 1389 invertedExpr = inverted.And(invertedExpr, child) 1390 } 1391 } 1392 1393 // If this object is not at the root and has more than one element, 1394 // we cannot produce tight spans. This is because we cannot rely on the keys 1395 // alone to determine whether the top level JSON is contained in another JSON. 1396 // For example, '[{"a": "b"}, {"c": "d"}]' and '[{"a": "b", "c": "d"}]' 1397 // have exactly the same keys, but 1398 // '[{"a": "b", "c": "d"}]' @> '[{"a": "b"}, {"c": "d"}]' is true, while 1399 // '[{"a": "b"}, {"c": "d"}]' @> '[{"a": "b", "c": "d"}]' is false. 1400 // We will return an expression with Tight=false for the second case, which 1401 // will signal the need to filter out false positives. 1402 if !isRoot && j.Len() > 1 { 1403 invertedExpr.SetNotTight() 1404 } 1405 1406 return invertedExpr, nil 1407 } 1408 1409 func (j jsonObject) encodeContainedInvertedIndexSpans( 1410 b []byte, isRoot, isObjectValue bool, 1411 ) (invertedExpr inverted.Expression, err error) { 1412 // The empty object should always be added to the spans, since it is contained 1413 // by everything. Empty object values are already accounted for when getting 1414 // the spans for a non-empty object value, so they should be excluded. 1415 if !isObjectValue || len(j) == 0 { 1416 emptyObjSpanExpr := inverted.ExprForSpan( 1417 inverted.MakeSingleValSpan(encoding.EncodeJSONEmptyObject(b[:len(b):len(b)])), false, /* tight */ 1418 ) 1419 emptyObjSpanExpr.Unique = true 1420 invertedExpr = emptyObjSpanExpr 1421 } 1422 // If the given jsonObject is empty, we return the SpanExpression. 1423 if len(j) == 0 { 1424 return invertedExpr, nil 1425 } 1426 1427 for i := range j { 1428 // We're trying to see if this is the end of the JSON path. If it is, then 1429 // we don't want to add an extra separator. 1430 end := isEnd(j[i].v) 1431 1432 prefix := encoding.EncodeJSONKeyStringAscending(b[:len(b):len(b)], string(j[i].k), end) 1433 child, err := j[i].v.encodeContainedInvertedIndexSpans( 1434 prefix, false /* isRoot */, true, /* isObjectValue */ 1435 ) 1436 if err != nil { 1437 return nil, err 1438 } 1439 1440 if invertedExpr == nil { 1441 invertedExpr = child 1442 } else { 1443 invertedExpr = inverted.Or(invertedExpr, child) 1444 } 1445 1446 // When we have a nested object or array, we want to include the empty 1447 // object or array span with the prefix. For example, '{"a": {"b": "c"}}' 1448 // should include the span for '{"a": {}}', and '{"a": [1]}' should include 1449 // '{"a": []}'. 1450 if !end { 1451 v := emptyJSONForType(j[i].v) 1452 if v != nil { 1453 prefixWithEnd := encoding.EncodeJSONKeyStringAscending(b[:len(b):len(b)], string(j[i].k), true) 1454 childWithEnd, err := v.encodeContainedInvertedIndexSpans( 1455 prefixWithEnd, false /* isRoot */, true, /* isObjectValue */ 1456 ) 1457 if err != nil { 1458 return nil, err 1459 } 1460 invertedExpr = inverted.Or(invertedExpr, childWithEnd) 1461 } 1462 } 1463 } 1464 1465 return invertedExpr, nil 1466 } 1467 1468 // isEnd returns true if a JSON value is the end of the JSON path. 1469 // If it is, then we don't want to add an extra separator when encoding 1470 // the keys. 1471 func isEnd(json JSON) bool { 1472 end := true 1473 switch t := json.(type) { 1474 case jsonArray, jsonObject: 1475 if t.Len() != 0 { 1476 end = false 1477 } 1478 1479 case *jsonEncoded: 1480 switch t.typ { 1481 case ArrayJSONType, ObjectJSONType: 1482 if t.containerLen != 0 { 1483 end = false 1484 } 1485 } 1486 } 1487 return end 1488 } 1489 1490 // emptyJSONForType returns either an empty JSON array or object corresponding 1491 // to the input JSON type. If the provided JSON is not an object or array, it 1492 // returns nil. 1493 func emptyJSONForType(json JSON) JSON { 1494 switch t := json.(type) { 1495 case jsonArray: 1496 return emptyJSONArray 1497 1498 case jsonObject: 1499 return emptyJSONObject 1500 1501 case *jsonEncoded: 1502 switch t.typ { 1503 case ArrayJSONType: 1504 return emptyJSONArray 1505 1506 case ObjectJSONType: 1507 return emptyJSONObject 1508 } 1509 } 1510 return nil 1511 } 1512 1513 // encodeContainingInvertedIndexSpansFromLeaf encodes the spans that must be 1514 // scanned in an inverted index to find the JSON objects that contain the given 1515 // leaf JSON value. A leaf is any scalar json such as '1', 'true', or 'null', 1516 // or an empty object or array. 1517 // 1518 // If isRoot is true, this function is being called at the root level of the 1519 // JSON hierarchy. If isObjectValue is true, the given JSON is the value of a 1520 // JSON object key. Note that isRoot and isObjectValue cannot both be true at 1521 // the same time. 1522 func encodeContainingInvertedIndexSpansFromLeaf( 1523 j JSON, b []byte, isRoot, isObjectValue bool, 1524 ) (invertedExpr inverted.Expression, err error) { 1525 keys, err := j.encodeInvertedIndexKeys(b) 1526 if err != nil { 1527 return nil, err 1528 } 1529 1530 invertedExpr = &inverted.SpanExpression{Tight: true} 1531 var unique bool 1532 prefix := b[:len(b):len(b)] 1533 if isObjectValue { 1534 if isRoot { 1535 return nil, errors.AssertionFailedf( 1536 "isObjectValue and isRoot should not both be true", 1537 ) 1538 } 1539 // Since isObjectValue is true, we know that at this point prefix contains 1540 // the encoded JSON object key. It does not yet contain the 1541 // escape + escapedJSONObjectKeyTerm separator, which is used to separate 1542 // the key from the value when the value is a non-empty object or array. 1543 // In order for '{}' and '[]' to match non-empty objects and arrays in the 1544 // switch that follows, we must add that separator. 1545 // 1546 // EncodeJSONKeyStringAscending(..., false) adds the separator, and 1547 // since the key is already encoded in prefix, we pass the empty string as 1548 // the key. 1549 prefix = encoding.EncodeJSONKeyStringAscending(prefix, "", false /* end */) 1550 } 1551 1552 switch t := j.(type) { 1553 case jsonArray: 1554 if t.Len() != 0 { 1555 return nil, errors.AssertionFailedf( 1556 "encodeContainingInvertedIndexSpansFromLeaf called on a non-empty jsonArray", 1557 ) 1558 } 1559 1560 // At this point, `keys` contains the empty array, which ensures that 1561 // '{"a": []}' matches '{"a": []}' and '[]' matches '[]'. This is correct 1562 // because a JSON object or array always contains itself. 1563 1564 // Add a key to cover all non-empty arrays. It is needed for JSON arrays 1565 // such as '[]' to match '[1]' and '{"a": []}' to match '{"a": [1]}' 1566 // (i.e., '[1]' @> '[]' and '{"a": [1]}' @> '{"a": []}' are true). 1567 // EncodeArrayAscending generates the prefix that is used for all non-empty 1568 // arrays. 1569 // 1570 // The span that will be generated for this key can have duplicate PKs, so 1571 // unique=false. 1572 keys = append(keys, encoding.EncodeArrayAscending(prefix)) 1573 1574 case jsonObject: 1575 if t.Len() != 0 { 1576 return nil, errors.AssertionFailedf( 1577 "encodeContainingInvertedIndexSpansFromLeaf called on a non-empty jsonObject", 1578 ) 1579 } 1580 1581 // At this point, `keys` contains the empty object, which ensures that 1582 // '{"a": {}}' matches '{"a": {}}' and '{}' matches '{}'. This is correct 1583 // because a JSON object always contains itself. This key will be converted 1584 // into a span below. 1585 1586 // Add a span to cover keys for non-empty objects. It is needed for 1587 // JSON objects such as '{}' to match '{"a": "b"}', but not '[1]', 1588 // and '{"a": {}}' to match '{"a": {"b": "c"}}', but not '{"a": [1]}' or 1589 // ["a"]. (i.e., '{"a": "b"}' @> '{}' and '{"a": {"b": "c"}}' @> '{"a": {}}' 1590 // are true, but '[1]' @> '{}', '{"a": [1]}' @> '{"a": {}}', and 1591 // '["a"]' @> '{"a": {}}' are false) 1592 // 1593 // This span can have duplicate PKs, so unique=false. 1594 invertedExpr = inverted.Or(invertedExpr, inverted.ExprForSpan(inverted.Span{ 1595 // EncodeJSONObjectSpanStartAscending generates the first possible value 1596 // for JSON objects. 1597 Start: inverted.EncVal(encoding.EncodeJSONObjectSpanStartAscending(prefix)), 1598 // This end key is equal to jsonInvertedIndex + 1. 1599 End: inverted.EncVal(keysbase.PrefixEnd(prefix)), 1600 }, true /* tight */)) 1601 1602 default: 1603 if isRoot { 1604 // If we find a scalar on the right side of the @> operator it means that 1605 // we need to find both matching scalars and arrays that contain that value. 1606 // In order to do this we generate two logical spans, one for the original 1607 // scalar (which we have already done above) and one for arrays containing 1608 // the scalar. This is *only* the case if the scalar is the root element. 1609 // For example, '1' @> '1' and '[1]' @> '1' are both true, but 1610 // '[1]' @> '[1]' is true, while '[[1]]' @> '[1]' is false. 1611 arr := NewArrayBuilder(1) 1612 arr.Add(j) 1613 jArr := arr.Build() 1614 arrKeys, err := jArr.encodeInvertedIndexKeys(prefix) 1615 if err != nil { 1616 return nil, err 1617 } 1618 keys = append(keys, arrKeys...) 1619 1620 // Even though we now have two spans, we can guarantee no PK duplicates 1621 // because in JSON something can either be an array or a scalar -- not 1622 // both. So the spans are guaranteed not to overlap when mapped onto the 1623 // primary key space. Therefore there won't be any duplicate primary keys 1624 // when we retrieve rows for both sets, and setting unique to true 1625 // below is valid. 1626 } 1627 1628 // We can guarantee that there will be no duplicate primary keys produced 1629 // for paths ending in a scalar, regardless of whether or not this is the 1630 // root. 1631 unique = true 1632 } 1633 1634 for _, key := range keys { 1635 invertedExpr = inverted.Or(invertedExpr, inverted.ExprForSpan( 1636 inverted.MakeSingleValSpan(inverted.EncVal(key)), true, /* tight */ 1637 )) 1638 } 1639 if spanExpr, ok := invertedExpr.(*inverted.SpanExpression); ok { 1640 spanExpr.Unique = unique 1641 } 1642 1643 return invertedExpr, nil 1644 } 1645 1646 // encodeContainedInvertedIndexSpansFromLeaf encodes the spans that must be 1647 // scanned in an inverted index to find the JSON objects that are contained by 1648 // the given leaf JSON value. A leaf is any scalar json such as '1', 'true', or 1649 // 'null'. The resulting span should only include the key for the leaf. 1650 // Unlike encodeContainingInvertedIndexSpansFromLeaf, empty JSON objects and 1651 // array spans are not encoded by this function. 1652 // 1653 // If isRoot is true, this function is being called at the root level of the 1654 // JSON hierarchy. 1655 func encodeContainedInvertedIndexSpansFromLeaf( 1656 j JSON, b []byte, isRoot bool, 1657 ) (invertedExpr inverted.Expression, err error) { 1658 keys, err := j.encodeInvertedIndexKeys(b) 1659 if err != nil { 1660 return nil, err 1661 } 1662 1663 invertedExpr = &inverted.SpanExpression{Tight: false} 1664 var unique bool 1665 switch j.(type) { 1666 case jsonArray: 1667 return nil, errors.AssertionFailedf( 1668 "encodeContainedInvertedIndexSpansFromLeaf called on a jsonArray", 1669 ) 1670 1671 case jsonObject: 1672 return nil, errors.AssertionFailedf( 1673 "encodeContainedInvertedIndexSpansFromLeaf called on a jsonObject", 1674 ) 1675 1676 default: 1677 // The leaf is a scalar, so we only want the single span that contains the 1678 // key for it, because the scalar can only contain itself. 1679 if isRoot { 1680 // We can guarantee that there will not be duplicate primary keys produced 1681 // for paths ending in a scalar if it is the root. 1682 unique = true 1683 } 1684 for _, key := range keys { 1685 invertedExpr = inverted.Or(invertedExpr, inverted.ExprForSpan( 1686 inverted.MakeSingleValSpan(key), false, /* tight */ 1687 )) 1688 } 1689 if spanExpr, ok := invertedExpr.(*inverted.SpanExpression); ok { 1690 spanExpr.Unique = unique 1691 } 1692 1693 return invertedExpr, nil 1694 } 1695 } 1696 1697 // NumInvertedIndexEntries returns the number of inverted index entries that 1698 // would be created for the given JSON value. Since identical elements of an 1699 // array are encoded identically in the inverted index, the total number of 1700 // distinct index entries may be less than the total number of paths. 1701 func NumInvertedIndexEntries(j JSON) (int, error) { 1702 return j.numInvertedIndexEntries() 1703 } 1704 1705 func (j jsonNull) numInvertedIndexEntries() (int, error) { 1706 return 1, nil 1707 } 1708 func (jsonTrue) numInvertedIndexEntries() (int, error) { 1709 return 1, nil 1710 } 1711 func (jsonFalse) numInvertedIndexEntries() (int, error) { 1712 return 1, nil 1713 } 1714 func (j jsonString) numInvertedIndexEntries() (int, error) { 1715 return 1, nil 1716 } 1717 func (j jsonNumber) numInvertedIndexEntries() (int, error) { 1718 return 1, nil 1719 } 1720 func (j jsonArray) numInvertedIndexEntries() (int, error) { 1721 if len(j) == 0 { 1722 return 1, nil 1723 } 1724 keys, err := j.encodeInvertedIndexKeys(nil) 1725 if err != nil { 1726 return 0, err 1727 } 1728 return len(keys), nil 1729 } 1730 1731 func (j jsonObject) numInvertedIndexEntries() (int, error) { 1732 if len(j) == 0 { 1733 return 1, nil 1734 } 1735 count := 0 1736 for _, kv := range j { 1737 n, err := kv.v.numInvertedIndexEntries() 1738 if err != nil { 1739 return 0, err 1740 } 1741 count += n 1742 } 1743 return count, nil 1744 } 1745 1746 // AllPaths returns a slice of new JSON documents, each a path to a leaf 1747 // through the input. Note that leaves include the empty object and array 1748 // in addition to scalars. 1749 func AllPaths(j JSON) ([]JSON, error) { 1750 return j.allPaths() 1751 } 1752 1753 func (j jsonNull) allPaths() ([]JSON, error) { 1754 return []JSON{j}, nil 1755 } 1756 1757 func (j jsonTrue) allPaths() ([]JSON, error) { 1758 return []JSON{j}, nil 1759 } 1760 1761 func (j jsonFalse) allPaths() ([]JSON, error) { 1762 return []JSON{j}, nil 1763 } 1764 1765 func (j jsonString) allPaths() ([]JSON, error) { 1766 return []JSON{j}, nil 1767 } 1768 1769 func (j jsonNumber) allPaths() ([]JSON, error) { 1770 return []JSON{j}, nil 1771 } 1772 1773 func (j jsonArray) allPaths() ([]JSON, error) { 1774 if len(j) == 0 { 1775 return []JSON{j}, nil 1776 } 1777 ret := make([]JSON, 0, len(j)) 1778 for i := range j { 1779 paths, err := j[i].allPaths() 1780 if err != nil { 1781 return nil, err 1782 } 1783 for _, path := range paths { 1784 ret = append(ret, jsonArray{path}) 1785 } 1786 } 1787 return ret, nil 1788 } 1789 1790 func (j jsonObject) allPaths() ([]JSON, error) { 1791 if len(j) == 0 { 1792 return []JSON{j}, nil 1793 } 1794 ret := make([]JSON, 0, len(j)) 1795 for i := range j { 1796 paths, err := j[i].v.allPaths() 1797 if err != nil { 1798 return nil, err 1799 } 1800 for _, path := range paths { 1801 ret = append(ret, jsonObject{jsonKeyValuePair{k: j[i].k, v: path}}) 1802 } 1803 } 1804 return ret, nil 1805 } 1806 1807 // FromSpatialObject transforms a SpatialObject into the json.JSON type. 1808 func FromSpatialObject(so geopb.SpatialObject, numDecimalDigits int) (JSON, error) { 1809 j, err := geo.SpatialObjectToGeoJSON(so, numDecimalDigits, geo.SpatialObjectToGeoJSONFlagZero) 1810 if err != nil { 1811 return nil, err 1812 } 1813 return ParseJSON(string(j)) 1814 } 1815 1816 // FromDecimal returns a JSON value given a apd.Decimal. 1817 func FromDecimal(v apd.Decimal) JSON { 1818 return jsonNumber(v) 1819 } 1820 1821 // FromNumber returns a JSON value given a json.Number. 1822 func FromNumber(v json.Number) (JSON, error) { 1823 // The JSON decoder has already verified that the string `v` represents a 1824 // valid JSON number, and the set of valid JSON numbers is a [proper] subset 1825 // of the set of valid apd.Decimal values. 1826 dec := apd.Decimal{} 1827 _, _, err := dec.SetString(string(v)) 1828 return jsonNumber(dec), err 1829 } 1830 1831 // FromString returns a JSON value given a string. 1832 func FromString(v string) JSON { 1833 return jsonString(v) 1834 } 1835 1836 // FromBool returns a JSON value given a bool. 1837 func FromBool(v bool) JSON { 1838 if v { 1839 return TrueJSONValue 1840 } 1841 return FalseJSONValue 1842 } 1843 1844 func fromArray(v []interface{}) (JSON, error) { 1845 elems := make([]JSON, len(v)) 1846 for i := range v { 1847 var err error 1848 elems[i], err = MakeJSON(v[i]) 1849 if err != nil { 1850 return nil, err 1851 } 1852 } 1853 return jsonArray(elems), nil 1854 } 1855 1856 func fromMap(v map[string]interface{}) (JSON, error) { 1857 keys := make([]string, len(v)) 1858 i := 0 1859 for k := range v { 1860 keys[i] = k 1861 i++ 1862 } 1863 sort.Strings(keys) 1864 result := make([]jsonKeyValuePair, len(v)) 1865 for i := range keys { 1866 v, err := MakeJSON(v[keys[i]]) 1867 if err != nil { 1868 return nil, err 1869 } 1870 result[i] = jsonKeyValuePair{ 1871 k: jsonString(keys[i]), 1872 v: v, 1873 } 1874 } 1875 return jsonObject(result), nil 1876 } 1877 1878 // FromInt returns a JSON value given a int. 1879 func FromInt(v int) JSON { 1880 dec := apd.Decimal{} 1881 dec.SetInt64(int64(v)) 1882 return jsonNumber(dec) 1883 } 1884 1885 // FromInt64 returns a JSON value given a int64. 1886 func FromInt64(v int64) JSON { 1887 dec := apd.Decimal{} 1888 dec.SetInt64(v) 1889 return jsonNumber(dec) 1890 } 1891 1892 // FromFloat64 returns a JSON value given a float64. 1893 func FromFloat64(v float64) (JSON, error) { 1894 dec := apd.Decimal{} 1895 _, err := dec.SetFloat64(v) 1896 if err != nil { 1897 return nil, err 1898 } 1899 return jsonNumber(dec), nil 1900 } 1901 1902 // MakeJSON returns a JSON value given a Go-style representation of JSON. 1903 // * JSON null is Go `nil`, 1904 // * JSON true is Go `true`, 1905 // * JSON false is Go `false`, 1906 // * JSON numbers are json.Number | int | int64 | float64, 1907 // * JSON string is a Go string, 1908 // * JSON array is a Go []interface{}, 1909 // * JSON object is a Go map[string]interface{}. 1910 func MakeJSON(d interface{}) (JSON, error) { 1911 switch v := d.(type) { 1912 case json.Number: 1913 return FromNumber(v) 1914 case string: 1915 return FromString(v), nil 1916 case bool: 1917 return FromBool(v), nil 1918 case nil: 1919 return NullJSONValue, nil 1920 case []interface{}: 1921 return fromArray(v) 1922 case map[string]interface{}: 1923 return fromMap(v) 1924 // The below are not used by ParseJSON, but are provided for ease-of-use when 1925 // constructing Datums. 1926 case int: 1927 return FromInt(v), nil 1928 case int64: 1929 return FromInt64(v), nil 1930 case float64: 1931 return FromFloat64(v) 1932 case JSON: 1933 // If we get passed a JSON, just accept it. This is useful in cases like the 1934 // random JSON generator. 1935 return v, nil 1936 } 1937 return nil, errors.AssertionFailedf("unknown value type passed to MakeJSON: %T", d) 1938 } 1939 1940 // This value was determined through some rough experimental results as a good 1941 // place to start doing binary search over a linear scan. 1942 const bsearchCutoff = 20 1943 1944 func findPairIndexByKey(j jsonObject, key string) (int, bool) { 1945 // For small objects, the overhead of binary search is significant and so 1946 // it's faster to just do a linear scan. 1947 var i int 1948 if len(j) < bsearchCutoff { 1949 for i = range j { 1950 if string(j[i].k) >= key { 1951 break 1952 } 1953 } 1954 } else { 1955 i = sort.Search(len(j), func(i int) bool { return string(j[i].k) >= key }) 1956 } 1957 if i < len(j) && string(j[i].k) == key { 1958 return i, true 1959 } 1960 return -1, false 1961 } 1962 1963 func (j jsonObject) FetchValKey(key string) (JSON, error) { 1964 i, ok := findPairIndexByKey(j, key) 1965 if ok { 1966 return j[i].v, nil 1967 } 1968 return nil, nil 1969 } 1970 1971 func (j jsonNull) EncodeForwardIndex(buf []byte, dir encoding.Direction) ([]byte, error) { 1972 buf = encoding.EncodeJSONNullKeyMarker(buf, dir) 1973 return buf, nil 1974 } 1975 1976 func (j jsonString) EncodeForwardIndex(buf []byte, dir encoding.Direction) ([]byte, error) { 1977 buf = encoding.EncodeJSONStringKeyMarker(buf, dir) 1978 1979 switch dir { 1980 case encoding.Ascending: 1981 buf = encoding.EncodeStringAscending(buf, string(j)) 1982 case encoding.Descending: 1983 buf = encoding.EncodeStringDescending(buf, string(j)) 1984 default: 1985 return nil, errors.AssertionFailedf("invalid direction") 1986 } 1987 buf = encoding.EncodeJSONKeyTerminator(buf, dir) 1988 return buf, nil 1989 } 1990 1991 func (j jsonNumber) EncodeForwardIndex(buf []byte, dir encoding.Direction) ([]byte, error) { 1992 buf = encoding.EncodeJSONNumberKeyMarker(buf, dir) 1993 var dec = apd.Decimal(j) 1994 switch dir { 1995 case encoding.Ascending: 1996 buf = encoding.EncodeDecimalAscending(buf, &dec) 1997 case encoding.Descending: 1998 buf = encoding.EncodeDecimalDescending(buf, &dec) 1999 default: 2000 return nil, errors.AssertionFailedf("invalid direction") 2001 } 2002 buf = encoding.EncodeJSONKeyTerminator(buf, dir) 2003 return buf, nil 2004 } 2005 2006 func (j jsonFalse) EncodeForwardIndex(buf []byte, dir encoding.Direction) ([]byte, error) { 2007 buf = encoding.EncodeJSONFalseKeyMarker(buf, dir) 2008 return buf, nil 2009 } 2010 2011 func (j jsonTrue) EncodeForwardIndex(buf []byte, dir encoding.Direction) ([]byte, error) { 2012 buf = encoding.EncodeJSONTrueKeyMarker(buf, dir) 2013 return buf, nil 2014 } 2015 2016 func (j jsonArray) EncodeForwardIndex(buf []byte, dir encoding.Direction) ([]byte, error) { 2017 buf = encoding.EncodeJSONArrayKeyMarker(buf, dir, int64(len(j))) 2018 buf = encoding.EncodeJSONValueLength(buf, dir, int64(len(j))) 2019 2020 var err error 2021 for _, a := range j { 2022 buf, err = a.EncodeForwardIndex(buf, dir) 2023 if err != nil { 2024 return nil, err 2025 } 2026 } 2027 buf = encoding.EncodeJSONKeyTerminator(buf, dir) 2028 return buf, nil 2029 } 2030 2031 func (j jsonObject) EncodeForwardIndex(buf []byte, dir encoding.Direction) ([]byte, error) { 2032 buf = encoding.EncodeJSONObjectKeyMarker(buf, dir) 2033 buf = encoding.EncodeJSONValueLength(buf, dir, int64(len(j))) 2034 2035 if buildutil.CrdbTestBuild { 2036 if ordered := j.AreKeysSorted(); !ordered { 2037 return nil, errors.AssertionFailedf("unexpectedly unordered keys in jsonObject %s", j) 2038 } 2039 } 2040 2041 var err error 2042 for _, a := range j { 2043 buf, err = a.k.EncodeForwardIndex(buf, dir) 2044 if err != nil { 2045 return nil, err 2046 } 2047 buf, err = a.v.EncodeForwardIndex(buf, dir) 2048 if err != nil { 2049 return nil, err 2050 } 2051 } 2052 buf = encoding.EncodeJSONKeyTerminator(buf, dir) 2053 return buf, nil 2054 } 2055 2056 func (jsonNull) FetchValKey(string) (JSON, error) { return nil, nil } 2057 func (jsonTrue) FetchValKey(string) (JSON, error) { return nil, nil } 2058 func (jsonFalse) FetchValKey(string) (JSON, error) { return nil, nil } 2059 func (jsonString) FetchValKey(string) (JSON, error) { return nil, nil } 2060 func (jsonNumber) FetchValKey(string) (JSON, error) { return nil, nil } 2061 func (jsonArray) FetchValKey(string) (JSON, error) { return nil, nil } 2062 2063 func fetchValIdxForScalar(j JSON, idx int) JSON { 2064 // The 0'th element (and -1'st element with negative indexing) of a scalar 2065 // JSON value is the scalar itself. This effectively treats scalar values as 2066 // single element arrays. 2067 if idx == 0 || idx == -1 { 2068 return j 2069 } 2070 return nil 2071 } 2072 2073 func (j jsonNull) FetchValIdx(idx int) (JSON, error) { return fetchValIdxForScalar(j, idx), nil } 2074 func (j jsonTrue) FetchValIdx(idx int) (JSON, error) { return fetchValIdxForScalar(j, idx), nil } 2075 func (j jsonFalse) FetchValIdx(idx int) (JSON, error) { return fetchValIdxForScalar(j, idx), nil } 2076 func (j jsonString) FetchValIdx(idx int) (JSON, error) { return fetchValIdxForScalar(j, idx), nil } 2077 func (j jsonNumber) FetchValIdx(idx int) (JSON, error) { return fetchValIdxForScalar(j, idx), nil } 2078 2079 func (j jsonArray) FetchValIdx(idx int) (JSON, error) { 2080 if idx < 0 { 2081 idx = len(j) + idx 2082 } 2083 if idx >= 0 && idx < len(j) { 2084 return j[idx], nil 2085 } 2086 return nil, nil 2087 } 2088 2089 func (jsonObject) FetchValIdx(int) (JSON, error) { return nil, nil } 2090 2091 // FetchPath implements the #> operator. 2092 func FetchPath(j JSON, path []string) (JSON, error) { 2093 var next JSON 2094 var err error 2095 for _, v := range path { 2096 next, err = j.FetchValKeyOrIdx(v) 2097 if next == nil { 2098 return nil, nil 2099 } 2100 if err != nil { 2101 return nil, err 2102 } 2103 j = next 2104 } 2105 return j, nil 2106 } 2107 2108 var errCannotSetPathInScalar = pgerror.WithCandidateCode(errors.New("cannot set path in scalar"), pgcode.InvalidParameterValue) 2109 2110 // setValKeyOrIdx sets a key or index within a JSON object or array. If the 2111 // provided value is neither an object or array the value is returned 2112 // unchanged. 2113 // If the value is an object which does not have the provided key and 2114 // createMissing is true, the key is inserted into the object with the value `to`. 2115 // If the value is an array and the provided index is negative, it counts from the back of the array. 2116 // Further, if the value is an array, and createMissing is true: 2117 // * if the provided index points to before the start of the array, `to` is prepended to the array. 2118 // * if the provided index points to after the end of the array, `to` is appended to the array. 2119 func setValKeyOrIdx(j JSON, key string, to JSON, createMissing bool) (JSON, error) { 2120 switch v := j.(type) { 2121 case *jsonEncoded: 2122 n, err := v.shallowDecode() 2123 if err != nil { 2124 return nil, err 2125 } 2126 return setValKeyOrIdx(n, key, to, createMissing) 2127 case jsonObject: 2128 return v.SetKey(key, to, createMissing) 2129 case jsonArray: 2130 idx, err := strconv.Atoi(key) 2131 if err != nil { 2132 return nil, err 2133 } 2134 if idx < 0 { 2135 idx = len(v) + idx 2136 } 2137 if !createMissing && (idx < 0 || idx >= len(v)) { 2138 return v, nil 2139 } 2140 var result jsonArray 2141 if idx < 0 { 2142 result = make(jsonArray, len(v)+1) 2143 copy(result[1:], v) 2144 result[0] = to 2145 } else if idx >= len(v) { 2146 result = make(jsonArray, len(v)+1) 2147 copy(result, v) 2148 result[len(result)-1] = to 2149 } else { 2150 result = make(jsonArray, len(v)) 2151 copy(result, v) 2152 result[idx] = to 2153 } 2154 return result, nil 2155 } 2156 return j, nil 2157 } 2158 2159 // DeepSet sets a path to a value in a JSON document. 2160 // Largely follows the same semantics as setValKeyOrIdx, but with a path. 2161 // Implements the jsonb_set builtin. 2162 func DeepSet(j JSON, path []string, to JSON, createMissing bool) (JSON, error) { 2163 if j.isScalar() { 2164 return nil, errCannotSetPathInScalar 2165 } 2166 return deepSet(j, path, to, createMissing) 2167 } 2168 2169 func deepSet(j JSON, path []string, to JSON, createMissing bool) (JSON, error) { 2170 switch len(path) { 2171 case 0: 2172 return j, nil 2173 case 1: 2174 return setValKeyOrIdx(j, path[0], to, createMissing) 2175 default: 2176 switch v := j.(type) { 2177 case *jsonEncoded: 2178 n, err := v.shallowDecode() 2179 if err != nil { 2180 return nil, err 2181 } 2182 return deepSet(n, path, to, createMissing) 2183 default: 2184 fetched, err := j.FetchValKeyOrIdx(path[0]) 2185 if err != nil { 2186 return nil, err 2187 } 2188 if fetched == nil { 2189 return j, nil 2190 } 2191 sub, err := deepSet(fetched, path[1:], to, createMissing) 2192 if err != nil { 2193 return nil, err 2194 } 2195 return setValKeyOrIdx(j, path[0], sub, createMissing) 2196 } 2197 } 2198 } 2199 2200 var errCannotReplaceExistingKey = pgerror.WithCandidateCode(errors.New("cannot replace existing key"), pgcode.InvalidParameterValue) 2201 2202 func insertValKeyOrIdx(j JSON, key string, newVal JSON, insertAfter bool) (JSON, error) { 2203 switch v := j.(type) { 2204 case *jsonEncoded: 2205 n, err := v.shallowDecode() 2206 if err != nil { 2207 return nil, err 2208 } 2209 return insertValKeyOrIdx(n, key, newVal, insertAfter) 2210 case jsonObject: 2211 result, err := v.SetKey(key, newVal, true) 2212 if err != nil { 2213 return nil, err 2214 } 2215 if len(result) == len(v) { 2216 return nil, errCannotReplaceExistingKey 2217 } 2218 return result, nil 2219 case jsonArray: 2220 idx, err := strconv.Atoi(key) 2221 if err != nil { 2222 return nil, err 2223 } 2224 if idx < 0 { 2225 idx = len(v) + idx 2226 } 2227 if insertAfter { 2228 idx++ 2229 } 2230 2231 var result = make(jsonArray, len(v)+1) 2232 if idx <= 0 { 2233 copy(result[1:], v) 2234 result[0] = newVal 2235 } else if idx >= len(v) { 2236 copy(result, v) 2237 result[len(result)-1] = newVal 2238 } else { 2239 copy(result[:idx], v[:idx]) 2240 copy(result[idx+1:], v[idx:]) 2241 result[idx] = newVal 2242 } 2243 return result, nil 2244 } 2245 return j, nil 2246 } 2247 2248 // DeepInsert inserts a value at a path in a JSON document. 2249 // Implements the jsonb_insert builtin. 2250 func DeepInsert(j JSON, path []string, to JSON, insertAfter bool) (JSON, error) { 2251 if j.isScalar() { 2252 return nil, errCannotSetPathInScalar 2253 } 2254 return deepInsert(j, path, to, insertAfter) 2255 } 2256 2257 func deepInsert(j JSON, path []string, to JSON, insertAfter bool) (JSON, error) { 2258 switch len(path) { 2259 case 0: 2260 return j, nil 2261 case 1: 2262 return insertValKeyOrIdx(j, path[0], to, insertAfter) 2263 default: 2264 switch v := j.(type) { 2265 case *jsonEncoded: 2266 n, err := v.shallowDecode() 2267 if err != nil { 2268 return nil, err 2269 } 2270 return deepInsert(n, path, to, insertAfter) 2271 default: 2272 fetched, err := j.FetchValKeyOrIdx(path[0]) 2273 if err != nil { 2274 return nil, err 2275 } 2276 if fetched == nil { 2277 return j, nil 2278 } 2279 sub, err := deepInsert(fetched, path[1:], to, insertAfter) 2280 if err != nil { 2281 return nil, err 2282 } 2283 return setValKeyOrIdx(j, path[0], sub, true) 2284 } 2285 } 2286 } 2287 2288 func (j jsonObject) FetchValKeyOrIdx(key string) (JSON, error) { 2289 return j.FetchValKey(key) 2290 } 2291 2292 func (j jsonArray) FetchValKeyOrIdx(key string) (JSON, error) { 2293 idx, err := strconv.Atoi(key) 2294 if err != nil { 2295 // We shouldn't return this error because it means we couldn't parse the 2296 // number, meaning it was a string and that just means we can't find the 2297 // value in an array. 2298 return nil, nil //nolint:returnerrcheck 2299 } 2300 return j.FetchValIdx(idx) 2301 } 2302 2303 func (jsonNull) FetchValKeyOrIdx(string) (JSON, error) { return nil, nil } 2304 func (jsonTrue) FetchValKeyOrIdx(string) (JSON, error) { return nil, nil } 2305 func (jsonFalse) FetchValKeyOrIdx(string) (JSON, error) { return nil, nil } 2306 func (jsonString) FetchValKeyOrIdx(string) (JSON, error) { return nil, nil } 2307 func (jsonNumber) FetchValKeyOrIdx(string) (JSON, error) { return nil, nil } 2308 2309 var errCannotDeleteFromScalar = pgerror.WithCandidateCode(errors.New("cannot delete from scalar"), pgcode.InvalidParameterValue) 2310 var errCannotDeleteFromObject = pgerror.WithCandidateCode(errors.New("cannot delete from object using integer index"), pgcode.InvalidParameterValue) 2311 2312 func (j jsonObject) SetKey(key string, to JSON, createMissing bool) (jsonObject, error) { 2313 result := make(jsonObject, 0, len(j)+1) 2314 curIdx := 0 2315 2316 for curIdx < len(j) && string(j[curIdx].k) < key { 2317 result = append(result, j[curIdx]) 2318 curIdx++ 2319 } 2320 2321 keyAlreadyExists := curIdx < len(j) && string(j[curIdx].k) == key 2322 if createMissing || keyAlreadyExists { 2323 result = append(result, jsonKeyValuePair{ 2324 k: jsonString(key), 2325 v: to, 2326 }) 2327 } 2328 if keyAlreadyExists { 2329 curIdx++ 2330 } 2331 2332 for curIdx < len(j) { 2333 result = append(result, j[curIdx]) 2334 curIdx++ 2335 } 2336 2337 return result, nil 2338 } 2339 2340 func (j jsonArray) RemoveString(s string) (JSON, bool, error) { 2341 b := NewArrayBuilder(j.Len()) 2342 removed := false 2343 for _, el := range j { 2344 // We want to remove only elements of string type. 2345 if el.Type() == StringJSONType { 2346 t, err := el.AsText() 2347 if err != nil { 2348 return nil, false, err 2349 } 2350 if t == nil { 2351 return nil, false, errors.AssertionFailedf("StringJSONType should not be nil here") 2352 } 2353 if *t != s { 2354 b.Add(el) 2355 } else { 2356 removed = true 2357 } 2358 } else { 2359 b.Add(el) 2360 } 2361 } 2362 if removed { 2363 return b.Build(), removed, nil 2364 } 2365 return j, false, nil 2366 } 2367 2368 func (j jsonObject) RemoveString(s string) (JSON, bool, error) { 2369 idx, ok := findPairIndexByKey(j, s) 2370 if !ok { 2371 return j, false, nil 2372 } 2373 2374 newVal := make([]jsonKeyValuePair, len(j)-1) 2375 copy(newVal, j[:idx]) 2376 copy(newVal[idx:], j[idx+1:]) 2377 return jsonObject(newVal), true, nil 2378 } 2379 2380 func (jsonNull) RemoveString(string) (JSON, bool, error) { 2381 return nil, false, errCannotDeleteFromScalar 2382 } 2383 func (jsonTrue) RemoveString(string) (JSON, bool, error) { 2384 return nil, false, errCannotDeleteFromScalar 2385 } 2386 func (jsonFalse) RemoveString(string) (JSON, bool, error) { 2387 return nil, false, errCannotDeleteFromScalar 2388 } 2389 func (jsonString) RemoveString(string) (JSON, bool, error) { 2390 return nil, false, errCannotDeleteFromScalar 2391 } 2392 func (jsonNumber) RemoveString(string) (JSON, bool, error) { 2393 return nil, false, errCannotDeleteFromScalar 2394 } 2395 2396 func (j jsonArray) RemoveIndex(idx int) (JSON, bool, error) { 2397 if idx < 0 { 2398 idx = len(j) + idx 2399 } 2400 if idx < 0 || idx >= len(j) { 2401 return j, false, nil 2402 } 2403 result := make(jsonArray, len(j)-1) 2404 for i := 0; i < idx; i++ { 2405 result[i] = j[i] 2406 } 2407 for i := idx + 1; i < len(j); i++ { 2408 result[i-1] = j[i] 2409 } 2410 return result, true, nil 2411 } 2412 2413 func (j jsonObject) RemoveIndex(int) (JSON, bool, error) { 2414 return nil, false, errCannotDeleteFromObject 2415 } 2416 2417 func (jsonNull) RemoveIndex(int) (JSON, bool, error) { return nil, false, errCannotDeleteFromScalar } 2418 func (jsonTrue) RemoveIndex(int) (JSON, bool, error) { return nil, false, errCannotDeleteFromScalar } 2419 func (jsonFalse) RemoveIndex(int) (JSON, bool, error) { return nil, false, errCannotDeleteFromScalar } 2420 func (jsonString) RemoveIndex(int) (JSON, bool, error) { return nil, false, errCannotDeleteFromScalar } 2421 func (jsonNumber) RemoveIndex(int) (JSON, bool, error) { return nil, false, errCannotDeleteFromScalar } 2422 2423 var errInvalidConcat = pgerror.WithCandidateCode(errors.New("invalid concatenation of jsonb objects"), pgcode.InvalidParameterValue) 2424 2425 func scalarConcat(left, other JSON) (JSON, error) { 2426 switch other.Type() { 2427 case ArrayJSONType: 2428 decoded, err := other.tryDecode() 2429 if err != nil { 2430 return nil, err 2431 } 2432 right := decoded.(jsonArray) 2433 result := make(jsonArray, len(right)+1) 2434 result[0] = left 2435 for i := range right { 2436 result[i+1] = right[i] 2437 } 2438 return result, nil 2439 case ObjectJSONType: 2440 return nil, errInvalidConcat 2441 default: 2442 return jsonArray{left, other}, nil 2443 } 2444 } 2445 2446 func (jsonNull) Concat(other JSON) (JSON, error) { return scalarConcat(NullJSONValue, other) } 2447 func (jsonTrue) Concat(other JSON) (JSON, error) { return scalarConcat(TrueJSONValue, other) } 2448 func (jsonFalse) Concat(other JSON) (JSON, error) { return scalarConcat(FalseJSONValue, other) } 2449 func (j jsonString) Concat(other JSON) (JSON, error) { return scalarConcat(j, other) } 2450 func (j jsonNumber) Concat(other JSON) (JSON, error) { return scalarConcat(j, other) } 2451 2452 func (j jsonArray) Concat(other JSON) (JSON, error) { 2453 left := j 2454 switch other.Type() { 2455 case ArrayJSONType: 2456 decoded, err := other.tryDecode() 2457 if err != nil { 2458 return nil, err 2459 } 2460 right := decoded.(jsonArray) 2461 result := make(jsonArray, len(left)+len(right)) 2462 copy(result, left) 2463 copy(result[len(left):], right) 2464 return result, nil 2465 default: 2466 result := make(jsonArray, len(left)+1) 2467 copy(result, left) 2468 result[len(left)] = other 2469 return result, nil 2470 } 2471 } 2472 2473 func (j jsonObject) Concat(other JSON) (JSON, error) { 2474 switch other.Type() { 2475 case ArrayJSONType: 2476 return scalarConcat(j, other) 2477 case ObjectJSONType: 2478 right := other.MaybeDecode().(jsonObject) 2479 // Since both objects are sorted, we can do the merge sort thing to 2480 // "concatenate" them. 2481 // The capacity here is an overestimate if the two objects share keys. 2482 result := make(jsonObject, 0, len(j)+len(right)) 2483 rightIdx := 0 2484 for _, kv := range j { 2485 for rightIdx < len(right) && right[rightIdx].k < kv.k { 2486 result = append(result, right[rightIdx]) 2487 rightIdx++ 2488 } 2489 // If we have any matching keys, the value in the right object takes 2490 // precedence (this allows || to work as a setter). 2491 if rightIdx < len(right) && right[rightIdx].k == kv.k { 2492 result = append(result, right[rightIdx]) 2493 rightIdx++ 2494 } else { 2495 result = append(result, kv) 2496 } 2497 } 2498 // We've exhausted all of the key-value pairs on the left, so just dump in 2499 // the remaining ones from the right. 2500 for i := rightIdx; i < len(right); i++ { 2501 result = append(result, right[i]) 2502 } 2503 return result, nil 2504 default: 2505 return nil, errInvalidConcat 2506 } 2507 } 2508 2509 func (j jsonString) AsText() (*string, error) { 2510 s := string(j) 2511 return &s, nil 2512 } 2513 func (j jsonNull) AsText() (*string, error) { return nil, nil } 2514 func (j jsonTrue) AsText() (*string, error) { 2515 s := j.String() 2516 return &s, nil 2517 } 2518 func (j jsonFalse) AsText() (*string, error) { 2519 s := j.String() 2520 return &s, nil 2521 } 2522 func (j jsonNumber) AsText() (*string, error) { 2523 s := j.String() 2524 return &s, nil 2525 } 2526 func (j jsonArray) AsText() (*string, error) { 2527 s := j.String() 2528 return &s, nil 2529 } 2530 func (j jsonObject) AsText() (*string, error) { 2531 s := j.String() 2532 return &s, nil 2533 } 2534 2535 func (jsonNull) Exists(string) (bool, error) { return false, nil } 2536 func (jsonTrue) Exists(string) (bool, error) { return false, nil } 2537 func (jsonFalse) Exists(string) (bool, error) { return false, nil } 2538 func (jsonNumber) Exists(string) (bool, error) { return false, nil } 2539 2540 func (j jsonString) Exists(s string) (bool, error) { 2541 return string(j) == s, nil 2542 } 2543 2544 func (j jsonArray) Exists(s string) (bool, error) { 2545 for i := 0; i < len(j); i++ { 2546 switch elem := j[i].(type) { 2547 case jsonString: 2548 if string(elem) == s { 2549 return true, nil 2550 } 2551 case *jsonEncoded: 2552 if elem.typ == StringJSONType && string(elem.value) == s { 2553 return true, nil 2554 } 2555 } 2556 } 2557 return false, nil 2558 } 2559 func (j jsonObject) Exists(s string) (bool, error) { 2560 v, err := j.FetchValKey(s) 2561 if err != nil { 2562 return false, err 2563 } 2564 return v != nil, nil 2565 } 2566 2567 func (j jsonNull) StripNulls() (JSON, bool, error) { 2568 return j, false, nil 2569 } 2570 func (j jsonTrue) StripNulls() (JSON, bool, error) { 2571 return j, false, nil 2572 } 2573 func (j jsonFalse) StripNulls() (JSON, bool, error) { 2574 return j, false, nil 2575 } 2576 func (j jsonNumber) StripNulls() (JSON, bool, error) { 2577 return j, false, nil 2578 } 2579 func (j jsonString) StripNulls() (JSON, bool, error) { 2580 return j, false, nil 2581 } 2582 func (j jsonArray) StripNulls() (JSON, bool, error) { 2583 for i, e := range j { 2584 json, needToStrip, err := e.StripNulls() 2585 if err != nil { 2586 return nil, false, err 2587 } 2588 if needToStrip { 2589 // Cannot return the original content, need to return the result 2590 // with new JSON array. 2591 newArr := make(jsonArray, 0, len(j)) 2592 newArr = append(append(newArr, j[:i]...), json) 2593 for _, elem := range j[i+1:] { 2594 if json, _, err = elem.StripNulls(); err != nil { 2595 return nil, false, err 2596 } 2597 newArr = append(newArr, json) 2598 } 2599 return newArr, true, nil 2600 } 2601 } 2602 return j, false, nil 2603 } 2604 func (j jsonObject) StripNulls() (JSON, bool, error) { 2605 for i, e := range j { 2606 var json JSON 2607 var err error 2608 needToStrip := false 2609 hasNullValue := e.v.Type() == NullJSONType 2610 if !hasNullValue { 2611 json, needToStrip, err = e.v.StripNulls() 2612 if err != nil { 2613 return nil, false, err 2614 } 2615 } 2616 if hasNullValue || needToStrip { 2617 // Cannot return the original content, need to return the result 2618 // with new JSON object. 2619 numNotNulls := i 2620 for _, elem := range j[i:] { 2621 if elem.v.Type() != NullJSONType { 2622 numNotNulls++ 2623 } 2624 } 2625 // Use number of fields not having null value to construct newObj 2626 // so that no need to grow the capacity of newObj. 2627 newObj := make(jsonObject, 0, numNotNulls) 2628 newObj = append(newObj, j[:i]...) 2629 if !hasNullValue { 2630 newObj = append(newObj, jsonKeyValuePair{ 2631 k: e.k, 2632 v: json, 2633 }) 2634 } 2635 for _, elem := range j[i+1:] { 2636 if elem.v.Type() != NullJSONType { 2637 if json, _, err = elem.v.StripNulls(); err != nil { 2638 return nil, false, err 2639 } 2640 newObj = append(newObj, jsonKeyValuePair{ 2641 k: elem.k, 2642 v: json, 2643 }) 2644 } 2645 } 2646 return newObj, true, nil 2647 } 2648 } 2649 return j, false, nil 2650 } 2651 2652 func (jsonNull) ObjectIter() (*ObjectIterator, error) { 2653 return nil, nil 2654 } 2655 func (jsonTrue) ObjectIter() (*ObjectIterator, error) { 2656 return nil, nil 2657 } 2658 func (jsonFalse) ObjectIter() (*ObjectIterator, error) { 2659 return nil, nil 2660 } 2661 func (jsonNumber) ObjectIter() (*ObjectIterator, error) { 2662 return nil, nil 2663 } 2664 func (jsonString) ObjectIter() (*ObjectIterator, error) { 2665 return nil, nil 2666 } 2667 func (jsonArray) ObjectIter() (*ObjectIterator, error) { 2668 return nil, nil 2669 } 2670 func (j jsonObject) ObjectIter() (*ObjectIterator, error) { 2671 return newObjectIterator(j), nil 2672 } 2673 2674 func (jsonNull) isScalar() bool { return true } 2675 func (jsonFalse) isScalar() bool { return true } 2676 func (jsonTrue) isScalar() bool { return true } 2677 func (jsonNumber) isScalar() bool { return true } 2678 func (jsonString) isScalar() bool { return true } 2679 func (jsonArray) isScalar() bool { return false } 2680 func (jsonObject) isScalar() bool { return false } 2681 2682 func (jsonNull) Len() int { return 0 } 2683 func (jsonTrue) Len() int { return 0 } 2684 func (jsonFalse) Len() int { return 0 } 2685 func (jsonNumber) Len() int { return 0 } 2686 func (jsonString) Len() int { return 0 } 2687 func (j jsonArray) Len() int { return len(j) } 2688 func (j jsonObject) Len() int { return len(j) } 2689 2690 func (jsonNull) toGoRepr() (interface{}, error) { return nil, nil } 2691 func (jsonTrue) toGoRepr() (interface{}, error) { return true, nil } 2692 func (jsonFalse) toGoRepr() (interface{}, error) { return false, nil } 2693 func (j jsonString) toGoRepr() (interface{}, error) { return string(j), nil } 2694 func (j jsonNumber) toGoRepr() (interface{}, error) { return json.Number(j.String()), nil } 2695 func (j jsonArray) toGoRepr() (interface{}, error) { 2696 result := make([]interface{}, len(j)) 2697 for i, e := range j { 2698 next, err := e.toGoRepr() 2699 if err != nil { 2700 return nil, err 2701 } 2702 result[i] = next 2703 } 2704 return result, nil 2705 } 2706 func (j jsonObject) toGoRepr() (interface{}, error) { 2707 result := make(map[string]interface{}) 2708 for _, e := range j { 2709 next, err := e.v.toGoRepr() 2710 if err != nil { 2711 return nil, err 2712 } 2713 result[string(e.k)] = next 2714 } 2715 return result, nil 2716 } 2717 2718 // Pretty pretty-prints the given JSON document as required by jsonb_pretty. 2719 func Pretty(j JSON) (string, error) { 2720 asGo, err := j.toGoRepr() 2721 if err != nil { 2722 return "", err 2723 } 2724 // Luckily for us, despite Go's random map ordering, MarshalIndent sorts the 2725 // keys of objects. 2726 res, err := json.MarshalIndent(asGo, "", " ") 2727 return string(res), errors.Handled(err) 2728 } 2729 2730 var errCannotDeletePathInScalar = pgerror.WithCandidateCode(errors.New("cannot delete path in scalar"), pgcode.InvalidParameterValue) 2731 2732 func (j jsonArray) RemovePath(path []string) (JSON, bool, error) { return j.doRemovePath(path) } 2733 func (j jsonObject) RemovePath(path []string) (JSON, bool, error) { return j.doRemovePath(path) } 2734 func (jsonNull) RemovePath([]string) (JSON, bool, error) { 2735 return nil, false, errCannotDeletePathInScalar 2736 } 2737 func (jsonTrue) RemovePath([]string) (JSON, bool, error) { 2738 return nil, false, errCannotDeletePathInScalar 2739 } 2740 func (jsonFalse) RemovePath([]string) (JSON, bool, error) { 2741 return nil, false, errCannotDeletePathInScalar 2742 } 2743 func (jsonString) RemovePath([]string) (JSON, bool, error) { 2744 return nil, false, errCannotDeletePathInScalar 2745 } 2746 func (jsonNumber) RemovePath([]string) (JSON, bool, error) { 2747 return nil, false, errCannotDeletePathInScalar 2748 } 2749 2750 func (j jsonArray) doRemovePath(path []string) (JSON, bool, error) { 2751 if len(path) == 0 { 2752 return j, false, nil 2753 } 2754 // In path-deletion we have to attempt to parse numbers (this is different 2755 // from the `-` operator, where strings just never match on arrays). 2756 idx, err := strconv.Atoi(path[0]) 2757 if err != nil { 2758 // TODO(yuzefovich): give the position of the path element to match psql. 2759 err := errors.Newf("a path element is not an integer: %s", path[0]) 2760 err = pgerror.WithCandidateCode(err, pgcode.InvalidTextRepresentation) 2761 return j, false, err 2762 } 2763 if len(path) == 1 { 2764 return j.RemoveIndex(idx) 2765 } 2766 2767 if idx < -len(j) || idx >= len(j) { 2768 return j, false, nil 2769 } 2770 if idx < 0 { 2771 idx += len(j) 2772 } 2773 newVal, ok, err := j[idx].doRemovePath(path[1:]) 2774 if err != nil { 2775 return nil, false, err 2776 } 2777 if !ok { 2778 return j, false, nil 2779 } 2780 2781 result := make(jsonArray, len(j)) 2782 copy(result, j) 2783 result[idx] = newVal 2784 2785 return result, true, nil 2786 } 2787 2788 func (j jsonObject) doRemovePath(path []string) (JSON, bool, error) { 2789 if len(path) == 0 { 2790 return j, false, nil 2791 } 2792 if len(path) == 1 { 2793 return j.RemoveString(path[0]) 2794 } 2795 idx, ok := findPairIndexByKey(j, path[0]) 2796 if !ok { 2797 return j, false, nil 2798 } 2799 2800 newVal, ok, err := j[idx].v.doRemovePath(path[1:]) 2801 if err != nil { 2802 return nil, false, err 2803 } 2804 if !ok { 2805 return j, false, nil 2806 } 2807 2808 result := make(jsonObject, len(j)) 2809 copy(result, j) 2810 result[idx].v = newVal 2811 2812 return result, true, nil 2813 } 2814 2815 // When we hit a scalar, we stop. #- only errors if there's a scalar at the 2816 // very top level. 2817 func (j jsonNull) doRemovePath([]string) (JSON, bool, error) { return j, false, nil } 2818 func (j jsonTrue) doRemovePath([]string) (JSON, bool, error) { return j, false, nil } 2819 func (j jsonFalse) doRemovePath([]string) (JSON, bool, error) { return j, false, nil } 2820 func (j jsonString) doRemovePath([]string) (JSON, bool, error) { return j, false, nil } 2821 func (j jsonNumber) doRemovePath([]string) (JSON, bool, error) { return j, false, nil } 2822 2823 func (j jsonObject) HasContainerLeaf() (bool, error) { 2824 if j.Len() == 0 { 2825 return true, nil 2826 } 2827 for _, c := range j { 2828 child, err := c.v.HasContainerLeaf() 2829 if err != nil { 2830 return false, err 2831 } 2832 if child { 2833 return true, nil 2834 } 2835 } 2836 return false, nil 2837 } 2838 2839 func (j jsonArray) HasContainerLeaf() (bool, error) { 2840 if j.Len() == 0 { 2841 return true, nil 2842 } 2843 for _, c := range j { 2844 child, err := c.HasContainerLeaf() 2845 if err != nil { 2846 return false, err 2847 } 2848 if child { 2849 return true, nil 2850 } 2851 } 2852 return false, nil 2853 } 2854 2855 func (j jsonNull) HasContainerLeaf() (bool, error) { return false, nil } 2856 func (j jsonTrue) HasContainerLeaf() (bool, error) { return false, nil } 2857 func (j jsonFalse) HasContainerLeaf() (bool, error) { return false, nil } 2858 func (j jsonString) HasContainerLeaf() (bool, error) { return false, nil } 2859 func (j jsonNumber) HasContainerLeaf() (bool, error) { return false, nil }