Functions

Functions

You can list all available functions in your current Memgraph instance with the following Cypher query:

CALL mg.functions() YIELD *;

User-defined functions

Memgraph offers the flexibility of implementing custom functions. When supported built-in functions are not enough, there is an option to define a custom one by using C, C++, Python or Rust. The mechanism of query modules enables the integration of custom functionalities.

Semantically, functions should be a small fragment of functionality that does not require long computations and large memory consumption. The only requirement for functions is to not modify the graph. Mentioned functionality offers flexibility in terms of nested calls within the Cypher.

Built-in functions

This section contains the list of supported functions.

Temporal functions

NameSignatureDescription
durationduration(value: string|Duration) -> (Duration)Returns the data type that represents a period of time.
datedate(value: string|Date|LocalDateTime) -> (Date)Returns the data type that represents a date with year, month, and day.
localTimelocalTime(value: string|LocalTime|LocalDateTime) -> (LocalTime)Returns the data type that represents time within a day without timezone.
localDateTimelocalDateTime(value: string|LocalDateTime)-> (LocalDateTime)Returns the data type that represents a date and local time.

Scalar functions

NameSignatureDescription
assertassert(expression: boolean, message: string = null) -> ()Raises an exception if the given argument is not true.
coalescecoalesce(expression: any [, expression: any]*) -> (any)Returns the first non-null value in the given list of expressions.
countercounter(name: string, initial-value: integer, increment: integer = 1) -> (integer)Generates integers that are guaranteed to be unique within a single query for a given counter name. The increment parameter can be any integer besides zero.
degreedegree(node: Node) -> (integer)Returns the number of relationships (both incoming and outgoing) of a node.
outDegreeoutDegree(node: Node) -> (integer)Returns the number of outgoing relationships of a node.
inDegreeinDegree(node: Node) -> (integer)Returns the number of incoming relationships of a node.
endNodeendNode(relationship: Relationship) -> (Node)Returns the destination node of a relationship.
headhead(list: List[any]) -> (any)Returns the first element of a list.
idid(value: Node|Relationship) -> (integer)Returns identifier for a given node or relationship. The identifier is generated during the initialization of a node or a relationship and will be persisted through the durability mechanism.
lastlast(list: List[any]) -> (any)Returns the last element of a list.
propertiesproperties(value: Node|Relationship) -> (Map[string, any])Returns the property map of a node or a relationship.
propertySizepropertySize(entity: Node|Relationship, property-name: string) -> (integer)Returns the total amount of bytes stored in RAM for the property of a given entity node or relationship. For more information, check storage of properties inside Memgraph.
sizesize(value: List[any]|string|Map[string, any]|Path) -> (integer)Returns the number of elements in the value. When given a list it returns the size of the list. When given a string it returns the number of characters. When given a path it returns the number of expansions (relationships) in that path.
startNodestartNode(relationship: Relationship) -> (Node)Returns the starting node of a relationship.
toBooleantoBoolean(value: boolean|integer|string) -> (boolean)Converts the input argument to a boolean value, regardless of case sensitivity. The values true and false are directly converted to true or false, respectively. Additionally, the strings "true" and "t" are mapped to true, while the strings "false" and "f" are mapped to false.
toFloattoFloat(value: number|string) -> (float)Converts the argument to a floating point number.
toIntegertoInteger(value: boolean|number|string) -> (integer)Converts the argument to an integer.
toStringtoString(value: any) -> (string)Converts the argument to a string.
typetype(relationship: Relationship) -> (string)Returns the type of a relationships as a character string.
timestamptimestamp() -> (integer)Returns the difference, measured in microseconds, between the current time and midnight, January 1, 1970 UTC.

Pattern functions

NameSignatureDescription
existsexists(pattern: Pattern)Checks if a pattern exists as part of the filtering clause. Symbols provided in the MATCH clause can also be used here. The function can be used only with the WHERE clause.

Lists

NameSignatureDescription
allall(variable IN list WHERE predicate)Check if all elements of a list satisfy a predicate.
NOTE: Whenever possible, use Memgraph's lambda functions when matching instead.
anyany(element IN list WHERE predicate_using_element)Check if any element in the list satisfies the predicate.
extractextract(variable IN list|expression)A list of values obtained by evaluating an expression for each element in list.
labelslabels(node: Node) -> (List[string])Returns a list of labels from a node. Each label is represented as string.
nodesnodes(path: Path) -> (List[Node])Returns a list of nodes from a path.
rangerange(start-number: integer, end-number: integer, increment: integer = 1) -> (List[integer])Constructs a list of value in given range.
reducereduce(accumulator = initial_value, variable IN list|expression)Accumulate list elements into a single result by applying an expression.
relationshipsrelationships(path: Path) -> (List[Relationship])Returns a list of relationships (edges) from a path.
singlesingle(variable IN list WHERE predicate)Check if only one element of a list satisfies a predicate.
tailtail(list: List[any]) -> (List[any])Returns all elements after the first of a given list.
uniformSampleuniformSample(list: List[any], size: integer) -> (List[any])Returns elements of a given list randomly oversampled or undersampled to desired size

Maps

NameSignatureDescription
keys`keys(map[Any, Any]Node
values`values(map[Any, Any]Node

Math functions

NameSignatureDescription
absabs(number: integer|float) -> (integer|float)Returns the absolute value of a number.
acosacos(number: integer|float) -> (float)Calculates the arccosine of a number between -1 and 1 in radians.
asinasin(number: integer|float) -> (float)Calculates the arcsine of a number between -1 and 1 in radians.
atanatan(number: integer|float) -> (float)Calculates the arctangent of a given number in radians.
atan2atan2(y: integer|float, x: integer|float) -> (float)Calculates a unique arctangent value from a set of coordinates in radians.
ceilceil(number: float) -> (integer)Returns the smallest integer greater than or equal to the given float number.
coscos(number: integer|float) -> (float)Calculates the cosine of an angle specified in radians.
ee() -> (float)Returns the base of the natural logarithm (2.71828)..
expexp(number: integer|float) -> (float)Calculates e^n where e is the base of the natural logarithm, and n is the given number.
floorfloor(number: float) -> (integer)Returns the largest integer smaller than or equal to the given float number.
loglog(number: integer|float) -> (float)Calculates the natural logarithm of a given number.
log10log10(number: integer|float) -> (float)Calculates the logarithm (base 10) of a given number.
pipi() -> (float)Returns the constant pi (3.14159).
randrand() -> (float)Returns a random floating point number between 0 (inclusive) and 1 (exclusive).
roundround(number: float) -> (integer)Returns the number, rounded to the nearest integer. Tie-breaking is done using the commercial rounding, where -1.5 produces -2 and 1.5 produces 2.
signsign(number: integer| float) -> (integer)Applies the signum function to a given number and returns the result. The signum of positive numbers is 1, of negative -1 and for 0 returns 0.
sinsin(number: integer|float) -> (float)Calculates the sine of an angle specified in radians.
sqrtsqrt(number: integer|float) -> (float)Calculates the square root of a given number.
tantan(number: integer|float) -> (float)Calculates the tangent of an angle specified in radians.

Aggregation functions

NameSignatureDescription
avgavg(row: int|float) -> (float)Returns an average value of rows with numerical values generated with the MATCH or UNWIND clause.
collectcollect(values: any) -> (List[any])Returns a single aggregated list containing returned values.
countcount(values: any) -> (integer)Counts the number of non-null values returned by the expression.
maxmax(row: integer|float) -> (integer|float)Returns the maximum value in a set of values.
minmin(row: integer|float) -> (integer|float)Returns the minimum value in a set of values.
sumsum(row: integer|float) -> (integer|float)Returns a sum value of rows with numerical values generated with the MATCH or UNWIND clause.

Graph projection functions

NameSignatureDescription
projectproject(row: path) -> map("nodes":list[Node], "edges":list[Edge])Creates a projected graph consisting of nodes and relationships from aggregated paths.

All aggregation functions can be used with the DISTINCT operator to perform calculations only on unique values. For example, count(DISTINCT n.prop) and collect(DISTINCT n.prop).

String functions

NameSignatureDescription
containscontains(string: string, substring: string) -> (boolean)Check if the first argument has an element which is equal to the second argument.
endsWithendsWith(string: string, substring: string) -> (boolean)Check if the first argument ends with the second.
leftleft(string: string, count: integer) -> (string)Returns a string containing the specified number of leftmost characters of the original string.
lTrimlTrim(string: string) -> (string)Returns the original string with leading whitespace removed.
replacereplace(string: string, search-string: string, replacement-string: string) -> (string)Returns a string in which all occurrences of a specified string in the original string have been replaced by another (specified) string.
reversereverse(string: string) -> (string)Returns a string in which the order of all characters in the original string have been reversed.
rightright(string: string, count: integer) -> (string)Returns a string containing the specified number of rightmost characters of the original string.
rTrimrTrim(string: string) -> (string)Returns the original string with trailing whitespace removed.
splitsplit(string: string, delimiter: string) -> (List[string])Returns a list of strings resulting from the splitting of the original string around matches of the given delimiter.
startsWithstartsWith(string: string, substring: string) -> (boolean)Check if the first argument starts with the second.
substringsubstring(string: string, start-index: integer, length: integer = null) -> (string)Returns a substring of the original string, beginning with a 0-based index start and length.
toLowertoLower(string: string) -> (string)Returns the original string in lowercase.
toUppertoUpper(string: string) -> (string)Returns the original string in uppercase.
trimtrim(string: string) -> (string)Returns the original string with leading and trailing whitespace removed.

Conversion functions

NameSignatureDescription
convert.str2objectconvert.str2object(string: string) -> (object: any)Converts the input string to an object it presents using Python's json.dumps function.