varName as q(func: ...) { ... }
varName as var(func: ...) { ... }
varName as predicate { ... }
varName as predicate @filter(...) { ... }
uid
Nodes (UIDs) matched at one place in a query can be stored in a variable and
used elsewhere. Query variables can be used in other query blocks or in a child
node of the defining block.
Query variables do not affect the semantics of the query at the point of
definition. Query variables are evaluated to all nodes matched by the defining
block.
In general, query blocks are executed in parallel, but variables impose an
evaluation order on some blocks. Cycles induced by variable dependence are not
permitted.
If a variable is defined, it must be used elsewhere in the query.
A query variable is used by extracting the UIDs in it with uid(var-name)
.
The syntax func: uid(A,B)
or @filter(uid(A,B))
means the union of UIDs for
variables A
and B
.
Query Example: the movies of Angelia Jolie and Brad Pitt where both have acted
on movies in the same genre. Note that B
and D
match all genres for all
movies, not genres per movie.
varName as scalarPredicate
varName as count(predicate)
varName as avg(...)
varName as math(...)
int
, float
, String
, dateTime
, default
, geo
, bool
Value variables store scalar values. Value variables are a map from the UIDs of
the enclosing block to the corresponding values.
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.
It is an error to define a value variable but not use it elsewhere in the query.
Value variables are used by extracting the values with val(var-name)
, or by
extracting the UIDs with uid(var-name)
.
Facet values can be stored in value variables.
Query Example: the number of movie roles played by the actors of the 80βs
classic βThe Princess Brideβ. Query variable pbActors
matches the UIDs of all
actors from the movie. Value variable roles
is thus a map from actor UID to
number of roles. Value variable roles
can be used in the totalRoles
query
block because that query block also matches the pbActors
UIDs, so the actor to
number of roles map is available.
roles
for matching UIDs in the totalRoles
query block.
myscore
is defined as mapping node with UID 0x01
to value 1. At B, the value for each friend is still 1: there is only one path
to each friend. Traversing the friend edge twice reaches the friends of friends.
The variable myscore
gets propagated such that each friend of friend receives
the sum of its parents values: if a friend of a friend is reachable from only
one friend, the value is still 1, if theyβre reachable from two friends, the
value is two and so on. That is, the value of myscore
for each friend of
friends inside the block marked C will be the number of paths to them.
The value that a node receives for a propagated variable is the sum of the
values of all its parent nodes.
This propagation is useful, for example, in normalizing a sum across users,
finding the number of paths between nodes and accumulating a sum through a
graph.
Query Example: for each Harry Potter movie, the number of roles played by actor
Warwick Davis.
math( <exp> )
and must be stored to a
value variable.
The supported operators are as follows:
Operators | Types accepted | What it does |
---|---|---|
+ - * / % | int , float | performs the corresponding operation |
min max | All types except geo , bool (binary functions) | selects the min/max value among the two |
< > <= >= == != | All types except geo , bool | Returns true or false based on the values |
floor ceil ln exp sqrt | int , float (unary function) | performs the corresponding operation |
since | dateTime | Returns the number of seconds in float from the time specified |
pow(a, b) | int , float | Returns a to the power b |
logbase(a,b) | int , float | Returns log(a) to the base b |
cond(a, b, c) | first operand must be a Boolean | selects b if a is true else c |
ln
, logbase
, sqrt
, pow
) which results in an illegal
operation, Dgraph will return an error.