Last updated February 4, 2019 at 7:00PM.

Adding new functions to NanoDB is straightforward. NanoDB provides a number of classes for implementing various kinds of functions, which will then be available to use in SQL queries. These classes are in the edu.caltech.nanodb.functions package.

Naming Functions

The FunctionDirectory class (also in the functions package) specifies the mapping of function names used in SQL queries, to the actual function classes that implement the operation in Java. A few special conversions are used for function names.

Make sure that you do not give your function the same name as a SQL keyword. The NanoDB parser cannot handle such circumstances.


Here are the steps for implementing your function.

  1. Implement a class for your function in the edu.caltech.nanodb.functions package. You can name the class whatever you want, but follow the pattern in this package and choose a name that clearly indicates the purpose of the function. Make sure to subclass the appropriate function base-class (see above), based on what your function needs to do.

    There are many good examples in the functions package for you to base your implementation off of. For example, you can look at the If class implementation, which provides a SQL function "IF(cond, expr1, expr2)".

    Make sure to report an error if your function doesn't receive the correct number of arguments, or the correct types of arguments. These errors are reported directly to the user, and are essential to give users guidance as to the correct usage.

  2. Once you have completed your function class implementation, add it to the FunctionDirectory class, which is used to store and look up all available functions. You will do this in the initBuiltinFunctions() method.

    Note that functions are grouped by type, and are in alphabetical order. Simple functions come first, then aggregate functions, and then finally table functions.

    If you created an aggregate function that works with or without DISTINCT, make sure you create two entries, one for each usage pattern. (See the COUNT and AVG functions, for example.)

    Make sure to use an "ALLCAPS" name for your function, since the parser will convert the function name to all-capital letters before trying to resolve it against the FunctionDirectory.

Once you have completed these steps, your new function should be available for use within NanoDB.