Skip to content

Commit

Permalink
sql/rowenc: avoid expensive errors in (*EncDatum).GetInt
Browse files Browse the repository at this point in the history
The errors returned by `(*EncDatum).GetInt` when the datum is the null
value were expensive to create because they caused heap allocations and
they walked the stack to build a stack trace. A global error is now used
to eliminate this overhead.

Release note: None
  • Loading branch information
mgartner committed Dec 28, 2024
1 parent 686a8fa commit a8010b1
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/sql/rowenc/encoded_datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,28 +425,30 @@ func (ed *EncDatum) CompareEx(
return ed.Datum.Compare(ctx, evalCtx, rhs.Datum)
}

var errNullInt = errors.New("NULL INT value")

// GetInt decodes an EncDatum that is known to be of integer type and returns
// the integer value. It is a more convenient and more efficient alternative to
// calling EnsureDecoded and casting the Datum.
func (ed *EncDatum) GetInt() (int64, error) {
if ed.Datum != nil {
if ed.Datum == tree.DNull {
return 0, errors.Errorf("NULL INT value")
return 0, errNullInt
}
return int64(*ed.Datum.(*tree.DInt)), nil
}

switch ed.encoding {
case catenumpb.DatumEncoding_ASCENDING_KEY:
if _, isNull := encoding.DecodeIfNull(ed.encoded); isNull {
return 0, errors.Errorf("NULL INT value")
return 0, errNullInt
}
_, val, err := encoding.DecodeVarintAscending(ed.encoded)
return val, err

case catenumpb.DatumEncoding_DESCENDING_KEY:
if _, isNull := encoding.DecodeIfNull(ed.encoded); isNull {
return 0, errors.Errorf("NULL INT value")
return 0, errNullInt
}
_, val, err := encoding.DecodeVarintDescending(ed.encoded)
return val, err
Expand All @@ -458,7 +460,7 @@ func (ed *EncDatum) GetInt() (int64, error) {
}
// NULL, true, and false are special, because their values are fully encoded by their value tag.
if typ == encoding.Null {
return 0, errors.Errorf("NULL INT value")
return 0, errNullInt
}

_, val, err := encoding.DecodeUntaggedIntValue(ed.encoded[dataOffset:])
Expand Down

0 comments on commit a8010b1

Please sign in to comment.