Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tree: remove roachpb references #80684

Merged
merged 3 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/ccl/backupccl/backup_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ func backupPlanHook(
if !p.ExecCfg().Codec.ForSystemTenant() {
return pgerror.Newf(pgcode.InsufficientPrivilege, "only the system tenant can backup other tenants")
}
initialDetails.SpecificTenantIds = []roachpb.TenantID{backupStmt.Targets.TenantID.TenantID}
initialDetails.SpecificTenantIds = []roachpb.TenantID{roachpb.MakeTenantID(backupStmt.Targets.TenantID.ID)}
}

jobID := p.ExecCfg().JobRegistry.MakeJobID()
Expand Down
4 changes: 2 additions & 2 deletions pkg/ccl/backupccl/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,11 @@ func selectTargets(
for _, tenant := range lastBackupManifest.GetTenants() {
// TODO(dt): for now it is zero-or-one but when that changes, we should
// either keep it sorted or build a set here.
if tenant.ID == targets.TenantID.ToUint64() {
if tenant.ID == targets.TenantID.ID {
return nil, nil, []descpb.TenantInfoWithUsage{tenant}, nil
}
}
return nil, nil, nil, errors.Errorf("tenant %d not in backup", targets.TenantID.ToUint64())
return nil, nil, nil, errors.Errorf("tenant %d not in backup", targets.TenantID.ID)
}

matched, err := backupresolver.DescriptorsMatchingTargets(ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ func ingestionPlanHook(
}

//TODO(casper): make target to be tenant-only.
oldTenantID := ingestionStmt.Targets.TenantID.TenantID
oldTenantID := roachpb.MakeTenantID(ingestionStmt.Targets.TenantID.ID)
newTenantID := oldTenantID
if ingestionStmt.AsTenant.Specified {
newTenantID = ingestionStmt.AsTenant.TenantID
newTenantID = roachpb.MakeTenantID(ingestionStmt.AsTenant.ID)
}
if oldTenantID == roachpb.SystemTenantID || newTenantID == roachpb.SystemTenantID {
return errors.Newf("either old tenant ID %d or the new tenant ID %d cannot be system tenant",
Expand Down
21 changes: 2 additions & 19 deletions pkg/roachpb/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (rk RKey) Equal(other []byte) bool {
// The method may only take a shallow copy of the RKey, so both the
// receiver and the return value should be treated as immutable after.
func (rk RKey) Next() RKey {
return RKey(BytesNext(rk))
return RKey(encoding.BytesNext(rk))
}

// PrefixEnd determines the end key given key as a prefix, that is the
Expand All @@ -140,23 +140,6 @@ func (rk RKey) StringWithDirs(valDirs []encoding.Direction, maxLen int) string {
// messages which refer to Cockroach keys.
type Key []byte

// BytesNext returns the next possible byte slice, using the extra capacity
// of the provided slice if possible, and if not, appending an \x00.
func BytesNext(b []byte) []byte {
if cap(b) > len(b) {
bNext := b[:len(b)+1]
if bNext[len(bNext)-1] == 0 {
return bNext
}
}
// TODO(spencer): Do we need to enforce KeyMaxLength here?
// Switched to "make and copy" pattern in #4963 for performance.
bn := make([]byte, len(b)+1)
copy(bn, b)
bn[len(bn)-1] = 0
return bn
}

// Clone returns a copy of the key.
func (k Key) Clone() Key {
if k == nil {
Expand All @@ -171,7 +154,7 @@ func (k Key) Clone() Key {
// take a shallow copy of the Key, so both the receiver and the return
// value should be treated as immutable after.
func (k Key) Next() Key {
return Key(BytesNext(k))
return Key(encoding.BytesNext(k))
}

// IsPrev is a more efficient version of k.Next().Equal(m).
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,7 @@ opt_as_tenant_clause:
if tenID == 0 {
return setErr(sqllex, errors.New("invalid tenant ID"))
}
$$.val = tree.TenantID{Specified: true, TenantID: roachpb.MakeTenantID(tenID)}
$$.val = tree.TenantID{Specified: true, ID: tenID}
}
| AS TENANT IDENT
{
Expand Down Expand Up @@ -6835,7 +6835,7 @@ targets:
if tenID == 0 {
return setErr(sqllex, errors.New("invalid tenant ID"))
}
$$.val = tree.TargetList{TenantID: tree.TenantID{Specified: true, TenantID: roachpb.MakeTenantID(tenID)}}
$$.val = tree.TargetList{TenantID: tree.TenantID{Specified: true, ID: tenID}}
}
| TENANT IDENT
{
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/sem/eval/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ go_test(
"eval_test.go",
"like_test.go",
"parse_doid_test.go",
"timeconv_test.go",
"window_funcs_test.go",
],
data = glob(["testdata/**"]),
embed = [":eval"],
deps = [
"//pkg/kv",
"//pkg/roachpb",
"//pkg/settings/cluster",
"//pkg/sql/faketreeeval",
"//pkg/sql/opt/exec/execbuilder",
Expand All @@ -118,6 +121,7 @@ go_test(
"//pkg/util/leaktest",
"//pkg/util/log",
"//pkg/util/randutil",
"//pkg/util/stop",
"//pkg/util/timeutil",
"@com_github_cockroachdb_apd_v3//:apd",
"@com_github_cockroachdb_datadriven//:datadriven",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package tree_test
package eval_test

import (
"context"
Expand Down
6 changes: 1 addition & 5 deletions pkg/sql/sem/tree/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ go_library(
deps = [
"//pkg/geo",
"//pkg/geo/geopb",
"//pkg/roachpb",
"//pkg/security",
"//pkg/server/telemetry",
"//pkg/sql/catalog/catconstants",
Expand All @@ -143,6 +142,7 @@ go_library(
"//pkg/util/bitarray",
"//pkg/util/cache",
"//pkg/util/duration",
"//pkg/util/encoding",
"//pkg/util/errorutil/unimplemented",
"//pkg/util/hlc",
"//pkg/util/ipaddr",
Expand Down Expand Up @@ -195,7 +195,6 @@ go_test(
"role_spec_test.go",
"table_name_test.go",
"time_test.go",
"timeconv_test.go",
"type_check_internal_test.go",
"type_check_test.go",
"type_name_test.go",
Expand All @@ -206,8 +205,6 @@ go_test(
deps = [
"//pkg/build/bazel",
"//pkg/internal/rsg",
"//pkg/kv",
"//pkg/roachpb",
"//pkg/security",
"//pkg/security/securitytest",
"//pkg/settings/cluster",
Expand All @@ -234,7 +231,6 @@ go_test(
"//pkg/util/log",
"//pkg/util/pretty",
"//pkg/util/randutil",
"//pkg/util/stop",
"//pkg/util/timeofday",
"//pkg/util/timetz",
"//pkg/util/timeutil",
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/sem/tree/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/cockroachdb/apd/v3"
"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/lex"
"github.com/cockroachdb/cockroach/pkg/sql/lexbase"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
Expand All @@ -36,6 +35,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/bitarray"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/ipaddr"
"github.com/cockroachdb/cockroach/pkg/util/json"
"github.com/cockroachdb/cockroach/pkg/util/stringencoding"
Expand Down Expand Up @@ -1260,7 +1260,7 @@ func (d *DString) Prev(ctx CompareContext) (Datum, bool) {

// Next implements the Datum interface.
func (d *DString) Next(ctx CompareContext) (Datum, bool) {
return NewDString(string(roachpb.Key(*d).Next())), true
return NewDString(string(encoding.BytesNext([]byte(*d)))), true
}

// IsMax implements the Datum interface.
Expand Down Expand Up @@ -1512,7 +1512,7 @@ func (d *DBytes) Prev(ctx CompareContext) (Datum, bool) {

// Next implements the Datum interface.
func (d *DBytes) Next(ctx CompareContext) (Datum, bool) {
return NewDBytes(DBytes(roachpb.Key(*d).Next())), true
return NewDBytes(DBytes(encoding.BytesNext([]byte(*d)))), true
}

// IsMax implements the Datum interface.
Expand Down
24 changes: 9 additions & 15 deletions pkg/sql/sem/tree/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,38 @@

package tree

import (
"strconv"

"github.com/cockroachdb/cockroach/pkg/roachpb"
)
import "strconv"

// TenantID represents a tenant ID that can be pretty-printed.
type TenantID struct {
roachpb.TenantID
ID uint64

// Specified is set to true when the TENANT clause was specified in
// the backup target input syntax. We need this, instead of relying
// on roachpb.TenantID.IsSet(), because we need a special marker for
// on ID != 0, because we need a special marker for
// the case when the value was anonymized. In other places, the
// value used for anonymized integer literals is 0, but we can't use
// 0 for TenantID as this is refused during parsing, nor can we use
// any other value since all non-zero integers are valid tenant IDs.
//
// TODO(knz): None of this complexity would be needed if the tenant
// ID was specified using an Expr here. This can be removed once the
// backup target syntax accepts expressions for tenant targets.
Specified bool
}

var _ NodeFormatter = (*TenantID)(nil)

// IsSet returns whether the TenantID is set.
func (t *TenantID) IsSet() bool {
return t.Specified && t.ID != 0
}

// Format implements the NodeFormatter interface.
func (t *TenantID) Format(ctx *FmtCtx) {
if ctx.flags.HasFlags(FmtHideConstants) || !t.IsSet() {
// The second part of the condition above is conceptually
// redundant, but is sadly needed here because we need to be able
// to re-print a TENANT clause after it has been anonymized,
// and we can't emit the value zero which is unparseable.
//
// TODO(knz): This branch can be removed once the TENANT clause
// accepts expressions.
ctx.WriteByte('_')
} else {
ctx.WriteString(strconv.FormatUint(t.ToUint64(), 10))
ctx.WriteString(strconv.FormatUint(t.ID, 10))
}
}
4 changes: 2 additions & 2 deletions pkg/storage/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func BenchmarkScanAllIntentsResolved(b *testing.B) {
}
// Skip to next key.
buf = append(buf[:0], k.Key...)
buf = roachpb.BytesNext(buf)
buf = encoding.BytesNext(buf)
iter.SeekGE(MVCCKey{Key: buf})
}
}
Expand Down Expand Up @@ -444,7 +444,7 @@ func BenchmarkScanOneAllIntentsResolved(b *testing.B) {
}
// Skip to next key.
buf = append(buf[:0], k.Key...)
buf = roachpb.BytesNext(buf)
buf = encoding.BytesNext(buf)
iter.Close()
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/mvcc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4198,7 +4198,7 @@ func TestRandomizedMVCCResolveWriteIntentRange(t *testing.T) {
lu := roachpb.LockUpdate{
Span: roachpb.Span{
Key: puts[0].key,
EndKey: roachpb.BytesNext(puts[len(puts)-1].key),
EndKey: encoding.BytesNext(puts[len(puts)-1].key),
},
Txn: txnMeta,
Status: status,
Expand Down Expand Up @@ -4307,7 +4307,7 @@ func TestRandomizedSavepointRollbackAndIntentResolution(t *testing.T) {
lu := roachpb.LockUpdate{
Span: roachpb.Span{
Key: puts[0].key,
EndKey: roachpb.BytesNext(puts[len(puts)-1].key),
EndKey: encoding.BytesNext(puts[len(puts)-1].key),
},
Txn: txn.TxnMeta,
Status: roachpb.PENDING,
Expand Down
3 changes: 2 additions & 1 deletion pkg/storage/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/storage/fs"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
Expand Down Expand Up @@ -1862,7 +1863,7 @@ func (p *pebbleReadOnly) rawMVCCGet(key []byte) ([]byte, error) {
}
options := pebble.IterOptions{
LowerBound: key,
UpperBound: roachpb.BytesNext(key),
UpperBound: encoding.BytesNext(key),
OnlyReadGuaranteedDurable: onlyReadGuaranteedDurable,
}
iter := p.parent.db.NewIter(&options)
Expand Down
17 changes: 17 additions & 0 deletions pkg/util/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3168,3 +3168,20 @@ func IsArrayKeyDone(buf []byte, dir Direction) bool {
}
return buf[0] == expected
}

// BytesNext returns the next possible byte slice, using the extra capacity
// of the provided slice if possible, and if not, appending an \x00.
func BytesNext(b []byte) []byte {
if cap(b) > len(b) {
bNext := b[:len(b)+1]
if bNext[len(bNext)-1] == 0 {
return bNext
}
}
// TODO(spencer): Do we need to enforce KeyMaxLength here?
// Switched to "make and copy" pattern in #4963 for performance.
bn := make([]byte, len(b)+1)
copy(bn, b)
bn[len(bn)-1] = 0
return bn
}