The SET
aggregation function takes a single-assign attribute and constructs a set of all of the (non-NULL) values from that attribute.
Single-assign attributes have non-set data types (such as mdex:long
). So the SET
function takes a non-set data type attribute and produces a set data type result (for example, mdex:long-set
).
SET
function's behavior is as follows:
The resulting set will have a set data type (such as mdex:double-set
). All subsequent operations on it must follow the rules for sets.
The SET
function is available in one-argument and two-argument versions, as described below. This function can be used only in SELECT
clauses.
SET one-argument version
SET
function is:
SET(<single-assign_attribute>)
where the data type of the attribute must be a non-set data type (such as mdex:double
for a single-assign double attribute).RETURN results AS SELECT SET(Price) AS prices FROM WineState GROUP BY WineType ORDER BY WineType
WineType prices --------------------------------------------------------------- | Blanc de Noirs | { 16.99 } | | Bordeaux | { 21.99 } | | Brut | { 22.99, 23.99 } | | Chardonnay | { 17.95, 34.95 } | | Merlot | { 25.99 } | | Pinot Noir | { 14.99 } | | Red | { 12.99, 13.95, 17.5, 18.99, 21.99, 9.99 } | | White | { 20.99, 32.99, 43.99 } | | Zinfandel | { } | ---------------------------------------------------------------
In the results, note that Zinfandel has an empty set because Zinfandel does not have a Price attribute assignment.
SET two-argument version
For situations where the result of the SET
aggregator can be extremely large (causing the Dgraph to consume excessive memory), a two-argument form of the aggregator is provided to limit the set size.
SET
function is:
SET(<single-assign_attribute>, <max-size>)
where:
mdex:string
for a single-assign string attribute).SET
always returns the empty set.SET(Price, 3) is valid. SET(Price, x) is not valid, even if x is an integer.
RETURN results AS SELECT SET(Price, 2) AS prices FROM WineState GROUP BY WineType ORDER BY WineType
WineType prices ------------------------------------- | Blanc de Noirs | { 16.99 } | | Bordeaux | { 21.99 } | | Brut | { 22.99, 23.99 } | | Chardonnay | { 17.95, 34.95 } | | Merlot | { 25.99 } | | Pinot Noir | { 14.99 } | | Red | { 12.99, 9.99 } | | White | { 20.99, 32.99 } | | Zinfandel | { } | -------------------------------------
In the results, note that Red set now has two elements, while it had six elements with the one-argument SET
version. Likewise with the White set, which previously had three elements.
Data type errors
When working with the
function, keep in mind that its resulting sets are of the set data types, such as a SET
mdex:double-set
data type.
RETURN results AS SELECT SET(Price) AS prices FROM WineState GROUP BY WineType HAVING prices > 10
In statement "results": In HAVING clause: Cannot compare mdex:double-set and mdex:long
The reason for the error is that the "prices" set is of type mdex:double-set
and it is being compared to the number 10 (which is an mdex:double
type).
RETURN results AS SELECT SET(Price) AS prices FROM WineState GROUP BY WineType HAVING SOME x IN prices SATISFIES (x > 10)
In this example, the SATISFIES
expression allows you to make a numerical comparison.