Skip to content

Commit db54874

Browse files
authored
rule: fix squash rule with convert_dates (#827)
rule: fix squash rule with convert_dates
2 parents 3664533 + 9d4893d commit db54874

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

integration_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"strings"
1010
"testing"
11+
"time"
1112

1213
"github.com/src-d/gitbase/cmd/gitbase/command"
1314
"github.com/src-d/gitbase/internal/rule"
@@ -354,6 +355,30 @@ func TestIntegration(t *testing.T) {
354355
WHERE r.ref_name = 'HEAD' AND IS_VENDOR(cf.file_path)`,
355356
[]sql.Row{{".gitignore"}, {"vendor/foo.go"}},
356357
},
358+
{
359+
`
360+
SELECT f.file_path, c.commit_author_when
361+
FROM repositories r
362+
NATURAL JOIN commits c
363+
NATURAL JOIN commit_files cf
364+
NATURAL JOIN files f
365+
WHERE r.repository_id = 'worktree'
366+
AND c.commit_hash = '6ecf0ef2c2dffb796033e5a02219af86ec6584e5'
367+
AND f.file_path NOT REGEXP '^vendor.*'
368+
AND NOT IS_BINARY(f.blob_content)
369+
LIMIT 10
370+
`,
371+
[]sql.Row{
372+
{".gitignore", time.Date(2015, time.April, 5, 21, 30, 47, 0, time.UTC)},
373+
{"CHANGELOG", time.Date(2015, time.April, 5, 21, 30, 47, 0, time.UTC)},
374+
{"LICENSE", time.Date(2015, time.April, 5, 21, 30, 47, 0, time.UTC)},
375+
{"binary.jpg", time.Date(2015, time.April, 5, 21, 30, 47, 0, time.UTC)},
376+
{"go/example.go", time.Date(2015, time.April, 5, 21, 30, 47, 0, time.UTC)},
377+
{"json/long.json", time.Date(2015, time.April, 5, 21, 30, 47, 0, time.UTC)},
378+
{"json/short.json", time.Date(2015, time.April, 5, 21, 30, 47, 0, time.UTC)},
379+
{"php/crappy.php", time.Date(2015, time.April, 5, 21, 30, 47, 0, time.UTC)},
380+
},
381+
},
357382
}
358383

359384
var pid uint64

internal/rule/squashjoins.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,32 @@ func squashProjects(parent, child *plan.Project) (sql.Node, bool) {
111111
projections := make([]sql.Expression, len(parent.Expressions()))
112112
schema := child.Child.Schema()
113113

114+
// When squashing two projects, it's possible that the parent project has
115+
// a reference to a column defined in the child project.
116+
// For that reason, we need to gather the new columns that were defined
117+
// in the child project in order to replace the reference to those in the
118+
// parent project with the new column definition.
119+
var newColumns = make(map[string]sql.Expression)
120+
for _, e := range child.Expressions() {
121+
if _, ok := e.(*expression.GetField); !ok {
122+
var name string
123+
if n, ok := e.(sql.Nameable); ok {
124+
name = n.Name()
125+
} else {
126+
name = e.String()
127+
}
128+
129+
newColumns[name] = e
130+
}
131+
}
132+
114133
for i, e := range parent.Expressions() {
134+
if f, ok := e.(*expression.GetField); ok && f.Table() == "" {
135+
if expr, ok := newColumns[f.Name()]; ok {
136+
e = expr
137+
}
138+
}
139+
115140
fe, err := fixFieldIndexes(e, schema)
116141
if err != nil {
117142
return nil, false

internal/rule/squashjoins_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ func TestAnalyzeSquashNaturalJoins(t *testing.T) {
8181
project, ok := exchange.Child.(*plan.Project)
8282
require.True(ok)
8383

84-
project, ok = project.Child.(*plan.Project)
85-
require.True(ok)
86-
8784
filter, ok := project.Child.(*plan.Filter)
8885
require.True(ok)
8986

0 commit comments

Comments
 (0)