简体   繁体   中英

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?

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 SQL 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};
        }
    }
$$;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM