Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Java Driver. Switch to the latest stable version.

Scylla Java Driver Manual Query builder Schema builder Function

Function¶

User-defined functions (UDF) enable users to create user code written in JSR-232 compliant scripting languages that can be evaluated in CQL queries. SchemaBuilder offers API methods for creating and dropping UDFs.

Creating a Function (CREATE FUNCTION)¶

To start a CREATE FUNCTION query, use createFunction in SchemaBuilder:

import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.*;

CreateFunctionStart create = createFunction("log");

Like all other CREATE queries, one may supply ifNotExists() to require that the UDF should only be created if it doesn’t already exist, i.e.:

CreateFunctionStart create = createFunction("cycling", "log").ifNotExists();

You may also specify that you would like to replace an existing function by the same signature if it exists. In this case, use orReplace:

CreateFunctionStart create = createFunction("cycling", "log").orReplace();

One may also specify the parameters of a function using withParameter:

createFunction("cycling", "left")
    .withParameter("colName", DataTypes.TEXT)
    .withParameter("num", DataTypes.DOUBLE)

There are a number of steps that must be executed to complete a function:

  • Specify whether the function is called on null input (calledOnNull) or if it should simply return null (returnsNullOnNull).

  • Specify the return type of the function using returnsType

  • Specify language of the function body using withJavaLanguage, withJavaScriptLanguage, or withLanguage

  • Specify the function body with as or asQuoted

For example, the following defines a complete CREATE FUNCTION statement:

createFunction("cycling", "log")
    .withParameter("input", DataTypes.DOUBLE)
    .calledOnNull()
    .returnsType(DataTypes.DOUBLE)
    .withJavaLanguage()
    .asQuoted("return Double.valueOf(Math.log(input.doubleValue()));");

// CREATE FUNCTION cycling.log (columnname text,num int) CALLED ON NULL INPUT RETURNS double LANGUAGE java
// AS 'return Double.valueOf(Math.log(input.doubleValue()));'

Note that when providing a function body, the as method does not implicitly quote your function body. If you would like to have the API handle this for you, use asQuoted. This will surround your function body in single quotes if the body itself does not contain a single quote, otherwise it will surround your function body in two dollar signs mimicking a postgres-style string literal, i.e.:

createFunction("sayhi")
    .withParameter("input", DataTypes.TEXT)
    .returnsNullOnNull()
    .returnsType(DataTypes.TEXT)
    .withJavaScriptLanguage()
    .asQuoted("'hi ' + input;");
// CREATE FUNCTION sayhi (input text) RETURNS NULL ON NULL INPUT RETURNS text LANGUAGE javascript AS $$ 'hi ' + input; $$

Dropping a Function (DROP FUNCTION)¶

To create a DROP FUNCTION query, use dropFunction:

dropFunction("cycling", "log");
// DROP FUNCTION cycling.log

You may also specify ifExists:

dropFunction("log").ifExists();
// DROP FUNCTION IF EXISTS log
PREVIOUS
Aggregate
NEXT
Index
  • 4.11.1.x
    • 4.13.0.x
    • 4.12.0.x
    • 4.11.1.x
    • 4.10.0.x
    • 4.7.2.x
    • 3.11.2.x
    • 3.11.0.x
    • 3.10.2.x
    • 3.7.2.x
  • Java Driver for Scylla and Apache Cassandra®
  • API Documentation
  • Manual
    • API conventions
    • Case sensitivity
    • Core driver
      • Address resolution
      • Asynchronous programming
      • Authentication
      • Bill of Materials (BOM)
      • Compression
      • Configuration
        • Reference configuration
      • Control connection
      • Custom codecs
      • Detachable types
      • Query idempotence
      • Integration
      • Load balancing
      • Logging
      • Metadata
        • Node metadata
        • Schema metadata
        • Token metadata
      • Metrics
      • Native protocol
      • Non-blocking programming
      • Paging
      • Performance
      • Connection pooling
      • Query timestamps
      • Reactive Style Programming
      • Reconnection
      • Request tracker
      • Retries
      • Using the shaded JAR
      • Speculative query execution
      • SSL
      • Statements
        • Batch statements
        • Per-query keyspace
        • Prepared statements
        • Simple statements
      • Temporal types
      • Request throttling
      • Query tracing
      • Tuples
      • User-defined types
    • Developer docs
      • Administrative tasks
      • Common infrastructure
        • Concurrency
        • Driver context
        • Event bus
      • Native protocol layer
      • Netty pipeline
      • Request execution
    • Mapper
      • Integration
        • Kotlin
        • Lombok
        • Java 14 Records
        • Scala
      • DAOs
        • Custom result types
        • Delete methods
        • GetEntity methods
        • Increment methods
        • Insert methods
        • Null saving strategy
        • Query methods
        • Query provider methods
        • Select methods
        • SetEntity methods
        • Statement attributes
        • Update methods
      • Entities
      • Mapper interface
    • OSGi
    • Query builder
      • Conditions
      • DELETE
      • Idempotence in the query builder
      • INSERT
      • Relations
      • Schema builder
        • Aggregate
        • Function
        • Index
        • Keyspace
        • Materialized View
        • Table
        • Type
      • SELECT
      • Terms
      • TRUNCATE
      • UPDATE
  • Upgrade guide
  • Frequently asked questions
  • Changelog
  • Create an issue
  • Edit this page

On this page

  • Function
    • Creating a Function (CREATE FUNCTION)
    • Dropping a Function (DROP FUNCTION)
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© 2022, ScyllaDB. All rights reserved.
Last updated on 25 May 2022.
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2