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;
}
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));
}
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);
}
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();
}
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);
}
Aggregations