Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ func TestIntegration(t *testing.T) {
{int32(4), "b029517f6300c2da0f4b651b8642506cd6aaf45d"},
},
},
{
`SELECT count(1), refs.repository_id
FROM refs
NATURAL JOIN commits
NATURAL JOIN commit_blobs
WHERE refs.ref_name = 'HEAD'
GROUP BY refs.repository_id`,
[]sql.Row{
{int32(9), "worktree"},
},
},
}

runTests := func(t *testing.T) {
Expand Down
87 changes: 86 additions & 1 deletion internal/rule/squashjoins.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func SquashJoins(
defer span.Finish()

a.Log("squashing joins, node of type %T", n)

projectSquashes := countProjectSquashes(n)

n, err := n.TransformUp(func(n sql.Node) (sql.Node, error) {
join, ok := n.(*plan.InnerJoin)
if !ok {
Expand All @@ -39,18 +42,100 @@ func SquashJoins(

return squashJoin(join)
})

if err != nil {
return nil, err
}

return n.TransformUp(func(n sql.Node) (sql.Node, error) {
n, err = n.TransformUp(func(n sql.Node) (sql.Node, error) {
t, ok := n.(*joinedTables)
if !ok {
return n, nil
}

return buildSquashedTable(t.tables, t.filters, t.columns, t.indexes)
})

if err != nil {
return nil, err
}

return n.TransformUp(func(n sql.Node) (sql.Node, error) {
if projectSquashes <= 0 {
return n, nil
}

project, ok := n.(*plan.Project)
if !ok {
return n, nil
}

child, ok := project.Child.(*plan.Project)
if !ok {
return n, nil
}

squashedProject, ok := squashProjects(project, child)
if !ok {
return n, nil
}

projectSquashes--
return squashedProject, nil
})
}

func countProjectSquashes(n sql.Node) int {
var squashableProjects int
plan.Inspect(n, func(node sql.Node) bool {
if project, ok := node.(*plan.Project); ok {
if _, ok := project.Child.(*plan.InnerJoin); ok {
squashableProjects++
}
}

return true
})

return squashableProjects - 1
}

func squashProjects(parent, child *plan.Project) (sql.Node, bool) {
projections := []sql.Expression{}
for _, expr := range parent.Expressions() {
parentField, ok := expr.(*expression.GetField)
if !ok {
return nil, false
}

index := parentField.Index()
for _, e := range child.Expressions() {
childField, ok := e.(*expression.GetField)
if !ok {
return nil, false
}

if referenceSameColumn(parentField, childField) {
index = childField.Index()
}
}

projection := expression.NewGetFieldWithTable(
index,
parentField.Type(),
parentField.Table(),
parentField.Name(),
parentField.IsNullable(),
)

projections = append(projections, projection)
}

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

func referenceSameColumn(parent, child *expression.GetField) bool {
return parent.Name() == child.Name() && parent.Table() == child.Table()
}

func squashJoin(join *plan.InnerJoin) (sql.Node, error) {
Expand Down