twalthr commented on a change in pull request #13144:
URL: https://github.com/apache/flink/pull/13144#discussion_r480140388



##########
File path: 
flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/functions/hive/HiveScalarFunction.java
##########
@@ -104,4 +114,66 @@ public Object eval(Object... args) {
         * Evaluation logical, args will be wrapped when is a single array.
         */
        protected abstract Object evalInternal(Object[] args);
+
+       private void setArguments(CallContext callContext) {
+               DataType[] inputTypes = 
callContext.getArgumentDataTypes().toArray(new DataType[0]);
+               Object[] constantArgs = new Object[inputTypes.length];
+               for (int i = 0; i < constantArgs.length; i++) {
+                       if (callContext.isArgumentLiteral(i)) {
+                               constantArgs[i] = callContext.getArgumentValue(
+                                               i, 
ClassLogicalTypeConverter.getDefaultExternalClassForType(inputTypes[i].getLogicalType()))
+                                               .orElse(null);
+                       }
+               }
+               this.constantArguments = constantArgs;
+               this.argTypes = inputTypes;
+       }
+
+       /**
+        * Infer return type of this function call.
+        */
+       protected abstract DataType inferReturnType() throws 
UDFArgumentException;
+
+       private class HiveUDFOutputStrategy implements TypeStrategy {
+
+               @Override
+               public Optional<DataType> inferType(CallContext callContext) {
+                       setArguments(callContext);
+                       try {
+                               return Optional.of(inferReturnType());
+                       } catch (UDFArgumentException e) {
+                               throw new FlinkHiveUDFException(e);
+                       }
+               }
+       }
+
+       private class HiveUDFInputStrategy implements InputTypeStrategy {
+
+               @Override
+               public ArgumentCount getArgumentCount() {
+                       return ConstantArgumentCount.any();
+               }
+
+               @Override
+               public Optional<List<DataType>> inferInputTypes(CallContext 
callContext, boolean throwOnFailure) {
+                       setArguments(callContext);
+                       try {
+                               inferReturnType();
+                       } catch (UDFArgumentException e) {
+                               if (throwOnFailure) {
+                                       throw new ValidationException(
+                                                       String.format("Cannot 
find a suitable Hive function from %s for the input arguments",
+                                                                       
hiveFunctionWrapper.getClassName()), e);
+                               } else {
+                                       return Optional.empty();
+                               }
+                       }
+                       return Optional.of(callContext.getArgumentDataTypes());
+               }
+
+               @Override
+               public List<Signature> getExpectedSignatures(FunctionDefinition 
definition) {
+                       return 
Collections.singletonList(Signature.of(Signature.Argument.of("*")));

Review comment:
       Actually, this is why this method is very generic and allows you to pass 
arbitrary strings like `NUMERIC` or `ANY`. I don't know how Hive implements 
UDFs but as a user I would find it useful to get exceptions like
   ```
   Invalid input arguments. Expected signatures are:\n
   f(STRING NOT NULL)\nf(INT NOT NULL)\nf(BIGINT NOT NULL)
   ```
   
   Btw you can also use `InputTypeStrategiesTestBase` for testing. See 
`InputTypeStrategiesTest` for an example.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to