Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
57506: colfetcher: fix kvtrace logic test option for inverted index scans r=mgartner a=mgartner

#### rowenc: fix kvtrace logic test option for JSON inverted index scans

Previously, `kvtrace` logic tests of queries that performed JSON
inverted index scans would error if the scan output any rows. When
`cFetcher` attempted to decode the JSON portion of the key,
`rowenc.DecodeTableKey` returned an empty `[]byte` as the portion of the
key remaining after the JSON value. Attempting to decode this empty
`[]byte` as the primary key value caused an error.

Now, `rowenc.DecodeTableKey` will correctly return the remaining bytes
of the key after the JSON value. Note that this function still does not
actually decode the JSON value.

Release note: None

#### encoding: fix typos in DecodeVarintAscending and DecodeVarintDescending

Release note: None


Co-authored-by: Marcus Gartner <[email protected]>
  • Loading branch information
craig[bot] and mgartner committed Dec 4, 2020
2 parents 9561258 + b30c7e0 commit 9cd5016
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ CPut /Table/53/1/1/0 -> /TUPLE/2:2:Int/333/1:3:Bytes/foo/
InitPut /Table/53/2/333/"a"/"b"/1/0 -> /BYTES/
InitPut /Table/53/3/333/"foo"/"a"/"b"/1/0 -> /BYTES/

# This test shows an inverted index scan followed by a primary index scan to
# retrieve all the table's columns.
query T kvtrace
SELECT * FROM t WHERE i = 333 AND j @> '{"a": "b"}'
----
Scan /Table/53/2/333/"a"/"b"{-/PrefixEnd}
Scan /Table/53/1/1{-/#}

# Don't insert duplicate values.
query T kvtrace
INSERT INTO t VALUES (2, 333, 'foo', '[7, 0, 7]'::json)
Expand Down
8 changes: 7 additions & 1 deletion pkg/sql/rowenc/column_type_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,13 @@ func DecodeTableKey(
d, err := tree.NewDCollatedString(r, valType.Locale(), &a.env)
return d, rkey, err
case types.JsonFamily:
return tree.DNull, []byte{}, nil
// Don't attempt to decode the JSON value. Instead, just return the
// remaining bytes of the key.
jsonLen, err := encoding.PeekLength(key)
if err != nil {
return nil, nil, err
}
return tree.DNull, key[jsonLen:], nil
case types.BytesFamily:
var r []byte
if dir == encoding.Ascending {
Expand Down
10 changes: 5 additions & 5 deletions pkg/util/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,17 @@ func getVarintLen(b []byte) (int, error) {
return length, nil
}

// DecodeVarintAscending decodes a value encoded by EncodeVaringAscending.
// DecodeVarintAscending decodes a value encoded by EncodeVarintAscending.
func DecodeVarintAscending(b []byte) ([]byte, int64, error) {
if len(b) == 0 {
return nil, 0, errors.Errorf("insufficient bytes to decode uvarint value")
return nil, 0, errors.Errorf("insufficient bytes to decode varint value")
}
length := int(b[0]) - intZero
if length < 0 {
length = -length
remB := b[1:]
if len(remB) < length {
return nil, 0, errors.Errorf("insufficient bytes to decode uvarint value: %q", remB)
return nil, 0, errors.Errorf("insufficient bytes to decode varint value: %q", remB)
}
var v int64
// Use the ones-complement of each encoded byte in order to build
Expand All @@ -345,7 +345,7 @@ func DecodeVarintAscending(b []byte) ([]byte, int64, error) {
return remB, int64(v), nil
}

// DecodeVarintDescending decodes a uint64 value which was encoded
// DecodeVarintDescending decodes a int64 value which was encoded
// using EncodeVarintDescending.
func DecodeVarintDescending(b []byte) ([]byte, int64, error) {
leftover, v, err := DecodeVarintAscending(b)
Expand Down Expand Up @@ -456,7 +456,7 @@ func EncLenUvarintDescending(v uint64) int {
return 2 + highestByteIndex(v)
}

// DecodeUvarintAscending decodes a varint encoded uint64 from the input
// DecodeUvarintAscending decodes a uint64 encoded uint64 from the input
// buffer. The remainder of the input buffer and the decoded uint64
// are returned.
func DecodeUvarintAscending(b []byte) ([]byte, uint64, error) {
Expand Down

0 comments on commit 9cd5016

Please sign in to comment.