Skip to content

Commit

Permalink
Working on more issues related to new driver
Browse files Browse the repository at this point in the history
  • Loading branch information
kellrott committed Oct 5, 2019
1 parent c3b210e commit f5096cc
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 41 deletions.
2 changes: 1 addition & 1 deletion conformance/tests/ot_distinct.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ def test_distinct(O):
for i in O.query().V().hasLabel("Person").as_("person").out().distinct("$person.name"):
count += 1
if count != 1:
errors.append("Distinct %s != %s" % (count, 1))
errors.append("Distinct O.query().V().hasLabel(\"Person\").as_(\"person\").out().distinct(\"$person.name\") %s != %s" % (count, 1))

return errors
2 changes: 1 addition & 1 deletion conformance/tests/ot_has.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_hasLabel(O):
"Fail: O.query().V().hasLabel(\"robot\") %s != %s" %
(count, 2))

for i in O.query().V().hasLabel("robot"):
for i in O.query().V().hasLabel("person"):
if i['gid'] not in ["vertex1", "vertex2", "vertex5", "vertex6"]:
errors.append("Wrong vertex returned %s" % (i))

Expand Down
108 changes: 72 additions & 36 deletions engine/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package inspect
import (
"fmt"
"github.com/bmeg/grip/gripql"
"github.com/bmeg/grip/protoutil"
log "github.com/sirupsen/logrus"
"strings"
)


func arrayEq(a, b []string) bool {
if len(a) != len(b) {
return false
Expand Down Expand Up @@ -37,16 +38,18 @@ func PipelineSteps(stmts []*gripql.GraphStatement) []string {
switch gs.GetStatement().(type) {
//These commands all change the postion of the traveler. When that happens,
//we go to the next 'step' of the traversal
case *gripql.GraphStatement_V, *gripql.GraphStatement_Out, *gripql.GraphStatement_In,
*gripql.GraphStatement_OutE, *gripql.GraphStatement_InE, *gripql.GraphStatement_Both,
*gripql.GraphStatement_BothE, *gripql.GraphStatement_Select:
case *gripql.GraphStatement_V, *gripql.GraphStatement_E, *gripql.GraphStatement_Out,
*gripql.GraphStatement_In, *gripql.GraphStatement_OutE, *gripql.GraphStatement_InE,
*gripql.GraphStatement_Both, *gripql.GraphStatement_BothE, *gripql.GraphStatement_Select:
curState += 1
case *gripql.GraphStatement_Limit, *gripql.GraphStatement_As, *gripql.GraphStatement_Has,
*gripql.GraphStatement_HasId, *gripql.GraphStatement_HasKey, *gripql.GraphStatement_HasLabel,
*gripql.GraphStatement_Count, *gripql.GraphStatement_Skip:
*gripql.GraphStatement_HasId, *gripql.GraphStatement_HasKey, *gripql.GraphStatement_HasLabel,
*gripql.GraphStatement_Count, *gripql.GraphStatement_Skip, *gripql.GraphStatement_Distinct,
*gripql.GraphStatement_Range, *gripql.GraphStatement_Aggregate, *gripql.GraphStatement_Render,
*gripql.GraphStatement_Fields:
case *gripql.GraphStatement_LookupVertsIndex:
default:
log.Printf("Unknown Graph Statement: %s", gs)
log.Printf("Unknown Graph Statement: %T", gs.GetStatement())
}
out = append(out, fmt.Sprintf("%d", curState))
}
Expand All @@ -56,65 +59,98 @@ func PipelineSteps(stmts []*gripql.GraphStatement) []string {
//PipelineSteps identify the variable names each step can be aliasesed using
//the as_ operation
func PipelineAsSteps(stmts []*gripql.GraphStatement) map[string]string {
out := map[string]string{}
steps := PipelineSteps(stmts)
out := map[string]string{}
steps := PipelineSteps(stmts)

for i, gs := range stmts {
switch stmt := gs.GetStatement().(type) {
case *gripql.GraphStatement_As:
out[ stmt.As ] = steps[i]
}
}
return out
for i, gs := range stmts {
switch stmt := gs.GetStatement().(type) {
case *gripql.GraphStatement_As:
out[stmt.As] = steps[i]
}
}
return out
}

//PipelineStepOutputs identify the required outputs for each step in the traversal
func PipelineStepOutputs(stmts []*gripql.GraphStatement) map[string][]string {

steps := PipelineSteps(stmts)
asMap := PipelineAsSteps(stmts)
asMap := PipelineAsSteps(stmts)
onLast := true
out := map[string][]string{}
for i := len(stmts) - 1; i >= 0; i-- {
gs := stmts[i]
gs := stmts[i]
if _, ok := gs.GetStatement().(*gripql.GraphStatement_Count); ok {
onLast = false
}
if onLast {
switch gs.GetStatement().(type) {
case *gripql.GraphStatement_Select:
sel := gs.GetSelect().Marks
for _, s := range sel {
if a, ok := asMap[s]; ok {
out[a] = []string{}
}
}
for _, s := range sel {
if a, ok := asMap[s]; ok {
out[a] = []string{"*"}
}
}
onLast = false
case *gripql.GraphStatement_Distinct:
fmt.Printf("Distinct\n")
//if there is a distinct step, we need to load data, but only for requested fields
fields := protoutil.AsStringList(gs.GetDistinct())
for _, f := range fields {
n := getNamespace(f)
if a, ok := asMap[n]; ok {
out[a] = []string{"*"}
}
}
case *gripql.GraphStatement_V, *gripql.GraphStatement_Out, *gripql.GraphStatement_In,
*gripql.GraphStatement_OutE, *gripql.GraphStatement_InE, *gripql.GraphStatement_Both,
*gripql.GraphStatement_BothE:
out[steps[i]] = []string{}
out[steps[i]] = []string{"*"}
onLast = false
case *gripql.GraphStatement_LookupVertsIndex:
out[steps[i]] = []string{}
out[steps[i]] = []string{"*"}
onLast = false
}
} else {
switch gs.GetStatement().(type) {
case *gripql.GraphStatement_HasLabel:
if x, ok := out[steps[i]]; ok {
out[steps[i]] = append(x, "_label")
} else {
out[steps[i]] = []string{"_label"}
}
case *gripql.GraphStatement_Has:
out[steps[i]] = []string{}
}
}
switch gs.GetStatement().(type) {
case *gripql.GraphStatement_HasLabel:
if x, ok := out[steps[i]]; ok {
out[steps[i]] = append(x, "_label")
} else {
out[steps[i]] = []string{"_label"}
}
case *gripql.GraphStatement_Has:
out[steps[i]] = []string{"*"}
case *gripql.GraphStatement_Distinct:
fmt.Printf("Distinct\n")
//if there is a distinct step, we need to load data, but only for requested fields
fields := protoutil.AsStringList(gs.GetDistinct())
for _, f := range fields {
n := getNamespace(f)
if a, ok := asMap[n]; ok {
out[a] = []string{"*"}
}
}
}
}
}
return out
}

//FIXME: tried to import this from jsonpath but ended up with an import cycle
func getNamespace(path string) string {
namespace := ""
parts := strings.Split(path, ".")
if strings.HasPrefix(parts[0], "$") {
namespace = strings.TrimPrefix(parts[0], "$")
}
if namespace == "" {
namespace = "__current__"
}
return namespace
}

func uniqueAppend(a []string, n string) []string {
if !contains(a, n) {
return append(a, n)
Expand Down
12 changes: 9 additions & 3 deletions engine/inspect/test/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestOutputMasking(t *testing.T) {
if len(out) != 1 {
t.Errorf("Wrong number of step outputs %d", len(out))
}
if !arrayEq(out["3"], []string{}) {
if !arrayEq(out["3"], []string{"*"}) {
t.Errorf("Incorrect output")
}

Expand All @@ -58,10 +58,10 @@ func TestOutputMasking(t *testing.T) {
if len(out) != 2 {
t.Errorf("Wrong number of step outputs %d", len(out))
}
if !arrayEq(out["3"], []string{}) {
if !arrayEq(out["3"], []string{"*"}) {
t.Errorf("Incorrect output")
}
if !arrayEq(out["4"], []string{}) {
if !arrayEq(out["4"], []string{"*"}) {
t.Errorf("Incorrect output")
}

Expand Down Expand Up @@ -92,6 +92,12 @@ func TestOutputMasking(t *testing.T) {
q = q.V().HasLabel("robot", "person")
out = inspect.PipelineStepOutputs(q.Statements)
fmt.Printf("vars: %s\n", out)

q = gripql.NewQuery()
q = q.V().HasLabel("Person").As("person").Out().Distinct("$person.name")
out = inspect.PipelineStepOutputs(q.Statements)
fmt.Printf("vars: %s -> %s\n", inspect.PipelineSteps(q.Statements), out)

}


Expand Down

0 comments on commit f5096cc

Please sign in to comment.