/** * Gets the element type of the given array type. Does not validate that the given type is an array. * * @param type An array type * @return Element type of the array * @throws IndexOutOfBoundsException If type is not an array * @see Types#isArrayType */ public static Type getElementType(Type type) { return type.getTypeParameters().get(0); }
/** * Gets the key type of the given map type. Does not validate that the given type is a map. * * @param type A map type * @return Key type of the map * @throws IndexOutOfBoundsException If type is not a map * @see Types#isMapType */ public static Type getKeyType(Type type) { return type.getTypeParameters().get(0); }
/** * Gets the value type of the given map type. Does not validate that the given type is a map. * * @param type A map type * @return Value type of the map * @throws IndexOutOfBoundsException If type is not a map * @see Types#isMapType */ public static Type getValueType(Type type) { return type.getTypeParameters().get(1); } }
public ArrayOfRowsUnnester(Type elementType) { requireNonNull(elementType, "elementType is null"); checkArgument(elementType instanceof RowType, "elementType is not of RowType"); this.fieldTypes = ImmutableList.copyOf(elementType.getTypeParameters()); }
public static boolean containsType(Type type, Predicate<Type> predicate, Predicate<Type>... orPredicates) { for (Predicate<Type> orPredicate : orPredicates) { predicate = predicate.or(orPredicate); } if (predicate.test(type)) { return true; } return type.getTypeParameters().stream().anyMatch(predicate); } }
private static List<Type> getUnnestedTypes(List<Type> types, boolean isLegacyUnnest) { ImmutableList.Builder<Type> builder = ImmutableList.builder(); for (Type type : types) { checkArgument(type instanceof ArrayType || type instanceof MapType, "Can only unnest map and array types"); if (type instanceof ArrayType && !isLegacyUnnest && ((ArrayType) type).getElementType() instanceof RowType) { builder.addAll(((ArrayType) type).getElementType().getTypeParameters()); } else { builder.addAll(type.getTypeParameters()); } } return builder.build(); }
private boolean isSupportedType(Type type) { if (isSupportedPrimitive(type)) { return true; } if (type.getTypeSignature().getBase().equalsIgnoreCase(ARRAY)) { checkArgument(type.getTypeParameters().size() == 1, "expecting exactly one type parameter for array"); return isSupportedPrimitive(type.getTypeParameters().get(0)); } if (type.getTypeSignature().getBase().equalsIgnoreCase(MAP)) { List<Type> typeParameters = type.getTypeParameters(); checkArgument(typeParameters.size() == 2, "expecting exactly two type parameters for map"); checkArgument(typeParameters.get(0) instanceof VarcharType, "Unsupported column type '%s' for map key", typeParameters.get(0)); return isSupportedPrimitive(type.getTypeParameters().get(1)); } return false; }
@Override public Block toBlock(Type desiredType) { checkArgument(desiredType.getTypeParameters().size() == 1 && BIGINT.equals(desiredType.getTypeParameters().get(0)), "type doesn't match: %s", desiredType); int numberOfRecords = numberOfRecords(); return ArrayBlock.fromElementBlock( numberOfRecords, Optional.of(nulls == null ? new boolean[numberOfRecords] : nulls), calculateOffsets(sizes, nulls, numberOfRecords), values != null ? values.toBlock(BIGINT) : new LongArrayBlock(0, Optional.empty(), new long[] {})); }
@Override protected RowExpression visitLambdaExpression(LambdaExpression node, Void context) { RowExpression body = process(node.getBody(), context); Type type = getType(node); List<Type> typeParameters = type.getTypeParameters(); List<Type> argumentTypes = typeParameters.subList(0, typeParameters.size() - 1); List<String> argumentNames = node.getArguments().stream() .map(LambdaArgumentDeclaration::getName) .map(Identifier::getValue) .collect(toImmutableList()); return new LambdaDefinitionExpression(argumentTypes, argumentNames, body); }
private static Object decodeRecordReaderMap(Type type, Map<?, ?> map) { Type keyType = type.getTypeParameters().get(0); Type valueType = type.getTypeParameters().get(1); Map<Object, Object> newMap = new HashMap<>(); for (Entry<?, ?> entry : map.entrySet()) { newMap.put(decodeRecordReaderValue(keyType, entry.getKey()), decodeRecordReaderValue(valueType, entry.getValue())); } return newMap; }
private static List<Object> decodeRecordReaderList(Type type, List<?> list) { Type elementType = type.getTypeParameters().get(0); return list.stream() .map(element -> decodeRecordReaderValue(elementType, element)) .collect(toList()); }
private static List<Object> decodeRecordReaderList(Type type, List<?> list) { Type elementType = type.getTypeParameters().get(0); return list.stream() .map(element -> decodeRecordReaderValue(elementType, element)) .collect(toList()); }
private static Object decodeRecordReaderMap(Type type, Map<?, ?> map) { Type keyType = type.getTypeParameters().get(0); Type valueType = type.getTypeParameters().get(1); Map<Object, Object> newMap = new HashMap<>(); for (Entry<?, ?> entry : map.entrySet()) { newMap.put(decodeRecordReaderValue(keyType, entry.getKey()), decodeRecordReaderValue(valueType, entry.getValue())); } return newMap; }
private static List<Object> decodeRecordReaderStruct(Type type, List<?> fields) { List<Type> fieldTypes = type.getTypeParameters(); List<Object> newFields = new ArrayList<>(fields.size()); for (int i = 0; i < fields.size(); i++) { Type fieldType = fieldTypes.get(i); Object field = fields.get(i); newFields.add(decodeRecordReaderValue(fieldType, field)); } return newFields; }
@Override public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { checkArgument(arity == 1, "Expected arity to be 1"); Type fromType = boundVariables.getTypeVariable("F"); Type toType = boundVariables.getTypeVariable("T"); if (fromType.getTypeParameters().size() != toType.getTypeParameters().size()) { throw new PrestoException(StandardErrorCode.INVALID_FUNCTION_ARGUMENT, "the size of fromType and toType must match"); } Class<?> castOperatorClass = generateRowCast(fromType, toType, functionRegistry); MethodHandle methodHandle = methodHandle(castOperatorClass, "castRow", ConnectorSession.class, Block.class); return new ScalarFunctionImplementation( false, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle, isDeterministic()); }
private static List<Object> decodeRecordReaderStruct(Type type, List<?> fields) { List<Type> fieldTypes = type.getTypeParameters(); List<Object> newFields = new ArrayList<>(fields.size()); for (int i = 0; i < fields.size(); i++) { Type fieldType = fieldTypes.get(i); Object field = fields.get(i); newFields.add(decodeRecordReaderValue(fieldType, field)); } for (int j = fields.size(); j < fieldTypes.size(); j++) { newFields.add(null); } return newFields; }
private static boolean hasType(Type testType, Set<String> baseTypes) { String testBaseType = testType.getTypeSignature().getBase(); if (StandardTypes.ARRAY.equals(testBaseType)) { Type elementType = testType.getTypeParameters().get(0); return hasType(elementType, baseTypes); } if (StandardTypes.MAP.equals(testBaseType)) { Type keyType = testType.getTypeParameters().get(0); Type valueType = testType.getTypeParameters().get(1); return hasType(keyType, baseTypes) || hasType(valueType, baseTypes); } if (StandardTypes.ROW.equals(testBaseType)) { return testType.getTypeParameters().stream() .anyMatch(fieldType -> hasType(fieldType, baseTypes)); } return baseTypes.contains(testBaseType); }
private ColumnChunk readMap(GroupField field) throws IOException { List<Type> parameters = field.getType().getTypeParameters(); checkArgument(parameters.size() == 2, "Maps must have two type parameters, found %d", parameters.size()); Block[] blocks = new Block[parameters.size()]; ColumnChunk columnChunk = readColumnChunk(field.getChildren().get(0).get()); blocks[0] = columnChunk.getBlock(); blocks[1] = readColumnChunk(field.getChildren().get(1).get()).getBlock(); IntList offsets = new IntArrayList(); BooleanList valueIsNull = new BooleanArrayList(); calculateCollectionOffsets(field, offsets, valueIsNull, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels()); Block mapBlock = ((MapType) field.getType()).createBlockFromKeyValue(Optional.of(valueIsNull.toBooleanArray()), offsets.toIntArray(), blocks[0], blocks[1]); return new ColumnChunk(mapBlock, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels()); }
private ColumnChunk readArray(GroupField field) throws IOException { List<Type> parameters = field.getType().getTypeParameters(); checkArgument(parameters.size() == 1, "Arrays must have a single type parameter, found %d", parameters.size()); Field elementField = field.getChildren().get(0).get(); ColumnChunk columnChunk = readColumnChunk(elementField); IntList offsets = new IntArrayList(); BooleanList valueIsNull = new BooleanArrayList(); calculateCollectionOffsets(field, offsets, valueIsNull, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels()); Block arrayBlock = ArrayBlock.fromElementBlock(valueIsNull.size(), Optional.of(valueIsNull.toBooleanArray()), offsets.toIntArray(), columnChunk.getBlock()); return new ColumnChunk(arrayBlock, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels()); }
public MapCoercer(TypeManager typeManager, HiveType fromHiveType, HiveType toHiveType, BridgingRecordCursor bridgingRecordCursor) { requireNonNull(typeManager, "typeManage is null"); requireNonNull(fromHiveType, "fromHiveType is null"); requireNonNull(toHiveType, "toHiveType is null"); this.bridgingRecordCursor = requireNonNull(bridgingRecordCursor, "bridgingRecordCursor is null"); HiveType fromKeyHiveType = HiveType.valueOf(((MapTypeInfo) fromHiveType.getTypeInfo()).getMapKeyTypeInfo().getTypeName()); HiveType fromValueHiveType = HiveType.valueOf(((MapTypeInfo) fromHiveType.getTypeInfo()).getMapValueTypeInfo().getTypeName()); HiveType toKeyHiveType = HiveType.valueOf(((MapTypeInfo) toHiveType.getTypeInfo()).getMapKeyTypeInfo().getTypeName()); HiveType toValueHiveType = HiveType.valueOf(((MapTypeInfo) toHiveType.getTypeInfo()).getMapValueTypeInfo().getTypeName()); this.fromKeyValueTypes = fromHiveType.getType(typeManager).getTypeParameters(); this.toType = toHiveType.getType(typeManager); this.toKeyValueTypes = toType.getTypeParameters(); this.coercers = new Coercer[2]; coercers[0] = fromKeyHiveType.equals(toKeyHiveType) ? null : createCoercer(typeManager, fromKeyHiveType, toKeyHiveType, bridgingRecordCursor); coercers[1] = fromValueHiveType.equals(toValueHiveType) ? null : createCoercer(typeManager, fromValueHiveType, toValueHiveType, bridgingRecordCursor); this.pageBuilder = coercers[0] == null && coercers[1] == null ? null : new PageBuilder(ImmutableList.of(toType)); }