-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathhelper.go
171 lines (151 loc) · 5.9 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package row
import (
"sort"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
)
// rowHelper has the common methods for table row manipulations.
type rowHelper struct {
Codec keys.SQLCodec
TableDesc catalog.TableDescriptor
// Secondary indexes.
Indexes []descpb.IndexDescriptor
indexEntries []rowenc.IndexEntry
// Computed during initialization for pretty-printing.
primIndexValDirs []encoding.Direction
secIndexValDirs [][]encoding.Direction
// Computed and cached.
primaryIndexKeyPrefix []byte
primaryIndexCols catalog.TableColSet
sortedColumnFamilies map[descpb.FamilyID][]descpb.ColumnID
}
func newRowHelper(
codec keys.SQLCodec, desc catalog.TableDescriptor, indexes []descpb.IndexDescriptor,
) rowHelper {
rh := rowHelper{Codec: codec, TableDesc: desc, Indexes: indexes}
// Pre-compute the encoding directions of the index key values for
// pretty-printing in traces.
rh.primIndexValDirs = catalogkeys.IndexKeyValDirs(rh.TableDesc.GetPrimaryIndex().IndexDesc())
rh.secIndexValDirs = make([][]encoding.Direction, len(rh.Indexes))
for i := range rh.Indexes {
rh.secIndexValDirs[i] = catalogkeys.IndexKeyValDirs(&rh.Indexes[i])
}
return rh
}
// encodeIndexes encodes the primary and secondary index keys. The
// secondaryIndexEntries are only valid until the next call to encodeIndexes or
// encodeSecondaryIndexes. includeEmpty details whether the results should
// include empty secondary index k/v pairs.
func (rh *rowHelper) encodeIndexes(
colIDtoRowIndex catalog.TableColMap,
values []tree.Datum,
ignoreIndexes util.FastIntSet,
includeEmpty bool,
) (primaryIndexKey []byte, secondaryIndexEntries []rowenc.IndexEntry, err error) {
primaryIndexKey, err = rh.encodePrimaryIndex(colIDtoRowIndex, values)
if err != nil {
return nil, nil, err
}
secondaryIndexEntries, err = rh.encodeSecondaryIndexes(colIDtoRowIndex, values, ignoreIndexes, includeEmpty)
if err != nil {
return nil, nil, err
}
return primaryIndexKey, secondaryIndexEntries, nil
}
// encodePrimaryIndex encodes the primary index key.
func (rh *rowHelper) encodePrimaryIndex(
colIDtoRowIndex catalog.TableColMap, values []tree.Datum,
) (primaryIndexKey []byte, err error) {
if rh.primaryIndexKeyPrefix == nil {
rh.primaryIndexKeyPrefix = rowenc.MakeIndexKeyPrefix(rh.Codec, rh.TableDesc,
rh.TableDesc.GetPrimaryIndexID())
}
primaryIndexKey, _, err = rowenc.EncodeIndexKey(
rh.TableDesc, rh.TableDesc.GetPrimaryIndex().IndexDesc(), colIDtoRowIndex, values, rh.primaryIndexKeyPrefix)
return primaryIndexKey, err
}
// encodeSecondaryIndexes encodes the secondary index keys based on a row's
// values.
//
// The secondaryIndexEntries are only valid until the next call to encodeIndexes
// or encodeSecondaryIndexes, when they are overwritten.
//
// This function will not encode index entries for any index with an ID in
// ignoreIndexes.
//
// includeEmpty details whether the results should include empty secondary index
// k/v pairs.
func (rh *rowHelper) encodeSecondaryIndexes(
colIDtoRowIndex catalog.TableColMap,
values []tree.Datum,
ignoreIndexes util.FastIntSet,
includeEmpty bool,
) (secondaryIndexEntries []rowenc.IndexEntry, err error) {
if cap(rh.indexEntries) < len(rh.Indexes) {
rh.indexEntries = make([]rowenc.IndexEntry, 0, len(rh.Indexes))
}
rh.indexEntries = rh.indexEntries[:0]
for i := range rh.Indexes {
index := &rh.Indexes[i]
if !ignoreIndexes.Contains(int(index.ID)) {
entries, err := rowenc.EncodeSecondaryIndex(rh.Codec, rh.TableDesc, index, colIDtoRowIndex, values, includeEmpty)
if err != nil {
return nil, err
}
rh.indexEntries = append(rh.indexEntries, entries...)
}
}
return rh.indexEntries, nil
}
// skipColumnInPK returns true if the value at column colID does not need
// to be encoded because it is already part of the primary key. Composite
// datums are considered too, so a composite datum in a PK will return false.
// TODO(dan): This logic is common and being moved into TableDescriptor (see
// #6233). Once it is, use the shared one.
func (rh *rowHelper) skipColumnInPK(colID descpb.ColumnID, value tree.Datum) (bool, error) {
if rh.primaryIndexCols.Empty() {
for i := 0; i < rh.TableDesc.GetPrimaryIndex().NumColumns(); i++ {
pkColID := rh.TableDesc.GetPrimaryIndex().GetColumnID(i)
rh.primaryIndexCols.Add(pkColID)
}
}
if !rh.primaryIndexCols.Contains(colID) {
return false, nil
}
if cdatum, ok := value.(tree.CompositeDatum); ok {
// Composite columns are encoded in both the key and the value.
return !cdatum.IsComposite(), nil
}
// Skip primary key columns as their values are encoded in the key of
// each family. Family 0 is guaranteed to exist and acts as a
// sentinel.
return true, nil
}
func (rh *rowHelper) sortedColumnFamily(famID descpb.FamilyID) ([]descpb.ColumnID, bool) {
if rh.sortedColumnFamilies == nil {
rh.sortedColumnFamilies = make(map[descpb.FamilyID][]descpb.ColumnID, rh.TableDesc.NumFamilies())
_ = rh.TableDesc.ForeachFamily(func(family *descpb.ColumnFamilyDescriptor) error {
colIDs := append([]descpb.ColumnID{}, family.ColumnIDs...)
sort.Sort(descpb.ColumnIDs(colIDs))
rh.sortedColumnFamilies[family.ID] = colIDs
return nil
})
}
colIDs, ok := rh.sortedColumnFamilies[famID]
return colIDs, ok
}