Unable to create Snowflake Java function

Viewed 474

Based on Snowflake product manager demo here at youtube -- https://www.youtube.com/watch?v=F5DWBdhFQF4

I created a java based function, compiled to jar, uploaded to stage and attempted to create function but it keep giving sql compilation error.

It looks like Snowflake now support Java function (beyond java script and SQL functions) but I unable to make it to work still.

I am on business critical snowflake edition.

--Function

create or replace function calc(int x, int y, char op)
returns int
language java
import @stage_java/SnowflakeFunctions-1.0-SNAPSHOT.jar
classpath='Calculate';

---Error

[42000][1003] SQL compilation error: syntax error line 4 at position 7 unexpected '@stage_java/SnowflakeFunctions-1.0-SNAPSHOT.jar'.

--Java Code

public class Calculate {

    public static int compute(int x, int y, char op) {

        int result = 0;

        switch (op) {
            case '+':
                result = x + y;
                break;
            case '-':
                result = x - y;
                break;
            case '/':
                result = x / y;
                break;
            case '*':
                result = x * y;
                break;
        }

        return result;
    }
}
2 Answers

Following up, Java UDFs are in public preview in Snowflake. To build an example using your provided code:

create or replace function calc(x int, y int, op varchar)
returns int
language java
handler = 'Calculate.compute'
as $$
public class Calculate {
    public static int compute(int x, int y, String op) {
        int result = 0;

        switch (op.charAt(0)) {
            case '+':
                result = x + y;
                break;
            case '-':
                result = x - y;
                break;
            case '/':
                result = x / y;
                break;
            case '*':
                result = x * y;
                break;
        }

        return result;
    }
}
$$;

select calc(3, 5, '*');
+-----------------+                                                             
| CALC(3, 5, '*') |
|-----------------|
|              15 |
+-----------------+

Note that I've used inline code for this example, meaning the JAR for the code is built when the query executes. Alternatively, you can compile the JAR yourself and then copy it to a stage using a PUT command; see the documentation for the distinction between in-line and pre-compiled Java UDFs.

This is demo of the Java UDF feature which is yet to be available publicly.

Related