twalthr commented on a change in pull request #13144:
URL: https://github.com/apache/flink/pull/13144#discussion_r479965411
##########
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 think it would be helpful to print the expected signature in case the
user passes wrong arguments?
##########
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)) {
Review comment:
We should also check for `isNullLiteral`?
##########
File path:
flink-connectors/flink-connector-hive/src/test/java/org/apache/flink/table/functions/hive/HiveSimpleUDFTest.java
##########
@@ -291,4 +297,58 @@ public String evaluate(String content) {
return content;
}
}
+
+ /**
+ * A CallContext implementation for Hive UDF tests.
+ */
+ public static class HiveUDFCallContext implements CallContext {
Review comment:
Use `org.apache.flink.table.types.inference.utils.CallContextMock`
##########
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(
Review comment:
the `CallContext` provides a method for throwing validation errors.
##########
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) {
Review comment:
Move this to a dedicated class because it will be shared for other
functions as well?
----------------------------------------------------------------
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]