Skip to content

Commit

Permalink
rowenc: move low-level key encoding functions to subpackage
Browse files Browse the repository at this point in the history
The `rowenc` package contains a hefty mix of functions operating at
different conceptual levels - some use higher level constructs like
table and index descriptors, others are lower level utilities. The
naming of these functions doesn't help, for example `EncodeTableKey`
sounds like a top-level function but is actually a low level utility
that appends a single value to a key

This commit moves the lower level utilities for encoding datum values
into keys to the `rowenc/keyside` package.

Release note: None
  • Loading branch information
RaduBerinde committed Jan 7, 2022
1 parent 2b7ba8c commit a8fc111
Show file tree
Hide file tree
Showing 53 changed files with 1,091 additions and 1,214 deletions.
1 change: 1 addition & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ ALL_TESTS = [
"//pkg/sql/randgen:randgen_test",
"//pkg/sql/row:row_test",
"//pkg/sql/rowcontainer:rowcontainer_test",
"//pkg/sql/rowenc/keyside:keyside_test",
"//pkg/sql/rowenc:rowenc_test",
"//pkg/sql/rowexec:rowexec_test",
"//pkg/sql/rowflow:rowflow_test",
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/changefeedccl/kvevent/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ go_test(
"//pkg/roachpb:with-mocks",
"//pkg/settings/cluster",
"//pkg/sql/randgen",
"//pkg/sql/rowenc",
"//pkg/sql/rowenc/keyside",
"//pkg/sql/types",
"//pkg/util/ctxgroup",
"//pkg/util/encoding",
Expand Down
4 changes: 2 additions & 2 deletions pkg/ccl/changefeedccl/kvevent/blocking_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/randgen"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/keyside"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
Expand All @@ -35,7 +35,7 @@ import (
func makeKV(t *testing.T, rnd *rand.Rand) roachpb.KeyValue {
const tableID = 42

key, err := rowenc.EncodeTableKey(
key, err := keyside.Encode(
keys.SystemSQLCodec.TablePrefix(tableID),
randgen.RandDatumSimple(rnd, types.String),
encoding.Ascending,
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/changefeedccl/kvfeed/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ go_test(
"//pkg/sql/catalog",
"//pkg/sql/catalog/catalogkv",
"//pkg/sql/catalog/descpb",
"//pkg/sql/rowenc",
"//pkg/sql/rowenc/keyside",
"//pkg/sql/sem/tree",
"//pkg/testutils/serverutils",
"//pkg/testutils/sqlutils",
Expand Down
4 changes: 2 additions & 2 deletions pkg/ccl/changefeedccl/kvfeed/kv_feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/keyside"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
Expand All @@ -46,7 +46,7 @@ func TestKVFeed(t *testing.T) {

mkKey := func(tableID uint32, k string) roachpb.Key {
vDatum := tree.DString(k)
key, err := rowenc.EncodeTableKey(keys.SystemSQLCodec.TablePrefix(tableID), &vDatum, encoding.Ascending)
key, err := keyside.Encode(keys.SystemSQLCodec.TablePrefix(tableID), &vDatum, encoding.Ascending)
require.NoError(t, err)
return key
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kv/kvserver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ go_test(
"//pkg/sql/catalog/descpb",
"//pkg/sql/catalog/systemschema",
"//pkg/sql/catalog/tabledesc",
"//pkg/sql/rowenc",
"//pkg/sql/rowenc/keyside",
"//pkg/sql/sem/tree",
"//pkg/sql/sessiondata",
"//pkg/sql/sqlutil",
Expand Down
6 changes: 3 additions & 3 deletions pkg/kv/kvserver/closed_timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/keyside"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
Expand Down Expand Up @@ -393,7 +393,7 @@ func TestClosedTimestampCanServeAfterSplitAndMerges(t *testing.T) {
}
// Split the table at key 2.
idxPrefix := keys.SystemSQLCodec.IndexPrefix(uint32(tableID), 1)
k, err := rowenc.EncodeTableKey(idxPrefix, tree.NewDInt(2), encoding.Ascending)
k, err := keyside.Encode(idxPrefix, tree.NewDInt(2), encoding.Ascending)
if err != nil {
t.Fatalf("failed to encode key: %+v", err)
}
Expand Down Expand Up @@ -974,7 +974,7 @@ func getEncodedKeyForTable(
t.Fatalf("failed to lookup ids: %+v", err)
}
idxPrefix := keys.SystemSQLCodec.IndexPrefix(uint32(tableID), 1)
k, err := rowenc.EncodeTableKey(idxPrefix, val, encoding.Ascending)
k, err := keyside.Encode(idxPrefix, val, encoding.Ascending)
if err != nil {
t.Fatalf("failed to encode split key: %+v", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/colencoding/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//pkg/roachpb:with-mocks",
"//pkg/sql/catalog/descpb",
"//pkg/sql/rowenc",
"//pkg/sql/rowenc/keyside",
"//pkg/sql/sem/tree",
"//pkg/sql/types",
"//pkg/util",
Expand Down
9 changes: 5 additions & 4 deletions pkg/sql/colencoding/key_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/keyside"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util"
Expand Down Expand Up @@ -64,7 +65,7 @@ func DecodeKeyValsToCols(
foundNull = foundNull || isNull
}
// Don't need the coldata - skip it.
key, err = rowenc.SkipTableKey(key)
key, err = keyside.Skip(key)
} else {
if unseen != nil {
unseen.Remove(vecIdx)
Expand All @@ -81,9 +82,9 @@ func DecodeKeyValsToCols(
return key, foundNull, scratch, nil
}

// decodeTableKeyToCol decodes a value encoded by EncodeTableKey, writing the
// decodeTableKeyToCol decodes a value encoded by keyside.Encode, writing the
// result to the rowIdx'th slot of the vecIdx'th vector in coldata.TypedVecs.
// See the analog, rowenc.DecodeTableKey, in rowenc/column_type_encoding.go.
// See the analog, keyside.Decode, in rowenc/column_type_encoding.go.
// decodeTableKeyToCol also returns whether or not the decoded value was NULL.
func decodeTableKeyToCol(
da *tree.DatumAlloc,
Expand Down Expand Up @@ -206,7 +207,7 @@ func decodeTableKeyToCol(
if dir == descpb.IndexDescriptor_DESC {
encDir = encoding.Descending
}
d, rkey, err = rowenc.DecodeTableKey(da, valType, key, encDir)
d, rkey, err = keyside.Decode(da, valType, key, encDir)
vecs.DatumCols[colIdx].Set(rowIdx, d)
}
return rkey, false, scratch, err
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/colexec/colexecspan/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ go_library(
"//pkg/sql/colmem", # keep
"//pkg/sql/execinfra", # keep
"//pkg/sql/rowenc", # keep
"//pkg/sql/rowenc/keyside", # keep
"//pkg/sql/sem/tree", # keep
"//pkg/sql/types", # keep
"//pkg/util", # keep
Expand Down
20 changes: 10 additions & 10 deletions pkg/sql/colexec/colexecspan/span_encoder.eg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/sql/colexec/colexecspan/span_encoder_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/col/typeconv"
"github.com/cockroachdb/cockroach/pkg/sql/colexecerror"
"github.com/cockroachdb/cockroach/pkg/sql/colmem"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/keyside"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
Expand All @@ -36,7 +36,7 @@ import (
// Workaround for bazel auto-generated code. goimports does not automatically
// pick up the right packages when run within the bazel sandbox.
var (
_ = rowenc.EncodeTableKey
_ = keyside.Encode
_ tree.Datum
)

Expand Down
Loading

0 comments on commit a8fc111

Please sign in to comment.