Class TypeInferenceUtil

java.lang.Object
org.apache.flink.table.types.inference.TypeInferenceUtil

@Internal public final class TypeInferenceUtil extends Object
Utility for performing type inference.

The following steps summarize the envisioned type inference process. Not all features are implemented or exposed through the API yet (*).

  • 1. Validate number of arguments.
  • 2. (*) Apply assignment operators on the call by permuting operands and adding default values. These are preparations for CallContext.
  • 3. For resolving unknown (NULL) operands: Access the outer wrapping call and try to get its operand type for the return type of the actual call. E.g. for takes_string(this_function(NULL)) infer operands from takes_string(NULL) and use the inferred string type as the return type of this_function(NULL).
  • 4. Try infer unknown operands, fail if not possible.
  • 5. (*) Check the usage of DEFAULT operands are correct using validator.isOptional().
  • 6. Perform input type validation.
  • 7. (Optional) Infer accumulator type.
  • 8. Infer return type.
  • 9. (*) In the planner: Call the strategies again at any point in time to enrich a DataType that has been created from a logical type with a conversion class.
  • 10. (*) In the planner: Check for an implementation evaluation method matching the operands. The matching happens class-based. Thus, for example, eval(Object) is valid for (INT). Or eval(Object...) is valid for (INT, STRING). We rely on the conversion classes specified by DataType.
  • Method Details

    • runTypeInference

      public static TypeInferenceUtil.Result runTypeInference(TypeInference typeInference, CallContext callContext, @Nullable TypeInferenceUtil.SurroundingInfo surroundingInfo)
      Runs the entire type inference process.
      Parameters:
      typeInference - type inference of the current call
      callContext - call context of the current call
      surroundingInfo - information about the outer wrapping call of a current function call for performing input type inference
    • adaptArguments

      public static CallContext adaptArguments(TypeInference typeInference, CallContext callContext, @Nullable DataType outputType)
      Adapts the call's argument if necessary.

      This includes casts that need to be inserted, reordering of arguments (*), or insertion of default values (*) where (*) is future work.

    • inferOutputType

      public static DataType inferOutputType(CallContext callContext, TypeStrategy outputTypeStrategy)
      Infers an output type using the given TypeStrategy. It assumes that input arguments have been adapted before if necessary.
    • generateSignature

      public static String generateSignature(TypeInference typeInference, String name, FunctionDefinition definition)
      Generates a signature of the given FunctionDefinition.
    • createInvalidInputException

      public static ValidationException createInvalidInputException(TypeInference typeInference, CallContext callContext, ValidationException cause)
      Returns an exception for invalid input arguments.
    • createInvalidCallException

      public static ValidationException createInvalidCallException(CallContext callContext, ValidationException cause)
      Returns an exception for an invalid call to a function.
    • createUnexpectedException

      public static TableException createUnexpectedException(CallContext callContext, Throwable cause)
      Returns an exception for an unexpected error during type inference.
    • validateArgumentCount

      public static boolean validateArgumentCount(ArgumentCount argumentCount, int actualCount, boolean throwOnFailure)
      Validates argument counts.
      Parameters:
      argumentCount - expected argument count
      actualCount - actual argument count
      throwOnFailure - if true, the function throws a ValidationException if the actual value does not meet the expected argument count
      Returns:
      a boolean indicating if expected argument counts match the actual counts