Skip to content

Commit

Permalink
exec: template out rank and dense rank into separate operators
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
yuzefovich committed May 3, 2019
1 parent 4b3b76e commit ae64952
Show file tree
Hide file tree
Showing 5 changed files with 389 additions and 135 deletions.
54 changes: 51 additions & 3 deletions pkg/sql/exec/execgen/cmd/execgen/rank_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,48 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"regexp"
"strings"
"text/template"
)

type rankTmplInfo struct {
Dense bool
HasPartition bool
}

func (r rankTmplInfo) UpdateRank() string {
switch r.Dense {
case true:
return fmt.Sprintf(
`r.rank++`,
)
case false:
return fmt.Sprintf(
`r.rank += r.rankIncrement
r.rankIncrement = 1`,
)
default:
panic("third value of boolean?")
}
}

func (r rankTmplInfo) UpdateRankIncrement() string {
switch r.Dense {
case true:
return ``
case false:
return fmt.Sprintf(
`r.rankIncrement++`,
)
default:
panic("third value of boolean?")
}
}

func genRankOps(wr io.Writer) error {
d, err := ioutil.ReadFile("pkg/sql/exec/vecbuiltins/rank_tmpl.go")
if err != nil {
Expand All @@ -28,16 +65,27 @@ func genRankOps(wr io.Writer) error {

s := string(d)

nextRank := makeFunctionRegex("_NEXT_RANK", 1)
s = nextRank.ReplaceAllString(s, `{{template "nextRank" buildDict "Global" $ "HasPartition" $1 }}`)
s = strings.Replace(s, "_DENSE", "{{.Dense}}", -1)
s = strings.Replace(s, "_PARTITION", "{{.HasPartition}}", -1)

updateRankRe := regexp.MustCompile(`_UPDATE_RANK\(\)`)
s = updateRankRe.ReplaceAllString(s, "{{.UpdateRank}}")
updateRankIncrementRe := regexp.MustCompile(`_UPDATE_RANK_INCREMENT\(\)`)
s = updateRankIncrementRe.ReplaceAllString(s, "{{.UpdateRankIncrement}}")

// Now, generate the op, from the template.
tmpl, err := template.New("rank_op").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s)
if err != nil {
return err
}

return tmpl.Execute(wr, struct{}{})
rankTmplInfos := []rankTmplInfo{
{Dense: false, HasPartition: false},
{Dense: false, HasPartition: true},
{Dense: true, HasPartition: false},
{Dense: true, HasPartition: true},
}
return tmpl.Execute(wr, rankTmplInfos)
}

func init() {
Expand Down
Loading

0 comments on commit ae64952

Please sign in to comment.