Utility Functions

util.compress()

Compress data using gzip or deflate.

Syntax: util.compress(data, [algorithm])

Parameters:

Parameter Type Description

data

String

String data to compress

algorithm

String

Optional - "gzip" (default) or "deflate"

Returns: String - Base64-encoded compressed data

The input is limited to 10MB. A larger value is rejected with Input size exceeds maximum allowed, so that a single query cannot exhaust server memory.

APOC Compatible: apoc.util.compress


util.decompress()

Decompress base64-encoded compressed data.

Syntax: util.decompress(data, [algorithm])

Returns: String - Decompressed string

The decompressed result is limited to 100MB. Anything larger is rejected with Decompressed output size exceeds maximum allowed, which protects the server against a "zip bomb": a small payload that expands to an enormous one.

APOC Compatible: apoc.util.decompress

WITH util.compress("Hello World!") AS compressed
RETURN util.decompress(compressed) AS original
// Returns: "Hello World!"

util.md5()

Compute MD5 hash of a value.

Syntax: util.md5(value)

Returns: String - MD5 hash (32 hex characters)

APOC Compatible: apoc.util.md5

RETURN util.md5("hello") AS hash
// Returns: "5d41402abc4b2a76b9719d911017c592"

util.sha1()

Compute SHA-1 hash of a value.

Syntax: util.sha1(value)

Returns: String - SHA-1 hash (40 hex characters)

APOC Compatible: apoc.util.sha1


util.sha256()

Compute SHA-256 hash of a value.

Syntax: util.sha256(value)

Returns: String - SHA-256 hash (64 hex characters)

APOC Compatible: apoc.util.sha256

RETURN util.sha256("hello") AS hash
// Returns: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

util.sha512()

Compute SHA-512 hash of a value.

Syntax: util.sha512(value)

Returns: String - SHA-512 hash (128 hex characters)

APOC Compatible: apoc.util.sha512


util.sleep()

Pause execution for a specified time.

Syntax: util.sleep(milliseconds)

Returns: Boolean - true after sleep

APOC Compatible: apoc.util.sleep


util.validate()

Abort the query with an error when a condition holds, as APOC’s apoc.util.validate does: the predicate states the condition that must NOT be true. When params is a non-empty list, its values fill the %s placeholders of the message (Java String.format); without it the message is used as written.

Syntax: util.validate(predicate, message [, params])

Returns: Boolean - true when the predicate is false (or null)

APOC Compatible: apoc.util.validate (also callable with CALL)

CALL apoc.util.validate(false, 'bad %s', ['x']) RETURN 1 AS ok
// Returns: one row, ok = 1

CALL apoc.util.validate(true, 'bad %s', ['x']) RETURN 1 AS ok
// Throws an error with message "bad x"

// The usual guard: abort the write when the account cannot pay
MATCH (a:Account {id: $id})
CALL apoc.util.validate(a.balance < $amount, 'insufficient balance on %s', [$id])
SET a.balance = a.balance - $amount

Up to 26.10.2 util.validate took two arguments and threw when the predicate was false, the opposite of APOC. It now follows APOC: it throws when the predicate is true. A query written for the old behavior must negate its predicate (util.validate(NOT (x > 0), …​)).


util.validatePredicate()

The function form of util.validate(), usable in a WHERE or a RETURN: throws when the predicate is true, returns true otherwise.

Syntax: util.validatePredicate(predicate, message [, params])

Returns: Boolean - true when the predicate is false (or null)

APOC Compatible: apoc.util.validatePredicate

MATCH (n:Person)
WHERE apoc.util.validatePredicate(n.age IS NULL, 'person %s has no age', [n.name])
RETURN n.name