I am creating a plugin to host an UDF, it is an http_get function, receiving the httpAddress
path, the query parameters
and the request headers
. It is implemeted in scala:
@ScalarFunction(value = "http_get", deterministic = true)
@Description("Returns the result of an Http Get request")
@SqlType(value = StandardTypes.VARCHAR)
def httpGetFromArrayMap(
@SqlType(StandardTypes.VARCHAR) httpAddress: Slice,
@SqlType(constants.STRING_MAP) parameters: ImmutableMap[Slice, Slice],
@SqlNullable @SqlType(constants.STRING_MAP) headers: ImmutableMap[Slice, Slice],
): String = {
val stringHeaders = castSliceMap(headers)
val stringParams = castSliceMap(parameters)
val request = Http(httpAddress.toStringUtf8).headers(stringHeaders).params(stringParams)
val stringResponse = request.asString.body
stringResponse
}
When running on trino, it raises the following exception:
io.trino.spi.TrinoException: Exact implementation of http_get do not match expected java types.
The problem is: What is the corresponding java type to map(varchar,varchar)
?
I've tried Many:
- Scala Map[String, String]
- Java Map<String, String>
- Java Map<Slice, Slice>
- ImmutableMap<String, String>
- ImmutableMap<Slice, Slice>
Can't find any example of plugin implementing a function which receives a map.
Any help is appreciated. Thanks
CodePudding user response:
Disclaimer: I'm not familiar at all with Trino UDF
According to the examples you can find in Trino repository, the type to use for a SqlType("map(X,y)")
is io.trino.spi.block.Block
.
The Block
can then be manipulated to extract its content as if it were a regular Map
.
See for instance: https://github.com/trinodb/trino/blob/master/core/trino-main/src/main/java/io/trino/operator/scalar/MathFunctions.java#L1340