Search in sources :

Example 1 with TypeSignature

use of io.crate.types.TypeSignature in project crate by crate.

the class Functions method isMoreSpecificThan.

/**
 * One method is more specific than another if invocation handled by the first method
 * could be passed on to the other one.
 * Additionally possible variadic type signatures are taken into account,
 * an exact amount of declared type signatures is more specific than expanded variadic type signatures.
 */
private static boolean isMoreSpecificThan(ApplicableFunction left, ApplicableFunction right) {
    List<TypeSignature> resolvedTypes = left.getBoundSignature().getArgumentTypes();
    BoundVariables boundVariables = SignatureBinder.withPrecedenceOnly(right.getDeclaredSignature()).bindVariables(resolvedTypes);
    if (boundVariables == null) {
        return false;
    }
    int leftArgsCount = left.getDeclaredSignature().getArgumentTypes().size();
    int rightArgsCount = right.getDeclaredSignature().getArgumentTypes().size();
    return leftArgsCount >= rightArgsCount;
}
Also used : TypeSignature(io.crate.types.TypeSignature) BoundVariables(io.crate.metadata.functions.BoundVariables)

Example 2 with TypeSignature

use of io.crate.types.TypeSignature in project crate by crate.

the class Signature method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    name.writeTo(out);
    out.writeVInt(kind.ordinal());
    out.writeVInt(argumentTypes.size());
    for (TypeSignature typeSignature : argumentTypes) {
        TypeSignature.toStream(typeSignature, out);
    }
    TypeSignature.toStream(returnType, out);
    out.writeVInt(EnumSets.packToInt(features));
}
Also used : TypeSignature(io.crate.types.TypeSignature)

Example 3 with TypeSignature

use of io.crate.types.TypeSignature in project crate by crate.

the class SignatureBinder method expandVarargFormalTypeSignature.

@Nullable
private static List<TypeSignature> expandVarargFormalTypeSignature(List<TypeSignature> formalTypeSignatures, List<TypeSignature> variableArityGroup, Map<String, TypeVariableConstraint> typeVariableConstraints, int actualArity) {
    int variableArityGroupCount = variableArityGroup.size();
    if (variableArityGroupCount > 0 && actualArity % variableArityGroupCount != 0) {
        // no match
        return null;
    }
    int arityCountIncludedInsideFormalSignature = variableArityGroupCount == 0 ? 1 : variableArityGroupCount;
    int variableArityArgumentsCount = actualArity - formalTypeSignatures.size() + arityCountIncludedInsideFormalSignature;
    if (variableArityArgumentsCount == 0) {
        return formalTypeSignatures.subList(0, formalTypeSignatures.size() - arityCountIncludedInsideFormalSignature);
    }
    if (variableArityArgumentsCount == arityCountIncludedInsideFormalSignature) {
        return formalTypeSignatures;
    }
    if (variableArityArgumentsCount > arityCountIncludedInsideFormalSignature && formalTypeSignatures.isEmpty()) {
        throw new IllegalArgumentException("Found variable argument(s) but list of formal type signatures is empty");
    }
    ArrayList<TypeSignature> builder = new ArrayList<>(formalTypeSignatures);
    if (variableArityGroup.isEmpty()) {
        TypeSignature lastTypeSignature = formalTypeSignatures.get(formalTypeSignatures.size() - 1);
        for (int i = 1; i < variableArityArgumentsCount; i++) {
            addVarArgTypeSignature(lastTypeSignature, typeVariableConstraints, builder, i);
        }
    } else {
        for (int i = 0; i < variableArityArgumentsCount - formalTypeSignatures.size(); ) {
            i += variableArityGroupCount;
            for (var typeSignature : variableArityGroup) {
                addVarArgTypeSignature(typeSignature, typeVariableConstraints, builder, i);
            }
        }
    }
    return Collections.unmodifiableList(builder);
}
Also used : TypeSignature(io.crate.types.TypeSignature) ParameterTypeSignature(io.crate.types.ParameterTypeSignature) ArrayList(java.util.ArrayList) Nullable(javax.annotation.Nullable)

Example 4 with TypeSignature

use of io.crate.types.TypeSignature in project crate by crate.

the class SignatureBinder method applyBoundVariables.

@Nullable
private static Signature applyBoundVariables(Signature signature, BoundVariables boundVariables, Map<String, TypeVariableConstraint> typeVariableConstraints, int arity) {
    List<TypeSignature> argumentSignatures;
    var bindingInfo = signature.getBindingInfo();
    assert bindingInfo != null : "Expecting the signature's binding info to be not null";
    if (bindingInfo.isVariableArity()) {
        argumentSignatures = expandVarargFormalTypeSignature(signature.getArgumentTypes(), bindingInfo.getVariableArityGroup(), typeVariableConstraints, arity);
        if (argumentSignatures == null) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Size of argument types does not match a multiple of the defined variable arguments");
            }
            return null;
        }
    } else {
        if (signature.getArgumentTypes().size() != arity) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Size of argument types does not match given arity");
            }
            return null;
        }
        argumentSignatures = signature.getArgumentTypes();
    }
    List<TypeSignature> boundArgumentSignatures = applyBoundVariables(argumentSignatures, boundVariables);
    TypeSignature boundReturnTypeSignature = applyBoundVariables(signature.getReturnType(), boundVariables);
    return Signature.builder().name(signature.getName()).kind(signature.getKind()).argumentTypes(boundArgumentSignatures).returnType(boundReturnTypeSignature).setVariableArity(false).build();
}
Also used : TypeSignature(io.crate.types.TypeSignature) ParameterTypeSignature(io.crate.types.ParameterTypeSignature) Nullable(javax.annotation.Nullable)

Example 5 with TypeSignature

use of io.crate.types.TypeSignature in project crate by crate.

the class SignatureBinder method applyBoundVariables.

private static TypeSignature applyBoundVariables(TypeSignature typeSignature, BoundVariables boundVariables) {
    String baseType = typeSignature.getBaseTypeName();
    if (boundVariables.containsTypeVariable(baseType)) {
        if (typeSignature.getParameters().isEmpty() == false) {
            throw new IllegalStateException("Type parameters cannot have parameters");
        }
        var boundTS = boundVariables.getTypeVariable(baseType).getTypeSignature();
        if (typeSignature instanceof ParameterTypeSignature) {
            return new ParameterTypeSignature(((ParameterTypeSignature) typeSignature).parameterName(), boundTS);
        }
        return boundTS;
    }
    List<TypeSignature> parameters = Lists2.map(typeSignature.getParameters(), typeSignatureParameter -> applyBoundVariables(typeSignatureParameter, boundVariables));
    if (typeSignature instanceof ParameterTypeSignature) {
        return new ParameterTypeSignature(((ParameterTypeSignature) typeSignature).parameterName(), new TypeSignature(baseType, parameters));
    }
    return new TypeSignature(baseType, parameters);
}
Also used : TypeSignature(io.crate.types.TypeSignature) ParameterTypeSignature(io.crate.types.ParameterTypeSignature) ParameterTypeSignature(io.crate.types.ParameterTypeSignature)

Aggregations

TypeSignature (io.crate.types.TypeSignature)6 ParameterTypeSignature (io.crate.types.ParameterTypeSignature)3 Nullable (javax.annotation.Nullable)2 FunctionName (io.crate.metadata.FunctionName)1 BoundVariables (io.crate.metadata.functions.BoundVariables)1 Signature (io.crate.metadata.functions.Signature)1 ArrayList (java.util.ArrayList)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1