Skip to main content

Functions

User-defined Memgraph Magic 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.

Supported 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) -> (Date)Returns the data type that represents a date with year, month, and day.
localTimelocalTime(value: string|LocalTime) -> (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
startNodestartNode(relationship: Relationship) -> (Node)Returns the starting node of a relationship.
endNodeendNode(relationship: Relationship) -> (Node)Returns the destination node of a relationship.
degreedegree(node: Node) -> (integer)Returns the number of relationships (both incoming and outgoing) of a node.
headhead(list: List[any]) -> (any)Returns the first element of a list.
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.
sizesize(value: any) -> (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.
toBooleantoBoolean(value: any) -> (boolean)Converts the argument to a boolean.
toFloattoFloat(value: any) -> (float)Converts the argument to a floating point number.
toIntegertoInteger(value: any) -> (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.
assertassert(value: any) -> ()Raises an exception if the given argument is not true.
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.
timestamptimestamp() -> (integer)Returns the difference, measured in microseconds, between the current time and midnight, January 1, 1970 UTC.
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.

Lists

NameSignatureDescription
keyskeys(value: Node|Relationship) -> (List[string])Returns a list keys of properties from a relationship or a node. Each key is represented as string.
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.
relationshipsrelationships(path: Path) -> (List[Relationship])Returns a list of relationships (edges) from a path.
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
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.
extractextract(variable IN list \| expression)A list of values obtained by evaluating an expression for each element in list.
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.
singlesingle(variable IN list WHERE predicate)Check if only one element of a list satisfies a predicate.

Math functions

NameSignatureDescription
absabs(number: integer | float) -> (integer|float)Returns the absolute value of a number.
ceilceil(number: float) -> (integer)Returns the smallest integer greater than or equal to the iven float number.
floorfloor(number: float) -> (integer)Returns the largest integer smaller than or equal to the given float number.
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.
pipi() -> (float)Returns the constant pi (3.14159).
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.
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.
sqrtsqrt(number: integer | float) -> (float)Calculates the square root of a given 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.
coscos(number: integer | float) -> (float)Calculates the cosine of an angle specified in radians.
sinsin(number: integer | float) -> (float)Calculates the sine of an angle specified in radians.
tantan(number: integer | float) -> (float)Calculates the tangent of an angle specified in radians.
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.
randrand() -> (float)Returns a random floating point number between 0 (inclusive) and 1 (exclusive).

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.

String functions

NameSignatureDescription
startsWithstartsWith(string: string, substring: string) -> (boolean)Check if the first argument starts with the second.
endsWithendsWith(string: string, substring: string) -> (boolean)Check if the first argument ends with the second.
containscontains(string: string, substring: string) -> (boolean)Check if the first argument has an element which is equal to the second argument.
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.
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.