From 59a2bfa2dfc0d63dad9e604b87eeb809d2dadf0f Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Tue, 24 Oct 2023 16:03:29 -0700 Subject: [PATCH] Adding sort statements and tests --- conformance/tests/ot_sort.py | 28 +++++++++++++++++++++++++++- engine/core/compile.go | 6 ++++++ engine/core/processors_sort.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 engine/core/processors_sort.go diff --git a/conformance/tests/ot_sort.py b/conformance/tests/ot_sort.py index 809ee426..886d599e 100644 --- a/conformance/tests/ot_sort.py +++ b/conformance/tests/ot_sort.py @@ -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) @@ -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 + diff --git a/engine/core/compile.go b/engine/core/compile.go index d83454fc..5a5941ea 100644 --- a/engine/core/compile.go +++ b/engine/core/compile.go @@ -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()) diff --git a/engine/core/processors_sort.go b/engine/core/processors_sort.go new file mode 100644 index 00000000..40974667 --- /dev/null +++ b/engine/core/processors_sort.go @@ -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 +}