5

I've created a custom udf that is registered but when I try to select custom_udf(10) I get the following error: Exact implementation of BasicPlatform do not match expected java types Here is my udf, I can't seem to figure out what is wrong with it: public class ScalarUdfs {

private ScalarUdfs() {};

@ScalarFunction("basic_platform")
@SqlType(StandardTypes.VARCHAR)
public static Slice BasicPlatform(@SqlNullable @SqlType(StandardTypes.INTEGER) Integer id) {
    final Slice IOS = Slices.utf8Slice("iOS");
    final Slice ANDROID = Slices.utf8Slice("Android");
    final Slice WEB = Slices.utf8Slice("Web");
    final Slice OTHER = Slices.utf8Slice("Other");
    final Map<Integer, Slice> PLATFORM_MAP = new HashMap<Integer, Slice>() {{
        put(20, IOS);
        put(42, ANDROID);
        put(100, WEB);
    }};

    if (id == null || !PLATFORM_MAP.containsKey(id)) {
        return OTHER;
    }
    return PLATFORM_MAP.get(id);
}

}

Does anything seem obviously wrong? I want it to return a string given an int as a parameter, and I think the java and sql types match (Integer -> Integer), (Slice -> varchar).

Thanks

Ace Haidrey
  • 1,198
  • 2
  • 14
  • 27

1 Answers1

6

The question was also asked and answered on presto-users:

You have to use @SqlNullable @SqlType(StandardTypes.INTEGER) Long id (as SQL integer is backed by Long in Java).

Piotr Findeisen
  • 19,480
  • 2
  • 52
  • 82
  • 1
    Hey Piotr. Yeah I asked on both platforms. It's resolved, thanks :) – Ace Haidrey Apr 20 '17 at 16:54
  • 2
    @AceHaidrey I know. Answering here too for the sake of future visitors. – Piotr Findeisen Apr 21 '17 at 20:07
  • 1
    If `StandardTypes.INTEGER` is backed by `Long`, then what is `StandardTypes.BIGINT` backed by? Furthermore, do *smaller* `Java` types like `Integer`, `Short` etc back any `StandardTypes`? – y2k-shubham Aug 14 '18 at 11:30
  • 2
    @y2k-shubham when values are kept in memory in bulk, they are kept efficiently. When operating on single value (eg. when calling UDF), `long` is used for all integral types for simplicity. Presto is optimized for 64-bit processors, and it would bring no gain to operate on narrower / smaller types like byte, short, int, while presenting non-negligible maintenance burden within the code base. – Piotr Findeisen Aug 14 '18 at 12:33
  • Thanks. From source of [`TypeUtils`](https://github.com/prestodb/presto/blob/master/presto-spi/src/main/java/com/facebook/presto/spi/type/TypeUtils.java) it became evident that it's all `Long`s in there. Is there a place where they've documented all these type-mappings? Because now I'm stuck with `StandardType.VARCHAR` & `java.lang.String` – y2k-shubham Aug 14 '18 at 12:44
  • 2
    @y2k-shubham see `com.facebook.presto.spi.type.Type#getJavaType`. `VARCHAR` is represented with `io.airlift.slice.Slice`. – Piotr Findeisen Aug 14 '18 at 12:48