Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion compiler/optimizer/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ func replaceSortAndHeadOrTailWithTop(seq dag.Seq) dag.Seq {
case *dag.TailOp:
limit = op.Count
for i, e := range exprs {
exprs[i].Order = !e.Order
exprs[i].Order = e.Order.Reverse()
}
reverse = !reverse
default:
Expand Down
2 changes: 1 addition & 1 deletion compiler/semantic/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func (t *translator) sortExpr(sch relScope, s ast.SortExpr, reverse bool, inType
}
}
if reverse {
o = !o
o = o.Reverse()
}
n := order.NullsLast
if s.Nulls != nil {
Expand Down
7 changes: 7 additions & 0 deletions order/direction.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func ParseDirection(s string) (Direction, error) {
}
}

func (d Direction) Which() Which {
if d == Down {
return Desc
}
return Asc
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned here, implicit conversion from Unknown to Asc feels like a potential rake, so maybe add a comment or a default parameter?

func (d Direction) HasOrder(which Which) bool {
switch d {
case Up:
Expand Down
71 changes: 19 additions & 52 deletions order/which.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
package order

import (
"encoding/json"
"fmt"
"strings"

"github.com/brimdata/super"
"github.com/brimdata/super/sup"
)

type Which bool
type Which string

const (
Asc Which = false
Desc Which = true
Asc Which = "asc"
Desc Which = "desc"
)

func Parse(s string) (Which, error) {
switch strings.ToLower(s) {
case "asc":
return Asc, nil
case "desc":
return Desc, nil
default:
return false, fmt.Errorf("unknown order: %s", s)
}
}

func (w Which) String() string {
if w == Desc {
return "desc"
}
return "asc"
return string(w)
}

func (w Which) Direction() Direction {
Expand All @@ -41,43 +23,28 @@ func (w Which) Direction() Direction {
return Up
}

// NullsMax returns the Nulls value corresponding to w and nullsMax.
func (w Which) NullsMax(nullsMax bool) Nulls {
if w == Asc && nullsMax || w == Desc && !nullsMax {
return NullsLast
}
return NullsFirst
}

func (w Which) MarshalJSON() ([]byte, error) {
return json.Marshal(w.String())
}

func (w *Which) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch s {
func Parse(s string) (Which, error) {
switch strings.ToLower(s) {
case "asc":
*w = Asc
return Asc, nil
case "desc":
*w = Desc
return Desc, nil
default:
return fmt.Errorf("unknown order: %s", s)
return "", fmt.Errorf("unknown order: %s", s)
}
return nil
}

func (w Which) MarshalBSUP(m *sup.MarshalBSUPContext) (super.Type, error) {
return m.MarshalValue(w.String())
func (w Which) Reverse() Which {
if w == Asc {
return Desc
}
return Asc
}

func (w *Which) UnmarshalBSUP(u *sup.UnmarshalBSUPContext, val super.Value) error {
which, err := Parse(string(val.Bytes()))
if err != nil {
return err
// NullsMax returns the Nulls value corresponding to w and nullsMax.
func (w Which) NullsMax(nullsMax bool) Nulls {
if w == Asc && nullsMax || w == Desc && !nullsMax {
return NullsLast
}
*w = which
return nil
return NullsFirst
}
2 changes: 1 addition & 1 deletion runtime/sam/op/aggregate/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewAggregator(ctx context.Context, sctx *super.Context, keyRefs, keyExprs,
}
var keyCompare, valueCompare expr.CompareFn
nkeys := len(keyExprs)
o := order.Which(inputDir < 0)
o := inputDir.Which()
if nkeys > 0 && inputDir != 0 {
keySortExpr := expr.NewSortExpr(keyRefs[0], o, o.NullsMax(true))
keyCompare = expr.NewComparator(keySortExpr).WithMissingAsNull().Compare
Expand Down
Loading