@Override public List<Type> getTypeParameters() { return ImmutableList.of(getKeyType(), getValueType()); }
public MapUnnester(MapType mapType, @Nullable Block mapBlock) { this.channelCount = 2; requireNonNull(mapType, "mapType is null"); this.keyType = mapType.getKeyType(); this.valueType = mapType.getValueType(); this.block = mapBlock; this.positionCount = mapBlock == null ? 0 : mapBlock.getPositionCount(); }
public static boolean canCastFromJson(Type type) { String baseType = type.getTypeSignature().getBase(); if (baseType.equals(StandardTypes.BOOLEAN) || baseType.equals(StandardTypes.BIGINT) || baseType.equals(StandardTypes.DOUBLE) || baseType.equals(StandardTypes.VARCHAR) || baseType.equals(StandardTypes.JSON)) { return true; } if (type instanceof ArrayType) { return canCastFromJson(((ArrayType) type).getElementType()); } if (type instanceof MapType) { return isValidJsonObjectKeyType(((MapType) type).getKeyType()) && canCastFromJson(((MapType) type).getValueType()); } return false; }
public static Block toMap(Type mapType, ConnectorSession connectorSession, Slice json) { try { Map<?, ?> map = (Map<?, ?>) stackRepresentationToObject(connectorSession, json, mapType); if (map == null) { return null; } Type keyType = ((MapType) mapType).getKeyType(); Type valueType = ((MapType) mapType).getValueType(); BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueType), new BlockBuilderStatus(), map.size() * 2); for (Map.Entry<?, ?> entry : map.entrySet()) { appendToBlockBuilder(keyType, entry.getKey(), blockBuilder); appendToBlockBuilder(valueType, entry.getValue(), blockBuilder); } return blockBuilder.build(); } catch (RuntimeException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to " + mapType, e); } } }
@Override protected RelationType visitUnnest(Unnest node, AnalysisContext context) { ImmutableList.Builder<Field> outputFields = ImmutableList.builder(); for (Expression expression : node.getExpressions()) { ExpressionAnalysis expressionAnalysis = analyzeExpression(expression, context.getLateralTupleDescriptor(), context); Type expressionType = expressionAnalysis.getType(expression); if (expressionType instanceof ArrayType) { outputFields.add(Field.newUnqualified(Optional.empty(), ((ArrayType) expressionType).getElementType())); } else if (expressionType instanceof MapType) { outputFields.add(Field.newUnqualified(Optional.empty(), ((MapType) expressionType).getKeyType())); outputFields.add(Field.newUnqualified(Optional.empty(), ((MapType) expressionType).getValueType())); } else { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Cannot unnest type: " + expressionType); } } if (node.isWithOrdinality()) { outputFields.add(Field.newUnqualified(Optional.empty(), BIGINT)); } RelationType descriptor = new RelationType(outputFields.build()); analysis.setOutputDescriptor(node, descriptor); return descriptor; }
while (parser.nextValue() != JsonToken.END_OBJECT) { Object key = mapKeyToObject(session, parser.getCurrentName(), ((MapType) type).getKeyType()); Object value = stackRepresentationToObjectHelper(session, parser, ((MapType) type).getValueType()); map.put(key, value);
} else if (type.getTypeSignature().getBase().equals(StandardTypes.MAP)) { final MapType mapType = (MapType) type; return "map<" + fromType(mapType.getKeyType()) + "," + fromType(mapType.getValueType()) + ">"; } else if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY)) { final String typeString = type.getTypeParameters().stream().map(this::fromType)
if (mapType.getValueType() != null && !UnknownType.UNKNOWN.equals(mapType.getValueType())) { schema = new LogicalSchema(); schema.addField(fromPrestoTypeToPigSchema(null, mapType.getValueType()));
private static <T> JsonCodec<T> getJsonCodecForType(Type type) { if (VarcharType.VARCHAR.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(String.class); } if (BooleanType.BOOLEAN.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Boolean.class); } if (BigintType.BIGINT.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Long.class); } if (DoubleType.DOUBLE.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Double.class); } if (type instanceof ArrayType) { Type elementType = ((ArrayType) type).getElementType(); return (JsonCodec<T>) JSON_CODEC_FACTORY.listJsonCodec(getJsonCodecForType(elementType)); } if (type instanceof MapType) { Type keyType = ((MapType) type).getKeyType(); Type valueType = ((MapType) type).getValueType(); return (JsonCodec<T>) JSON_CODEC_FACTORY.mapJsonCodec(getMapKeyType(keyType), getJsonCodecForType(valueType)); } throw new PrestoException(INVALID_SESSION_PROPERTY, format("Session property type %s is not supported", type)); }
public static Block createMap(MapType mapType, Block keyBlock, Block valueBlock) { BlockBuilder blockBuilder = new InterleavedBlockBuilder(mapType.getTypeParameters(), new BlockBuilderStatus(), keyBlock.getPositionCount() * 2); checkCondition(keyBlock.getPositionCount() == valueBlock.getPositionCount(), INVALID_FUNCTION_ARGUMENT, "Key and value arrays must be the same length"); for (int i = 0; i < keyBlock.getPositionCount(); i++) { if (keyBlock.isNull(i)) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null"); } mapType.getKeyType().appendTo(keyBlock, i, blockBuilder); mapType.getValueType().appendTo(valueBlock, i, blockBuilder); } return blockBuilder.build(); } }
Map<Object, Object> map = (Map<Object, Object>) value; Type keyType = ((MapType) type).getKeyType(); Type valueType = ((MapType) type).getValueType(); BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry(); for (Entry<Object, Object> entry : map.entrySet()) {
MapType mapType = (MapType) type; Type keyType = mapType.getKeyType(); Type valueType = mapType.getValueType(); Object keyNonNullValue = getNonNullValueForType(keyType); Object valueNonNullValue = getNonNullValueForType(valueType);