Skip to content

Commit

Permalink
Adding sort statements and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kellrott committed Oct 24, 2023
1 parent bb105ee commit 59a2bfa
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
28 changes: 27 additions & 1 deletion conformance/tests/ot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def test_sort_name(man):
G = man.setGraph("swapi")

q = G.query().V().hasLabel("Character").sort( "name" )

last = ""
for row in q:
#print(row)
Expand All @@ -17,3 +16,30 @@ def test_sort_name(man):
last = row["data"]["name"]
return errors



def test_sort_units(man):
errors = []
G = man.setGraph("swapi")

q = G.query().V().hasLabel("Vehicle").sort( "max_atmosphering_speed" )
last = 0
for row in q:
#print(row)
value = row["data"]["max_atmosphering_speed"]
if value < last:
errors.append("incorrect sort: %s < %s" % (value, last))
last = value


q = G.query().V().hasLabel("Vehicle").sort( "max_atmosphering_speed", decending=True )
last = 1000000000
for row in q:
print(row)
value = row["data"]["max_atmosphering_speed"]
if value > last:
errors.append("incorrect sort: %s > %s" % (value, last))
last = value

return errors

6 changes: 6 additions & 0 deletions engine/core/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ func StatementProcessor(gs *gripql.GraphStatement, db gdbi.GraphInterface, ps *p
return &Selector{stmt.Select.Marks}, nil
}

case *gripql.GraphStatement_Sort:
if len(stmt.Sort.Fields) == 0 {
return nil, fmt.Errorf("`sort` requires at least on sort fields")
}
return &Sort{stmt.Sort.Fields}, nil

case *gripql.GraphStatement_Render:
if ps.LastType != gdbi.VertexData && ps.LastType != gdbi.EdgeData {
return nil, fmt.Errorf(`"render" statement is only valid for edge or vertex types not: %s`, ps.LastType.String())
Expand Down
30 changes: 30 additions & 0 deletions engine/core/processors_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package core

import (
"context"

"github.com/bmeg/grip/gdbi"
"github.com/bmeg/grip/gripql"
)

// Sort rows
type Sort struct {
sortFields []*gripql.SortField
}

// Process runs LookupEdges
func (s *Sort) Process(ctx context.Context, man gdbi.Manager, in gdbi.InPipe, out gdbi.OutPipe) context.Context {

go func() {
defer close(out)
for t := range in {
if t.IsSignal() {
out <- t
continue
}
out <- t
}
}()

return ctx
}

0 comments on commit 59a2bfa

Please sign in to comment.