calcite icon indicating copy to clipboard operation
calcite copied to clipboard

[CALCITE-2772] Support varargs UDF for scalar function

Open gr4ve opened this issue 5 years ago • 16 comments

Support varargs for user-defined functions as the case followed:

public class ConcatWs { public String eval(String sep, String... strs) {...} }

JIRA: https://issues.apache.org/jira/browse/CALCITE-2772

gr4ve avatar Aug 24 '19 06:08 gr4ve

Can you create a JIRA for this and link this PR with JIRA?

hsyuan avatar Aug 25 '19 00:08 hsyuan

Sorry, I found the JIRA: https://issues.apache.org/jira/browse/CALCITE-2772

hsyuan avatar Aug 25 '19 00:08 hsyuan

@danny0405

One example I found for varargs use case is when I tried to built ZetaSQL on top of Calcite. ZetaSQL has a concat function definition that accepts varargs [1]. So varargs will make concat(string...) implementable. I will agree concat(string...) can be written as a nested of binary concat, but it makes query more readable if need to concat more than 3 strings.

[1] : https://github.com/google/zetasql/blob/master/docs/string_functions.md#concat

amaliujia avatar Sep 27 '19 01:09 amaliujia

Another example is ST_Reclass in PostGIS. Variable argument function is helpful.

hsyuan avatar Sep 27 '19 01:09 hsyuan

@danny0405

One example I found for varargs use case is when I tried to built ZetaSQL on top of Calcite. ZetaSQL has a concat function definition that accepts varargs [1]. So varargs will make concat(string...) implementable. I will agree concat(string...) can be written as a nested of binary concat, but it makes query more readable if need to concat more than 3 strings.

[1] : https://github.com/google/zetasql/blob/master/docs/string_functions.md#concat

Thanks for the example for CONCAT, in Calcite, we already have such function support as sql dialect [1], we declare it's operand types as OperandTypes.repeat, for repeated types arguments, we can support as this way, but if the arguments are different types but also variadic, maybe support freely variadic arguments is the right way to go.

[1] https://github.com/apache/calcite/blob/2dc97e6723e1b5bf762540f87ffffb5cd1a848a1/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java#L255

danny0405 avatar Sep 29 '19 08:09 danny0405

Maybe I was looking into a wrong place. At least Calcite does not have a vararg version of concat in [1]. This PR should be able to enable it as last time I tried.

[1] : https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java#L447

amaliujia avatar Sep 30 '19 22:09 amaliujia

I have fixed most issues based on above comments. Are there any issues that need to be improved? @danny0405

gr4ve avatar Jan 16 '20 08:01 gr4ve

Hi, @gr4ve, could you please rebase this work?

We have encountered use case that varargs UDF can play important role. As we use Calcite to connect different datasources, we need to define many "fake" (i.e., just registered but without implemention) functions and push them down and then generates SQL to run in underlying datasources. On one hand, the function iteself may be vararag. On the other hand, varargs UDF can help us to define just one for many functions that have same name.

DonnyZone avatar Jan 17 '20 10:01 DonnyZone

Hi, @gr4ve, could you please rebase this PR?

XuQianJin-Stars avatar Feb 11 '20 02:02 XuQianJin-Stars

It seems that @gr4ve adressed the comments of @danny0405 . @DonnyZone , @XuQianJin-Stars do you believe this PR is ready for merge? If yes then I can do the rebase and solve any remaining minor glithces (if there are).

zabetak avatar Feb 11 '20 08:02 zabetak

Thanks @zabetak, I have not ever tried the fix in our environment. But the PR is OK to me. I do not have any further comments.

DonnyZone avatar Feb 11 '20 09:02 DonnyZone

I have merged the latest master commit to this branch

gr4ve avatar Feb 13 '20 01:02 gr4ve

Hi @gr4ve, to solve the conflicts, you need to rebase this pr on latest calcite/master branch.

DonnyZone avatar Mar 27 '20 03:03 DonnyZone

@gr4ve, do you still want to work on this? If no, I would like to take over if you don't mind.

chunweilei avatar Jul 30 '20 03:07 chunweilei

when will this pull request merge? this feature is very helpful. thanks @gr4ve @hsyuan

astor-oss avatar Mar 28 '22 12:03 astor-oss

Agreed. This is something we'd really like because we're having trouble working around it. Any chance this could be handed off to someone with the time to do it?

ian-bertolacci avatar Apr 04 '22 20:04 ian-bertolacci