C++
See the following link for the full documentation of the C++ API.
In addition to interfacing with the database, the C++ API offers users the ability to define custom functions via User Defined Functions (UDFs), described below.
UDF API
Kùzu provides two interfaces that enable you to define your own custom scalar and vectorized functions.
Scalar functions
This API allows user to register ordinary scalar functions which are defined in c++ and use them as Kùzu built-in functions in a query. UDF functions are as efficient as the built-in functions.
Since two c++ primitive types may map to the same Cypher data types(e.g. int32
can map to both INT32
or DATE
in Kùzu), Kùzu provides two overloaded APIs which can eliminate the ambiguity in datatype mapping.
Option 1: Scalar function with automatic inference
Create a scalar function by automatically inferring the parameter and result type in Kùzu.
Template parameters:
TR
: return type of the UDF in c++.ARGS
: are the type of the arguments in c++, Kùzu currently support UDF functions with up to 3 parameters.
Parameters:
name
: the name of the function to be created in Kùzu (note: function name must be unique).udfFunc
: the UDF defined in c++.
Inference rule for c++ type to cypher type is defined as follows:
C++ type | Cypher type |
---|---|
bool | BOOLEAN |
int16 | INT16 |
int32 | INT32 |
int64 | INT64 |
float | FLOAT |
double | DOUBLE |
std::string | STRING |
Example
Option 2: Scalar function with input & return type in Cypher
Create a scalar function with input and return type in Cypher.
Template parameters:
TR
: return type of the UDF in c++.ARGS
: are the type of the arguments in c++, Kùzu currently support UDF functions with up to 3 parameters.
Parameters:
name
: the name of the function to be created in Kùzu (note: function name must be unique).parameterTypes
: the type of parameters in cypher.returnType
: the type of return value in cypher.udfFunc
: the UDF defined in c++.
Cypher and C++ type mapping
Cypher type | C++ type |
---|---|
BOOLEAN | bool |
INT16 | int16 |
INT32, DATE | int32 |
INT64, TIMESTAMP | int64 |
FLOAT | float |
DOUBLE | double |
STRING | std::string |
Example
Vectorized functions
Due its columnar storage and architecture decisions, Kùzu can execute functions on input data in an
efficient and vectorized manner. In addition to creating scalar UDFs, Kùzu also provides support for
user-defined vectorized UDFs. The createVectorizedFunction
interface also offers two APIs to enhance clarity in datatype mapping.
Vector types in Kùzu
The first type of vector that Kùzu supports is flat vectors.
The flat vector only holds one value at selectedPositions[0]
position.
The second type of vector is “unflat” vector.
This type of vector can hold SELECTED_SIZE
number of values.
Option 1. Vectorized function with automatic inference
Create a vectorized function by automatically inferring the parameter and result type in Kùzu.
Template parameters:
TR
: return type of the UDF in c++.ARGS
: are the type of the arguments in c++, Kùzu currently support UDF functions with up to 3 parameters.
Parameters:
name
: the name of the function to be created in Kùzu (note: function name must be unique).scalarFunc
: a vectorized udf function. The vectorized udf function takes in a vector of ValueVectors and puts the result in the resultVector.
Inference rule for c++ type to cypher type is defined as follows:
C++ type | Cypher type |
---|---|
bool | BOOLEAN |
int16 | INT16 |
int32 | INT32 |
int64 | INT64 |
float | FLOAT |
double | DOUBLE |
std::string | STRING |
Example
Option 2. Vectorized function with input and return type in Cypher
Create a vectorized function with input and return type in Cypher.
Parameters:
name
: the name of the function to be created in Kùzu (note: function name must be unique).parameterTypes
: the type of parameters in cypher.returnType
: the type of return value in cypher.scalarFunc
: a vectorized udf function. The vectorized udf function takes in a vector of ValueVectors and puts the result in the resultVector.
Inference rule for c++ type to cypher type is defined as follows:
Cypher type | C++ type |
---|---|
BOOLEAN | bool |
INT16 | int16 |
INT32, DATE | int32 |
INT64, TIMESTAMP | int64 |
FLOAT | float |
DOUBLE | double |
STRING | std::string |
Example
Linking
See the C API Documentation for details as linking to the C++ API is more or less identical.