CREATE FUNCTION
Executes user-provided Java or Javascript code in SELECT, UPDATE, INSERT or provides a building block for user-defined aggregate. Functions are only available in the keyspace where they were created.
UDF supports Java generic methods or Javascript in the user provided codeblock. UDFs are susceptible to all of the normal issues that may occur with the chosen programming language. Safe guard against exceptions, such as null pointer exceptions, illegal arguments, or any other potential sources of problems. An exception during function execution results in the entire statement failing.
By default, the database does not allow UDFs. To enable, change the following settings in the cassandra.yaml and restart all nodes:
|
Synopsis
CREATE [ OR REPLACE ] FUNCTION [ IF NOT EXISTS ] [<keyspace_name>]<function_name> (<argument_list> [ , ... ]) ( CALLED | RETURNS NULL ) ON NULL INPUT RETURNS <type> [ DETERMINISTIC ] [ MONOTONIC [ ON <argument_name> ] ] LANGUAGE ( java | javascript ) AS $$ <code_block> $$ ;
Syntax legend
Syntax conventions | Description |
---|---|
UPPERCASE |
Literal keyword. |
Lowercase |
Not literal. |
|
Variable value. Replace with a user-defined value. |
|
Optional.
Square brackets ( |
|
Group.
Parentheses ( |
|
Or.
A vertical bar ( |
|
Repeatable.
An ellipsis ( |
|
Single quotation ( |
|
Map collection.
Braces ( |
Set, list, map, or tuple.
Angle brackets ( |
|
|
End CQL statement.
A semicolon ( |
|
Separate the command line options from the command arguments with two hyphens ( |
|
Search CQL only: Single quotation marks ( |
|
Search CQL only: Identify the entity and literal value to overwrite the XML element in the schema and solrConfig files. |
- <function_name>
-
Specify a keyspace-qualified function name. Names must start with a letter or number. To preserve case, enclose the name in double-quotes.
- OR REPLACE
-
Overwrite the function, if one already exists with the same name.
Cannot be used with the |
- IF NOT EXISTS
-
Performs no operation and suppresses the error message if a function with the same name already exists.
- argument_list
-
Comma separated list of arguments with data types passed to the code block for processing:
<arg_name> <cql_type> [,...]
In the list, specify an argument name followed by the CQL data type.
For requests, an argument value can be read from a column with the corresponding data type or manually entered (literal).
- CALLED ON NULL INPUT
-
Executes the user-provided code block even if the input value is null or missing.
- RETURNS NULL ON NULL INPUT
-
Does not execute the user-provided code block on null values; returns null.
- RETURNS <cql_data_type>
-
Map the expected output from the code block to a compatible CQL data type.
- DETERMINISTIC
-
Specify for functions that always returns the same output for a certain input. For example, toJson() is a deterministic function, while now() and currentDate() are not.
Default:
false
(non-deterministic).
GROUP BY only supports functions that are both deterministic and monotonic. |
- MONOTONIC [ ON <argument_name> ]
-
All arguments or the specified argument are monotonic if they are either entirely non-increasing or non-decreasing.
GROUP BY only supports functions that are both deterministic and monotonic. |
- LANGUAGE <language_name>
-
Supported types are
java
orjavascript
.When https://docs.datastax.com/en/dse/6.8/dse-admin/datastax_enterprise/config/configCassandra_yaml.html#configCassandra_yamlenable_scripted_user_defined_functions[enable_scripted_user_defined_functions] is false and https://docs.datastax.com/en/dse/6.8/dse-admin/datastax_enterprise/config/configCassandra_yaml.html#configCassandra_yamlenable_user_defined_functions[enable_user_defined_functions] is true, Java is the only supported language.
- '<code_block>' | $$ <code_block> $$
-
Enclose the code block in single quotes or if the code block contains any special characters enclose it in double dollar signs ($$). The code is wrapped as a function and applied to the target variables.
Examples
Use Java to create FLOG function
Overwrite or create the fLog function that computes the logarithm of an
input value. CALLED ON NULL INPUT
ensures that the function will
always be executed.
CREATE OR REPLACE FUNCTION cycling.flog ( input double )
CALLED ON NULL INPUT
RETURNS double
LANGUAGE java AS
$$ return Double.valueOf(Math.log(input.doubleValue())); $$
;
Results
WARNING: cqlsh was built against 5.0-beta1, but this server is 5.0. All features may not work!
CREATE FUNCTION cycling.flog(input double)
CALLED ON NULL INPUT
RETURNS double
LANGUAGE java
AS $$ return Double.valueOf(Math.log(input.doubleValue())); $$;
Use Javascript to create SQL-like LEFT function
Create a function that returns the first <N> characters from a text field in Javascript. RETURNS NULL ON NULL INPUT ensures that if the input value is null then the function is not executed.
CREATE OR REPLACE FUNCTION cycling.left (column text, num int)
RETURNS NULL ON NULL INPUT
RETURNS text
LANGUAGE javascript AS $$ column.substring(0, num) $$;
Results
WARNING: cqlsh was built against 5.0-beta1, but this server is 5.0. All features may not work!
Use the function in requests:
SELECT left(firstname, 1), lastname FROM cycling.cyclist_name;
Results
WARNING: cqlsh was built against 5.0-beta1, but this server is 5.0. All features may not work!