Skip to content

Commit

Permalink
execgen: try not to touch .eg.go files
Browse files Browse the repository at this point in the history
This changes the Makefile to output .eg.go output to a tmp file before
actually editing .eg.go files, to prevent build churn.

Also, fix a bug in one of the generators that causes it to output
generated code in an inconsistent (but semantically identical) manner.

Release note: None
  • Loading branch information
jordanlewis committed Mar 5, 2020
1 parent 9d684ea commit 1dca38f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,8 @@ pkg/sql/colexec/vec_comparators.eg.go: pkg/sql/colexec/vec_comparators_tmpl.go
pkg/sql/colexec/window_peer_grouper.eg.go: pkg/sql/colexec/window_peer_grouper_tmpl.go

$(EXECGEN_TARGETS): bin/execgen
execgen $@
execgen $@ > $@.tmp; cmp $@.tmp $@ && rm -f $@.tmp || mv $@.tmp $@


optgen-defs := pkg/sql/opt/ops/*.opt
optgen-norm-rules := pkg/sql/opt/norm/rules/*.opt
Expand Down
21 changes: 5 additions & 16 deletions pkg/sql/colexec/execgen/cmd/execgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (g *execgen) run(args ...string) bool {
g.reportError(errors.Errorf("unrecognized filename: %s", file))
return false
}
if err := g.generate(gen, out); err != nil {
if err := g.generate(gen); err != nil {
g.reportError(err)
return false
}
Expand All @@ -96,7 +96,7 @@ func (g *execgen) run(args ...string) bool {
var emptyCommentRegex = regexp.MustCompile(`[ \t]*//[ \t]*\n`)
var emptyBlockCommentRegex = regexp.MustCompile(`[ \t]*/\*[ \t]*\*/[ \t]*\n`)

func (g *execgen) generate(genFunc generator, out string) error {
func (g *execgen) generate(genFunc generator) error {
var buf bytes.Buffer
buf.WriteString("// Code generated by execgen; DO NOT EDIT.\n")

Expand All @@ -122,23 +122,12 @@ func (g *execgen) generate(genFunc generator, out string) error {
b = buf.Bytes()
}

// Ignore any write error if another error already occurred.
_, writeErr := os.Stdout.Write(b)
if err != nil {
// Ignore any write error if another error already occurred.
_ = g.writeOutputFile(b, out)
return err
}
return g.writeOutputFile(b, out)
}

func (g *execgen) writeOutputFile(b []byte, out string) error {
file, err := os.Create(out)
if err != nil {
return err
}
defer file.Close()

_, err = file.Write(b)
return err
return writeErr
}

// usage is a replacement usage function for the flags package.
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/colexec/execgen/cmd/execgen/rowstovec_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package main
import (
"io"
"io/ioutil"
"sort"
"strings"
"text/template"

Expand Down Expand Up @@ -97,8 +98,15 @@ func genRowsToVec(wr io.Writer) error {

columnConversions := make([]columnConversion, 0, len(conversionsMap))
for _, conversion := range conversionsMap {
sort.Slice(conversion.Widths, func(i, j int) bool {
return conversion.Widths[i].Width < conversion.Widths[j].Width
})
columnConversions = append(columnConversions, *conversion)
}
// Sort the list so that we output in a consistent order.
sort.Slice(columnConversions, func(i, j int) bool {
return strings.Compare(columnConversions[i].Family, columnConversions[j].Family) < 0
})
return tmpl.Execute(wr, columnConversions)
}

Expand Down

0 comments on commit 1dca38f

Please sign in to comment.