Skip to content

Commit

Permalink
Merge #85681
Browse files Browse the repository at this point in the history
85681: colexecbase: add all casts of native types to strings r=yuzefovich a=yuzefovich

**colexecbase: propagate bytes.Buffer as argument in cast templates**

This is not used currently but will be in the following commit.

Release note: None

**colexecbase: add all casts of native types to strings**

This commit adds all of the casts from the types natively supported by
the vectorized engine to strings. This has an additional benefit (apart
from better performance on a lot of data: the concat binary projection
with a string on one side is now supported natively (we recently fixed
an issue to plan a cast of a non-string type to a string for a concat
operation, but since we didn't have the cast support, we'd be falling
back to the row-by-row engine in most cases).

Addresses: #48135.
Fixes: #49463.
Fixes: #55841.

Release note: None

Co-authored-by: Yahor Yuzefovich <[email protected]>
  • Loading branch information
craig[bot] and yuzefovich committed Aug 8, 2022
2 parents 97dc5c5 + e20e163 commit ce8bfd4
Show file tree
Hide file tree
Showing 13 changed files with 6,419 additions and 2,143 deletions.
8,214 changes: 6,184 additions & 2,030 deletions pkg/sql/colexec/colexecbase/cast.eg.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/sql/colexec/colexecbase/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestRandomizedCast(t *testing.T) {
}
}
}
const numTypePairs = 5
var numTypePairs = rng.Intn(10) + 1
numRows := 1 + rng.Intn(coldata.BatchSize()) + rng.Intn(3)*coldata.BatchSize()
log.Infof(ctx, "num rows = %d", numRows)
for run := 0; run < numTypePairs; run++ {
Expand Down
18 changes: 11 additions & 7 deletions pkg/sql/colexec/colexecbase/cast_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package colexecbase

import (
"bytes"
"context"
"fmt"
"math"
Expand Down Expand Up @@ -81,7 +82,7 @@ const _TYPE_WIDTH = 0
// "castOp" template in the scope of this value's "callsite".
const _GENERATE_CAST_OP = 0

func _CAST(to, from, evalCtx, toType interface{}) {
func _CAST(to, from, evalCtx, toType, buf interface{}) {
colexecerror.InternalError(errors.AssertionFailedf(""))
}

Expand Down Expand Up @@ -235,6 +236,7 @@ type castOpBase struct {
colIdx int
outputIdx int
evalCtx *eval.Context
buf bytes.Buffer
}

func (c *castOpBase) Reset(ctx context.Context) {
Expand Down Expand Up @@ -448,15 +450,15 @@ func (c *cast_NAMEOp) Next() coldata.Batch {
if inputVec.MaybeHasNulls() {
outputNulls.Copy(inputNulls)
if sel != nil {
castTuples(inputCol, inputNulls, outputCol, outputNulls, toType, n, sel, c.evalCtx, true, true)
castTuples(inputCol, inputNulls, outputCol, outputNulls, toType, n, sel, c.evalCtx, &c.buf, true, true)
} else {
castTuples(inputCol, inputNulls, outputCol, outputNulls, toType, n, sel, c.evalCtx, true, false)
castTuples(inputCol, inputNulls, outputCol, outputNulls, toType, n, sel, c.evalCtx, &c.buf, true, false)
}
} else {
if sel != nil {
castTuples(inputCol, inputNulls, outputCol, outputNulls, toType, n, sel, c.evalCtx, false, true)
castTuples(inputCol, inputNulls, outputCol, outputNulls, toType, n, sel, c.evalCtx, &c.buf, false, true)
} else {
castTuples(inputCol, inputNulls, outputCol, outputNulls, toType, n, sel, c.evalCtx, false, false)
castTuples(inputCol, inputNulls, outputCol, outputNulls, toType, n, sel, c.evalCtx, &c.buf, false, false)
}
}
},
Expand Down Expand Up @@ -516,11 +518,13 @@ func castTuples(
n int,
sel []int,
evalCtx *eval.Context,
buf *bytes.Buffer,
hasNulls bool,
hasSel bool,
) {
// Silence unused warning.
// Silence unused warnings.
_ = evalCtx
_ = buf
if !hasSel {
// {{if $fromInfo.Sliceable}}
_ = inputCol.Get(n - 1)
Expand All @@ -546,7 +550,7 @@ func castTuples(
}
v := inputCol.Get(tupleIdx)
var r _TO_GO_TYPE
_CAST(r, v, evalCtx, toType)
_CAST(r, v, evalCtx, toType, buf)
if !hasSel {
// {{if .Sliceable}}
//gcassert:bce
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/colexec/execgen/cmd/execgen/avg_agg_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func genAvgAgg(inputFileContents string, wr io.Writer) error {
// canonical representatives, so we can operate with their type family
// directly.
for _, inputTypeFamily := range []types.Family{types.IntFamily, types.DecimalFamily, types.FloatFamily, types.IntervalFamily} {
tmplInfo := avgAggTypeTmplInfo{TypeFamily: toString(inputTypeFamily)}
tmplInfo := avgAggTypeTmplInfo{TypeFamily: familyToString(inputTypeFamily)}
for _, inputTypeWidth := range supportedWidthsByCanonicalTypeFamily[inputTypeFamily] {
// Note that we don't use execinfrapb.GetAggregateInfo because we don't
// want to bring in a dependency on that package to reduce the burden
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/colexec/execgen/cmd/execgen/cast_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func genCastOperators(inputFileContents string, wr io.Writer) error {
)
s := r.Replace(inputFileContents)

castRe := makeFunctionRegex("_CAST", 4)
s = castRe.ReplaceAllString(s, makeTemplateFunctionCall("Cast", 4))
castRe := makeFunctionRegex("_CAST", 5)
s = castRe.ReplaceAllString(s, makeTemplateFunctionCall("Cast", 5))

tmpl, err := template.New("cast").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s)
if err != nil {
Expand Down
Loading

0 comments on commit ce8bfd4

Please sign in to comment.