@Override public SolverReturnStatus update(BoundVariables.Builder bindings) ImmutableList.Builder<TypeSignatureParameter> originalTypeTypeParametersBuilder = ImmutableList.builder(); List<TypeSignatureParameter> parameters = formalTypeSignature.getParameters(); for (int i = 0; i < parameters.size(); i++) { TypeSignatureParameter typeSignatureParameter = parameters.get(i); if (typeSignatureParameter.getKind() == ParameterKind.VARIABLE) { if (bindings.containsLongVariable(typeSignatureParameter.getVariable())) { originalTypeTypeParametersBuilder.add(TypeSignatureParameter.of(bindings.getLongVariable(typeSignatureParameter.getVariable()))); Optional<Type> type = typeManager.coerceTypeBase(actualType, formalTypeSignature.getBase()); if (!type.isPresent()) { return SolverReturnStatus.UNSOLVABLE; TypeSignature typeSignature = type.get().getTypeSignature(); originalTypeTypeParametersBuilder.add(TypeSignatureParameter.of(typeSignature.getParameters().get(i).getLongLiteral())); verify(typeSignatureParameter.getKind() == ParameterKind.LONG); originalTypeTypeParametersBuilder.add(typeSignatureParameter); Type originalType = typeManager.getType(new TypeSignature(formalTypeSignature.getBase(), originalTypeTypeParametersBuilder.build())); Optional<Type> commonSuperType = typeManager.getCommonSuperType(originalType, actualType); if (!commonSuperType.isPresent()) { return SolverReturnStatus.UNSOLVABLE; TypeSignature commonSuperTypeSignature = commonSuperType.get().getTypeSignature(); if (!commonSuperTypeSignature.getBase().equals(formalTypeSignature.getBase())) { return SolverReturnStatus.UNSOLVABLE;
public static MapType mapType(Type keyType, Type valueType) { return (MapType) TYPE_MANAGER.getParameterizedType(StandardTypes.MAP, ImmutableList.of( TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature()))); } }
for (Type type : typeManager.getTypes()) { if (type.isComparable()) { if (!functions.canResolveOperator(HASH_CODE, BIGINT, ImmutableList.of(type))) { missingOperators.put(type, HASH_CODE); if (!functions.canResolveOperator(EQUAL, BOOLEAN, ImmutableList.of(type, type))) { missingOperators.put(type, EQUAL); if (!functions.canResolveOperator(NOT_EQUAL, BOOLEAN, ImmutableList.of(type, type))) { missingOperators.put(type, NOT_EQUAL); if (type.isOrderable()) { for (OperatorType operator : ImmutableList.of(LESS_THAN, LESS_THAN_OR_EQUAL, GREATER_THAN, GREATER_THAN_OR_EQUAL)) { if (!functions.canResolveOperator(operator, BOOLEAN, ImmutableList.of(type, type))) {
private boolean satisfiesCoercion(boolean allowCoercion, Type fromType, TypeSignature toTypeSignature) { if (allowCoercion) { return typeManager.canCoerce(fromType, typeManager.getType(toTypeSignature)); } else { return fromType.getTypeSignature().equals(toTypeSignature); } }
private void verifyTypeSignature(Symbol symbol, TypeSignature expected, TypeSignature actual) { // UNKNOWN should be considered as a wildcard type, which matches all the other types TypeManager typeManager = metadata.getTypeManager(); if (!actual.equals(UNKNOWN.getTypeSignature()) && !typeManager.isTypeOnlyCoercion(typeManager.getType(actual), typeManager.getType(expected))) { checkArgument(expected.equals(actual), "type of symbol '%s' is expected to be %s, but the actual type is %s", symbol, expected, actual); } } }
private CompatibilityAssertion assertThat(Type firstType, Type secondType) { Optional<Type> commonSuperType1 = typeRegistry.getCommonSuperType(firstType, secondType); Optional<Type> commonSuperType2 = typeRegistry.getCommonSuperType(secondType, firstType); assertEquals(commonSuperType1, commonSuperType2, "Expected getCommonSuperType to return the same result when invoked in either order"); boolean canCoerceFirstToSecond = typeRegistry.canCoerce(firstType, secondType); boolean canCoerceSecondToFirst = typeRegistry.canCoerce(secondType, firstType); return new CompatibilityAssertion(commonSuperType1, canCoerceFirstToSecond, canCoerceSecondToFirst); }
if (entry.getValue() != boundVariables.getTypeVariable(entry.getKey()).getJavaType()) { return Optional.empty(); if (returnNativeContainerType != Object.class && returnNativeContainerType != typeManager.getType(boundSignature.getReturnType()).getJavaType()) { return Optional.empty(); if (boundSignature.getArgumentTypes().get(i).getBase().equals(FunctionType.NAME)) { if (argumentNativeContainerTypes.get(i).isPresent()) { return Optional.empty(); Class<?> argumentType = typeManager.getType(boundSignature.getArgumentTypes().get(i)).getJavaType(); Class<?> argumentNativeContainerType = argumentNativeContainerTypes.get(i).get(); if (argumentNativeContainerType != Object.class && argumentNativeContainerType != argumentType) {
private static Function<HiveColumnHandle, ColumnMetadata> columnMetadataGetter(Table table, TypeManager typeManager) ImmutableList.Builder<String> columnNames = ImmutableList.builder(); table.getPartitionColumns().stream().map(Column::getName).forEach(columnNames::add); table.getDataColumns().stream().map(Column::getName).forEach(columnNames::add); List<String> allColumnNames = columnNames.build(); if (allColumnNames.size() > Sets.newHashSet(allColumnNames).size()) { throw new PrestoException(HIVE_INVALID_METADATA, ImmutableMap.Builder<String, Optional<String>> builder = ImmutableMap.builder(); for (Column field : concat(tableColumns, table.getPartitionColumns())) { if (field.getComment().isPresent() && !field.getComment().get().equals("from deserializer")) { builder.put(field.getName(), field.getComment()); builder.put(field.getName(), Optional.empty()); typeManager.getType(handle.getTypeSignature()), columnComment.get(handle.getName()).orElse(null), columnExtraInfo(handle.isPartitionKey()),
(!node.getEscape().isPresent() || node.getEscape().get() instanceof StringLiteral)) { if (node.getEscape().isPresent()) { escape = process(node.getEscape().get(), context); Type patternType = createVarcharType(unescapedPattern.length()); TypeManager typeManager = metadata.getTypeManager(); Optional<Type> commonSuperType = typeManager.getCommonSuperType(valueType, patternType); checkArgument(commonSuperType.isPresent(), "Missing super type when optimizing %s", node); Expression valueExpression = toExpression(value, valueType); Type superType = commonSuperType.get(); if (!valueType.equals(superType)) { valueExpression = new Cast(valueExpression, superType.getTypeSignature().toString(), false, typeManager.isTypeOnlyCoercion(valueType, superType)); patternExpression = new Cast(patternExpression, superType.getTypeSignature().toString(), false, typeManager.isTypeOnlyCoercion(patternType, superType));
.map(type -> { if (type instanceof RowType) { return type.getTypeParameters(); Type superType = fieldTypes.get(i); Optional<Type> commonSuperType = metadata.getTypeManager().getCommonSuperType(fieldType, superType); if (!commonSuperType.isPresent()) { throw new SemanticException(MISMATCHED_SET_COLUMN_TYPES, node, rowType); fieldTypes.set(i, commonSuperType.get()); Type actualType = analysis.getType(item); if (!actualType.equals(expectedType)) { analysis.addCoercion(item, expectedType, metadata.getTypeManager().isTypeOnlyCoercion(actualType, expectedType)); Type expectedType = fieldTypes.get(0); if (!actualType.equals(expectedType)) { analysis.addCoercion(row, expectedType, metadata.getTypeManager().isTypeOnlyCoercion(actualType, expectedType)); .map(valueType -> Field.newUnqualified(Optional.empty(), valueType)) .collect(toImmutableList());
return createAndAssignScope(relation, scope, relationScope.getRelationType().withOnlyVisibleFields()); }) .collect(toImmutableList()); Optional<Type> commonSuperType = metadata.getTypeManager().getCommonSuperType(outputFieldTypes[i], descFieldType); if (!commonSuperType.isPresent()) { throw new SemanticException( TYPE_MISMATCH, i + 1, setOperationName, outputFieldTypes[i].getDisplayName(), descFieldType.getDisplayName()); outputFieldTypes[i] = commonSuperType.get();
@Override public SolverReturnStatus update(BoundVariables.Builder bindings) { if (!bindings.containsTypeVariable(typeParameter)) { if (!satisfiesConstraints(actualType)) { return SolverReturnStatus.UNSOLVABLE; } bindings.setTypeVariable(typeParameter, actualType); return SolverReturnStatus.CHANGED; } Type originalType = bindings.getTypeVariable(typeParameter); Optional<Type> commonSuperType = typeManager.getCommonSuperType(originalType, actualType); if (!commonSuperType.isPresent()) { return SolverReturnStatus.UNSOLVABLE; } if (!satisfiesConstraints(commonSuperType.get())) { // This check must not be skipped even if commonSuperType is equal to originalType return SolverReturnStatus.UNSOLVABLE; } if (commonSuperType.get().equals(originalType)) { return SolverReturnStatus.UNCHANGED_SATISFIED; } bindings.setTypeVariable(typeParameter, commonSuperType.get()); return SolverReturnStatus.CHANGED; }
if (FunctionType.NAME.equals(formalTypeSignature.getBase())) { List<TypeSignature> formalTypeParameterTypeSignatures = formalTypeSignature.getTypeParametersAsTypeSignatures(); resultBuilder.add(new FunctionSolver( getLambdaArgumentTypeSignatures(formalTypeSignature), formalTypeParameterTypeSignatures.get(formalTypeParameterTypeSignatures.size() - 1), if (formalTypeSignature.getParameters().isEmpty()) { TypeVariableConstraint typeVariableConstraint = typeVariableConstraints.get(formalTypeSignature.getBase()); if (typeVariableConstraint == null) { return true; Type actualType = typeManager.getType(actualTypeSignatureProvider.getTypeSignature()); resultBuilder.add(new TypeParameterSolver( formalTypeSignature.getBase(), actualType, typeVariableConstraint.isComparableRequired(), typeVariableConstraint.isOrderableRequired(), Optional.ofNullable(typeVariableConstraint.getVariadicBound()))); return true; Type actualType = typeManager.getType(actualTypeSignatureProvider.getTypeSignature()); if (isTypeWithLiteralParameters(formalTypeSignature)) { resultBuilder.add(new TypeWithLiteralParametersSolver(formalTypeSignature, actualType)); return true; actualTypeParametersTypeSignatureProvider = fromTypes(actualType.getTypeParameters()); formalTypeParameterTypeSignatures.add(typeSignature.get());
.map(type -> { if (type instanceof RowType) { return type.getTypeParameters(); return ImmutableList.of(type); }) .collect(toImmutableSet()); Type superType = fieldTypes.get(i); Optional<Type> commonSuperType = metadata.getTypeManager().getCommonSuperType(fieldType, superType); if (!commonSuperType.isPresent()) { throw new SemanticException(MISMATCHED_SET_COLUMN_TYPES, node, Iterables.get(rowTypes, 1)); fieldTypes.set(i, commonSuperType.get()); .map(valueType -> Field.newUnqualified(Optional.empty(), valueType)) .collect(toImmutableList()));
Optional<ResolvedField> rightField = right.tryResolveField(column); if (!leftField.isPresent()) { throw new SemanticException(MISSING_ATTRIBUTE, column, "Column '%s' is missing from left side of join", column.getValue()); if (!rightField.isPresent()) { throw new SemanticException(MISSING_ATTRIBUTE, column, "Column '%s' is missing from right side of join", column.getValue()); metadata.getFunctionRegistry().resolveOperator(OperatorType.EQUAL, ImmutableList.of( leftField.get().getType(), rightField.get().getType())); Optional<Type> type = metadata.getTypeManager().getCommonSuperType(leftField.get().getType(), rightField.get().getType()); analysis.addTypes(ImmutableMap.of(NodeRef.of(column), type.get())); ImmutableList.Builder<Field> outputs = ImmutableList.builder(); outputs.addAll(joinFields); ImmutableList.Builder<Integer> leftFields = ImmutableList.builder(); for (int i = 0; i < left.getRelationType().getAllFieldCount(); i++) { if (!leftJoinFields.contains(i)) { outputs.add(left.getRelationType().getFieldByIndex(i)); leftFields.add(i);
private Type coerceToSingleType(StackableAstVisitorContext<Context> context, Node node, String message, Expression first, Expression second) { Type firstType = UNKNOWN; if (first != null) { firstType = process(first, context); } Type secondType = UNKNOWN; if (second != null) { secondType = process(second, context); } // coerce types if possible Optional<Type> superTypeOptional = typeManager.getCommonSuperType(firstType, secondType); if (superTypeOptional.isPresent() && typeManager.canCoerce(firstType, superTypeOptional.get()) && typeManager.canCoerce(secondType, superTypeOptional.get())) { Type superType = superTypeOptional.get(); if (!firstType.equals(superType)) { addOrReplaceExpressionCoercion(first, firstType, superType); } if (!secondType.equals(superType)) { addOrReplaceExpressionCoercion(second, secondType, superType); } return superType; } throw new SemanticException(TYPE_MISMATCH, node, message, firstType, secondType); }
public static Optional<List<Type>> toTypes(List<TypeSignatureProvider> typeSignatureProviders, TypeManager typeManager) { ImmutableList.Builder<Type> resultBuilder = ImmutableList.builder(); for (TypeSignatureProvider typeSignatureProvider : typeSignatureProviders) { if (typeSignatureProvider.hasDependency()) { return Optional.empty(); } resultBuilder.add(typeManager.getType(typeSignatureProvider.getTypeSignature())); } return Optional.of(resultBuilder.build()); }
if (!lambdaArgumentTypes.isPresent()) { return SolverReturnStatus.UNCHANGED_NOT_SATISFIED; if (!typeSignatureProvider.hasDependency()) { actualLambdaTypeSignature = typeSignatureProvider.getTypeSignature(); if (!FunctionType.NAME.equals(actualLambdaTypeSignature.getBase()) || !getLambdaArgumentTypeSignatures(actualLambdaTypeSignature).equals(toTypeSignatures(lambdaArgumentTypes.get()))) { return SolverReturnStatus.UNSOLVABLE; actualLambdaTypeSignature = typeSignatureProvider.getTypeSignature(lambdaArgumentTypes.get()); if (!FunctionType.NAME.equals(actualLambdaTypeSignature.getBase())) { return SolverReturnStatus.UNSOLVABLE; Type actualLambdaType = typeManager.getType(actualLambdaTypeSignature); Type actualReturnType = ((FunctionType) actualLambdaType).getReturnType(); ImmutableList.Builder<TypeConstraintSolver> constraintsBuilder = ImmutableList.builder(); if (!appendTypeRelationshipConstraintSolver(constraintsBuilder, formalLambdaReturnTypeSignature, new TypeSignatureProvider(actualReturnType.getTypeSignature()), false)) { return SolverReturnStatus.UNSOLVABLE; if (!appendConstraintSolvers(constraintsBuilder, formalLambdaReturnTypeSignature, new TypeSignatureProvider(actualReturnType.getTypeSignature()), allowCoercion)) { return SolverReturnStatus.UNSOLVABLE; for (TypeConstraintSolver constraint : constraintsBuilder.build()) { statusMerger.add(constraint.update(bindings)); if (statusMerger.getCurrent() == SolverReturnStatus.UNSOLVABLE) {
@Override protected Type visitFunctionCall(FunctionCall node, StackableAstVisitorContext<Context> context) for (SortItem sortItem : getSortItemsFromOrderBy(node.getWindow().get().getOrderBy())) { process(sortItem.getSortKey(), context); Type type = getExpressionType(sortItem.getSortKey()); if (!type.isOrderable()) { throw new SemanticException(TYPE_MISMATCH, node, "%s is not orderable, and therefore cannot be used in window function ORDER BY", type); return innerExpressionAnalyzer.analyze(expression, baseScope, context.getContext().expectingLambda(types)).getTypeSignature(); })); argumentTypesBuilder.add(new TypeSignatureProvider(process(expression, context).getTypeSignature())); ImmutableList<TypeSignatureProvider> argumentTypes = argumentTypesBuilder.build(); Signature function = resolveFunction(node, argumentTypes, functionRegistry); Type expectedType = typeManager.getType(function.getArgumentTypes().get(i)); if (argumentTypes.get(i).hasDependency()) { FunctionType expectedFunctionType = (FunctionType) expectedType; process(expression, new StackableAstVisitorContext<>(context.getContext().expectingLambda(expectedFunctionType.getArgumentTypes()))); Type actualType = typeManager.getType(argumentTypes.get(i).getTypeSignature()); coerceType(expression, actualType, expectedType, format("Function %s argument %d", function, i)); Type type = typeManager.getType(function.getReturnType()); return setExpressionType(node, type);