-
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.
exec: Add framework for vectorized casts.
This PR adds in a framework for performing casts within the vectorized engine, along with a few casts implemented already. Release note: None
- Loading branch information
Showing
7 changed files
with
426 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
any_not_null_agg.eg.go | ||
avg_agg.eg.go | ||
cast_operator.eg.go | ||
coldata/vec.eg.go | ||
const.eg.go | ||
distinct.eg.go | ||
|
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,153 @@ | ||
// Copyright 2019 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. | ||
|
||
// {{/* | ||
// +build execgen_template | ||
// | ||
// This file is the execgen template for cast_operator.eg.go. It's formatted in a | ||
// special way, so it's both valid Go and a valid text/template input. This | ||
// permits editing this file with editor support. | ||
// | ||
// */}} | ||
|
||
package exec | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/apd" | ||
"github.com/cockroachdb/cockroach/pkg/sql/exec/types/conv" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/exec/coldata" | ||
"github.com/cockroachdb/cockroach/pkg/sql/exec/types" | ||
semtypes "github.com/cockroachdb/cockroach/pkg/sql/types" | ||
) | ||
|
||
// {{/* | ||
|
||
type _ALLTYPES interface{} | ||
type _OVERLOADTYPES interface{} | ||
type _TOTYPE interface{} | ||
type _FROMTYPE interface{} | ||
|
||
var _ apd.Decimal | ||
|
||
//var _ types.T | ||
//var _ bytes.Buffer | ||
|
||
func _ASSIGN_CAST(to, from interface{}) { | ||
panic("") | ||
} | ||
|
||
// */}} | ||
|
||
func GetCastOperator( | ||
input Operator, colIdx int, resultIdx int, fromType *semtypes.T, toType *semtypes.T, | ||
) (Operator, error) { | ||
switch from := conv.FromColumnType(fromType); from { | ||
// {{ range $typ, $overloads := . }} | ||
case types._ALLTYPES: | ||
switch to := conv.FromColumnType(toType); to { | ||
// {{ range $overloads }} | ||
// {{ if isCastFuncSet . }} | ||
case types._OVERLOADTYPES: | ||
return &castOp_FROMTYPE_TOTYPE{ | ||
OneInputNode: NewOneInputNode(input), | ||
colIdx: colIdx, | ||
outputIdx: resultIdx, | ||
fromType: from, | ||
toType: to, | ||
}, nil | ||
// {{end}} | ||
// {{end}} | ||
default: | ||
return nil, errors.Errorf("unhandled cast FROM -> TO type: %s -> %s", from, to) | ||
} | ||
// {{end}} | ||
default: | ||
return nil, errors.Errorf("unhandled FROM type: %s", from) | ||
} | ||
} | ||
|
||
// {{ range $typ, $overloads := . }} | ||
// {{ range $overloads }} | ||
// {{ if isCastFuncSet . }} | ||
|
||
type castOp_FROMTYPE_TOTYPE struct { | ||
OneInputNode | ||
colIdx int | ||
outputIdx int | ||
fromType types.T | ||
toType types.T | ||
} | ||
|
||
var _ StaticMemoryOperator = &castOp_FROMTYPE_TOTYPE{} | ||
|
||
func (c *castOp_FROMTYPE_TOTYPE) EstimateStaticMemoryUsage() int { | ||
return EstimateBatchSizeBytes([]types.T{c.toType}, coldata.BatchSize) | ||
} | ||
|
||
func (c *castOp_FROMTYPE_TOTYPE) Init() { | ||
c.input.Init() | ||
} | ||
|
||
func (c *castOp_FROMTYPE_TOTYPE) Next(ctx context.Context) coldata.Batch { | ||
batch := c.input.Next(ctx) | ||
n := batch.Length() | ||
if n == 0 { | ||
return batch | ||
} | ||
if c.outputIdx == batch.Width() { | ||
batch.AppendCol(types._TOTYPE) | ||
} | ||
vec := batch.ColVec(c.colIdx) | ||
col := vec._FROMTYPE() | ||
projVec := batch.ColVec(c.outputIdx) | ||
projCol := projVec._TOTYPE() | ||
if vec.MaybeHasNulls() { | ||
vecNulls := vec.Nulls() | ||
projNulls := projVec.Nulls() | ||
if sel := batch.Selection(); sel != nil { | ||
for _, i := range sel { | ||
if vecNulls.NullAt(uint16(i)) { | ||
projNulls.SetNull(uint16(i)) | ||
} else { | ||
_ASSIGN_CAST(projCol[i], col[i]) | ||
} | ||
} | ||
} else { | ||
col = col[:n] | ||
projCol = projCol[:n] | ||
for i := range col { | ||
if vecNulls.NullAt(uint16(i)) { | ||
projNulls.SetNull(uint16(i)) | ||
} else { | ||
_ASSIGN_CAST(projCol[i], col[i]) | ||
} | ||
} | ||
} | ||
} else { | ||
if sel := batch.Selection(); sel != nil { | ||
for _, i := range sel { | ||
_ASSIGN_CAST(projCol[i], col[i]) | ||
} | ||
} else { | ||
for i := range col { | ||
_ASSIGN_CAST(projCol[i], col[i]) | ||
} | ||
} | ||
} | ||
return batch | ||
} | ||
|
||
// {{end}} | ||
// {{end}} | ||
// {{end}} |
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,49 @@ | ||
// Copyright 2019 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 main | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
func genCastOperators(wr io.Writer) error { | ||
t, err := ioutil.ReadFile("pkg/sql/exec/cast_operator_tmpl.go") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s := string(t) | ||
|
||
assignCast := makeFunctionRegex("_ASSIGN_CAST", 2) | ||
s = assignCast.ReplaceAllString(s, `{{.Assign "$1" "$2"}}`) | ||
s = strings.Replace(s, "_ALLTYPES", "{{$typ}}", -1) | ||
s = strings.Replace(s, "_OVERLOADTYPES", "{{.ToTyp}}", -1) | ||
s = strings.Replace(s, "_FROMTYPE", "{{.FromTyp}}", -1) | ||
s = strings.Replace(s, "_TOTYPE", "{{.ToTyp}}", -1) | ||
|
||
isCastFuncSet := func(ov castOverload) bool { | ||
return ov.AssignFunc != nil | ||
} | ||
|
||
tmpl, err := template.New("cast_operator").Funcs(template.FuncMap{"isCastFuncSet": isCastFuncSet}).Parse(s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return tmpl.Execute(wr, castOverloads) | ||
} | ||
|
||
func init() { | ||
registerGenerator(genCastOperators, "cast_operator.eg.go") | ||
} |
Oops, something went wrong.