According to Value Variable Doc:
… It therefore only makes sense to use the values from a value variable in a context that matches the same UIDs - if used in a block matching different UIDs the value variable is undefined.
However, in one case where the enclosing block matches a subset of the UIDs, I am able to use val
to extract value from value vars. In another case, it just gives “Division by zero” error.
The following are mutation, schema and queries I used to trigger the inconsistency:
Mutation:
{
set {
_:m1 <name> "Alice Movie" .
_:m2 <name> "Movie II" .
_:m3 <name> "Movie 3" .
_:m1 <genre> _:g1 .
_:m1 <genre> _:g2 .
_:m2 <genre> _:g2 .
_:m3 <genre> _:g1 .
_:m1 <style> _:s1 .
_:m2 <style> _:s1 .
_:g1 <name> "Genre1" .
_:g2 <name> "Genre2" .
_:s1 <name> "Style1" .
}
}
Schema:
genre: uid @reverse .
style: uid @reverse .
name: string @index(hash) .
Queries:
query{
var(func: eq(name, "Alice Movie")) {
n_genres as count(genre)
n_styles as count(style)
}
dist(func: eq(name, "Alice Movie")) @normalize {
m1_name: name
m1_g as genre {
~genre {
n_common_genres as count(genre @filter(uid(m1_g)))
}
}
m1_s as style {
~style {
n_common_styles as count(style @filter(uid(m1_s)))
}
}
n_genres: val(n_genres) # 2
n_styles: val(n_styles) # 1
# subset of UIDs for the following two
n_common_genres: val(n_common_genres) # 2
n_common_styles: val(n_common_styles) # 1
# all four variables were extracted by val()
# but the folowing gives "Division by zero" error
# score as math(
# (n_common_genres + n_common_styles) /
# (n_genres + n_styles)
# )
# score: val(score)
}
m2(func: eq(name, "Movie II")) {
name
val(n_common_genres) # 1
val(n_common_styles) # 1
# I am trying to get 'val(score)' here
}
m3(func: eq(name, "Movie 3")) {
name
val(n_common_genres) # 1
val(n_common_styles) # no output
# I am trying to get 'val(score)' here
}
}
Dgraph version: v1.0.11
It would be great if dgraph can handle those cases consistently, or at least gives errors more relevant than ‘Division by Zero’.
A related question regarding the above queries: is there a way in GraphQL+- to calculate Jaccard distance using multiple UID predicates? That is what I was trying to do with the commented lines, a similar formula to the Jaccard distance.
Even nicer, can dgraph properly allow value_var as count(...)
to be used in block matching a subset of original UIDs?