Skip to content

Commit

Permalink
updated the builder interface to return the new builder when doing a …
Browse files Browse the repository at this point in the history
…MakeDistinct()

Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Nov 16, 2020
1 parent de1e60d commit aa9df89
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 27 deletions.
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type builder interface {
PushSelect(pb *primitiveBuilder, expr *sqlparser.AliasedExpr, origin builder) (rc *resultColumn, colNumber int, err error)

// MakeDistinct makes the primitive handle the distinct clause.
MakeDistinct() error
MakeDistinct() (builder, error)
// PushGroupBy makes the primitive handle the GROUP BY clause.
PushGroupBy(sqlparser.GroupBy) error

Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/concatenate.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func (c *concatenate) PushSelect(pb *primitiveBuilder, expr *sqlparser.AliasedEx
return nil, 0, unreachable("Select")
}

func (c *concatenate) MakeDistinct() error {
return vterrors.New(vtrpc.Code_UNIMPLEMENTED, "only union-all is supported for this operator")
func (c *concatenate) MakeDistinct() (builder, error) {
return nil, vterrors.New(vtrpc.Code_UNIMPLEMENTED, "only union-all is supported for this operator")
}

func (c *concatenate) PushGroupBy(by sqlparser.GroupBy) error {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (d *distinct) PushSelect(pb *primitiveBuilder, expr *sqlparser.AliasedExpr,
return d.input.PushSelect(pb, expr, origin)
}

func (d *distinct) MakeDistinct() error {
return nil
func (d *distinct) MakeDistinct() (builder, error) {
return d, nil
}

func (d *distinct) PushGroupBy(by sqlparser.GroupBy) error {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ func (jb *join) PushSelect(pb *primitiveBuilder, expr *sqlparser.AliasedExpr, or
}

// MakeDistinct satisfies the builder interface.
func (jb *join) MakeDistinct() error {
return errors.New("unsupported: distinct on cross-shard join")
func (jb *join) MakeDistinct() (builder, error) {
return nil, errors.New("unsupported: distinct on cross-shard join")
}

// PushGroupBy satisfies the builder interface.
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func (l *limit) PushSelect(_ *primitiveBuilder, expr *sqlparser.AliasedExpr, ori
}

// MakeDistinct satisfies the builder interface.
func (l *limit) MakeDistinct() error {
return errors.New("limit.MakeDistinct: unreachable")
func (l *limit) MakeDistinct() (builder, error) {
return nil, errors.New("limit.MakeDistinct: unreachable")
}

// PushGroupBy satisfies the builder interface.
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/memory_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func (ms *memorySort) PushSelect(_ *primitiveBuilder, expr *sqlparser.AliasedExp
}

// MakeDistinct satisfies the builder interface.
func (ms *memorySort) MakeDistinct() error {
return errors.New("memorySort.MakeDistinct: unreachable")
func (ms *memorySort) MakeDistinct() (builder, error) {
return nil, errors.New("memorySort.MakeDistinct: unreachable")
}

// PushGroupBy satisfies the builder interface.
Expand Down
9 changes: 7 additions & 2 deletions go/vt/vtgate/planbuilder/merge_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ func (ms *mergeSort) PushSelect(pb *primitiveBuilder, expr *sqlparser.AliasedExp
}

// MakeDistinct satisfies the builder interface.
func (ms *mergeSort) MakeDistinct() error {
return ms.input.MakeDistinct()
func (ms *mergeSort) MakeDistinct() (builder, error) {
distinctSrc, err := ms.input.MakeDistinct()
if err != nil {
return nil, err
}
ms.input = distinctSrc
return ms, err
}

// PushGroupBy satisfies the builder interface.
Expand Down
11 changes: 8 additions & 3 deletions go/vt/vtgate/planbuilder/ordered_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,18 +356,23 @@ func (oa *orderedAggregate) needDistinctHandling(pb *primitiveBuilder, funcExpr
return true, innerAliased, nil
}

func (oa *orderedAggregate) MakeDistinct() error {
func (oa *orderedAggregate) MakeDistinct() (builder, error) {
for i, rc := range oa.resultColumns {
// If the column origin is oa (and not the underlying route),
// it means that it's an aggregate function supplied by oa.
// So, the distinct 'operator' cannot be pushed down into the
// route.
if rc.column.Origin() == oa {
return errors.New("unsupported: distinct cannot be combined with aggregate functions")
return nil, errors.New("unsupported: distinct cannot be combined with aggregate functions")
}
oa.eaggr.Keys = append(oa.eaggr.Keys, i)
}
return oa.input.MakeDistinct()
distinctSrc, err := oa.input.MakeDistinct()
if err != nil {
return nil, err
}
oa.input=distinctSrc
return oa, err
}

// PushGroupBy satisfies the builder interface.
Expand Down
4 changes: 3 additions & 1 deletion go/vt/vtgate/planbuilder/postprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import (
// and ensures that there are no subqueries.
func (pb *primitiveBuilder) pushGroupBy(sel *sqlparser.Select) error {
if sel.Distinct {
if err := pb.bldr.MakeDistinct(); err != nil {
newBuilder, err := pb.bldr.MakeDistinct()
if err != nil {
return err
}
pb.bldr = newBuilder
}

if err := pb.st.ResolveSymbols(sel.GroupBy); err != nil {
Expand Down
9 changes: 7 additions & 2 deletions go/vt/vtgate/planbuilder/pullout_subquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ func (ps *pulloutSubquery) PushSelect(pb *primitiveBuilder, expr *sqlparser.Alia
}

// MakeDistinct satisfies the builder interface.
func (ps *pulloutSubquery) MakeDistinct() error {
return ps.underlying.MakeDistinct()
func (ps *pulloutSubquery) MakeDistinct() (builder, error) {
distinctUnderlying, err := ps.underlying.MakeDistinct()
if err != nil {
return nil, err
}
ps.underlying = distinctUnderlying
return ps, err
}

// PushGroupBy satisfies the builder interface.
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ func (rb *route) PushAnonymous(expr sqlparser.SelectExpr) *resultColumn {
}

// MakeDistinct satisfies the builder interface.
func (rb *route) MakeDistinct() error {
func (rb *route) MakeDistinct() (builder, error) {
rb.Select.(*sqlparser.Select).Distinct = true
return nil
return rb, nil
}

// PushGroupBy satisfies the builder interface.
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/sql_calc_found_rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func (s *sqlCalcFoundRows) PushSelect(*primitiveBuilder, *sqlparser.AliasedExpr,
}

//MakeDistinct implements the builder interface
func (s *sqlCalcFoundRows) MakeDistinct() error {
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unreachable: sqlCalcFoundRows.MakeDistinct")
func (s *sqlCalcFoundRows) MakeDistinct() (builder, error) {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unreachable: sqlCalcFoundRows.MakeDistinct")
}

//PushGroupBy implements the builder interface
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/subquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ func (sq *subquery) PushSelect(_ *primitiveBuilder, expr *sqlparser.AliasedExpr,
}

// MakeDistinct satisfies the builder interface.
func (sq *subquery) MakeDistinct() error {
return errors.New("unsupported: distinct on cross-shard subquery")
func (sq *subquery) MakeDistinct() (builder, error) {
return nil, errors.New("unsupported: distinct on cross-shard subquery")
}

// PushGroupBy satisfies the builder interface.
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/vindex_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ func (vf *vindexFunc) PushSelect(_ *primitiveBuilder, expr *sqlparser.AliasedExp
}

// MakeDistinct satisfies the builder interface.
func (vf *vindexFunc) MakeDistinct() error {
return errors.New("unsupported: distinct on vindex function")
func (vf *vindexFunc) MakeDistinct() (builder, error) {
return nil, errors.New("unsupported: distinct on vindex function")
}

// PushGroupBy satisfies the builder interface.
Expand Down

0 comments on commit aa9df89

Please sign in to comment.