[
https://issues.apache.org/jira/browse/CALCITE-3000?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16818596#comment-16818596
]
pengzhiwei commented on CALCITE-3000:
-------------------------------------
Hi [~julianhyde], I mean overload function.Thanks for your correction.
Here is the example :
{code:java}
@Function(name = "my_udf")
public class MyUdf {
public static String eval(String arg0,String arg1) {...}
public static String eval(long arg0, long arg1) {...}
public static String eval(int arg0, int arg1) {...}
}{code}
_MyUdf_ is a user defined scalar function named "my_udf" with multiple
overload method. Currently the ModelHandler would register multiple
ScalarFunctions to the schema for each method.
I would like to mapping one SqlUserDefinedFunction to "my_udf" no mater how
many overload functions it has just like the BuildIn operator.
To implement this, I will extend the Types#lookupMethod:
{code:java}
public static Method lookupMethod(Class clazz,String name, Class... inputTypes)
{
// Find the best match method from the overload functions for the input types.
....
}{code}
In the SqlReturnTypeInference for SqlUserDefinedFunction, I can use the
Types#lookupMethod to infer the return type :
{code:java}
SqlReturnTypeInference returnTypeInference =
opBinding -> {
......
List<Class<?> paramTypes = convertToJavaTypes(
opBinding.collectOperandTypes(), opBinding.getTypeFactory());
Method method = Types.lookupMethod(udfClazz, "eval", paramTypes);
return typeFactory.createType(method.getReturnType());
}{code}
The similar thing can apply to SqlOperandTypeInference and
SqlOperandTypeChecker.
So we can implement a dynamic type inference for the overload functions in
scalar function. And there is only one SqlUserDefinedFunction mapping to the
sql function.
We can also do the same thing for SqlUserDefinedTableFunction for the "my_udtf"
as followed:
{code:java}
@Function(name = "my_udtf")
public class MyUdtf {
public static ScannableTable eval(String arg0,String arg1) {...}
public static ScannableTable eval(long arg0, long arg1) {...}
public static ScannableTable eval(int arg0, int arg1) {...}
}
{code}
We mapping one SqlUserDefinedTableFunction to the "my_udtf" table function and
support all the overload methods call.
> Improve SqlUserDefinedFunction and SqlUserDefinedTableFunction to support
> overload method call
> ----------------------------------------------------------------------------------------------
>
> Key: CALCITE-3000
> URL: https://issues.apache.org/jira/browse/CALCITE-3000
> Project: Calcite
> Issue Type: Improvement
> Components: core
> Reporter: pengzhiwei
> Assignee: pengzhiwei
> Priority: Major
>
> Currently the ModelHandler adds one Function to the schema for one java
> method. For the scalar function,ModelHandler resolves all the method in the
> class and translate each of them to ScalarFunction to support overload method
> call. But for TableFunction, this has not been support yet.
> Maybe we can support it as the scalar function does. However in that way,
> one sql function may match multiple Functions in the schema.I think this is
> not a good way. It is better to have only one Function in the schema for one
> sql function just like the BuildIn operator.
> I'd like to support a new operand type check strategy for
> SqlUserDefinedFunction and SqlUserDefinedTableFunction which can infer the
> operand type and return type according to the input parameter types.
> I will extend the Types#lookupMethod to support finding the best method in
> all of the override methods in a class. And the
> SqlReturnTypeInference、SqlOperandTypeInference and
> SqlOperandTypeChecker can use this to do the type inference according to the
> input types. I think it is a dynamically way compared with the original way.
> Any suggestion is welcomed,thanks!
>
>
>
>
>
>
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)