A newer version of this documentation is available.

View Latest

EXECUTE FUNCTION

  • reference
    +
    The EXECUTE FUNCTION statement enables you to execute a user-defined function.

    RBAC Privileges

    To execute global internal functions, you must have the Execute Global Functions role. To execute scoped internal functions, you must have the Execute Scope Functions role, with permissions on the specified bucket and scope.

    To execute global external functions, you must have the Execute Global External Functions role. To execute scoped external functions, you must have the Execute Scope External Functions role, with permissions on the specified bucket and scope.

    For more details about user roles, see Authorization.

    Purpose

    The EXECUTE FUNCTION statement enables you to execute a user-defined function. It is useful for testing user-defined functions outside the context of a query. It also enables you to execute functions which have side effects, such as performing mutations, which is not possible when calling a user-defined function in an expression.

    You cannot use the EXECUTE FUNCTION statement to execute a built-in N1QL function. If you do this, error 10101: Function not found is generated.

    Syntax

    execute-function ::= 'EXECUTE' 'FUNCTION' function '(' ( expr ( ',' expr )* )? ')'
    Syntax diagram
    function

    Function Name

    expr

    Arguments

    Function Name

    function ::= ( namespace ':' ( bucket '.' scope '.' )? )? identifier
    Syntax diagram

    The name of the function. This is usually an unqualified identifier, such as func1 or `func-1`. In this case, the path to the function is determined by the current query context.

    To execute a global function in a particular namespace, the function name must be a qualified identifier with a namespace, such as default:func1. Similarly, to execute a scoped function in a particular scope, the function name must be a qualified identifier with the full path to a scope, such as default:`travel-sample`.inventory.func1. Refer to Global Functions and Scoped Functions for more information.

    The name of a user-defined function is case-sensitive, unlike that of a built-in function. You must execute the user-defined function using the same case that was used when it was created.

    Arguments

    [Optional] Comma-separated expressions specify arguments for the function. If the function was created with named parameters, you must supply all the arguments that were specified when the function was created. If the function was created without named parameters, you cannot supply an argument. If the function is variadic, you can supply as many arguments as needed, or none.

    Return Value

    The function returns one value, of any valid N1QL type. The result (and the data type of the result) depend on the expression or code that were used to define the function.

    If you supply the wrong number of arguments, or arguments with the wrong data type, the possible results differ, depending on whether the function is variadic, or requires a definite number of arguments.

    If the function requires a definite number of arguments:

    • If you do not supply enough arguments, the function generates error 10104: Incorrect number of arguments.

    • If you supply too many arguments, the function generates error 10104: Incorrect number of arguments.

    • If any of the arguments have the wrong data type, the function may return unexpected results, depending on the function expression or code.

    If the function is variadic:

    • If you do not supply enough arguments, the function may return unexpected results, depending on the function expression or code.

    • If you supply too many arguments, the extra parameters are ignored.

    • If any of the arguments have the wrong data type, the function may return unexpected results, depending on the function expression or code.

    Examples

    For examples, refer to CREATE FUNCTION.