Skip to content

Commit

Permalink
Merge #49727
Browse files Browse the repository at this point in the history
49727: execgen: some small improvements r=jordanlewis a=jordanlewis

- Use `imports` to avoid having to precisely specify template imports
- Use `strings.Replacer` to streamline some code

Co-authored-by: Jordan Lewis <jordanthelewis@gmail.com>
  • Loading branch information
craig[bot] and jordanlewis committed May 30, 2020
2 parents 47a6a02 + 2735048 commit 77fa68e
Show file tree
Hide file tree
Showing 46 changed files with 230 additions and 632 deletions.
18 changes: 0 additions & 18 deletions pkg/col/coldata/vec_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,22 @@ package coldata

import (
"fmt"
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/typeconv"
// {{/*
"github.com/cockroachdb/cockroach/pkg/sql/colexec/execgen"
// */}}
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/duration"
)

// {{/*

// _GOTYPESLICE is the template variable.
type _GOTYPESLICE interface{}

// Dummy import to pull in "typeconv" package.
var _ = typeconv.DatumVecCanonicalTypeFamily

// _CANONICAL_TYPE_FAMILY is the template variable.
const _CANONICAL_TYPE_FAMILY = types.UnknownFamily

// _TYPE_WIDTH is the template variable.
const _TYPE_WIDTH = 0

// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// Dummy import to pull in "duration" package.
var _ duration.Duration

// */}}

func (m *memColumn) Append(args SliceArgs) {
Expand Down
12 changes: 0 additions & 12 deletions pkg/sql/colexec/any_not_null_agg_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
package colexec

import (
"time"
"unsafe"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/typeconv"
"github.com/cockroachdb/cockroach/pkg/sql/colexec/execgen"
"github.com/cockroachdb/cockroach/pkg/sql/colmem"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/errors"
)

Expand All @@ -40,15 +37,6 @@ var _ = execgen.UNSAFEGET

// Declarations to make the template compile properly.

// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// Dummy import to pull in "duration" package.
var _ duration.Duration

// _GOTYPESLICE is the template variable.
type _GOTYPESLICE interface{}

Expand Down
8 changes: 0 additions & 8 deletions pkg/sql/colexec/avg_agg_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,17 @@ package colexec
import (
"unsafe"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/typeconv"
"github.com/cockroachdb/cockroach/pkg/sql/colexecbase/colexecerror"
"github.com/cockroachdb/cockroach/pkg/sql/colmem"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/errors"
)

// {{/*
// Declarations to make the template compile properly

// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "tree" package.
var _ tree.Datum

// _CANONICAL_TYPE_FAMILY is the template variable.
const _CANONICAL_TYPE_FAMILY = types.UnknownFamily

Expand Down
12 changes: 0 additions & 12 deletions pkg/sql/colexec/const_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ package colexec

import (
"context"
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/typeconv"
"github.com/cockroachdb/cockroach/pkg/sql/colexec/execgen"
"github.com/cockroachdb/cockroach/pkg/sql/colexecbase"
"github.com/cockroachdb/cockroach/pkg/sql/colmem"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/errors"
)

Expand All @@ -41,15 +38,6 @@ var _ = execgen.UNSAFEGET

// Declarations to make the template compile properly.

// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// Dummy import to pull in "duration" package.
var _ duration.Duration

// _GOTYPE is the template variable.
type _GOTYPE interface{}

Expand Down
28 changes: 0 additions & 28 deletions pkg/sql/colexec/distinct_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,14 @@
package colexec

import (
"bytes"
"context"
"math"
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/coldataext"
"github.com/cockroachdb/cockroach/pkg/col/typeconv"
"github.com/cockroachdb/cockroach/pkg/sql/colexec/execgen"
"github.com/cockroachdb/cockroach/pkg/sql/colexecbase"
"github.com/cockroachdb/cockroach/pkg/sql/colexecbase/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/errors"
)

Expand Down Expand Up @@ -98,27 +91,6 @@ var _ = execgen.UNSAFEGET

// Declarations to make the template compile properly.

// Dummy import to pull in "bytes" package.
var _ bytes.Buffer

// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// Dummy import to pull in "duration" package.
var _ duration.Duration

// Dummy import to pull in "tree" package.
var _ tree.Datum

// Dummy import to pull in "math" package.
var _ = math.MaxInt64

// Dummy import to pull in "coldataext" package.
var _ coldataext.Datum

// _GOTYPE is the template variable.
type _GOTYPE interface{}

Expand Down
14 changes: 8 additions & 6 deletions pkg/sql/colexec/execgen/cmd/execgen/and_or_projection_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ func genAndOrProjectionOps(wr io.Writer) error {
return err
}

s := string(t)
s = strings.ReplaceAll(s, "_OP_LOWER", "{{.Lower}}")
s = strings.ReplaceAll(s, "_OP_TITLE", "{{.Title}}")
s = strings.ReplaceAll(s, "_IS_OR_OP", ".IsOr")
s = strings.ReplaceAll(s, "_L_HAS_NULLS", "$.lHasNulls")
s = strings.ReplaceAll(s, "_R_HAS_NULLS", "$.rHasNulls")
r := strings.NewReplacer(
"_OP_LOWER", "{{.Lower}}",
"_OP_TITLE", "{{.Title}}",
"_IS_OR_OP", ".IsOr",
"_L_HAS_NULLS", "$.lHasNulls",
"_R_HAS_NULLS", "$.rHasNulls",
)
s := r.Replace(string(t))

addTupleForRight := makeFunctionRegex("_ADD_TUPLE_FOR_RIGHT", 1)
s = addTupleForRight.ReplaceAllString(s, `{{template "addTupleForRight" buildDict "Global" $ "lHasNulls" $1}}`)
Expand Down
17 changes: 9 additions & 8 deletions pkg/sql/colexec/execgen/cmd/execgen/any_not_null_agg_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ func genAnyNotNullAgg(wr io.Writer) error {
return err
}

s := string(t)

s = strings.ReplaceAll(s, "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}")
s = strings.ReplaceAll(s, "_TYPE_WIDTH", typeWidthReplacement)
s = strings.ReplaceAll(s, "_GOTYPESLICE", "{{.GoTypeSliceName}}")
s = strings.ReplaceAll(s, "_GOTYPE", "{{.GoType}}")
s = strings.ReplaceAll(s, "_TYPE", "{{.VecMethod}}")
s = strings.ReplaceAll(s, "TemplateType", "{{.VecMethod}}")
r := strings.NewReplacer(
"_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}",
"_TYPE_WIDTH", typeWidthReplacement,
"_GOTYPESLICE", "{{.GoTypeSliceName}}",
"_GOTYPE", "{{.GoType}}",
"_TYPE", "{{.VecMethod}}",
"TemplateType", "{{.VecMethod}}",
)
s := r.Replace(string(t))

findAnyNotNull := makeFunctionRegex("_FIND_ANY_NOT_NULL", 4)
s = findAnyNotNull.ReplaceAllString(s, `{{template "findAnyNotNull" buildDict "Global" . "HasNulls" $4}}`)
Expand Down
17 changes: 9 additions & 8 deletions pkg/sql/colexec/execgen/cmd/execgen/avg_agg_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ func genAvgAgg(wr io.Writer) error {
return err
}

s := string(t)

s = strings.ReplaceAll(s, "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}")
s = strings.ReplaceAll(s, "_TYPE_WIDTH", typeWidthReplacement)
s = strings.ReplaceAll(s, "_GOTYPESLICE", "{{.GoTypeSliceName}}")
s = strings.ReplaceAll(s, "_GOTYPE", "{{.GoType}}")
s = strings.ReplaceAll(s, "_TYPE", "{{.VecMethod}}")
s = strings.ReplaceAll(s, "TemplateType", "{{.VecMethod}}")
r := strings.NewReplacer(
"_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}",
"_TYPE_WIDTH", typeWidthReplacement,
"_GOTYPESLICE", "{{.GoTypeSliceName}}",
"_GOTYPE", "{{.GoType}}",
"_TYPE", "{{.VecMethod}}",
"TemplateType", "{{.VecMethod}}",
)
s := r.Replace(string(t))

assignDivRe := makeFunctionRegex("_ASSIGN_DIV_INT64", 6)
s = assignDivRe.ReplaceAllString(s, makeTemplateFunctionCall("AssignDivInt64", 6))
Expand Down
9 changes: 5 additions & 4 deletions pkg/sql/colexec/execgen/cmd/execgen/bool_and_or_agg_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ func genBooleanAgg(wr io.Writer) error {
return err
}

s := string(t)

s = strings.ReplaceAll(s, "_OP_TYPE", "{{.OpType}}")
s = strings.ReplaceAll(s, "_DEFAULT_VAL", "{{.DefaultVal}}")
r := strings.NewReplacer(
"_OP_TYPE", "{{.OpType}}",
"_DEFAULT_VAL", "{{.DefaultVal}}",
)
s := r.Replace(string(t))

accumulateBoolean := makeFunctionRegex("_ACCUMULATE_BOOLEAN", 3)
s = accumulateBoolean.ReplaceAllString(s, `{{template "accumulateBoolean" buildDict "Global" .}}`)
Expand Down
19 changes: 10 additions & 9 deletions pkg/sql/colexec/execgen/cmd/execgen/cast_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ func genCastOperators(wr io.Writer) error {
return err
}

s := string(t)

s = strings.ReplaceAll(s, "_LEFT_CANONICAL_TYPE_FAMILY", "{{.LeftCanonicalFamilyStr}}")
s = strings.ReplaceAll(s, "_LEFT_TYPE_WIDTH", typeWidthReplacement)
s = strings.ReplaceAll(s, "_RIGHT_CANONICAL_TYPE_FAMILY", "{{.RightCanonicalFamilyStr}}")
s = strings.ReplaceAll(s, "_RIGHT_TYPE_WIDTH", typeWidthReplacement)
s = strings.ReplaceAll(s, "_R_GO_TYPE", "{{.Right.GoType}}")
s = strings.ReplaceAll(s, "_L_TYP", "{{.Left.VecMethod}}")
s = strings.ReplaceAll(s, "_R_TYP", "{{.Right.VecMethod}}")
r := strings.NewReplacer(
"_LEFT_CANONICAL_TYPE_FAMILY", "{{.LeftCanonicalFamilyStr}}",
"_LEFT_TYPE_WIDTH", typeWidthReplacement,
"_RIGHT_CANONICAL_TYPE_FAMILY", "{{.RightCanonicalFamilyStr}}",
"_RIGHT_TYPE_WIDTH", typeWidthReplacement,
"_R_GO_TYPE", "{{.Right.GoType}}",
"_L_TYP", "{{.Left.VecMethod}}",
"_R_TYP", "{{.Right.VecMethod}}",
)
s := r.Replace(string(t))

castRe := makeFunctionRegex("_CAST", 2)
s = castRe.ReplaceAllString(s, makeTemplateFunctionCall("Right.Cast", 2))
Expand Down
15 changes: 8 additions & 7 deletions pkg/sql/colexec/execgen/cmd/execgen/const_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ func genConstOps(wr io.Writer) error {
return err
}

s := string(d)

s = strings.ReplaceAll(s, "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}")
s = strings.ReplaceAll(s, "_TYPE_WIDTH", typeWidthReplacement)
s = strings.ReplaceAll(s, "_GOTYPE", "{{.GoType}}")
s = strings.ReplaceAll(s, "_TYPE", "{{.VecMethod}}")
s = strings.ReplaceAll(s, "TemplateType", "{{.VecMethod}}")
r := strings.NewReplacer(
"_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}",
"_TYPE_WIDTH", typeWidthReplacement,
"_GOTYPE", "{{.GoType}}",
"_TYPE", "{{.VecMethod}}",
"TemplateType", "{{.VecMethod}}",
)
s := r.Replace(string(d))

s = replaceManipulationFuncs(s)

Expand Down
16 changes: 8 additions & 8 deletions pkg/sql/colexec/execgen/cmd/execgen/distinct_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func genDistinctOps(wr io.Writer) error {
return err
}

s := string(d)

s = strings.ReplaceAll(s, "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}")
s = strings.ReplaceAll(s, "_TYPE_WIDTH", typeWidthReplacement)
s = strings.ReplaceAll(s, "_GOTYPESLICE", "{{.GoTypeSliceName}}")
s = strings.ReplaceAll(s, "_GOTYPE", "{{.GoType}}")
s = strings.ReplaceAll(s, "_TYPE", "{{.VecMethod}}")
s = strings.ReplaceAll(s, "TemplateType", "{{.VecMethod}}")
r := strings.NewReplacer(
"_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}",
"_TYPE_WIDTH", typeWidthReplacement,
"_GOTYPESLICE", "{{.GoTypeSliceName}}",
"_GOTYPE", "{{.GoType}}",
"_TYPE", "{{.VecMethod}}",
"TemplateType", "{{.VecMethod}}")
s := r.Replace(string(d))

assignNeRe := makeFunctionRegex("_ASSIGN_NE", 6)
s = assignNeRe.ReplaceAllString(s, makeTemplateFunctionCall("Assign", 6))
Expand Down
11 changes: 6 additions & 5 deletions pkg/sql/colexec/execgen/cmd/execgen/hash_aggregator_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ func genHashAggregator(wr io.Writer) error {
return err
}

s := string(t)

s = strings.ReplaceAll(s, "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}")
s = strings.ReplaceAll(s, "_TYPE_WIDTH", typeWidthReplacement)
s = strings.ReplaceAll(s, "TemplateType", "{{.VecMethod}}")
r := strings.NewReplacer(
"_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}",
"_TYPE_WIDTH", typeWidthReplacement,
"TemplateType", "{{.VecMethod}}",
)
s := r.Replace(string(t))

s = replaceManipulationFuncsAmbiguous(".Global", s)

Expand Down
13 changes: 7 additions & 6 deletions pkg/sql/colexec/execgen/cmd/execgen/hash_utils_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ func genHashUtils(wr io.Writer) error {
return err
}

s := string(t)

s = strings.ReplaceAll(s, "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}")
s = strings.ReplaceAll(s, "_TYPE_WIDTH", typeWidthReplacement)
s = strings.ReplaceAll(s, "_TYPE", "{{.VecMethod}}")
s = strings.ReplaceAll(s, "TemplateType", "{{.VecMethod}}")
r := strings.NewReplacer(
"_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}",
"_TYPE_WIDTH", typeWidthReplacement,
"_TYPE", "{{.VecMethod}}",
"TemplateType", "{{.VecMethod}}",
)
s := r.Replace(string(t))

assignHash := makeFunctionRegex("_ASSIGN_HASH", 4)
s = assignHash.ReplaceAllString(s, makeTemplateFunctionCall("Global.UnaryAssign", 4))
Expand Down
Loading

0 comments on commit 77fa68e

Please sign in to comment.