Skip to content

Commit a471062

Browse files
author
James Cor
committed
refactor and tidy
1 parent 3bd72cc commit a471062

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

sql/planbuilder/factory.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package planbuilder
1616

1717
import (
18+
"strings"
19+
1820
"github.com/dolthub/go-mysql-server/sql"
1921
"github.com/dolthub/go-mysql-server/sql/expression"
2022
"github.com/dolthub/go-mysql-server/sql/plan"
@@ -175,3 +177,30 @@ func (f *factory) buildTableAlias(name string, child sql.Node) (plan.TableIdNode
175177
}
176178
}
177179
}
180+
181+
func (f *factory) buildDistinct(child sql.Node) sql.Node {
182+
if proj, isProj := child.(*plan.Project); isProj {
183+
// TODO: if projection columns are just primary key, distinct is no-op
184+
// TODO: distinct literals are just one row
185+
if sort, isSort := proj.Child.(*plan.Sort); isSort {
186+
projMap := make(map[string]struct{})
187+
for _, p := range proj.Projections {
188+
projMap[strings.ToLower(p.String())] = struct{}{}
189+
}
190+
hasDiff := false
191+
for _, s := range sort.SortFields {
192+
if _, ok := projMap[strings.ToLower(s.Column.String())]; !ok {
193+
hasDiff = true
194+
break
195+
}
196+
}
197+
if !hasDiff {
198+
proj.Child = sort.Child
199+
sort.Child = plan.NewDistinct(proj)
200+
return sort
201+
}
202+
}
203+
}
204+
return plan.NewDistinct(child)
205+
}
206+

sql/planbuilder/select.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -198,33 +198,7 @@ func (b *Builder) buildDistinct(inScope *scope, distinct bool) {
198198
if !distinct {
199199
return
200200
}
201-
// TODO: move this to factory.go
202-
node := inScope.node
203-
if proj, isProj := node.(*plan.Project); isProj {
204-
// TODO: if projection columns are just primary key, distinct is no-op
205-
// TODO: distinct literals are just one row
206-
if sort, isSort := proj.Child.(*plan.Sort); isSort {
207-
projMap := make(map[string]struct{})
208-
for _, p := range proj.Projections {
209-
// TODO: to lower
210-
projMap[p.String()] = struct{}{}
211-
}
212-
hasDiff := false
213-
for _, s := range sort.SortFields {
214-
if _, ok := projMap[s.Column.String()]; !ok {
215-
hasDiff = true
216-
break
217-
}
218-
}
219-
if !hasDiff {
220-
proj.Child = sort.Child
221-
sort.Child = plan.NewDistinct(proj)
222-
inScope.node = sort
223-
return
224-
}
225-
}
226-
}
227-
inScope.node = plan.NewDistinct(node)
201+
inScope.node = b.f.buildDistinct(inScope.node)
228202
}
229203

230204
func (b *Builder) currentDb() sql.Database {

0 commit comments

Comments
 (0)