@Override public Type getType(TypeSignature signature) { return typeManager.getType(signature); }
public static RowType rowType(List<NamedTypeSignature> elementTypeSignatures) { return (RowType) TYPE_MANAGER.getParameterizedType( StandardTypes.ROW, ImmutableList.copyOf(elementTypeSignatures.stream() .map(TypeSignatureParameter::of) .collect(toList()))); }
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); }
Optional<Type> type = typeManager.coerceTypeBase(actualType, formalTypeSignature.getBase()); if (!type.isPresent()) { return SolverReturnStatus.UNSOLVABLE; Type originalType = typeManager.getType(new TypeSignature(formalTypeSignature.getBase(), originalTypeTypeParametersBuilder.build())); Optional<Type> commonSuperType = typeManager.getCommonSuperType(originalType, actualType); if (!commonSuperType.isPresent()) { return SolverReturnStatus.UNSOLVABLE;
@Override public Type createType(TypeManager typeManager, List<TypeParameter> parameters) { checkArgument(parameters.size() == 2, "Expected two parameters, got %s", parameters); TypeParameter firstParameter = parameters.get(0); TypeParameter secondParameter = parameters.get(1); checkArgument( firstParameter.getKind() == ParameterKind.TYPE && secondParameter.getKind() == ParameterKind.TYPE, "Expected key and type to be types, got %s", parameters); Type keyType = firstParameter.getType(); Type valueType = secondParameter.getType(); MethodHandle keyNativeEquals = typeManager.resolveOperator(OperatorType.EQUAL, ImmutableList.of(keyType, keyType)); MethodHandle keyBlockNativeEquals = compose(keyNativeEquals, nativeValueGetter(keyType)); MethodHandle keyBlockEquals = compose(keyNativeEquals, nativeValueGetter(keyType), nativeValueGetter(keyType)); MethodHandle keyNativeHashCode = typeManager.resolveOperator(OperatorType.HASH_CODE, ImmutableList.of(keyType)); MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(keyType)); return new MapType( keyType, valueType, keyBlockNativeEquals, keyBlockEquals, keyNativeHashCode, keyBlockHashCode); } }
private boolean typesMatchForInsert(Iterable<Type> tableTypes, Iterable<Type> queryTypes) { if (Iterables.size(tableTypes) != Iterables.size(queryTypes)) { return false; } Iterator<Type> tableTypesIterator = tableTypes.iterator(); Iterator<Type> queryTypesIterator = queryTypes.iterator(); while (tableTypesIterator.hasNext()) { Type tableType = tableTypesIterator.next(); Type queryType = queryTypesIterator.next(); if (!metadata.getTypeManager().canCoerce(queryType, tableType)) { return false; } } return true; }
@Test public void testCoerceTypeBase() { assertEquals(typeRegistry.coerceTypeBase(createDecimalType(21, 1), "decimal"), Optional.of(createDecimalType(21, 1))); assertEquals(typeRegistry.coerceTypeBase(BIGINT, "decimal"), Optional.of(createDecimalType(19, 0))); assertEquals(typeRegistry.coerceTypeBase(INTEGER, "decimal"), Optional.of(createDecimalType(10, 0))); assertEquals(typeRegistry.coerceTypeBase(TINYINT, "decimal"), Optional.of(createDecimalType(3, 0))); assertEquals(typeRegistry.coerceTypeBase(SMALLINT, "decimal"), Optional.of(createDecimalType(5, 0))); }
@Override public Type createType(TypeManager typeManager, List<TypeParameter> parameters) { checkArgument(parameters.size() == 2, "Expected two parameters, got %s", parameters); TypeParameter firstParameter = parameters.get(0); TypeParameter secondParameter = parameters.get(1); checkArgument( firstParameter.getKind() == ParameterKind.TYPE && secondParameter.getKind() == ParameterKind.TYPE, "Expected key and type to be types, got %s", parameters); Type keyType = firstParameter.getType(); Type valueType = secondParameter.getType(); MethodHandle keyNativeEquals = typeManager.resolveOperator(OperatorType.EQUAL, ImmutableList.of(keyType, keyType)); MethodHandle keyBlockNativeEquals = compose(keyNativeEquals, nativeValueGetter(keyType)); MethodHandle keyBlockEquals = compose(keyNativeEquals, nativeValueGetter(keyType), nativeValueGetter(keyType)); MethodHandle keyNativeHashCode = typeManager.resolveOperator(OperatorType.HASH_CODE, ImmutableList.of(keyType)); MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(keyType)); return new MapType( keyType, valueType, keyBlockNativeEquals, keyBlockEquals, keyNativeHashCode, keyBlockHashCode); } }
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); }
private boolean typesMatchForInsert(Iterable<Type> tableTypes, Iterable<Type> queryTypes) { if (Iterables.size(tableTypes) != Iterables.size(queryTypes)) { return false; } Iterator<Type> tableTypesIterator = tableTypes.iterator(); Iterator<Type> queryTypesIterator = queryTypes.iterator(); while (tableTypesIterator.hasNext()) { Type tableType = tableTypesIterator.next(); Type queryType = queryTypesIterator.next(); if (!metadata.getTypeManager().canCoerce(queryType, tableType)) { return false; } } return true; }
@Test public void testCoerceTypeBase() { assertEquals(typeRegistry.coerceTypeBase(createDecimalType(21, 1), "decimal"), Optional.of(createDecimalType(21, 1))); assertEquals(typeRegistry.coerceTypeBase(BIGINT, "decimal"), Optional.of(createDecimalType(19, 0))); assertEquals(typeRegistry.coerceTypeBase(INTEGER, "decimal"), Optional.of(createDecimalType(10, 0))); assertEquals(typeRegistry.coerceTypeBase(TINYINT, "decimal"), Optional.of(createDecimalType(3, 0))); assertEquals(typeRegistry.coerceTypeBase(SMALLINT, "decimal"), Optional.of(createDecimalType(5, 0))); }
public static Type resolveType(TypeSignature typeName, TypeManager typeManager) { return typeManager.getType(typeName); }
public static RowType rowType(List<NamedTypeSignature> elementTypeSignatures) { return (RowType) TYPE_MANAGER.getParameterizedType( StandardTypes.ROW, ImmutableList.copyOf(elementTypeSignatures.stream() .map(TypeSignatureParameter::of) .collect(toList()))); }
@Override public Block readBlock(BlockEncodingSerde blockEncodingSerde, SliceInput sliceInput) { Type keyType = TypeSerde.readType(typeManager, sliceInput); MethodHandle keyNativeEquals = typeManager.resolveOperator(OperatorType.EQUAL, asList(keyType, keyType)); MethodHandle keyBlockNativeEquals = compose(keyNativeEquals, nativeValueGetter(keyType)); MethodHandle keyNativeHashCode = typeManager.resolveOperator(OperatorType.HASH_CODE, singletonList(keyType)); Block keyBlock = blockEncodingSerde.readBlock(sliceInput); Block valueBlock = blockEncodingSerde.readBlock(sliceInput); int[] hashTable = new int[sliceInput.readInt()]; sliceInput.readBytes(wrappedIntArray(hashTable)); if (keyBlock.getPositionCount() != valueBlock.getPositionCount() || keyBlock.getPositionCount() * HASH_MULTIPLIER != hashTable.length) { throw new IllegalArgumentException( format("Deserialized SingleMapBlock violates invariants: key %d, value %d, hash %d", keyBlock.getPositionCount(), valueBlock.getPositionCount(), hashTable.length)); } return new SingleMapBlock(0, keyBlock.getPositionCount() * 2, keyBlock, valueBlock, hashTable, keyType, keyNativeHashCode, keyBlockNativeEquals); } }
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); }
private void coerceType(Expression expression, Type actualType, Type expectedType, String message) { if (!actualType.equals(expectedType)) { if (!typeManager.canCoerce(actualType, expectedType)) { throw new SemanticException(TYPE_MISMATCH, expression, message + " must evaluate to a %s (actual: %s)", expectedType, actualType); } addOrReplaceExpressionCoercion(expression, actualType, expectedType); } }
@Override public Type getType(TypeSignature signature) { return typeManager.getType(signature); }