ksql icon indicating copy to clipboard operation
ksql copied to clipboard

Augment UdfSchemaProvider Interface with more information

Open agavra opened this issue 6 years ago • 1 comments

Instead of requiring the signature SqlType provider(List<SqlType> inputTypes), we should have a more powerful signature such as SqlType provider(UdfSignature signature) which will contain not just the input types, but also indicate whether at any position one of the values is a literal.

We could use this and #3414, to implement something like TO_STRUCT("foo", col1, "bar", col2)

@Udf 
public class ToStruct {
 
  // ... implement the UDF itself

  @SchemaProvider
  public SqlType provider(final UdfSignature singature) {
    // check for even number of arguments
    for (int i = 0; i < signature.arguments.size(); i+=2) {
      // add struct field with name signature.arguments(i).literalValue()
      // and type signature.arguments(i+1).type()
    }
  }
}

agavra avatar Sep 25 '19 21:09 agavra

We can use the same extension to add better error handling to some of our UDFs too.

Consider #3451, which highlights a doc issue with TIMESTAMPTOSTRING. However, when I read that bug I thought 'we should reject that statement because we know the supplied zone is invalid - and we could include the list of supported zones in the error message', but we've no way of implementing that.

With this change TimestampToString could have a schema provider that returned a known/fixed schema, but which also checked the signature and if the zone was a literal, (it might be a column ref), it could check that it was a supported zone and throw a very helpful error message if it wasn't... boom, better UX :D

big-andy-coates avatar Oct 11 '19 12:10 big-andy-coates