-
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: template out RANK and ROW_NUMBER
Templates out rankOp into two operators (for dense and non-dense case) and templates out support for PARTITION BY clause for both rankOp and rowNumberOp. The code is undertested and is lacking benchmarks, but that will be addressed soon. Release note: None
- Loading branch information
1 parent
4aa32dc
commit 02332ed
Showing
9 changed files
with
400 additions
and
212 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
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,97 @@ | ||
// Copyright 2019 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"regexp" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
type rankTmplInfo struct { | ||
Dense bool | ||
HasPartition bool | ||
} | ||
|
||
// UpdateRank is used to encompass the different logic between DENSE_RANK and | ||
// RANK window functions when updating the internal state of rank operators. It | ||
// is used by the template. | ||
func (r rankTmplInfo) UpdateRank() string { | ||
if r.Dense { | ||
return fmt.Sprintf( | ||
`r.rank++`, | ||
) | ||
} | ||
return fmt.Sprintf( | ||
`r.rank += r.rankIncrement | ||
r.rankIncrement = 1`, | ||
) | ||
} | ||
|
||
// UpdateRankIncrement is used to encompass the different logic between | ||
// DENSE_RANK and RANK window functions when updating the internal state of | ||
// rank operators. It is used by the template. | ||
func (r rankTmplInfo) UpdateRankIncrement() string { | ||
if r.Dense { | ||
return `` | ||
} | ||
return fmt.Sprintf( | ||
`r.rankIncrement++`, | ||
) | ||
} | ||
|
||
// Avoid unused warnings. These methods are used in the template. | ||
var ( | ||
_ = rankTmplInfo{}.UpdateRank() | ||
_ = rankTmplInfo{}.UpdateRankIncrement() | ||
) | ||
|
||
func genRankOps(wr io.Writer) error { | ||
d, err := ioutil.ReadFile("pkg/sql/exec/vecbuiltins/rank_tmpl.go") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s := string(d) | ||
|
||
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 | ||
} | ||
|
||
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() { | ||
registerGenerator(genRankOps, "rank.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,45 @@ | ||
// Copyright 2019 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"text/template" | ||
) | ||
|
||
func genRowNumberOp(wr io.Writer) error { | ||
d, err := ioutil.ReadFile("pkg/sql/exec/vecbuiltins/row_number_tmpl.go") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s := string(d) | ||
|
||
nextRowNumber := makeFunctionRegex("_NEXT_ROW_NUMBER_", 1) | ||
s = nextRowNumber.ReplaceAllString(s, `{{template "nextRowNumber" buildDict "Global" $ "HasPartition" $1 }}`) | ||
|
||
// Now, generate the op, from the template. | ||
tmpl, err := template.New("row_number_op").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return tmpl.Execute(wr, struct{}{}) | ||
} | ||
|
||
func init() { | ||
registerGenerator(genRowNumberOp, "row_number.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
Oops, something went wrong.