User-defined functions (UDFs) are a very useful feature supported in SQL++. Couchbase 7.6 introduces improvements that allow for more debuggability and visibility into UDF execution.

This blog will explore two new features in Couchbase 7.6 in the world of UDFs.

    1. Profiling for SQL++ statements executed in JavaScript UDFs
    2. EXPLAIN FUNCTION to access query plans of SQL++ statements within UDFs

The examples in this post require the travel-sample dataset to be installed.

Profiling SQL++ Executed in JavaScript UDFs

Query profiling is a debuggability feature that SQL++ offers.

When profiling is enabled for a statement’s execution, the result of the request includes a detailed execution tree with timing and metrics of each step of the statement’s execution. In addition to the profiling information being returned in the results of the statement, it can also be accessed for the request in the system:active_requests and system:completed_requests system keyspaces.

Here is a post that delves deeper into request profiling.

In Couchbase 7.0, profiling was included for subqueries, including profiling subqueries from Inline UDFs.

However, with new features in Couchbase 7.6, profiling was extended to SQL++ statements within JavaScript UDFs.

In earlier versions, to profile statements within a JavaScript UDF, the user would be required to open up the function’s definition, individually run each statement within the UDF and collect their profiles. This additional step will no longer be needed in 7.6.0!

Now, when profiling is enabled, if the statement contains JavaScript UDF execution, profiles for all SQL++ statements executed in the UDF will also be collected. And this UDF related profiling information will be available in the request output, system:active_requests and system:completed_requests system keyspaces as well.

Example 1:

Create a JavaScript UDF js1 in a global library lib1 via the REST endpoint or via the UI.

Create the corresponding SQL++ function:

Execute the UDF with profiling enabled:

In the profile section of the returned response, the executionTimings subsection contains a field ~udfStatements.

~udfStatements is an array of profiling information that contains an entry for every SQL++ statement within the JavaScript UDF.

Every entry within the ~udfStatements section contains:

    • executionTimings The execution tree for the statement. It has metrics and timings information of every step of the statement’s execution.
    • statementThe statement string.
    • functionThe name of the function where the statement was executed. This is helpful to identify the UDF that executed the statement when there are nested UDF executions.

Query Plans with EXPLAIN FUNCTION

SQL++ offers another wonderful capability to access the plan of a statement with the EXPLAIN statement. But the EXPLAIN statement does not extend to plans of statements within UDFs—neither Inline or JavaScript UDFs.

In earlier versions, to analyze the query plans for SQL++ within a UDF it would require the user to open the function’s definition, and individually run an EXPLAIN on all the statements within the UDF.

These extra steps are minimized in Couchbase 7.6 with the introduction of a new statement—EXPLAIN FUNCTION. This statement does exactly what EXPLAIN does, but for SQL++ statements within a UDF.

Let’s explore how to use the EXPLAIN FUNCTION statement!

Syntax

Here, function refers to the name of the function.

For more detailed information on syntax, please check out the documentation.

Prerequisites

To run EXPLAIN FUNCTION on a UDF, the user must have sufficient RBAC permissions to execute the function.

The user must also have the necessary RBAC permissions to execute the SQL++ statements within the UDF function body.

Here are the roles supported in Couchbase.

Inline UDF

EXPLAIN FUNCTION on an inline UDF will return the query plans of all the subqueries within its definition.

Example 2 – EXPLAIN FUNCTION on an inline function

Create an inline UDF and run EXPLAIN FUNCTION on it:

The results of the above statement will contain:

    • function – The name of the function EXPLAIN FUNCTION was run on.
    • plans – An array of plan information that contains an entry for every subquery within the Inline UDF.

JavaScript UDF

SQL++ statements within JavaScript UDFs can be of two types and EXPLAIN FUNCTION works differently based on the way the SQL++ statement is called.

Here is the documentation reference to calling SQL++ in JavaScript functions.

Embedded SQL++

    • Embedded SQL++ is “embedded” in the function body and its detection is handled by the JavaScript transpiler.
    • EXPLAIN FUNCTION can return query plans for embedded SQL++ statements.

 SQL++ executed by the N1QL() function call

    • SQL++ can also be executed by passing a statement in the form of a string as an argument to the N1QL() function.
    • When parsing the function for potential SQL++ statements to run the EXPLAIN on, it is difficult to get the dynamic string in the function argument. This can only be reliably resolved at runtime.
    • With this reasoning, EXPLAIN FUNCTION does not return the query plans for SQL++ statements executed via N1QL() calls. But instead returns the line numbers where the N1QL() function calls have been made. This line number is calculated from the beginning of the function definition.
    • The user can then map the line numbers in the actual function definition and investigate further.

Example 3 – EXPLAIN FUNCTION on an external JavaScript function

Create a JavaScript UDF js2 in a global library lib1 via the REST endpoint or via the UI:

Create the corresponding SQL++ function:

Run EXPLAIN FUNCTION on the SQL++ function:

The results of the above statement will contain:

    1. function – The name of the function EXPLAIN FUNCTION was run on.
    2. line_numbers – An array of line numbers calculated from the beginning of the JavaScript function definition where there are N1QL() function calls.
    3. plans – An array of plan information that contains an entry for every embedded SQL++ statement within the JavaScript UDF.

Constraints

If the N1QL() function has been aliased in a JavaScript function definition, EXPLAIN FUNCTION will not be able to return the line numbers where this aliased function was called. For example:

If the UDF contains nested UDF executions, EXPLAIN FUNCTION does not support generating the query plans of SQL++ statements within these nested UDFs.

Summary

Couchbase 7.6 introduces new features for UDF debuggability which will help users peek into UDF execution easily.

Reference the following documentation links to learn more, or check out the other Couchbase 7.6 innovations:

 

Author

Posted by Dhanya Gowrish, Software Engineer

Leave a reply