lirui-apache commented on a change in pull request #13144:
URL: https://github.com/apache/flink/pull/13144#discussion_r480038553
##########
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:
I'm not sure whether it's possible to get all the valid signatures for a
hive function. Some hive function supports implicit type conversion, e.g. a
function that operates on string values may take numeric input as well. I'll
double check.
----------------------------------------------------------------
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]