-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sem/tree: add support for producing vectorized data from strings
tree.ValueHandler exposes raw machine type hooks that are used by vec_handler to build coldata.Vec's. Epic: CRDB-18892 Informs: #91831 Release note: None
- Loading branch information
Showing
6 changed files
with
552 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// Copyright 2023 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 coldataext | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cockroachdb/apd/v3" | ||
"github.com/cockroachdb/cockroach/pkg/col/coldata" | ||
"github.com/cockroachdb/cockroach/pkg/col/typeconv" | ||
"github.com/cockroachdb/cockroach/pkg/sql/colexecerror" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/types" | ||
"github.com/cockroachdb/cockroach/pkg/util/duration" | ||
"github.com/cockroachdb/cockroach/pkg/util/encoding" | ||
"github.com/cockroachdb/cockroach/pkg/util/json" | ||
"github.com/cockroachdb/cockroach/pkg/util/timeutil/pgdate" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// MakeVecHandler makes a tree.ValueHandler that stores values to a coldata.Vec. | ||
func MakeVecHandler(vec coldata.Vec) tree.ValueHandler { | ||
v := vecHandler{nulls: vec.Nulls()} | ||
switch vec.CanonicalTypeFamily() { | ||
case types.BoolFamily: | ||
v.bools = vec.Bool() | ||
case types.BytesFamily: | ||
v.bytes = vec.Bytes() | ||
case types.DecimalFamily: | ||
v.decimals = vec.Decimal() | ||
case types.IntFamily: | ||
v.ints = vec.Int64() | ||
case types.FloatFamily: | ||
v.floats = vec.Float64() | ||
case types.TimestampTZFamily: | ||
v.timestamps = vec.Timestamp() | ||
case types.IntervalFamily: | ||
v.intervals = vec.Interval() | ||
case types.JsonFamily: | ||
v.jsons = vec.JSON() | ||
case typeconv.DatumVecCanonicalTypeFamily: | ||
v.datums = vec.Datum() | ||
default: | ||
colexecerror.InternalError(errors.AssertionFailedf("unhandled type %s", vec.Type())) | ||
} | ||
return &v | ||
} | ||
|
||
type vecHandler struct { | ||
nulls *coldata.Nulls | ||
bools coldata.Bools | ||
bytes *coldata.Bytes | ||
decimals coldata.Decimals | ||
// TODO(cucaroach): implement small int types | ||
//int16s coldata.Int16s | ||
//int32s coldata.Int32s | ||
ints coldata.Int64s | ||
floats coldata.Float64s | ||
timestamps coldata.Times | ||
intervals coldata.Durations | ||
jsons *coldata.JSONs | ||
datums coldata.DatumVec | ||
row int | ||
} | ||
|
||
var _ tree.ValueHandler = (*vecHandler)(nil) | ||
|
||
// Reset is used to re-use a batch handler across batches. | ||
func (v *vecHandler) Reset() { | ||
v.row = 0 | ||
} | ||
|
||
// Len returns the current length of the vector. | ||
func (v *vecHandler) Len() int { | ||
return v.row | ||
} | ||
|
||
// Decimal implements tree.ValueHandler interface. It returns a pointer into the | ||
// vec to allow the decimal to be constructed in place which avoids expensive | ||
// copying and temporary allocations. | ||
func (v *vecHandler) Decimal() *apd.Decimal { | ||
d := &v.decimals[v.row] | ||
v.row++ | ||
return d | ||
} | ||
|
||
// Null implements tree.ValueHandler interface. | ||
func (v *vecHandler) Null() { | ||
v.nulls.SetNull(v.row) | ||
v.row++ | ||
} | ||
|
||
// String is part of the tree.ValueHandler interface. | ||
func (v *vecHandler) String(s string) { | ||
v.bytes.Set(v.row, encoding.UnsafeConvertStringToBytes(s)) | ||
v.row++ | ||
} | ||
|
||
// Date is part of the tree.ValueHandler interface. | ||
func (v *vecHandler) Date(d pgdate.Date) { | ||
v.ints[v.row] = d.UnixEpochDaysWithOrig() | ||
v.row++ | ||
} | ||
|
||
// Datum is part of the tree.ValueHandler interface. | ||
func (v *vecHandler) Datum(d tree.Datum) { | ||
v.datums.Set(v.row, d) | ||
v.row++ | ||
} | ||
|
||
// Bool is part of the tree.ValueHandler interface. | ||
func (v *vecHandler) Bool(b bool) { | ||
v.bools[v.row] = b | ||
v.row++ | ||
} | ||
|
||
// Bytes is part of the tree.ValueHandler interface. | ||
func (v *vecHandler) Bytes(b []byte) { | ||
v.bytes.Set(v.row, b) | ||
v.row++ | ||
} | ||
|
||
// Float is part of the tree.ValueHandler interface. | ||
func (v *vecHandler) Float(f float64) { | ||
v.floats[v.row] = f | ||
v.row++ | ||
} | ||
|
||
// Int is part of the tree.ValueHandler interface. | ||
func (v *vecHandler) Int(i int64) { | ||
v.ints[v.row] = i | ||
v.row++ | ||
} | ||
|
||
// Duration is part of the tree.ValueHandler interface. | ||
func (v *vecHandler) Duration(d duration.Duration) { | ||
v.intervals[v.row] = d | ||
v.row++ | ||
} | ||
|
||
// JSON is part of the tree.ValueHandler interface. | ||
func (v *vecHandler) JSON(j json.JSON) { | ||
v.jsons.Set(v.row, j) | ||
v.row++ | ||
} | ||
|
||
// TimestampTZ is part of the tree.ValueHandler interface. | ||
func (v *vecHandler) TimestampTZ(t time.Time) { | ||
v.timestamps[v.row] = t | ||
v.row++ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.