Skip to content

Commit

Permalink
Add command line args to server func
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpeterkort committed Jan 5, 2025
1 parent 928a38d commit 66465ea
Show file tree
Hide file tree
Showing 7 changed files with 462 additions and 413 deletions.
42 changes: 30 additions & 12 deletions conformance/tests/ot_serverfunc.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@

attr_len_func = """
function attr_len(x) {
x["skin_colors_len"] = x["skin_colors"].length;
x["hair_colors_len"] = x["hair_colors"].length;
x["eye_colors_len"] = x["eye_colors"].length;
return [x]
}
"""

def test_flatmap(man):
attr_len_func = """
function attr_len(x, args) {
x["skin_colors_len"] = x["skin_colors"].length;
x["hair_colors_len"] = x["hair_colors"].length;
x["eye_colors_len"] = x["eye_colors"].length;
return [x]
}
"""

errors = []

G = man.setGraph("swapi")

count = 0
q = G.query().V().hasLabel("Species").flatMap("attr_len", attr_len_func)
q = G.query().V().hasLabel("Species").flatMap("attr_len", attr_len_func, {})
for row in q:
count += 1
if row["data"]["skin_colors_len"] != len(row["data"]["skin_colors"]):
errors.append("count function not correct")
if count != 5:
errors.append("Incorrect row count returned: %d != 5" % (count))
return errors
return errors


def test_command_line_args(man):
func = """
function AddObj(x, args) {
for (var k in args){
x[k]= args[k]
}
return [x]
}
"""

errors = []
G = man.setGraph("swapi")
q = G.query().V().hasLabel("Species").flatMap("AddObj", func, {"OtherSpecies":["Sullustan", "RathStar", "Bith"]})
for row in q:
if "OtherSpecies" not in row['data'] or not all(species in row['data']["OtherSpecies"] for species in ["Sullustan", "RathStar", "Bith"]):
errors.append("Appended items '{'OtherSpecies':['Sullustan', 'RathStar', 'Bith']}' Not Present in row")
return errors
7 changes: 6 additions & 1 deletion engine/core/processors_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type FlatMap struct {
source string
func_name string
args map[string]any
}

// Process LookupVerts
Expand Down Expand Up @@ -43,7 +44,11 @@ func (fm *FlatMap) Process(ctx context.Context, man gdbi.Manager, in gdbi.InPipe
src := t.GetCurrent().Get()
data := src.ToDict()
dataObj := vm.ToValue(data)
fout, err := jfunc(goja.Null(), dataObj)

argsObj := vm.ToValue(fm.args)

fout, err := jfunc(goja.Null(), dataObj, argsObj)

if err == nil {
o := fout.Export()
if oList, ok := o.([]any); ok {
Expand Down
2 changes: 1 addition & 1 deletion engine/core/statement_compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (sc *DefaultStmtCompiler) Aggregate(stmt *gripql.GraphStatement_Aggregate,
}

func (sc *DefaultStmtCompiler) FlatMap(stmt *gripql.GraphStatement_FlatMap, ps *gdbi.State) (gdbi.Processor, error) {
return &FlatMap{source: stmt.FlatMap.Source, func_name: stmt.FlatMap.Function}, nil
return &FlatMap{source: stmt.FlatMap.Source, func_name: stmt.FlatMap.Function, args: stmt.FlatMap.Args.AsMap()}, nil
}

func (sc *DefaultStmtCompiler) Custom(gs *gripql.GraphStatement, ps *gdbi.State) (gdbi.Processor, error) {
Expand Down
806 changes: 409 additions & 397 deletions gripql/gripql.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions gripql/gripql.proto
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ message ExtendQuery {
message Code {
string function = 1;
string source = 2;
google.protobuf.Struct args = 3;
}

enum JobState {
Expand Down
4 changes: 2 additions & 2 deletions gripql/python/gripql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ def aggregate(self, aggregations):
aggregations = _wrap_dict_value(aggregations)
return self.__append({"aggregate": {"aggregations": aggregations}})

def flatMap(self, fname, source):
return self.__append({"flat_map": {"function":fname, "source":source}})
def flatMap(self, fname, source, args):
return self.__append({"flat_map": {"function":fname, "source":source, "args": args}})

def to_json(self):
"""
Expand Down
13 changes: 13 additions & 0 deletions gripql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ func (q *Query) Unwind(path string) *Query {
return q.with(&GraphStatement{Statement: &GraphStatement_Unwind{Unwind: path}})
}

func (q *Query) FlatMap(code *Code) *Query {
return q.with(&GraphStatement{Statement: &GraphStatement_FlatMap{FlatMap: code}})
}

func (q *Query) String() string {
parts := []string{}
add := func(name string, x ...string) {
Expand Down Expand Up @@ -297,6 +301,15 @@ func (q *Query) String() string {
case *GraphStatement_Aggregate:
add("Aggregate")

case *GraphStatement_Unwind:
add("Unwind", stmt.Unwind)

case *GraphStatement_Pivot:
add("Pivot", fmt.Sprintf("%s", stmt.Pivot.Id), fmt.Sprintf("%s", stmt.Pivot.Field), fmt.Sprintf("%s", stmt.Pivot.Value))

case *GraphStatement_FlatMap:
add("FlatMap", fmt.Sprintf("%s", stmt.FlatMap.Function), fmt.Sprintf("%s", stmt.FlatMap.Source), fmt.Sprintf("%s", stmt.FlatMap.Args.String()))

case *GraphStatement_Render:
jtxt, err := protojson.Marshal(stmt.Render)
if err != nil {
Expand Down

0 comments on commit 66465ea

Please sign in to comment.