Skip to content

Commit 748a50a

Browse files
committed
internal/rule: fix projections when squash joins
Signed-off-by: Manuel Carmona <[email protected]>
1 parent 593e510 commit 748a50a

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

integration_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,52 @@ func TestIntegration(t *testing.T) {
183183
{int32(3), "worktree", "[email protected]"},
184184
},
185185
},
186+
{
187+
`SELECT
188+
c.commit_hash AS hash,
189+
c.commit_message AS message,
190+
commit_author_name AS author,
191+
te.tree_entry_name AS file
192+
FROM
193+
commits c
194+
NATURAL JOIN commit_trees
195+
NATURAL JOIN tree_entries as te
196+
WHERE te.repository_id='worktree'
197+
LIMIT 8`,
198+
[]sql.Row{
199+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "Máximo Cuadros Ortiz", ".gitignore"},
200+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "Máximo Cuadros Ortiz", "CHANGELOG"},
201+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "Máximo Cuadros Ortiz", "LICENSE"},
202+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "Máximo Cuadros Ortiz", "README"},
203+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "Máximo Cuadros Ortiz", "binary.jpg"},
204+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "Máximo Cuadros Ortiz", "go"},
205+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "Máximo Cuadros Ortiz", "json"},
206+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "Máximo Cuadros Ortiz", "php"},
207+
},
208+
},
209+
{
210+
`SELECT
211+
c.commit_hash AS hash,
212+
c.commit_message AS message,
213+
te.tree_entry_name AS file,
214+
te.tree_hash AS thash
215+
FROM
216+
commits c
217+
NATURAL JOIN commit_trees
218+
NATURAL JOIN tree_entries as te
219+
WHERE te.repository_id='worktree'
220+
LIMIT 8`,
221+
[]sql.Row{
222+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", ".gitignore", "dbd3641b371024f44d0e469a9c8f5457b0660de1"},
223+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "CHANGELOG", "dbd3641b371024f44d0e469a9c8f5457b0660de1"},
224+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "LICENSE", "dbd3641b371024f44d0e469a9c8f5457b0660de1"},
225+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "README", "dbd3641b371024f44d0e469a9c8f5457b0660de1"},
226+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "binary.jpg", "dbd3641b371024f44d0e469a9c8f5457b0660de1"},
227+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "go", "dbd3641b371024f44d0e469a9c8f5457b0660de1"},
228+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "json", "dbd3641b371024f44d0e469a9c8f5457b0660de1"},
229+
{"e8d3ffab552895c19b9fcf7aa264d277cde33881", "some code in a branch\n", "php", "dbd3641b371024f44d0e469a9c8f5457b0660de1"},
230+
},
231+
},
186232
}
187233

188234
runTests := func(t *testing.T) {

internal/rule/squashjoins.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,16 @@ func countProjectSquashes(n sql.Node) int {
108108
}
109109

110110
func squashProjects(parent, child *plan.Project) (sql.Node, bool) {
111-
var projections []sql.Expression
112-
for _, expr := range parent.Expressions() {
113-
parentField, ok := expr.(*expression.GetField)
114-
if !ok {
115-
return nil, false
116-
}
117-
118-
index := parentField.Index()
119-
for _, e := range child.Expressions() {
120-
childField, ok := e.(*expression.GetField)
121-
if !ok {
122-
return nil, false
123-
}
111+
projections := make([]sql.Expression, len(parent.Expressions()))
112+
schema := child.Child.Schema()
124113

125-
if referenceSameColumn(parentField, childField) {
126-
index = childField.Index()
127-
}
114+
for i, e := range parent.Expressions() {
115+
fe, err := fixFieldIndexes(e, schema)
116+
if err != nil {
117+
return nil, false
128118
}
129119

130-
projection := expression.NewGetFieldWithTable(
131-
index,
132-
parentField.Type(),
133-
parentField.Table(),
134-
parentField.Name(),
135-
parentField.IsNullable(),
136-
)
137-
138-
projections = append(projections, projection)
120+
projections[i] = fe
139121
}
140122

141123
return plan.NewProject(projections, child.Child), true

0 commit comments

Comments
 (0)