Variables
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
Query variables
Syntax Examples:
varName as q(func: ...) { ... }
varName as var(func: ...) { ... }
varName as predicate { ... }
varName as predicate @filter(...) { ... }
Types : 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.
Value variables
Syntax Examples:
varName as scalarPredicate
varName as count(predicate)
varName as avg(...)
varName as math(...)
Types : 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.
Value variables can be used in place of UID variables by extracting the UID list from the map.
Query Example: the same query as the previous example, but using value variable
roles
for matching UIDs in the totalRoles
query block.
At line A, a value variable 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.
Query Example: each actor who has been in a Peter Jackson movie and the fraction of Peter Jackson movies they have appeared in.
More examples can be found in two Dgraph blog posts about using variable propagation for recommendation engines (post 1, post 2).
Math on value variables
Value variables can be combined using mathematical functions. For example, this could be used to associate a score which is then used to order or perform other operations, such as might be used in building news feeds, simple recommendation systems, and so on.
Math statements must be enclosed within 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 |
If an integer overflow occurs, or an operand is passed to a math operation
(such as ln
, logbase
, sqrt
, pow
) which results in an illegal
operation, Dgraph will return an error.
Query Example: Form a score for each of Steven Spielberg’s movies as the sum of number of actors, number of genres and number of countries. List the top five such movies in order of decreasing score.
Value variables and aggregations of them can be used in filters.
Query Example: Calculate a score for each Steven Spielberg movie with a condition on release date to penalize movies that are more than 20 years old, filtering on the resulting score.
Values calculated with math operations are stored to value variables and so can be aggregated.
Query Example: Compute a score for each Steven Spielberg movie and then aggregate the score.
Was this page helpful?