Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
michae2 committed Dec 18, 2024
1 parent f333456 commit 7a95b22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pkg/sql/catalog/catenumpb/encoded_datum.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ enum DatumEncoding {
DESCENDING_KEY = 1;
// Indicates that the datum is encoded using the encoding used for values.
VALUE = 2;
// Indicates that the datum is encoded using the legacy encoding for values.
VALUE_LEGACY = 3;
}
21 changes: 8 additions & 13 deletions pkg/sql/row/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/fetchpb"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/keyside"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/valueside"
"github.com/cockroachdb/cockroach/pkg/sql/rowinfra"
"github.com/cockroachdb/cockroach/pkg/sql/scrub"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
Expand Down Expand Up @@ -1076,21 +1075,17 @@ func (rf *Fetcher) processValueSingle(
return prettyKey, "", nil
}
typ := table.spec.FetchedColumns[idx].Type
// TODO(arjun): The value is a directly marshaled single value, so we
// unmarshal it eagerly here. This can potentially be optimized out,
// although that would require changing UnmarshalColumnValue to operate
// on bytes, and for Encode/DecodeTableValue to operate on marshaled
// single values.
value, err := valueside.UnmarshalLegacy(rf.args.Alloc, typ, kv.Value)
if err != nil {
return "", "", err
}
encValue := rowenc.EncDatumValueLegacyFromValue(kv.Value)
if rf.args.TraceKV {
prettyValue = value.String()
err := encValue.EnsureDecoded(typ, rf.args.Alloc)
if err != nil {
return "", "", err
}
prettyValue = encValue.Datum.String()
}
table.row[idx] = rowenc.DatumToEncDatum(typ, value)
table.row[idx] = encValue
if DebugRowFetch {
log.Infof(ctx, "Scan %s -> %v", kv.Key, value)
log.Infof(ctx, "Scan %d -> %v", kv.Key, encValue)
}
return prettyKey, prettyValue, nil
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/sql/rowenc/encoded_datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"unsafe"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catenumpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc/keyside"
Expand Down Expand Up @@ -179,6 +180,18 @@ func EncDatumValueFromBufferWithOffsetsAndType(
return ed, buf[encLen:], nil
}

// EncDatumValueLegacyFromValue
func EncDatumValueLegacyFromValue(value roachpb.Value) EncDatum {
if value.RawBytes == nil {
return NullEncDatum()
}
return EncDatum{
encoding: catenumpb.DatumEncoding_VALUE_LEGACY,
encoded: value.TagAndDataBytes(),
Datum: nil,
}
}

// DatumToEncDatum initializes an EncDatum with the given Datum.
func DatumToEncDatum(ctyp *types.T, d tree.Datum) EncDatum {
ed, err := DatumToEncDatumEx(ctyp, d)
Expand Down Expand Up @@ -242,6 +255,9 @@ func (ed *EncDatum) IsNull() bool {
}
return typ == encoding.Null

case catenumpb.DatumEncoding_VALUE_LEGACY:
// should already have ed.Datum set to tree.DNull
return false
default:
panic(errors.AssertionFailedf("unknown encoding %s", ed.encoding))
}
Expand All @@ -264,6 +280,10 @@ func (ed *EncDatum) EnsureDecoded(typ *types.T, a *tree.DatumAlloc) error {
ed.Datum, rem, err = keyside.Decode(a, typ, ed.encoded, encoding.Descending)
case catenumpb.DatumEncoding_VALUE:
ed.Datum, rem, err = valueside.Decode(a, typ, ed.encoded)
case catenumpb.DatumEncoding_VALUE_LEGACY:
var v roachpb.Value
v.SetTagAndData(ed.encoded)
ed.Datum, err = valueside.UnmarshalLegacy(a, typ, v)
default:
return errors.AssertionFailedf("unknown encoding %d", redact.Safe(ed.encoding))
}
Expand Down

0 comments on commit 7a95b22

Please sign in to comment.