1

Snowflake Java UDF can't return an array of floats?

I'm trying to return an array of Floats in a Java UDF, but Snowflake throws an error:

create or replace function float_array_test()
returns array
language java
handler='MyClass.test'
as
$$
    class MyClass {
        public static Float[] test() {
            return new Float[] {1.1, 2.2};
        }
    }
$$;

Error:

100315 (P0000): Error while compiling source: /InlineCode.java:4: error: incompatible types: double cannot be converted to Float
            return new Float[] {1.1, 2.2};

Any way to fix?

Submitted July 23rd 2021 by Admin

Answers
0

Check the docs for allowed types: float is ok, but `Float is unsupported.

Therefore, this is the code fixed:

create or replace function float_array_test()
returns array
language java
handler='MyClass.test'
as
$$ class MyClass { public static float[] test() { return new float[] {(float)1.1, (float)2.2}; } }
$$;

But beware of the error incompatible types: double cannot be converted to Float, that's why I had to cast the hard-coded doubles to float.

An even better solution is to use doubles in Java land, which will be converted to floats in Snowflake land without precision loss:

create or replace function float_array_test()
returns array
language java
handler='MyClass.test'
as
$$ class MyClass { public static double[] test() { return new double[] {1.1, 2.2}; } }
$$;

Admin | 1 month ago



Relevant Questions